Here is my example, how to get arond decorator classmethod (without it not
work, self.__class__ instead of Address not override this function)
session = sqlalchemy.orm.scoped_session(sqlalchemy.orm.sessionmaker(
bind=engine, autoflush=False))
def make_dump(obj):
return json.dumps(
obj, default=lambda o: o.__dict__, sort_keys=True, indent=4)
class TownDetail(object):
def __init__(self, name, value):
self.name = name
self.value = value
def __composite_values__(self):
return self.name, self.value
def __eq__(self, other):
return isinstance(other, self.__class__) and\
make_dump(self) == make_dump(other)
def __ne__(self, other):
return not self.__eq__(other)
class Address(object):
def __init__(self, name=None, value=None, town_detail=None):
self.name = name
self.value = value
self.town_detail = town_detail
Address.generate = self.generate
def __composite_values__(self):
return (self.name, self.value) + self.town_detail.__composite_values__()
def __eq__(self, other):
return isinstance(other, self.__class__) and\
make_dump(self) == make_dump(other)
def __ne__(self, other):
return not self.__eq__(other)
def address_gen_function(name, value, town_detail_name, town_detail_value):
return Address(name, value, TownDetail(town_detail_name, town_detail_value
))
Address.generate = address_gen_function
class TestComposite(Base):
__tablename__ = 'test_composite'
id = sqlalchemy.Column(sqlalchemy.types.Integer, primary_key=True)
name = sqlalchemy.Column(sqlalchemy.types.String)
_street_address_name = sqlalchemy.Column(sqlalchemy.types.String)
_street_address_value = sqlalchemy.Column(sqlalchemy.types.Integer)
_street_address_town_detail_name = sqlalchemy.Column(sqlalchemy.types.
String)
_street_address_town_detail_value = sqlalchemy.Column(
sqlalchemy.types.Integer)
street_address = composite(
Address.generate, _street_address_name, _street_address_value,
_street_address_town_detail_name, _street_address_town_detail_value
)
test_instance = TestComposite(
name='tratata!', street_address=Address(
name=22, value='values', town_detail=TownDetail(name='test_nested',
value=228))
)
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.