473,698 Members | 2,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help for Python programming

I don't know why i entered the below code and it will miss some
records.
Anyone can help me???

users = {}
users1 = {}
while 1:
user, serviceType, msgType, inOut, date, time, numBytes =
aLog.GetNextMes sage("")
fullmsg = serviceType + "|" + msgType + "|" + inOut
bytemsg = user + " " + serviceType + " " + msgType + " " + inOut + " "
+ numBytes

user1 = user

msgDict = {}
byteDict = {}

print bytemsg # 53 records in source file
msgDict = users[user] # get the cum statistics for this user
byteDict = users1[user1]
print bytemsg # 50 records in source file

Jul 18 '05 #1
13 1585
yy****@gmail.co m wrote:
I don't know why i entered the below code and it will miss some
records.
Anyone can help me???


Sorry, my mindreading brain extension is at the service right now, so I
can't guess what that piece of syntactically incorrect code is supposed to
do and what error message it produces.

Until you come up with a more detailed error description, I'll have a good
read at

http://www.catb.org/~esr/faqs/smart-questions.html
Which I suggest you read too.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
yy****@gmail.co m 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.GetNextMes sage("")
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 idem
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, c.l.py 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 'o****@xiludom. gro'.split('@')])"
Jul 18 '05 #3
This is what I see (minus the '> '):
while 1:
user, serviceType, msgType, inOut, date, time, numBytes =
aLog.GetNextMes sage("")

[etc]

Advice: use spaces, not tabs, to indent posted code (some readers discard
tabs). Don't use google groups to post code (it deletes initial spaces and
won't, apparently, fix this bug). Use news.gmane.org group
gmane.comp.pyth on.genral instead (I believe they also have web interface in
addition to newsreader interface). If you really, really must post thru
google, prefix lines with char such as '|', even tho this kill cut and
pastability.

Terry J. Reedy

Jul 18 '05 #4
Actually, this script will open a text file which is seperated by tabs.
The PRINT code is for my testing. My problem is bytemsg will be omitted
some records. For example, my text file have 53 records about username.
After byteDict = users1[user1], it remains 50 records in the output
file.

I would like to know how to culmulate some data by users.

Thanks!

Jul 18 '05 #5
yy****@gmail.co m wrote:
(snip)
The PRINT code is for my testing. My problem is bytemsg will be omitted
some records. For example, my text file have 53 records about username.
After byteDict = users1[user1],
Which, from your previous snippet, should raise a KeyError... If it
does, then first understand why and fix it. If it does not, then the
code you posted is absolutely useless for us to help you.
it remains 50 records in the output
file.
There is nothing about reading and parsing file in the code you posted.

I'm afraid you did not follow Diez's advice, nor mine. Please re-read my
previous post, take appropriate action, and re-post with the minimum
*working* snippet that exhibit your problem.

I would like to know how to culmulate some data by users.


---- data.txt
user1;aaa;000
user2;aab;001
user3;aac;002
user1;aad;004
user3;aae;005
user1;aaf;006
user2;aag;007
user2;aah;008
user2;aak;009
user1;zzz;999
---- accu.py
import sys
import pprint

try:
f = open('data.txt' , 'r')
except IOError, e:
print >> sys.stderr, "Cannot open file data.txt for reading : %s" % e
sys.exit(1)

users = {}
for line in f:
try:
user, data1, data2 = line.strip().sp lit(';')
except ValueError:
print >> sys.stderr, "wrong file format"
f.close()
sys.exit(1)
try:
users[user].append("%s : %s" % (data1, data2))
except KeyError:
users[user] = ["%s : %s" % (data1, data2)]

f.close()
print "collected data:"
pprint.pprint(u sers)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Jul 18 '05 #6
Terry,
This was posted from google groups, can you see the indents?
# code snippet
convertpage = 0
form = None
for o, a in opts:
if o in ["-h", "--help"]:
Usage()
sys.exit()
if o in ["-o", "--output", "--out"]:
output = a
if o in ["-i", "--input", "--in"]:
input = a
if input in [".", "cwd"]:
input = os.getcwd()

Notice the 'fixed font / proportional font' link in the top right
corner.
I think they have "fixed" the problem.
M.E.Farmer

Terry Reedy wrote:
This is what I see (minus the '> '):
while 1:
user, serviceType, msgType, inOut, date, time, numBytes =
aLog.GetNextMes sage("") [etc]

Advice: use spaces, not tabs, to indent posted code (some readers

discard tabs). Don't use google groups to post code (it deletes initial spaces and won't, apparently, fix this bug). Use news.gmane.org group
gmane.comp.pyth on.genral instead (I believe they also have web interface in addition to newsreader interface). If you really, really must post thru google, prefix lines with char such as '|', even tho this kill cut and pastability.

Terry J. Reedy


Jul 18 '05 #7

"M.E.Farmer " <me*****@hotmai l.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Terry,
This was posted from google groups, can you see the indents?
Yes, looks good
# code snippet
convertpage = 0
form = None
for o, a in opts:
if o in ["-h", "--help"]:
Usage()
sys.exit()
if o in ["-o", "--output", "--out"]:
output = a
if o in ["-i", "--input", "--in"]:
input = a
if input in [".", "cwd"]:
input = os.getcwd()

Notice the 'fixed font / proportional font' link in the top right
corner.
I presume this is on the Google page.
I think they have "fixed" the problem.


Great. Now people have to learn to use the fixed font choice.

TJR

Jul 18 '05 #8
Hello again,
For some strange reason Google isn't showing this so I got this from
gmane sorry if I missed something.
The fixed/proportional link is located on the google groups c.l.py
pages.
I wasn't clear at all sorry for the ambiguity.
Google groups for c.l.py seems to be fixed by default, so no need to
click it.
Proportional still retains spaces but is less tidy.
M.E.Farmer
Terry,
This was posted from google groups, can you see the indents?
Yes, looks good # code snippet
convertpage = 0
form = None
for o, a in opts:
if o in ["-h", "--help"]:
Usage()
sys.exit()
if o in ["-o", "--output", "--out"]:
output = a
if o in ["-i", "--input", "--in"]:
input = a
if input in [".", "cwd"]:
input = os.getcwd()

Notice the 'fixed font / proportional font' link in the top right
corner. I presume this is on the Google page. I think they have "fixed" the problem.

Great. Now people have to learn to use the fixed font choice. TJR


Jul 18 '05 #9
Thanks for your advice.^^

I find out something special (because i don't know). I would like to
retrieve data from source file into array. The data will be omited if
the value is duplicated. For example, there is 3 values - 1,2,1. It
will only display 1,2 after i ran the script.

value = (1,2,1)
while 1:
users = {}
byteDict = {}

byteDict = users[value]

users[value] = byteDict
print users

I think the above code cannot run properly. Any ppl knows why the value
omited when it was duplicated.

Many of thanks for all!!!

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
3727
by: kbass | last post by:
In different articles that I have read, persons have constantly eluded to the productivity gains of Python. One person stated that Python's productivity gain was 5 to 10 times over Java in some in some cases. The strange thing that I have noticed is that there were no examples of this productivity gain (i.e., projects, programs, etc.,...). Can someone give me some real life examples of productivity gains using Python as opposed other...
68
5856
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
10
2049
by: Robert | last post by:
Where can i find a free web- based VC++ course? (also i've to dowanload it) I'm interested also in VB and C++ thanks, Robert
118
6707
by: 63q2o4i02 | last post by:
Hi, I've been thinking about Python vs. Lisp. I've been learning Python the past few months and like it very much. A few years ago I had an AI class where we had to use Lisp, and I absolutely hated it, having learned C++ a few years prior. They didn't teach Lisp at all and instead expected us to learn on our own. I wasn't aware I had to uproot my thought process to "get" it and wound up feeling like a moron. In learning Python I've...
35
2371
by: John Coleman | last post by:
Greetings, I have a rough classification of languages into 2 classes: Zen languages and tool languages. A tool language is a language that is, well, a *tool* for programming a computer. C is the prototypical tool language. Most languages in the Algol family are tool languages. Visual Basic and Java are also tool languages. On the other hand, a Zen language is a language which is purported to transform your way of thinking about...
17
2970
by: MilkmanDan | last post by:
I'll be a college freshman this fall, attending Florida Institute of Tech studying electrical engineering. I was considering taking some classes in programming and computer science, and I happened to notice that everything taught is using C++. After further research, it seems to me that C++ seems to be the dominating language in universities. By comparison, our local community college teaches a few classes in VB, Java, Javascript,...
10
1404
by: Jerry | last post by:
I have just started to do some semi-serious programming (not one-off specialized scripts) and am loving Python. I just seem to get most of the concepts, the structure, and the classes (something I struggled with before in other languages). I've seen many concepts on the web (i.e. stacks, queues, linked lists) and can usually understand them pretty well (event if I don't always know when to use them). Now I've come accross decorators and...
852
28399
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for my general education. Mark
11
1332
by: Monty Taylor | last post by:
Hey everybody, MySQL has put up a poll on http://dev.mysql.com asking what your primary programming language is. Even if you don't use MySQL - please go stick in a vote for Python. I'm constantly telling folks that Python needs more love, but PHP and Java are kicking our butts... (I know the world would be a better place if the poll were honest, but I'd rather that people got the message that they should do more python development...
28
1560
by: Hussein B | last post by:
Hey, I'm a Java/Java EE developer and I'm playing with Python these days. I like the Python language so much and I like its communities and the Django framework. My friends are about to open a Ruby/Rails shop and they are asking me to join them. I don't know what, sure I'm not leaving Java, but they are asking me to stop learning Python and concentrate on Ruby/Rails. The sad fact (at least to me), Ruby is getting a lot of attention...
0
8672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9018
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7711
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3038
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.