Re: [Tutor] Why don't I see anything when I run this program?

2007-05-12 Thread Alan Gauld

"Bob Gailer" <[EMAIL PROTECTED]> wrote

>>  print “Welcome to the Area calculation program”
>
> I guess you used a word processor to write the program,

And in case you missed Bob's point - don't do that!
Use a text editor like Notepad, or better still IDLE
or Pythonwin or any other editor designed to work
with Python. But it must save the files as plain text.

> Therefore: it is better to run the program by calling
> python explicitly. Open a command prompt and
> enter (for example):

Or alternatively use an IDE like IDLE or Pythonwin
to execute the program while you are testing it since
they will stay open after an error. Once it works in
IDLE you can run it by double clicking as usual.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] newb - Problem reading binary files

2007-05-12 Thread Kent Johnson
Elizabeth Finn wrote:
> I need to read a file that is in binary format, and then convert some of 
> the values into integer values. These values can range from 1 to 4 bytes.
>  
> First question – is there an easy way to do this? I finally wrote my own 
> little utility to handle multi-byte integers because I couldn’t find a 
> built-in way (except for ord() which works only for single bytes)

Alan suggested the struct module. It doesn't directly support 3-byte 
integers but perhaps you could pad them with a zero byte at the front. 
struct does let you unpack a whole string at once.

The Construct library is a higher-level interface to similar 
functionality. It doesn't seem to support 3-byte integers either but it 
is extensible.
>  
> def getnum(num_str):
> """
> Given a string representing a binary number, return the number.
> If string is more than one byte, calculate the number to return.
> Assume that each byte is signed magnitude
> """   
> x = len(num_str)
> ans = 0
> for i in range( x ):
> nextstr = num_str[i:i+1]
> ans = ans * 256
> ans = ans + ord(nextstr)
> return ans

Your loop could be written more simply as
for nextstr in num_str:
   ans = ans * 256
   ans = ans + ord(nextstr)

> This “brute force” method usually works, but - now here is the other 
> question -sometimes the code does not pick up two full bytes when it is 
> supposed to. I open the file and read a block that I want into a string:
>
> f=open(fname, 'rb')
> f.seek(offset, 0)
> block = f.read(2000)
>  
> Then for each number I pull the bytes from the string, then call 
> getnum() to calculate the number.
>  
> test = block[0:1] # 1 byte
> test = block[1:4] # 3 bytes
> test = block[4:6] # 2 bytes
> test = block[20:12] # 2 bytes
> test = block[1996:2000]   #4 bytes
>  
> This method usually works, except that for some of the 2-byte numbers I 
> get only the first byte and first half of the second byte – for 
> instance: 'x06\x33’ comes out as ‘x063’. This is very confusing 
> especially because one 2-byte substring – “00 01” comes out as expected, 
> but “06 52” becomes “065”. Any ideas?

It seems to work for me. I wonder if you are confused about the input 
you are giving it? Using your definition of getnum(), I get these results:
In [31]: getnum('\x06\x33')
Out[31]: 1587
In [33]: 6*256 + 0x33
Out[33]: 1587

In [34]: getnum('\x06\x52')
Out[34]: 1618
In [35]: 6*256 + 0x52
Out[35]: 1618

So it seems to be doing the right thing. Can you put
   print repr(test)
   print getnum(test)
into your test program and show us the results?

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


[Tutor] Loop-checking question

2007-05-12 Thread Alan Gilfoy
My programs often have long, detailed loops in them, and would like  
to, as I'm executing the loop, view what part of the loop Python is  
currently processing.

Thus, if my program gets stuck in one part of the loop, I would see that.
Thus, if one part of my loop is never triggered, I would see that.

I could use, within my loop, print 'I am here, at part X of the loop',  
but I believe that would be far to clunky to put in every section of  
my program's loop(s).
-- 
"Computers were the first God-Satan collaboration project."
"Blind faith in bad leadership is not patriotism."

"One of the most horrible features of war is that all the war-propaganda, all
the screaming and lies and hatred, comes invariably from people who are not
fighting."-George Orwell, _Homage to Catalonia





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


[Tutor] Repeating an action

2007-05-12 Thread Alan Gilfoy
How do you 'tell' Python to repeat a certain action X amount of times,  
and then stop.

I could use a 'counter' (see below), but that seems kind of clunky.



counter = 0
example = True
while example:

 print "Do something"
 counter += 1

 if counter == 100:
 example = False




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


Re: [Tutor] Repeating an action

2007-05-12 Thread F.Lee
On 5/12/07, Alan Gilfoy <[EMAIL PROTECTED]> wrote:
> How do you 'tell' Python to repeat a certain action X amount of times,
> and then stop.
>
> I could use a 'counter' (see below), but that seems kind of clunky.
>
>
>
> counter = 0
> example = True
> while example:
>
>  print "Do something"
>  counter += 1
>
>  if counter == 100:
>  example = False
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>

Hi,

Have you considered a "for"  loop? It's quite a bit less "clunky":

for i in range(100)
   print "Do something"

HTH,


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


Re: [Tutor] Repeating an action

2007-05-12 Thread Andre Engels
2007/5/12, Alan Gilfoy <[EMAIL PROTECTED]>:
> How do you 'tell' Python to repeat a certain action X amount of times,
> and then stop.
>
> I could use a 'counter' (see below), but that seems kind of clunky.
>
>
>
> counter = 0
> example = True
> while example:
>
>  print "Do something"
>  counter += 1
>
>  if counter == 100:
>  example = False

You can use a for loop:

for i in range(100):
print "Do something"

Your own code is unnecessary wordy too, by the way. The same thing can
be done with:

counter = 0
while counter < 100:
print "Do something"
counter += 1

but of course the 'for' solution is still shorter than this.

-- 
Andre Engels, [EMAIL PROTECTED]
ICQ: 6260644  --  Skype: a_engels
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python fast enough for ad server?

2007-05-12 Thread Eric Walstad
Hi OkaMthembo,

Again, are you sure you need it?  Have you done any benchmarking yet?
It looks like you have to run Psyco on 32 bit x86 architecture.  That
may limit other parts of your system design that you want to speed up by
running on x86-64, for example.  OTOH, it's easy to add/remove psyco (at
least it was easy to add it to one of my Django projects in the past).

I think it's good to consider possible bottle necks at design time.
Just remember that your assumptions may be wrong yielding your premature
optimizations useless.  Psyco may help, it may not.  I suspect that you
won't need it because Python is pretty darned fast already and the
Python web-framework guys are *really smart* and have done the much of
the optimization for you already.

Best regards,

Eric.

OkaMthembo wrote:
> Hi guys,
> 
> I stumbled upon a tool called Psyco (http://psyco.sourceforge.net/) sounds
> like what i need.
> 
> Thanks again,
> 
> Lloyd
> 
> On 5/10/07, OkaMthembo <[EMAIL PROTECTED]> wrote:
>>
>> Thanks for all your contributions. i think i will do it all in Python, it
>> seems to me that the advantages far outweigh any negatives.
>>
>> Maybe once its a working project, we can then benchmark the code and see
>> what gives.
>>
>> Thanks again,
>>
>> Lloyd
>>
>> On 5/9/07, Eric Walstad <[EMAIL PROTECTED]> wrote:
>> >
>> > Hey OkaMthenbo,
>> >
>> > OkaMthembo wrote:
>> > > Hi guys,
>> > >
>> > > I need to write an ad-serving application and i'm using Win XP as my
>> > dev
>> > > platform. Naturally, i want it to be as painless as possible and i
>> was
>> > > thinking of writing it 100% in Python. However, i have not written
>> any
>> >
>> > > big apps in the language and i wonder if Python would have the
>> > > performance or scale fast enough to a large user base.
>> > Most certainly for some definitions of 'large'  :)
>> >
>> > Most web apps these days are not written in a single
>> language/technology
>> >
>> > and are often not running on a single piece of hardware.  If you search
>> > the archives of your favorite Python web application framework I'm
>> > pretty sure you'll find a discussion on how to scale your app to handle
>> > a 'large' user base.  At the risk of oversimplification, but in
>> hopes of
>> > avoiding premature optimization, I'd focus first on achieving working
>> > code, then benchmark it, then optimize if optimization is still needed.
>> >
>> > Many others have achieved high volume Python web apps using a mix of
>> all
>> > the wonderful open source tools available.  If your content doesn't
>> > change quickly and the ratio of GETs/POSTs is high, a caching server in
>> > front of your python app might be just the trick (memcached,
>> squid,etc).
>> > But don't waste your time if you don't need to.  Define 'too slow' and
>> > then prove to yourself that your app passes that threshold.  If so,
>> then
>> >
>> > figure out why it's slow and optimize the slow parts.
>> >
>> > Good luck,
>> >
>> > Eric.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Loop-checking question

2007-05-12 Thread Kent Johnson
Alan Gilfoy wrote:
> My programs often have long, detailed loops in them, and would like  
> to, as I'm executing the loop, view what part of the loop Python is  
> currently processing.
> 
> Thus, if my program gets stuck in one part of the loop, I would see that.
> Thus, if one part of my loop is never triggered, I would see that.
> 
> I could use, within my loop, print 'I am here, at part X of the loop',  
> but I believe that would be far to clunky to put in every section of  
> my program's loop(s).

I'm not really sure what you expect this view to look like. I don't know 
of any tool that will let you dynamically watch a program as it 
executes. Some alternatives:

- A debugger lets you step through the code and see how it behaves. 
winpdb is a pretty nice GUI-based Python debugger and some Python 
development tools have built-in debuggers.
http://www.digitalpeers.com/pythondebugger/

- If the program is stuck in a loop, pressing control-C will abort the 
loop and give a stack trace showing you where it was.

- The Python profiler will tell you (after the fact) how much time you 
spend in each function. To see what part of a loop the program is in you 
would have to break the loop up into functions.
http://docs.python.org/lib/profile.html

- A code coverage tool will tell you (after the fact) which lines of the 
program were executed and which were not. (google python code coverage)

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


Re: [Tutor] (no subject)

2007-05-12 Thread Rolando Pereira

adam urbas escreveu:

Hi,I just started python today and I would like a few pointers, if you don't mind.  I tried 
using a tutorial, but was only able to get the correct results for the most basic problems.  
# Area calculation programprint “Welcome to the Area calculation program”print 
“–”print# Print out the menu:print “Please select a shape:”print “1  
Rectangle”print “2  Circle”# Get the user’s choice:shape = input(“> “)# Calculate the 
area:if shape == 1:height = input(“Please enter the height: “)width = input(“Please 
enter the width: “)area = height*widthprint “The area is”, areaelse:radius = 
input(“Please enter the radius: “)area = 3.14*(radius**2)print “The area is”, 
areaI've been trying to get this to work.  I was on a forum on Google and they said to 
put:input("press ENTER to continue")at the end.  I did, but it didn't work.  It 
runs the program but just shuts itself off when its done and i don't even get to select any 
of the option things that i'm s

upposed to be able to select.  It just turns on then back off and I don't even 
get to see anything.  Could someone help me out.ThanksAdam

_
Create the ultimate e-mail address book. Import your contacts to Windows Live 
Hotmail.
www.windowslive-hotmail.com/learnmore/managemail2.html?locale=en-us&ocid=TXT_TAGLM_HMWL_reten_impcont_0507




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


First, welcome to the world of Python. :D
Second. please give a title when you start a new thread on a mailing list.
Third, format your posts and code. Since Python uses indented code, it's 
kinda hard to read it when it's all in one line (Don't worry, I'll paste 
it indented in a file attached to this email :D )


Now for the code.

After arranging the code, the first thing I noticed were this characters “ ”

I tried running the code, and if gave me a error there, so I just 
replace then with " ", and voilá, the code worked :D . So the lesson 
here is always use either " " or ' ' in the code.


Oh, also another thing. Don't use input() to get the user input, because 
that command can run code and it may be evilly used. Always use 
raw_input() instead :D .


Anyway, I hope I helped you,


--
   _
ASCII ribbon campaign ( )
 - against HTML email  X
 & vCards / \
#!/usr/bin/python

# Area calculation program

print "Welcome to the Area calculation program"
print "–––––––––––––"
print

# Print out the menu:
print "Please select a shape:"
print "1  Rectangle"
print "2  Circle"

# Get the user’s choice:
shape = input("> ")

# Calculate the area:
if shape == 1:
height = input("Please enter the height: ")
width = input("Please enter the width: ")
area = height*width
print "The area is", area
else:
radius = input("Please enter the radius: ")
area = 3.14*(radius**2)
print "The area is", area
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Checking/Debugging tools

2007-05-12 Thread Alan Gilfoy
Quoting Kent Johnson <[EMAIL PROTECTED]>:

> Alan Gilfoy wrote:
>> My programs often have long, detailed loops in them, and would like  
>>to, as I'm executing the loop, view what part of the loop Python  
>>   is  currently processing.
>>
>> Thus, if my program gets stuck in one part of the loop, I would see that.
>> Thus, if one part of my loop is never triggered, I would see that.
>>
>> I could use, within my loop, print 'I am here, at part X of the
>> loop', but I believe that would be far to clunky to put in every
>> section of  my program's loop(s).
>
> I'm not really sure what you expect this view to look like. I don't
> know of any tool that will let you dynamically watch a program as it
> executes. Some alternatives:
>
> - A debugger lets you step through the code and see how it behaves.
> winpdb is a pretty nice GUI-based Python debugger and some Python
> development tools have built-in debuggers.
> http://www.digitalpeers.com/pythondebugger/

I got winpdb downloaded, but when I tried to run the script, it said I
needed wxpython. I tried to go to the wxpytohn website, and it's
evidently down right now.
>
> - If the program is stuck in a loop, pressing control-C will abort the
> loop and give a stack trace showing you where it was.
>
> - The Python profiler will tell you (after the fact) how much time you
> spend in each function. To see what part of a loop the program is in
> you would have to break the loop up into functions.
> http://docs.python.org/lib/profile.html

My loop isn't broken into functions, just a truckload of if statements
>
> - A code coverage tool will tell you (after the fact) which lines of
> the program were executed and which were not. (google python code
> coverage)
>
Running that Google Search, I seem to be running into a lot of what
is, to me, technical gobbeldygook. Can y'all help me choose a good
code-coverage tool, and a good tutorial?

> Kent

Alan


- End forwarded message -


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


Re: [Tutor] Loop-checking question

2007-05-12 Thread Alan Gauld

"Kent Johnson" <[EMAIL PROTECTED]> wrote

> I'm not really sure what you expect this view to look like. I don't 
> know
> of any tool that will let you dynamically watch a program as it
> executes.

Not particularly relevant but there are some commercial tools
that do this kind of thing, they are usually called program 
visualisers.
The best one I've used was for C++ (called iLooK I think) and it
displayed objects as little icons and showed arrows between
objects to represent messages. It could cope with multi threading
by colourising the arrows and you could stop/start the program
like a debugger, attach breakpoints on particular instances etc.

It won a Jolt award for productivity back around 1996/97.
I think they might have done a Java one too, but I never used it.

I think there is a .NET visualiser somewhere too. And there are
several for SmallTalk and Lisp.

None of which helps the OP in any way :-/

Alan G. 


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


Re: [Tutor] Running and passing variables to/from Fortran

2007-05-12 Thread John Washakie
WAY too large a project I'm afraid. Yes, that would be the one which
would make me an 'expert' in Python ;)

Too bad there's just no time right now...

On 5/10/07, Bob Gailer <[EMAIL PROTECTED]> wrote:
> John Washakie wrote:
> > I have access to the source code.
> Did you tell us why you want to keep the code in FORTRAN? Would
> converting it to Python solve the issue?
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Checking/Debugging tools

2007-05-12 Thread Kent Johnson
Alan Gilfoy wrote:
> Quoting Kent Johnson <[EMAIL PROTECTED]>:

>> - A debugger lets you step through the code and see how it behaves.
>> winpdb is a pretty nice GUI-based Python debugger and some Python
>> development tools have built-in debuggers.
>> http://www.digitalpeers.com/pythondebugger/
> 
> I got winpdb downloaded, but when I tried to run the script, it said I
> needed wxpython. I tried to go to the wxpytohn website, and it's
> evidently down right now.

It seems to be working now:
http://wxpython.org/

>> - The Python profiler will tell you (after the fact) how much time you
>> spend in each function. To see what part of a loop the program is in
>> you would have to break the loop up into functions.
>> http://docs.python.org/lib/profile.html
> 
> My loop isn't broken into functions, just a truckload of if statements

Perhaps you could break out some pieces into functions.

>> - A code coverage tool will tell you (after the fact) which lines of
>> the program were executed and which were not. (google python code
>> coverage)
>>
> Running that Google Search, I seem to be running into a lot of what
> is, to me, technical gobbeldygook. Can y'all help me choose a good
> code-coverage tool, and a good tutorial?

The first link is an ad. The second link is for Ned Batcheldor's 
coverage module which seems to include pretty good docs. The third link 
is a recipe that creates a nice presentation of the output of coverage, 
with an example.

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


[Tutor] Inherit from int?

2007-05-12 Thread Marilyn Davis
Hello Tutors,

I'm stumped.  This silly bit of code doesn't work.  I expect the
output to be 8, not 18.  What am I missing?

#!/usr/bin/env python
'''An Under10 class, just to fiddle with inheriting from int.'''

class Under10(int):

def __init__(self, number):
number %= 10
int.__init__(self, number)

if __name__ == '__main__':
n = Under10(18)
print n

'''
$ ./new_style.py
18
'''

Any ideas?

Thank you.

Marilyn Davis


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


Re: [Tutor] Inherit from int?

2007-05-12 Thread John Fouhy
On 13/05/07, Marilyn Davis <[EMAIL PROTECTED]> wrote:
> #!/usr/bin/env python
> '''An Under10 class, just to fiddle with inheriting from int.'''
>
> class Under10(int):
>
> def __init__(self, number):
> number %= 10
> int.__init__(self, number)

Subclassing int and other types is a bit special.  Check out this
page; it may help you:
http://www.python.org/download/releases/2.2.3/descrintro/#__new__

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


Re: [Tutor] Inherit from int?

2007-05-12 Thread jim stockford

the problem is in the return.

if you append a print statement to the class function
such as
print "number is ",number   # (properly indented, of course)
you'll get 8



On May 12, 2007, at 5:11 PM, John Fouhy wrote:

> On 13/05/07, Marilyn Davis <[EMAIL PROTECTED]> wrote:
>> #!/usr/bin/env python
>> '''An Under10 class, just to fiddle with inheriting from int.'''
>>
>> class Under10(int):
>>
>> def __init__(self, number):
>> number %= 10
>> int.__init__(self, number)
>
> Subclassing int and other types is a bit special.  Check out this
> page; it may help you:
> http://www.python.org/download/releases/2.2.3/descrintro/#__new__
>
> --
> John.
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>

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