File to dict

Glauco 00515879256 at fastwebnet.it
Fri Dec 7 10:46:56 EST 2007


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)





Glauco



More information about the Python-list mailing list