[sqlalchemy] Re: sqlalchemy and redis caching

2011-12-26 Thread Mengu
hi michael,

thank you very much for your response. i have read the beaker cache
examples but i am yet to grasp it. the current solution i have found
is querying the relationships as well with the joinedload() and
caching the result.

after i fully understand what is going on and how to do it better with
the examples, i will do a blog post.

thanks again.

On 25 Aralık, 01:04, Michael Bayer mike...@zzzcomputing.com wrote:
 On Dec 24, 2011, at 5:38 PM, Mengu wrote:

  hi all. i am caching my query results in redis with
  sqlalchemy.ext.serializer.loads/dumps. like results =
  DBSession.query(ShowContestant).all() and redis.set(key,
  dumps(results))

  when i do loads(redis.get(key)) i get the obj without any problems but
  if i want to access any of its relations, i get this error:
  DetachedInstanceError: Parent instance ShowContestant at
  0x7f532403b1d0 is not bound to a Session; lazy load operation of
  attribute 'videos' cannot proceed.

  how can i fix this? please also let me know if i'm doing this proper
  or not.

 You need to merge the objects back in.  See how the Beaker caching example 
 does this, it's in the distro and introduced 
 athttp://www.sqlalchemy.org/docs/orm/examples.html?highlight=beaker#bea

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] sqlalchemy and redis caching

2011-12-24 Thread Mengu
hi all. i am caching my query results in redis with
sqlalchemy.ext.serializer.loads/dumps. like results =
DBSession.query(ShowContestant).all() and redis.set(key,
dumps(results))

when i do loads(redis.get(key)) i get the obj without any problems but
if i want to access any of its relations, i get this error:
DetachedInstanceError: Parent instance ShowContestant at
0x7f532403b1d0 is not bound to a Session; lazy load operation of
attribute 'videos' cannot proceed.

how can i fix this? please also let me know if i'm doing this proper
or not.

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Re: Validation of new objects before committing

2011-09-28 Thread Mengu
while we are on the subject, i'd like to ask a question if i'm doing
it right or wrong.

i have created a class called Validation with a method called is_valid
and I have mixed it in my SQLAlchemy models. all of my models has an
attribute called validation that consists of model's attributes that
must be valid. it's like this:

class User(DeclarativeBase, Validation):
validation = {
'username': [formencode.validations.NotEmpty],
'email': [formencode.validations.NotEmpty,
formencode.validations.Email]
   }

   # model definition here..

and then in my controller i check if model_instance.is_valid() and
then add it to my db. if it's not valid, i render the previous page.

let me know if you guys have any recommendations.

On 27 Eylül, 21:56, Michael Bayer mike...@zzzcomputing.com wrote:
 On Sep 27, 2011, at 1:57 PM, Kirk Strauser wrote:

  Does SA natively support (or is there a module on PyPI that supports) 
  client-side validation of SQLAlchemy objects? For example, I have this 
  declarative class:

     class ImportedPayment(Base):
         __tablename__ = 'importedpayment'
         __table_args = {'schema': 'public'}
         paymentid = Column(Integer, primary_key=True)
         externalid = Column(String(16), nullable=False)
         line = Column(Integer, nullable=False)
         invoicestatus = Column(String(32), nullable=False)
         quantity = Column(Numeric(scale=2), nullable=False)
         rate = Column(Numeric(scale=2), nullable=False)

  Is there an easy way to do this?

 OK so the aspect you're looking for here is to define those validations just 
 once, this is easy enough through a recipe like this:

 def notnull(key):
     def validate(obj):
         if  getattr(obj, key) is None:
             return Object %s key %s is None % (obj, key)
         else:
             return False
     return validate

 _validators = {}

 def get_validators(someobject):
     mapper = object_mapper(someobject)
     if mapper in _validators:
         return _validators[mapper]
     _validators[mapper] = v = []
     for prop in mapper.iterate_properties():
         if hasattr(prop, columns):
             col = prop.columns[0]
             if not col.nullable:
                 v.append(notnull(prop.key))
           # ... ad nauesum, i.e.
           #  if something else about the column:
           #      v.append(some other kind of validation function)
     return v

 def validate(someobject):
    for validator in get_validators(someobject):
        msg = validator()
        if msg:
             log(msg)
             # etc., i.e.
             # alert_the_authorities()

  If not, why?

 So the theme for today is why does SQLA have recipes, basically when we can 
 provide the core fragment of a feature but not a fully polished, documented, 
 tested, packaged result, something that can just as easily be delivered as a 
 small batch of customizable source code gets the job done pretty well, and 
 would be better suited as a separate library if fully fleshed out.

 The above recipe lacks a lot of features one might want, such as customizable 
 ways of defining the validation failure, behavior on the receipt of a failed 
 validation, etc.    A full blown validation library might use the idea 
 above but expand upon it in a much bigger way.    I've had other ad-hoc 
 validation use cases that wouldn't work with the above structure, instead 
 needing a slightly different structure, so having a small thing just as code 
 for now is more flexible than a built in feature that only handles a small 
 subset of use cases.

  And if the answer to that is because you haven't written it yet, would 
  anyone be interested in using it if I were to create such a thing?

 You might want to check around if similar things don't exist already, I did 
 findhttp://pypi.python.org/pypi/SAValidation/andhttp://pypi.python.org/pypi/sqlalchemy_elixir_validations/for
  example, there might be features there that are of use.   But by all means, 
 produce a better validation library for SQLAlchemy, the more the merrier and 
 I'd love to see more.

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.