Re: [Tutor] reading POST method in cgi script

2007-10-16 Thread Eric Abrahamsen
Thanks for the detailed help. I'm dumping sys.stdin into a variable  
at the very start now, and operating on that variable for everything  
else, and all is well.

Thanks again,
E


On Oct 16, 2007, at 12:22 PM, Luke Paireepinart wrote:

> Eric Abrahamsen wrote:
>> I'm trying to learn the fundamentals of cgi scripting, before  
>> moving  on to actually using the cgi module and, eventually,  
>> mod_python. I've  grasped that, if users submit a form to my cgi  
>> script, I can read the  form contents from os.environ 
>> ['QUERY_STRING'] if it was stuck in the  URL via method=get, and  
>> from sys.stdin if it was sent via method=post.
>>
>> The get method works fine, but when it comes to post I can't  
>> actually  read anything off sys.stdin.
> Yeah, you can.
>> What's particularly odd is that in my  test script below, the  
>> returnform() function should only be called if  len(sys.stdin.read 
>> ())!=0, and yet when I submit a word and the script  runs  
>> returnform(), it tells me that len(sys.stdin.read()) is equal to   
>> 0. If that's the case, how did returnform() get called in the  
>> first  place? Why didn't it just re-run printform()?
>>
> Because you're confused about what sys.stdin.read() does.
> Imagine stdin is a string:
> "Hello, Eric!"
> now if I call
> sys.stdin.read(5)
> it will return
> "Hello,"
> and the contents of sys.stdin.read() will now be
> " Eric!"
> or, to be more accurate, the current-position pointer will be set  
> to offset 5, so that future reads will start at that position.
>
> To understand why they would do it this way, consider you want to  
> read in 5 characters at a time from a file.
> Say the file is 6 GB.  If you were to read in the whole file and  
> loop over it 5 characters at a time,
> you'd probably overflow your memory.
> However, if you tell the OS you want to open the file for reading,  
> and you then read 5 characters at a time from the file,
> you won't have to load everything into memory.  This is  
> accomplished using pointers inside the file, so you know where you
> last read.
>
> Even though sys.stdin in this case is not a file, it works the same  
> way, because it's a "File-Like Object."
> Python uses Duck Typing, wherein the feature set of a particular  
> item determines its type, rather than something arbitrary you define.
> So if any item has read(), write(), and seek() methods, you can  
> usually use these in place of file objects.
> This is true no matter what your functions actually do.
> In other words, your read() function could just count how many  
> times it's called, but do nothing with the value, or anything else
> you may want it to do.
>> At first I tried printing each line of sys.stdin to the HTML page,  
>> so  I could see the details of how post works. Nothing was  
>> printed, and  that's when I tried using len() to see whether  
>> sys.stdin contained  anything. Then, thinking that stdin was  
>> getting reset somehow,
> It's not getting reset, really.
>>  I  tried calling returnform() and directly passing in stdin as a   
>> parameter. That had the same result.
> Usually when you get to the point where you're trying random things  
> hoping something will work,
> you've reached the time when you either sleep on it or ask someone  
> for help.
> Even if you reach a solution through exhaustive testing, you still  
> haven't learned anything, which is pretty useless to you in the  
> long run.
>> Now, I'm thoroughly confused...
>>
> Don't worry about it.  It's good you asked.
>> Any help would be much appreciated.
>>
>> Yours,
>> Eric
>>
>> 
>>
>> #!/Library/Frameworks/Python.framework/Versions/Current/bin/python
>> import sys
>> import cgitb;cgitb.enable()
>>
>> def main():
>>  if len(sys.stdin.read())!=0:
>>
> This is your problem right here.
> len(sys.stdin.read()) is calling len() on a string returned by  
> sys.stdin.read()
> As mentioned earlier, calling this function sets the current- 
> location pointer to one after the last-read position.
> Since you passed no maximum to read(), the whole stdin contents  
> were read into this variable, leaving
> the pointer at the end of the file.  Thus on subsequent calls,  
> sys.stdin.read() will return nothing.
> Try changing this to len(sys.stdin.read(3)) and then pass your  
> program a string longer than 3 characters.
> Your length output in returnform should then be 3 less than your  
> expected value, or 0, whichever is greater.
>>  returnform()
>>  else:
>>  printform()
>>
>> def printform():
>>  [snip printing form]
>>
>> def returnform():
>>
> This name is a bit confusing, because the function doesn't return  
> anything.
> Perhaps display_form_contents would be a better name?
>>  print "Content-Type: text/html\n\n"
>>
> You could include this in the block print statement, if you  
> wanted.  It's fine to keep the header separate, though,
> and probably a good idea.
>>  print """
>>  
>>  
>>  
>>  Here's

Re: [Tutor] All Apress Titles 50% off sale

2007-10-16 Thread bhaaluu
On 10/15/07, Dick Moores <[EMAIL PROTECTED]> wrote:
> 
> That includes some titles that aren't out yet, such as:
>
> The Definitive Guide to Django: Web Development Done Right
> 
>
> Beginning Game Development with Python and Pygame: From Novice to Professional
> 
>
> Dick Moores

FYI:
Will McGugan, author of _Beginning Game Development with Python and
Pygame_ has a free chapter available:
http://www.willmcgugan.com/2007/10/04/free-chapter-of-beginning-game-development-with-python-and-pygame/
It is Chapter 7, "Take Me to Your Leader" which discusses Artificial
Intelligence.
There is also a download of sample source code for the Ant Simulation as well
as a required gameobjects library. Chapter 7 is a PDF file, and the other files
are ZIP archived. The Table of Contents of the book can be viewed at:
http://www.willmcgugan.com/2007/10/07/table-of-contents-for-pygame-book/
The book will be available October 24th.

Happy Programming!
-- 
b h a a l u u at g m a i l dot c o m
http://www.geocities.com/ek.bhaaluu/index.html
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] symbol encoding and processing problem

2007-10-16 Thread Tim Michelsen
Dear list,
I have encountered a problem with encoding of user input and variables.

I want to read in user defined coordinates as a string like: 121° 55' 5.55''
Furthermore I would like to extract the degrees (integer number before the " ° "
sign), the minutes (integer number before the " ' " sign) and the seconds
(floating point number before the " '' " sign).

When reading and processing the degree part I get some errors:

Heres my test script:

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 from easygui import easygui
 4 import sys
 5 #raw = sys.argv[1]
 6 raw = easygui.enterbox(message="Enter something.", title="",
argDefaultText="20° 12' 33''")
 7 #unicode = unicode(raw)
 8 #conv = raw.encoding('latin-1')
 9 split = raw.split('°')
10 out = raw
11 print out
12 easygui.msgbox(out)

Here ist my output:

20° 12' 33''
Traceback (most recent call last):
  File "C:\python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
  File "D:\python\scripts\encoding-test.py", line 22, in ?
split = raw.split('°')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 0: ordinal
not in range(128)
Traceback (most recent call last):
  File "C:\python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
  File "D:\python\scripts\encoding-test.py", line 22, in ?
split = raw.split('°')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 0: ordinal
not in range(128)

Therefore my question:
* How can I split at the "°" without getting a charater encoding error?
* How do I have to encode the "°"-symbol that it gets correctly displayed in the
Easygui msgbox at line 6?

Thanks inadvance for answering,
Timmie


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


Re: [Tutor] All Apress Titles 50% off sale

2007-10-16 Thread Kent Johnson
Dick Moores wrote:
> 

Broken at the moment...but to clarify, IIUC it is Bookpool that is 
having the sale, not Apress.
http://www.bookpool.com/

Kent

> That includes some titles that aren't out yet, such as:
> 
> The Definitive Guide to Django: Web Development Done Right
> 
> 
> Beginning Game Development with Python and Pygame: From Novice to Professional
> 
> 
> Dick Moores
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

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


Re: [Tutor] Decimal Conversion

2007-10-16 Thread Linpeiheng
There is one small mistake in Michael's answer. Variable 'r' should not 
be put behind but ahead. So operator '+=' could not be used here. You can use 
the following statement instead.

b = r + ',' + b

Operator '+' concatenate string together to make a new string. In this 
statement it concatenate three strings together, 'r' and 'b' are str type 
varible, ',' is str type value.


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


Re: [Tutor] All Apress Titles 50% off sale

2007-10-16 Thread Dick Moores
At 04:55 AM 10/16/2007, Kent Johnson wrote:
>Dick Moores wrote:
> > 
>
>Broken at the moment...but to clarify, IIUC it is Bookpool that is
>having the sale, not Apress.
>http://www.bookpool.com/

Certainly is. Thanks, Kent.

Dick


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


Re: [Tutor] symbol encoding and processing problem

2007-10-16 Thread Evert Rol
   Hi Tim,

> Heres my test script:
>
>  1 #!/usr/bin/env python
>  2 # -*- coding: utf-8 -*-
>  3 from easygui import easygui
>  4 import sys
>  5 #raw = sys.argv[1]
>  6 raw = easygui.enterbox(message="Enter something.", title="",
> argDefaultText="20° 12' 33''")
>  7 #unicode = unicode(raw)
>  8 #conv = raw.encoding('latin-1')
>  9 split = raw.split('°')
> 10 out = raw
> 11 print out
> 12 easygui.msgbox(out)
>
> Here ist my output:
>
> 20° 12' 33''
> Traceback (most recent call last):
>   File "C:\python24\Lib\site-packages\pythonwin\pywin\framework 
> \scriptutils.py",
> line 310, in RunScript
> exec codeObject in __main__.__dict__
>   File "D:\python\scripts\encoding-test.py", line 22, in ?
> split = raw.split('°')
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in  
> position 0: ordinal
> not in range(128)
> Traceback (most recent call last):
>   File "C:\python24\Lib\site-packages\pythonwin\pywin\framework 
> \scriptutils.py",
> line 310, in RunScript
> exec codeObject in __main__.__dict__
>   File "D:\python\scripts\encoding-test.py", line 22, in ?
> split = raw.split('°')
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in  
> position 0: ordinal
> not in range(128)
>
> Therefore my question:
> * How can I split at the "°" without getting a charater encoding  
> error?
> * How do I have to encode the "°"-symbol that it gets correctly  
> displayed in the
> Easygui msgbox at line 6?

I don't know all the details about unicode, but here's what works for  
me:

# -*- coding: utf-8 -*-
import easygui
raw = unicode("121° 55' 5.55''", 'utf-8')
print raw.encode('utf-8')
lines = raw.split(unicode('°', 'utf-8'))
print lines
easygui.enterbox(message="Enter something.", title="",  
argDefaultText=raw)

So you may need to explicitly define the encoding (and encode the  
degree sign in the split argument as well). Google a bit for Python  
and unicode to get some more info, if you didn't do so already. (I'm  
really hoping that with Python 3 all this messy stuff does go away.)

I had no problem with the easygui box, either my way or hardcoding  
the string to argDefaultText as you did. Perhaps it's an underlying  
problem with Tkinter, which may not support unicode on your system? A  
simple test (not sure how definite that would be), is to start up a  
Python cmdline, and do
 >>> import Tkinter
 >>> Tkinter._test()

And see if you get a ç.


Good luck,

   Evert

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


Re: [Tutor] symbol encoding and processing problem

2007-10-16 Thread Kent Johnson
Tim Michelsen wrote:
> Dear list,
> I have encountered a problem with encoding of user input and variables.

> Heres my test script:

Posting without the line numbers would make it easier to try your code.
> 
>  1 #!/usr/bin/env python
>  2 # -*- coding: utf-8 -*-
>  3 from easygui import easygui
>  4 import sys
>  5 #raw = sys.argv[1]
>  6 raw = easygui.enterbox(message="Enter something.", title="",
> argDefaultText="20° 12' 33''")
>  7 #unicode = unicode(raw)
>  8 #conv = raw.encoding('latin-1')
>  9 split = raw.split('°')

try a unicode string: split = raw.split(u'°')
> 10 out = raw
> 11 print out
> 12 easygui.msgbox(out)
> 
> Here ist my output:
> 
> 20° 12' 33''

How do you get this output? The print is after the statement causing the 
traceback. Are you showing the same code as you ran?

> Traceback (most recent call last):
>   File 
> "C:\python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
> line 310, in RunScript
> exec codeObject in __main__.__dict__
>   File "D:\python\scripts\encoding-test.py", line 22, in ?
> split = raw.split('°')
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 0: 
> ordinal
> not in range(128)
> Traceback (most recent call last):
>   File 
> "C:\python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
> line 310, in RunScript
> exec codeObject in __main__.__dict__
>   File "D:\python\scripts\encoding-test.py", line 22, in ?
> split = raw.split('°')
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 0: 
> ordinal
> not in range(128)
> 
> Therefore my question:
> * How can I split at the "°" without getting a charater encoding error?
> * How do I have to encode the "°"-symbol that it gets correctly displayed in 
> the
> Easygui msgbox at line 6?

It displays correctly for me (on MacOS X). Are you sure your source is 
actually encoded in utf-8? What platform are you on?

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


Re: [Tutor] symbol encoding and processing problem

2007-10-16 Thread Tim Michelsen
> How do you get this output? The print is after the statement causing the 
> traceback. Are you showing the same code as you ran?
Yes.
I created this file in PythonWin and run it with IPython.

> It displays correctly for me (on MacOS X). Are you sure your source is 
> actually encoded in utf-8?
Not really.
The thing is that I am exchanging my files from Linux (Ubuntu with UTF-8
default) to Windows.
> What platform are you on?
Yesterday I run the code on windows.

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


Re: [Tutor] Timers in Python

2007-10-16 Thread Trilok Khairnar
Doug Hellmann's PyMotW (Python Module of the Week) series recently covered
sched
http://feeds.feedburner.com/~r/PyMOTW/~3/161303668/pymotw-sched.html

He always provides an overview supported by examples.
You think of a functionality in some context, and it tends to appear in the
series. :-)

Regards,
Trilok.


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Noufal
Ibrahim
Sent: Thursday, October 04, 2007 10:53 PM
To: Kamal
Cc: tutor@python.org
Subject: Re: [Tutor] Timers in Python

Kent Johnson wrote:
> Kamal wrote:
>> hello everyone,
>>
>> Is there a method in Python which does what
>> setInterval('someFunction()',5000) does in Javascript.
>>
>> Basically, I want to call functionOne() every x minutes, and
>> wondering whats the best way to do it.

You can also look at the sched module that comes with the standard
distribution.




--
~noufal
http://nibrahim.net.in/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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


[Tutor] Regex parsing and writing to file

2007-10-16 Thread Peter Mexbacher
Hello,

first post here :-)

I have the following task:

1) read in a file line by line
2) parse each line with a regular expression, and substitute certain 
patterns
3) end result: the old file with the substituted stuff (and of course 
everything which did not need substitution)

My question:

Do I have to write each line out to a new file, and then rename, or can
I substitute "in place" - that the changes are incorporated in the old
file automatically?

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


Re: [Tutor] Regex parsing and writing to file

2007-10-16 Thread Tim Golden
Peter Mexbacher wrote:
> Hello,
> 
> first post here :-)
> 
> I have the following task:
> 
> 1) read in a file line by line
> 2) parse each line with a regular expression, and substitute certain 
> patterns
> 3) end result: the old file with the substituted stuff (and of course 
> everything which did not need substitution)
> 
> My question:
> 
> Do I have to write each line out to a new file, and then rename, or can
> I substitute "in place" - that the changes are incorporated in the old
> file automatically?

The "write out to new file" approach is the generally accepted
method. FWIW, if the file were small enough, you could read
it in in its entirety and overwrite immediately without the
rename step, but you obviously have no fallback then if things
go wrong!

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


Re: [Tutor] Regex parsing and writing to file

2007-10-16 Thread Kent Johnson
Peter Mexbacher wrote:

> I have the following task:
> 
> 1) read in a file line by line
> 2) parse each line with a regular expression, and substitute certain 
> patterns
> 3) end result: the old file with the substituted stuff (and of course 
> everything which did not need substitution)
> 
> Do I have to write each line out to a new file, and then rename, or can
> I substitute "in place" - that the changes are incorporated in the old
> file automatically?

If you use the fileinput module with inplace=1 it will take care of this 
for you and even make a backup file if you like.

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