Can't catch CTRL-C when SimpleXMLRPCServer running ?
Hi - I have a script which instantiates a SimpleXMLRPCServer server and which I use to simulate a 'real server' when developing client scripts which will eventually get used with the 'real server'. I would like to stop the script running in response to a CTRL-C. The script is run on windows. The script looks like this ... from SimpleXMLRPCServer import SimpleXMLRPCServer from SimulateCRRWebServices import SimulateCRRWebServices import signal import sys #Set up signal handler def signal_handler(signal, frame): print 'Closing down now' sys.exit(0) signal.signal(signal.SIGINT, signal_handler) #Set up XMLRPC Server server = SimpleXMLRPCServer(('localhost', 42001), logRequests=True) server.register_introspection_functions() server.register_multicall_functions() server.register_instance(SimulateCRRWebServices()) #Off we go ... print 'Use Control-C to exit' server.serve_forever() ... the trouble the script is unable to see my CTRL-C's ! As a reference I've written another script which *does* catch CTRL-C's ... import signal import sys def signal_handler(signal, frame): print 'You pressed Ctrl+C!' sys.exit(0) signal.signal(signal.SIGINT, signal_handler) print 'Press Ctrl+C' while True: continue ... so is there a way around this ? Be grateful for any advice. -- http://mail.python.org/mailman/listinfo/python-list
Object Models - decoupling data access - good examples ?
I'm interested in best practice approaches to : decoupling data access code from application code; and translations between database structures and domain objects. For some time I've done database access by in a particular way and while I think it's OK it's not very pythonic so I'd be interested in hearing of other approaches - even better if there are open source examples whose code might be read. I should say I'm talking relational database here and, for various reasons, ORMs are not involved. So this is what I do (in a one class project call 'bar') in order to allow objects of type foo to be fetched/inserted/updated bar/foo.py bar/bardb/bardbConnection.py bar/bardb/BusinessLogic/fooManager.py bar/bardb/BusinessObject/foo.py bar/bardb/DataAccess/fooDB.py And this is what they actually do : bar/foo.py The class as the outside world knows it bar/bardb/bardbConnection.py Manages database connection bar/bardb/BusinessLogic/fooManager.py Exposes methods used by bar/foo.py such as 'save'/'update' etc and implements necessary validation etc bar/bardb/BusinessObject/foo.py A collection of getters/setters which does any translation between types necessary on the way to/from the rdbms bar/bardb/DataAccess/fooDB.py Implements the actual SQL necessary for the relevant interactions with the database As I say this works OK for me but I'd be interested to hear of what others do. Thanks Richard. -- http://mail.python.org/mailman/listinfo/python-list
Re: [newbie] Looking for a good introduction to object oriented programming with Python
One reason you may be having difficulty is that unlike some languages (C++/Java) object-orientation is not a be all and end all in Python, in fact you could work with Python for a long time without really 'doing it' at all (well other than calling methods/properties on existing API's). Having said that here's what I would suggest ... Could do worse than this : http://www.diveintopython.net/object_oriented_framework/index.html and this http://docs.python.org/tutorial/classes.html read together. Judging by your question this is a probably a little advanced for now but you could bookmark it for the future: http://www.catonmat.net/blog/learning-python-design-patterns-through-video-lectures/ Here's the corresponding PDF to go with the video: http://assets.en.oreilly.com/1/event/45/Practical%20Python%20Patterns%20Presentation.pdf -- http://mail.python.org/mailman/listinfo/python-list
Re: Object Models - decoupling data access - good examples ?
Just out of curiosity, why do you eschew ORMs? Good question ! I'm not anti-ORM (in fact in many circs I'm quite pro-ORM) but for some time I've been working with a client who doesn't want ORMs used (they do have quite good reasons for this although probably not as good as they think). I was interested to know, given that was the case, how you might - in Python, go about structuring an app which didn't use an ORM but which did use a RDBMS fairly intensively. I take your point about having rolled my own ORM - lol - but I can assure you what's in that 'bardb' is a pretty thin layer over the SQL and nothing like the, pretty amazing, functionality of, for instance, SQLAlchemy. -- http://mail.python.org/mailman/listinfo/python-list
Re: constant sharing works differently in REPL than in script ?
Thanks for all the replies. I hadn't thought about the opportunities that exist for optimization when the whole script is there (or when compound operations are taking place) by contrast with plain old REPL ops. I liked your code Chris demoing the different ranges in different versions. I tried to write something like that myself but you did it an awful lot better ! -- http://mail.python.org/mailman/listinfo/python-list
constant sharing works differently in REPL than in script ?
Listening to 'Radio Free Python' episode 8 (http://radiofreepython.com/episodes/8/ - around about the 30 minute mark) I heard that Python pre creates some integer constants to avoid a proliferation of objects with the same value. I was interested in this and so I decided to try it out. First I did this at the prompt : c = 1 print id(1) 26906152 print id(c) 26906152 c is 1 True So that matched what I'd heard and then I did this to test the limits of it : c = 259 print id(259) 26167488 print id(c) 26167512 c is 259 False And that was reasonable too as the podcast mentioned it was only done for a small set of integers around zero. However when I wrote this script : c = 259 print id(259) print id(c) if c is 259: print %s - yes % (c) else: print %s - no % (c) I got this output : C:\data\src\Python\foopython untitled-2.py 26760884 26760884 259 - yes So what's going on here. The script seems to be sharing objects in a way the REPL isn't ? Can anyone explain please ? BTW this is all on : Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32 . -- http://mail.python.org/mailman/listinfo/python-list
Serialize my class as JSON causes __init__() got an unexpected keyword argument 'indent' ?
Hi - I've got a straightforward class I want to serialize as JSON (actually I want to a serialize a list of them but I believe that's irrelevant). I've subclassed JSONEncoder and defined my own version of the 'default' method ( based upon what I read at http://docs.python.org/library/json.html ) but when I then try to serialize the class I get the (fairly weird) error message : TypeError: __init__() got an unexpected keyword argument 'indent'. I suspect I'm doing something pretty fundamentally wrong but I don't know what - can anyone tell me what's wrong (or alternatively tell me how to log this as a bug ;-) Here's my test case : import json class SuperPeople(object): pass class People(SuperPeople, json.JSONEncoder): def __init__(self, name, age): self.__name = name self.__age = age def default(self, obj): if isinstance(obj, People): return [obj.__name, obj.__age] else: return json.JSONEncoder.default(self, obj) def main(): lstPeople = [] lstPeople.append(People(Mary, 50)) lstPeople.append(People(Joe, 40)) lstPeople.append(People(Sue, 30)) print json.dumps(lstPeople, cls=People) if __name__ == __main__: main() ... and this is what the stacktrace looks like Traceback (most recent call last): File testJSON.py, line 24, in module main() File testJSON.py, line 20, in main json.dumps(lstPeople, cls=People) File C:\bin\installed\Python2.6\lib\json\__init__.py, line 237, in dumps **kw).encode(obj) TypeError: __init__() got an unexpected keyword argument 'indent' ... I'm running Python 2.6 on Win32. All suggestions welcomed . Richard. -- http://mail.python.org/mailman/listinfo/python-list
Re: Serialize my class as JSON causes __init__() got an unexpected keyword argument 'indent' ?
On Dec 18, 11:30 pm, Peter Otten __pete...@web.de wrote: shearichard wrote: Hi - I've got a straightforward class I want to serialize as JSON (actually I want to a serialize a list of them but I believe that's irrelevant). I've subclassed JSONEncoder and defined my own version of the 'default' method ( based upon what I read at http://docs.python.org/library/json.html) but when I then try to serialize the class I get the (fairly weird) error message : TypeError: __init__() got an unexpected keyword argument 'indent'. I suspect I'm doing something pretty fundamentally wrong but I don't know what - can anyone tell me what's wrong (or alternatively tell me how to log this as a bug ;-) Here's my test case : import json class SuperPeople(object): pass class People(SuperPeople, json.JSONEncoder): def __init__(self, name, age): self.__name = name self.__age = age def default(self, obj): if isinstance(obj, People): return [obj.__name, obj.__age] else: return json.JSONEncoder.default(self, obj) def main(): lstPeople = [] lstPeople.append(People(Mary, 50)) lstPeople.append(People(Joe, 40)) lstPeople.append(People(Sue, 30)) print json.dumps(lstPeople, cls=People) if __name__ == __main__: main() ... and this is what the stacktrace looks like Traceback (most recent call last): File testJSON.py, line 24, in module main() File testJSON.py, line 20, in main json.dumps(lstPeople, cls=People) File C:\bin\installed\Python2.6\lib\json\__init__.py, line 237, in dumps **kw).encode(obj) TypeError: __init__() got an unexpected keyword argument 'indent' ... I'm running Python 2.6 on Win32. All suggestions welcomed . You pass the encoder *class* to json.dumps(), so the function has to instantiate it. It does that with the arguments that an encoder class must accept. There's no way for it to expect that an encoder requires a name and an age. The solution is to separate the encoder and the class that shall be encoded. Here's one way: import json class People(object): def __init__(self, name, age): self.__name = name self.__age = age def get_json_state(self): return [self.__name, self.__age] class PeopleEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, People): return obj.get_json_state() else: return json.JSONEncoder.default(self, obj) def main(): lstPeople = [] lstPeople.append(People(Mary, 50)) lstPeople.append(People(Joe, 40)) lstPeople.append(People(Sue, 30)) print json.dumps(lstPeople, cls=PeopleEncoder) if __name__ == __main__: main() Brilliant - thank you very much. Now that you've explained it I can see why the documentation is written the way it is ! Before I saw your example I thought the documentation was a bit strange but I can see now what it was trying to tell me ! Your help is much appreciated. Richard. -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie question about importing modules.
On Dec 17, 4:42 pm, cronoklee cronok...@gmail.com wrote: Hi I'm starting my first python project but I'm having trouble getting off the ground. I've read all I can find about relative and absolute import paths but it's just not making sense to me... There seems to be around ten different ways to import a script. I need my project to be portable so I can copy the whole folder to run on any PC that has python installed. Is it always possible to simply include modules in the project directory and reference them without installing into the main python directory? I've managed this with small classes through trial and error but when I try it with anything larger (like PIL module for example) I get errors. Do I need to actually install anything or is it enough just to include the relevant scripts? All the modules I've found come with tonnes of files and subdirectories. Do I need all these files or should I just choose the scripts/folders I need? Thanks, cronoklee You may find this useful as an overview of importing ... http://effbot.org/zone/import-confusion.htm ... also this ... http://diveintopython.org/object_oriented_framework/importing_modules.html I may be stating the obvious but here's an example of using the Image object offered by PIL ... from PIL import Image ... as documented here ... http://www.pythonware.com/library/pil/handbook/image.htm Regarding bundling PIL with an app I'd second what Tim Roberts has to say regarding py2Exe regards Richard. -- http://mail.python.org/mailman/listinfo/python-list
pudb on windows (dependent upon unix only termios ?)
Hi - I was just trying to install the Python debugger pudb (http:// pypi.python.org/pypi/pudb) and easy install fell over saying it couldn't find a module termios. It turns out termios is only for Unix (http://docs.python.org/library/ termios.html). Does anyone know of way around this ? Is there an equivalent to termios which would run on windows and which could be hacked into pudb ? Alternatively suggestions for a pudb workalike ? (I like the console/ keyboard centric aspect of pudb). Thanks Richard. -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP8 compliance and exception messages ?
On Dec 6, 6:21 pm, Ben Finney ben+pyt...@benfinney.id.au wrote: shearichard shearich...@gmail.com writes: Hi - PEP8 says lines should not exceed 79 characters in length (http://www.python.org/dev/peps/pep-0008/). So if you've got some code that looks like this : raise fooMod.fooException(Some message which is quite long) PEP 8 also says those names are poorly chosen. Better: raise foomod.FooException(Some message which is quite long) raise fooMod.fooException(\ Some message \ which is quite long) Take advantage of the parsing of string literals and parenthesis: raise foomod.FooException( Some message which is quite long) and for the sake of my eyes, avoid camelCase. OK you got me ! Thanks for pointing this out, I will take a look at the relevant section -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP8 compliance and exception messages ?
On Dec 7, 9:17 am, Andreas Waldenburger use...@geekmail.invalid wrote: On Mon, 6 Dec 2010 00:22:49 -0500 Andreas Waldenburger use...@geekmail.invalid wrote: On Sun, 5 Dec 2010 19:52:54 -0800 Chris Rebert c...@rebertia.com wrote: On Sun, Dec 5, 2010 at 7:40 PM, shearichard shearich...@gmail.com wrote: Hi - PEP8 says lines should not exceed 79 characters in length (http://www.python.org/dev/peps/pep-0008/). So if you've got some code that looks like this : raise fooMod.fooException(Some message which is quite long) ... and assuming a certain amount of indenting you're going to break that guideline. [etc.] [...] Alternatively, you could disregard PEP 8 on this point on the grounds that the 79/80 characters per line limit is outdated. Maybe, but it's not outmoded. As a more useful (I hope) reply, my opinion in this case is to just make the line a little longer. Even if you can't read it all at once, it is pretty obvious what comes next: The rest of the error message. There is no additional functionality hidden there, and you don't need to see it all at once to grasp the meaning of the code. /W -- To reach me via email, replace INVALID with the country code of my home country. But if you spam me, I'll be one sour Kraut. Thanks to everyone for their helpful replies. Thanks for the pointers towards implicit (or explicit) string concatenation - just what was needed. I appreciate everyone has different opinions by I'm happy to try to stick with 79 character lines for the meantime - largely for the 'may have a wide screen but like to have lots of files open in slim windows' reason. regards Richard. -- http://mail.python.org/mailman/listinfo/python-list
Re: find memory leaks in running program
On Dec 8, 5:51 am, Marco Hornung pythonma...@gmail.com wrote: Hey, -- questions -- 1. What are the best tools to analyze pythons memory stack, while it is running? 2. Is there a possibility to analyze the memory stack of a program with external programs? (without to change the source code - I am only interested in the object size) 3. Can I sort of break into the memory to see what objects consume how much memory? -- my scenario -- I have to debug a multithreaded server, which is written in the twisted-framework. One of the processes has some sort of memory leak - After one of our jobs is finished the main process has still over 59% of the entire memory allocated. I will probably have to recreate our scenario and equip our server with some memory sensors - but it takes 12h to reproduce the scenario and I will have to change the source code(at least I do not know of other options). Therefore I am looking for quicker possibilities to look into what causes our memory leak. Regards, Marco I haven't used this myself but sometime ago I bookmarked yappi which may go some way towards helping you ... http://code.google.com/p/yappi/ http://code.google.com/p/yappi/wiki/apiyappi ... also sometime ago I was at presentation (Pycon NZ 2009) where the twisted.manhole functionality was used to hook a remote console up to a running twisted task and examine the interior of the target task. Can't remember the details but someone else here may be able to help. This touches upon the idea ... http://stackoverflow.com/questions/1721699/is-there-any-remote-console-for-twisted-server ... and in turn references Heapy which I'd forgotten about but may also be useful in your circs ... http://guppy-pe.sourceforge.net/ regards Richard. -- http://mail.python.org/mailman/listinfo/python-list
PEP8 compliance and exception messages ?
Hi - PEP8 says lines should not exceed 79 characters in length ( http://www.python.org/dev/peps/pep-0008/ ). So if you've got some code that looks like this : raise fooMod.fooException(Some message which is quite long) ... and assuming a certain amount of indenting you're going to break that guideline. However there's a way around that ! You can do this ... raise fooMod.fooException(\ Some message \ which is quite long) ... but the trouble is when that Exception is raised the message is displayed as : Some message which is quite long I'm aware that a foolish consistency is the hobgoblin of something or the other so maybe I should just let the PEP8 verifier complain but otherwise does anyone have any ideas for how to get around this ? Thanks richard -- http://mail.python.org/mailman/listinfo/python-list
Pypi (Cheeseshop) Score - derivation of ?
Hi - Anyone know how the score offered by Pypi is derived ? For instance in ... http://pypi.python.org/pypi?%3Aaction=searchterm=spamsubmit=search ... 'bud.nospam 1.0.1' has a score of 9 but 'pydspam 1.1.9' has a score of 7. Where are those numbers from and what do they mean ? Thanks R. -- http://mail.python.org/mailman/listinfo/python-list
Re: Pypi (Cheeseshop) Score - derivation of ?
On Nov 25, 3:54 pm, Ben Finney ben+pyt...@benfinney.id.au wrote: shearichard shearich...@gmail.com writes: Hi - Anyone know how the score offered by Pypi is derived ? Specifically, the score offered in response to a search query. For instance in ... http://pypi.python.org/pypi?%3Aaction=searchterm=spamsubmit=search ... 'bud.nospam 1.0.1' has a score of 9 but 'pydspam 1.1.9' has a score of 7. Where are those numbers from and what do they mean ? They are the relevance of the result to that particular search query. I don't know the scale of the score or how it's derived, but that's the intended meaning AFAIK. I think a better term than “score” could be chosen; perhaps you could submit a bug report against PyPI. OK that makes sense. I thought it was some comment on how 'good' the package in question was ! I will do as you say and submit a bug report to get the literal altered to something a little more self-explanatory. -- http://mail.python.org/mailman/listinfo/python-list
PIL - despeckle image ?
Hi - I have some images which I would like to remove specks from using the PIL. I would like to be able to say that if a pixel is in a blob of less than n contiguous pixels then all pixels in that blob should be removed. The images are 8 bit images with only 0 and 255 values. I can think of quite crude ways of doing this but I'm sure there are some smart ways of doing it - would anyone be able to point me in the right direction ? Lastly in case it matters the reason I'm doing this is that the images are scanned images of sheets of papers on which sample signatures have been placed. Typically the signatures are much smaller than the sheets of paper so I would like to 'autocrop' the images to remove the white space around the signature. At some point in the process the images having picked up small islands of pixels (dust on scanner or paper ?) which are nowhere near the signature but which are causing my autocrop efforts to go awry. Thanks Richard. -- http://mail.python.org/mailman/listinfo/python-list
MySQLdb - parameterised SQL - how to see resulting SQL ?
Hi - I've got SQL that looks like this ... cursor = self.MySQLDb_conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) sqlQuery = SELECT * FROM T1 WHERE C1 = %s and C2 = %s sql = cursor.execute(sqlQuery,(strURLAlias,strSessionID)) rows = cursor.fetchall() cursor.close ... i would be interested in seeing what the actual SQL used by the .execute looks like after the replacements have been done. Is there a way of doing this ? thanks richard. -- http://mail.python.org/mailman/listinfo/python-list
MySQLdb begin() -
Hi - Feeling a bit weird about this but I cannot find the 'begin' method on a connection object of MySQLdb. Can anyone explain why ? I'm using version 1.2.0 which is pretty recent and I've read that 'begin' should be a method of connection but it's not there ! Feeling pretty puzzled ! Below are the details of what I see ... import MySQLdb c= MySQLdb.Connect(host='localhost', user='a',passwd='b', db='c',compress=1) c.begin() Traceback (most recent call last): File stdin, line 1, in ? AttributeError: 'Connection' object has no attribute 'begin' o=MySQLdb o.__version__ '1.2.0' o.__revision__ '1.37' o.apilevel '2.0' Be interested in any comments. regards Richard. -- http://mail.python.org/mailman/listinfo/python-list
Re: MySQLdb begin() -
What do i expect the begin method to do ? Explicitly start a transaction (and therefore suppress autocommits) in an environment where autocommit is on. No i haven't read the pep, thanks for that. -- http://mail.python.org/mailman/listinfo/python-list
Re: MySQLdb begin() -
Thanks for your advice. In fact subsquent to posting I started using ... conn.autocommit = False ... as a synonm for ... conn.begin() ... and as you say that does the job. (Sorry i should have said it's not practicable to turn off autocommit always [or rather it may be but I'm not about to shake the boat to that extent just now ;-] As to what I was reading yes it's here ... http://www.devshed.com/index2.php?option=contenttask=viewid=210pop=1page=0hide_js=1 ... or more succinctly ... http://tinyurl.com/s6yd2 ... page down to ... connection.begin() - start a transaction ... that article refers to version 0.92 of MySQLdb so clearly it was either wrong then or for some weird reason the method has been dropped. thanks again. richard. -- http://mail.python.org/mailman/listinfo/python-list
Convert (sorted) list of dics to nested list ?
Hi - I want to take something like ... lstIn = [] lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 10, 'LEA_AUTOID': 1000}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2000}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2001}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 11, 'LEA_AUTOID': 2003}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3000}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3001}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3002}) lstIn.append({'COM_AUTOID': 1, 'PRG_AUTOID': 12, 'LEA_AUTOID': 3003}) lstIn.append({'COM_AUTOID': 2, 'PRG_AUTOID': 110, 'LEA_AUTOID': 4000}) ... and produce something like ... sampleOut = [[1,[10,1000]],[1,[11,[2000,2001,2003]]],[1,[12,[3000,3001,3002,3003]]],[2,[110,4000]] Well I've now been around the block a few times with this one and I'm still frowning !! In the process my code has become uglier and uglier - I'm sure there must be quite an elegant way of dealing with it - could anyone give me a push in the right direction ? Just to provide some motivation here - I should just say that this is cut down test case - the real problem involves creating a Javascript structure which in turn defines a three level menu. The resulting JS will be something like this, I think you can see how the nested loops get into it.: var MENU_ITEMS = [ { pos:'relative', leveloff:[b,a], itemoff:[d,c], size:[e,f], ... }, { ...Item 1... }, { ...Item 2... , sub:[ { ...level format... }, { ...Item 1... }, { ...Item 2... }, { ...Item 3... , sub:[ { ...level format... }, { ...Item 1... }, { ...Item 2... }, { ...Item 3... }, { ...Item 4... } ] }, { ...Item 4... }, ] }, { ...Item 3... } ]; Interested to hear of any smart/elegant ideas. thanks Richard. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to Mount/Unmount Drives on Windows?
This looks like it might help you ... http://support.microsoft.com/?kbid=311272 ... although the blurb does say DevCon is not redistributable. It is provided for use as a debugging and development tool. There is an article about it at ... http://tinyurl.com/4kb8m regards richard. -- http://mail.python.org/mailman/listinfo/python-list
MySQLDB - parameterised SQL - TypeError: not all arguments converted during string formatting
Hi - I have written some python to insert a row into a table using MySQLDB. I have never before written SQL/Python using embedded parameters in the SQL and I'm having some difficulties. Could someone point me in the right direction please ? The python looks like this : import MySQLdb import MySQLdb.cursors conn = MySQLdb.Connect(host='localhost', user='abc,passwd='def', db='ghi',compress=1) cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) sqlQuery = INSERT INTO EVA_COMPANY (COM_ADDRESS,COM_AUTOID,COM_COMPANY_ADDRESS,COM_STRUCT_OWNER_CODE) VALUES (?,?,?,?) print sqlQuery sql = cursor.execute(sqlQuery,(test,NULL,Tester1017,5)) cursor.close() conn.commit() ... and the table looks like this ... CREATE TABLE `eva_company` ( `COM_AUTOID` int(9) unsigned NOT NULL auto_increment, `COM_COMPANY_NAME` varchar(128) NOT NULL, `COM_TRADING_NAME` varchar(128) default NULL, `COM_ADDRESS` varchar(256) NOT NULL, `COM_POSTAL_ADDRESS` varchar(256) default NULL, `COM_CONTACT_NAME` varchar(56) default NULL, `COM_CONTACT_POSITION` varchar(56) default NULL, `COM_CONTACT_TELEPHONE` varchar(16) default NULL, `COM_CONTACT_FAX` varchar(16) default NULL, `COM_CONTACT_EMAIL` varchar(256) default NULL, `COM_STRUCT_OWNER_CODE` int(9) unsigned default NULL, `COM_INDUSTRY_CODE` varchar(16) default NULL, `COM_INDUSTRY_DESC` varchar(56) default NULL, `COM_NUMBER_OF_SITES` int(9) default NULL, `COM_NATURE_OF_RELATIONSHIP` varchar(128) default NULL, PRIMARY KEY (`COM_AUTOID`), KEY `IDX_COM1` (`COM_STRUCT_OWNER_CODE`), KEY `IDX_COM0` (`COM_COMPANY_NAME`), CONSTRAINT `FK_COS_COM0` FOREIGN KEY (`COM_STRUCT_OWNER_CODE`) REFERENCES `eva_code_comp_struct_ownership` (`COS_AUTOID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC; ... but when I try to execute the python I get an error ... Traceback (most recent call last): File sheadbtest1.py, line 8, in ? sql = cursor.execute(sqlQuery,(test,NULL,Tester1017,5)) File C:\bin\installed\Python2.4.1\Lib\site-packages\MySQLdb\cursors.py, line 132, in execute self.errorhandler(self, TypeError, m) File C:\bin\installed\Python2.4.1\Lib\site-packages\MySQLdb\connections.py, line 33, in defaulterrorhandler raise errorclass, errorvalue TypeError: not all arguments converted during string formatting ... as I say if anyone could provide me with some tips I'd appreciate it. thanks richard shea. -- http://mail.python.org/mailman/listinfo/python-list
Re: MySQLDB - parameterised SQL - TypeError: not all arguments converted during string formatting
Hi Dennis - Thanks for your help with this Dennis Lee Bieber wrote: On 19 Feb 2006 16:26:15 -0800, [EMAIL PROTECTED] declaimed the following in comp.lang.python: Hi - I have written some python to insert a row into a table using MySQLDB. I have never before written SQL/Python using embedded parameters in the SQL and I'm having some difficulties. Could someone point me in the right direction please ? sqlQuery = INSERT INTO EVA_COMPANY (COM_ADDRESS,COM_AUTOID,COM_COMPANY_ADDRESS,COM_STRUCT_OWNER_CODE) VALUES (?,?,?,?) One: I believe MySQLdb uses %s placeholders, not ? import MySQLdb MySQLdb.paramstyle 'format' From the DB-API PEP paramstyle String constant stating the type of parameter marker formatting expected by the interface. Possible values are [2]: 'qmark' Question mark style, e.g. '...WHERE name=?' 'numeric' Numeric, positional style, e.g. '...WHERE name=:1' 'named' Named style, e.g. '...WHERE name=:name' 'format'ANSI C printf format codes, e.g. '...WHERE name=%s' 'pyformat' Python extended format codes, e.g. '...WHERE name=%(name)s' Great stuff. I had seen some example code which used question marks and rather too slavishly used it as a model. As you say MySQLDb does indeed use 'format' and so %s are the way to go ... print sqlQuery sql = cursor.execute(sqlQuery,(test,NULL,Tester1017,5)) Two: I think it is smart enough to translate a Python None to a MySQL NULL -- which is different from a four-character string containing NULL (or even a zero-character string ). ... just to confirm that yes using a Python None works fine if you wish to put a Null into a column ... CREATE TABLE `eva_company` ( `COM_AUTOID` int(9) unsigned NOT NULL auto_increment, As an auto-increment field, the recommendation would be to not even mention the field in the INSERT SQL -- that also takes care of the problem of specifying NULL to a non-null field! ... yes it didn't start off that way but I was busy trying everthing I could think of to try to make it happier. thanks for your help it's all working now. regards richard. -- == [EMAIL PROTECTED] | Wulfraed Dennis Lee Bieber KD6MOG [EMAIL PROTECTED] | Bestiaria Support Staff == Home Page: http://www.dm.net/~wulfraed/ Overflow Page: http://wlfraed.home.netcom.com/ -- http://mail.python.org/mailman/listinfo/python-list