Search the web
Sign In
New User? Sign Up
python-list · Python List
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Dictionary that uses regular expressions   Message List  
Reply | Forward Message #106132 of 122067 |
Re: Dictionary that uses regular expressions


"Erik Lechak" <prochak@...> wrote in message
news:1f0bdf30.0308202049.7d251469@....
> Hello all,
>
> I wrote the code below. It is simply a dictionary that uses regular
> expressions to match keys. A quick look at _test() will give you an
> example.
>
> Is there a module that already does this?

Not that I know of

> Is there a way and would it
> be better to use list comprehension? (using python 2.3)

List comp builds a list. Your __getitem__(s,key) code builds an
ReDict, not a list. If you extended your initializer to initialize
from a list of items (by passing the list of items on to
dict.__init__), then you could build such a list with a l.c. and
initialize your return ReDict from that. IE (untested, obviously, and
it's late...)

return ReDict( [item for item in self.items if
re.search(key,item[0]) ] )

If you were always (or even almost always) matching by re's, you might
as well just use a list of items (key,value). If you really must mix
normal and re retrieval, but only after the dict is built and stable,
then keeping items() as an attribute rather than re-extracting it for
each re access would be faster.

Terry J. Reedy


--
http://mail.python.org/mailman/listinfo/python-list



Thu Aug 21, 2003 5:17 am

tjreedy@...
Send Email Send Email

Forward
Message #106132 of 122067 |
Expand Messages Author Sort by Date

Hello all, I wrote the code below. It is simply a dictionary that uses regular expressions to match keys. A quick look at _test() will give you an example. ...
Erik Lechak
prochak@...
Send Email
Aug 21, 2003
4:54 am

"Erik Lechak" <prochak@...> wrote in message news:1f0bdf30.0308202049.7d251469@.... ... Google may prove me wrong, but this looks like...
Raymond Hettinger
vze4rx4y@...
Send Email
Aug 21, 2003
5:14 am

... Some months ago I started rewriting the C regexps module to allow for matching pure-python string types[1]. The python class would just have to define a...
Jack Diederich
jack@...
Send Email
Aug 26, 2003
2:45 pm

"Erik Lechak" <prochak@...> wrote in message news:1f0bdf30.0308202049.7d251469@.... ... Not that I know of ... List comp builds a...
Terry Reedy
tjreedy@...
Send Email
Aug 21, 2003
5:27 am

I would leave the __getitem__() method alone and just add a filterMatches(self, regexp) returning a ReDict or findMatches(self, regexp) returning a...
Peter Otten
__peter__@...
Send Email
Aug 21, 2003
7:49 am

Hey, cool code, I wrote a dictionary-keyed dictionary a while back to get something like the same functionality, basically I wanted an n-dimensional database...
Corey Coughlin
corey.coughlin@...
Send Email
Sep 5, 2003
2:21 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help