newb comment request

Peter Otten __peter__ at web.de
Thu Nov 27 04:05:09 EST 2003


Alexandre wrote:

> Hi,
> 
> Im a newb to dev and python...  my first sefl assigned mission was to read
> a pickled file containing a list with DB like data and convert this to
> MySQL... So i wrote my first module which reads this pickled file and
> writes an XML file with list of tables and fields (... next step will the
> module who creates the tables according to details found in the XML file).
> 
> If anyone has some minutes to spare, suggestions and comments would be
> verry much appreciated to help me make my next modules better.. and not
> starting with bad habit :)

I would suggest that you repost the script without the excessive comments.
Most programmers find Python very readable, your comments actually make it
harder to parse for the human eye.
Also, make it work if you can, or point to the actual errors that you cannot
fix yourself. Provide actual test data in your post instead of the
unpickling code, so that others can easily reproduce your errors.
Only then you should ask for improvements. 

Random remarks:

Object oriented programming is about programming against interfaces, so
exessive type checking is a strong hint to design errors.

Avoid using global variables in your functions; rather pass them explicitly
as arguments.

Where is valuesD introduced?

"%s" % str(1.23) 
is the same as 
"%s" % 1.23

type(value) is not types.NoneType
is the same as
value is not None

Module level code is habitually wrapped like so:

if __name__ == "__main__":
    infile = open('cached-objects-Python-pickled-sample', 'rb')
    _data = pickle.load(infile)
    infile.close()
    # process _data...

That way, you can import the module as well as use it as a standalone
script.

I'm sure, there is more, but then again, clean up the comments, try to make
it work, and then repost.

Peter




More information about the Python-list mailing list