File to dict

J. Clifford Dyer jcd at sdf.lonestar.org
Fri Dec 7 22:12:08 EST 2007


On Fri, 2007-12-07 at 03:31 -0800, mrkafk at gmail.com wrote: 
> 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]
> 
> The /etc/virtual/domainowners file contains double-colon separated
> entries:
> domain1.tld: owner1
> domain2.tld: own2
> domain3.another: somebody
> ...
> 
> Now, the above lookupdmo function works. However, it's rather tedious
> to transform files into dicts this way and I have quite a lot of such
> files to transform (like custom 'passwd' files for virtual email
> accounts etc).

Don't do more lookup than you have to to get the answer you need.

(untested code)

class DictFromFile(object):
    def __init__(self, filename):
        self.f = open(filename)
        self.d = dict()

    def __getitem__(self, key):
        while key not in self.d:
            try:
                k, v = self._retrieve_next()
                self.d[k] = v
                if k == key:
    break
            except StopIteration:
                break
        return self.d[key]

    def _retrieve_next(self):
        line = self.f.next()
        k, v = line.split(':',1)
        return k.strip(), v.strip()


This will act like a dictionary, but only look as far into the file as
it needs to.  If you've got a 10,000 line file, and all your results
come out of the first 10 lines, this will save you some processing.
There's more operator overloading you could do, of course.

Cheers,
Cliff




More information about the Python-list mailing list