Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Zope: DB

Question Concerning Z SQL Method and Class Bindings

 

 

Zope db RSS feed   Index | Next | Previous | View Threaded


mark at phillipsmarketing

May 24, 2010, 9:33 PM

Post #1 of 5 (3215 views)
Permalink
Question Concerning Z SQL Method and Class Bindings

I am reading the zope 2 book (relational database section) and it talks
about binding classes by putting them in the Extensions directory and then
associating them with a Z SQL Method. That seems to be rather straight
forward, but where can I find some examples of accessing the values returned
from the Z SQL Method in Python. For example,

Z SQL Method
select * from offense where gameID=<some input value from a form>

This returns ~10 rows. Each row has columns playerID, PB, BB, SacBunt, HBP
as well as other columns, all containing integer values.

class GameStats
"""Reads the database for the stats for every player in a game and
calculates the stats"""

def AtBat(self, player_id):
"""Calculate the AB for a player. AB = PA - (BB + SacBunt + HBP -
SF)"""
How do I get the columns PA, BB, etc for a particular playerID to
calculate the BA from the result returned by the Z SQL method? Is there some
documentation for how to access the results of the query within this class?

Thanks!

Mark


peter at sabaini

May 25, 2010, 12:03 PM

Post #2 of 5 (3082 views)
Permalink
Re: Question Concerning Z SQL Method and Class Bindings [In reply to]

On Mon, 2010-05-24 at 21:33 -0700, Mark Phillips wrote:
> I am reading the zope 2 book (relational database section) and it
> talks about binding classes by putting them in the Extensions
> directory and then associating them with a Z SQL Method. That seems to
> be rather straight forward, but where can I find some examples of
> accessing the values returned from the Z SQL Method in Python. For
> example,
>
> Z SQL Method
> select * from offense where gameID=<some input value from a form>
>
> This returns ~10 rows. Each row has columns playerID, PB, BB, SacBunt,
> HBP as well as other columns, all containing integer values.
>
> class GameStats
> """Reads the database for the stats for every player in a game and
> calculates the stats"""
>
> def AtBat(self, player_id):
> """Calculate the AB for a player. AB = PA - (BB + SacBunt +
> HBP - SF)"""
> How do I get the columns PA, BB, etc for a particular playerID
> to calculate the BA from the result returned by the Z SQL method? Is
> there some documentation for how to access the results of the query
> within this class?

Individual columns should be accessible as attributes of the Brain
object, eg. self.playerID etc. If there's a column PA, it should be
named self.PA.

Btw., usually when I'm in doubt which attributes or methods are
available for a given object, I just put a breakpoint at the location in
question and poke at it with pdb, the Python debugger. See eg.
http://wiki.zope.org/zope2/DebuggingWithPdb , under "breakpoint in the
code" how to invoke the debugger. See
http://docs.python.org/library/pdb.html for pdb docs.

hth,
peter.

>
> Thanks!
>
> Mark
> _______________________________________________
> Zope-DB mailing list
> Zope-DB [at] zope
> https://mail.zope.org/mailman/listinfo/zope-db
Attachments: signature.asc (0.19 KB)


mark at phillipsmarketing

May 25, 2010, 12:21 PM

Post #3 of 5 (3072 views)
Permalink
Re: Question Concerning Z SQL Method and Class Bindings [In reply to]

Peter,

I don't think I phrased my question very well. Also, I am coming from a Java
background, so I am thinking about result sets, which may not be applicable
to Python.

If I have a Z SQL Method query that returns multiple rows to a brains class,
how do I

(1) reference the returned object that has all the rows
(2) reference a particular row in the returned object
(3) reference a particular piece of data in the returned object.

What is the returned object - list, dictionary, some special object with
associated APIs?

Where is this documented? I can't seem to find it in the Zope 2 book.

Thanks!

Mark

On Tue, May 25, 2010 at 12:03 PM, Peter Sabaini <peter [at] sabaini> wrote:

> On Mon, 2010-05-24 at 21:33 -0700, Mark Phillips wrote:
> > I am reading the zope 2 book (relational database section) and it
> > talks about binding classes by putting them in the Extensions
> > directory and then associating them with a Z SQL Method. That seems to
> > be rather straight forward, but where can I find some examples of
> > accessing the values returned from the Z SQL Method in Python. For
> > example,
> >
> > Z SQL Method
> > select * from offense where gameID=<some input value from a form>
> >
> > This returns ~10 rows. Each row has columns playerID, PB, BB, SacBunt,
> > HBP as well as other columns, all containing integer values.
> >
> > class GameStats
> > """Reads the database for the stats for every player in a game and
> > calculates the stats"""
> >
> > def AtBat(self, player_id):
> > """Calculate the AB for a player. AB = PA - (BB + SacBunt +
> > HBP - SF)"""
> > How do I get the columns PA, BB, etc for a particular playerID
> > to calculate the BA from the result returned by the Z SQL method? Is
> > there some documentation for how to access the results of the query
> > within this class?
>
> Individual columns should be accessible as attributes of the Brain
> object, eg. self.playerID etc. If there's a column PA, it should be
> named self.PA.
>
> Btw., usually when I'm in doubt which attributes or methods are
> available for a given object, I just put a breakpoint at the location in
> question and poke at it with pdb, the Python debugger. See eg.
> http://wiki.zope.org/zope2/DebuggingWithPdb , under "breakpoint in the
> code" how to invoke the debugger. See
> http://docs.python.org/library/pdb.html for pdb docs.
>
> hth,
> peter.
>
> >
> > Thanks!
> >
> > Mark
> > _______________________________________________
> > Zope-DB mailing list
> > Zope-DB [at] zope
> > https://mail.zope.org/mailman/listinfo/zope-db
>
>


peter at sabaini

May 25, 2010, 12:55 PM

Post #4 of 5 (3077 views)
Permalink
Re: Question Concerning Z SQL Method and Class Bindings [In reply to]

On Tue, 2010-05-25 at 12:21 -0700, Mark Phillips wrote:
> Peter,
>
> I don't think I phrased my question very well. Also, I am coming from
> a Java background, so I am thinking about result sets, which may not
> be applicable to Python.
>
> If I have a Z SQL Method query that returns multiple rows to a brains
> class, how do I
>
> (1) reference the returned object that has all the rows
> (2) reference a particular row in the returned object
> (3) reference a particular piece of data in the returned object.

"Brain" objects are bound to individual record objects; each brain
object has access to the columns of the record it is bound to.

There is no way to access the whole result object this way. This is not
something Python-specific but rather the way brains are designed -- they
are used to enhance individual records, not the result set.

> What is the returned object - list, dictionary, some special object
> with associated APIs?

The whole result set is an object of type
Shared.DC.ZRDB.Results.Results. Individual records are instances of an
"inner class" r which inherits from Record.Record and the brain class
you specify (Python supports multiple inheritance).

Result objects can roughly be treated like lists (eg. iterable, allow
individual record access, have a length).

> Where is this documented? I can't seem to find it in the Zope 2 book.

Unfortunately this is largely undocumented.

peter.

>
> Thanks!
>
> Mark
>
> On Tue, May 25, 2010 at 12:03 PM, Peter Sabaini <peter [at] sabaini>
> wrote:
>
> On Mon, 2010-05-24 at 21:33 -0700, Mark Phillips wrote:
> > I am reading the zope 2 book (relational database section)
> and it
> > talks about binding classes by putting them in the
> Extensions
> > directory and then associating them with a Z SQL Method.
> That seems to
> > be rather straight forward, but where can I find some
> examples of
> > accessing the values returned from the Z SQL Method in
> Python. For
> > example,
> >
> > Z SQL Method
> > select * from offense where gameID=<some input value from a
> form>
> >
> > This returns ~10 rows. Each row has columns playerID, PB,
> BB, SacBunt,
> > HBP as well as other columns, all containing integer values.
> >
> > class GameStats
> > """Reads the database for the stats for every player in a
> game and
> > calculates the stats"""
> >
> > def AtBat(self, player_id):
> > """Calculate the AB for a player. AB = PA - (BB +
> SacBunt +
> > HBP - SF)"""
> > How do I get the columns PA, BB, etc for a
> particular playerID
> > to calculate the BA from the result returned by the Z SQL
> method? Is
> > there some documentation for how to access the results of
> the query
> > within this class?
>
>
> Individual columns should be accessible as attributes of the
> Brain
> object, eg. self.playerID etc. If there's a column PA, it
> should be
> named self.PA.
>
> Btw., usually when I'm in doubt which attributes or methods
> are
> available for a given object, I just put a breakpoint at the
> location in
> question and poke at it with pdb, the Python debugger. See eg.
> http://wiki.zope.org/zope2/DebuggingWithPdb , under
> "breakpoint in the
> code" how to invoke the debugger. See
> http://docs.python.org/library/pdb.html for pdb docs.
>
> hth,
> peter.
>
> >
> > Thanks!
> >
> > Mark
> > _______________________________________________
> > Zope-DB mailing list
> > Zope-DB [at] zope
> > https://mail.zope.org/mailman/listinfo/zope-db
>
>
Attachments: signature.asc (0.19 KB)


lists at zopyx

May 25, 2010, 9:25 PM

Post #5 of 5 (3088 views)
Permalink
Re: Question Concerning Z SQL Method and Class Bindings [In reply to]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mark Phillips wrote:
> Peter,
>
> I don't think I phrased my question very well. Also, I am coming from a
> Java background, so I am thinking about result sets, which may not be
> applicable to Python.
>
> If I have a Z SQL Method query that returns multiple rows to a brains
> class, how do I
>
> (1) reference the returned object that has all the rows
> (2) reference a particular row in the returned object
> (3) reference a particular piece of data in the returned object.
>
> What is the returned object - list, dictionary, some special object with
> associated APIs?
>
> Where is this documented? I can't seem to find it in the Zope 2 book.
>
>

If you want object-oriented approach in your Zope app: use an ORM like
SQLAlchemy or Storm. Sqlalchemy + zope.sqlalchemy (or z3c.sqlalchemy)
are widely used for SQL connectivity inside Zope/Plone/Python apps.

- -aj
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkv8otEACgkQCJIWIbr9KYy+4ACgr0LCPDN/lPuYNGDq7AGxXP+P
wK8AnjQMDVx3sYd1wlMIYxf6Cd7JFcBG
=dM/l
-----END PGP SIGNATURE-----
Attachments: lists.vcf (0.31 KB)

Zope db RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.