[Tutor] Sets and Python; new shiny tool syndrome?
Liam Clarke
ml.cyresse at gmail.com
Wed Dec 28 04:53:54 CET 2005
On 12/28/05, Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:
>
> > I just tried out sets for the first time, and I'm finding multiple uses
> > for them, particularly for replacing and simplifying what would normally
> > be one or two list comprehensions i.e.
> >
> > def parseKws(self, kw_data):
> > ignoreSet = set(['import', 'from', 'as', ' ', ''])
> > kws = set([])
> > for line in kw_data:
> > line = line.rstrip("\n")
> > if "," in line: line = line.replace(",", " ")
> > if ";" in line: line = line.replace(";", " ")
>
> Hi Liam,
>
> Quick comment: the two if statements there can be simplified by just doing
> the replacement straight-out:
>
> #############################
> line = line.replace(",", " ")
> line = line.replace(";", " ")
> #############################
>
> The reason is that if those characters aren't present, no harm is done.
>
>
> But looking at the code, I'm curious: it looks like you're trying to
> tokenize the keywords out of Python source? If so, you may want to look
> at the 'tokenize' module:
>
> http://www.python.org/doc/lib/module-tokenize.html
>
> as it'll handle some of the especially tricky cases like handling string
> literals.
Thanks Danny, I'm writing a little database to stash my code snippets
in, and my thinking tends to go along the lines of "That code I did
that use imaplib..." so I'm linking module names to the stored files;
that said, I'll use tokenizer next time I try and write a wxPython
frontend for IronPython and save myself a lot of grief!
> > However, I'm reminded of the joke about you can tell what chapter
> > someone reading the Patterns book by the Gang of Four is up to by what
> > new pattern they're trying to use the next day, no matter the problem.
> >
> > Are there any drawbacks to sets that I need to watch out for?
>
> Use them when they're appropriate, and don't use them when they're not.
> *grin*
>
> It really is problem sensitive: if order matters, then sets probably
> aren't appropriate. But sets are such a pervasive concept that few
> problems don't provide opportunities to take advantage of them.
Good to know; I'm very glad that they're part of Python. They're
making my job a whole lot simpler.
> Happy holidays to you!
And likewise. :)
More information about the Tutor
mailing list