how best to split into singleton and sequence

2005-10-18 Thread Randy Bush
>>> l = []
>>> s = 'a|b'
>>> t, l = s.split('|')
>>> t
'a'
>>> l
'b'
>>> s = 'a|b|c|d'
>>> t, l = s.split('|')
Traceback (most recent call last):
  File "", line 1, in ?
ValueError: too many values to unpack
>>> 

so, i imagine what is happening is the lhs, t,l, is really
(t, (l)), i.e. only two items.

so how should i have done this readably and simply?

randy

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OpenSource documentation problems

2005-09-01 Thread Randy Bush
> I'm very sorry to say, that the Python doc is one of the worst possible
> in the industry.

you are entitled to a full refund

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Arguement error

2005-08-31 Thread Randy Bush
> I am wondering why I am getting this error. when I try to run a script. 
> TypeError: translate() takes at most 3 arguments (10 given)
> but the thing is the method translate actually accepts 10 arguements. 

without code, how is anyone to know?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: trictionary?

2005-08-29 Thread Randy Bush
> So I'm going to try to pump you for a little more information here.  Is 
> your goal to count, for each week, how many times it's "full" and how 
> many times it's "not full"?  What do you use the counts for?  What does 
> "full" mean?  Is it always a 0 or 1?  What's the importance of the 
> output formatting?

'full' is boolean.  it says whether a particular bgp announcement
was for the entire ip address allocation, or is a longer prefix.
e.g., if an allocation was for 666.42.0.0/16 and we heard a bgp
announcement for 666.42.1.0/24 that is !full, while an announcement
for the prefix 666.42.0.0/16 is full.

you asked :-)

>  for start, end, AS, full in heard:
>  week = int((start-startDate)/aWeek)
>  if week in bin:
>  bin[week][not full] += 1
>  else:
>  # I'm assuming "full" takes the values 0 or 1
>  # but if not, you can coerce it with bool()
>  bin[week] = [full, int(not full)]

hmmm.  this also reads well.

as an old pascal and modula-2 bondage and discipline type, i gotta
say is it a breath of fresh air to be in a language and community
which care about how code reads more than how clever it is.

randy

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: trictionary?

2005-08-29 Thread Randy Bush
> bin = {}
> for start, end, AS, full in heard:
>week = int((start-startDate)/aWeek)
>counters = bin.setdefault(week, [0, 0])
>if full:
>   counters[0] += 1
>else:
>   counters[1] += 1

yes!  thanks!

> Using an idea you used earlier, you could get smaller code by saying:
> for start, end, AS, full in heard:
>week = int((start-startDate)/aWeek)
>counters = bin.setdefault(week, [0, 0])
>counters[not full] += 1
> Or
> for start, end, AS, full in heard:
>week = int((start-startDate)/aWeek)
>bin.setdefault(week, [0, 0])[not full] += 1
> Or even
> for start, end, AS, full in heard:
>   bin.setdefault(int((start-startDate)/aWeek), [0, 0])[not full] += 1

as you say, too clever.

> Using lists to represent structs is perfectly fine if the list doesn't 
> live longer than about one screen of code.

i can definitely see that.  in last weeks installment, i buried a
complex trinary tree in a class.

thanks for the advice!

randy

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: trictionary?

2005-08-28 Thread Randy Bush
>> bin = {}
>> for whatever:
>>for [a, b] in foo:
>>x = 42 - a
>>if bin.has_key(x):
>>   bin[x.b] += 1
>>else:
>>   bin[x.b] = 1
>>   bin[x.not b] = 0
>> for x, y, z in bin.iteritems():
>>print x, y, z
>> 
>> should the dict value become a two element list, or is there a
>> cleaner way to do this?
> It would probably help if you explained what the real problem is
> you're trying to solve.

actually, that code fragment was meant to do that.  it's pretty much
what i needed to do at that point, just the variable names made
simple.

> Using a two element list to store a pair of counts has a bad code
> smell to me.

exactly.  which is why i was asking.

> That said, you could write your code something like:
>  bin = {}
>  for whatever:
> # NOTE: brackets are unnecessary
> for a, b in foo:
> x = 42 - a
># NOTE: 'in' is generally faster than has_key()
> if x in bin
>bin[x][0] += 1
> else:
>   bin[x] = [1, 0]
>  # NOTE: extra parens necessary to unpack count list
>  for x, (y, z) in bin.iteritems():
> print x, y, z

so, to do this using the real names, it looks like

   for [start, end, AS, full] in heard:
  week = int((start-startDate)/aWeek)
  if week in bin:
 if full:
bin[week][0] += 1
 else:
bin[week][1] += 1
  else:
 if full:
bin[week] = [1, 0]
 else:
bin[week] = [0, 1]
   ...
   for i, (j, k) in bin.iteritems():
  if j == 0:
 print str(i) + ",," + str(k)
  elif k == 0:
 print str(i) + "," + str(j)
  else:
 print str(i) + "," + str(j) + "," + str(k)

which is still pretty darned grotty and unexpressive.  of course,
i could be a bit more obscure and do

  if week in bin:
 bin[week][not full] += 1
  else:
 bin[week] = [ full, not full ]

except i probably have to coerce the types or something.  less
code but less obvious.

randy

-- 
http://mail.python.org/mailman/listinfo/python-list


trictionary?

2005-08-28 Thread Randy Bush
i have some code which looks kinda like 

bin = {}
for whatever:
   for [a, b] in foo:
  x = 42 - a
  y = 42 - b
  if bin.has_key(x):
 bin[x] += 1
  else:
 bin[x] = 1
for i, j in bin.iteritems():
   print i, j

now i want to add a second count column, kinda like

bin = {}
for whatever:
   for [a, b] in foo:
  x = 42 - a
  if bin.has_key(x):
 bin[x.b] += 1
  else:
 bin[x.b] = 1
 bin[x.not b] = 0
for x, y, z in bin.iteritems():
   print x, y, z

should the dict value become a two element list, or is
there a cleaner way to do this?

randy

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list insertion

2005-08-27 Thread Randy Bush
>>hold = self.next
>>self.next = DaClass(value)
>>self.next.next = hold
>>
>> but i suspect (from print statement insertions) that the result
>> is not as i expect.  as the concept and code should be very
>> common, as i am too old for pride, i thought i would ask.
> I think you're fine.

indeed.  the bug was elsewhere.  i got confused and tried to
look-ahead too far when i could have just recursed.  i threw
away the too-clever code and replaced it with one line.  i
love it when that happens.

randy

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Experience regarding Python tutorials?

2005-08-27 Thread Randy Bush
> There is also a python tutor newsgroup at gmane
> (gmane.comp.python.tutor).

is there a mailing list to which it is gated?

randy

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: need a little help with time

2005-08-27 Thread Randy Bush
i am doing disgusting looking junk based on calendar.  example

now = calendar.timegm(time.gmtime())
aWeek = 7*24*60*60
print time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(now + aWeek))

randy

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Jargons of Info Tech industry

2005-08-26 Thread Randy Bush
i left the usenet in the latter half of the '80s.  a few weeks
ago i decided i wanted to do a new project with a new language,
and chose python.  so i joined this mailing list, which is
gated to the usenet.  i am impressed that the s:n has not
gotten significantly worse than when i left, about 0.25, this
message being my contribution to the noise.

the s here is pretty darn good.  but the n is pretty silly.

randy

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list insertion

2005-08-24 Thread Randy Bush
>> hold = self.next
>> self.next = DaClass(value)
>> self.next.next = hold
> shouldn't that last line be this?
>   self.next.prev = hold

single threaded list

> What did you expect, and what did you ovserve?

i will try to distill a case

randy

-- 
http://mail.python.org/mailman/listinfo/python-list


list insertion

2005-08-23 Thread Randy Bush
i am trying to insert into a singly linked list

hold = self.next
self.next = DaClass(value)
self.next.next = hold

but i suspect (from print statement insertions) that the result
is not as i expect.  as the concept and code should be very
common, as i am too old for pride, i thought i would ask.

mahalo,
randy

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: loop in python

2005-08-23 Thread Randy Bush
computers are cheap.  i am expensive.  give me clear and maintainable
code every time.

randy

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dict duplicity

2005-08-18 Thread Randy Bush
> Firstly, to remove one possible source of confusion, change the name of 
> your dictionary ... "mydict" or "fred" ... anything but "dict"
> 
> Next, after you have created the dictionary and added items to it, do this:
> print len(fred)
> print len(fred.items())
> nitems = 0
> for k, v in fred.iteritems():
>  nitems += 1
> print nitems
> 
> If by this stage you haven't worked out what you are doing wrong, post 
> an exact copy/paste of the MINIMAL code that exhibits the "multiple 
> instances of same key in .iteritems()" problem

i did not work out what i was doing wrongly.  but now it works.  i
hate ing magic!

but thanks for making me permute.

randy

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dict duplicity

2005-08-18 Thread Randy Bush
>> a dict written as
>> 
>>pKey = (prefix, pLen, origin)
>> 
>>val = dict.get(pKey)
>>if val == None:
>>   dict[pKey] = (timeB, timeB)
>>else:
>>   if val[0]> timeB:  val[0] = timeB
>>   if val[1] < timeB:  val[1] = timeB
>>   dict[pKey] = val
>> 
>> and read back as
>> 
>>for pKey, pVal in dict.iteritems():
>>   print \
>>  pKey[0], hash(pKey[0]), \
>>  pKey[1], hash(pKey[1]), \
>>  pKey[2], hash(pKey[2]), \
>>  "hash=", hash(pKey), \
>>  pVal[0], hash(pVal[0]), \
>>  pVal[1], hash(pVal[1])
>> 
>> when run with | sort, produces
>> 
>> 12.0.0.0 -2054516913 8 8 7018 329707286 hash= -604503432 917088000 917088000 
>> 917088000 917088000
>> 12.0.0.0 -2054516913 8 8 7018 329707286 hash= -604503432 917088000 917088000 
>> 917088000 917088000
>> 12.0.0.0 -2054516913 8 8 7018 329707286 hash= -604503432 917088000 917088000 
>> 917088000 917088000
>> 12.0.0.0 -2054516913 8 8 7018 329707286 hash= -604503432 917088000 917088000 
>> 917088000 917088000
>> 12.0.0.0 -2054516913 9 -1293912648 7018 329707286 hash= -1578430040 
>> 917088000 917088000 917088000 917088000
>> 12.0.0.0 -2054516913 9 -1293912648 7018 329707286 hash= -1578430040 
>> 917088000 917088000 917088000 917088000
>> 
>> not that there are two entries with the same hash=
>> 
>> i am utterly confused
>> 
>> randy
>> 
> I'm not sure I got your question but having the same hash(key) is not 
> having the same key for a dict.
> 
> >>> {-1:0,-2:0}
> {-2: 0, -1: 0}
> >>> hash(-1)!=hash(-2)
> False
> 
> A key lookup in a dict involve real keys comparisons via '==' among the 
> keys of the bin identified by the hash of the key.

ack.  my point was the key touple and its hash are identical for
each pair of entries.

i executed the write section 55953 times.  the iteritems gets me
111906 entries.

while it did not charge extra, it kinda spoils my code :-)

randy

-- 
http://mail.python.org/mailman/listinfo/python-list


dict duplicity

2005-08-18 Thread Randy Bush
a dict written as

   pKey = (prefix, pLen, origin)

   val = dict.get(pKey)
   if val == None:
  dict[pKey] = (timeB, timeB)
   else:
  if val[0] > timeB:  val[0] = timeB
  if val[1] < timeB:  val[1] = timeB
  dict[pKey] = val

and read back as

   for pKey, pVal in dict.iteritems():
  print \
 pKey[0], hash(pKey[0]), \
 pKey[1], hash(pKey[1]), \
 pKey[2], hash(pKey[2]), \
 "hash=", hash(pKey), \
 pVal[0], hash(pVal[0]), \
 pVal[1], hash(pVal[1])

when run with | sort, produces

12.0.0.0 -2054516913 8 8 7018 329707286 hash= -604503432 917088000 917088000 
917088000 917088000
12.0.0.0 -2054516913 8 8 7018 329707286 hash= -604503432 917088000 917088000 
917088000 917088000
12.0.0.0 -2054516913 8 8 7018 329707286 hash= -604503432 917088000 917088000 
917088000 917088000
12.0.0.0 -2054516913 8 8 7018 329707286 hash= -604503432 917088000 917088000 
917088000 917088000
12.0.0.0 -2054516913 9 -1293912648 7018 329707286 hash= -1578430040 917088000 
917088000 917088000 917088000
12.0.0.0 -2054516913 9 -1293912648 7018 329707286 hash= -1578430040 917088000 
917088000 917088000 917088000

not that there are two entries with the same hash=

i am utterly confused

randy

-- 
http://mail.python.org/mailman/listinfo/python-list