[Tutor] optimize code

Kent Johnson kent37 at tds.net
Tue Dec 30 16:04:00 CET 2008


On Tue, Dec 30, 2008 at 9:32 AM, Norman Khine <norman at khine.net> wrote:
> Hello,
> I have this piece of code which I would like to further streamline it.

There is no need to re-create the mapping at each level of if; you can
just add new key/value pairs. For example
                    if level2 is not None:
                        mapping['level2'] = level2

> What the code does is that it takes a form value and builds a dictionary

It seems that the you are really returning a string, or at least
building a string to pass to gettext(). I would have each if statement
just append to the string. That would consolidate the return
statements. For example, if value is the string being built,
                    if level2 is not None:
                        value = '%s: %s' % (value, level2)

Kent


More information about the Tutor mailing list