By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

Just magic ...

P: n/a
Hi all,

I'm just porting a piece of python code for using a different
database.
When I run the original version .. the type of 'set' is a tuple ...
and is magically in the new version a 'class instance'. The code is
the same in both versions:

ins_list = [(0,1,2,3),]
self.IMpos =0
for set in ins_list:
setdata = (self.IMpos,) + set[1:]

Any ideas why set is treated as a 'tuple' and in the other case as a
'class instance' ?? (Python 2.3.3 )
Regards

Armin
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I don't know, and since the code you posted doesn't run I can't find
out.
self.IMpos =0

Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'self' is not defined

Jeff

Jul 18 '05 #2

P: n/a
Jeff Epler wrote:
I don't know, and since the code you posted doesn't run I can't find
out.
#it's only a subset of the code ...

class SlaveParSet(QDialog):
def __init__(self,parent = None, name=None, modal = 0, fl = 0,
Conf='PROFIBUS', saddr= 0):

if name == None:
self.setName('SlaveParSet')

self.config_name=conf
self.slave_addr = saddr

self.gsd_connect = sqlite.connect("gsd_db")
self.gsd_curs = self.gsd_connect.cursor()

# open project data base
self.prj_connect = sqlite.connect("proj_db")
self.prj_curs = self.prj_connect.cursor()

self.ID = ''
self.IMpos = 0
# insert selected module into installed modules
def ModuleSelectionval(self, nr):
module = str(self.ModuleSelection.text(nr))

#rebuild pos/module relationship
Sel = str("""select mod_pos, mod_name, proj, saddr,
mod_config_data, mod_prm_data
from INSTMODULE where proj= %s and saddr = %s""")
Key = (self.config_name, self.slave_addr)
self.prj_curs.execute(Sel, Key)
ins_list = self.prj_curs.fetchall()

#ins_list is a list and contains now one tuple

if len(ins_list):
self.IMpos +=1
InsPos = self.IMpos

Ins = str("""insert into INSTMODULE(mod_pos, mod_name, proj,

saddr, mod_config_data, mod_prm_data)
values (%s, %s, %s, %s, %s, %s)""")

self.IMpos =0
for set in ins_list:
setdata = (self.IMpos,) + set[1:]

# set should reference a tuple but it refers to class instance .. WHY??

self.prj_curs.execute(Ins, setdata)
self.InstModules.insertItem(set[1], self.IMpos)

self.IMpos +=1
if InsPos == self.IMpos:
self.InstModules.insertItem(new_item[1], self.IMpos)

self.prj_curs.execute(self.Ins, new_item)
self.IMpos +=1
if self.IMpos > 0:
self.IMpos -= 1

self.InstModules.setCurrentItem(self.IMpos)
self.prj_connect.commit()
else:
self.IMpos = 0
self.InstModules.clear()
self.InstModules.insertItem(module, self.IMpos)
self.InstModules.setCurrentItem(self.IMpos)

self.IMpos =0


Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'self' is not defined

Jeff

Jul 18 '05 #3

P: n/a
root wrote:
Jeff Epler wrote:
I don't know, and since the code you posted doesn't run I can't find
out.

#it's only a subset of the code ...

class SlaveParSet(QDialog):
def __init__(self,parent = None, name=None, modal = 0, fl = 0,
Conf='PROFIBUS', saddr= 0):

if name == None:
self.setName('SlaveParSet')

self.config_name=conf
self.slave_addr = saddr

self.gsd_connect = sqlite.connect("gsd_db")
self.gsd_curs = self.gsd_connect.cursor()

# open project data base
self.prj_connect = sqlite.connect("proj_db")
self.prj_curs = self.prj_connect.cursor()

self.ID = ''
self.IMpos = 0
# insert selected module into installed modules
def ModuleSelectionval(self, nr):
module = str(self.ModuleSelection.text(nr))

#rebuild pos/module relationship
Sel = str("""select mod_pos, mod_name, proj, saddr,
mod_config_data, mod_prm_data
from INSTMODULE where proj= %s and saddr = %s""")
Key = (self.config_name, self.slave_addr)
self.prj_curs.execute(Sel, Key)
ins_list = self.prj_curs.fetchall()

#ins_list is a list and contains now one tuple

if len(ins_list):
self.IMpos +=1
InsPos = self.IMpos

Ins = str("""insert into INSTMODULE(mod_pos, mod_name, proj,
saddr, mod_config_data, mod_prm_data)
values (%s, %s, %s, %s, %s, %s)""")

self.IMpos =0
for set in ins_list:
setdata = (self.IMpos,) + set[1:]

# set should reference a tuple but it refers to class instance .. WHY??

set.__class__ <class sqlite.main.PgResultSetConcreteClass at 0x4164914c>


So why is 'set' ( a local var of a procedure defined in the class
SetSlavePar ) now a 'class instance' of the class sqlite ??

Armin


self.prj_curs.execute(Ins, setdata)
self.InstModules.insertItem(set[1], self.IMpos)
self.IMpos +=1
if InsPos == self.IMpos:
self.InstModules.insertItem(new_item[1], self.IMpos)
self.prj_curs.execute(self.Ins, new_item)
self.IMpos +=1
if self.IMpos > 0:
self.IMpos -= 1

self.InstModules.setCurrentItem(self.IMpos)
self.prj_connect.commit()
else:
self.IMpos = 0
self.InstModules.clear()
self.InstModules.insertItem(module, self.IMpos)
self.InstModules.setCurrentItem(self.IMpos)

> self.IMpos =0

Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'self' is not defined

Jeff

Jul 18 '05 #4

P: n/a
root wrote:
Jeff Epler wrote:
I don't know, and since the code you posted doesn't run I can't find
out.

#it's only a subset of the code ...

class SlaveParSet(QDialog):
def __init__(self,parent = None, name=None, modal = 0, fl = 0,
Conf='PROFIBUS', saddr= 0):

if name == None:
self.setName('SlaveParSet')

self.config_name=conf
self.slave_addr = saddr

self.gsd_connect = sqlite.connect("gsd_db")
self.gsd_curs = self.gsd_connect.cursor()

# open project data base
self.prj_connect = sqlite.connect("proj_db")
self.prj_curs = self.prj_connect.cursor()

self.ID = ''
self.IMpos = 0
# insert selected module into installed modules
def ModuleSelectionval(self, nr):
module = str(self.ModuleSelection.text(nr))

#rebuild pos/module relationship
Sel = str("""select mod_pos, mod_name, proj, saddr,
mod_config_data, mod_prm_data
from INSTMODULE where proj= %s and saddr = %s""")
Key = (self.config_name, self.slave_addr)
self.prj_curs.execute(Sel, Key)
ins_list = self.prj_curs.fetchall()

#ins_list is a list and contains now one tuple

if len(ins_list):
self.IMpos +=1
InsPos = self.IMpos

Ins = str("""insert into INSTMODULE(mod_pos, mod_name, proj,
saddr, mod_config_data, mod_prm_data)
values (%s, %s, %s, %s, %s, %s)""")

self.IMpos =0
for set in ins_list:
setdata = (self.IMpos,) + set[1:]

# set should reference a tuple but it refers to class instance .. WHY??

However ... my work around looks like:

for set in ins_list:
lset = ()
for i,val in enumerate(set):
lset = lset+ (val,)

self.setdata = (self.IMpos,) + lset[1:]
self.prj_curs.execute(Ins, self.setdata)
self.InstModules.insertItem(lset[1], self.IMpos)

Is there a better solution ??

Armin

self.prj_curs.execute(Ins, setdata)
self.InstModules.insertItem(set[1], self.IMpos)
self.IMpos +=1
if InsPos == self.IMpos:
self.InstModules.insertItem(new_item[1], self.IMpos)
self.prj_curs.execute(self.Ins, new_item)
self.IMpos +=1
if self.IMpos > 0:
self.IMpos -= 1

self.InstModules.setCurrentItem(self.IMpos)
self.prj_connect.commit()
else:
self.IMpos = 0
self.InstModules.clear()
self.InstModules.insertItem(module, self.IMpos)
self.InstModules.setCurrentItem(self.IMpos)

> self.IMpos =0

Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'self' is not defined

Jeff

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.