diff --git a/examples/A10_modules/foo/__init__.py b/examples/A10_modules/foo/__init__.py new file mode 100644 index 0000000..0926d04 --- /dev/null +++ b/examples/A10_modules/foo/__init__.py @@ -0,0 +1,37 @@ +# Package demonstrating relative imports and an export decorator. +# ------------------------------------------------------------------------------ +# Shows relative imports and how to define __all__. +# Define the packet API +__version__ = '1.0.0' + +# The dot(.) operator is used to import modules relative to the current package. +# A single dot (.) refers to the current package. +# Two dots (..) refer to the parent package. +# Three dots (...) refer to the grandparent package, and so on. + +from .api.submodule1 import func1 +from .core.submodule2 import func2 +from .gui.submodule3 import func3 + + +# The __all__ variable is used to define the public API of a module or a package. +__all__ = ['func1', 'func2', 'func3'] + + +# Export decorator +def export(defn): + + # Add the object to the global namespace + globals()[defn.__name__] = defn + + # Set the object to be exported + __all__.append(defn.__name__) + + # Return the object + return defn + + +# Demonstration of the export decorator +@export +def func4(): + print('func4') diff --git a/examples/A10_modules/foo/__main__.py b/examples/A10_modules/foo/__main__.py new file mode 100644 index 0000000..8a5e8e1 --- /dev/null +++ b/examples/A10_modules/foo/__main__.py @@ -0,0 +1,17 @@ +# Entry point executed when running the package with -m. +# ------------------------------------------------------------------------------ +# Allows running the package directly as a script. +import sys +import os.path + + +def main(): + progname = sys.argv[0] + sys.argv[:] = sys.argv[1:] + sys.path.insert(0, os.path.dirname(progname)) + + print('sys.argv =', sys.argv) + + +if __name__ == "__main__": + main() diff --git a/examples/A10_modules/foo/api/__init__.py b/examples/A10_modules/foo/api/__init__.py new file mode 100644 index 0000000..7c413cd --- /dev/null +++ b/examples/A10_modules/foo/api/__init__.py @@ -0,0 +1,4 @@ +# API subpackage exposing a version constant. +# ------------------------------------------------------------------------------ +# Holds version information for the API. +__version__ = '0.1.0' diff --git a/examples/A10_modules/foo/api/submodule1.py b/examples/A10_modules/foo/api/submodule1.py new file mode 100644 index 0000000..cc909f7 --- /dev/null +++ b/examples/A10_modules/foo/api/submodule1.py @@ -0,0 +1,9 @@ +# Submodule used to demonstrate absolute and relative imports. +# ------------------------------------------------------------------------------ +# Provides a function and constant for tests. +# Define a constant +my_id = 1 + + +def func1(): + print('func1() from submodule1.py with my_id =', my_id) diff --git a/examples/A10_modules/foo/core/__init__.py b/examples/A10_modules/foo/core/__init__.py new file mode 100644 index 0000000..039b80e --- /dev/null +++ b/examples/A10_modules/foo/core/__init__.py @@ -0,0 +1,3 @@ +# Core subpackage used in import demonstrations. +# ------------------------------------------------------------------------------ +# Core components used by the package. diff --git a/examples/A10_modules/foo/core/submodule2.py b/examples/A10_modules/foo/core/submodule2.py new file mode 100644 index 0000000..b8a0638 --- /dev/null +++ b/examples/A10_modules/foo/core/submodule2.py @@ -0,0 +1,9 @@ +# Submodule used in absolute import demonstrations. +# ------------------------------------------------------------------------------ +# Supplies a second function and constant. +# Define a constant +my_id = 2 + + +def func2(): + print('func2() from submodule2.py with my_id =', my_id) diff --git a/examples/A10_modules/foo/gui/__init__.py b/examples/A10_modules/foo/gui/__init__.py new file mode 100644 index 0000000..56981e5 --- /dev/null +++ b/examples/A10_modules/foo/gui/__init__.py @@ -0,0 +1,3 @@ +# GUI subpackage used in import demonstrations. +# ------------------------------------------------------------------------------ +# GUI layer that references the other submodules. diff --git a/examples/A10_modules/foo/gui/submodule3.py b/examples/A10_modules/foo/gui/submodule3.py new file mode 100644 index 0000000..744258b --- /dev/null +++ b/examples/A10_modules/foo/gui/submodule3.py @@ -0,0 +1,9 @@ +# Submodule used by the GUI package for import demonstrations. +# ------------------------------------------------------------------------------ +# Contains a GUI-specific function and constant. +# Define a constant +my_id = 3 + + +def func3(): + print('func3() from submodule1.py with my_id =', my_id) diff --git a/examples/A10_modules/import_absolute.py b/examples/A10_modules/import_absolute.py new file mode 100644 index 0000000..f98d91f --- /dev/null +++ b/examples/A10_modules/import_absolute.py @@ -0,0 +1,16 @@ +# Shows how to enforce and use absolute imports. +# ------------------------------------------------------------------------------ +# Example script demonstrating absolute imports. +from __future__ import absolute_import +# Using absolute imports +import asyncio + +# Absolute imports +from foo.api.submodule1 import func1 +from foo.core.submodule2 import func2 +from foo.gui.submodule3 import func3 + +# Call all functions +func1() +func2() +func3() diff --git a/examples/A10_modules/import_relative.py b/examples/A10_modules/import_relative.py new file mode 100644 index 0000000..6fac139 --- /dev/null +++ b/examples/A10_modules/import_relative.py @@ -0,0 +1,11 @@ +# Shows how to use relative imports within a package. +# ----------------------------------------------------------------------------- +# Demonstrates importing modules from the current package using relative syntax. + +from .foo.api.submodule1 import func1 +from .foo.core.submodule2 import func2 +# Relative imports are scoped to packages. + +# Call the imported functions +func1() +func2() diff --git a/examples/A21_import_system/import_absolute.py b/examples/A21_import_system/import_absolute.py index 9e10644..f6855bc 100644 --- a/examples/A21_import_system/import_absolute.py +++ b/examples/A21_import_system/import_absolute.py @@ -1,10 +1,8 @@ # Shows how to enforce and use absolute imports. # ------------------------------------------------------------------------------ # Example script demonstrating absolute imports. -from __future__ import absolute_import -# Using absolute imports -import asyncio +from __future__ import absolute_import # Absolute imports from foo.api.submodule1 import func1