# Отчет по теме 8 Махнов Георгий, А-01-23 ## Тема 8. Модули и структурирование программы ## 2. Создание и использование модулей в среде Python. ## 2.1. Запуск модуля на выполнение путем его импорта. ```py >>> import os,sys,imp __main__:1: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses >>> os.chdir("C:\\Users\\gmack\\OneDrive\\Рабочий стол\\3 KURS\\LABS\\python-labs\\TEMA8") >>> import Mod1 Mod1:Введите значение = 5 Mod1:Значение perm1= 5 >>> Mod1.perm1 '5' >>> import Mod1 >>> imp.reload(Mod1) Mod1:Введите значение = 3 Mod1:Значение perm1= 3 >>> Mod1.perm1 '3' ``` ## 2.2. Импортированные модули заносятся в словарь – значение атрибута sys.modules. ```py >>> print(sorted(sys.modules.keys())) ['Mod1', '__main__', '_abc', '_bootlocale', '_codecs', '_collections', '_collections_abc', '_frozen_importlib', '_frozen_importlib_external', '_functools', '_heapq', '_imp', '_io', '_locale', '_operator', '_signal', '_sitebuiltins', '_socket', '_sre', '_stat', '_thread', '_warnings', '_weakref', '_weakrefset', '_winapi', 'abc', 'atexit', 'builtins', 'codecs', 'collections', 'collections.abc', 'contextlib', 'copyreg', 'encodings', 'encodings.aliases', 'encodings.cp1251', 'encodings.latin_1', 'encodings.utf_8', 'enum', 'errno', 'functools', 'genericpath', 'heapq', 'imp', 'importlib', 'importlib._bootstrap', 'importlib._bootstrap_external', 'importlib.abc', 'importlib.machinery', 'importlib.util', 'io', 'itertools', 'keyword', 'linecache', 'marshal', 'math', 'mpl_toolkits', 'msvcrt', 'nt', 'ntpath', 'operator', 'os', 'os.path', 'platform', 'pywin32_bootstrap', 'pywin32_system32', 're', 'reprlib', 'select', 'selectors', 'signal', 'site', 'socket', 'sre_compile', 'sre_constants', 'sre_parse', 'stat', 'subprocess', 'sys', 'threading', 'time', 'token', 'tokenize', 'traceback', 'types', 'warnings', 'winreg', 'zipimport', 'zope'] >>> sys.modules.pop('Mod1') >>> print(sorted(sys.modules.keys())) ['__main__', '_abc', '_bootlocale', '_codecs', '_collections', '_collections_abc', '_frozen_importlib', '_frozen_importlib_external', '_functools', '_heapq', '_imp', '_io', '_locale', '_operator', '_signal', '_sitebuiltins', '_socket', '_sre', '_stat', '_thread', '_warnings', '_weakref', '_weakrefset', '_winapi', 'abc', 'atexit', 'builtins', 'codecs', 'collections', 'collections.abc', 'contextlib', 'copyreg', 'encodings', 'encodings.aliases', 'encodings.cp1251', 'encodings.latin_1', 'encodings.utf_8', 'enum', 'errno', 'functools', 'genericpath', 'heapq', 'imp', 'importlib', 'importlib._bootstrap', 'importlib._bootstrap_external', 'importlib.abc', 'importlib.machinery', 'importlib.util', 'io', 'itertools', 'keyword', 'linecache', 'marshal', 'math', 'mpl_toolkits', 'msvcrt', 'nt', 'ntpath', 'operator', 'os', 'os.path', 'platform', 'pywin32_bootstrap', 'pywin32_system32', 're', 'reprlib', 'select', 'selectors', 'signal', 'site', 'socket', 'sre_compile', 'sre_constants', 'sre_parse', 'stat', 'subprocess', 'sys', 'threading', 'time', 'token', 'tokenize', 'traceback', 'types', 'warnings', 'winreg', 'zipimport', 'zope'] >>> import Mod1 Mod1:Введите значение = 5 Mod1:Значение perm1= 5 >>> print(sorted(sys.modules.keys())) ['Mod1', '__main__', '_abc', '_bootlocale', '_codecs', '_collections', '_collections_abc', '_frozen_importlib', '_frozen_importlib_external', '_functools', '_heapq', '_imp', '_io', '_locale', '_operator', '_signal', '_sitebuiltins', '_socket', '_sre', '_stat', '_thread', '_warnings', '_weakref', '_weakrefset', '_winapi', 'abc', 'atexit', 'builtins', 'codecs', 'collections', 'collections.abc', 'contextlib', 'copyreg', 'encodings', 'encodings.aliases', 'encodings.cp1251', 'encodings.latin_1', 'encodings.utf_8', 'enum', 'errno', 'functools', 'genericpath', 'heapq', 'imp', 'importlib', 'importlib._bootstrap', 'importlib._bootstrap_external', 'importlib.abc', 'importlib.machinery', 'importlib.util', 'io', 'itertools', 'keyword', 'linecache', 'marshal', 'math', 'mpl_toolkits', 'msvcrt', 'nt', 'ntpath', 'operator', 'os', 'os.path', 'platform', 'pywin32_bootstrap', 'pywin32_system32', 're', 'reprlib', 'select', 'selectors', 'signal', 'site', 'socket', 'sre_compile', 'sre_constants', 'sre_parse', 'stat', 'subprocess', 'sys', 'threading', 'time', 'token', 'tokenize', 'traceback', 'types', 'warnings', 'winreg', 'zipimport', 'zope'] >>> sys.modules.pop('Mod1') >>> print(sorted(sys.modules.keys())) ['__main__', '_abc', '_bootlocale', '_codecs', '_collections', '_collections_abc', '_frozen_importlib', '_frozen_importlib_external', '_functools', '_heapq', '_imp', '_io', '_locale', '_operator', '_signal', '_sitebuiltins', '_socket', '_sre', '_stat', '_thread', '_warnings', '_weakref', '_weakrefset', '_winapi', 'abc', 'atexit', 'builtins', 'codecs', 'collections', 'collections.abc', 'contextlib', 'copyreg', 'encodings', 'encodings.aliases', 'encodings.cp1251', 'encodings.latin_1', 'encodings.utf_8', 'enum', 'errno', 'functools', 'genericpath', 'heapq', 'imp', 'importlib', 'importlib._bootstrap', 'importlib._bootstrap_external', 'importlib.abc', 'importlib.machinery', 'importlib.util', 'io', 'itertools', 'keyword', 'linecache', 'marshal', 'math', 'mpl_toolkits', 'msvcrt', 'nt', 'ntpath', 'operator', 'os', 'os.path', 'platform', 'pywin32_bootstrap', 'pywin32_system32', 're', 'reprlib', 'select', 'selectors', 'signal', 'site', 'socket', 'sre_compile', 'sre_constants', 'sre_parse', 'stat', 'subprocess', 'sys', 'threading', 'time', 'token', 'tokenize', 'traceback', 'types', 'warnings', 'winreg', 'zipimport', 'zope'] ``` ## 2.3. Запуск модуля на выполнение с помощью функции exec(). ```py >>> exec(open('Mod1.py').read()) Mod1:Введите значение = 5 Mod1:Значение perm1= 5 >>> exec(open('Mod1.py').read()) Mod1:Введите значение = 3 Mod1:Значение perm1= 3 >>> perm1 '3' >>> exec(open('Mod1.py').read()) Mod1:Введите значение = 6 Mod1:Значение perm1= 6 >>> perm1 '6' >>> exec(open('Mod1.py').read()) Mod1:Введите значение = 9 Mod1:Значение perm1= 9 >>> perm1 '9' ``` ## 2.4. Использование инструкции from … import … Пример 1. ```py >>> from Mod1 import perm1 Mod1:Введите значение = 3 Mod1:Значение perm1= 3 >>> perm1 '3' >>> print(sorted(sys.modules.keys())) ['Mod1', '__main__', '_abc', '_bootlocale', '_codecs', '_collections', '_collections_abc', '_frozen_importlib', '_frozen_importlib_external', '_functools', '_heapq', '_imp', '_io', '_locale', '_operator', '_signal', '_sitebuiltins', '_socket', '_sre', '_stat', '_thread', '_warnings', '_weakref', '_weakrefset', '_winapi', 'abc', 'atexit', 'builtins', 'codecs', 'collections', 'collections.abc', 'contextlib', 'copyreg', 'encodings', 'encodings.aliases', 'encodings.cp1251', 'encodings.latin_1', 'encodings.utf_8', 'enum', 'errno', 'functools', 'genericpath', 'heapq', 'imp', 'importlib', 'importlib._bootstrap', 'importlib._bootstrap_external', 'importlib.abc', 'importlib.machinery', 'importlib.util', 'io', 'itertools', 'keyword', 'linecache', 'marshal', 'math', 'mpl_toolkits', 'msvcrt', 'nt', 'ntpath', 'operator', 'os', 'os.path', 'platform', 'pywin32_bootstrap', 'pywin32_system32', 're', 'reprlib', 'select', 'selectors', 'signal', 'site', 'socket', 'sre_compile', 'sre_constants', 'sre_parse', 'stat', 'subprocess', 'sys', 'threading', 'time', 'token', 'tokenize', 'traceback', 'types', 'warnings', 'winreg', 'zipimport', 'zope' ``` Mod1 появился в памяти. Программа вызвалась из модуля. perm1 имеет заданное значение 3. Пример 2. ```py >>> from Mod2 import beta >>> g = beta(2) ****BETA**** >>> g 535.4916555247646 >>> print(sorted(sys.modules.keys())) ['Mod1', 'Mod2', '__main__', '_abc', '_bootlocale', '_codecs', '_collections', '_collections_abc', '_frozen_importlib', '_frozen_importlib_external', '_functools', '_heapq', '_imp', '_io', '_locale', '_operator', '_signal', '_sitebuiltins', '_socket', '_sre', '_stat', '_thread', '_warnings', '_weakref', '_weakrefset', '_winapi', 'abc', 'atexit', 'builtins', 'codecs', 'collections', 'collections.abc', 'contextlib', 'copyreg', 'encodings', 'encodings.aliases', 'encodings.cp1251', 'encodings.latin_1', 'encodings.utf_8', 'enum', 'errno', 'functools', 'genericpath', 'heapq', 'imp', 'importlib', 'importlib._bootstrap', 'importlib._bootstrap_external', 'importlib.abc', 'importlib.machinery', 'importlib.util', 'io', 'itertools', 'keyword', 'linecache', 'marshal', 'math', 'mpl_toolkits', 'msvcrt', 'nt', 'ntpath', 'operator', 'os', 'os.path', 'platform', 'pywin32_bootstrap', 'pywin32_system32', 're', 'reprlib', 'select', 'selectors', 'signal', 'site', 'socket', 'sre_compile', 'sre_constants', 'sre_parse', 'stat', 'subprocess', 'sys', 'threading', 'time', 'token', 'tokenize', 'traceback', 'types', 'warnings', 'winreg', 'zipimport', 'zope'] >>> alpha() Traceback (most recent call last): File "", line 1, in NameError: name 'alpha' is not defined >>> from Mod2 import alpha as al >>> al() ****ALPHA**** Значение t=3 '3' >>> del al, beta >>> from Mod2 import alpha as al, beta as bt >>> del al, bt >>> from Mod2 import * >>> tt=alpha() ****ALPHA**** Значение t=0.12 >>> uu=beta(float(tt)) ****BETA**** >>> uu 1.4578913609506803 ``` ## 3. Создание многомодульных программ. ## 3.1. Пример простой многомодульной программы. ```py >>> import Mod0 perm1= 3 ****ALPHA**** Значение t=3 tt= 3 ****BETA**** qq= 12391.647807916694 >>> Mod0.tt;Mod0.qq;Mod0.Mod1.perm1 '3' 12391.647807916694 '3' ``` ## 3.2. Еще пример. ```py >>> import MM0 k1,T,k2,Xm,A,F,N=8,5,3,10,2,0.5,1000 y= [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1.0183086292055208, 0, 26.39885775889784, -36.65029553691161, -34.19982663883278, 196.29963397615063, -151.6919482160481, -388.32493988337274, 1057.8073200868555, -308.3186572590445, -2798.051869998873, 5004.749701095182, 1362.331454336744, -17303.76245797908, 20708.797073656922, 23131.712847291765, -96666.92589990808,...] ``` ## 3.3. Области действия объектов в модулях. ```py >>> from Mod2 import alpha as al, beta as bt >>> al(bt()) Traceback (most recent call last): File "", line 1, in TypeError: beta() missing 1 required positional argument: 'q' >>> bt(al()) ****ALPHA**** Значение t=3 ****BETA**** Traceback (most recent call last): File "", line 1, in File "C:\Users\gmack\OneDrive\Рабочий стол\3 KURS\LABS\python-labs\TEMA8\Mod2.py", line 9, in beta expi=q*math.pi TypeError: can't multiply sequence by non-int of type 'float' >>> t Traceback (most recent call last): File "", line 1, in NameError: name 't' is not defined >>> expi Traceback (most recent call last): File "", line 1, in NameError: name 'expi' is not defined >>> import Mod0 perm1= 55 ****ALPHA**** Значение t=3 tt= 33 ****BETA**** qq= 24783.295615833387 ```