Skip to content

Commit 7ccc5f3

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 79080af + 776e1a2 commit 7ccc5f3

File tree

11 files changed

+119
-3
lines changed

11 files changed

+119
-3
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)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Shows how to enforce and use absolute imports.
2+
# ------------------------------------------------------------------------------
3+
# Example script demonstrating absolute imports.
4+
from __future__ import absolute_import
5+
# Using absolute imports
6+
import asyncio
7+
8+
# Absolute imports
9+
from foo.api.submodule1 import func1
10+
from foo.core.submodule2 import func2
11+
from foo.gui.submodule3 import func3
12+
13+
# Call all functions
14+
func1()
15+
func2()
16+
func3()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Shows how to use relative imports within a package.
2+
# -----------------------------------------------------------------------------
3+
# Demonstrates importing modules from the current package using relative syntax.
4+
5+
from .foo.api.submodule1 import func1
6+
from .foo.core.submodule2 import func2
7+
# Relative imports are scoped to packages.
8+
9+
# Call the imported functions
10+
func1()
11+
func2()

0 commit comments

Comments
 (0)