Skip to content

Commit 57541f3

Browse files
committed
Refactor import examples into modules section
1 parent 31fe8f3 commit 57541f3

File tree

10 files changed

+91
-0
lines changed

10 files changed

+91
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Package demonstrating relative imports and an export decorator.
2+
# ------------------------------------------------------------------------------
3+
# Shows relative imports and how to define __all__.
4+
# Define the packet API
5+
__version__ = '1.0.0'
6+
7+
# The dot(.) operator is used to import modules relative to the current package.
8+
# A single dot (.) refers to the current package.
9+
# Two dots (..) refer to the parent package.
10+
# Three dots (...) refer to the grandparent package, and so on.
11+
12+
from .api.submodule1 import func1
13+
from .core.submodule2 import func2
14+
from .gui.submodule3 import func3
15+
16+
17+
# The __all__ variable is used to define the public API of a module or a package.
18+
__all__ = ['func1', 'func2', 'func3']
19+
20+
21+
# Export decorator
22+
def export(defn):
23+
24+
# Add the object to the global namespace
25+
globals()[defn.__name__] = defn
26+
27+
# Set the object to be exported
28+
__all__.append(defn.__name__)
29+
30+
# Return the object
31+
return defn
32+
33+
34+
# Demonstration of the export decorator
35+
@export
36+
def func4():
37+
print('func4')
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Entry point executed when running the package with -m.
2+
# ------------------------------------------------------------------------------
3+
# Allows running the package directly as a script.
4+
import sys
5+
import os.path
6+
7+
8+
def main():
9+
progname = sys.argv[0]
10+
sys.argv[:] = sys.argv[1:]
11+
sys.path.insert(0, os.path.dirname(progname))
12+
13+
print('sys.argv =', sys.argv)
14+
15+
16+
if __name__ == "__main__":
17+
main()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# API subpackage exposing a version constant.
2+
# ------------------------------------------------------------------------------
3+
# Holds version information for the API.
4+
__version__ = '0.1.0'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Submodule used to demonstrate absolute and relative imports.
2+
# ------------------------------------------------------------------------------
3+
# Provides a function and constant for tests.
4+
# Define a constant
5+
my_id = 1
6+
7+
8+
def func1():
9+
print('func1() from submodule1.py with my_id =', my_id)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Core subpackage used in import demonstrations.
2+
# ------------------------------------------------------------------------------
3+
# Core components used by the package.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Submodule used in absolute import demonstrations.
2+
# ------------------------------------------------------------------------------
3+
# Supplies a second function and constant.
4+
# Define a constant
5+
my_id = 2
6+
7+
8+
def func2():
9+
print('func2() from submodule2.py with my_id =', my_id)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# GUI subpackage used in import demonstrations.
2+
# ------------------------------------------------------------------------------
3+
# GUI layer that references the other submodules.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Submodule used by the GUI package for import demonstrations.
2+
# ------------------------------------------------------------------------------
3+
# Contains a GUI-specific function and constant.
4+
# Define a constant
5+
my_id = 3
6+
7+
8+
def func3():
9+
print('func3() from submodule1.py with my_id =', my_id)

0 commit comments

Comments
 (0)