views:

115

answers:

3

So I'm trying to comprehend the source file for csv2rec in matplotlib.mlab. It is used to take a csv file and parse the data into certain formats. So it may take a string '234' and convert it to int. or take a date string and make it into python datetimes.

def get_converters(reader):

    converters = None
    for i, row in enumerate(reader):
        if i==0:
            converters = [mybool]*len(row)
        if checkrows and i>checkrows:
            break
        #print i, len(names), len(row)
        #print 'converters', zip(converters, row)
        for j, (name, item) in enumerate(zip(names, row)):
            func = converterd.get(j)
            if func is None:
                func = converterd.get(name)
            if func is None:
                #if not item.strip(): continue
                func = converters[j]
                if len(item.strip()):
                    func = get_func(name, item, func)
            else:
                # how should we handle custom converters and defaults?
                func = with_default_value(func, None)
            converters[j] = func
    return converters

My issue with this function is 'converters.' It starts off as None. Then later 'func = converters[j]' j I know is a number which is just created through enumeration. So it is looking for the corresponding converters item as indexed by j. But there is nothing in converters because it is None right? Unless python programs don't have to be read from top to bottom? In that case we get the func from the next two lines "if len(item.st....etc)" or from the 'else:' section. But, I just assumed it would have to be read from top to bottom.

I don't know if any of the other things are important so I just included the whole function. converterd is a dictionary mapping I believe that the user can provide as a parameter to find a converter automatically. checkrows is just a number provided by the user as a parameter in the beginning to check for validity. It is by default None. I'm still kind of a beginner, so just fyi. =)

Thanks everyone. This site is so helpful!

+1  A: 

Unless I'm missing something, on the first iteration "i" is 0, so the following is executed:

converters = [mybool]*len(row)

and that initializes "converters"

Joril
+2  A: 

Converters gets set again at the beginning of the loop with

if i==0:
  converters = [mybool]*len(row)

So after that it's not None anymore.

sepp2k
+1  A: 

First,

converters = None

sets an initial value for converters. This way, if the iteration doesn't happen (because readers might be empty) then when the function returns converters it will exist and have the value None.

If the iteration over readers happens, then converters is immediately reset to a more meaningful value in the first pass through the iteration (when i==0):

converters = [mybool]*len(row)
tom10