This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

metalchemy: SQLAlchemy hierarchical key/value helper

The metalchemy package provides helpers for your SQLAlchemy models to add dynamic properties.


pip install metalchemy


metalchemy usage example:

from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

import metalchemy

metalchemy_attributes = metalchemy.initialize(Base)

Session = sessionmaker(bind=engine)
sess = Session()

class MyModel(Base)

    meta = metalchemy_attributes.Metadata()

my_object = MyModel()
my_object.meta.some.value = 'some value'

assert  my_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>:

class HasMetadata(object):
    meta = metadata.Metadata()

Any instance of this class will now have its metadata accessible via the meta attribute.

Such meta attributes allow free reading and assigning of attributes, with no limits on the depth of the attributes. i.e., is always available for reading and writing, but also, without any setup beforehand.

Assigning to a metadata property is simple and obvious: = 42 = 'qux'

Any metadata attribute is also implicitly an array. It is possible to assign and read from any index:[0] = 42[1] = 'baz'[1].bar = 'qux'[1].spam[2] = 'eggs'[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 methods FieldWrapper.append, FieldWrapper.iteritems and FieldWrapper.__iter__ are supported as well for direct iteration.

Reading the value back requires using get_value method:  # returns 42  # 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 of FieldWrappers: regular and indexed.

A regular wrapper wraps an unindexed attribute. It handles reading and writing of array elements on its attribute (FieldWrapper.__getitem__ and FieldWrapper.__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. accessing[0][1][2] is internally handled as accessing[0].<hidden>[1].<hidden>[2].


  • Before metadata is assigned, primary key must have been set to its value. This means they must be at least flushed once before assigning metadata.


If you have questions, bug reports, suggestions, etc. please create an issue on the GitHub project page.


This software is licensed under the MIT license

See License

© 2014 Paylogic International.



  • Initial public release
Release History

Release History


This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
metalchemy-1.0.0.tar.gz (10.4 kB) Copy SHA256 Checksum SHA256 Source Nov 10, 2014

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting