Re: [Tutor] Handling MySQLdb exceptions

2007-12-20 Thread Paul Schewietzek
Joshua Simpson schrieb:
> 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

Thanks a lot! It works 8D

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


Re: [Tutor] Handling MySQLdb exceptions

2007-12-20 Thread Paul Schewietzek
Kent Johnson schrieb:
> A more robust solution would be to read the file with the csv module and 
> use cursor.execute() with a proper parameter list. This lets the csv and 
> database modules correctly (un)escape the data values.
> 
> Kent
> 

WOW thanks! I never thought about that there might be a csv-module! I'm 
on my way exploring it ;)

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


[Tutor] XML data reading

2007-12-20 Thread Lockhart, Luke

Hello all,

So I'm a very novice Python programmer. I've done stuff up to the intermediate 
level in Microsoft flavors of BASIC and C++, but now I'm a Linux man and trying 
to realize my overly ambitious programming dreams with Python, mainly because I 
have friends who use it and because it has libraries that in general are very 
good at doing what I want to do.

Now, the program I'm working on would hypothetically store and read all data as 
XML, and yes I'm aware of the performance drawbacks and I'm willing to live 
with them. But I just can't figure out the various libraries that Python uses 
to read XML, and my friend's code doesn't help much.

Basically, what I really would like to do, without going into a lot of detail, 
is be able to read various tags into classes and arrays until the entire file 
has been read, then remove the file from memory. I first tried to use the basic 
Python XML libraries, and then my friend recommended SAX - but so far as I can 
tell, either method requires numerous lines of code to support one new tag. Is 
this what I'm going to have to do, or is there a simpler way?

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


[Tutor] XML

2007-12-20 Thread Lockhart, Luke


Hello all,

Sorry if this is a double post, I had some technical problems with subscribing 
to this list.

So I'm a very novice Python programmer. I've done stuff up to the intermediate 
level in Microsoft flavors of BASIC and C++, but now I'm a Linux man and trying 
to realize my overly ambitious programming dreams with Python, mainly because I 
have friends who use it and because it has libraries that in general are very 
good at doing what I want to do.

Now, the program I'm working on would hypothetically store and read all data as 
XML, and yes I'm aware of the performance drawbacks and I'm willing to live 
with them. But I just can't figure out the various libraries that Python uses 
to read XML, and my friend's code doesn't help much.

Basically, what I really would like to do, without going into a lot of detail, 
is be able to read various tags into classes and arrays until the entire file 
has been read, then remove the file from memory. I first tried to use the basic 
Python XML libraries, and then my friend recommended SAX - but so far as I can 
tell, either method requires numerous lines of code to support one new tag. Is 
this what I'm going to have to do, or is there a simpler way?

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


Re: [Tutor] XML

2007-12-20 Thread Kent Johnson
Lockhart, Luke wrote:
> Now, the program I'm working on would hypothetically store and read all 
> data as XML, and yes I'm aware of the performance drawbacks and I'm 
> willing to live with them. But I just can't figure out the various 
> libraries that Python uses to read XML, and my friend's code doesn't 
> help much.
> 
> Basically, what I really would like to do, without going into a lot of 
> detail, is be able to read various tags into classes and arrays until 
> the entire file has been read, then remove the file from memory.

Do you want to create your own classes to represent the data or do you 
just want to get some kind of object model? If the latter, take a look 
at ElementTree, it is IMO the best of the included XML packages.
http://docs.python.org/lib/module-xml.etree.ElementTree.html

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


Re: [Tutor] XML data reading

2007-12-20 Thread Michael Langford
Sax is the simplest to get started with. here is a simple example. See
http://docs.python.org/lib/content-handler-objects.html for more info
on the methods of ContentHandler. Using some list or dict of tags
you're processing such as I do in the following example will keep "tag
specific code" down to a minimum. Theoretically, you'd just have to
change the list you initialize the object with and the endElement
function to handle a new tag.

from xml import sax
from xml.sax.handler import ContentHandler
class myhandler(ContentHandler):
def __init__(self, tagsToChirpOn=None):
self.last = ""
self.info = ""
if tagsToChirpOn is None:
self.chirpTags = []
else:
self.chirpTags = tagsToChirpOn


#then you define a start element method
#   this is called for each open tag you see
def startElement(self,name,attr):
self.last = name
self.info = ""
if name in self.chirpTags:
print "starting %s tag"  % name


#then you define a characters method, which
#  is called on sections of text inside the
#  tags until it is all found
def characters(self,content):
if self.last in self.chirpTags:
self.info +=content

#then if you need to define an action to happen
#when an end tag is hit, you write a
def endElement(self,name):
"""called at  0:
print "In tag %s was data{{%s}}" % (self.last,self.info)
if name in self.chirpTags:
print "Now leaving the %s tag" % name

if __name__=="__main__":
 document = """
 
line 1
   bars are fun

line 2
   dogs don't like celery


   121309803124.12

 """
 hand = myhandler(["bar","baz"])
 sax.parseString(document,hand)


You often need to build a state machine or some other stateful
tracking system to make Sax parsers do complicated things, but the
above is good enough for most things involving data. If you use the
start tag to create a new object, the characters tag to populate it
and then the endElement tag to submit the object to a greater data
structure, you can very easily build objects out of XML data of any
source. I used sax parsers most recently on parsing out REST data from
amazon.

urlib2 and sax parsers are formidable, quick technologies to perform
simple parsing needs. Look into BeautifulSoup as well:
http://www.crummy.com/software/BeautifulSoup/

--Michael

On Dec 20, 2007 4:15 PM, Lockhart, Luke <[EMAIL PROTECTED]> wrote:
>
>
>
>
> Hello all,
>
>  So I'm a very novice Python programmer. I've done stuff up to the
> intermediate level in Microsoft flavors of BASIC and C++, but now I'm a
> Linux man and trying to realize my overly ambitious programming dreams with
> Python, mainly because I have friends who use it and because it has
> libraries that in general are very good at doing what I want to do.
>
>  Now, the program I'm working on would hypothetically store and read all
> data as XML, and yes I'm aware of the performance drawbacks and I'm willing
> to live with them. But I just can't figure out the various libraries that
> Python uses to read XML, and my friend's code doesn't help much.
>
>  Basically, what I really would like to do, without going into a lot of
> detail, is be able to read various tags into classes and arrays until the
> entire file has been read, then remove the file from memory. I first tried
> to use the basic Python XML libraries, and then my friend recommended SAX -
> but so far as I can tell, either method requires numerous lines of code to
> support one new tag. Is this what I'm going to have to do, or is there a
> simpler way?
>
>  Thanks in advance,
>  Luke
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>



-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.RowdyLabs.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XML data reading [slightly off topic/silly]

2007-12-20 Thread Eric Walstad
Lockhart, Luke wrote:
> ...now I'm a
> Linux man and trying to realize my overly ambitious programming dreams 
> with Python
Welcome to the real world, Neo.  I'm not saying that you'll be able to 
work with XML via DOM or SAX in Python.  I'm saying that when you are 
ready, you won't have to.  :)
(with apologies to the Wachowski brothers)


 > But I just can't figure out the various
 > libraries that Python uses to read XML, and my friend's code doesn't
 > help much.
I think Google will help with many examples.  Also have a look at the 
Python Cookbook:
http://aspn.activestate.com/ASPN/Python/Cookbook/
for examples.


/me climbs onto soap box (no pun intended)
I think this might be a little off topic but my advice as you learn 
Python, considering you are parsing data and might have OOP experience, 
is to consider that there is value in simple code.  Try to keep an open 
mind and try not to use too many dots, especially if you have control 
over both ends of your data transfer.

Here's what I think is a nice example of roughly 17 lines of python code 
that elegantly parse and process a 5M record file with an impressive 
speed gain, too:
http://groups.google.com/group/comp.lang.python/msg/38a13587b6b298a2

Welcome, have fun and do post to the list when you have specific questions!

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


[Tutor] re-initialising shells

2007-12-20 Thread Jim Morcombe
I have had a couple of strange cases I don't understand.

I am using IDLE on Windows

My program imports some stuff from another file using the statement "from 
qwerty import *"

I have changed qwerty and saved it away.  I have then run my program (F5) and 
the program acts as if it is using an old version of qwerty.

I close down all my python windows, start it again and the changes are picked 
up.

What are the rules with "shells" and so on?

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