Re: File to dict




On Fri, 2007-12-07 at 03:31 -0800, mrkafk@xxxxxxxxx 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

.



Relevant Pages

  • Re: File to dict
    ... file to Python dict: ... to transform files into dicts this way and I have quite a lot of such ... def generate_dict: ...
    (comp.lang.python)
  • Re: Summing a 2D list
    ... So, writing C in python, which has dictionnary as builtin type, should be ... def c_dict(users, votes): ... print "with dict", do ...
    (comp.lang.python)
  • RE: unintuitive dict timings
    ... > # Test of various methods to add an item to a list in a dict. ... > def ifelse: ... You're timing lots of things besides what you're trying to time here. ... sequence of Python statements executed in ifelse and get1, ...
    (comp.lang.python)
  • Re: Efficient Rank Ordering of Nested Lists
    ... lists may be accomplished via: ... def rankList: ... If you find the for-clause too rich in functionality, ... Agreed that dict() approach looks promising. ...
    (comp.lang.python)
  • Re: How can I use __setitem__ method of dict object?
    ... add support for the required subset of the dict interface. ... def _set_xA: ... raise AttributeError( ...
    (comp.lang.python)