any chance for contracts and invariants in Python?

2013-02-14 Thread mrkafk
This PEP seems to be gathering dust: http://www.python.org/dev/peps/pep-0316/ I was thinking the other day, would contracts and invariants not be better than unit tests? That is, they could do what unit tests do and more, bc they run at execution time and not just at development time? -- htt

Re: dict invert - learning question

2008-05-03 Thread mrkafk
On 4 Maj, 01:27, [EMAIL PROTECTED] wrote: > >>> a={1:'a', 2:'b', 3:'c'} Oops, it should obviously be: >>> dict(zip(a.values(), a.keys())) {'a': 1, 'c': 3, 'b': 2} -- http://mail.python.org/mailman/listinfo/python-list

Re: dict invert - learning question

2008-05-03 Thread mrkafk
Assuming all the values are unique: >>> a={1:'a', 2:'b', 3:'c'} >>> dict(zip(a.keys(), a.values())) {1: 'a', 2: 'b', 3: 'c'} The problem is you obviously can't assume that in most cases. Still, zip() is very useful function. -- http://mail.python.org/mailman/listinfo/python-list

Error handling in SAX

2008-05-03 Thread mrkafk
(this is a repost, for it's been a while since I posted this text via Google Groups and it plain didn't appear on c.l.py - if it did appear anyway, apols) So I set out to learn handling three-letter-acronym files in Python, and SAX worked nicely until I encountered badly formed XMLs, like with bad

Daily WTF with XML, or error handling in SAX

2008-05-03 Thread mrkafk
So I set out to learn handling three-letter-acronym files in Python, and SAX worked nicely until I encountered badly formed XMLs, like with bad characters in it (well Unicode supposed to handle it all but apparently doesn't), using http://dchublist.com/hublist.xml.bz2 as example data, with goal to

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 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 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

File to dict

2007-12-07 Thread mrkafk
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

Descriptors and side effects

2007-11-04 Thread mrkafk
Hello everyone, I'm trying to do seemingly trivial thing with descriptors: have another attribute updated on dot access in object defined using descriptors. For example, let's take a simple example where you set an attribute s to a string and have another attribute l set automatically to its leng

Re: s.split() on multiple separators

2007-09-30 Thread mrkafk
> > c=' abcde abc cba fdsa bcd '.split() > > dels='ce ' > > for j in dels: > >cp=[] > >for i in xrange(0,len(c)-1): > > The "-1" looks like a bug; remember in Python 'stop' bounds > are exclusive. The indexes of c are simply xrange(len(c)). Yep. Just found it out, though this seems a bi

Re: s.split() on multiple separators

2007-09-30 Thread mrkafk
On 30 Wrz, 20:27, William James <[EMAIL PROTECTED]> wrote: > On Sep 30, 8:53 am, [EMAIL PROTECTED] wrote: > E:\Ruby>irb > irb(main):001:0> ' abcde abc cba fdsa bcd '.split(/[ce ]/) > => ["", "ab", "d", "", "ab", "", "", "ba", "fdsa", "b", "d"] That's acceptable only if you write perfect ruby-to-p

Re: s.split() on multiple separators

2007-09-30 Thread mrkafk
> > ['ab', 'd', '', 'ab', '', ''] > > Given your original string, I'm not sure how that would be the > expected result of "split c on the characters in dels". Oops, the inner loop should be: for i in xrange(0,len(c)): Now it works. > >>> c=' abcde abc cba fdsa bcd ' > >>> import re > >>

s.split() on multiple separators

2007-09-30 Thread mrkafk
Hello everyone, OK, so I want to split a string c into words using several different separators from a list (dels). I can do this the following C-like way: >>> c=' abcde abc cba fdsa bcd '.split() >>> dels='ce ' >>> for j in dels: cp=[] for i in xrange(0,len(c)-1):