sqlite autoincrement of primary key

2010-11-29 Thread tinauser
Dear List
I'm writing an application that has to create and populate an SQLite
database.
I'm doing pretty well, but now I'm facing a problem I can not solve.

I create a table with a primary key autoincrement, something like

sqlcmd="CREATE TABLE foo (id INTEGER PRIMARY KEY AUTOINCREMENT, name
TEXT)"
cur.execute(sqlcmd)

Now comes the time of populating the database.
I perfectly know that if I do something like:

sqlcmd="INSERT INTO foo (name) VALUES (?))"
cur.execute(sqlcmd, ('xxx',))
The table will automatically insert the value of id.


However, for readibility problem, I need to use the sqlite insert
command giving all the entries. I want, however, to let sqlite to
handle the primary key.
Normally, the sqlite command that works would be

INSERT INTO 'foo' VALUES (NULL, 'yyy' )

however, if in python i try to execute a script like:

cur.execute(
'''
INSERT INTO 'foo' VALUES (?,?)
'''
,('NULL','yyy'))

I get a datatype mismatch error.

Has anyone a workaround ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sqlite autoincrement of primary key

2010-11-29 Thread tinauser
On Nov 29, 7:28 pm, Tim Harig  wrote:
> On 2010-11-29, tinauser  wrote:
>
> > '''
> > INSERT INTO 'foo' VALUES (?,?)
> > '''
> > ,('NULL','yyy'))
>
> s/'NULL'/None/
>
> > I get a datatype mismatch error.
>
> The sqlite module is smart enough to convert between Python types and
> Sqlite types.  If you pass it 'NULL' it thinks you are passing it a string.
> Python uses None in much the same way that databases use NULL, so the
> module converts None to 'NULL' and vise versa.

Thanks all of you for the fast answers!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sqlite autoincrement of primary key

2010-11-29 Thread tinauser
On Nov 29, 10:49 pm, "D'Arcy J.M. Cain"  wrote:
> On Mon, 29 Nov 2010 19:11:18 + (UTC)
>
> Tim Harig  wrote:
> > >   INSERT INTO foo (name) VALUES ('xxx')
>
> > > That's the standard SQL way.
>
> > Yes, it works; but, the OP asked specifically to be able to enter all of
> > the field values, including the autoincrement field.
>
> You're right, I missed that.  However reading the OP's message I am
> still confused.  How does removing the field name and adding a
> positional NULL or None improve readability.  I now wonder if it was
> more of an assignment requirement rather than a real one.
>
> --
> D'Arcy J.M. Cain          |  Democracy is three 
> wolveshttp://www.druid.net/darcy/               |  and a sheep voting on
> +1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.


Was not an assignment.
I created a general table class and several specific tables.
The application I'm going to write is going to be used by scientist
that might not have a strong computational background, but they might
be willing of adding some new tables. Their code knowledge might be so
low that they might not go further a copy-paste.
The way I defined the table, they just have to create lists indicating
the name of the columns, their content and eventually some initial
values for instance.
self.colNames= [ 'id',   'name' ,   'surname',
'age']
self.colType   = [INTEGER, TEXT ,TEXT  , INTEGER]
self.init.append([   None,  'john'  ,  'Lennon'   ,
'51'   ])
i.e. the code has to resable the structure of the table and be easily
copy and paste (this last thing makes this system better than,
instead, using python dictionary...also because the future user might
not be familiar at all with dictionary).I might think of doing an
automatic substitution of the primarykey with None, that is not a
problem

Anyhow, as said, thanks for all the answers, you solved my question

Regards
-- 
http://mail.python.org/mailman/listinfo/python-list


dictionary as attribute of a class...

2011-01-05 Thread tinauser
Hallo list,
here again I have a problem whose solution might be very obvious, but
I really cannot see it:
I have a class having as attribute a dictionary whose keys are names
and values are instance of another class.
This second class has in turn as an attribute a dictionary.
I want a function of the first class that can change value of one of
the second class instance's dictionary.
however I cannot do it without modifying this attribute for ALL the
instance of the second class contained in the first class' dictionary.
What I'm doing wrong?

the code:

###
###can i change a dictionary attribute of an instantated object
without affectin all the instances of that object?

class mistClass():
def __init__(self,name,cDict={}):
print 'mistClass ',name,' Init'
self._name=name
self._cDict=cDict

def setName(self,n):
self._name=n

def getName(self):
return self._name

##def setDict(self,one,two):
##self._cDict['one']=one
##self._cDict['two']=two
def setDict(self,listK,listV):
assert len(listK)==len(listV)
for k,v in zip(listK,listV):
self._cDict[k]=v

def getDict(self):
return self._cDict

class mistClassContainer():
def __init__(self,name,dict_of_mistclass={}):
print 'init mistClassContainer ',name
self._name=name
self._DOM=dict_of_mistclass

def add_mistclass(self,mc):
for el in mc:
self._DOM[el]=mistClass(el)

##def mod_mistclasscDict(self,mc,one,two):
##self._DOM[mc].setDict(one,two)
def mod_mistclasscDict(self,mc,lK,lV):
self._DOM[mc].setDict(lK,lV)

a=mistClassContainer('firsmistclasscontainer')
a.add_mistclass(['mc1','mc2','mc3','mc4','mc5','mc6'])
print 'before modification'
for el in a._DOM.iterkeys():
print a._DOM[el].getDict()
print a._DOM[el].getName()

a.mod_mistclasscDict('mc1',['one','two'],['modone','modtwo'])
print 'after modification'
for el in a._DOM.iterkeys():
print a._DOM[el].getDict()
print a._DOM[el].getName()

b=mistClass('mc7')
print b.getDict()
print b.getName()
b.setName('modified name')
b.getName()

for el in a._DOM.iterkeys():
print a._DOM[el].getName()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary as attribute of a class...

2011-01-05 Thread tinauser
On Jan 5, 4:34 pm, Peter Otten <[email protected]> wrote:
> tinauser wrote:
> > Hallo list,
> > here again I have a problem whose solution might be very obvious, but
> > I really cannot see it:
> > I have a class having as attribute a dictionary whose keys are names
> > and values are instance of another class.
> > This second class has in turn as an attribute a dictionary.
> > I want a function of the first class that can change value of one of
> > the second class instance's dictionary.
> > however I cannot do it without modifying this attribute for ALL the
> > instance of the second class contained in the first class' dictionary.
> > What I'm doing wrong?
>
> > the code:
>
> > ###
> > ###can i change a dictionary attribute of an instantated object
> > without affectin all the instances of that object?
>
> > class mistClass():
> >     def __init__(self,name,cDict={}):
>
> When you don't provide a cDict argument the default is used which is the
> same for every instance. Change the above to
>
> def __init__(self, name, cDict=None):
>     if cDict is None:
>         cDict = {}
>
> > class mistClassContainer():
> >     def __init__(self,name,dict_of_mistclass={}):
>
> Same here.
>
> >     def setName(self,n):
> >         self._name=n
>
> >     def getName(self):
> >         return self._name
>
> Python has properties, so you don't need this just-in-case getter/setter
> nonsense.
>
> >         for k,v in zip(listK,listV):
> >             self._cDict[k]=v
>
> Make that
>
> self._cDict.update(zip(listK, listV))
>
> By the way, not everyone loves Hungarian notation...

Thanks both for the reply, I'll take some time to digest so to avoid
further error in the future.Thanks again.
-- 
http://mail.python.org/mailman/listinfo/python-list


open file on mac

2010-10-08 Thread tinauser
hi, sorry if it is a stupid qustio,but i cannot figure out where's the
problem.
i've a simpleModule:
class Starter:
def init(self,num):
print "hithere!"
print "the answer is ",num
import sys,os
print "path:",sys.path


try:
#f = open("/Users/lguerrasio/myfold/initfile.py",'r')
f = open("initfile.py",'r')
f.close()
print "huurray!"
except IOError:
print "The file does not exist, exiting gracefully"
print "This line will always print"


The module is located in the same folder of initfile.py
now,from terminal I import sys and add to the path the folder /Users/
lguerrasio/myfold; the I import the module and execute
simpleModule.Starter().init(48)

on mac I get an error if i do not give the full path of initfile.py
(commented out in the code above);
on windows i did not have this problem.
Am I missing anything?
-- 
http://mail.python.org/mailman/listinfo/python-list


open file on mac

2010-10-08 Thread tinauser
hallo, i'm sorry if the question is very stupid, but i cannot
understand what i'm doing wrong here.

i have this myModule.py

class Starter:
def init(self,num):
print "hithere!"
print "the answer is ",num
import sys,os
print "path:",sys.path
print "bye"

try:
##f = open("/Users/lguerrasio/_myfold/initfile.py",'r')
f = open("initfile.py",'r')
f.close()
print "huurray!"
except IOError:
print "The file does not exist, exiting gracefully"
print "This line will always print"


the module is in the same folder of initfile
from terminal, i import sys and add to the path /Users/lguerrasio/
_myfold/;
then i import the module and call
myModule.Starter().init(9)

now,the file will be opened only if i give the full path, not if i
give only the name of the file, although the folder is in the path.
what am I missing?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: open file on mac

2010-10-10 Thread tinauser
On Oct 10, 6:54 am, Lawrence D'Oliveiro  wrote:
> In message
> ,
>
> tinauser wrote:
> > now,the file will be opened only if i give the full path, not if i
> > give only the name of the file, although the folder is in the path.
> > what am I missing?
>
> The fact that sys.path is not used for that.

ok,then could you tell me if there is a way to have the folder of the
module where the "open" command is?
this module is called from C, and user could install the python code
folder (where both the module and the file are) where he wants.

thank you in advance
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: open file on mac

2010-10-10 Thread tinauser
On Oct 10, 11:44 pm, Benjamin Kaplan  wrote:
> On Sun, Oct 10, 2010 at 5:29 PM, tinauser  wrote:
> > On Oct 10, 6:54 am, Lawrence D'Oliveiro  > central.gen.new_zealand> wrote:
> >> In message
> >> ,
>
> >> tinauser wrote:
> >> > now,the file will be opened only if i give the full path, not if i
> >> > give only the name of the file, although the folder is in the path.
> >> > what am I missing?
>
> >> The fact that sys.path is not used for that.
>
> > ok,then could you tell me if there is a way to have the folder of the
> > module where the "open" command is?
> > this module is called from C, and user could install the python code
> > folder (where both the module and the file are) where he wants.
>
> open uses the current working directory, which is available through
> the os.getcwd() function in Python.
> If you want to use a path relative to the current module, you can use
> that module's __file__ attribute.
> os.path.dirname(os.path.abspath(__file__))
> will give you the absolute path to the directory with the current module in 
> it.
>
> So if you want to open the foo.txt file in the same directory as the
> module, you use
> os.path.join(os.path.dirname(__file__), 'foo.txt')
>
> > thank you in advance
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
>

thanks a lot!
-- 
http://mail.python.org/mailman/listinfo/python-list


emdding python gui in c code - OS independent

2010-10-11 Thread tinauser
hi there,
i need to embed python GUI in a c++ code. I've seen that,while on
windows running GUI is no problem, in mac i need to use pythonw
instead python.
the question is,how should i tell the program that if the OS is mac,
it should pythonw, otherwise python is fine?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: emdding python gui in c code - OS independent

2010-10-11 Thread tinauser
On Oct 11, 6:49 pm, Chris Rebert  wrote:
> On Mon, Oct 11, 2010 at 6:16 AM, tinauser  wrote:
> > hi there,
> > i need to embed python GUI in a c++ code. I've seen that,while on
> > windows running GUI is no problem, in mac i need to use pythonw
> > instead python.
> > the question is,how should i tell the program that if the OS is mac,
> > it should pythonw, otherwise python is fine?
>
> I think you have it backwards. MS Windows is where one typically needs
> to use pythonw to suppress a console window from opening when Python
> is run. *nixes (including Mac OS X) have no such problem and (I'm
> pretty sure) only have a pythonw executable for compatibility
> purposes. Just specify pythonw regardless of OS and you should be
> fine.
>
> Cheers,
> Chris
> --http://blog.rebertia.com

thanks:
how do i specify pythonw programmatically?
i tried Py_SetProgramName("pythonw");

it doesn't raise errors, but does not solve the problem on mac, i.e. i
get the error:

This program needs access to the screen.
Please run with 'pythonw', not 'python', and only when you are logged
in on the main display of your Mac.
-- 
http://mail.python.org/mailman/listinfo/python-list