My first Python program

Hallvard B Furuseth h.b.furuseth at usit.uio.no
Tue Oct 12 22:14:52 CEST 2010


Seebs writes:
> http://github.com/wrpseudo/pseudo/blob/master/makewrappers

>        self.f = file(path, 'r')
>        if not self.f:
>            return None

No.  Failures tend to raise exceptions, not return error codes.
Except in os.path.exists() & co.

$ python
>>> open("nonesuch")   
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'nonesuch'
>>> 

So,
    import errno
    ...
    try:
        self.f = file(path, 'r')
    except IOError:
        if e.errno != errno.ENOENT: raise    # if you are picky
        return None

Nitpicks:

> if not section in self.sections:

  if section not in self.sections:

> list = map(lambda x: x.call(), self.args)
> return ', '.join(list)

  return ', '.join([x.call() for x in self.args])

> self.type, self.name = None, None

Actually you can write self.type = self.name = None,
though assignment statements are more limited than in C.
(And I think they're assigned left-to-right.)

>  match = re.match('(.*)\(\*([a-zA-Z0-9_]*)\)\((.*)\)', text)

Make a habit of using r'' for strings with lots of backslashes,
like regexps.

-- 
Hallvard



More information about the Python-list mailing list