symbol
is a callable within the sqlalchemy.util.langhelpers
module of the SQLAlchemy project.
public_factory
is another callable from the sqlalchemy.util.langhelpers
package with code examples.
sqlalchemy-utils (project documentation and PyPI package information) is a code library with various helper functions and new data types that make it easier to use SQLAlchemy when building projects that involve more specific storage requirements such as currency. The wide array of data types includes ranged values and aggregated attributes.
sqlalchemy-utils / sqlalchemy_utils / models.py
# models.py
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy.util.langhelpers import symbol
class Timestamp(object):
created = sa.Column(sa.DateTime, default=datetime.utcnow, nullable=False)
updated = sa.Column(sa.DateTime, default=datetime.utcnow, nullable=False)
@sa.event.listens_for(Timestamp, 'before_update', propagate=True)
def timestamp_before_update(mapper, connection, target):
target.updated = datetime.utcnow()
NO_VALUE = symbol('NO_VALUE')
NOT_LOADED_REPR = '<not loaded>'
def _generic_repr_method(self, fields):
state = sa.inspect(self)
field_reprs = []
if not fields:
fields = state.mapper.columns.keys()
for key in fields:
value = state.attrs[key].loaded_value
if value == NO_VALUE:
value = NOT_LOADED_REPR
else:
value = repr(value)
field_reprs.append('='.join((key, value)))
return '%s(%s)' % (self.__class__.__name__, ', '.join(field_reprs))
def generic_repr(*fields):
if len(fields) == 1 and callable(fields[0]):
target = fields[0]
target.__repr__ = lambda self: _generic_repr_method(self, fields=None)
return target
## ... source file continues with no further symbol examples...