On 1/26/11 6:13 AM, Wade Leftwich wrote:
I've been dealing with similar issues on my first-ever Pyramid
project, also using sqlalchemy and traversal.

This may be indirection overkill, but I separated Context objects from
Data objects. Note this is a reporting application, so it's more into
displaying the results of queries than in interacting with individual
objects in the model. The Context objects organize the tree and
queries the database.

# in resources package

# sqlalchemy declarative classes
from orm import Site
from orm import SiteMonth

DBSession =
scoped_session(sessionmaker(extension=ZopeTransactionExtension()))

class Context(object):

     __name__ = None
     __parent__ = None
     children = {}

     def __getitem__(self, name):
         return self.children[name]

     def add_child(self, child, name):
         child.__name__ = name
         child.__parent__ = self
         self.children[name] = child

maybe you omitted code for brevity, but you don't really want `children` to be defined at the class level, do you? i.e. you have an __init__(self) method that sets `self.children = {}`, either here or in all of the base classes right? otherwise all of your instances are sharing the same children:

>>> class B(object):
...     foo = {}
...
>>> class C(B):
...     pass
...
>>> b = B()
>>> c = C()
>>> b.foo
{}
>>> c.foo
{}
>>> b.foo['bar'] = 'baz'
>>> b.foo
{'bar': 'baz'}
>>> c.foo
{'bar': 'baz'}

also, while this isn't necessary, you could override __setitem__ to do the same thing add_child is doing, then you could maintain the dictionary-like API, like so:

    root['report'] = Report()

and if you subclass from dict or UserDict, and define __delitem__ and keys methods, you can get the rest of the dictionary API pretty much for free.

-r


class Root(Context):

     def get_sites(self, like=None):
         session = DBSession()
         query = session.query(Site).order_by(Site.host)
         if like:
             if '%' not in like:
                 like += '%'
             query = query.filter(Site.host.like(like))
         return iter(query)


class Report(Context):

     def get_report(self, hosts=None, startdate=None, enddate=None):
         session = DBSession()
         query = session.query(SiteMonth).order_by(SiteMonth.host,
SiteMonth.month)
         if hosts:
             query = query.filter(SiteMonth.host.in_(hosts))
         if startdate:
             query = query.filter(SiteMonth.month>= startdate)
         if enddate:
             query = query.filter(SiteMonth.month<= enddate)
         return iter(query)

root = Root()
root.add_child(Report(), 'report')

--
You received this message because you are subscribed to the Google Groups 
"pylons-devel" group.
To post to this group, send email to pylons-devel@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-devel?hl=en.

Reply via email to