File to dict
Glauco
00515879256 at fastwebnet.it
Fri Dec 7 12:24:36 EST 2007
david ha scritto:
> 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
yezzz!
you can use global or static
Gla
More information about the Python-list
mailing list