Just magic ...

  • Thread starter Armin Steinhoff
  • Start date
A

Armin Steinhoff

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
 
J

Jeff Epler

I don't know, and since the code you posted doesn't run I can't find
out.
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'self' is not defined

Jeff
 
R

root

Jeff said:
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)
 
A

Armin Steinhoff

root said:
Jeff said:
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??

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)


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

Jeff
 
A

Armin Steinhoff

root said:
Jeff said:
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)


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

Jeff
 

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

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,969
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top