Searching for the best scripting language,
David Eppstein
eppstein at ics.uci.edu
Tue Jun 15 12:53:12 EDT 2004
In article <60dfb6f6.0406150827.6dcc5ef5 at posting.google.com>,
imbosol at aerojockey.com (Carl Banks) wrote:
> > > import glob
> > > import os
> > > import re
> > >
> > > f = {}
> > > for pattern in ("*.rar.*","*.r[0-9][0-9].*"):
> > > for listing in glob.glob(prefix+pattern):
> > > f[listing] = None
> > > for filename in f:
> > > os.system("cat %s.* > %s" % (sesc(filename),sesc(filename)))
> > >
> > >
> > > I don't know what sesc is. I assume he had defined it elsewhere,
> > > because he said this was only part of a script he wrote (and that's
> > > what scares me--I can understand a throwaway one-liner looking like
> > > this, but not a line in a script).
> >
> > As long as we're cleaning up code, how about
> >
> > import glob, os, sets
> > f = Set()
> > for pattern in ("*.rar.*","*.r[0-9][0-9].*"):
> > f.update(glob.glob(prefix+pattern))
> > for filename in f:
> > os.system("cat %s.* > %s" % (sesc(filename),sesc(filename)))
> >
> > Now it's not even much longer than the original unreadable mess...
>
> I just noticed that I mistakenly left out the regexp in my clean code.
> Where I had f[listing] = None, I should have
> f[re.sub(r"\.\d+[\d.-]*$","",listing)] = None, or an extra varible.
I was wondering where the import re was supposed to be used...
so the line
f.update(glob.glob(prefix+pattern))
should become
for listing in glob.glob(prefix+pattern):
f.add(re.sub(r"\.\d+[\d.-]*$","",listing)
I guess? Not so different from what you posted, after that change...
As long as we're admitting to mistakes in code, I should have used
sets.Set instead of just Set in the second line.
--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
More information about the Python-list
mailing list