-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunit4_lesson_02_understanding_packages.py
123 lines (89 loc) · 4.03 KB
/
unit4_lesson_02_understanding_packages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
__author__ = 'Kalyan'
notes = '''
Sometimes a collection of modules provides related functionality as part of a larger framework,
then it makes sense to group all of them together. Packages allows you to group related modules together.
The relationship between packages and modules is similar to that of directories and files in the
filesystem. Packages can contain sub-packages and modules.
A module with name a.b.c is saying that c is a module in package b which is a sub-package of package a.
'''
from placeholders import *
import sys
# Look at the package1 and package2 directories before starting...
def test_package_basic_import():
clear_sys_modules()
print("sys.path value", sys.path) # note this value in the test output.
assert False == ("package1" in locals())
assert False == ("module1" in locals())
assert False == ("package1.module1" in locals())
import package1
assert True == ("package1" in locals())
assert False == ("module1" in locals())
assert False == ("package1.module1" in locals())
assert "module" == type(package1).__name__
assert True == ("package1" in sys.modules)
assert False == ("module1" in sys.modules)
assert False == ("package1.module1" in sys.modules)
try:
print(package1.module1.__doc__)
except AttributeError as ie :
print(ie)
#modules need explicit import generally.
import package1.module1
print(package1.module1.__doc__)
assert True == ("package1" in sys.modules)
assert False == ("module1" in sys.modules)
assert True == ("package1.module1" in sys.modules)
def clear_sys_modules():
sys.modules.pop("module1", None)
sys.modules.pop("package1", None)
sys.modules.pop("package1.module1", None)
sys.modules.pop("package1.subpackage", None)
sys.modules.pop("package1.subpackage.m1", None)
sys.modules.pop("package2", None)
sys.modules.pop("package2.module2", None)
def test_package_from_import():
clear_sys_modules()
assert False == ("package1" in locals())
assert False == ("module1" in locals())
assert False == ("package1.module1" in locals())
from package1 import module1
assert False == ("package1" in locals())
assert True == ("module1" in locals())
assert False == ("package1.module1" in locals())
assert True == ("package1" in sys.modules)
assert False == ("module1" in sys.modules)
assert True == ("package1.module1" in sys.modules)
# https://docs.python.org/3/glossary.html#term-package
def test_package_import_namespacepackage():
clear_sys_modules()
# package 2 directory has no __init__.py still it imports as
# a package in python 3.4+. These are called namespace packages will package1 will be called a regular package.
import package2
assert True == ("package2" in sys.modules )
from package2 import module2
assert True== ("package2.module2" in sys.modules )
def test_package_sub_packages():
clear_sys_modules()
assert False == ("package1" in locals())
assert False == ("subpackage" in locals())
assert False == ("package1.subpackage" in locals())
from package1 import subpackage
from package1.subpackage import m1
assert False == ("package1" in locals())
assert True == ("subpackage" in locals())
assert False == ("package1.subpackage" in locals())
assert True == ("package1" in sys.modules)
assert False == ("module1" in sys.modules)
assert False == ("package1.module1" in sys.modules)
assert True == ("package1.subpackage" in sys.modules)
assert True == ("package1.subpackage.m1" in sys.modules)
#why is this not raising an exception here?
print(subpackage.m1.__doc__)
assert True == ("package1.subpackage.m1" in sys.modules)
three_things_i_learnt = """
-package is just a module
-in two ways we can import modules in a package that is:
1.like package1.module1
2.from package1 import module1
-a package can inturn have subpackages
"""