Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions examples/A10_modules/foo/__init__.py
Original file line number Diff line number Diff line change
@@ -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')
17 changes: 17 additions & 0 deletions examples/A10_modules/foo/__main__.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 4 additions & 0 deletions examples/A10_modules/foo/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# API subpackage exposing a version constant.
# ------------------------------------------------------------------------------
# Holds version information for the API.
__version__ = '0.1.0'
9 changes: 9 additions & 0 deletions examples/A10_modules/foo/api/submodule1.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions examples/A10_modules/foo/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Core subpackage used in import demonstrations.
# ------------------------------------------------------------------------------
# Core components used by the package.
9 changes: 9 additions & 0 deletions examples/A10_modules/foo/core/submodule2.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions examples/A10_modules/foo/gui/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# GUI subpackage used in import demonstrations.
# ------------------------------------------------------------------------------
# GUI layer that references the other submodules.
9 changes: 9 additions & 0 deletions examples/A10_modules/foo/gui/submodule3.py
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 16 additions & 0 deletions examples/A10_modules/import_absolute.py
Original file line number Diff line number Diff line change
@@ -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()
11 changes: 11 additions & 0 deletions examples/A10_modules/import_relative.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 1 addition & 3 deletions examples/A21_import_system/import_absolute.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down