views:

21

answers:

1

I would like to share my data model between different Elixir/SQLAlchemy applications, one of which would be a Camelot UI and the others stuff like web interfaces and so on. They would all connect to the same underlying database.

As far as I know, to build a Camelot app my model would do from camelot import blah and that would prevent it to run in any environment without Camelot installed.

I would like to know if there is a recommended way / best practice to do that. The idea is of course to maintain a single code base for my model and not to replicate it with subtle differences between different apps (like importing from SA/Elixir here, and from Camelot there, and so on).

My project is currently laid out with a model/ python package:

model/__init__.py
      foo.py
      bar.py
      ...

init.py looks like this:

from foo import a, b, c 
from bar import d, e, f 
__all__ = ('a', 'b', 'c', 'd', 'e', 'f') 

and python modules foo.py, bar.py, and so on actually implement the various parts. Every one of these modules starts like this:

from sqlalchemy import Integer, Numeric, Date, Unicode, LargeBinary 
from elixir import Entity, Field, ManyToOne, OneToMany, ManyToMany 
from elixir import using_options 

An idea could be to do something like:

try:
    from camelot import Integer, Numeric, ...
except ImportError:
    from elixir import Integer, Numeric, ...

would that be actually a good idea or there's something I'm missing? Also, ideally I'd do that kind of "environment initialization" stuff in some central place, like in model/__init__.py, but how would I pass my imports to the underlying modules?

A: 

I can't speak as to whether it would be a good idea, but it's easy to make the imports central because modules are 'singletons' in the Java idiom: they share state. In other words, you could do the following:

dataProxy.py

try:
    from camelot import Integer, Numeric, ...
except ImportError:
    from elixir import Integer, Numeric, ...

and then in a different module do

from dataProxy import Integer

and you will get the same classes everywhere (in the same interpreter session, that is). This idiom is often used for configuration files, because you can write e.g. setup code in a settings.py and then the rest of your app will have access to the results of that code.

katrielalex