Re: [Tutor] encode question!

2005-09-27 Thread ZIYAD A. M. AL-BATLY
On Tue, 2005-09-27 at 14:01 +0800, 铁石 wrote: 
> I am trying to write a stript that extract jpg files
>  from a html I had downloaded.I encounter a problem with
>  a Big5 charset html file.Big5 used in Hongkong ans Taiwan.
> In this html file there's a jpg names "xvg_h%202.jpg"
> in vi ,the tag of the image is  As I try to test the size of this image,python report that 
> file "xvg_h%2525202.jpg" don't  exists.
>  I think %25 mean the char "%",the "%2525" was equal to 
> the "%25" in html and "%" in the shell listed file name.
> I had no idea about this encode! Is the html tag use big5 too?
> Why '%' should be code as big5, I think it was a ASCII char before
> today!
>  So, how can I read this file name correctly!
> 
> 
> [EMAIL PROTECTED]
>   2005-09-27
> 
This has nothing to do with Big5 encoding!  This is how URL are sent in
HTTP requests.  As an example: a space letter " " become "%20".

In your example above your file name is probably named "xvg_h 2.jpg".
So, what's going on?  When you viewed the image in you browser (or the
application you used to download it, even if you were using Python
scrip) the request was something like:
http://server.domain.tld/dir1/dir2/xvg_h%202.jpg
which translate to "xvg_h 2.jpg" which is right.  However, when you
saved the HTML file along with the image, the application interpreted
the "%20" as the character "percent sign" followed by the characters
"20".  It also encoded them as such (just like when sending HTTP
requests) and that resulted in "xvg_h%25202.jpg"!

How to fix this?  Use "unquote()" from the "urllib" module twice!

Here:
>>> urllib.unquote(urllib.unquote('xvg_h%25202.jpg'))
'xvg_h 2.jpg'

I hope this is the right explanation and that it will work for you.  If
anyone have a better opinion please don't be shy and help us all.
Ziyad.

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


Re: [Tutor] 2 questions......novice but not beginner

2005-09-27 Thread Danny Yoo


On Mon, 26 Sep 2005, Mike Pippin wrote:

> I need to know how to allow the user to search their local drives and
> directories in order to load a file...or if there are any pre-written
> opensource functions that will also be well appreciated.. Second

Hi Mike,

This is not completely obvious to do efficiently: that's what tools like
Google Desktop, Apple's Spotlight, and Microsoft's WinFS are trying to
enable.  If you can tell us what operating system you' re using, we might
be able to point you toward something.

If efficiency doesn't matter, you can brute force a search with os.walk:

http://www.python.org/doc/lib/os-file-dir.html#l2h-1628

However, it's preferable not to use this as a general-purpose searching
utility, as it's very expensive to walk across a system's directory tree
structure like this: that's why using something OS specific might be a
good idea here.  If you're on Unix, you might even want to take advantage
of something like 'locate' or 'glimpse', which can maintain a quick
searchable index of your computer's files.

Can you limit the scope of the search somehow to a particular set of
directories, or do you have to search your whole hard drive?



> I need to know how to render graphics without the windowed
> enviroment.I know with pygame you can have a windowless enviroment
> but I need to be able to have only the graphics i choose displayed on
> the screen without pygame filling the rest of the screen with black if
> thats possible.

Out of curiosity, why?  I don't know offhand how to do this, but the
pygame folks might: have you asked them yet?

Could you just capture the screen first, start up pygame, and then draw
the screen-captured image there?  That could provide an illusion of having
the game run on top of the desktop environment.  It's cheap, but it might
work.  *grin*

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


[Tutor] Simulating case statement

2005-09-27 Thread Jan Eden
Hi,

I used to use an extended if...elif sequence to instantiate an object and call 
this object's display method afterwards:

if safe['type'] == pages:
page = Show.Page(id=safe['id'], start=safe['start'] ...),
elif safe['type'] == pages:
author = Show.Author(id=safe['id']...)
 ...

page.Display()
 

To improve readability, I changed this code to use a dictionary:


valid_types = dict(
pages=Show.Page,
authors=Show.Author,
...
)

page = valid_types[safe_parameters['type']](safe_parameters)
page.Display()


The problem is that the __init__ methods of the respective classes take a 
different number of parameters - this is why I pass the whole safe_parameters 
dictionary.

This has a number of drawbacks when instantiating an object in other situations 
because I cannot use a default for some parameters while passing some others.

So I'd like to do pass the parameters individually, based on the class. I know 
I would need to expand the valid_types dictionary to include the parameters - 
but how can I pass these one by one?

What I came up with is a monster (which does not work anyway):

valid_types = dict(
pages=dict(klasse=Show.Page, parameters=dict(id=safe['id'], 
start=safe['start'] ...))
authors=dict(klasse=Show.Author, ...)
...
)

page = valid_types[safe_parameters['type']]['klasse'](valid_types['parameters'])

page.Display()

How can I circumvent the if...elif sequence and have the parameters passed 
individually at the same time?

Thanks for any suggestions,

Jan
-- 
Common sense is what tells you that the world is flat.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2 questions......novice but not beginner

2005-09-27 Thread Liam Clarke
> I need to know how to render graphics without the windowed enviroment.I
> know with pygame you can have a windowless enviroment but I need to be able
> to have only the graphics i choose displayed on the screen without pygame
> filling the rest of the screen with black if thats possible.

I believe to do this in Windows requires usage of the COM interface. I
know it's how
Konfabulator/ObjectDesktop etc. draw their widgets without little windows.

FWIW.

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


Re: [Tutor] 2 questions......novice but not beginner

2005-09-27 Thread R. Alan Monroe
> I need to know how to render graphics without the windowed enviroment.I
> know with pygame you can have a windowless enviroment but I need to be able
> to have only the graphics i choose displayed on the screen without pygame
> filling the rest of the screen with black if thats possible.


Not sure if this is what you mean, but pygame can run within a normal
window (and you choose the size). It doesn't HAVE to be fullscreen. Is
that what you mean?


Alan

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


Re: [Tutor] Challenge [was Re: Why won't it enter the quiz?]

2005-09-27 Thread Adam
Well Nathan you said you wanted an honest opinion so here goes. All the
program seems to do is create a clear text file that is world readable.
I personally wouldn't even use it if it was free because it would be a
lot easier to use a text editor. Now for improvements that would make
it a worthwhile program. Some sort of encryption would be good along
with making the file readable only to the owner where possible. Also
for the majority of users a GUI would probably be prefereable although
I personally have no problem with command line programs. Oh and don't
put the main password in the source code.On 27/09/05, Nathan Pinno <[EMAIL PROTECTED]> wrote:







No problem 
Adam. Glad you decided to take a look and see. Tell the group what your honest 
opinion is after looking at the file.

  - Original Message - 
  
From: 
  Adam 
  
  To: 
Nathan Pinno 
  Cc: 
bob ; tutor@python.org 
  Sent: Monday, September 26, 2005 3:37 
  PM
  Subject: Re: [Tutor] Challenge [was Re: 
  Why won't it enter the quiz?]
  Can I have a look at the password program by any 
chance?
  
  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] Challenge [was Re: Why won't it enter the quiz?]

2005-09-27 Thread Adam
There's one more problem as well Nathan. Most if not all of the
programs you have made have been made by other people as well albeit
implemented slightly differently and you could probably get a
replacement for any of your programs for free on sourceforge. If you
want to make any serious money you'll need something more original or
just a lot better than the competition.
On 27/09/05, 
Nathan Pinno <[EMAIL PROTECTED]> wrote:







No problem 
Adam. Glad you decided to take a look and see. Tell the group what your honest 
opinion is after looking at the file.

  - Original Message - 
  

From: 
  Adam 
  
  To: 

Nathan Pinno 
  Cc: 

bob ; tutor@python.org 
  Sent: Monday, September 26, 2005 3:37 
  PM
  Subject: Re: [Tutor] Challenge [was Re: 
  Why won't it enter the quiz?]
  Can I have a look at the password program by any 
chance?
  
  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] Simulating case statement

2005-09-27 Thread Kent Johnson
Jan Eden wrote:
> Hi,
> 
> I used to use an extended if...elif sequence to instantiate an object and 
> call this object's display method afterwards:
> 
> if safe['type'] == pages:
> page = Show.Page(id=safe['id'], start=safe['start'] ...),
> elif safe['type'] == pages:
> author = Show.Author(id=safe['id']...)
>  ...
> 
> page.Display()
>  
> 
> To improve readability, I changed this code to use a dictionary:
> 
> 
> valid_types = dict(
> pages=Show.Page,
> authors=Show.Author,
> ...
> )
> 
> page = valid_types[safe_parameters['type']](safe_parameters)
> page.Display()
> 
> 
> The problem is that the __init__ methods of the respective classes
> take a different number of parameters - this is why I pass the whole
> safe_parameters dictionary.

I think if you pass the dictionary as keyword arguments rather than as a single 
dict you will get what you want.
page = valid_types[safe_parameters['type']](**safe_parameters)

This syntax means, use safe_parameters to populate the keyword arguments of the 
function. Any parameters which are not in safe_parameters will be set to their 
default values, and in other calls you can set the parameters as you like. Here 
is a simple example:

 >>> def f1(id='123', value='abc', **kwds):
 ...   print 'id =', id
 ...   print 'value =', value
 ...
 >>> def f2(id='345', stuff='nonsense', **kwds):
 ...   print 'id =', id
 ...   print 'stuff =', stuff
 ...
 ...
 >>> params = dict(id=3, value='def')
 >>> f1(**params)
id = 3
value = def
 >>> f2(**params)
id = 3
stuff = nonsense
 >>> f1()
id = 123
value = abc
 >>> f1(value=34)
id = 123
value = 34

The **kwds parameter to the function is needed to allow the extra parameters in 
the passed dictionary. Otherwise you will get a TypeError:

 >>> def f3(id='345', stuff='nonsense'):
 ...   print 'id =', id
 ...   print 'stuff =', stuff
 ...
 >>> f3(**params)
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: f3() got an unexpected keyword argument 'value'

Kent

> 
> This has a number of drawbacks when instantiating an object in other
> situations because I cannot use a default for some parameters while
> passing some others.
> 
> So I'd like to do pass the parameters individually, based on the
> class. I know I would need to expand the valid_types dictionary to
> include the parameters - but how can I pass these one by one?> 
> What I came up with is a monster (which does not work anyway):
> 
> valid_types = dict(
> pages=dict(klasse=Show.Page, parameters=dict(id=safe['id'], 
> start=safe['start'] ...))
> authors=dict(klasse=Show.Author, ...)
> ...
> )
> 
> page = 
> valid_types[safe_parameters['type']]['klasse'](valid_types['parameters'])
> 
> page.Display()
> 
> How can I circumvent the if...elif sequence and have the parameters passed 
> individually at the same time?
> 
> Thanks for any suggestions,
> 
> Jan

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


Re: [Tutor] Simulating case statement

2005-09-27 Thread Jan Eden
Kent Johnson wrote on 27.09.2005:

>Jan Eden wrote:

>>The problem is that the __init__ methods of the respective classes
>>take a different number of parameters - this is why I pass the
>>whole safe_parameters dictionary.
>
>I think if you pass the dictionary as keyword arguments rather than
>as a single dict you will get what you want.

>page =
>valid_types[safe_parameters['type']](**safe_parameters)
>
>This syntax means, use safe_parameters to populate the keyword
>arguments of the function. Any parameters which are not in
>safe_parameters will be set to their default values, and in other
>calls you can set the parameters as you like. Here is a simple
>example:

I see. I did not know that I can use a formal parameter **param in calls - 
thought I could only do so in function definitions:

def func(**param):
...

 func(id=1, stuff='blah')
 
 Thanks for that!
 
 Jan
-- 
Mac OS X. Because making Unix user-friendly is easier than debugging Windows.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] encode question!

2005-09-27 Thread 铁石
tutor-request
 Thanks Ziyad.You answer is exactly the realson it is! I am missdirect
by the coding of the html. the %25 in the url is the '%' char in file name.
after  
urllib.unquote('xvg_h%25202.jpg')
'xvg_h%202.jpg'
That's the real filename I want!
thanks too, Danny Yoo!

>This has nothing to do with Big5 encoding!  This is how URL are sent in
>HTTP requests.  As an example: a space letter " " become "%20".
>
>In your example above your file name is probably named "xvg_h 2.jpg".
>So, what's going on?  When you viewed the image in you browser (or the
>application you used to download it, even if you were using Python
>scrip) the request was something like:
>http://server.domain.tld/dir1/dir2/xvg_h%202.jpg
>which translate to "xvg_h 2.jpg" which is right.  However, when you
>saved the HTML file along with the image, the application interpreted
>the "%20" as the character "percent sign" followed by the characters
>"20".  It also encoded them as such (just like when sending HTTP
>requests) and that resulted in "xvg_h%25202.jpg"!
>
>How to fix this?  Use "unquote()" from the "urllib" module twice!
>
>Here:
>>>> urllib.unquote(urllib.unquote('xvg_h%25202.jpg'))
>'xvg_h 2.jpg'
>
>I hope this is the right explanation and that it will work for you.  If
>anyone have a better opinion please don't be shy and help us all.
>Ziyad.
>
>

[EMAIL PROTECTED]
  2005-09-27

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


[Tutor] Linking with C programs

2005-09-27 Thread Matt Williams
Dear List,

Could someone explain how, in very general terms, one would use python
to wrap some C libraries/ API. 

I ask because there are a few bits of C software that look quite
interesting, and I know that Python can be used to wrap the C - but how
does it work?

Thanks,

Matt

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


[Tutor] ICMP checksum

2005-09-27 Thread the taloner
Hi list,I was just playing  around with one of the
examples given in Python cook-book. This examples show you how to send
data using ICMP packets. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439224While running the script I encountered a problem which was with the statement 

icmp.set_icmp_cksum(0)The error encountered was Traceback (most recent call last):  File "icmpdata.py", line 38, in ?icmp.set_icmp_chksum(0)AttributeError: ICMP instance has no attribute 'set_icmp_chksum'
So
I did the next logical thing, commented the line in code (which appears
twice). Script ran smoothly and I captured the traffic using tcpdump.When
the script sends first 54 bytes everything is good. When I send a
longer data string, and the script is forced to split the data over
multiple packets I start fasing a problem with checksum.17:36:36.198543
IP (tos 0x0, ttl 255, id 16951, offset 0, flags [none], length: 82)
192.168.3.37 > 192.168.3.36: icmp 62: echo reply seq 00x:  0002
3fb9 17fe 000c 299c e8a5 0800 4500  ..?.).E.0x0010:  0052
4237  ff01 f1d9 c0a8 0325 c0a8  .RB7.%..0x0020:  0324
 bab9 0001  6161 6161 6161  .$aa0x0030:  6161
6161 6161 6161 6161 6161 6161 6161  0x0040:  6161
6161 6161 6161 6161 6161 6161 6161  0x0050:  6161
6161 6161 6161 6161 6161 6161 6161  17:36:37.198470
IP (tos 0x0, ttl 255, id 16952, offset 0, flags [none], length: 82)
192.168.3.37 > 192.168.3.36: icmp 62: echo reply seq 0 (wrong icmp
cksum bab9 (->bab8)!)0x:  0002
3fb9 17fe 000c 299c e8a5 0800 4500  ..?.).E.0x0010:  0052
4238  ff01 f1d8 c0a8 0325 c0a8  .RB8.%..0x0020:  0324
 bab9 0002  6161 6161 6161  .$aa0x0030:  6161
6161 6161 6161 6161 6161 6161 6161  0x0040:  6161
6161 6161 6161 6161 6161 6161 6161  0x0050:  6161
6161 6161 6161 6161 6161 6161 6161  
Also as you can see that seq is not increasing as ideally it should have been as directed by this line:

seq_id = seq_id + 1	
Can someone give some light on these issues?

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


Re: [Tutor] ICMP checksum

2005-09-27 Thread Kent Johnson
the taloner wrote:
> Hi list,
> 
> I was just playing  around with one of the examples given in Python 
> cook-book. This examples show you how to send data using ICMP packets.
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439224
> 
> While running the script I encountered a problem which was with the 
> statement
> 
> icmp.set_icmp_cksum(0)
> 
> The error encountered was
> 
> Traceback (most recent call last):
>   File "icmpdata.py", line 38, in ?
> icmp.set_icmp_chksum(0)
> AttributeError: ICMP instance has no attribute 'set_icmp_chksum'

This recipe uses a package called impacket which is available from
http://oss.coresecurity.com/projects/impacket.html

Looking at the implementation of ICMP in the latest release of ImpactPacket.py, 
the correct spelling of the method is 'set_icmp_cksum'

Kent

> 
> 
> So I did the next logical thing, commented the line in code (which 
> appears twice). Script ran smoothly and I captured the traffic using 
> tcpdump.
> When the script sends first 54 bytes everything is good. When I send a 
> longer data string, and the script is forced to split the data over 
> multiple packets I start fasing a problem with checksum.

That shouldn't be too surprising if you commented out the line that sets the 
checksum!

> 
> 17:36:36.198543 IP (tos 0x0, ttl 255, id 16951, offset 0, flags [none], 
> length: 82) 192.168.3.37  > 192.168.3.36 
> : icmp 62: echo reply seq 0
> 0x:  0002 3fb9 17fe 000c 299c e8a5 0800 4500  ..?.).E.
> 0x0010:  0052 4237  ff01 f1d9 c0a8 0325 c0a8  .RB7.%..
> 0x0020:  0324  bab9 0001  6161 6161 6161  .$aa
> 0x0030:  6161 6161 6161 6161 6161 6161 6161 6161  
> 0x0040:  6161 6161 6161 6161 6161 6161 6161 6161  
> 0x0050:  6161 6161 6161 6161 6161 6161 6161 6161  
> 17:36:37.198470 IP (tos 0x0, ttl 255, id 16952, offset 0, flags [none], 
> length: 82) 192.168.3.37  > 192.168.3.36 
> : icmp 62: echo reply seq 0 (wrong icmp cksum bab9 
> (->bab8)!)
> 0x:  0002 3fb9 17fe 000c 299c e8a5 0800 4500  ..?.).E.
> 0x0010:  0052 4238  ff01 f1d8 c0a8 0325 c0a8  .RB8.%..
> 0x0020:  0324  bab9 0002  6161 6161 6161  .$aa
> 0x0030:  6161 6161 6161 6161 6161 6161 6161 6161  
> 0x0040:  6161 6161 6161 6161 6161 6161 6161 6161  
> 0x0050:  6161 6161 6161 6161 6161 6161 6161 6161  
> 
> Also as you can see that seq is not increasing as ideally it should have 
> been as directed by this line:
> 
> seq_id = seq_id + 1
> 
> Can someone give some light on these issues?
> 
> Thanks
> 
> 
> 
> 
> ___
> 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] Linking with C programs

2005-09-27 Thread Eric Walker
There is a program called swig. this generates python interfaces to external 
C/C++ libraries. I never used it as I am a very new python programmer but I 
took the Mark Lutz class and he pushes it.

Python Newbie


On Tuesday 27 September 2005 07:49 am, Matt Williams wrote:
> Dear List,
>
> Could someone explain how, in very general terms, one would use python
> to wrap some C libraries/ API.
>
> I ask because there are a few bits of C software that look quite
> interesting, and I know that Python can be used to wrap the C - but how
> does it work?
>
> Thanks,
>
> Matt
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Eric Walker
EDA/CAD Engineer
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] find data in html file

2005-09-27 Thread lmac
Hi there,
i have a base-question. If i want to read some kind of data out of a line
which i know the start-tag and the end-tag in an html-file how do i 
recognize
if it's more than one line ?

Example:

Some textlinktext . DATA  etc.

I would use >text as the starting tag to localize the beginning of the DATA.
And then  as the ending tag of the DATA. But if there is \n then 
there are more than
one line.

I hope i explained it well what i am going for. English is not my native 
language.

Thank you.

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


Re: [Tutor] ICMP checksum

2005-09-27 Thread the taloner
Thanks Kent,

It worked for the checksum.

Only thing that i am still confused with is the part where sequence
numbers are concerned. In the tcpdump o/p I still see seq as 0 over
multiple packets. 

RegardsOn 9/27/05, Kent Johnson <[EMAIL PROTECTED]> wrote:
the taloner wrote:> Hi list,>> I was just playing  around with one of the examples given in Python> cook-book. This examples show you how to send data using ICMP packets.> 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439224>> While running the script I encountered a problem which was with the> statement>> icmp.set_icmp_cksum(0)>> The error encountered was
>> Traceback (most recent call last):>   File "icmpdata.py", line 38, in ?> icmp.set_icmp_chksum(0)> AttributeError: ICMP instance has no attribute 'set_icmp_chksum'
This recipe uses a package called impacket which is available fromhttp://oss.coresecurity.com/projects/impacket.htmlLooking
at the implementation of ICMP in the latest release of ImpactPacket.py,
the correct spelling of the method is 'set_icmp_cksum'Kent>>> So I did the next logical thing, commented the line in code (which> appears twice). Script ran smoothly and I captured the traffic using
> tcpdump.> When the script sends first 54 bytes everything is good. When I send a> longer data string, and the script is forced to split the data over> multiple packets I start fasing a problem with checksum.
That shouldn't be too surprising if you commented out the line that sets the checksum!>> 17:36:36.198543 IP (tos 0x0, ttl 255, id 16951, offset 0, flags [none],> length: 82) 
192.168.3.37  > 192.168.3.36> : icmp 62: echo reply seq 0
>
0x:  0002 3fb9 17fe 000c 299c e8a5 0800
4500  ..?.).E.>
0x0010:  0052 4237  ff01 f1d9 c0a8 0325
c0a8  .RB7.%..>
0x0020:  0324  bab9 0001  6161 6161
6161  .$aa>
0x0030:  6161 6161 6161 6161 6161 6161 6161
6161  >
0x0040:  6161 6161 6161 6161 6161 6161 6161
6161  >
0x0050:  6161 6161 6161 6161 6161 6161 6161
6161  > 17:36:37.198470 IP (tos 0x0, ttl 255, id 16952, offset 0, flags [none],> length: 82) 192.168.3.37  > 192.168.3.36> : icmp 62: echo reply seq 0 (wrong icmp cksum bab9> (->bab8)!)>
0x:  0002 3fb9 17fe 000c 299c e8a5 0800
4500  ..?.).E.>
0x0010:  0052 4238  ff01 f1d8 c0a8 0325
c0a8  .RB8.%..>
0x0020:  0324  bab9 0002  6161 6161
6161  .$aa>
0x0030:  6161 6161 6161 6161 6161 6161 6161
6161  >
0x0040:  6161 6161 6161 6161 6161 6161 6161
6161  >
0x0050:  6161 6161 6161 6161 6161 6161 6161
6161  >> Also as you can see that seq is not increasing as ideally it should have> been as directed by this line:>> seq_id = seq_id + 1>> Can someone give some light on these issues?
>> Thanks>>> >> ___> Tutor maillist  -  
Tutor@python.org> http://mail.python.org/mailman/listinfo/tutor___Tutor maillist  -  
Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] HTTP GET Request

2005-09-27 Thread Jerl Simpson
Hello,

I have been looking through some of the HTTP projects and haven't quite found what I'm looking for.
Basicall, what I need is a stand alone CGI.  Instead of the
program passing the data off to a CGI, I want it to parse and handle
the request directly.

The part I'm having trouble with is actually getting the request and parsing it.

Let's say I have a URI that looks like:  ?var1=val1&var2=val2&...varn=valn

I'd like to find a way to get these into some datastructure so I can use them to generate my output.

It seems like a simple thing, but as I'm new to python, I don't know where to start.

Thank you for any help you can give.


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


Re: [Tutor] Linking with C programs

2005-09-27 Thread Gabriel Farrell
You can read some of what Guido has to say about Python and C at
http://python.org/doc/essays/omg-darpa-mcc-position.html
and see SWIG documentation (a few pertain to Python) at
http://www.swig.org/doc.html

gsf


On Tue, Sep 27, 2005 at 08:41:31AM -0600, Eric Walker wrote:
> There is a program called swig. this generates python interfaces to external 
> C/C++ libraries. I never used it as I am a very new python programmer but I 
> took the Mark Lutz class and he pushes it.
> 
> Python Newbie
> 
> 
> On Tuesday 27 September 2005 07:49 am, Matt Williams wrote:
> > Dear List,
> >
> > Could someone explain how, in very general terms, one would use python
> > to wrap some C libraries/ API.
> >
> > I ask because there are a few bits of C software that look quite
> > interesting, and I know that Python can be used to wrap the C - but how
> > does it work?
> >
> > Thanks,
> >
> > Matt
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> 
> -- 
> Eric Walker
> EDA/CAD Engineer
> ___
> 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] Linking with C programs

2005-09-27 Thread Kent Johnson
Matt Williams wrote:
> Dear List,
> 
> Could someone explain how, in very general terms, one would use python
> to wrap some C libraries/ API. 

The "Extending and Embedding" document is the official doc:
http://docs.python.org/ext/ext.html

In addition to SWIG I have heard good things about SIP:
http://directory.fsf.org/devel/prog/cpp/Python-SIP.html

ctypes is another approach which lets you call C libraries directly from Python:
http://starship.python.net/crew/theller/ctypes/

Kent

> 
> I ask because there are a few bits of C software that look quite
> interesting, and I know that Python can be used to wrap the C - but how
> does it work?
> 
> Thanks,
> 
> Matt
> 
> ___
> 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] HTTP GET Request

2005-09-27 Thread paul brian
> Basicall, what I need is a stand alone CGI.  Instead of the program passing
> the data off to a CGI, I want it to parse and handle the request directly.

instead of which program ?

Http requests are served by a web server (ie Apache), which depending
on the type of request passes the request to wherever.

As such any HTTP request *must* be handled first by a web server, and
cgi scripts traditionally lived in cgi-bin directory on the server so
a URL would look like http://www.example.com/cgi-bin/myscript.py

I think you have 3 options

1. use the cgi module in python to create scripts like the one above.
They will not be fast but it gives you a lowlevel access to the request
However cgi was out of date about 8 years ago - it has some
seriouslimitations mostly on speed/capacity.

2. use a system like mod_python. This is better than cgi for lots of reasons,
mostly to do with speed. Here you also have access to the request 
 objects,   but there is a bit of a learning curve.

3. Zope - higher level than even mod_python and still more of a learning curve

(there is a multitude of python based cgi repalcements, Django,
webware and others spring to mind.  But there is no clear "winner"
amoungst the community)

I would recommend that you look at taking a weekend to install apache,
and play with both the cgi module and mod_python.  mod_python is
pretty good and fairly well documented, as well as being pretty low
level.

I think there is a lot to do here - perhaps if you tell us exactly
what you need we can point you at a solution. Some web hosters provide
mod_python or zope hosting and that might be a way to get up and
running faster.





On 9/27/05, Jerl Simpson <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I have been looking through some of the HTTP projects and haven't quite
> found what I'm looking for.
> Basicall, what I need is a stand alone CGI.  Instead of the program passing
> the data off to a CGI, I want it to parse and handle the request directly.
>
> The part I'm having trouble with is actually getting the request and parsing
> it.
>
> Let's say I have a URI that looks like:
> ?var1=val1&var2=val2&...varn=valn
>
> I'd like to find a way to get these into some datastructure so I can use
> them to generate my output.
>
> It seems like a simple thing, but as I'm new to python, I don't know where
> to start.
>
> Thank you for any help you can give.
>
>
> Jerl
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] HTTP GET Request

2005-09-27 Thread Kent Johnson
Jerl Simpson wrote:
> Hello,
> 
> I have been looking through some of the HTTP projects and haven't quite 
> found what I'm looking for.
> Basicall, what I need is a stand alone CGI.  Instead of the program 
> passing the data off to a CGI, I want it to parse and handle the request 
> directly.
> 
> The part I'm having trouble with is actually getting the request and 
> parsing it.

It sounds like you need to set up a web server. It also sounds like maybe you 
don't know that you need to set up a web server? You might want to take a look 
at CherryPy which is pretty easy to get started with. The third tutorial 
program shows how to get query parameters from the URL.
http://www.cherrypy.org

Kent

> 
> Let's say I have a URI that looks like:  ?var1=val1&var2=val2&...varn=valn
> 
> I'd like to find a way to get these into some datastructure so I can use 
> them to generate my output.
> 
> It seems like a simple thing, but as I'm new to python, I don't know 
> where to start.
> 
> Thank you for any help you can give.
> 
> 
> Jerl
> 
> 
> 
> 
> ___
> 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] Embedding

2005-09-27 Thread Joseph Quigley
hi,
I'm working on a game with other friends who don't use python. However 
they'd like to use Java Python and C/C++. I was wondering if I could 
embedd C in a Python program in Jython and lastly in java (or the other 
way arround). My goal is: Can these for languages be used to make one 
program?
Thanks,
   Joe
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Embedding

2005-09-27 Thread Kent Johnson
Joseph Quigley wrote:
> hi,
> I'm working on a game with other friends who don't use python. However 
> they'd like to use Java Python and C/C++. I was wondering if I could 
> embedd C in a Python program in Jython and lastly in java (or the other 
> way arround). My goal is: Can these for languages be used to make one 
> program?

Not easily. You can embed C in Java with JNI. You can access the result from 
Jython. But the original requirement seems a bit weird - why Java and C in one 
program? ISTM you should pick Python/C or Jython/Java but not both. Can you say 
more about what you are trying to accomplish?

(By "Java Python" do you mean "Java AND Python" or "the Java version of Python" 
e.g. Jython?)

Kent

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


Re: [Tutor] find data in html file

2005-09-27 Thread Don Arnold
--- lmac <[EMAIL PROTECTED]> wrote:

> Hi there,
> i have a base-question. If i want to read some kind
> of data out of a line
> which i know the start-tag and the end-tag in an
> html-file how do i 
> recognize
> if it's more than one line ?
> 
> Example:
> 
> Some textlinktext . DATA
>  etc.
> 
> I would use >text as the starting tag to localize
> the beginning of the DATA.
> And then  as the ending tag of the DATA. But if
> there is \n then 
> there are more than
> one line.
> 
> I hope i explained it well what i am going for.
> English is not my native 
> language.
> 
> Thank you.
> 

As you've seen, normal string-handling methods aren't
very useful for parsing complicated (or even simple)
HTML documents. For parsing these, take a look at the
HTMLParser or htmllib modules.

HTH,
Don




__ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] find data in html file

2005-09-27 Thread Kent Johnson
lmac wrote:
> Hi there,
> i have a base-question. If i want to read some kind of data out of a line
> which i know the start-tag and the end-tag in an html-file how do i 
> recognize
> if it's more than one line ?
> 
> Example:
> 
> Some textlinktext . DATA  etc.
> 
> I would use >text as the starting tag to localize the beginning of the DATA.
> And then  as the ending tag of the DATA. But if there is \n then 
> there are more than
> one line.

Beautiful Soup is good for this.
http://www.crummy.com/software/BeautifulSoup/

Kent

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


[Tutor] Quick way to find the data type

2005-09-27 Thread Bernard Lebel
Hello,

Let say I have a string. The value of the string might be 'False',
'True', '3', '1.394', or whatever else. Is there a quick way to
convert this string into the appropriate data type other than with
try/except?


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


Re: [Tutor] HTTP GET Request

2005-09-27 Thread Jerl Simpson
I don't think I explained my situation clearly enough.

Let me tell you what I'm doing.
First, I'm looking for an excuse to learn Python.

Second, I'm running a server that takes request from a remote
server.  This request is an HTTP GET request.  I used to have
(lost the source) a C program I wrote that functions as a web server.
It takes the request on port 80, parses the GET.  From the GET I construct a MySQL query and spit the results back out.

Now, the logical solution is to run Apache to handle the webserver
aspect, and have it hand off to a CGI to run the MySQL query and spit
the results back out to the requesting server.  In this situation,
I could have thousands of requests per minute, and it bogs the server
down.  To much overhead when Apache calls the CGI.  I've
tried using several different setups with Apache, and C, Perl, and PHP
CGIs.  

I found it to be much faster if I wrote the webserver, and handle the MySQL call within the same thread.

I was told by a friend of mine that Python is good for creating network
servers.  So I thought I'd give it a try...instead of rewriting my
C program I wanted to take the opportunity to learn a bit of Python.

The only piece I don't have going the Python route is actually getting the GETOn 9/27/05, paul brian <[EMAIL PROTECTED]
> wrote:> Basicall, what I need is a stand alone CGI.  Instead of the program passing
> the data off to a CGI, I want it to parse and handle the request directly.instead of which program ?Http requests are served by a web server (ie Apache), which dependingon the type of request passes the request to wherever.
As such any HTTP request *must* be handled first by a web server, andcgi scripts traditionally lived in cgi-bin directory on the server soa URL would look like 
http://www.example.com/cgi-bin/myscript.pyI think you have 3 options1. use the cgi module in python to create scripts like the one above.They will not be fast but it gives you a lowlevel access to the request
However cgi was out of date about 8 years ago - it has someseriouslimitations mostly on speed/capacity.2. use a system like mod_python. This is better than cgi for lots of reasons,mostly to do with speed. Here you also have access to the request
 objects,   but there is a bit of a learning curve.3. Zope - higher level than even mod_python and still more of a learning curve(there is a multitude of python based cgi repalcements, Django,webware and others spring to mind.  But there is no clear "winner"
amoungst the community)I would recommend that you look at taking a weekend to install apache,and play with both the cgi module and mod_python.  mod_python ispretty good and fairly well documented, as well as being pretty low
level.I think there is a lot to do here - perhaps if you tell us exactlywhat you need we can point you at a solution. Some web hosters providemod_python or zope hosting and that might be a way to get up and
running faster.On 9/27/05, Jerl Simpson <[EMAIL PROTECTED]> wrote:> Hello,>> I have been looking through some of the HTTP projects and haven't quite
> found what I'm looking for.> Basicall, what I need is a stand alone CGI.  Instead of the program passing> the data off to a CGI, I want it to parse and handle the request directly.>> The part I'm having trouble with is actually getting the request and parsing
> it.>> Let's say I have a URI that looks like:> ?var1=val1&var2=val2&...varn=valn>> I'd like to find a way to get these into some datastructure so I can use> them to generate my output.
>> It seems like a simple thing, but as I'm new to python, I don't know where> to start.>> Thank you for any help you can give.>>> Jerl>> ___
> Tutor maillist  -  Tutor@python.org> http://mail.python.org/mailman/listinfo/tutor>>>
Paul Brianm. 07875 074 534t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] HTTP GET Request

2005-09-27 Thread Jerl Simpson
Sorry, accidentally sent the last one before I was finished.
I was saying, I just lack getting the GET URI into a variable, and then
finding out the best way to parse it into it's components using Python.

Thanks again,

JerlOn 9/27/05, Jerl Simpson <[EMAIL PROTECTED]> wrote:
I don't think I explained my situation clearly enough.

Let me tell you what I'm doing.
First, I'm looking for an excuse to learn Python.

Second, I'm running a server that takes request from a remote
server.  This request is an HTTP GET request.  I used to have
(lost the source) a C program I wrote that functions as a web server.
It takes the request on port 80, parses the GET.  From the GET I construct a MySQL query and spit the results back out.

Now, the logical solution is to run Apache to handle the webserver
aspect, and have it hand off to a CGI to run the MySQL query and spit
the results back out to the requesting server.  In this situation,
I could have thousands of requests per minute, and it bogs the server
down.  To much overhead when Apache calls the CGI.  I've
tried using several different setups with Apache, and C, Perl, and PHP
CGIs.  

I found it to be much faster if I wrote the webserver, and handle the MySQL call within the same thread.

I was told by a friend of mine that Python is good for creating network
servers.  So I thought I'd give it a try...instead of rewriting my
C program I wanted to take the opportunity to learn a bit of Python.

The only piece I don't have going the Python route is actually getting the GETOn 9/27/05, paul brian <
[EMAIL PROTECTED]
> wrote:> Basicall, what I need is a stand alone CGI.  Instead of the program passing
> the data off to a CGI, I want it to parse and handle the request directly.instead of which program ?Http requests are served by a web server (ie Apache), which dependingon the type of request passes the request to wherever.
As such any HTTP request *must* be handled first by a web server, andcgi scripts traditionally lived in cgi-bin directory on the server soa URL would look like 

http://www.example.com/cgi-bin/myscript.pyI think you have 3 options1. use the cgi module in python to create scripts like the one above.They will not be fast but it gives you a lowlevel access to the request
However cgi was out of date about 8 years ago - it has someseriouslimitations mostly on speed/capacity.2. use a system like mod_python. This is better than cgi for lots of reasons,mostly to do with speed. Here you also have access to the request
 objects,   but there is a bit of a learning curve.3. Zope - higher level than even mod_python and still more of a learning curve(there is a multitude of python based cgi repalcements, Django,webware and others spring to mind.  But there is no clear "winner"
amoungst the community)I would recommend that you look at taking a weekend to install apache,and play with both the cgi module and mod_python.  mod_python ispretty good and fairly well documented, as well as being pretty low
level.I think there is a lot to do here - perhaps if you tell us exactlywhat you need we can point you at a solution. Some web hosters providemod_python or zope hosting and that might be a way to get up and
running faster.On 9/27/05, Jerl Simpson <[EMAIL PROTECTED]> wrote:
> Hello,>> I have been looking through some of the HTTP projects and haven't quite
> found what I'm looking for.> Basicall, what I need is a stand alone CGI.  Instead of the program passing> the data off to a CGI, I want it to parse and handle the request directly.>> The part I'm having trouble with is actually getting the request and parsing
> it.>> Let's say I have a URI that looks like:> ?var1=val1&var2=val2&...varn=valn>> I'd like to find a way to get these into some datastructure so I can use> them to generate my output.
>> It seems like a simple thing, but as I'm new to python, I don't know where> to start.>> Thank you for any help you can give.>>> Jerl>> ___
> Tutor maillist  -  Tutor@python.org> 
http://mail.python.org/mailman/listinfo/tutor>>>
Paul Brianm. 07875 074 534t. 0208 352 1741


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


Re: [Tutor] Quick way to find the data type

2005-09-27 Thread Kent Johnson
Bernard Lebel wrote:
> Hello,
> 
> Let say I have a string. The value of the string might be 'False',
> 'True', '3', '1.394', or whatever else. Is there a quick way to
> convert this string into the appropriate data type other than with
> try/except?

There is a nice solution here:
http://groups.google.com/group/comp.lang.python/msg/5ead7dae977ed990?hl=en&;

It doesn't avoid the try/except but it wraps it up real pretty :-)

Kent

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


Re: [Tutor] HTTP GET Request

2005-09-27 Thread Mike Hansen
> Subject:
> Re: [Tutor] HTTP GET Request
> From:
> paul brian <[EMAIL PROTECTED]>
> Date:
> Tue, 27 Sep 2005 17:04:46 +0100
> To:
> Jerl Simpson <[EMAIL PROTECTED]>
> 
> To:
> Jerl Simpson <[EMAIL PROTECTED]>
> CC:
> tutor@python.org
> 
> 
>>Basicall, what I need is a stand alone CGI.  Instead of the program passing
>>the data off to a CGI, I want it to parse and handle the request directly.
> 
> 
> instead of which program ?
> 
> Http requests are served by a web server (ie Apache), which depending
> on the type of request passes the request to wherever.
> 
> As such any HTTP request *must* be handled first by a web server, and
> cgi scripts traditionally lived in cgi-bin directory on the server so
> a URL would look like http://www.example.com/cgi-bin/myscript.py
> 
> I think you have 3 options
> 
> 1. use the cgi module in python to create scripts like the one above.
> They will not be fast but it gives you a lowlevel access to the request
> However cgi was out of date about 8 years ago - it has some
> seriouslimitations mostly on speed/capacity.
> 

 From what others have told me, I wouldn't say that cgi is out of date. The 
couple of apps I've done perform just fine. Also, I'd recommend that every 
programmer do at least a little app using cgi to get a good understanding of 
the 
basics before jumping into alternative methods like mod_python or one of the 
million python web frameworks where things get more complex.

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


Re: [Tutor] Quick way to find the data type

2005-09-27 Thread Christopher Arndt
Bernard Lebel schrieb:
> Hello,
> 
> Let say I have a string. The value of the string might be 'False',
> 'True', '3', '1.394', or whatever else. Is there a quick way to
> convert this string into the appropriate data type other than with
> try/except?

A quick way, yes. But also secure? No.

>>> l = ['false', 'True', '3', '1.394']
>>> l = [eval(x) for x in l]
>>> print l
[False, True, 3, 1.3939]

but this fails when it encounters a string that eval can't handle, for example
'false'. Also eval will evaluate any valid Pythin expression in the string, so
you should use it only when you know *exactly* that the string can not contain
anything harmful. Which is rarely the case.

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


Re: [Tutor] Linking with C programs

2005-09-27 Thread Michael Sparks
On Tuesday 27 September 2005 14:49, Matt Williams wrote:
> Could someone explain how, in very general terms, one would use python
> to wrap some C libraries/ API.
>
> I ask because there are a few bits of C software that look quite
> interesting, and I know that Python can be used to wrap the C - but how
> does it work?

Use Pyrex. If you don't care about produce bindings for languages other than 
python and you just want access to C based libraries, then pyrex is by far 
the most pleasant way I've found of wrapping C.

Essentially pyrex is a bridge language - half C and half python. It compiles 
down to pure C, but handles all the awkward parts of bridging the two for 
you. (like handling ref counts and similar.)

We've used this to wrap a few libraries so far and it's been a pleasant 
experience everytime. It has a couple of tutorials, that work, and even shows 
you how to use distutils (aka setup.py) to make it easy for others to build 
your bindings too.

   * http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/
   * http://ldots.org/pyrex-guide/ - Michael Smith's guide to Pyrex - it's IMO
  brilliant and the examples work, and if you're used to C and used to
  python just feels _natural_ .

If you're after a couple of examples to work forward, you can download them
from:
* http://sourceforge.net/projects/kamaelia

Specifically the vorbissimple and python-dirac downloads will hopefully get 
you started. (The vorbissimple example includes a small simple library 
written in C and show hand off of data from python to C and from C to python, 
as well as how to have a "context" variable in C that python doesn't need to 
understand the internal structure of. (Much like with a FILE structure you 
don't need to understand the FILE structure's internals, just how you pass it 
about)

Regards,


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


Re: [Tutor] Quick way to find the data type

2005-09-27 Thread Danny Yoo


> A quick way, yes. But also secure? No.
>
> >>> l = ['false', 'True', '3', '1.394']
> >>> l = [eval(x) for x in l]
> >>> print l
> [False, True, 3, 1.3939]
>
> but this fails when it encounters a string that eval can't handle, for
> example 'false'. Also eval will evaluate any valid Pythin expression in
> the string, so you should use it only when you know *exactly* that the
> string can not contain anything harmful. Which is rarely the case.

Yeah, I also strongly discourage eval() here: it's very dangerous.  And
even if its weren't dangerous, for the particular job of doing data
conversion from strings to values, it's still probably the wrong tool,
since it doesn't allow for any kind of customization.

We know eval() is both dangerous and uncustomizable, so that makes it all
the more worthwhile to avoid it like the plague.  *grin*  Don't use it for
data parsing and conversion.

Kent's link to Paul McGuire's solution sounds like a straightforward way
to do the string processing: it's controlled, and can be easily modified
to handle specialized literals like lowercased 'true' or 'false'.
"Lexers" are a more specialized class of tools for doing this sort of
thing, and there are several of them out there for Python.

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


Re: [Tutor] Quick way to find the data type

2005-09-27 Thread Bernard Lebel
Thanks a lot Chris.


Bernard



On 9/27/05, Christopher Arndt <[EMAIL PROTECTED]> wrote:
> Bernard Lebel schrieb:
> > Hello,
> >
> > Let say I have a string. The value of the string might be 'False',
> > 'True', '3', '1.394', or whatever else. Is there a quick way to
> > convert this string into the appropriate data type other than with
> > try/except?
>
> A quick way, yes. But also secure? No.
>
> >>> l = ['false', 'True', '3', '1.394']
> >>> l = [eval(x) for x in l]
> >>> print l
> [False, True, 3, 1.3939]
>
> but this fails when it encounters a string that eval can't handle, for example
> 'false'. Also eval will evaluate any valid Pythin expression in the string, so
> you should use it only when you know *exactly* that the string can not contain
> anything harmful. Which is rarely the case.
>
> Chris
> ___
> 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] Quick way to find the data type

2005-09-27 Thread Bernard Lebel
Well I understand all the security issues but unless I'm missing
something, I don't see anything wrong here.

This is in order to read some XML data and transfer its content to the
parameters of a 3D animation software. Since I wrote the XML writer, I
always know how the XML will be formatted. Also, the xml data is read
from disk, in predefined directories. Would the tree not conform to
what I expect the read would crash right away. Finally, the evaluation
of tag content is transposed to parameter values.

So far eval() seems to do a good job for my needs unless I'm
missing a piece?


Cheers
Bernard



On 9/27/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
>
>
> > A quick way, yes. But also secure? No.
> >
> > >>> l = ['false', 'True', '3', '1.394']
> > >>> l = [eval(x) for x in l]
> > >>> print l
> > [False, True, 3, 1.3939]
> >
> > but this fails when it encounters a string that eval can't handle, for
> > example 'false'. Also eval will evaluate any valid Pythin expression in
> > the string, so you should use it only when you know *exactly* that the
> > string can not contain anything harmful. Which is rarely the case.
>
> Yeah, I also strongly discourage eval() here: it's very dangerous.  And
> even if its weren't dangerous, for the particular job of doing data
> conversion from strings to values, it's still probably the wrong tool,
> since it doesn't allow for any kind of customization.
>
> We know eval() is both dangerous and uncustomizable, so that makes it all
> the more worthwhile to avoid it like the plague.  *grin*  Don't use it for
> data parsing and conversion.
>
> Kent's link to Paul McGuire's solution sounds like a straightforward way
> to do the string processing: it's controlled, and can be easily modified
> to handle specialized literals like lowercased 'true' or 'false'.
> "Lexers" are a more specialized class of tools for doing this sort of
> thing, and there are several of them out there for Python.
>
> ___
> 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] HTTP GET Request

2005-09-27 Thread Kent Johnson
Jerl Simpson wrote:
> The only piece I don't have going the Python route is actually
> getting the GET

Which pieces do you have so far? If it is just Python, then you don't have all 
the pieces yet. If you already have a server, which one are you using?

> Sorry, accidentally sent the last one before I was finished.
> I was saying, I just lack getting the GET URI into a variable, and then 
> finding out the best way to parse it into it's components using Python.

The GET URI doesn't just magically appear - you need a webserver somewhere in 
the picture. If you have written a webserver in C you must know a bit about 
what is involved. It's not just getting a query parameter into a variable, 
somewhere you have to implement the HTTP protocol. 

There are many, many choices for how to do this in Python. Several have already 
been pointed out to you - 
Apache + Python CGI - simple way to get started
Apache + mod_python - runs Python in the same process as Apache, 
industrial-strength and faster than CGI
CherryPy - All-Python solution, fairly easy to get started with though for 
high-volume deployments it is recommended to run it behind Apache with 
mod_rewrite.

Here is a long list of other possibilities:
http://wiki.python.org/moin/WebProgramming

Maybe you should ask your friend what server he recommends?

Kent
> 
> Thanks again,
> 
> Jerl
> 
> On 9/27/05, *Jerl Simpson* <[EMAIL PROTECTED] 
> > wrote:
> 
> I don't think I explained my situation clearly enough.
> 
> Let me tell you what I'm doing.
> First, I'm looking for an excuse to learn Python.
> 
> Second, I'm running a server that takes request from a remote
> server.  This request is an HTTP GET request.  I used to have (lost
> the source) a C program I wrote that functions as a web server.
> It takes the request on port 80, parses the GET.  From the GET I
> construct a MySQL query and spit the results back out.
> 
> Now, the logical solution is to run Apache to handle the webserver
> aspect, and have it hand off to a CGI to run the MySQL query and
> spit the results back out to the requesting server.  In this
> situation, I could have thousands of requests per minute, and it
> bogs the server down.  To much overhead when Apache calls the CGI. 
> I've tried using several different setups with Apache, and C, Perl,
> and PHP CGIs. 
> 
> I found it to be much faster if I wrote the webserver, and handle
> the MySQL call within the same thread.
> 
> I was told by a friend of mine that Python is good for creating
> network servers.  So I thought I'd give it a try...instead of
> rewriting my C program I wanted to take the opportunity to learn a
> bit of Python.
> 
> The only piece I don't have going the Python route is actually
> getting the GET
> 
> 
> On 9/27/05, *paul brian* < [EMAIL PROTECTED]
> > wrote:
> 
>>  Basicall, what I need is a stand alone CGI.  Instead of the
> program passing
>>  the data off to a CGI, I want it to parse and handle the
> request directly.
> 
> instead of which program ?
> 
> Http requests are served by a web server (ie Apache), which
> depending
> on the type of request passes the request to wherever.
> 
> As such any HTTP request *must* be handled first by a web
> server, and
> cgi scripts traditionally lived in cgi-bin directory on the
> server so
> a URL would look like http://www.example.com/cgi-bin/myscript.py
> 
> I think you have 3 options
> 
> 1. use the cgi module in python to create scripts like the one
> above.
> They will not be fast but it gives you a lowlevel access to
> the request
> However cgi was out of date about 8 years ago - it has some
> seriouslimitations mostly on speed/capacity.
> 
> 2. use a system like mod_python. This is better than cgi for
> lots of reasons,
> mostly to do with speed. Here you also have access to the
> request
> objects,   but there is a bit of a learning curve.
> 
> 3. Zope - higher level than even mod_python and still more of a
> learning curve
> 
> (there is a multitude of python based cgi repalcements, Django,
> webware and others spring to mind.  But there is no clear "winner"
> amoungst the community)
> 
> I would recommend that you look at taking a weekend to install
> apache,
> and play with both the cgi module and mod_python.  mod_python is
> pretty good and fairly well documented, as well as being pretty low
> level.
> 
> I think there is a lot to do here - perhaps if you tell us exactly
> what you need we can point you at a solution. Some web hosters
> provide
> mod_python or zope hosting and that might be 

Re: [Tutor] Tutor Digest, Vol 19, Issue 82

2005-09-27 Thread Andrei
sanjay sinha wrote:
> I am sanju sinha just started python from the last one month . I have 
> gone through all basic programming of with python and also use Tkinter 
> for window . I want to know from you all experience person in python 
> that weather i move to wx or tkinter is sufficient for GUI programming.

Tkinter is usable for GUI development and it's the default GUI toolkit 
in Python. wx is also usable. wx offers IMO better tools (visual GUI 
builders) and better looks, but your users will have to download both Py 
and wxPy to use it - unless you use Py2exe. Whether you should switch 
depends on whether you like Tkinter or not. I for one really dislike it 
and avoid programs written in it, but others seem to prefer Tkinter - 
it's very much a matter of taste.

In the future please don't include a full tutor digest in your message 
and mind the warning at the top of the digest:

'When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tutor digest..."'

-- 
Yours,

Andrei

=
Mail address in header catches spam. Real contact info:
''.join([''.join(s) for s in zip(
"[EMAIL PROTECTED] pmfe!Pes ontuei ulcpss  edtels,s hr' one oC.",
"rjc5wndon.Sa-re laed o s npbi ot.Ira h it oteesn edt C")])

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


Re: [Tutor] Quick way to find the data type

2005-09-27 Thread bob
At 12:08 PM 9/27/2005, Bernard Lebel wrote:
>Hello,
>
>Let say I have a string. The value of the string might be 'False',
>'True', '3', '1.394', or whatever else. Is there a quick way to
>convert this string into the appropriate data type other than with
>try/except?

eval()? 

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


Re: [Tutor] Embedding

2005-09-27 Thread Joseph Quigley

>From: Kent Johnson <[EMAIL PROTECTED]>
>Subject: Re: [Tutor] Embedding
>  
>
>Not easily. You can embed C in Java with JNI.
>
Thanks, that just migh be perfect... or is there a program that will 
embedd Java into C/C++?

> You can access the result from Jython. But the original requirement seems a 
> bit weird - why Java and C in one program? ISTM you should pick Python/C or 
> Jython/Java but not both. Can you say more about what you are trying to 
> accomplish?
>  
>
Well we are three programmers. I know python, another knows Java and the 
other C++. We are trying to figure out how to combine all three 
langauges to make a game.

>(By "Java Python" do you mean "Java AND Python"
>
Yes. Sorry forgot the comma

> or "the Java version of Python" e.g. Jython?)
>  
>
No.

Thanks.
Joe

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


Re: [Tutor] Quick way to find the data type

2005-09-27 Thread Danny Yoo


On Tue, 27 Sep 2005, Bernard Lebel wrote:

> Well I understand all the security issues but unless I'm missing
> something, I don't see anything wrong here.


Hi Bernard,

The other major problem with it, besides security, is that it can
introduce certain error messages that will look inscrutable unless you
take special precautions.

Assume for the moment that the file reads bad data:

##
>>> eval("3.1.4")
Traceback (most recent call last):
  File "", line 1, in ?
  File "", line 1
3.1.4
   ^
SyntaxError: unexpected EOF while parsing
##

The issue here is to use the tools that are not only powerful, but easy to
debug.  Programs that use eval() are hard to debug because the dynamic
input to eval() because another possible source of errors.  Why should
your programs have SyntaxErrors, when they're not yours?  *grin*


Of course, you can then put try/except around the eval() to handle the
situation gracefully.  But if you're going to do that, you're already
halfway near the other solution, using try/except around the other
data-converter functions like int() and float().


> This is in order to read some XML data and transfer its content to the
> parameters of a 3D animation software. Since I wrote the XML writer, I
> always know how the XML will be formatted. Also, the xml data is read
> from disk, in predefined directories. Would the tree not conform to what
> I expect the read would crash right away. Finally, the evaluation of tag
> content is transposed to parameter values.
>
> So far eval() seems to do a good job for my needs unless I'm
> missing a piece?

It's most likely the wrong tool here.  And people HAVE been bitten by this
before:

http://phpxmlrpc.sourceforge.net/#security

Different language, but same exact problem.  Those folks, too, though
using eval() was fine, because it was part of the internals, and bad data
would obviously never get into those internals.

They were wrong.  But it's wrong to say that they weren't careful enough
to control the flow of data: the real conceptual error was putting in
eval() in their program in the first place.  They finally woke up and
stripped it out, after getting exploited several times.


The assumptions you make, about controlling the input, and being the only
source of the xml output, sound fine.  But you're assuming that the
environment that your program will work in will be static.  Software often
ends up being applied in places that are ridiculous and unplanned-for,
like a network application.  It's best practice to avoid unneccessary
risk, and not to court it.  *grin*

The solution that Paul McGuire outlines is just as easy to use as eval(),
once you have it in a module.  And unlike eval(), there's no exploitation
risk at all.  So the argument should really be: why would we want to use
eval()?

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


Re: [Tutor] Quick way to find the data type

2005-09-27 Thread Danny Yoo


> >Let say I have a string. The value of the string might be 'False',
> >'True', '3', '1.394', or whatever else. Is there a quick way to convert
> >this string into the appropriate data type other than with try/except?
>
> eval()?

Gaaa, don't say that word!  *grin*

(Sorry, I know I'm sounding like a ranting maniac about this.  It's one of
my pet peeves.)

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


Re: [Tutor] Quick way to find the data type

2005-09-27 Thread Bernard Lebel
Fair enough Danny,
I made the changes. Thanks for the input.


Still, regarding this comment.

On 9/27/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
> The assumptions you make, about controlling the input, and being the only
> source of the xml output, sound fine.  But you're assuming that the
> environment that your program will work in will be static.  Software often
> ends up being applied in places that are ridiculous and unplanned-for,
> like a network application.  It's best practice to avoid unneccessary
> risk, and not to court it.  *grin*

Well, the fact is I'm in charge of these things here, fortunately. My
job is to make sure nothing gets used the wrong way. :-)

Still, your points are valid and I have taken note of them :-)

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


Re: [Tutor] Embedding

2005-09-27 Thread Kent Johnson
Joseph Quigley wrote:
>>From: Kent Johnson <[EMAIL PROTECTED]>
>>Subject: Re: [Tutor] Embedding 
>>
>>Not easily. You can embed C in Java with JNI.
>>
> Thanks, that just migh be perfect... or is there a program that will 
> embedd Java into C/C++?

Yes, that is supported by Sun. I'm not sure if it is part of JNI or something 
else. But I don't think that is a good solution to your problem.

I suppose you could have a core game engine written in C++ with Java or Python 
as an embedded scripting language. What kind of game do you have in mind?
> 
> Well we are three programmers. I know python, another knows Java and the 
> other C++. We are trying to figure out how to combine all three 
> langauges to make a game.

Sounds like a bit of a hash to me. Show them pygame and convince them to learn 
Python :-)

Kent

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


Re: [Tutor] Embedding

2005-09-27 Thread Michael Sparks
On Tuesday 27 September 2005 23:16, Kent Johnson wrote:
> Joseph Quigley wrote:...
> > Well we are three programmers. I know python, another knows Java and the
> > other C++. We are trying to figure out how to combine all three
> > langauges to make a game.
>
> Sounds like a bit of a hash to me. Show them pygame and convince them to
> learn Python :-)

If you tell them to drop the type declarations, braces {}, semicolons, and 
make their code a touch simpler, then they already (more or less) know python 
(At least that's the way I normally introduce people who know C/C++/Java to 
python).


Michael.

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


Re: [Tutor] File mode r+

2005-09-27 Thread Shitiz Bansal


--- "Michael P. Reilly" <[EMAIL PROTECTED]> wrote:

> On 9/24/05, Shitiz Bansal <[EMAIL PROTECTED]> wrote:
> >
> > Hi,
> >
> > How do i proceed if i am not sure about the number
> of characters in the
> > fist two lines and want to write in the third
> line.is there a way to skip two
> lines and write on the third??
> >
> > Also could anyone explain why the readline() did
> not work. according to
> > what i understand it should.
> > *bob <[EMAIL PROTECTED]>* wrote:
> >
> > At 01:11 AM 9/24/2005, Shitiz Bansal wrote:
> > >Hi,
> > >I want to update a textfile using the r+ file
> mode.
> > >contents of file:
> > >
> > >abcd
> > >efgh
> > >ijkl
> > >mnop
> > >qrst
> > >uvwx
> > >yx12
> > >
> > >my scripts is:
> > >
> > >file1=open("aa.txt",'r+')
> >
> > >>Instead of readline, use skip to position the
> file to where you want to
> > >>overwrite it.
> > >>file1.seek(10)
> >
> >
> > >file1.readline()
> > >file1.readline()
> > >file1.write("1234\n")
> > >file1.close()
> > >
> > >This should replace the third line with 1234.
> > >However it does nothing.
> > >
> > >Moreover the script:
> > >
> > >file1=open("aa.txt",'r+')
> > >file1.write("1234\n")
> > >file1.close()
> > >
> > >does replace the first line with 1234.
> > >
> > >could anyone explain what is happening?
> > >
> > >shitiz
> >
> > Hello Shitiz,
> 
> I'm not sure if it is true anymore, I haven't gotten
> to test it. But in the
> olden days of C and standard libraries and UNIX, you
> would open a file for
> reading and writing and before you could switch
> between read() and write()
> you would have to perform a seek(). Usually
> something like:
> file.readline() # read line 1
> file.readline() # read line 2
> file.seek(0, 1) # move 0 bytes from current position
> (no place)
> file.write("1234\n")
> file.close()
>  There may be one problem here. What you write will
> overwrite line 3 or
> only part of line 3... or maybe all of line 3 and
> some of line 4. It is good
> to know how the file is structured before you write
> to it, in case you end
> up with unexpected data. If all your lines are four
> characters plus a
> newline, then you don't have to worry (most of us
> aren't that lucky).
> -Arcege
> --
> There's so many different worlds,
> So many different suns.
> And we have just one world,
> But we live in different ones.
> 




__ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Embedding

2005-09-27 Thread R. Alan Monroe
> Well we are three programmers. I know python, another knows Java and the
> other C++. We are trying to figure out how to combine all three 
> langauges to make a game.

I checked out this book from the local library:
MUD Game Programming by Ron Penton
http://www.amazon.com/exec/obidos/tg/detail/-/1592000908/qid=1127867592/sr=2-1/ref=pd_bbs_b_2_1/103-5133587-6168643?v=glance&s=books

It had a chapter on how to embed Python in a C++ program. Might be
worth a glance through.

Alan

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


Re: [Tutor] Embedding

2005-09-27 Thread Kent Johnson
Michael Sparks wrote:
> On Tuesday 27 September 2005 23:16, Kent Johnson wrote:
>>Sounds like a bit of a hash to me. Show them pygame and convince them to
>>learn Python :-)
> 
> If you tell them to drop the type declarations, braces {}, semicolons, and 
> make their code a touch simpler, then they already (more or less) know python 
> (At least that's the way I normally introduce people who know C/C++/Java to 
> python).

Yes, my experience with porting Java to Python is that the major activity is 
pressing 'delete' :-)

Kent

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


[Tutor] web development

2005-09-27 Thread Don Jennings
Earlier this month, Kent posted that Jython and Velocity are a good way 
to develop dynamic web sites. After a little searching, it seems that 
there are quite a few options for web development in Python (perhaps 
too many?). So, rather than ask for recommendations of which one to 
use, what I would really like to know are how people decided to use any 
particular framework.

Thanks!
Don

P.S. As an aside, does anyone have any experience with django? (I 
really like the name since I am fond of django reinhardt, the jazz 
guitarist.)

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