tags:

views:

129

answers:

1

I wrote a minimal database dialect for SQLAlchemy that doesn't really belong in the core. How do I make it work as its own Python package?

+2  A: 

When SQLAlchemy resolves a database url example://... it will first try to find it in import sqlalchemy.dialects.example. If that doesn't work it falls back to pkg_resources.iter_entry_points('sqlachemy.dialects').

Put the new dialect in a package using setuptools, include an entry point named after your dialect, run python setup.py develop or python setup.py install, and SQLAlchemy should be able to find the dialect.

In setup.py:

  entry_points="""
  [sqlalchemy.dialects]
  example = example_sa:base.dialect
  """

example_sa:base.dialect means import example_sa; return base.dialect.

After installing this package, pkg_resources.iter_entry_points(group) yields pkg_resources.EntryPoint instances from group. Call .load() on the EntryPoint with entrypoint.name='example' and you get example_sa.base.dialect.

I was pleasantly suprised at how easy it is to write new dialects for SQLAlchemy 0.6. If your database has just a few quirks compared to standard SQL, chances are you will be able to inherit from the standard (MySQL-like) SQL dialect, define your database's keywords, and copy the implementation of those quirks (like SELECT TOP 10 instead of SELECT ... LIMIT 10) from an existing dialect.

joeforker
In SQLAlchemy 0.5 the entry point is called sqlalchemy.databases
pi