开发者

Python client/server project code layout

Suppose you have a client/server application, say a webserver component and a qt gui. How do you layout your python code?

  • Packages foo.server and foo.client?
  • Packages fooserver and fooclient, wi开发者_StackOverflow中文版th both importing from foocommon?
  • Everything together with no clear distinction?

Having subpackages for server and client code (foo.server and foo.client) seems the best approach to me, but then how do you handle your distutils setup if you don't want the server code to be shipped along with the client code? If you use setuptools (I would rather not), how do you create separate eggs?


buildbot has opted to create separate packages (for the master and the slave). In source control, they have three folders: common, master, and slave (plus documentation).


I like namespaces, so yes. foo.client and foo.server and foo.common and foo.anythingelsethatcanbeusedseparately. But it's all a matter of taste, really.

And I'd release them as separate packages, and yes, I'd use Distribute.


Now I'm use only setuptools (really distribute), so I'm use that code in my projects:

setup.py:

from setuptools import find_packages, setup

setup(
    name = "foo.common",
    version = __import__("foo.common", fromlist=[""]).__version__,
    packages = find_packages(),
    namespace_packages = ["foo"]
)

All __init__.py files in namespace modules:

# this is a namespace package
try:
    import pkg_resources
    pkg_resources.declare_namespace(__name__)
except ImportError:
    import pkgutil
    __path__ = pkgutil.extend_path(__path__, __name__)

And real __init__.py file looks like:

VERSION = (0, 1, 0, "dev")

def get_version():
    if VERSION[3] != "final":
        return "%s.%s.%s%s" % (VERSION[0], VERSION[1], VERSION[2], VERSION[3])
    else:
        return "%s.%s.%s" % (VERSION[0], VERSION[1], VERSION[2])

__version__ = get_version()


Having subpackages for server and client code (foo.server and foo.client) seems the best approach to me,

Why? No single user (except you the developer) will ever use both sides. They're completely separate.

but then how do you handle your distutils setup if you don't want the server code to be shipped along with the client code?

Precisely. They're almost completely unrelated.

For guidance, look at other client-server applications.

The World Wide Web, for example.

The Apache HTTPD server and the Firefox browser do not have any common code that I can see. Maybe a few underlying libraries, but they're clearly not htttpd.client and httpd.server. They're clearly utterly separate.

The Sendmail server and the pop/imap libraries in Python seem to be utterly separated with almost nothing in common.

The MySQL database server and the MySQLDB interface in Python seem to be utterly and completely separated with almost nothing in common.

I can't see any examples of foo.server and foo.client anywhere. Perhaps you could share an example as part of your question to help clarify your thinking on this subject.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜