[sqlalchemy] Re: Looking for feedback on encapsulating SA logic (newbie)

2007-10-30 Thread Lukasz Szybalski

Is this a turbogears app? or just your stand alone app?



On 10/28/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

 I just started experimenting with .4 last night, and I'm really
 jazzed. The tutorials have been helpful, but I'm struggling to figure
 out how to bridge the gap between the tutorial fragments and a real-
 world application.

 So far, I've got a little idea working, and I'm hoping to get some
 feedback if I'm working in the right direction. I'm trying to
 encapsulate my SA logic in its own module and work with these objects
 in my programs.

 Below is a very simplified example of what I'm trying to do. I could
 be handling my sessions, metadata, and tables poorly/inefficiently,
 and I'd love some feedback where it could be better.

 One glaring problem is the handling of the session information. I
 tried to put it into a __get_session() method of ModelHandler, but I
 was having trouble getting it working when called from a subclass. (b/
 c User has no method __get_session())

 It also seems that the four lines under my classes (creating the
 engine, metadata, mapping, etc.) could be put into the constructor of
 my superclass, but I'm not sure how to refrerence it yet.

 Thanks in advance - I'm really looking forward to diving deeper with
 SA!

 *** Models.py ***

 from sqlalchemy import *
 from sqlalchemy.orm import mapper
 from sqlalchemy.orm import sessionmaker

 class ModelHandler(object):
 def save(self):
 # This session stuff should probably be handled by a private
 method
 # but I'm having trouble getting it to work when save is
 subclassed()
 Session = sessionmaker(bind=db, autoflush=True,
 transactional=True)
 session = Session()
 session.save(self)
 session.commit()
 print Debugging Statement: Saved User

 class User(ModelHandler):
 def __init__(self, name, fullname, password):
 self.name = name
 self.fullname = fullname
 self.password = password

 # There should be some tidy place to put these in my objects
 db = create_engine('postgres://apache:@localhost:5432/test')
 metadata = MetaData(db)
 users_table = Table('users', metadata, autoload=True)
 mapper(User, users_table)


  from models import *
  new_user = User('wendy', 'Wendy Williams', 'foobar')
  new_user.save()


 



-- 
-- 
Vim auto completion for python
http://lucasmanual.com/mywiki/FrontPage#head-8ce19b13e89893059e126b719bebe4ee32fe103c
TurboGears from start to finish:
http://www.lucasmanual.com/mywiki/TurboGears

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Looking for feedback on encapsulating SA logic (newbie)

2007-10-30 Thread Michael Bayer


On Oct 30, 2007, at 2:26 AM, Sanjay wrote:


 mapper(User, users_table)

 Is not using assign_mapper preferable?

assign_mapper is an optional extension.  its replaced in version 0.4  
by the ScopedSession.mapper function, which is described in the  
docs.  only use these extensions if you're looking for their specific  
functionality...they aren't needed to use sqlalchemy.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Looking for feedback on encapsulating SA logic (newbie)

2007-10-30 Thread [EMAIL PROTECTED]

 Is this a turbogears app? or just your stand alone app?

It's a standalone (and non-web) app.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Looking for feedback on encapsulating SA logic (newbie)

2007-10-29 Thread Sanjay

 mapper(User, users_table)

Is not using assign_mapper preferable?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---