Re: Functional schmunctional...

2009-02-12 Thread Steve Holden
Hendrik van Rooyen wrote: > "Steve Holden" wrote: > >> Jeez, doesn't anyone read the fine manual any more? I hope this was just >> an academic exercise. >> > socket.inet_ntoa(struct.pack("!l", 10)) >> '59.154.202.0' >> Holden's rule: when it looks simple enough to be worth including i

Re: Functional schmunctional...

2009-02-11 Thread Hendrik van Rooyen
"Steve Holden" wrote: > Jeez, doesn't anyone read the fine manual any more? I hope this was just > an academic exercise. > > >>> socket.inet_ntoa(struct.pack("!l", 10)) > '59.154.202.0' > >>> > > Holden's rule: when it looks simple enough to be worth including in the > standard library

Re: Functional schmunctional...

2009-02-11 Thread wolfram . hinderer
On 10 Feb., 21:28, r0g wrote: > def inet2ip(n, l=[], c=4): >   if c==0: return ".".join(l) >   p = 256**( c-1 ) >   l.append( str(n/p) ) >   return inet2ip( n-(n/p)*p, l, c-1 ) > The results for 1 > iterations of each were as follows... > > 0.113744974136 seconds for old INET->IP method > 27

Re: Functional schmunctional...

2009-02-11 Thread Michele Simionato
On Feb 10, 9:28 pm, r0g wrote: > def ip2inet(a): >   li = a.split('.') >   assert len(li) == 4 or len(li) == 6 >   return reduce(add,[int(li[e])*(256**((len(li)-1)-e)) for e in > xrange(0,len(li))]) Aagh! Notice that functional programming is not about filter, map, reduce and other unreadable con

Re: Functional schmunctional...

2009-02-11 Thread Steve Holden
Terry Reedy wrote: > r0g wrote: > >> >> def inet2ip(n): >> p = (n/16777216) >> q = ((n-(p*16777216))/65536) >> r = ((n-((p*16777216)+(q*65536)))/256) >> s = ((n-((p*16777216)+(q*65536)+(r*256 >> return str(p)+"."+str(q)+"."+str(r)+"."+str(s) > > Beyond what other wrote: > To future-

Re: Functional schmunctional...

2009-02-10 Thread python
> This is a fantastically didactic newsgroup: you start off just musing about > , you end up learning python has , > brilliant :-) +1 !! Malcolm -- http://mail.python.org/mailman/listinfo/python-list

Re: Functional schmunctional...

2009-02-10 Thread r0g
bearophileh...@lycos.com wrote: > Here a small benchmark: > > def ip2inet01(a): # can't be used with 6 > li = a.split('.') Wow, thanks everybody for all the suggestions, v.interesting esp as I didn't even ask for any suggestions! This is a fantastically didactic newsgroup: you start off jus

Re: Functional schmunctional...

2009-02-10 Thread Terry Reedy
r0g wrote: def inet2ip(n): p = (n/16777216) q = ((n-(p*16777216))/65536) r = ((n-((p*16777216)+(q*65536)))/256) s = ((n-((p*16777216)+(q*65536)+(r*256 return str(p)+"."+str(q)+"."+str(r)+"."+str(s) Beyond what other wrote: To future-proof code, use // instead of / for integer di

Re: Functional schmunctional...

2009-02-10 Thread bearophileHUGS
Here a small benchmark: def ip2inet01(a): # can't be used with 6 li = a.split('.') assert len(li) == 4 a = int(li[0])*16777216 b = int(li[1])*65536 c = int(li[2])*256 d = int(li[3]) return a+b+c+d from itertools import count def ip2inet02(a): blocks = a.split('.')

Re: Functional schmunctional...

2009-02-10 Thread Scott David Daniels
For expressiveness, try something like: def ip2in(dotted_ip_addr): result = 0 assert dotted_ip_addr.count('.') in (3, 7) for chunk in dotted_ip_addr.split('.'): result = (result << 8) + int(chunk) return result def inet2ip(ip_number): assert 0 < ip_number < 1 << 48

Re: Functional schmunctional...

2009-02-10 Thread Paul Rubin
r0g writes: > def inet2ip(n): > p = (n/16777216) > q = ((n-(p*16777216))/65536) > r = ((n-((p*16777216)+(q*65536)))/256) > s = ((n-((p*16777216)+(q*65536)+(r*256 > return str(p)+"."+str(q)+"."+str(r)+"."+str(s) from struct import pack def inet2ip(n): xs = pack('L',n) return '.'.

Re: Functional schmunctional...

2009-02-10 Thread andrew cooke
r0g wrote: > def ip2inet(a): > li = a.split('.') > assert len(li) == 4 or len(li) == 6 > return reduce(add,[int(li[e])*(256**((len(li)-1)-e)) for e in > xrange(0,len(li))]) what a mess. i don't use this extreme a functional style in python (it's not really how the language is intended to be

Functional schmunctional...

2009-02-10 Thread r0g
I remember being forced to do a bit of functional programming in ML back at Uni in the mid 90, the lecturers were all in a froth about it and I must admit the code was elegant to look at. The problem was the dog slow performance for anything half practical, especially with recursion being the techn