Linking Dictionary Values

Maan Hamze mmhamze at pleiades.net
Sat Aug 25 14:25:14 EDT 2001


Interesting.  Thanks!  This is added to my archive :)
Maan

"Jesse F. W" <jessefw at loop.com> wrote in message
news:mailman.998717078.21823.python-list at python.org...
> Greetings to all you large snake fanciers,
> Maan M. Hamze wrote:
> > Is it possible to link Dictionary values of each key into a text file?
> This is probably more tricky and cute than you really need, but it is
> so tricky and cute I could not resist posting it on the list.
>
> In Python 2.2 and later...
>
> import types, os
>
> class FileDict(dictionary):
> def __getitem__(self, key):
> value=dictionary.__getitem__(self, key)
> if os.access(value, os.R_OK):
> return open(value).read()
> raise ValueError, 'Value is not a readable file!'
>
> class FunctionDict(dictionary):
> def __getitem__(self, key):
> return dictionary.__getitem__(self, key)()
>
> class FileAndFunctionDict(dictionary):
> def __getitem__(self, key):
> value=dictionary.__getitem__(self, key)
> if type(value) == types.FunctionType:
> return value()
> if os.access(value, os.R_OK):
> return open(value).read()
> raise ValueError, 'Value is not a readable file!'
>
> I only downloaded Python 2.2 _today_ and already I am using
> the class/type unification features!<0.5 wink>
> >  So the value of a key is a text file somewhere on the PC.  And if so,
> > how do I instruct Python to open the text file and print into screen
> > when the key of the text file is used? Also, is it possible to link a
> > value of a Dictionary key into a Python function/procedure?  So, if
> > the key is encountered, a way for the functionprocedure to be run.
> > Maan
>
>
>
>





More information about the Python-list mailing list