gpt4 book ai didi

python - 一个用SQLAlchemy做的图形化界面的应用程序源码怎么编排?

转载 作者:太空狗 更新时间:2023-10-30 00:04:21 25 4
gpt4 key购买 nike

我正在使用 SQLAlchemy 和 wxPython 开发一个应用程序,我试图将其分布在由业务逻辑、ORM 和 GUI 组成的独立模块中。

我不完全确定如何以 pythonic 方式执行此操作。

考虑到mapping()必须调用orthern才能使用对象,所以我想把它放在业务逻辑的__init__.py中,但将所有表定义保存在单独的 orm.py 模块中。

我应该保留这样的东西吗:

/Business
/__init__.py
| mapping (module1.Class1, orm.table1)
|
/module1.py
Class1

/orm.py
import
table1 = Table()
/GUI
/main.py
| import business
/crud.py

或者类似的东西

/Business
/__init__.py
| import
|
/module1.py
Class1
table1 = Table()
mapping (module1.Class1, orm.table1)

/GUI
/main.py
| import business
/crud.py

是否推荐第一种方法?还有其他选择吗?我见过第二种方式,但我不喜欢将数据库处理代码和业务逻辑代码放在同一个模块中。我是不是想多了?真的不是那么大的问题吗?

最佳答案

我找到了 this document由 Jp Calderone 撰写,是关于如何(不)构建 Python 项目的重要提示。按照它你不会有问题。我将在此处复制整个文本:

Filesystem structure of a Python project

Do:

  • name the directory something related to your project. For example, if your project is named "Twisted", name the top-level directory for its source files Twisted. When you do releases, you should include a version number suffix: Twisted-2.5.
  • create a directory Twisted/bin and put your executables there, if you have any. Don't give them a .py extension, even if they are Python source files. Don't put any code in them except an import of and call to a main function defined somewhere else in your projects.
  • If your project is expressable as a single Python source file, then put it into the directory and name it something related to your project. For example, Twisted/twisted.py. If you need multiple source files, create a package instead (Twisted/twisted/, with an empty Twisted/twisted/__init__.py) and place your source files in it. For example, Twisted/twisted/internet.py.
  • put your unit tests in a sub-package of your package (note - this means that the single Python source file option above was a trick - you always need at least one other file for your unit tests). For example, Twisted/twisted/test/. Of course, make it a package with Twisted/twisted/test/__init__.py. Place tests in files like Twisted/twisted/test/test_internet.py.
  • add Twisted/README and Twisted/setup.py to explain and install your software, respectively, if you're feeling nice.

Don't:

  • put your source in a directory called src or lib. This makes it hard to run without installing.
  • put your tests outside of your Python package. This makes it hard to run the tests against an installed version.
  • create a package that only has a __init__.py and then put all your code into __init__.py. Just make a module instead of a package, it's simpler.
  • try to come up with magical hacks to make Python able to import your module or package without having the user add the directory containing it to their import path (either via PYTHONPATH or some other mechanism). You will not correctly handle all cases and users will get angry at you when your software doesn't work in their environment.

关于python - 一个用SQLAlchemy做的图形化界面的应用程序源码怎么编排?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1506887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com