[PEAK] Newbie Question
William Trenker
wtrenker at shaw.ca
Fri Dec 5 09:00:00 EST 2003
I'm trying to wrap my brain around PEAK. I've studied the tutorial, browsed around in the API docs, and read quite a few of the messages in the mailing-list archive. (I've worked with Python for about 5 years now and have developed applications and products in Zope. Recently I've been tinkering with Zope X3 - Milestone 4). I've got PEAK downloaded and the unit tests running just fine here on Linux. Now I'm trying to make some headway through the many PEAK modules by studying the code.
I've got a newbie question. It's pretty silly, I'm sure, but it reflects one place where I'm stuck. I have a simple SQLite database that I'm trying to access using PEAK. The database works fine with the PEAK n2 command as shown here:
~/python/peak# peak n2 sqlite:foo.bar
1> select * from sqlite_master;
type name tbl_name rootpage sql
-------------------- -------------------- -------------------- -------------------- --------------------
table a a 3 create table a (b va
(1 rows)
1> \exit
But I can't grok how to get it to work in Python. Here's a small example:
Python 2.3.2 (#1, Oct 27 2003, 10:19:56)
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from peak.api import *
>>> db=binding.bindTo('sqlite:foo.bar')
>>> type(db)
<class 'peak.binding.components.Obtain'>
>>> db
binding.Obtain(Base(body='foo.bar',scheme='sqlite'))
Here's the silly part, I don't know how to figure out what method to use on the db object to feed it a simple SQL statmement. The advice given in this list to other newbies is to read the interfaces.py file for the object, but what object? The db variable is bound to a peak.binding.components.Obtain for which the interfaces file isn't going to document a method for firing off some SQL. But somewhere the Obtain object is bound internally to some sort of database object that probably does have appropriate methods documented in an interfaces file. In other words, I think where I'm stuck is to know how to use PEAK to introspect the db object and work my way down to the actual underlying database class so I can figure out which module it comes from. Then I'll know where to look in the source files for documentation.
When I noticed that the type of my db object was actually a class, I thought maybe I can only use binding.bindTo in a subclass of a binding.Component, so I tried the following (sorry for the long traceback):
Python 2.3.2 (#1, Oct 27 2003, 10:19:56)
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from peak.api import *
>>> class myDB(binding.Component):
... db = binding.bindTo('sqlite:foo.bar')
...
>>> x=myDB()
>>> x
<__main__.myDB object at 0x4044d52c>
>>> x.db
Traceback (most recent call last):
File "<stdin>", line 1, in ?
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/binding/components.py", line 442, in computeValue
return self.targetName.findComponent(obj, self.default)
File "/usr/local/lib/python2.3/site-packages/peak/naming/names.py", line 198, in findComponent
return lookup(
File "/usr/local/lib/python2.3/site-packages/peak/naming/api.py", line 53, in lookup
return InitialContext(parent, **options).lookup(name,default)
File "/usr/local/lib/python2.3/site-packages/peak/naming/api.py", line 34, in InitialContext
return spi.getInitialContext(parent, componentName, **options)
File "/usr/local/lib/python2.3/site-packages/peak/naming/spi.py", line 61, in getInitialContext
factory = importObject( INIT_CTX_FACTORY(parentComponent) )
File "/usr/local/lib/python2.3/site-packages/peak/api/__init__.py", line 221, in __call__
return getProperty(forObj, self, default)
File "/usr/local/lib/python2.3/site-packages/peak/config/api_impl.py", line 116, in getProperty
return adapt(
File "/usr/local/lib/python2.3/site-packages/peak/config/interfaces.py", line 102, in propertyNotFound
raise exceptions.InvalidRoot(
peak.exceptions.InvalidRoot: Root component <__main__.myDB object at 0x4044d52c> does not implement 'IConfigurationRoot' (was looking up peak.naming.initialContextFactory for <__main__.myDB object at 0x4044d52c>)
Obviously I'm way off-track here. Can someone get me back into reality?
Thanks for any help,
Bill
More information about the PEAK
mailing list