Based on Michael Bayer's hint, I built this instance-level changeable
object.  It used Declarative, which actually makes it a bit tougher, but
this code should work for pypo's also. It's hackish in that it just monkeys
with __setattr__, but it's clear(ish) what's happening.

-------------------------
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Integer,String,Column

Base = declarative_base()

class NotEditableError(AttributeError):
    pass

class Thingy(Base):
    __tablename__ = 'thingy'
    field = Column(String, nullable=False, primary_key=True)

    def __init__(self, field='', editable=True):
        self.field = field
        self.editable = editable # must be last set, or __setattr__
                                 # will have problems

    def __setattr__(self,attr,value):
        # catch before editable is defined
        try:
           editable = self.editable
        except AttributeError:
           editable = True
        # the only 'always editable' value is "editable"
        if attr == "editable" or editable: # it's all good
            Base.__setattr__(self, attr, value)
        else:
            raise NotEditableError, "for this object, fields are not
editable"


def Test():
    T = Thingy('abc',False)
    assert T.field == 'abc'
    try:
        T.field = "pdq"
    except NotEditableError, exc:
        pass
    T.editable = True
    T.field = "pdq"
    assert T.field == 'pdq'
    return T

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to