[PEAK] Newbie Question

William Trenker wtrenker at shaw.ca
Sat Dec 6 18:33:26 EST 2003


[John Landahl advised:]
Have you see the scrap of a tutorial in the PeakDatabaseApplications entry of the wiki?
http://peak.telecommunity.com/DevCenter/PeakDatabaseApplications

I've tried the small tutorial "scrap" on the page you suggested and unfortunately I get a traceback:

from peak.api import binding, storage
class Worker(binding.Component):
	db = binding.Obtain(storage.ISQLConnection)
	def doWork(self):
		for row in self.db('select name, description from aTable'):
			print row.name, row.description
class Application(binding.Component):
	db     = binding.Obtain('sqlite:test.db', offerAs=[storage.ISQLConnection])
	worker = binding.Make(Worker)
	def run(self):
		self.worker.doWork()
from peak.api import config
root = config.makeRoot()
app = Application(root)
app.run()
Traceback (most recent call last):
  File "wikiSnippet.py", line 19, in ?
    app.run()
  File "wikiSnippet.py", line 14, in run
    self.worker.doWork()
  File "wikiSnippet.py", line 6, in doWork
    for row in self.db('select name, description from aTable'):
  File "/usr/local/lib/python2.3/site-packages/peak/storage/connections.py", line 224, in __call__
    cursor.execute(*args)
  File "/usr/local/lib/python2.3/site-packages/peak/storage/SQL.py", line 130, in execute
    self.setTxnState()
  File "/usr/local/lib/python2.3/site-packages/peak/storage/connections.py", line 191, in joinTxn
    return self.joinedTxn
  File "C:\cygwin\home\pje\PEAK\src/peak/binding/_once.pyx", line 112, in _once.BaseDescriptor.__get__
  File "C:\cygwin\home\pje\PEAK\src/peak/binding/_once.pyx", line 100, in _once.__get__
  File "/usr/local/lib/python2.3/site-packages/peak/storage/transactions.py", line 383, in joinedTxn
    ts.join(self)
  File "/usr/local/lib/python2.3/site-packages/peak/storage/transactions.py", line 154, in join
    raise exceptions.OutsideTransaction
peak.exceptions.OutsideTransaction


[Phillip J. Eby also advised:]
All that you're missing is that your 'myDB' object doesn't have a parent component.  It needs a root...

I tried following your instructions as shown below but I got the same traceback with this snippet as well:

from peak.api import *
class myDB(binding.Component):
	db = binding.Obtain('sqlite:test.db')
root = config.makeRoot()
x = myDB(root)
y = x.db('select name,description from atable')
Traceback (most recent call last):
  File "myDB.py", line 6, in ?
    y = x.db('select name,description from atable')
  File "/usr/local/lib/python2.3/site-packages/peak/storage/connections.py", line 224, in __call__
    cursor.execute(*args)
  File "/usr/local/lib/python2.3/site-packages/peak/storage/SQL.py", line 130, in execute
    self.setTxnState()
  File "/usr/local/lib/python2.3/site-packages/peak/storage/connections.py", line 191, in joinTxn
    return self.joinedTxn
  File "C:\cygwin\home\pje\PEAK\src/peak/binding/_once.pyx", line 112, in _once.BaseDescriptor.__get__
  File "C:\cygwin\home\pje\PEAK\src/peak/binding/_once.pyx", line 100, in _once.__get__
  File "/usr/local/lib/python2.3/site-packages/peak/storage/transactions.py", line 383, in joinedTxn
    ts.join(self)
  File "/usr/local/lib/python2.3/site-packages/peak/storage/transactions.py", line 154, in join
    raise exceptions.OutsideTransaction
peak.exceptions.OutsideTransaction

Do I need to explicitly "begin" a transaction?  I thought that cursor.execute would wrap itself automatically in a transaction.  But then maybe I'm thinking too much of the DB-API-2.0 spec.  Does PEAK's cursor.execute require an explicit transaction?  How do I do that?

For both of the above snippets I've set up an SQLite db called test.db and created an aTable table with a single record.  It works fine with n2.

I should add that I don't mind tracing through the code trying to figure out why tracebacks like the above are happening--I don't want to bother you with trivial questions.  Could I ask what debugging tools you use to work through tracebacks?  Do you just use pdb at the python command prompt, or do you use one of the graphical debugging tools?  Any recommendations?

Thanks for your help,
Bill






More information about the PEAK mailing list