Getting happier ;-), but wondering if I'm thinking pythonically

Brian Quinlan brian at sweetapp.com
Tue May 20 17:17:28 EDT 2003


I'm not going to answer whatever question that you asked but here are
some style comments:

1. print is a statement so parens are superfluous (e.g. line 30)
2. explicitly closing files is not usually necessary (e.g. line 35)
3. bare return statements are not necessary (e.g. 38)
4. the decision to exit an application should be made close to the
   top level (this is true of other languages with exception handling)
   (e.g. line 52)
5. you should generate more descriptive error messages (e.g. line 30)
6. utility functions (e.g. find_len) are normally placed near the top of

   the module
7. "str" is a bad name for an identifier since it masks a built-in 
   (e.g. line 164)
8. I'd write find_len (semantics changed) something like this:

def find_delimited_end(s, start_delimiter, end_delimiter, 
                       opening_count = 0):
    """find_delimited_end("{{5}}", "{", "}") => 4

    ...
    """

    for i in range(len(s)):
        c = s[i]

        if c == start_delimiter:
            opening_count += 1
        elif c == '}':
            if end_delimiter == 1:
                return i
            opening_count += 1

    raise ValueError, "unmatched delimiters found"

It is a bit shorter, more generic, removes an exception check and defers
an exception handling decision to a higher level. 

Cheers,
Brian 






More information about the Python-list mailing list