Thanks! That was it. My weak Python understanding was my downfall! On Dec 8, 11:06 pm, Diana Clarke <[email protected]> wrote: > [hmmm, let's try that again... not sure why gmail mangled my email] > > Hi John: > > The following import is causing that error. > > from sqlalchemy.orm import session, Session > > In the all-in-one example you provided, you later go on to reassign > 'session' and 'Session', replacing those imported values with your own > values. > > Session = scoped_session(sessionmaker(bind=engine)) session = Session() > In the two-file version, restresource.py isn't getting the 'Session' > you created in two_file_server.py, but rather a new blank one from the > import statement. > Hope that helps, > --diana > > > > > > > > On Thu, Dec 8, 2011 at 8:41 PM, John Hufnagle <[email protected]> wrote: > > Thanks Michael, > > > Could not find a hidden Session object. > > So I broke it down into a problem that works/doesn't work based on > > files separation > > > 1. If I place all of the code into one file all_in_one.py which > > contains the cherrypy startup, the SA init code and ORM object and the > > cherrypy REST resource then it works when trying to use a session > > during request handling > > > 2. If I divide up the code into 2 files...the cherrypy startup code & > > SA init code in one file and then the REST class, and the ORM class in > > the other then it fails when trying to use a session during request > > handling. > > > In both cases the code is 'essentially' the same. > > I'm sure I'm missing a python based problem...I'm new to it as well as > > SA. > > ------------------------------------ > > Working single file case all_in_one.py > > > import cherrypy > > from sqlalchemy import * > > import MySQLdb > > from sqlalchemy.orm import session, Session, scoped_session, > > sessionmaker > > from sqlalchemy.ext.declarative import declarative_base > > from sqlalchemy.dialects.mysql import MEDIUMTEXT > > from sqlalchemy import Column, Integer, String, DateTime, func > > > engine = create_engine('mysql://xxxx:xxxxx@localhost/test', echo=True) > > meta = MetaData() > > Session = scoped_session(sessionmaker(bind=engine)) > > > class RESTResource(object): > > orm_class = None > > > def __init__(self, ormcls): > > self.orm_class = ormcls > > > @cherrypy.expose > > def default(self, *vpath, **params): > > method = getattr(self, "handle_" + cherrypy.request.method, > > None) > > if not method: > > methods = [x.replace("handle_", "") > > for x in dir(self) if x.startswith("handle_")] > > cherrypy.response.headers["Allow"] = ",".join(methods) > > raise cherrypy.HTTPError(405, "Method not implemented.") > > return method(*vpath, **params); > > > def handle_GET(self, *vpath, **params): > > try: > > session = Session() > > session.query(ProjectORM).filter(ProjectORM.id == > > 12).one() > > except: > > print "got exception!!!!!" > > raise > > print "all ok!" > > return "done GET" > > > def handle_PUT(self, *vpath, **params): > > return "done PUT" > > > Base = declarative_base() > > class ProjectORM(Base): > > __tablename__ = 'project' > > > id = Column(Integer, primary_key=True) > > name = Column(String(256)) > > about = Column(String(1024)) > > url = Column(String(512)) > > version = Column(Integer) > > mbd_metadata = Column('metadata',MEDIUMTEXT) > > creation = Column(DateTime) > > > def __init__(self, name, about, url, version, mbd_metadata): > > self.name = name > > self.about = about > > self.url = url > > self.version = version > > self.mbd_metadata = mbd_metadata > > self.creation = func.now() > > > def __repr__(self): > > return "<User('%s','%s', '%s')>" % (self.name, self.about, > > self.url, self.version, self.mbd_metadata, self.creation) > > > class Root(object): > > project = RESTResource(ProjectORM) > > > @cherrypy.expose > > def index(self): > > return "JBPC VAMPS REST Service" > > > cherrypy.quickstart(Root()) > > > ----------------------------------------- > > ----------------------------------------- > > Failing two file case > > first file > > two_file_server.py as follows: > > > import cherrypy > > from sqlalchemy import * > > import MySQLdb > > from sqlalchemy.orm import session, Session, scoped_session, > > sessionmaker > > from sqlalchemy.ext.declarative import declarative_base > > from sqlalchemy.dialects.mysql import MEDIUMTEXT > > from restresource import * > > > engine = create_engine('mysql://xxxx:xxxxx@localhost/test', echo=True) > > meta = MetaData() > > Session = scoped_session(sessionmaker(bind=engine)) > > > class Root(object): > > project = RESTResource(ProjectORM) > > > @cherrypy.expose > > def index(self): > > return "JBPC VAMPS REST Service" > > > cherrypy.quickstart(Root()) > > > ----------------------------------------- > > ----------------------------------------- > > > the second file of the two file set: > > restresource.py > > > import cherrypy > > from sqlalchemy import * > > import MySQLdb > > from sqlalchemy.orm import session, Session, scoped_session, > > sessionmaker > > from sqlalchemy.ext.declarative import declarative_base > > from sqlalchemy.dialects.mysql import MEDIUMTEXT > > from sqlalchemy import Column, Integer, String, DateTime, func > > > Base = declarative_base() > > class ProjectORM(Base): > > __tablename__ = 'project' > > > id = Column(Integer, primary_key=True) > > name = Column(String(256)) > > about = Column(String(1024)) > > url = Column(String(512)) > > version = Column(Integer) > > mbd_metadata = Column('metadata',MEDIUMTEXT) > > creation = Column(DateTime) > > > def __init__(self, name, about, url, version, mbd_metadata): > > self.name = name > > self.about = about > > self.url = url > > self.version = version > > self.mbd_metadata = mbd_metadata > > self.creation = func.now() > > > def get_one(self): > > pass > > > def __repr__(self): > > return "<User('%s','%s', '%s')>" % (self.name, self.about, > > self.url, self.version, self.mbd_metadata, self.creation) > > > class RESTResource(object): > > orm_class = None > > > def __init__(self, ormcls): > > self.orm_class = ormcls > > > @cherrypy.expose > > def default(self, *vpath, **params): > > method = getattr(self, "handle_" + cherrypy.request.method, > > None) > > if not method: > > methods = [x.replace("handle_", "") > > for x in dir(self) if x.startswith("handle_")] > > cherrypy.response.headers["Allow"] = ",".join(methods) > > raise cherrypy.HTTPError(405, "Method not implemented.") > > return method(*vpath, **params); > > > def handle_GET(self, *vpath, **params): > > try: > > session = Session() > > session.query(ProjectORM).filter(ProjectORM.id == > > 12).one() > > except: > > print "got exception!!!!!" > > raise > > print "all ok!" > > return "done GET" > > > def handle_PUT(self, *vpath, **params): > > return "done PUT" > > > def get_all_or_one(self,vpath): > > if len(vpath) == 0: > > all = self.orm_class.get_all() > > else: > > one = self.orm_class.get_instance(vpath[0]) > > k = 1 > > > -- > > You received this message because you are subscribed to the Google Groups > > "sqlalchemy" group. > > To post to this group, send email to [email protected]. > > To unsubscribe from this group, send email to > > [email protected]. > > For more options, visit this group > > athttp://groups.google.com/group/sqlalchemy?hl=en.
-- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected]. 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.
