Packages in a project's context

Within the src directory is the package tree for the project. Each directory level at or under the example_project directory that has an __init__.py file is a formal Python package, and will be accessible through an import statement in Python code. Once this project is built and installed, then, and assuming that the code within it is written to accommodate the relevant import structure, all of the following would be legitimate imports from the project's code:

import example_project

Imports the entire example_project namespace

import example_project.package

Imports example_project.package and all its members

from example_project import package

from example_project.package import member

Assuming that member exists, imports it from example_project.package

import example_project.package.subpackage

Imports example_project.package.subpackage and all its members

from example_project.package import subpackage

from example_project.package.subpackage import member

Assuming that member exists, imports it from example_project.package.subpackage

 

A typical pattern for packages in Python is to group code elements around common realms of functionality. For example, a package that, at a very high level, is focused on DOM manipulation (HTML page structure), and supports XML, XHTML, and HTML5 might group things like so:

  • dom (__init__.py)
    • generic (__init__.py)
      • [General-purpose classes for working with elements]
    • html (__init__.py)
      • generic (generic.py)
        • [General-purpose classes for working with HTML elements]
        • forms (forms.py)
    • html5 (__init__.py)
      • [Classes for working with HTML-5-specific elements]
      • forms (forms.py)
    • xhtml (__init__.py)
      • [Classes for working with XHTML-specific elements]
      • forms (forms.py)
    • xml (__init__.py)

A full implementation, then, of that structure might allow a developer to access an HTML5 Email field object by creating an instance of a class that lived at the dom.html5.forms.EmailField namespace, and whose code lived in .../dom/html5/forms.py as a class named EmailField.

Deciding where specific classes, functions, constants, and so on should exist in the structure of a code base is a complex topic, and will be explored in greater depth as part of the early architecture and design of hms_sys.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.54.7