Tuesday, October 19, 2010

Yield, Generators & Iterables, non separable concepts in Python


Iterables

To understand what yield does, you must understand what generators are. And before generators come iterables. When you create a list, you can read its items one by one, and it's called iteration :
>>> mylist = [1, 2, 3]
>>> for i in mylist :
...    print(i)
1
2
3
Mylist is an iterable. When you use a comprehension list, you create a list, and so an iterable :
>>> mylist = [x*x for x in range(3)]
>>> for i in mylist :
...    print(i)
0
1
4
Everything you can use "for... in..." on is an iterable : lists, strings, files... These iterables are handy because you can read them as much as you wish, but you store all the values in memory and it's not always what you want when you have a lot of values.

Generators

Generators are iterables, but you can only read them once. It's because they do not store all the values in memory, they generate the values on the fly :
>>> mygenerator = (x*x for x in range(3))
>>> for i in mygenerator :
...    print(i)
0
1
4
It just the same except you used () instead of []. BUT, you can not perform for i in mygenerator a second time since generators can only be used once: they calculate 0, then forget about it and calculate 1 and ends calculating 4, one by one.

Yield

Yield is a keyword that is used like return, except the function will return a generator.
>>> def createGenerator() :
...    mylist = range(3)
...    for i in mylist :
...        yield i*i...
>>> mygenerator = createGenerator() # create a generator
>>> print(mygenerator) # mygenerator is an object !
<generator object createGenerator at 0xb7555c34>
>>> for i in mygenerator:
...     print(i)
0
1
4
Here it's a useless example, but it's handy when you know your function will return a huge set of values that you will only need to read once.
To master yield, you must understand that when you call the function, the code you have written in the function body does not run. The function only returns the generator object, this is bit tricky :-)

Monday, October 18, 2010

Show Line numbers in VIM

To show line numbers in vim while editing document, run the following command :

Press Esc and write

:set number

Saturday, October 16, 2010

Print on the same line, python

sys.stdout.write()

Convert a tuple to String

one_big_string = ''.join( xyz )
print one_big_string

reading from a pickled file, python

import pprint, pickle

pkl_file = open('data.pkl', 'rb')

data1 = pickle.load(pkl_file)
pprint.pprint(data1)

data2 = pickle.load(pkl_file)
pprint.pprint(data2)

pkl_file.close()

How long time does your code take to run in Python ?


def test():
    "Stupid test function"
    L = []
    for i in range(100):
        L.append(i)

if __name__=='__main__':
    from timeit import Timer
    t = Timer("test()", "from __main__ import test")
    print t.timeit()

Monday, October 11, 2010

Can't search through google chrome address bar

If this happens to you, then probably your default search engine is NOT chosen. So all you need to do to click on SettingPreferences, and then Basic, Default search.
The value of default search should be set as Google(or any other search engine you like).

Friday, September 17, 2010

process all the files under a directory & its sub-directories, C#

public void processDir(string sourceDir, string searchForText, string replaceWithText)
        {
            // Process the list of files found in the directory.
            string[] fileEntries = Directory.GetFiles(sourceDir);
            foreach (string fileName in fileEntries)
            {
                // do something with fileName
                Console.WriteLine(fileName);
            }
            // Recurse into subdirectories of this directory.
            string[] subdirEntries = Directory.GetDirectories(sourceDir);
            foreach (string subdir in subdirEntries)
            // Do not iterate through reparse points
            if ((File.GetAttributes(subdir) &
                FileAttributes.ReparsePoint) !=
                FileAttributes.ReparsePoint)
            {
                processDir(subdir, searchForText, replaceWithText);//recursive function
                recursionLvl += 1;
                Console.write ("Changing at deepness level : " + recursionLvl.ToString() + "\n");
            }