yalchemy

yalchemy is a tool for converting YAML (or Python dictionaries) to SQLAlchemy objects.

Quick Start

yalchemy provides the following intermediate yalchemy objects:

  1. Table - For converting SQLAlchemy Tables.
  2. Index - For converting SQLAlchemy Indexes.
  3. ForeignKey - For converting SQLAlchemy ForeignKeys.
  4. UniqueConstraint - For converting SQLAlchemy UniqueConstraints.
  5. CheckConstraint - For converting SQLAlchemy CheckConstraints.
  6. Column - For converting SQLAlchemy Columns
  7. TableSet - A utility for representing a list of SQLAlchemy Tables.

Each of these objects implement the following yalchemy object interface:

class yalchemy.table_structures.Yalchemy[source]

The base yalchemy object that defines the interface for serialization

classmethod from_dict(dict_obj)[source]

Constructs a yalchemy object from a dictionary

classmethod from_sqla(sqla_obj)[source]

Constructs a yalchemy object from a sqlalchemy object

Parameters:sqla_obj – The sqlalchemy object
classmethod from_yaml(yaml_str, loader_factory=None)[source]

Loads a yalchemy object from a yaml string

Parameters:
  • yaml (str) – The yaml string
  • loader_factory (function(stream)) – (optional) constructs a custom PyYaml loader. If not provided and libyaml is installed, it will default to CSafeLoader. If not provided and libyaml is not installed, it will default to SafeLoader.
to_dict()[source]

Converts a yalchemy object to a dictionary.

Note

Wrapper around _to_dict that will order the _to_dict returned dict by the __slots__ order in an OrderedDict. Please remember to override _to_dict and not to_dict when subclassing yalchemy.

to_sqla(**kwargs)[source]

Converts a yalchemy object to a sqlalchemy object

Parameters:kwargs – Keyword arguments specific to the yalchemy object being created. Some objects need special parameters for their construction (e.g. sqlalchemy MetaData)
to_yaml()[source]

Converts a yalchemy object to a yaml string

With this interface, it’s possible to do the following types of conversions:

import yalchemy

# Create a SQLAlchemy table from a dictionary
table = yalchemy.Table.from_dict({
    'name': 'my_table',
    'schema': 'schema',
    'columns': [
        {'name': 'col1', 'datatype': 'varchar', 'format': [123], 'null': '^@'},
        {'name': 'col2', 'datatype': 'integer', 'required': True},
        {'name': 'col3', 'datatype': 'timestamptz', 'required': True,
         'default': {'type': 'function', 'value': 'NOW()'}}
    ],
    'foreign_keys': [
        {'column': 'col2', 'remote_table': 'other_table', 'remote_column': 'other_col'}
    ],
    'indexes': [{'columns': ['col1', 'col2']}],
    'unique_constraints': [{'columns': ['col3']}],
    'primary_keys': ['col2']
}).to_sqla(...)

# Columns, foreign keys, and indexes can also be created / converted in the same way
column = yalchemy.Column.from_dict({
    'name': 'col2',
    'datatype': 'integer',
    'required': True
}).to_sqla(...)

fkey = yalchemy.ForeignKey.from_dict({
    'column': 'col2',
    'remote_table': 'other_table',
    'remote_column': 'other_col'
}).to_sqla(...)

index = yalchemy.Index.from_dict({'columns': ['col1', 'col2']}).to_sqla(...)

In yalchemy, the keys used in the dictionaries in the from_dict constructors are similar to the constructor arguments. For example,

# Create a yalchemy column object directly
column = yalchemy.Column(name='col2', remote_table='other_table', remote_column='other_col')

# Create the yalchemy object from a dictionary
column = yalchemy.Column.from_dict({'name': 'col2', 'remote_table': 'other_table', 'remote_column': 'other_col'})

# Convert it to sqlalchemy
column.to_sqla()

When instantiating more complex objects like a Table, the keys used in the dictionaries are also similar to the constructor arguments, however, the values are sometimes other yalchemy objects. It is recommended to use from_dict constructor when instantiating a yalchemy object whenever possible.

Keep in mind that to_sqla methods sometimes require additional arguments for conversion. Please consult the Interface section for details on all of the to_sqla methods.

Next Steps

For the full documentation on the interface and the spec used to create yalchemy object from dictionaries, view the Interface section.