[Tutor] Help with cursors please

2010-02-04 Thread Alan Harris-Reid

Hi,

I have a SQLite cursor which I want to traverse more than once, eg...
for row in MyCursor:
   method1(row)
  
then later...

for row in MyCursor:
   method2(row)
  
Method2 is never run, I guess because the pointer is at the bottom of 
the row 'stack' after the first 'for' loop


How can I move the pointer back to the top of the stack?  Or do I have 
to reload the cursor first?


Any help would be appreciated.
Alan
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with cursors please

2010-02-04 Thread Alan Harris-Reid

Kent Johnson wrote:

On Thu, Feb 4, 2010 at 12:47 PM, Alan Harris-Reid
 wrote:
  

Hi,

I have a SQLite cursor which I want to traverse more than once, eg...
for row in MyCursor:
  method1(row)
 then later...
for row in MyCursor:
  method2(row)
 Method2 is never run, I guess because the pointer is at the bottom of the
row 'stack' after the first 'for' loop

How can I move the pointer back to the top of the stack?  Or do I have to
reload the cursor first?



Either reload the cursor or use fetchall() to get the results into a
list and iterate the list.

rows = MyCursor.fetchall()
for row in rows;
  ...


Kent
Thanks for the reply Kent.  I used fetchall() to get the results into a 
list and everything works fine now.


Regards,
Alan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Superclass call problem

2010-02-20 Thread Alan Harris-Reid

Hi,

I am having trouble understanding how superclass calls work.  Here's 
some code...


class ParentClass():
   def __init__(self):
   do something here

class ChildClass(ParentClass):
   def __init__(self):
  super().__init__(self) # call parentclass 
__init__ method

  do something else here


When the super().__init__ line runs I get the error "__init__() takes 
exactly 1 positional argument (2 given)"


Can anyone tell me where I have gone wrong?  I thought the self 
parameter should be passed to all class methods.


TIA
Alan Harris-Reid
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Superclass call problem

2010-02-20 Thread Alan Harris-Reid

Kent Johnson wrote:

On Sat, Feb 20, 2010 at 12:50 PM, Alan Harris-Reid
 wrote:
  

Hi,

I am having trouble understanding how superclass calls work.  Here's some
code...

class ParentClass():
  def __init__(self):
  do something here



You should inherit object to use super():
class ParentClass(object):

Kent


Hi Kent, thanks for the reply,

Sorry, left out 'object' from my example.  The actual code already reads
class ParentClass(object):

Alan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Superclass call problem

2010-02-20 Thread Alan Harris-Reid

Luke Paireepinart wrote:

Your call to super is wrong. It should be super and you pass the class
and instance, then call init.

On 2/20/10, Alan Harris-Reid  wrote:
  

Hi,

I am having trouble understanding how superclass calls work.  Here's
some code...

class ParentClass():
def __init__(self):
do something here

class ChildClass(ParentClass):
def __init__(self):
   super().__init__(self) # call parentclass
__init__ method
   do something else here


When the super().__init__ line runs I get the error "__init__() takes
exactly 1 positional argument (2 given)"

Can anyone tell me where I have gone wrong?  I thought the self
parameter should be passed to all class methods.

TIA
Alan Harris-Reid
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Hi Luke, thanks for the reply,

In addition to your suggestion, looks like I can use

class ChildClass(ParentClass):
   def __init__(self, arg):
   super(ParentClass, self).__init__(arg)
   or
   super().__init__(arg)# this only works in Python 3, I think

I prefer the 2nd way, as it is more generic (ie. don't need to state 
parent-class).


Regards,
Alan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Superclass call problem

2010-02-21 Thread Alan Harris-Reid



Hi,

I am having trouble understanding how superclass calls work.  Here's
some code...



What version of Python are you using?

In Python 2.x, you MUST inherit from object to use super, and you MUST 
explicitly pass the class and self:


class ParentClass(object):
def __init__(self, a, b, c):
do something here

class ChildClass(ParentClass):
def __init__(self, a, b, c):
super(ChildClass, self).__init__(a, b, c)

In Python 3.x, all classes inherit from object and you no longer need to 
explicitly say so, and super becomes a bit smarter about where it is 
called from:


# Python 3 only
class ParentClass:
def __init__(self, a, b, c):
do something here

class ChildClass(ParentClass):
def __init__(self, a, b, c):
super().__init__(a, b, c)


I assume you are using Python 3.0 or 3.1. (If you're 3.0, you should 
upgrade to 3.1: 3.0 is s-l-o-w and no longer supported.)



Your mistake was to pass self as an explicit argument to __init__. This 
is not needed, because Python methods automatically get passed self:


  

def __init__(self):
super().__init__(self)



That has the effect of passing self *twice*, when __init__ expects to 
get self *once*, hence the error message you see:


  

When the super().__init__ line runs I get the error "__init__() takes
exactly 1 positional argument (2 given)"


Hi Steven, thanks for the reply.

Fortunately I am using Python 3.1, so I can use the super().__init__(a, 
b, c) syntax.


Regards,
Alan
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] SQLite error messages

2010-03-09 Thread Alan Harris-Reid

Hi there,

I am using the sqlite3 module with Python 3.1, and have some code which 
goes something like as follows...


import sqlite3
con = sqlite3.connect('MyDatabase.db')

try:
   execresult = con.execute('INSERT INTO MyTable (field_name) VALUES 
("MyValue")')

   con.commit()
except:
   con.rollback()
   
If con.execute() fails, nothing is returned, and although the code 
correctly executes the rollback next, I have no idea why, and therefore 
cannot create a suitable error-handler with meaningful messages.  

I notice from the SQLite website that there are error codes, but it 
looks like the sqlite3 module is not reporting them.  


Has anyone any ideas how to get around this problem?

TIA,
Alan Harris-Reid
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] SQLite error messages

2010-03-10 Thread Alan Harris-Reid

Benno Lang wrote:

On 10 March 2010 11:37, Alan Harris-Reid  wrote:
  

Hi there,

I am using the sqlite3 module with Python 3.1, and have some code which goes
something like as follows...

import sqlite3
con = sqlite3.connect('MyDatabase.db')

try:
  execresult = con.execute('INSERT INTO MyTable (field_name) VALUES
("MyValue")')
  con.commit()
except:
  con.rollback()
  If con.execute() fails, nothing is returned, and although the code
correctly executes the rollback next, I have no idea why, and therefore
cannot create a suitable error-handler with meaningful messages.
I notice from the SQLite website that there are error codes, but it looks
like the sqlite3 module is not reporting them.



Do you mean numerical error codes? Which page on the SQLite website
are you referring to? Certainly the exception contains usable data.
Try something like this:

try:
  execresult = con.execute('INSERT INTO MyTable (field_name) VALUES
("MyValue")')
  con.commit()
except Exception as error:
  print("Didn't work:", error)
  con.rollback()

(I didn't create a table, so I get "Didn't work: no such table: MyTable")

HTH,
benno

Hi Benno,  your example is great - just what I needed!

Regarding SQLite error codes, the list I was referring to is at 
www.sqlite.org/c3ref/c_abort.html 
<http://www.sqlite.org/c3ref/c_abort.html>, but it doesn't look complete 
because I have already come-across some IntegrityError messages which 
aren't on the list.


Regards,
Alan




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] SQLite error messages

2010-03-10 Thread Alan Harris-Reid

Sander Sweers wrote:

- Original message -
  

I am using the sqlite3 module with Python 3.1, and have some code which
goes something like as follows...

import sqlite3
con = sqlite3.connect('MyDatabase.db')

try:
execresult = con.execute('INSERT INTO MyTable (field_name) VALUES
("MyValue")')
con.commit()
except:



Here you catch all exceptions. Normally you would catch a specific exception like ValueError.  
  

con.rollback()




Do you know finally? It is run after all the exceptions have been handled and 
this is where I would put the rollback.

Greets,
Sander


Hello Sander, thanks for the reply.

"Normally you would catch a specific exception like ValueError."
Agreed, but as I don't know what type the exception is, I would have to 
provide a suitable error message for all exception types (ValueError, 
IntegrityError, etc.).  At this stage catching Exception as errormessage 
is sufficient for my purposes.


"Do you know finally? It is run after all the exceptions have been 
handled and this is where I would put the rollback."
In this case there is no 'finally' section, because if the 'try' section 
doesn't work, then I want the rollback to occur for *all *exceptions. 

Maybe I have misunderstood you, but I always thought that the 'finally' 
section was run even if the 'try' section is successful, in which case I 
would not want a rollback.  (According to the Python documentation 
(section 8.6) "A /finally clause/ is always executed before leaving the 
try  
statement, whether an exception has occurred or not.").


Regards,
Alan



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unable to run a simple Hello.py in WinXP

2010-05-17 Thread Alan Harris-Reid

Hi Siva,

I can run the .py file from the DOS command line without any problem 
under WinXP.  Can you give some more information regarding your error 
message?


Regards,
Alan Harris-Reid

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Cherrypy and Iframes (or how to insert a file in a, page)

2011-04-09 Thread Alan Harris-Reid


Andreas...

>  NotFound: (404, "The path '/file.txt' was not found.")

This error message often occurs because file.txt is a static file and 
you have not told CherryPy where to look for static files.  From the 
look of the error I am guessing that file.txt is in your root directory, 
in which case you need the following in your configuration file.


[/]
tools.staticdir.root = '/path/to/root/folder' # no trailing backslash
tools.staticdir.on = True
tools.staticdir.dir = ""   # necessary 
to serve static files in home folder


Hope this helps.
Alan Harris-Reid

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor