Package: metalchemy
metalchemy: SQLAlchemy hierarchical key/value helperThemetalchemypackage provides helpers for your SQLAlchemy models to add dynamic properties.InstallationpipinstallmetalchemyUsagemetalchemy usage example:fromsqlalchemy.ext.declarativeimportdeclarative_baseBase=declarative_base()importmetalchemymetalchemy_attributes=metalchemy.initialize(Base)Session=sessionmaker(bind=engine)sess=Session()classMyModel(Base)meta=metalchemy_attributes.Metadata()my_object=MyModel()my_object.meta.some.value='some value'sess.add(my_object)sess.commit()sess.refresh(my_object)assertmy_object.meta.some.value.get_value()=='some value'In order to give a class metadata capabilities, add a single class attribute to it which is an instance
of <Metadata>:classHasMetadata(object):meta=metadata.Metadata()Any instance of this class will now have its metadata accessible via themetaattribute.Such meta attributes allow free reading and assigning of attributes, with no limits on the depth of the attributes.
i.e., meta.foo is always available for reading and writing, but also meta.foo.bar.baz, without any setup beforehand.Assigning to a metadata property is simple and
obvious:inst.meta.foo=42inst.meta.foo.bar.baz='qux'Any metadata attribute is also implicitly an array. It is possible to assign and read from any
index:inst.meta.foo[0]=42inst.meta.foo[1]='baz'inst.meta.foo[1].bar='qux'inst.meta.foo[1].spam[2]='eggs'inst.meta.foo[1][2]='xyzzy'Any non-indexed attribute is implicitly converted to an index of zero.All metadata values are converted to unicode strings on assignment. Assigned values are automatically added to
the SQLAlchemy session, but not committed, so remember to execute session.commit().
Furthermore, the methodsFieldWrapper.append,FieldWrapper.iteritemsandFieldWrapper.__iter__are supported as well for direct iteration.Reading the value back requires usingget_valuemethod:inst.meta.foo.get_value()# returns 42inst.meta.foo.bar.baz.get_value()# returns u'qux'Internals:
The hierarchical structure of the fields is stored in an adjacency list (represented by <_Fields>), which is unique
for a single class. All instances of a class share this same tree. An <_Object> maps the class name to this tree,
and is set up to have the entire tree load at once when it is needed.The <Metadata> instance assigned to a container class will load the <_Object> (and implicitly, the field hierarchy)
on access and return a wrapped root node.<FieldWrappers> wrap each <_Field>, performing two functions:
- They allow accessing fields that have no concrete <_Field> instance yet, creating these as necessary
- They actually access the values list of a specific container class instance.Values are stored in a flat list, which is loaded entirely for the container class instane when it is first needed.Array support is handled by having two states ofFieldWrappers: regular and indexed.A regular wrapper wraps an unindexed attribute. It handles reading and writing of array elements on its attribute
(FieldWrapper.__getitem__andFieldWrapper.__setitem__), and defers attribute access to its own zeroth index.An indexed wrapper wraps an indexed attribute. This wrapper handles attribute access by returning a wrapper for that
child attribute. Indexed wrappers can also be indexed, but this is internally done by deferring the secondary index
to a hidden child attribute. i.e. accessingmeta.foo[0][1][2]is internally handled as accessingmeta.foo[0].<hidden>[1].<hidden>[2].WarningBefore metadata is assigned, primary key must have been set to its value. This means they must be at least flushed once before assigning metadata.ContactIf you have questions, bug reports, suggestions, etc. please create an issue on
theGitHub project page.LicenseThis software is licensed under theMIT licenseSeeLicense© 2014 Paylogic International.Changelog1.0.0Initial public release