instancemethod

G

Gert Cuykens

import MySQLdb

class Db:

_db=-1
_cursor=-1

@classmethod
def __init__(self,server,user,password,database):
self._db=MySQLdb.connect(server , user , password , database)
self._cursor=self._db.cursor()

@classmethod
def excecute(self,cmd):
self._cursor.execute(cmd)
self._db.commit()

@classmethod
def rowcount(self):
return int(self._cursor.rowcount)

@classmethod
def fetchone(self):
return self._cursor.fetchone()

@classmethod
def close(self):
self._cursor.close()
self._db.close()

if __name__ == '__main__':
gert=Db('localhost','root','******','gert')
gert.excecute('select * from person')
for x in range(0,gert.rowcount):
print gert.fetchone()
gert.close()

gert@gert:~$ python ./Desktop/svn/db/Py/db.py
Traceback (most recent call last):
File "./Desktop/svn/db/Py/db.py", line 35, in <module>
for x in range(0,gert.rowcount):
TypeError: range() integer end argument expected, got instancemethod.
gert@gert:~$

Can anybody explain what i must do in order to get integer instead of
a instance ?
 
N

Nanjundi

if __name__ == '__main__':
gert=Db('localhost','root','******','gert')
gert.excecute('select * from person')
for x in range(0,gert.rowcount):
print gert.fetchone()
gert.close()

gert@gert:~$ python ./Desktop/svn/db/Py/db.py
Traceback (most recent call last):
File "./Desktop/svn/db/Py/db.py", line 35, in <module>
for x in range(0,gert.rowcount):
TypeError: range() integer end argument expected, got instancemethod.
gert@gert:~$

Can anybody explain what i must do in order to get integer instead of
a instance ?
Gert,
for x in range(0,gert.rowcount):
gert.rowcount is the method (and not a data attribute).
gert.rowcount() is the method call, which get the return value from
method.

So try this.
for x in range( 0,gert.rowcount() ):

-N
 
G

Gert Cuykens

gert.rowcount is the method (and not a data attribute).
gert.rowcount() is the method call, which get the return value from
method.

So try this.
for x in range( 0,gert.rowcount() ):

Doh! :)

thx
 
B

Bruno Desthuilliers

Gert Cuykens a écrit :
import MySQLdb

class Db: (snip)
def excecute(self,cmd):
self._cursor.execute(cmd)
self._db.commit()
What about autocommit and automagic delegation ?

import MySQLdb

class Db(object):
def __init__(self,server, user, password, database):
self._db = MySQLdb.connect(server , user , password , database)
self._db.autocommit(True)
self._cursor = self._db.cursor()

def close(self):
self._cursor.close()
self._db.close()

def __del__(self):
try:
self.close()
except:
pass

def __getattr__(self, name):
attr = getattr(
self._cursor, name,
getattr(self._db, name, None)
)
if attr is None:
raise AttributeError(
"object %s has no attribute %s" \
% (self.__class__.__name__, name)
)
return attr

(NB :not tested...)
 
G

Gert Cuykens

Reading all of the above this is the most simple i can come too.

import MySQLdb

class Db:

def __init__(self,server,user,password,database):
self._db=MySQLdb.connect(server , user , password , database)
self._db.autocommit(True)
self.cursor=self._db.cursor()

def excecute(self,cmd):
self.cursor.execute(cmd)
self.rowcount=int(self.cursor.rowcount)

def close(self):
self.cursor.close()
self._db.close()

def __del__(self):
try:
self.close()
except:
pass

if __name__ == '__main__':
gert=Db('localhost','root','******','gert')
gert.excecute('select * from person')
for row in gert.cursor:
print row

This must be the most simple it can get right ?

PS i didn't understand the __getattr__ quit well but i thought it was
just to overload the privies class
 
B

Bruno Desthuilliers

Gert Cuykens a écrit :
Reading all of the above this is the most simple i can come too.

import MySQLdb

class Db:

def __init__(self,server,user,password,database):
self._db=MySQLdb.connect(server , user , password , database)
self._db.autocommit(True)
self.cursor=self._db.cursor()

def excecute(self,cmd):

Just out of curiousity: is there any reason you spell it "excecute"
instead of "execute" ?
self.cursor.execute(cmd)
self.rowcount=int(self.cursor.rowcount)

def close(self):
self.cursor.close()
self._db.close()

def __del__(self):
try:
self.close()
except:
pass


if __name__ == '__main__':
gert=Db('localhost','root','******','gert')
gert.excecute('select * from person')
for row in gert.cursor:
print row

This must be the most simple it can get right ?

Using __getattr__ is still simpler.
PS i didn't understand the __getattr__ quit well but i thought it was
just to overload the privies class

The __getattr__ method is called when an attribute lookup fails (and
remember that in Python, methods are -callable- attributes). It's
commonly used for delegation.
 
G

Gert Cuykens

import MySQLdb

class Db(object):

def __enter__(self):
pass

def __init__(self,server,user,password,database):
self._db=MySQLdb.connect(server , user , password , database)
self._db.autocommit(True)
self.cursor=self._db.cursor()

def execute(self,cmd):
self.cursor.execute(cmd)
self.rowcount=int(self.cursor.rowcount)

def close(self):
self.cursor.close()
self._db.close()

def __getattr__(self, name):
attr = getattr(self._cursor, name,getattr(self._db, name, None))
if attr is None:
raise AttributeError("object %s has no attribute %s"
%(self.__class__.__name__, name))
return attr

def __del__(self):
try:
self.close()
finally:
pass
except:
pass

def __exit__(self):
pass

if __name__ == '__main__':
gert = Db('localhost','root','*****','gert')
gert.execute('select * from person')
for row in gert.cursor:
print row

with Db('localhost','root','*****','gert') as gert:
gert.excecute('select * from person')
for row in gert.cursor:
print row

Desktop/svn/db/Py/db.py:45: Warning: 'with' will become a reserved
keyword in Python 2.6
File "Desktop/svn/db/Py/db.py", line 45
with Db('localhost','root','*****','gert') as gert:
^
SyntaxError: invalid syntax

I was thinking if it would be possible to create a object that uses
it's own instance name as a atribute.

For example instead of
gert = Db('localhost','root','*****','gert')

you would do this
gert = Db('localhost','root','*****')

and the name of the object itself 'gert' get's assigned to database somehow ?
 
B

Bruno Desthuilliers

Gert Cuykens a écrit :
import MySQLdb

class Db(object):

def __enter__(self):
pass

def __init__(self,server,user,password,database):
self._db=MySQLdb.connect(server , user , password , database)
self._db.autocommit(True)
self.cursor=self._db.cursor()

def execute(self,cmd):
self.cursor.execute(cmd)
self.rowcount=int(self.cursor.rowcount)

isn't cursor.rowcount already an int ?
def close(self):
self.cursor.close()
self._db.close()

def __getattr__(self, name):
attr = getattr(self._cursor, name,getattr(self._db, name, None))
if attr is None:
raise AttributeError("object %s has no attribute %s"
%(self.__class__.__name__, name))
return attr

def __del__(self):
try:
self.close()
finally:
pass
except:
pass

The finally clause is useless here.
def __exit__(self):
pass

if __name__ == '__main__':
gert = Db('localhost','root','*****','gert')
gert.execute('select * from person')
for row in gert.cursor:
print row

with Db('localhost','root','*****','gert') as gert:
gert.excecute('select * from person')
for row in gert.cursor:
print row

Desktop/svn/db/Py/db.py:45: Warning: 'with' will become a reserved
keyword in Python 2.6
File "Desktop/svn/db/Py/db.py", line 45
with Db('localhost','root','*****','gert') as gert:
^
SyntaxError: invalid syntax

I was thinking if it would be possible to create a object that uses
it's own instance name as a atribute.

class Obj(object):
pass

toto = tutu = tata = titi = Obj()

What's an "instance name" ?
 
G

Gert Cuykens

class Obj(object):
pass

toto = tutu = tata = titi = Obj()

What's an "instance name" ?

i would say __object__.__name__[3] == toto

And if your obj is a argument like

something(Obj())

i would say __object__.__name__[0] == 0x2b7bd17e9910
 
S

Steven D'Aprano

The finally clause is useless here.


In principle, closing a file could raise an exception. I've never seen it
happen, but it could. From the Linux man pages:

"Not checking the return value of close() is a common but nevertheless
serious programming error. It is quite possible that errors on a previous
write(2) operation are first reported at the final close(). Not checking
the return value when closing the file may lead to silent loss of data.
This can especially be observed with NFS and with disk quota."

http://www.die.net/doc/linux/man/man2/close.2.html

I assume that the same will apply in Python.

It has to be said, however, that the error recovery shown ("pass") is
fairly pointless :)
 
G

Gabriel Genellina

"Steven D'Aprano" <[email protected]> escribió en el
mensaje
The finally clause is useless here.

In principle, closing a file could raise an exception. I've never seen it
happen, but it could. From the Linux man pages: [...]
I assume that the same will apply in Python.

Note that he said that the *finally* clause were useless (and I'd say so,
too), not the *except* clause.
And yes, in Python it is checked - when the close method was called
explicitely, an exception is raised; when called when the object is garbage
collected, a message is printed on sys.stderr
It has to be said, however, that the error recovery shown ("pass") is
fairly pointless :)
Only supresses the message on sys.stderr - exceptions raised on __del__ are
never propagated.
 
S

Steven D'Aprano

"Steven D'Aprano" <[email protected]> escribió en el
mensaje
def __del__(self):
try:
self.close()
finally:
pass
except:
pass

The finally clause is useless here.

In principle, closing a file could raise an exception. I've never seen it
happen, but it could. From the Linux man pages: [...]
I assume that the same will apply in Python.

Note that he said that the *finally* clause were useless (and I'd say so,
too), not the *except* clause.

Doh!

Yes, he's right. Worse, the code as show can't possibly work: the finally
clause must come AFTER the except clause.
.... pass
.... finally:
.... pass
.... except:
File "<stdin>", line 5
except:
^
SyntaxError: invalid syntax
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top