Re: [Tutor] python interpreter vs bat file

2009-07-19 Thread Alan Gauld

Dinesh B Vadhia dineshbvad...@hotmail.com wrote

Not much more information available.  
Have a batch file (eg. 'test.bat') with entries:


python program a.py
python program b.py
python program c.py

One of the programs (eg. 'program c.py') fails with a 
memory error when performing a pickle.dump:


Traceback (most recent call last):
 ...
File py, line 176, in pickleObject
pickle.dump(self, f, 2)
MemoryError

When the programs are run in the same order from the 
Python interpreter there are no memory errors.  


Can you elaborate on how you run the programs. It looks like 
an environmental issue so we need to know exactly what 
you are doing.


How do you run the bat file?
How do you run the programs from the Python interpreter

Are you using Windows Explorer or a CMD wondow? 
or the Start-Run dialog etc?


Which folders are you starting from in each case?


This has happened before and it seems odd behavior.


So how did you fix it before? 
I've never seen or heard of this before.


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python interpreter vs bat file

2009-07-19 Thread bob gailer

Dinesh B Vadhia wrote:

1.  Run Python Programs with Batch file
Python programs run from a Windows XP batch file (test.bat) in a CMD 
window initiated from Windows Explorer.  All programs except one 
execute successfully which stops with a memory error but batch file 
continues to execute other Python programs (as it should).


What happens if you open a CMD window, run program 1, close CMD window, 
and repeat for the other programs?


--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python interpreter vs bat file

2009-07-19 Thread Dinesh B Vadhia
1.  Run Python Programs with Batch file
Python programs run from a Windows XP batch file (test.bat) in a CMD window 
initiated from Windows Explorer.  All programs except one execute successfully 
which stops with a memory error but batch file continues to execute other 
Python programs (as it should).

2.  Run Python Programs with Python Interpreter
Fire up Python Interpreter, open .py program, Run.  

When the program with the memory error in 1. is run independently as in 2. it 
works.

Dinesh





Message: 4
Date: Sun, 19 Jul 2009 07:18:08 +0100
From: Alan Gauld alan.ga...@btinternet.com
To: tutor@python.org
Subject: Re: [Tutor] python interpreter vs bat file
Message-ID: h3udr2$f8...@ger.gmane.org
Content-Type: text/plain; format=flowed; charset=Windows-1252;
reply-type=original

Dinesh B Vadhia dineshbvad...@hotmail.com wrote

 Not much more information available.  
 Have a batch file (eg. 'test.bat') with entries:

 python program a.py
 python program b.py
 python program c.py

 One of the programs (eg. 'program c.py') fails with a 
 memory error when performing a pickle.dump:

 Traceback (most recent call last):
  ...
 File py, line 176, in pickleObject
 pickle.dump(self, f, 2)
 MemoryError
 
 When the programs are run in the same order from the 
 Python interpreter there are no memory errors.  

Can you elaborate on how you run the programs. It looks like 
an environmental issue so we need to know exactly what 
you are doing.

How do you run the bat file?
How do you run the programs from the Python interpreter

Are you using Windows Explorer or a CMD wondow? 
or the Start-Run dialog etc?

Which folders are you starting from in each case?

 This has happened before and it seems odd behavior.

So how did you fix it before? 
I've never seen or heard of this before.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] UnicodeEncodeError

2009-07-19 Thread gpo

I'm doing a simple excercise in reading a file, and printing each line. 
However, I'm getting this error.  The file is a windows txt file, ecoded in
ANSI(ascii).  I don't understand why Pythin is displaying a Unicode error.  

Here is my script:

f=open('I:\\PythonScripts\\statement.txt')
for line in f:
print (line)
f.close()

statement.txt is just a copy of the text from an article at
(http://www.tgdaily.com/content/view/43296/98/) into notepad.

The error I get is:

Traceback (most recent call last):
  File I:\PythonScripts\fileloop.py, line 9, in 
print (line)
  File C:\Python31\lib\encodings\cp437.py, line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2014' in
position
10: character maps to 

I've looked this up, and see that others have had a similar error; however,
I don't see any thing saying what I should be encoding to/from since my
input and output files are both ascii. 
-- 
View this message in context: 
http://www.nabble.com/UnicodeEncodeError-tp24554280p24554280.html
Sent from the Python - tutor mailing list archive at Nabble.com.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] UnicodeEncodeError

2009-07-19 Thread Mark Tolonen


gpo goodpotat...@yahoo.com wrote in message 
news:24554280.p...@talk.nabble.com...


I'm doing a simple excercise in reading a file, and printing each line.
However, I'm getting this error.  The file is a windows txt file, ecoded 
in

ANSI(ascii).  I don't understand why Pythin is displaying a Unicode error.

Here is my script:

f=open('I:\\PythonScripts\\statement.txt')
for line in f:
   print (line)
f.close()

statement.txt is just a copy of the text from an article at
(http://www.tgdaily.com/content/view/43296/98/) into notepad.

The error I get is:

Traceback (most recent call last):
 File I:\PythonScripts\fileloop.py, line 9, in
   print (line)
 File C:\Python31\lib\encodings\cp437.py, line 19, in encode
   return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2014' in
position
10: character maps to

I've looked this up, and see that others have had a similar error; 
however,

I don't see any thing saying what I should be encoding to/from since my
input and output files are both ascii.


Notepad doesn't encode in ASCII.  What it calls ANSI is the default file 
system encoding, which on US Windows is cp1252.  I see you are using Python 
3.1.  When not given an explicit encoding, Python 3.1 defaults to the 
default file system encoding.  It successfully reads the file, which happens 
to contain non-ASCII data.


cp1252 supports the Unicode character \u2014 (EM DASH).  However, when you 
try to print the file on the console, the console's default encoding is 
cp437 and doesn't support this character.  On Python 3.1 there is a 
function, ascii(), that will display the contents of a string using 
ascii-only.  Try print(ascii(line)).


Another trick is to switch to Lucida Console font and change the console's 
code page to 1252 with the command chcp 1252.  This font and code page 
supports the EM DASH and will display the text properly with print(line).


You can also use a shell that supports the full Unicode character set such 
as Idle or PythonWin instead of the console.


-Mark


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python interpreter vs bat file

2009-07-19 Thread Dave Kuhlman
On Sun, Jul 19, 2009 at 05:40:41AM -0700, Dinesh B Vadhia wrote:
 
1.  Run Python Programs with Batch file
 
Python programs run from a Windows XP batch file (test.bat) in a CMD
window initiated from Windows Explorer.  All programs except one
execute successfully which stops with a memory error but batch file
continues to execute other Python programs (as it should).
 
 
 
2.  Run Python Programs with Python Interpreter
 
Fire up Python Interpreter, open .py program, Run.
 

Dinesh -

Please tell us how you did this.  Did you type python at a
command prompt and then see the  prompt?  If so how did you
open .py program, Run? Or, did you start Idle (or some other IDE)
then click File--Open, then run with the Run--RunModule menu
item?

You have been asked several times for more information.  You really
need to read:

http://catb.org/~esr/faqs/smart-questions.html

There are people on this list who are very generous with their
time.  It's a valuable resource.  Please don't waste it.

I don't mean to be rude.  But, you will help us all, yourself
included, if you think carefully when asking a question.

- Dave



-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] hitting a wall (not a collision detection question :P)

2009-07-19 Thread Michael
I have virtually no background in programming. I'm currently teaching myself 
python using the following books:

Beginning Python - From Novice to Professional by Magnus Lie Hetland
Beginning Python by Peter Norton et. al.
Making Use of Python by Rashi Gupta
Learning Python  by Mark Lutz

as well as a variety of tutorials I picked up off the web. I'm using v.2.5.2, 
and I'm trying to upgrade to 2.6.2, but I'm running Linux/Ubuntu so the upgrade 
is giving me problems. The main reason I'm not using 3.1 is because all my 
books talk about 2.x.

Ultimately, my goal is to delve into pygame and make some games and maybe some 
multimedia applications. I'd like my first project to be an old-school style 
RPG (e.g. Final Fantasy), as something with such simple graphics and interface 
would be a good benchmark of where I want to go from there.

I've been progressing steadily, until now. At this point, I have a pretty solid 
understanding of strings, integers, tuples, lists, dictionaries, etc. and 
everything up to functions vs. methods and the basics of classes and OOP. This 
is where I'm hitting a wall. It's at this point the all the books go off in 
different directions and I'm not sure a) what I'm learning, b) why I'm learning 
it, and c) how this is going to help me get to my goals. I'm not really even 
understanding much of what these books are talking about at this point anyway. 
It's like a few chapters after Classes and OOP were torn out of all of them.

So, I'm just wondering what I should be doing at this point. Sorry for the 
vague question, but I'm pretty lost right now and this is about as specific as 
I can be. Thanks in advance for any help.

Michae



  ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python interpreter vs bat file

2009-07-19 Thread Dinesh B Vadhia
Hi Dave

Sorry, I wasn't being obtuse.  Here is more info:

1.  Run Python Programs with Batch file
- OS (correction): Windows 64-bit Vista SP2
- Python 2.5.4 64 bit (AMD64)
- The Python programs run from a Windows batch file (test.bat) in a CMD window 
initiated from Windows Explorer.  All programs except one execute successfully 
which stops with a memory error but the batch file continues to execute the 
other Python programs (as it should).

2.  Run Python Programs with Python Interpreter
- Start Idle, File/Open .py program, Run/Run Module
- When the program with the memory error in 1. is run independently with Idle 
it works.

Bob Gailer suggested running the Python programs individually in CMD one after 
the other.  This is sensible but my test programs run for days and the full 
suite of programs take longer.  The programs are memory intensive (the 64-bit 
machine has 8gb ram).  Hence, it is not easy to test this scenario right now.

It seems to me as if Windows is not freeing up memory between Python 
invocations in the batch file but can't be sure.  I said earlier that this has 
happened before but the fix, as now, is to run the program individually with 
Idle.  Hth ...

Dinesh





Message: 5
Date: Sun, 19 Jul 2009 11:56:15 -0700
From: Dave Kuhlman dkuhl...@rexx.com
To: tutor@python.org
Subject: Re: [Tutor] python interpreter vs bat file
Message-ID: 20090719185615.ga5...@cutter.rexx.com
Content-Type: text/plain; charset=us-ascii

On Sun, Jul 19, 2009 at 05:40:41AM -0700, Dinesh B Vadhia wrote:
 
1.  Run Python Programs with Batch file
 
Python programs run from a Windows XP batch file (test.bat) in a CMD
window initiated from Windows Explorer.  All programs except one
execute successfully which stops with a memory error but batch file
continues to execute other Python programs (as it should).
 
 
 
2.  Run Python Programs with Python Interpreter
 
Fire up Python Interpreter, open .py program, Run.
 

Dinesh -

Please tell us how you did this.  Did you type python at a
command prompt and then see the  prompt?  If so how did you
open .py program, Run? Or, did you start Idle (or some other IDE)
then click File--Open, then run with the Run--RunModule menu
item?

You have been asked several times for more information.  You really
need to read:

http://catb.org/~esr/faqs/smart-questions.html

There are people on this list who are very generous with their
time.  It's a valuable resource.  Please don't waste it.

I don't mean to be rude.  But, you will help us all, yourself
included, if you think carefully when asking a question.

- Dave

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] hitting a wall (not a collision detection question :P)

2009-07-19 Thread David

Michael wrote:
I have virtually no background in programming.. I'm currently teaching 
myself python using the following books:


Beginning Python - From Novice to Professional by Magnus Lie Hetland
Beginning Python by Peter Norton et. al.
Making Use of Python by Rashi Gupta
Learning Python  by Mark Lutz

as well as a variety of tutorials I picked up off the web. I'm using 
v.2.5.2, and I'm trying to upgrade to 2.6.2, but I'm running 
Linux/Ubuntu so the upgrade is giving me problems. The main reason I'm 
not using 3.1 is because all my books talk about 2.x.


Ultimately, my goal is to delve into pygame and make some games and 
maybe some multimedia applications. I'd like my first project to be an 
old-school style RPG (e.g. Final Fantasy), as something with such simple 
graphics and interface would be a good benchmark of where I want to go 
from there.


I've been progressing steadily, until now. At this point, I have a 
pretty solid understanding of strings, integers, tuples, lists, 
dictionaries, etc. and everything up to functions vs. methods and the 
basics of classes and OOP. This is where I'm hitting a wall. It's at 
this point the all the books go off in different directions and I'm not 
sure a) what I'm learning, b) why I'm learning it, and c) how this is 
going to help me get to my goals. I'm not really even understanding much 
of what these books are talking about at this point anyway. It's like a 
few chapters after Classes and OOP were torn out of all of them.


So, I'm just wondering what I should be doing at this point. Sorry for 
the vague question, but I'm pretty lost right now and this is about as 
specific as I can be. Thanks in advance for any help.


Michae





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


I am new to Python and never programed before. What has helped me is to 
think of something you want to automate, as an example I visit a few 
different sites each day to view new bugs reported. I made a program 
that emails me the differences from what I have gathered before. I take 
a piece of paper and write down the steps that I think this will take 
then start doing small pieces at a time and check each one to make sure 
it works, plus this gives me encouragement to continue.

-david
--
Powered by Gentoo GNU/Linux
http://linuxcrazy.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] hitting a wall (not a collision detection question :P)

2009-07-19 Thread Luke Paireepinart
 Ultimately, my goal is to delve into pygame and make some games and maybe
 some multimedia applications. I'd like my first project to be an old-school
 style RPG (e.g. Final Fantasy), as something with such simple graphics and
 interface would be a good benchmark of where I want to go from there
I can recommend the book Game Development with Python and Pygame by
Will McGugan.
This may help you when you are working on the game, but you can also
just use online resources if you'd like (that's what I did initially.)

 I've been progressing steadily, until now. At this point, I have a pretty
 solid understanding of strings, integers, tuples, lists, dictionaries, etc.
 and everything up to functions vs. methods and the basics of classes and
 OOP. This is where I'm hitting a wall. It's at this point the all the books
 go off in different directions and I'm not sure a) what I'm learning, b) why
 I'm learning it, and c) how this is going to help me get to my goals.
You're at the point now that you should start working on your own
simple programs that do things you need to.
The best way to learn is to come up with a problem and figure out how
to solve it yourself.  If you need any hints,
we'd be happy to help.
See, the thing about programming is that there are a lot of really
advanced topics that don't necessarily help you or
aren't necessarily useful for every problem.  For example, Regular
Expressions are very powerful and for certain applications,
they're nearly indispensible.  However, whenever I write a game in
Pygame, I don't hardly ever use them.

So writing an application you want to write will help you see the
bigger picture, and how all the things you've learned so far
can be utilized to achieve whatever goal you desire.

Honestly if you already understand OOP and classes, you're a lot
further along than a lot of people are.
When I started writing Python I didn't understand OOP at all.  In fact
I went through quite a few programs
without using it whatsoever.  Later on, when I learned what OOP was, I
went back and modified my programs
and in some cases it simplified them quite a bit.  But in some cases
it wasn't even necessary and was maybe
even more confusing to use OOP.

I'm a Computer Science grad student right now, but everything that
gets covered in class I've already learned through
my own experimentation with Python, to a pretty large extent.  You can
learn a great deal independently, if you really want to.
Free online resources and books go into incredibly advanced detail
that you aren't expected to grasp immediately.
It's a progressive thing.  Try writing some simple apps and if you
come up with a problem that you can't solve
with the tools you have already learned how to use (or can't solve
easily - most things in Python should be
easy), let us know and we'll tell you which next section of the book
you should read.

HTH,
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python interpreter vs bat file

2009-07-19 Thread Alan Gauld


Dinesh B Vadhia dineshbvad...@hotmail.com wrote 

Bob Gailer suggested running the Python programs individually 
in CMD one after the other.  This is sensible but my test programs 
run for days and the full suite of programs take longer.  


OK, But it can't take longer than in IDLE? Or even in the bat file.
So you can start the program running and then iconify it.

The reason this is important is that IDLE catches some errors 
that the normal python interpreter does not So IDLE may be 
masking a real problem in your code. However...


The programs are memory intensive (the 64-bit machine 
has 8gb ram).  Hence, it is not easy to test this scenario 
right now.


Have you chedked in Task Manager how much RAM the python 
programs use up - they should be visible in the process tab.


If it is a lot then maybe we can rewrite the code to use less 
memory (Or maybe leak less memory).


It seems to me as if Windows is not freeing up memory 
between Python invocations in the batch file but can't be 
sure.  


Windows should free up the memory, but it might depend on 
how you run the programs. In your earlier post you said the 
bat file contained lines like


python foo.py
python bar.py

You could try usng the start command instead, as in:

start foo.py

You might want to explore the /I, /B and /WAIT options

start gives you a lot more control over the execution environment.

Notice you don;t need the 'python' because start uses the file 
association.


HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] hitting a wall (not a collision detection question :P)

2009-07-19 Thread Alan Gauld

Michael punkbohem...@yahoo.com wrote

...everything up to functions vs. methods and the basics of classes
and OOP. This is where I'm hitting a wall. It's at this point the all the
books go off in different directions


OK, First thing is don;t worry about it, you are far from alone.
Many, Many programmers (even long term pros) find the transition
from functions to objects really hard to adjust to. Not surprising,
since it doers require a new way of thinking about program
structure. Eventually the OOP way will become second nature,
in fact you might even find it hard to think about ordinary functions
after a while! But it can take a while.


and I'm not sure a) what I'm learning, b) why I'm learning it,
and c) how this is going to help me get to my goals.


It might be good to throw us some specific questions and we can
try to answer them. General questions tend to produce vague
answers!

You can try my tutorial on OOP to see if that helps. Follow it
up with the case study to see OOP in action.


I'm not really even understanding much of what these books
are talking about at this point anyway.


Again, anything you are unsure about tell us and we can try to
explain. That isd what this klist is really good at because there
are many different perspectives who have all gone through
the same learning curve. Someone likely has the same way if
thinking about it as you do!

It's like a few chapters after Classes and OOP were torn out of all of 
them.


:-)


So, I'm just wondering what I should be doing at this point.


Start writing code. Don't worry about OOP too much but look out
for when you use objects in normal python code - like strings and
files for example - and see how the library writers did things. Look
for things in your code that sound like they might be objects, try writing
a class to bring together the data and the functions that work on
that data.

The more you use objects and classes the more familiar they
become and what works and doesn't work becomes obvious.
Until you use them it all remains somewhat academic.


this is about as specific as I can be.


Go back to those tutorials and find the bits you aren't sure about
and just ask - what does this mean? How should I use this? And why?
Is this a sensible way to program this? etc etc... We will try to help.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] UnicodeEncodeError

2009-07-19 Thread Alan Gauld


Mark Tolonen metolone+gm...@gmail.com wrote

... I see you are using Python 3.1.  ...

You can also use a shell that supports the full Unicode character set 
such as Idle or PythonWin instead of the console.


As a matter of interest - and somewhat off topic - does anyone
know if there is a Python 3 (3.0 or 3.1) version of Pythonwin out yet?

Too lazy to look :-)

Alan G 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Using insert method on a list matrix

2009-07-19 Thread Raj Medhekar
I would like to know how I could use the insert method in a List matrix eg. for 
the matrix below

M=[[1,2,3], [3,2,1], [4,3,2]]

if wanted to insert the string 'pod' in list [0] before [1] in list [0] in M 
using the built in method insert How would I go about doing this? I've tried 
may List Comprehension possibilities but none of then worked. Below are some of 
the things I tried. 

 M=[[1,2,3], [3,2,1], [4,3,2]]
 M.insert([1][1], 'pod')

Traceback (most recent call last):
  File pyshell#19, line 1, in module
M.insert([1][1], 'pod')
IndexError: list index out of range
 [row[0] for row in M.insert(1, 'pod')]

Traceback (most recent call last):
  File pyshell#20, line 1, in module
[row[0] for row in M.insert(1, 'pod')]
TypeError: 'NoneType' object is not iterable
 M.insert(1,'pod')
 M
[[1, 2, 3], 'pod', 'pod', [3, 2, 1], [4, 3, 2]]
 M.insert[0](1,'pod')

Traceback (most recent call last):
  File pyshell#23, line 1, in module
M.insert[0](1,'pod')
TypeError: 'builtin_function_or_method' object is unsubscriptable


Any help is greatly appreciated! Thanks!

-Raj



  ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using insert method on a list matrix

2009-07-19 Thread Ken Oliver
tryM[0].insert(0, 'pod')-Original Message-
From: Raj Medhekar 
Sent: Jul 19, 2009 7:12 PM
To: Python Tutor 
Subject: [Tutor] Using insert method on a list matrix

I would like to know how I could use the insert method in a List matrix eg. for the matrix belowM=[[1,2,3], [3,2,1], [4,3,2]]if wanted to insert the string 'pod' in list [0] before [1] in list [0] in M using the built in method insert How would I go about doing this? I've tried may List Comprehension possibilities but none of then worked. Below are some of the things I tried.  M=[[1,2,3], [3,2,1], [4,3,2]] M.insert([1][1], 'pod')Traceback (most recent call last): File "pyshell#19", line 1, in module M.insert([1][1], 'pod')IndexError: list index out of range [row[0] for row in M.insert(1, 'pod')]Traceback (most recent call last): File
 "pyshell#20", line 1, in module [row[0] for row in M.insert(1, 'pod')]TypeError: 'NoneType' object is not iterable M.insert(1,'pod') M[[1, 2, 3], 'pod', 'pod', [3, 2, 1], [4, 3, 2]] M.insert[0](1,'pod')Traceback (most recent call last): File "pyshell#23", line 1, in module M.insert[0](1,'pod')TypeError: 'builtin_function_or_method' object is unsubscriptableAny help is greatly appreciated! Thanks!-Raj



  

 .
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] UnicodeEncodeError

2009-07-19 Thread Mark Tolonen


Alan Gauld alan.ga...@btinternet.com wrote in message 
news:h407ah$lc...@ger.gmane.org...


Mark Tolonen metolone+gm...@gmail.com wrote

... I see you are using Python 3.1.  ...

You can also use a shell that supports the full Unicode character set 
such as Idle or PythonWin instead of the console.


As a matter of interest - and somewhat off topic - does anyone
know if there is a Python 3 (3.0 or 3.1) version of Pythonwin out yet?

Too lazy to look :-)


Yes.  PythonWin 214 is the latest version and supports 3.0 and 3.1.

-Mark


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading complex data types from text file

2009-07-19 Thread Chris Castillo
okay so I figured my program out. I am posting the final version so if
someone else is having problems with something like this it may benefit the
community. (comments are included to help those who might not understand the
code)

'''
Program reads names of bowlers and their corresponding scores,
then writes their names to a new text file with a description
of either below average, average, above average or a perfect score
'''

scores = {}# empty dictionary
total = 0#initalize total to 0 value


for line in open(bowlingscores.txt, r):# iterate through txt file
with names and scores
if line.strip().isdigit():
score = int(line)# convert score into int type
scores[name] = score# add scores to dictionary
total += score# add score to running total
else:
name = line.strip()# if the line isn't a digit name will be the
key in the dictionary



averageScore = total / len(scores)# get average of all scores
fileOut = open(bowlingaverages.txt, w)# create a file to write names
and scores to
fileOut.write(Bowling Report\n + (- * 50) + \n)# header


for name, score in scores.items():#iterate through each score in the
dictionary to get an score value for each player
if score == 300:
score = \tPerfect score!\n
scores[name] = score
elif score  averageScore:
score = \tBelow average\n
scores[name] = score
elif score  averageScore:
score = \tAbove average!\n
scores[name] = score
else:
score = \tAverage!\n
scores[name] = score

for items in scores.items():#iterate through the items in the dictionary
and format them to the output file
fileOut.write(%s%s\n % items)
---
your output for this code should look like this inside the text file:

*Bowling Report
--
sueBelow average

billAbove average!

natBelow average

tomPerfect score!*



Thanks to everyone who helped me with this.

On Sun, Jul 19, 2009 at 12:15 AM, Chris Castillo ctc...@gmail.com wrote:

 so could I also replace the score of each bowler (key value) in the
 dictionary with a new key such as below average or above average
 according to each if-elif-else structure and then write to a text file in
 the following format?

 Jim Above Average
 SueBelow Average
 BobPerfect score


 On Fri, Jul 17, 2009 at 12:48 PM, bob gailer bgai...@gmail.com wrote:

 Chris Castillo wrote:

 how would i go about adding the names to a dictionary as a key and the
 scores as a value in this code?


  # refactored for better use of Python, correct logic, and flow

 scores = {} # empty dictionary
 total = 0
 for line in open(bowlingscores.txt, r):
   if line.strip().isdigit():
   score = int(line)
   scores[name] = score
   total += score
   else:
   name = line.strip()
 averageScore = total / len(scores)
 fileOut = open(bowlingaverages.txt, w)
 fileOut.write(Bowling Report\n + (- * 50) + \n)
 for name, score in scores.items():
  if score == 300:
  score = \tPerfect score!
  elif score  averageScore:
  score = \tBelow average
  elif score  averageScore:
  score = \tAbove average!
  else:
  score = \tAverage!
  print name, score


 --
 Bob Gailer
 Chapel Hill NC
 919-636-4239



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor