File to dict
david
asdrubale at gnx.it
Fri Dec 7 11:29:00 EST 2007
On Fri, 07 Dec 2007 16:46:56 +0100, Glauco wrote:
> mrkafk at gmail.com ha scritto:
>> Hello everyone,
>>
>> I have written this small utility function for transforming legacy file
>> to Python dict:
>>
>>
>> def lookupdmo(domain):
>> lines = open('/etc/virtual/domainowners','r').readlines() lines
>> = [ [y.lstrip().rstrip() for y in x.split(':')] for x in
>> lines]
>> lines = [ x for x in lines if len(x) == 2 ] d = dict()
>> for line in lines:
>> d[line[0]]=line[1]
>> return d[domain]
>>
>>
> cache = None
>
> def lookup( domain ):
> if not cache:
> cache = dict( [map( lambda x: x.strip(), x.split(':')) for x in
> open('/etc/virtual/domainowners','r').readlines()])
> return cache.get(domain)
>
>>> lookup(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in lookup
UnboundLocalError: local variable 'cache' referenced before assignment
You miss the:
def lookup(domain):
global cache
...
bye
More information about the Python-list
mailing list