Re: [Tutor] insert queries into related tables referencing foreign keys using python
Lie Ryan gmail.com> writes: > In python-sqlite, the rowid of the > last insert operation can be queried using cursor.lastrowid. Therefore, > you can query the lastrowid, right after the insert, to find the primary > key of the value you had just inserted. So, in code: > > ... > cur = conn.execute('INSERT ... ') > pk = cur.lastrowid > ... > > or even: > > ... > pk = conn.execute('INSERT ... ').lastrowid > ... > > Be careful that in multithreaded program, each thread should have their > own cursors, or otherwise another thread could possibly do another > insert before you can query the lastrowid. > okay, this touches on something that had been worrying me a bit... whether another insert could hit before I queried for the lastrowid, regardless of how I did it. So you're saying that as long as I have the one cursor open, the lastrowid it gets will be the lastrowid from its operations, regardless of other commits or transactions that may have happened in the meantime? > > In general, despite the superficial similarities, most database engine > wrappers have their own ways of doing stuffs. Generally, you need a > full-blown ORM to smooth out the differences. > So... what would be considered a 'full-blown' ORM? SQLobject or SQLalchemy... or something else? Thanks, Monte ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] insert queries into related tables referencing foreign keys using python
On Sat, Dec 24, 2011 at 9:57 AM, Monte Milanuk wrote: > Lie Ryan gmail.com> writes: > >> In python-sqlite, the rowid of the >> last insert operation can be queried using cursor.lastrowid. Therefore, >> you can query the lastrowid, right after the insert, to find the primary >> key of the value you had just inserted. So, in code: >> >> ... >> cur = conn.execute('INSERT ... ') >> pk = cur.lastrowid >> ... >> >> or even: >> >> ... >> pk = conn.execute('INSERT ... ').lastrowid >> ... >> >> Be careful that in multithreaded program, each thread should have their >> own cursors, or otherwise another thread could possibly do another >> insert before you can query the lastrowid. >> > > okay, this touches on something that had been worrying me a bit... whether > another insert could hit before I queried for the lastrowid, regardless of > how I > did it. So you're saying that as long as I have the one cursor open, the > lastrowid it gets will be the lastrowid from its operations, regardless of > other > commits or transactions that may have happened in the meantime? > >> >> In general, despite the superficial similarities, most database engine >> wrappers have their own ways of doing stuffs. Generally, you need a >> full-blown ORM to smooth out the differences. >> > > So... what would be considered a 'full-blown' ORM? SQLobject or SQLalchemy... > or something else? > > Thanks, > > Monte > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Django has an ORM and it works with sqlite3, mysql and I think postgress Although Django is a full framework for writing web apps the various modules can be used together or separately. http://www.djangobook.com/en/2.0/chapter05/ talks about models and the ORM -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A few Python Mysteries [Reset]
My guess is that some months ago I looked at .idlerc on another "mission" to figure what ails my python install, and just more or less backed up the recent-files.lst. However, the important point here is, I think, how to change the permissions for the .idlerc folder. They vary by how I might login. On 12/23/2011 11:47 PM, Lie Ryan wrote: On 12/23/2011 03:20 PM, Wayne Watson wrote: Hi, I found it, but not in a place I would expect. It's under my username, Wayne. It is a folder and has three files: breakpoints.lst recent-files.lst ZZrecent-files.lst The last one has the odd ZZ, but is empty. breakpoints.lst is empty too. That certainly is curious, have you tried renaming ZZrecent-files.lst to recent-file.lst? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet CE 1955 October 20 07:53:32.6 UT -- "The Date" The mystery unfolds. Web Page: ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Which libraries for Python 2.5.2
I'm trying to restore Python 2.5.2 on an old PC for a particular application that uses it from 4-5 years ago. According to the latest manual on it, the following should be installed. python-2.5.2.msi PIL-1.1.6.win32-py2.5.exe numpy-1.1.0-win32-superpack-python2.5.exe matplotlib-0.98.1.win32-py2.5.exe When I install them, and try to run the app program, Sentinel.py, some part of matplotlib complains (error msgs) and the program quits. The program begins with: from Tkinter import * from numpy import * import Image import ImageChops import ImageTk import time import binascii import tkMessageBox import tkSimpleDialog from pylab import plot, xlabel, ylabel, title, show, xticks, bar I tried numpy-1.2.0 and matplotlib-0.98.3 and had the same difficulty. What are wiser choices? -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet CE 1955 October 20 07:53:32.6 UT -- "The Date" The mystery unfolds. Web Page: ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A few Python Mysteries [Reset]
Permissions as follows: SYSTEM: All. From Full control to write Account Unknown(S-1-5-21...): read, exec, list folder contents, Read Wayne: (normal use) All. From Full control to write Admin: All. From Full control to write WMPNetwork: Read -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet CE 1955 October 20 07:53:32.6 UT -- "The Date" The mystery unfolds. Web Page: ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Which libraries for Python 2.5.2
On Sat, Dec 24, 2011 at 6:37 PM, Wayne Watson wrote: > I'm trying to restore Python 2.5.2 on an old PC for a particular application > that uses it from 4-5 years ago. > According to the latest manual on it, the following should be installed. > > python-2.5.2.msi > PIL-1.1.6.win32-py2.5.exe > numpy-1.1.0-win32-superpack-python2.5.exe > matplotlib-0.98.1.win32-py2.5.exe > > When I install them, and try to run the app program, Sentinel.py, some part > of matplotlib complains (error msgs) and the program quits. > If we are to give any kind of useful advice at all, we're going to need to see those error messages. > The program begins with: > from Tkinter import * > from numpy import * > import Image > import ImageChops > import ImageTk > import time > import binascii > import tkMessageBox > import tkSimpleDialog > from pylab import plot, xlabel, ylabel, title, show, xticks, bar > > I tried numpy-1.2.0 and matplotlib-0.98.3 and had the same difficulty. What > are wiser choices? > This question is based on the assumption that version mismatch is the cause of your problems. Even though that might be correct, it is not an assumption you can safely make. In general, when asking questions here (and anywhere really, imho) you should try to provide as much factual information and as little conjecture as you can. Hugo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A few Python Mysteries [Reset]
Yikes. I gave the permissions for .idlerc above. The problem is with recent-files.py. IOError: [Errno 13] Permission denied: 'C:\\Users\\Wayne\\.idlerc\\recent-files.lst' These are for it. Same as before except for Account Unknown, which had "list folder contents". Permissions as follows: SYSTEM: All. From Full control to write Account Unknown(S-1-5-21...): read&exec, Read Wayne: (normal use) All. From Full control to write Admin: All. From Full control to write WMPNetwork: Read -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet CE 1955 October 20 07:53:32.6 UT -- "The Date" The mystery unfolds. Web Page: ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A few Python Mysteries [Reset]
On 24/12/11 18:58, Wayne Watson wrote: Yikes. I gave the permissions for .idlerc above. The problem is with recent-files.py. IOError: [Errno 13] Permission denied: 'C:\\Users\\Wayne\\.idlerc\\recent-files.lst' Can you open it in Notepad from the same command prompt? ie. is it just idle that can't open it, or is it any program? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A few Python Mysteries [Reset]
On 12/25/2011 06:24 AM, Alan Gauld wrote: On 24/12/11 18:58, Wayne Watson wrote: Yikes. I gave the permissions for .idlerc above. The problem is with recent-files.py. IOError: [Errno 13] Permission denied: 'C:\\Users\\Wayne\\.idlerc\\recent-files.lst' Can you open it in Notepad from the same command prompt? ie. is it just idle that can't open it, or is it any program? also, try deleting the whole folder (or just in case, move the folder somewhere else), IDLE should create a new folder and config files, hopefully with the correct permission. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] question about the build-in function reversed in Python2.5
The build-in function reversed() in Python2.5 returns a iterator. But I don't know how to use the iterator. Please give me a simple example about how to use bulid-in function reversed() to reverse a list. Thank you in advance. daedae11___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] insert queries into related tables referencing foreign keys using python
On 12/25/2011 01:57 AM, Monte Milanuk wrote: Lie Ryan gmail.com> writes: Be careful that in multithreaded program, each thread should have their own cursors, or otherwise another thread could possibly do another insert before you can query the lastrowid. okay, this touches on something that had been worrying me a bit... whether another insert could hit before I queried for the lastrowid, regardless of how I did it. So you're saying that as long as I have the one cursor open, the lastrowid it gets will be the lastrowid from its operations, regardless of other commits or transactions that may have happened in the meantime? Querying .lastrowid is pretty much safe as long as you don't use a single cursor from multiple threads. The .lastrowid attribute belongs to a cursor, so write operations from one cursor would not affect the .lastrowid of other cursors. However, note that multiple cursors created from a single connection will be able to see each other's changes immediately (as opposed to when commited). This might or might not always be desirable. In sqlite, it is more common to create one **connection** for each thread. Creating one connection for each thread prevents concurrency problems since each thread will not see uncommitted data from another thread. However, the recommended scenario is to avoid multithreading at all. sqlite developers have a strong opinion against multithreading (http://www.sqlite.org/faq.html#q6), even though they claimed that sqlite is the *embedded* SQL engine with the most concurrency (and it does very well in multithreaded scenarios). It is common pattern in sqlite-backed applications to have a single thread doing all the writes. In general, despite the superficial similarities, most database engine wrappers have their own ways of doing stuffs. Generally, you need a full-blown ORM to smooth out the differences. So... what would be considered a 'full-blown' ORM? SQLobject or SQLalchemy... or something else? Most database engine thin-wrappers conforms to the Python Database API Specification (PEP249), including sqlite3; despite that these wrappers all conforms to a common API, you still need to be familiar with each database engine to do a lot of common stuffs and -- except on trivial cases -- code written for one PEP249-conforming database engine generally cannot be ported to another PEP249-conforming database engine without modification. Almost all ORM that supports multiple DB engine backends should be able to abstract the differences. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about the build-in function reversed in Python2.5
On 12/25/2011 12:02 AM, daedae11 wrote: The build-in function reversed() in Python2.5 returns a iterator. But I don't know how to use the iterator. Please give me a simple example about how to use bulid-in function reversed() to reverse a list. >>> [x for x in reversed([1,2,3])] [3, 2, 1] >>> list(reversed([1,2,3])) [3, 2, 1] From the docs (it's all here though takes a bit of digging): iterator An object representing a stream of data. Repeated calls to the iterator's next() method return successive items in the stream. When no more data are available a StopIteration exception is raised instead. At this point, the iterator object is exhausted and any further calls to its next() method just raise StopIteration again. Iterators are required to have an __iter__() method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted. One notable exception is code which attempts multiple iteration passes. A container object (such as a list ) produces a fresh new iterator each time you pass it to the iter() function or use it in a for loop. Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container. More information can be found in /Iterator Types/ . iterable A container object capable of returning its members one at a time. Examples of iterables include all sequence types (such as list , str , and tuple ) and some non-sequence types like dict and file and objects of any classes you define with an __iter__() or __getitem__() method. Iterables can be used in a for loop and in many other places where a sequence is needed (zip() , map() , ...). When an iterable object is passed as an argument to the built-in function iter() , it returns an iterator for the object. This iterator is good for one pass over the set of values. When using iterables, it is usually not necessary to call iter() or deal with iterator objects yourself. The for statement does that automatically for you, creating a temporary unnamed variable to hold the iterator for the duration of the loop. See also /iterator/ <#term-iterator>, /sequence/ <#term-sequence>, and /generator/ <#term-generator>. 7.3. The for <#for> statement The for <#for> statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object: *for_stmt* ::= "for"target_list"in"expression_list ":"suite <#grammar-token-suite> ["else" ":"suite <#grammar-token-suite>] The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the expression_list. The suite is then executed once for each item provided by the iterator, in the order of ascending indices. Each item in turn is assigned to the target list using the standard rules for assignments, and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty), the suite in the else <#else> clause, if present, is executed, and the loop terminates. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor