473,378 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Just magic ...

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
4 1328
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jacek Generowicz | last post by:
I am writing an extension type, and wish to add some magic methods which are not catered for by the tp_<whatever> slots (eg tp_init -> __init__) in PyTypeObject. My methods seem to work correctly...
38
by: Kevin Smith | last post by:
For what it's worth, I wrote the original PEP 318. I probably wasn't qualified, but I just wanted a nice simple way to declare class methods without having to repeat the function name. After...
0
by: Nilsson Mats | last post by:
Hi! I have an intresting problem for our programming community on Solaris. I want to develop an environment where: 1) The developers shouldn't need to bother about which Perl version to use....
17
by: Mike Labosh | last post by:
I can say this: int commandTimeout = Convert.ToInt32(ConfigurationSettings.AppSettings); but I can't say this: int commandTimeout = (int)ConfigurationSettings.AppSettings; Or maybe my VB...
19
by: youpak2000 | last post by:
Are MAGIC numbers always bad? Using magic numbers (constant numbers) in programs are generally considered a bad programming practice, and it's recommended that to define constants in single,...
16
by: per9000 | last post by:
Hi, I recently started working a lot more in python than I have done in the past. And I discovered something that totally removed the pretty pink clouds of beautifulness that had surrounded my...
8
KoreyAusTex
by: KoreyAusTex | last post by:
I am pretty new at programming and need some feedback as to why this program is not working??? It was a pretty big undertaking for me but I can't seem to figure out what is wrong: import...
2
by: jyck91 | last post by:
i have done the magic square: #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 13 main() { FILE *fp; int i, j, n, row, column;
1
by: manju01 | last post by:
in the below program we can generate magic square of size 3-160 but i want to print the output like for magic size n ************************ * * * * * * 5 * 8 * 7 *...
9
by: Larry Hale | last post by:
I've heard tell of a Python binding for libmagic (file(1) *nixy command; see http://darwinsys.com/file/). Generally, has anybody built this and worked with it under Windows? The only thing I've...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.