Re: [Tutor] Running multiple version of Python on 1 windows machine

2006-11-01 Thread wesley chun
> try
>import sqlite3 as sqlite # Python 2.5
> except ImportError:
>from pysqlite2 import dbapi2 as sqlite


i second kent's suggestion here.  in fact, i ran into a very similar
issue while working on the database chapter for the new edition of the
book.  i had 2.4.2 with pysqlite 2.1.3 on it, and then when the first
release of 2.5 came out, discovered sqlite3.  for testing and
compatibility reasons, i wanted the example app to run under both, so
i added practically the same lines to the code for ushuffle_db.py:

if db == 'sqlite':
try:
import sqlite3
except ImportError, e:
try:
from pysqlite2 import dbapi2 as sqlite3
except ImportError, e:
return None

DB_EXC = sqlite3
:

i save off the same module again as DB_EXC to catch exceptions -- this
value is different for different databases, i.e. "import
_mysql_exceptions as DB_EXC" or "from gadfly import gadfly; DB_EXC =
gadfly". then in other parts of the code, i can do stuff like:

try:
cur.execute('SELECT * FROM table')
except DB_EXC.OperationalError, e:
# handle error for any database type

the entire source can be found here if you're interested:
http://corepython.com -> Source Code -> ch21 -> ushuffle_db.py

good luck!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to capture 'Ctrl+c' in Python

2006-11-01 Thread Asrarahmed Kadri
 
Hi folks,
How can I know that the user has pressed a particular key or a combination of keys.. for example 'q' or 'Ctrl-c' ?
 
Thanks in anticipation.
 
Regards,
 
Asrarahmed
 
 
-- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running multiple version of Python on 1 windows

2006-11-01 Thread Kent Johnson
Tony Cappellini wrote:
>  >From Kent
>  >>You don't say what OS you are running but under Windows it is trivial to
>  >>have multiple versions of Python installed, I have 2.3, 2.4 and 2.5.
>  >>They are each in their own directories, all in the system path. I have
>  >>aliases called py23, py24 and py25 that let me launch the version I
>  >>want. I'm pretty sure you can do something similar with other OSes.
> 
> If you want script abc.py to run with python 2.3, and script xyz.py to 
> run with 2.5, how can you control this form the command line?

I have Python 2.3, 2.4 and 2.5 all installed on my PC. All three 
directories are in my PATH. As it happens Python2.4 is first in the path 
so if I just run 'python' I get Python 2.4.

In each Python directory I have a copy of python.exe that is named py2x; 
i.e. py23.exe, py24.exe, py25.exe. python.exe is small so I don't mind 
copying it; a shortcut would probably work as well. Since each dir is in 
the path, I can select which version to run by the name of the exe.
> 
> python abc.py

py23 abc.py

> 
> vs
> 
> python xyz.py?

py24 xyz.py

> 
> 
> One task I'm faced with is evaluating the benefits of migrating a huge 
> framework from 2.3 to 2.4?
> I'd rather use two machines for this rather than install 2.4 on my 2.3 
> machine.
> 
> If I need to make any changes in the scripts to work on 2.4, I don't 
> want that interfering with a fully-working 2.3 environment.

You should be able to have two copies of the scripts on your machine, a 
working production copy and a development copy.

Kent
> 


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


Re: [Tutor] Simple calculator

2006-11-01 Thread Kent Johnson
Joe Cox wrote:
> I found this simple calculator on the web:
> 
> from Tkinter import *
> from math import *
> ###http://sunsite.uakom.sk/sunworldonline/swol-02-1998/swol-02-python.htmlBy
> Cameron Laird and Kathryn Soraiz...Getting Started with Python###
> 
> def evaluate(event):
> label['text'] = "Result:  " + str(eval(expression.get()))

Note that eval() is highly insecure. For this program maybe it doesn't 
matter but in general you should avoid using eval().

> 
> frame = Frame(None)
> 
> entry = Entry(frame)
> entry['textvariable'] = expression = StringVar()
> entry.bind("", evaluate)

You need to say what kind of event you want to bind to the evaluate 
function. In this case you probably want to bind the Enter key so you 
would use
   entry.bind("", evaluate)

Kent

> 
> label = Label(frame)
> 
> button = Button(frame, text = "Submit", command = evaluate)
> 
> frame.pack()
> entry.pack()
> label.pack()
> button.pack()
> frame.mainloop()
> 
> I get this:
> 
 Traceback (most recent call last):
>   File
> "D:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
> line 310, in RunScript
> exec codeObject in __main__.__dict__
>   File "D:\Python24\Calculator\Calc.py", line 12, in ?
> entry.bind("", evaluate)
>   File "D:\Python24\lib\lib-tk\Tkinter.py", line 933, in bind
> return self._bind(('bind', self._w), sequence, func, add)
>   File "D:\Python24\lib\lib-tk\Tkinter.py", line 888, in _bind
> self.tk.call(what + (sequence, cmd))
> TclError: no events specified in binding
> 
> Please have a look for me thanks!
> 
> 
> 
> 
> Joe Cox
> 513-293-4830
> 
> ___
> 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] Simple calculator

2006-11-01 Thread Michael Lange
Hi Joe,

On Tue, 31 Oct 2006 18:37:27 -0800
"Joe Cox" <[EMAIL PROTECTED]> wrote:

> I found this simple calculator on the web:
> 
> from Tkinter import *
> from math import *
> ###http://sunsite.uakom.sk/sunworldonline/swol-02-1998/swol-02-python.htmlBy
> Cameron Laird and Kathryn Soraiz...Getting Started with Python###
> 
> def evaluate(event):
> label['text'] = "Result:  " + str(eval(expression.get()))
> 
> frame = Frame(None)
> 
> entry = Entry(frame)
> entry['textvariable'] = expression = StringVar()
> entry.bind("", evaluate)
> 
> label = Label(frame)
> 
> button = Button(frame, text = "Submit", command = evaluate)
> 
> frame.pack()
> entry.pack()
> label.pack()
> button.pack()
> frame.mainloop()
> 
> I get this:
> 
> >>> Traceback (most recent call last):
>   File
> "D:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
> line 310, in RunScript
> exec codeObject in __main__.__dict__
>   File "D:\Python24\Calculator\Calc.py", line 12, in ?
> entry.bind("", evaluate)
>   File "D:\Python24\lib\lib-tk\Tkinter.py", line 933, in bind
> return self._bind(('bind', self._w), sequence, func, add)
>   File "D:\Python24\lib\lib-tk\Tkinter.py", line 888, in _bind
> self.tk.call(what + (sequence, cmd))
> TclError: no events specified in binding
> 

this code seems to be quite old, so I guess that the line

entry.bind("", evaluate)

used to be legal in older version of Tk. I don't know what it is supposed
to do, though. Maybe there was some default event defined for such cases.
In today's Tk you need to specify an event sequence the callback should be 
bound to, like

entry.bind("", evaluate)

This does not work either, because you will get a syntax error on "incomplete"
expressions like "3*" when trying to type in "3*3" ,
so the evaluate() callback will have to catch this syntax error:

def evaluate(event):
try:
label['text'] = "Result:  " + str(eval(expression.get()))
except SyntaxError:
pass

This still does not work, when you press the "submit" button you get:

Traceback (most recent call last):
  File "/usr/lib/python2.4/lib-tk/Tkinter.py", line 1345, in __call__
return self.func(*args)
TypeError: evaluate() takes exactly 1 argument (0 given)

so the constructor must be changed like:

   def evaluate(event=None):
  (...)

because the Tkinter.Button's "command" callback is called without any arguments
(again I don't know if this was different in old Tk versions).

With these changes at least the few simple examples I tried seem to work.

I hope this helps

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


Re: [Tutor] How to capture 'Ctrl+c' in Python

2006-11-01 Thread Andreas Kostyrka
Am Mittwoch, den 01.11.2006, 10:14 + schrieb Asrarahmed Kadri:
> 
>  
> Hi folks,
> How can I know that the user has pressed a particular key or c
> combination of keys.. for example 'q' or 'Ctrl-c' ?

In practice that depends upon your environment. Unix/Linux, MacOS,
Windows, GUI vs. cmdline have all different ways to "read" from the
keyboard.

Ctrl-c OTOH, usually sends the interrupt signal to the process, and as
such can caught via the KeyboardInterrupt exception:

try:
somework
except KeyboardInterrupt:
ctrlc_caught

Andreas

>  
> Thanks in anticipation.
>  
> Regards,
>  
> Asrarahmed
>  
>  
> 
> -- 
> To HIM you shall return. 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor


signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mapping to object attributes

2006-11-01 Thread Mike Hansen
 

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Alan Gauld
> Sent: Tuesday, October 31, 2006 4:47 PM
> To: tutor@python.org
> Subject: Re: [Tutor] Mapping to object attributes
> 
> 
> "Mike Hansen" <[EMAIL PROTECTED]> wrote
> 
> > I've got a web form with a lot of form fields. I'd like to be able 
> > to map
> > the form fields to an object's attributes. I'm having a little 
> > trouble
> > figuring out how.
> 
> John has answered that bit.
> 
> > There will be some fields I'll need to validate(boolean or 
> int), but 
> > the
> > majority are just text fields that can be passed to the object.
> 
> One thing you can do is store the validation functions in the
> dictionary with the value.
> 
> def intValidator(i):
>  try: return int(i)
>  except: return None
> 
> def boolValidator(b):
>  try: return b and True or False
>  except: return None
> 
> mapping = { 'field': (intvalue, intValidator),
>   'another': (boolvalue,boolValidator)...}
> 
> You can then access the validator like so:
> 
> value = mapping[fieldname][0]
> validator = mapping[fieldname][1]
> value = validator(value)
> if value == None: #ooops!
> 
> or more concisely:
> 
> value = mapping[fieldname][1](mapping[fieldname]0])
> 
> This style of validation has the "benefit" (or side-effect if you 
> prefer)
> of converting compatible types into true types. eg. validating a 
> string
> or float representation of an integer returns the actual integer 
> value.
> 
> HTH,
> 
> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld 
> 

Thanks John and Alan. Your suggestions will make my code less nested.

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


Re: [Tutor] immutable objects

2006-11-01 Thread Danny Yoo
> here is the stack
> trace. i know that SOAPpy is doing wrong by not checking the type of
> the element along with id(ele).
> I thought if there is any work around the problem i could use that without
> upgrading SOAPpy after it is fixed.

Hi Premnath,

You might want to see if some other SOAP module might be better supported. 
The last version of SOAPpy that I've seen appears to have been released in 
2005, and as far as I can tell, the project is languishing a bit.  Most of 
the energy toward Python and SOAP seems to be focused on the Zolera SOAP 
Infrastructure (ZSI) project, which can be found at:

 http://pywebsvcs.sourceforge.net/


Thanks again for the stack trace; it helps pinpoint some problems.  I did 
some quick checks on the stack trace; the line numbers from the stack 
trace aren't matching up with SOAPpy 0.12.0., so I'm not sure if this 
problem has already been fixed or not.  You may want to make sure you have 
the latest release of SOAPpy if you continue using that module.


The code for SOAPBuilder.dump_string() just doesn't look right to me. 
Why does one have to do a checkref() on it when serializing the string 
data, given that a string has no internal structure to speak of?  I think 
the code there is just wrong.  You're seeing this failure simply because 
you're passing in two strings with the same id, which should be perfectly 
legal and fine.  SOAPpy is definitely at fault here.

I kludged up the recursive-checking code on strings (i.e. turned it off), 
and now see the following:

##
>>> import SOAPpy
>>> rmt = SOAPpy.SOAPProxy("http://soap.test.com";,
...header = SOAPpy.Types.headerType(
...data = {'username': 'prem',
...'password': 'prem'}))
>>> rmt.testcall()
Traceback (most recent call last):
   File "", line 1, in ?
   File "SOAPpy/Client.py", line 470, in __call__
 return self.__r_call(*args, **kw)
   File "SOAPpy/Client.py", line 492, in __r_call
 self.__hd, self.__ma)
   File "SOAPpy/Client.py", line 363, in __call
 config = self.config)
   File "SOAPpy/Client.py", line 263, in call
 raise HTTPError(code, msg)
SOAPpy.Errors.HTTPError: 
##

which looks a little more promising.


In fact, you can see this yourself by playing a small trick:

###
>>> rmt = SOAPpy.SOAPProxy("http://soap.test.com";, header = 
SOAPpy.Types.headerType(data={'username': 'p' + 'rem', 'password': 'p' + 
'rem'}))
>>> rmt.testcall()
Traceback (most recent call last):
   File "", line 1, in ?
   File "SOAPpy/Client.py", line 470, in __call__
 return self.__r_call(*args, **kw)
   File "SOAPpy/Client.py", line 492, in __r_call
 self.__hd, self.__ma)
   File "SOAPpy/Client.py", line 363, in __call
 config = self.config)
   File "SOAPpy/Client.py", line 263, in call
 raise HTTPError(code, msg)
SOAPpy.Errors.HTTPError: 
###

Forcing the manual string concatenation lets you go on, but this is just a 
silly kludge around what is fundamentally a bad SOAPYpy bug.


My current recommendation is to try using ZSI instead: the SOAPpy code 
base appears to have a bit of bit rot.


Good luck to you!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] off topic GNOME background

2006-11-01 Thread Danny Yoo


On Tue, 31 Oct 2006, Amadeo Bellotti wrote:

> my background just disappeared it shows a black screen like it cant find 
> the default background. if that makes sense I've added my own and it 
> cant seem to find it i removed it and re added it it doesn't seem to be 
> working though anyone have any ideas?

Try asking on a GNOME-specific forum; I don't think any of us have good 
technical expertise on the GNOME desktop.  Try:

 http://www.gnome.org/support/

I'm sure the people there will be happy to help you.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to test .mov file using python

2006-11-01 Thread [EMAIL PROTECTED]
I know that python has many packages such as nose, which is for
testing. Now I am going to test a quicktime file to find out if it
works properly. Are there an existing python package could do that? And
how to write the program?

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


[Tutor] Help me with graphs...using tkinter

2006-11-01 Thread Asrarahmed Kadri
Hi folks,
 
I want to draw bar-charts using Tkinter. I am not able to find material on this topic.
 
Help me
 
Regards,
Asrarahmed
 
 
-- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] I am terribly confused about "generators" and "iterators".. Help me

2006-11-01 Thread Kent Johnson
Kent Johnson wrote:
> Bob Gailer wrote:
>> Asrarahmed Kadri wrote:
>>>  
>>> What are generators and iterators...??And why are they are needed..??
>>>  
>>> I can do my stuff with a 'for' or a 'while' loop.. so why do I need an 
>>> ITERATOR..?
>> iterators & generators do not replace while or for loops! 
> 
> No, actually iterators and generators are the foundation of for loops. 
> Whenever you write a for loop, Python creates an iterator to return the 
> values that are assigned to the loop variables.
> 
> Writing your own iterator is a way to make a new kind of thing that can 
> be iterated in a for loop. Generators are a convenient way to make 
> iterators. They can be used to encapsulate loop logic. For some examples 
> see these recent threads:
> http://thread.gmane.org/gmane.comp.python.tutor/36068/focus=36069
> http://article.gmane.org/gmane.comp.python.tutor/36105/match=generator

Here is another good article about what happens behind the scenes of a 
for loop:
http://online.effbot.org/2006_11_01_archive.htm#for

Kent

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


Re: [Tutor] immutable objects

2006-11-01 Thread Premnath Sah
 On 11/1/06, Danny Yoo <[EMAIL PROTECTED]> wrote:
> here is the stack> trace. i know that SOAPpy is doing wrong by not checking the type of> the element along with id(ele).> I thought if there is any work around the problem i could use that without
> upgrading SOAPpy after it is fixed.Hi Premnath,You might want to see if some other SOAP module might be better supported.The last version of SOAPpy that I've seen appears to have been released in
2005, and as far as I can tell, the project is languishing a bit.  Most ofthe energy toward Python and SOAP seems to be focused on the Zolera SOAPInfrastructure (ZSI) project, which can be found at: 
http://pywebsvcs.sourceforge.net/Thanks again for the stack trace; it helps pinpoint some problems.  I didsome quick checks on the stack trace; the line numbers from the stack
trace aren't matching up with SOAPpy 0.12.0., so I'm not sure if thisproblem has already been fixed or not.  You may want to make sure you havethe latest release of SOAPpy if you continue using that module.
The code for SOAPBuilder.dump_string() just doesn't look right to me.Why does one have to do a checkref() on it when serializing the stringdata, given that a string has no internal structure to speak of?  I think
the code there is just wrong.  You're seeing this failure simply becauseyou're passing in two strings with the same id, which should be perfectlylegal and fine.  SOAPpy is definitely at fault here.I kludged up the recursive-checking code on strings (
i.e. turned it off),and now see the following:##>>> import SOAPpy>>> rmt = SOAPpy.SOAPProxy("
http://soap.test.com",...header = SOAPpy.Types.headerType(...data = "" 'prem',...'password': 'prem'}))
>>> rmt.testcall()Traceback (most recent call last):   File "", line 1, in ?   File "SOAPpy/Client.py", line 470, in __call__ return self.__r_call(*args, **kw)
   File "SOAPpy/Client.py", line 492, in __r_call self.__hd, self.__ma)   File "SOAPpy/Client.py", line 363, in __call config = self.config)   File "SOAPpy/Client.py", line 263, in call
 raise HTTPError(code, msg)SOAPpy.Errors.HTTPError: ##which looks a little more promising.
In fact, you can see this yourself by playing a small trick:###>>> rmt = SOAPpy.SOAPProxy("
http://soap.test.com", header =SOAPpy.Types.headerType(data="" 'p' + 'rem', 'password': 'p' +'rem'}))>>> rmt.testcall()Traceback (most recent call last):   File "", line 1, in ?
   File "SOAPpy/Client.py", line 470, in __call__ return self.__r_call(*args, **kw)   File "SOAPpy/Client.py", line 492, in __r_call self.__hd, self.__ma)   File "SOAPpy/Client.py", line 363, in __call
 config = self.config)   File "SOAPpy/Client.py", line 263, in call raise HTTPError(code, msg)SOAPpy.Errors.HTTPError: ###
Forcing the manual string concatenation lets you go on, but this is just asilly kludge around what is fundamentally a bad SOAPYpy bug.My current recommendation is to try using ZSI instead: the SOAPpy code
base appears to have a bit of bit rot.Good luck to you!Thanks for your reply. I will definitely look into ZSI -- With Regards,Premnath Sah T. H.
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to test .mov file using python

2006-11-01 Thread Andreas Kostyrka
Am Mittwoch, den 01.11.2006, 10:39 -0500 schrieb [EMAIL PROTECTED]:
> I know that python has many packages such as nose, which is for
> testing. Now I am going to test a quicktime file to find out if it
> works properly. Are there an existing python package could do that?
> And how to write the program?

AFAIK, quicktime mov files are complicated beasts, with an embedded
scripting language and all. Depending upon your requirement, you can use
mplayer to test if the video track decodes, or need manual testing with
the quicktime player.

Andreas

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


signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help me with graphs...using tkinter

2006-11-01 Thread Michael Lange
Hi Asrarahmed,

On Wed, 1 Nov 2006 16:16:31 +
"Asrarahmed Kadri" <[EMAIL PROTECTED]> wrote:

> Hi folks,
> 
> I want to draw bar-charts using Tkinter. I am not able to find material on
> this topic.
> 

have a look at the Canvas widget and especially at its create_rectangle() 
method.

Documentation is here: 

I hope this helps

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


Re: [Tutor] How to capture 'Ctrl+c' in Python

2006-11-01 Thread Chris Hengge
Do you by chance know of a way to capture special keys like "Print Screen"?I have a small script to grab all they keycodes, but it doesn't seem to catch several keys on the keyboard. I've got a utility that I'd like to be able to automagically get a screenshot when something goes wrong so I dont have to hope the user can re-create the error. Universal support would be best, but WinXP is the main OS.
On 11/1/06, Andreas Kostyrka <[EMAIL PROTECTED]> wrote:
Am Mittwoch, den 01.11.2006, 10:14 + schrieb Asrarahmed Kadri:>>> Hi folks,> How can I know that the user has pressed a particular key or c> combination of keys.. for example 'q' or 'Ctrl-c' ?
In practice that depends upon your environment. Unix/Linux, MacOS,Windows, GUI vs. cmdline have all different ways to "read" from thekeyboard.Ctrl-c OTOH, usually sends the interrupt signal to the process, and as
such can caught via the KeyboardInterrupt exception:try:someworkexcept KeyboardInterrupt:ctrlc_caughtAndreas>> Thanks in anticipation.>> Regards,
>> Asrarahmed --> To HIM you shall return.> ___> 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


Re: [Tutor] how to test .mov file using python

2006-11-01 Thread [EMAIL PROTECTED]
Andreas,
Thank you very much for your reply. "Using mplayer to test if the video
track decodes" sounds like a good idea. I am a new python user. Could
you please talk more details about how to do that. 

Thanks again,
YiOn 11/1/06, Andreas Kostyrka <[EMAIL PROTECTED]> wrote:
Am Mittwoch, den 01.11.2006, 10:39 -0500 schrieb [EMAIL PROTECTED]:> I know that python has many packages such as nose, which is for> testing. Now I am going to test a quicktime file to find out if it
> works properly. Are there an existing python package could do that?> And how to write the program?AFAIK, quicktime mov files are complicated beasts, with an embeddedscripting language and all. Depending upon your requirement, you can use
mplayer to test if the video track decodes, or need manual testing withthe quicktime player.Andreas>> Thanks,> Yi> ___> 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] Writing Classes in python

2006-11-01 Thread Rajesh R
I am new to Python and i have a very basic question about writing classes.
Lets say  i have the following code :

class Sample:
  'A simple class'

  def method1():
   print 'Inside Method1'

When i create an instance of the class and when i try to call the method 
method1, i get an error message.
What i do is on the promt, i import the class, create an instance say 
obj=Sample and i call the method obj.method1

Am i doing something wrong or i might have not understood the class coding 
in Python.

Thanks
Rajesh

_
Spice up your IM conversations. New, colorful and animated emoticons. Get 
chatting! http://server1.msn.co.in/SP05/emoticons/

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


Re: [Tutor] how to test .mov file using python

2006-11-01 Thread Hugo Gonzalez M
[EMAIL PROTECTED] wrote:
> Andreas,
> Thank you very much for your reply. "Using mplayer to test if the video 
> track decodes" sounds like a good idea. I am a new python user. Could 
> you please talk more details about how to do that.
Hi,

mplayer is a separate program and has nothing to do with Python (apart 
from the fact that you could somehow control it) You can find it at 
www.mplayerhq.hu

There is AFAIK, no python module for working with quicktime files. For 
writing one, you'd need specifics on how qicktime works, but it is not 
meant for newbies.

I recommend  you pick some other project if you want to start working 
with Python; for the QT file, use mplayer. It even has a verbose mode 
for outputting lots of debugging information about the QT file.


HTH

Hugo

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


Re: [Tutor] Writing Classes in python

2006-11-01 Thread Kent Johnson
Rajesh R wrote:
> I am new to Python and i have a very basic question about writing classes.
> Lets say  i have the following code :
> 
> class Sample:
>   'A simple class'
> 
>   def method1():
>print 'Inside Method1'
> 
> When i create an instance of the class and when i try to call the method 
> method1, i get an error message.
> What i do is on the promt, i import the class, create an instance say 
> obj=Sample and i call the method obj.method1
> 
> Am i doing something wrong or i might have not understood the class coding 
> in Python.

You need parentheses for calls in Python. Try obj=Sample() and 
obj.method1().

Kent

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


Re: [Tutor] Writing Classes in python

2006-11-01 Thread Danny Yoo


On Wed, 1 Nov 2006, Rajesh R wrote:

> I am new to Python and i have a very basic question about writing 
> classes. Lets say i have the following code :
>
[code cut]

Hi Rajesh,

Take a look at a Python class tutorial:

http://www.diveintopython.org/object_oriented_framework/defining_classes.html

The syntax you're writing is almost right, but there are some specifics 
that you're missing.  In Python, methods need to take an explicit 'self' 
parameter.


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


Re: [Tutor] how to test .mov file using python

2006-11-01 Thread Andreas Kostyrka
Am Mittwoch, den 01.11.2006, 15:57 -0500 schrieb [EMAIL PROTECTED]:
> Andreas,
> Thank you very much for your reply. "Using mplayer to test if the
Well, that's nothing python related. mplayer is an Linux movie player
that can be used for all kinds of secondary uses.

Andreas
>  video track decodes" sounds like a good idea. I am a new python user.
> Could you please talk more details about how to do that. 
> 
> Thanks again,
> Yi
> 
> On 11/1/06, Andreas Kostyrka <[EMAIL PROTECTED]> wrote:
> Am Mittwoch, den 01.11.2006, 10:39 -0500 schrieb
> [EMAIL PROTECTED]:
> > I know that python has many packages such as nose, which is
> for
> > testing. Now I am going to test a quicktime file to find out
> if it 
> > works properly. Are there an existing python package could
> do that?
> > And how to write the program?
> 
> AFAIK, quicktime mov files are complicated beasts, with an
> embedded
> scripting language and all. Depending upon your requirement,
> you can use 
> mplayer to test if the video track decodes, or need manual
> testing with
> the quicktime player.
> 
> Andreas
> 
> >
> > Thanks,
> > Yi
> > ___
> > Tutor maillist  -   Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> 
> 
> 


signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing Classes in python

2006-11-01 Thread Andreas Kostyrka
class Sample:
def method1(self):
print "method1"

s = Sample()
s.method1()

Please notice, you need () to invoke a method in Python, and you need an
explicit self argument on the method definition.

Andreas

Am Mittwoch, den 01.11.2006, 21:21 + schrieb Rajesh R:
> I am new to Python and i have a very basic question about writing classes.
> Lets say  i have the following code :
> 
> class Sample:
>   'A simple class'
> 
>   def method1():
>print 'Inside Method1'
> 
> When i create an instance of the class and when i try to call the method 
> method1, i get an error message.
> What i do is on the promt, i import the class, create an instance say 
> obj=Sample and i call the method obj.method1
> 
> Am i doing something wrong or i might have not understood the class coding 
> in Python.
> 
> Thanks
> Rajesh
> 
> _
> Spice up your IM conversations. New, colorful and animated emoticons. Get 
> chatting! http://server1.msn.co.in/SP05/emoticons/
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor


signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing Classes in python

2006-11-01 Thread Rajesh R
Thanks All, It worked.

Rajesh

>From: Andreas Kostyrka <[EMAIL PROTECTED]>
>To: Rajesh R <[EMAIL PROTECTED]>
>CC: Tutor@python.org
>Subject: Re: [Tutor] Writing Classes in python
>Date: Wed, 01 Nov 2006 22:39:57 +0100
>
>class Sample:
> def method1(self):
> print "method1"
>
>s = Sample()
>s.method1()
>
>Please notice, you need () to invoke a method in Python, and you need an
>explicit self argument on the method definition.
>
>Andreas
>
>Am Mittwoch, den 01.11.2006, 21:21 + schrieb Rajesh R:
> > I am new to Python and i have a very basic question about writing 
>classes.
> > Lets say  i have the following code :
> >
> > class Sample:
> >   'A simple class'
> >
> >   def method1():
> >print 'Inside Method1'
> >
> > When i create an instance of the class and when i try to call the method
> > method1, i get an error message.
> > What i do is on the promt, i import the class, create an instance say
> > obj=Sample and i call the method obj.method1
> >
> > Am i doing something wrong or i might have not understood the class 
>coding
> > in Python.
> >
> > Thanks
> > Rajesh
> >
> > _
> > Spice up your IM conversations. New, colorful and animated emoticons. 
>Get
> > chatting! http://server1.msn.co.in/SP05/emoticons/
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor


><< signature.asc >>

_
Live the life in style with MSN Lifestyle. Check out! 
http://content.msn.co.in/Lifestyle/Default

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


Re: [Tutor] Using sys.exit()

2006-11-01 Thread Dick Moores
At 12:14 AM 10/31/2006, Alan Gauld wrote:

>"Dick Moores" <[EMAIL PROTECTED]> wrote
> > I'd like to know how to use sys.exit() to quit a program.
> >
>
>I see that you already figured that out.
>You can also quit by raising SystemExit, which is
>what sys.exit does... but you don't need to import sys...

I'm afraid I don't know what you mean. How do I raise SystemExit, and 
why don't I need to import sys?

> > Is there a way to use it the way I want to? Maybe with an argument?
>
>You can use an argument if you want to pass an error value
>back to the OS. This is good practice if your script might be
>used in a batch file or shell script

So what should that value be? But if answering is a lot of bother, 
don't, because I don't (yet) use python to write either batch files 
or shell scripts.

> > I'm writing a script, which in a couple of places I can't
> > use "break" to quit.
>
>break is not intended to quit programs, break is intended
>to quit loops. To exit a program you should use sys.exit()
>and if its an abnormal exit provide an argument.

Of course, I've been using break to exit only when it works. Why is 
it wrong to do so?

Thanks,

Dick Moores


>Alan G.
>
>
>___
>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] Using sys.exit()

2006-11-01 Thread Andreas Kostyrka
Am Mittwoch, den 01.11.2006, 15:43 -0800 schrieb Dick Moores:
> At 12:14 AM 10/31/2006, Alan Gauld wrote:
> 
> >"Dick Moores" <[EMAIL PROTECTED]> wrote
> > > I'd like to know how to use sys.exit() to quit a program.
> > >
> >
> >I see that you already figured that out.
> >You can also quit by raising SystemExit, which is
> >what sys.exit does... but you don't need to import sys...
> 
> I'm afraid I don't know what you mean. How do I raise SystemExit, and 
> why don't I need to import sys?

raise SystemExit(2)

is equal to sys.exit(2) (actually sys.exit(2) just raises SystemExit(2))

>>> try:
... import sys
... sys.exit(2)
... except SystemExit, v:
... print v 
... 
2


And you don't need to import sys, because SystemExit is a standard
exception that are builtin, available everywhere.

Andreas

> 
> > > Is there a way to use it the way I want to? Maybe with an argument?
> >
> >You can use an argument if you want to pass an error value
> >back to the OS. This is good practice if your script might be
> >used in a batch file or shell script
> 
> So what should that value be? But if answering is a lot of bother, 
> don't, because I don't (yet) use python to write either batch files 
> or shell scripts.
> 
> > > I'm writing a script, which in a couple of places I can't
> > > use "break" to quit.
> >
> >break is not intended to quit programs, break is intended
> >to quit loops. To exit a program you should use sys.exit()
> >and if its an abnormal exit provide an argument.
> 
> Of course, I've been using break to exit only when it works. Why is 
> it wrong to do so?
> 
> Thanks,
> 
> Dick Moores
> 
> 
> >Alan G.
> >
> >
> >___
> >Tutor maillist  -  Tutor@python.org
> >http://mail.python.org/mailman/listinfo/tutor
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor


signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] str.strip() help

2006-11-01 Thread Chris Hengge
I can't figure out a way to .strip("string")Example might look like this:>>> myStr = "I want to strip my words.">>> print myStr.strip("my")>>> 'I want to strip words.'
Thanks.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] str.strip() help

2006-11-01 Thread John Fouhy
On 02/11/06, Chris Hengge <[EMAIL PROTECTED]> wrote:
> I can't figure out a way to .strip("string")
>
> Example might look like this:
>
> >>> myStr = "I want to strip my words."
> >>> print myStr.strip("my")
> >>> 'I want to strip words.'

.strip() only removes text from the beginning and end of the string.

eg:
>>> myStr = 'my words are what I want to strip'
>>> print myStr.strip('my')
 words are what I want to strip

You can use .replace() to be more general.

>>> myStr = 'I want to strip my words'
>>> print myStr.replace('my', '')
I want to strip  words

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


Re: [Tutor] Using sys.exit()

2006-11-01 Thread Dick Moores
At 03:56 PM 11/1/2006, Andreas Kostyrka wrote:
>Am Mittwoch, den 01.11.2006, 15:43 -0800 schrieb Dick Moores:
> > At 12:14 AM 10/31/2006, Alan Gauld wrote:
> >
> > >"Dick Moores" <[EMAIL PROTECTED]> wrote
> > > > I'd like to know how to use sys.exit() to quit a program.
> > > >
> > >
> > >I see that you already figured that out.
> > >You can also quit by raising SystemExit, which is
> > >what sys.exit does... but you don't need to import sys...
> >
> > I'm afraid I don't know what you mean. How do I raise SystemExit, and
> > why don't I need to import sys?
>
>raise SystemExit(2)
>
>is equal to sys.exit(2) (actually sys.exit(2) just raises SystemExit(2))

OK, that works well. But why the 2?

BTW at the command line, "raise SystemExit(2)" produces a completely 
silent exit. In Win IDE I get "SystemExit: 2". With IDLE:

Traceback (most recent call last):
   File "E:\Python25\dev\1unitConversion5a.py", line 425, in 
 main()
   File "E:\Python25\dev\1unitConversion5a.py", line 413, in main
 s = formatAndCheckStringFromUser(s)
   File "E:\Python25\dev\1unitConversion5a.py", line 342, in 
formatAndCheckStringFromUser
 s = stripResponseAndCheckForUserRequestForHelpOrToQuit(s)
   File "E:\Python25\dev\1unitConversion5a.py", line 253, in 
stripResponseAndCheckForUserRequestForHelpOrToQuit
 raise SystemExit(2)
SystemExit: 2

If I can manage to use "break", all 3 exits are silent. Why is it 
wrong to use "break" to exit?

Dick Moores

Dick Moores


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


Re: [Tutor] Using sys.exit()

2006-11-01 Thread Dick Moores
At 10:16 PM 11/1/2006, Dick Moores wrote:
>BTW at the command line, "raise SystemExit(2)" produces a completely
>silent exit. In Win IDE I get "SystemExit: 2". With IDLE:
>
>Traceback (most recent call last):
>File "E:\Python25\dev\1unitConversion5a.py", line 425, in 
>  main()
>File "E:\Python25\dev\1unitConversion5a.py", line 413, in main
>  s = formatAndCheckStringFromUser(s)
>File "E:\Python25\dev\1unitConversion5a.py", line 342, in
>formatAndCheckStringFromUser
>  s = stripResponseAndCheckForUserRequestForHelpOrToQuit(s)
>File "E:\Python25\dev\1unitConversion5a.py", line 253, in
>stripResponseAndCheckForUserRequestForHelpOrToQuit
>  raise SystemExit(2)
>SystemExit: 2

I should have added that Ulipad is also completely silent.

Dick Moores



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


Re: [Tutor] Using sys.exit()

2006-11-01 Thread Luke Paireepinart
Dick Moores wrote:
> At 03:56 PM 11/1/2006, Andreas Kostyrka wrote:
>   
>> Am Mittwoch, den 01.11.2006, 15:43 -0800 schrieb Dick Moores:
>> 
>>> At 12:14 AM 10/31/2006, Alan Gauld wrote:
>>>
>>>   
 "Dick Moores" <[EMAIL PROTECTED]> wrote
 
> I'd like to know how to use sys.exit() to quit a program.
>
>   
 I see that you already figured that out.
 You can also quit by raising SystemExit, which is
 what sys.exit does... but you don't need to import sys...
 
>>> I'm afraid I don't know what you mean. How do I raise SystemExit, and
>>> why don't I need to import sys?
>>>   
>> raise SystemExit(2)
>>
>> is equal to sys.exit(2) (actually sys.exit(2) just raises SystemExit(2))
>> 
>
> OK, that works well. But why the 2?
>
> BTW at the command line, "raise SystemExit(2)" produces a completely 
> silent exit. In Win IDE I get "SystemExit: 2". With IDLE:
>
> Traceback (most recent call last):
>File "E:\Python25\dev\1unitConversion5a.py", line 425, in 
>  main()
>File "E:\Python25\dev\1unitConversion5a.py", line 413, in main
>  s = formatAndCheckStringFromUser(s)
>File "E:\Python25\dev\1unitConversion5a.py", line 342, in 
> formatAndCheckStringFromUser
>  s = stripResponseAndCheckForUserRequestForHelpOrToQuit(s)
>File "E:\Python25\dev\1unitConversion5a.py", line 253, in 
> stripResponseAndCheckForUserRequestForHelpOrToQuit
>  raise SystemExit(2)
> SystemExit: 2
>   
Whenever an error is raised, IDEs catch the error and display it for you.
The SystemExit ended the process that was running your python script, 
just not the IDE.
So the IDE is around to display the error.
Imagine if whenever you tried to read in a file, and got an IOError, 
your IDE didn't catch it.
Wouldn't that be inconvenient?
The fact that it's called SystemExit doesn't change the fact that it's 
an exception.

'stripResponseAndCheckForUserRequestForHelpOrToQuit' is rather a long 
function name.
Perhaps you need more concise function names.
> If I can manage to use "break", all 3 exits are silent. Why is it 
> wrong to use "break" to exit?
>   
'break' doesn't exit.  It ends a loop.
It's not wrong to use a 'break' to exit a loop.  That's what it's there for.
But what if you were doing something after the loop?
'break' wouldn't help you there.
A SystemExit immediately ends the program at that call
(unless you catch the exception :)
example:
import random
a = random.randint(1,5)
i = 0
while i < 5:
if i == a:
   break
print "A is greater than %s" % i
print "A must be 5!"

See, if you were to raise a SystemExit there, then the final print 
statement wouldn't ever take place
unless a was actually 5.

The reason the exits with break are 'silent'
is because no exceptions are occurring.
SystemExit is an exception.
There's nothing wrong with using it.
You could raise an IOError if you wanted.

> Dick Moores
>
> 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