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
"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
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
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
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-
> 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
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
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
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('.')
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
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 '.'.
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
12 matches
Mail list logo