Re: [Tutor] Placing entire Application inside a class

2007-12-19 Thread Alan Gauld
"Jim Morcombe" <[EMAIL PROTECTED]> wrote

> In it, Dakota recomends placing the entire application within a 
> class.
> Why is this so?  Surely in many cases you end up with a constructor
> for the class that is cumbersome and complex?

This is a very common OOP technique, especially in languages which
only support objects, like Java and SmallTalk. Indeed some OOP purists
would claim that its the only way to wsrite a pure OOP application.

The constructor should not be very complex in a well constructed OOP
program because it should only consist of processing any startup args
and then the creation of a few top level objects. And without a top
level class this would be done in some kind of initialisation function
anyhow. Usually the App class will have a Run method or similar
so that the module code looks like:

 module here

class MyApp:
 def __init__(...)
  # process sys.argv
  # create top level classes as attributes
 def run(...)
 # control logic for application

if __name__ == '__main__':
 MyApp().run()

###

The advantage is that it is relatively easy to create  variations of 
the
program by subclassing the application. The disadvantage is that you
don't often sub class applications in practice! (At least in my 
experience)

> Is this a recomended Python programming technique?

I've never seen it recommended for Python in general, but it is quite
common, particularly in GUI apps. Indeed it is the recommend way for
wxPython where you must create an Application class or use the built
in SimpleApp class,

HTH,

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


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


[Tutor] Condensed python2.5 cheat sheet

2007-12-19 Thread Samm and Andy
Hi people,

I've a competent programmer friend who I'm trying to convert to the ways 
of python and I was wondering if people could recommend a decent cheat 
sheet for python 2.5.
He know how to program but just needs the syntax to become pythonic

Thanks

Andy

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


Re: [Tutor] Placing entire Application inside a class

2007-12-19 Thread Kent Johnson
Jim Morcombe wrote:
> I have just read through "Creating a GUI in Python - by Dakota Lemaster"
>  
> In it, Dakota recomends placing the entire application within a class.
>  
> Why is this so?  Surely in many cases you end up with a constructor for 
> the class that is cumbersome and complex?

Many GUI frameworks (though not Tkinter) include an application class 
that you must instantiate and to start the application's event loop. It 
is convenient to subclass the framework's application class for your own 
initialization.

Event callbacks for button presses, etc, generally must access some 
shared state, either the app's data model or other GUI elements or both. 
Shared state requires either global variables or a class instance to 
hold the state. Here again, an app class is useful.

In my experience most apps don't have that many constructor arguments. 
If there are many options, usually some kind of settings object is 
created and used by the app.

> Is this a recomended Python programming technique?

For GUI programs it works well. For non-GUI programs classes may not be 
needed at all, it depends on the requirements of the program. One of the 
strengths of Python is that it allows different programming styles, you 
can pick the one that fits the problem and your preferences.

> http://student-iat.ubalt.edu/sde/students/lemaster/COSC330/Final/sec4_AppClass.html

In TicTacToe.py, notice that the event handler labelClicked() accesses 
self.labelList and self.gameOver. This is an example of shared state.

The reset button is bound to a nested function; I think this would be 
better written as another method of the class also.

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


Re: [Tutor] Condensed python2.5 cheat sheet

2007-12-19 Thread Kent Johnson
Samm and Andy wrote:
> Hi people,
> 
> I've a competent programmer friend who I'm trying to convert to the ways 
> of python and I was wondering if people could recommend a decent cheat 
> sheet for python 2.5.
> He know how to program but just needs the syntax to become pythonic

Maybe one of these?
http://rgruet.free.fr/
http://www.python.org/doc/QuickRef.html
http://www.limsi.fr/Individu/pointal/python/pqrc/

Also Python in a Nutshell has a good, brief introduction to the language.

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


Re: [Tutor] Condensed python2.5 cheat sheet

2007-12-19 Thread Jeff Johnson
There is an excellent book for programmers from other languages.  Dive
Into Python.  http://www.diveintopython.org/toc/index.html

Jeff

On Wed, 2007-12-19 at 10:08 +, Samm and Andy wrote:
> Hi people,
> 
> I've a competent programmer friend who I'm trying to convert to the ways 
> of python and I was wondering if people could recommend a decent cheat 
> sheet for python 2.5.
> He know how to program but just needs the syntax to become pythonic
> 
> Thanks
> 
> Andy
> 
> ___
> 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] Condensed python2.5 cheat sheet

2007-12-19 Thread Alan Gauld

"Samm and Andy" <[EMAIL PROTECTED]> wrote

> of python and I was wondering if people could recommend a decent 
> cheat
> sheet for python 2.5.

There is a quick reference linked on the Python web site:

Here is the URL:

http://rgruet.free.fr/PQR25/PQR2.5.html

However I'd still recommend an afternoon going through the official
tutorial which introduces the idioms of Python aswell as the features
and syntax.


HTH,


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


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


[Tutor] Handling MySQLdb exceptions

2007-12-19 Thread Paul Schewietzek
Hi there!

I'm writing a script that inserts data from a .csv file into a 
MySQL-Database.

Actually, it works fine (the data make it into the database correctly), 
however everytime it runs it raises an exception coming from the 
MySQLdb-module.

Here's the code:

--
#!/usr/bin/env python

import MySQLdb
import sys

if len(sys.argv) <> 2:
 print("""Usage: put_data_into_pool 
 Columns need to be:
 title, firstname, lastname, street, number of 
house, postal code, city, phone number""")
 sys.exit(0)

tabelle = open(sys.argv[1], "r")

db = MySQLdb.connect(host="localhost", user="user", passwd="", 
db="db")

cursor = MySQLdb.cursors.Cursor(db)

line = tabelle.readline()

while line <> "":
#try:
 cursor.execute('INSERT INTO pool (titel, vorname, 
nachname, strasse, hausnummer, plz, ort, rufnummer, datum) VALUES (' + 
line + ');')
 line = tabelle.readline()
#except(_mysql_exceptions.OperationalError):
#pass

tabelle.close()
--

The exception goes like this:

--
Traceback (most recent call last):
   File "bin/auftragserfassung/put_data_into_pool.py", line 22, in 
 cursor.execute('INSERT INTO pool (titel, vorname, nachname, 
strasse, hausnummer, plz, ort, rufnummer, datum) VALUES (' + line + ');')
   File "/var/lib/python-support/python2.5/MySQLdb/cursors.py", line 
166, in execute
 self.errorhandler(self, exc, value)
   File "/var/lib/python-support/python2.5/MySQLdb/connections.py", line 
35, in defaulterrorhandler
 raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1136, "Column count doesn't match 
value count at row 1")
---

Is there any way to handle this exception? As you can see, I already 
tried it with _mysql_exceptions.OperationalError (the lines that are 
commented out), but _mysql_exceptions is not defined to Python

Just so you don't need to wonder: The .csv-file I give to the script for 
testing is absolutely OK.

On a side note, would you think I should post this somewhere else? If 
so, where?

Any help is appreciated - I'll answer tomorrow (have to go now).

Kindest regards, Paul
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Handling MySQLdb exceptions

2007-12-19 Thread Joshua Simpson
On Dec 19, 2007 10:14 AM, Paul Schewietzek <[EMAIL PROTECTED]> wrote:

>
> Is there any way to handle this exception? As you can see, I already
> tried it with _mysql_exceptions.OperationalError (the lines that are
> commented out), but _mysql_exceptions is not defined to Python
>
>
"OperationalError" is contained in the MySQLdb module and thus namespace, so
you'll have to reference it like so:

except MySQLdb.OperationalError:


-- 
-
http://stderr.ws/
"Insert pseudo-insightful quote here." - Some Guy
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Handling MySQLdb exceptions

2007-12-19 Thread Kent Johnson
Paul Schewietzek wrote:
> Just so you don't need to wonder: The .csv-file I give to the script for 
> testing is absolutely OK.

Except that it contains data that the insert statement doesn't 
like...does it contain any blank lines? Printing 'line' in the exception 
handler would be useful.

Also this code is vulnerable to SQL injection attacks, if you don't 
trust the source of the input file you should not use this. For example 
if the file contained a line like
titel, vorname, nachname, strasse, hausnummer, plz, ort, 
rufnummer,datum); delete from pool; --
that would be bad.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] constants, flags or whatever

2007-12-19 Thread Jim Morcombe
In a program, I want to set some kind of variable or object to indicate what 
"mode" the program is currently in.
What is the most elegant way of doing this?

Jim
---
constant: moving = "m"
constant: inserting = "i"
constant:jumping = "j"
.
.
action = moving
.
.
.
if action == jumping:
jumpSomewhere()
elseif action == moving:
moveSomewhere()
elseif action == inserting:
insertSomething()

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


Re: [Tutor] constants, flags or whatever

2007-12-19 Thread bhaaluu
This isn't elegant, but it is a start. My method is: get SOMETHING working,
then work from there. 8^D

"""
constant: moving = "m"
constant: inserting = "i"
constant: jumping = "j"
.
.
action = moving
.
.
.
if action == jumping:
jumpSomewhere()
elseif action == moving:
moveSomewhere()
elseif action == inserting:
insertSomething()
"""
##
moving = False
inserting = False
jumping = False

def jumpingSomewhere():
global jumping
print jumping
jumping = True
return jumping

def insertingSomething():
global inserting
print inserting
inserting = True
return inserting

def movingSomewhere():
global moving
print moving
moving = True
return moving

jumpingSomewhere()
insertingSomething()
movingSomewhere()

print jumping
print inserting
print moving
#

Output:
False
False
False
True
True
True


On Dec 19, 2007 8:44 PM, Jim Morcombe <[EMAIL PROTECTED]> wrote:
>
>
> In a program, I want to set some kind of variable or object to indicate what
> "mode" the program is currently in.
> What is the most elegant way of doing this?
>
> Jim
> ---
> constant: moving = "m"
> constant: inserting = "i"
> constant:jumping = "j"
> .
> .
> action = moving
> .
> .
> .
> if action == jumping:
> jumpSomewhere()
> elseif action == moving:
> moveSomewhere()
> elseif action == inserting:
> insertSomething()
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>



-- 
b h a a l u u at g m a i l dot c o m
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] constants, flags or whatever

2007-12-19 Thread bob gailer
Jim Morcombe wrote:
> In a program, I want to set some kind of variable or object to 
> indicate what "mode" the program is currently in.
> What is the most elegant way of doing this?
>  
> Jim
> ---
> constant: moving = "m"
> constant: inserting = "i"
> constant:jumping = "j"
> .
> .
> action = moving
> .
> .
> .
> if action == jumping:
> jumpSomewhere()
> elseif action == moving:
> moveSomewhere()
> elseif action == inserting:
> insertSomething()
> 
1 - I see no value in introducing variables. I'd just use string constants:

action = "moving"
.
.
if action == "jumping":

etc.


2 - It is common in Python to use a dictionary to map the constants to 
the functions instead of if-elif statements:

actions = dict( jumping = jumpSomewhere, 
moving = moveSomewhere, 
inserting = insertSomething)


the entire if-elif construct now becomes:

actions[action]()

adding new action-function pairs is now a lot easier.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] constants, flags or whatever

2007-12-19 Thread Dave Kuhlman
On Wed, Dec 19, 2007 at 09:41:13PM -0500, bob gailer wrote:

> 1 - I see no value in introducing variables. I'd just use string constants:
> 
> action = "moving"
> .
> .
> if action == "jumping":
> 
> etc.

I agree.  But, some people do prefer something that looks a bit
like an enum.  If so, here is a Python idiom for enums:

Mode_none, Mode_moving, Mode_inserting, Mode_jumping = range(4)

action = Mode_moving

if action == Mode_jumping:
o
o
o

- Dave


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


Re: [Tutor] constants, flags or whatever

2007-12-19 Thread Steve Willoughby
Dave Kuhlman wrote:
> On Wed, Dec 19, 2007 at 09:41:13PM -0500, bob gailer wrote:
> 
>> 1 - I see no value in introducing variables. I'd just use string constants:
>>
>> action = "moving"
>> .
>> .
>> if action == "jumping":
>>
>> etc.
> 
> I agree.  But, some people do prefer something that looks a bit
> like an enum.  If so, here is a Python idiom for enums:

One big advantage to using variables over string constants is that
there is a big danger of misspelling one of the constants and not
noticing the error.

action = "moving"
...
if action == "moveing":
   # oops, this never gets executed


if you had used variables, Python would catch the error as an
attempt to use an undefined variable:

action = MOVING
...
if action == MOVEING:  <-- error caught by python


> Mode_none, Mode_moving, Mode_inserting, Mode_jumping = range(4)
> 
> action = Mode_moving
> 
> if action == Mode_jumping:
> o
> o
> o
> 
> - Dave
> 
> 


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