Please help for Python programming

bruno modulix onurb at xiludom.gro
Mon Mar 21 17:34:25 CET 2005

yy0127 at wrote:
> I don't know why i entered the below code 

And we don't know either !-)

> and it will miss some
> records.
> Anyone can help me???

If you hope to get some useful help, it would be a good idea to follow 
Diez's advice (see other post in this thread)

> users = {}
> users1 = {} 
> while 1:

Unless there is a break somewhere in the following code (I can't find 
one...), this is an endless loop. But you know this, don't you ?

> 	user, serviceType, msgType, inOut, date, time, numBytes =
> aLog.GetNextMessage("")
> 	fullmsg = serviceType + "|" + msgType + "|" + inOut
tip : use string formating instead (string concatenations are to be 
avoided in Python), ie:
         fullmsg = "%s | %s | %s" % (serviceType, msgType, inOut)

> 	bytemsg = user + " " + serviceType + " " + msgType + " " + inOut + " "
> + numBytes

> 	user1 = user
> 	msgDict = {}
> 	byteDict = {}

This re-initialises both dicts on each iteration. Is this really what 
you want ?
> 		print bytemsg		# 53 records in source file

Here you have an obvious indentation problem. This code *cannot* run.
Take care of not mixing tabs and spaces (tip: configure you editor to 
only use spaces)

> 		msgDict = users[user]	# get the cum statistics for this user
Given the above binding of 'users' as an empty dict, this should raise a 
KeyError. It's also overwriting the previous binding of msgDict.

> 		byteDict = users1[user1]

idem. Also, you don't need to bind 2 different names to the same value 
to use this value as key in 2 different dicts. Here you could as well 
use user for both dicts, since user and user1 are bound to the same value.

> 		print bytemsg		# 50 records in source file

In addition to Diez's reading advice, here are some that are more 
specific to code-related questions:

1/ paste code, dont re-type it
... this avoid stupid typos

2/ post running code
... if the code is so obviously broked that it cannot even compile|run, 
readers will have to fix it first - which they'll probably won't do. 
Even if they do, they may not fix it the right way. Everyone's losing 
its time...

3/ post the smallest possible bit of code that exhibit your problem
... no one's going to [read 3000 lines of code | install 30 gigabytes of 
third part libs | etc...] just to help you. Moreover, quite often, one 
finds the bug while reducing the problematic code to it's smallest possible.

And one last: don't forget to put your bullet-proof jacket on before 
reading the answers !-) (well, is probably one of the friendliest 
groups on usenet, but still, this is usenet).

bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for 
p in 'onurb at xiludom.gro'.split('@')])"

More information about the Python-list mailing list