Re: File to dict

2007-12-08 Thread Jeremy C B Nicoll
Chris <[EMAIL PROTECTED]> wrote: > For the first one you are parsing the entire file everytime you want > to lookup just one domain... Is the file sorted? If so wouldn't it be easier either to read the whole thing and then binary-chop search it, or if the file is vast to use seek creatively to b

Re: File to dict

2007-12-07 Thread J. Clifford Dyer
On Fri, 2007-12-07 at 03:31 -0800, [EMAIL PROTECTED] 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.ls

RE: File to dict

2007-12-07 Thread Joe Goldthwaite
Duncan Booth wrote: >for item in list: >if item == 'searched.domain': >return item... [EMAIL PROTECTED] wrote: >Sure, but I have two options here, none of them nice: either "write C >in Python" or do it inefficient and still elaborate way. I don't understand your point at all. How

Re: File to dict

2007-12-07 Thread mrkafk
Glauco wrote: > 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) Neat solution! It just needs small correction for empty or ba

Re: File to dict

2007-12-07 Thread Glauco
david ha scritto: > On Fri, 07 Dec 2007 16:46:56 +0100, Glauco wrote: > >> [EMAIL PROTECTED] 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

Re: File to dict

2007-12-07 Thread david
On Fri, 07 Dec 2007 16:46:56 +0100, Glauco wrote: > [EMAIL PROTECTED] 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').readline

Re: File to dict

2007-12-07 Thread Neil Cerutti
On 2007-12-07, Duncan Booth <[EMAIL PROTECTED]> wrote: > Neil Cerutti <[EMAIL PROTECTED]> wrote: > >> On 2007-12-07, Duncan Booth <[EMAIL PROTECTED]> wrote: >>> from __future__ import with_statement >>> >>> def loaddomainowners(domain): >>> with open('/etc/virtual/domainowners','r') as infile:

Re: File to dict

2007-12-07 Thread Glauco
[EMAIL PROTECTED] 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

Re: File to dict

2007-12-07 Thread Duncan Booth
Neil Cerutti <[EMAIL PROTECTED]> wrote: > On 2007-12-07, Duncan Booth <[EMAIL PROTECTED]> wrote: >> from __future__ import with_statement >> >> def loaddomainowners(domain): >> with open('/etc/virtual/domainowners','r') as infile: > > I've been thinking I have to use contextlib.closing for >

Re: File to dict

2007-12-07 Thread Neil Cerutti
On 2007-12-07, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] a écrit : >> >>> The csv module is your friend. >> >> (slapping forehead) why the Holy Grail didn't I think about this? > > If that can make you feel better, a few years ago, I spent two > days writing my own (Squa

Re: File to dict

2007-12-07 Thread Neil Cerutti
On 2007-12-07, Duncan Booth <[EMAIL PROTECTED]> wrote: > from __future__ import with_statement > > def loaddomainowners(domain): > with open('/etc/virtual/domainowners','r') as infile: I've been thinking I have to use contextlib.closing for auto-closing files. Is that not so? -- Neil Cerutti

Re: File to dict

2007-12-07 Thread mrkafk
> >>> def shelper(line): > ... return x.replace(' ','').strip('\n').split(':',1) Argh, typo, should be def shelper(x) of course. -- http://mail.python.org/mailman/listinfo/python-list

Re: File to dict

2007-12-07 Thread mrkafk
> I guess Duncan's point wasn't the construction of the dictionary but the > throw it away part. If you don't keep it, the loop above is even more > efficient than building a dictionary with *all* lines of the file, just to > pick one value afterwards. Sure, but I have two options here, none of

Re: File to dict

2007-12-07 Thread Duncan Booth
Matt Nordhoff <[EMAIL PROTECTED]> wrote: > Using two list comprehensions mean you construct two lists, which sucks > if it's a large file. Only if it is very large. You aren't duplicating the data except for entries with whitespace round them. If there isn't a lot of whitespace then the extra o

Re: File to dict

2007-12-07 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit : > >> The csv module is your friend. > > (slapping forehead) why the Holy Grail didn't I think about this? If that can make you feel better, a few years ago, I spent two days writing my own (SquaredWheel(tm) of course) csv reader/writer... before realizing there was

Re: File to dict

2007-12-07 Thread Marc 'BlackJack' Rintsch
On Fri, 07 Dec 2007 04:44:25 -0800, mrkafk wrote: > Duncan Booth wrote: >> But why do you construct a dict from that input data simply to throw it >> away? > > Because comparing strings for equality in a loop is writing C in > Python, and that's exactly what I'm trying to unlearn. > > The proper

Re: File to dict

2007-12-07 Thread mrkafk
> The csv module is your friend. (slapping forehead) why the Holy Grail didn't I think about this? That should be much simpler than using SimpleParse or SPARK. Thx Bruno & everyone. -- http://mail.python.org/mailman/listinfo/python-list

Re: File to dict

2007-12-07 Thread mrkafk
Duncan Booth wrote: > Just some minor points without changing the basis of what you have done > here: All good points, thanks. Phew, there's nothing like peer review for your code... > But why do you construct a dict from that input data simply to throw it > away? Because comparing strings for

Re: File to dict

2007-12-07 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit : > Hello everyone, (snip) > Say, I would like to transform a file containing entries like > the following into a list of lists with doublecolon treated as > separators, i.e. this: > > tm:$1$$:1010:6::/home/owner1/imap/domain1.tld/tm:/sbin/nologin > > would get

Re: File to dict

2007-12-07 Thread Matt Nordhoff
Chris wrote: > Ta Matt, wasn't paying attention to what I typed. :) > And didn't know that about .get() and not having to declare the > global. > Thanks for my mandatory new thing for the day ;) :-) -- -- http://mail.python.org/mailman/listinfo/python-list

Re: File to dict

2007-12-07 Thread Chris
Ta Matt, wasn't paying attention to what I typed. :) And didn't know that about .get() and not having to declare the global. Thanks for my mandatory new thing for the day ;) -- http://mail.python.org/mailman/listinfo/python-list

Re: File to dict

2007-12-07 Thread Matt Nordhoff
Duncan Booth wrote: > Just some minor points without changing the basis of what you have done > here: > > Don't bother with 'readlines', file objects are directly iterable. > Why are you calling both lstrip and rstrip? The strip method strips > whitespace from both ends for you. > > It is usual

Re: File to dict

2007-12-07 Thread Matt Nordhoff
Chris wrote: > For the first one you are parsing the entire file everytime you want > to lookup just one domain. If it is something reused several times > during your code execute you could think of rather storing it so it's > just a simple lookup away, for eg. > > _domain_dict = dict() > def gen

Re: File to dict

2007-12-07 Thread Duncan Booth
[EMAIL PROTECTED] wrote: > 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 l

Re: File to dict

2007-12-07 Thread Chris
On Dec 7, 1:31 pm, [EMAIL PROTECTED] 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 i