Camelot 和非 Camelot 应用程序之间的共享模型

发布于 2024-09-27 15:04:06 字数 1310 浏览 9 评论 0原文

我想在不同的 Elixir/SQLAlchemy 应用程序,其中之一是 Camelot UI 和其他东西,如 Web 界面等。它们都将连接到同一个底层数据库。

据我所知,要构建 Camelot 应用程序,我的模型将执行 from Camelot import blah ,这将阻止它在没有安装 Camelot 的任何环境中运行。

我想知道是否有推荐的方法/最佳实践来做到这一点。当然,这个想法是为我的模型维护一个单一的代码库,而不是在不同应用程序之间复制具有细微差别的代码库(例如从 SA/Elixir 导入,从 Camelot 导入,等等)。

我的项目目前使用 model/ python 包进行布局:

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

init.py 如下所示:

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

python 模块 foo.py、bar.py 等实际实现了各个部分。 这些模块中的每一个都是这样开始的:

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

一个想法可能是做这样的事情:

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

这实际上是一个想法还是我遗漏了一些东西?另外,理想情况下,我会在某个中心位置执行此类“环境初始化”操作,例如在 model/__init__.py 中,但是我如何将导入传递到底层模块?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池予 2024-10-04 15:04:06

我不能说这是否是一个好主意,但是很容易使导入成为中心,因为模块在 Java 习惯用法中是“单例”:它们共享状态。换句话说,您可以执行以下操作:

dataProxy.py

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

然后在不同的模块中执行此操作

from dataProxy import Integer

,您将在各处获得相同的类(即在同一个解释器会话中)。这种习惯用法通常用于配置文件,因为您可以在 settings.py 中编写设置代码,然后应用程序的其余部分将可以访问该代码的结果。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文