Curs 5
Curs 5
Course 5
MODULES
Any Python code (python script) can be used as a module.
Python 3.x Python 3.x
File: MyModule.py File: test.py
def Sum(x,y): import MyModule Output
return x+y
print (MyModule.Sum(10,20)) 30
Both files test.py and MyModule.py are located in the same folder.
After the execution of test.py a folder with the name __pycache__ that contains a file
called MyModule.cpython-37.pyc will appear in the same folder (for Python 3.7) →
the version will be different for different versions of Python 3 (pyc = python compiled
files)
MODULES
Any Python code (python script) can be used as a module.
Python 3.x Python 3.x
File: MyModule.py File: test.py
def Sum(x,y): import MyModule
return x+y
print ("MyModule loaded") print (MyModule.Sum(10,20))
import MyModule
Loading a module will automatically execute any code (main code) that Output
resides in that module. MyModule loaded
The main code of a module (code that is written directly and not within a 30
function or a class) will only me executed once (the first time a module is
loaded).
MODULES
Any Python code (python script) can be used as a module.
Python 3.x Python 3.x
File: MyModule.py File: test.py
def Sum(x,y): import MyModule
return x+y
print ("MyModule loaded") print (MyModule.Sum(10,20))
import MyModule
import MyModule
print (MyModule.Sum(10,20))
import MyModule
In the above piece of code “<folder>” represents a path to the folder Output
where the file MyModule.py resides. MyModule loaded
30
MODULES
Any Python code (python script) can be used as a module.
Python 3.x Python 3.x
File: MyModule.py File: test.py
def Sum(x,y): import MyModule
return x+y
print ("MyModule loaded") print (dir (MyModule))
Output
['Sum', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__spec__']
MODULES
Any Python code (python script) can be used as a module.
Python 3.x Python 3.x
File: MyModule.py File: test.py
def Sum(x,y): import MyModule
return x+y
print ("MyModule loaded") print (MyModule.__file__)
print (MyModule.__name__)
print (MyModule.__package__)
Attributes:
o __file__ ➔ full path of the file that corresponds to the module (it could be a pyc file as well)
o __name__ ➔ name of the module (in this example : MyModule)
o __package__ ➔ name of the package
MODULES
Any Python code (python script) can be used as a module.
Python 3.x Python 3.x
File: MyModule.py File: test.py
def Sum(x,y): import MyModule as m
return x+y
__doc__ = "Computes the sum of two numbers" print (m.__doc__)
Output
Computes the sum of two numbers
MODULES
Alternatively, the keyword “help” can be used as well
Python 3.x Python 3.x
File: MyModule.py File: test.py
def Sum(x,y): import MyModule as m
return x+y
__doc__ = "Computes the sum of two numbers" help (m)
Output
Help on module MyModule:
NAME
MyModule - Computes the sum of two numbers
FUNCTIONS
Sum(x, y)
FILE
…\facultate\python_modules\mymodule.py
MODULES
Alternatively, the keyword “help” can be used as well
Python 3.x Python 3.x
File: MyModule.py File: test.py
def Sum(x,y): import MyModule as m
"""returns the sum of x plus y"""
return x+y help (m)
__doc__ = "Computes the sum of two numbers"
Output
Help on module MyModule:
NAME
MyModule - Computes the sum of two numbers
FUNCTIONS
Sum(x, y)
returns the sum of x plus y
FILE
e:\documente\facultate\python\2020-2021\mymodule.py
MODULES
Any Python code (python script) can be used as a module.
Python 3.x Python 3.x
File: MyModule.py File: test.py
def Sum(x,y): import MyModule
return x+y
print (__name__)
Output Output
__main__ MyModule
Output Output
Main code Module loaded
Testing sum(10,20) = 30
PACKAGES
Python scripts can also be grouped in packages. Packages must be grouped in folder,
and in each folder a __init__.py must exist. That file is an entry point for that
package/sub-package.
MathOps
__init__.py
Simple
__init__.py
Arithmetic.py
Bits.py
Complex
__init__.py
Series.py
PACKAGES
Python scripts can also be grouped in packages. Packages must be grouped in folder,
and in each folder a __init__.py must exist. That file is an entry point for that
package/sub-package.
MathOps Python 3.x
__init__.py File: __init__.py
Simple print ("Package MathOps init")
__init__.py
Arithmetic.py
Bits.py
Complex
__init__.py
Series.py
PACKAGES
Python scripts can also be grouped in packages. Packages must be grouped in folder,
and in each folder a __init__.py must exist. That file is an entry point for that
package/sub-package.
MathOps Python 3.x
__init__.py File: __init__.py
Simple print ("Package MathOps.Simple init")
__init__.py
Arithmetic.py
Bits.py
Complex
__init__.py
Series.py
PACKAGES
Python scripts can also be grouped in packages. Packages must be grouped in folder,
and in each folder a __init__.py must exist. That file is an entry point for that
package/sub-package.
MathOps Python 3.x
__init__.py File: Arithmetic.py
Simple def Add(x,y):
__init__.py return x+y
Arithmetic.py def Sub(x,y):
Bits.py return x-y
Complex
__init__.py
Series.py
PACKAGES
Python scripts can also be grouped in packages. Packages must be grouped in folder,
and in each folder a __init__.py must exist. That file is an entry point for that
package/sub-package.
MathOps Python 3.x
__init__.py File: Bits.py
Simple def SHL(x,y):
__init__.py return x << y
Arithmetic.py def SHR(x,y):
Bits.py return x >> y
Complex
__init__.py
Series.py
PACKAGES
Python scripts can also be grouped in packages. Packages must be grouped in folder,
and in each folder a __init__.py must exist. That file is an entry point for that
package/sub-package.
MathOps Python 3.x
__init__.py File: __init__.py
Simple print ("Package MathOps.Complex init")
__init__.py
Arithmetic.py
Bits.py
Complex
__init__.py
Series.py
PACKAGES
Python scripts can also be grouped in packages. Packages must be grouped in folder,
and in each folder a __init__.py must exist. That file is an entry point for that
package/sub-package.
MathOps Python 3.x
__init__.py File: Series.py
Simple def Sum(*p):
__init__.py c = 0
Arithmetic.py for i in p:
Bits.py c+= i
Complex return c
__init__.py def Product(*p):
Series.py c = 1
for i in p:
c *= i
return c
PACKAGES
Usage:
Python 3.x
import MathOps.Simple.Arithmetic
print (MathOps.Simple.Arithmetic.Add(2,3))
from MathOps.Simple import Arithmetic as a
print (a.Add(2,3))
Output
Package MathOps init
Package MathOps.Simple init
5
PACKAGES
Usage:
Python 3.x
from MathOps.Simple import *
print (Arithmetic.Add(2,3))
print (Bits.SHL(2,3))
Output
Package MathOps init
Package MathOps.Simple init
Traceback (most recent call last):
File “test.py", line 3, in <module>
print (Arithmetic.Add(2,3))
NameError: name 'Arithmetic' is not defined
PACKAGES
To be able to use a syntax similar to “from <module> import *” a module variable
“__all__” must be defined. That variable will hold a list of all modules that belongs to
that package.
MathOps Python 3.x
__init__.py File: __init__.py
Simple print ("Package MathOps.Simple init
__init__.py with __all__ set")
Arithmetic.py __all__ = ["Arithmetic","Bits"]
Bits.py
Complex
__init__.py
Series.py
PACKAGES
Usage:
Python 3.x
from MathOps.Simple import *
print (Arithmetic.Add(2,3))
print (Bits.SHL(2,3))
Output
Package MathOps init
Package MathOps.Simple init with __all__ set
5
16
MODULES/PACKAGES
If you want a module and/or package to be available to all the scripts that are
executed on that system just copy the module or the entire package folder on the
Python search path and you will be able to access it directly. These paths are:
o Windows: <PythonFolder>\Lib
Exemple: C:\Python27\Lib or C:\Python37\Lib
o Linux: /usr/lib/<PythonVersion>
Example: /usr/lib/python2.7 or /usr/lib/python3.7)
MODULES/PACKAGES
Python also has a special library (importlib) that can be used to dynamically import a
module.
o importlib.import_module (moduleName,package=None) ➔ to import a module
o importlib.reload (module) ➔ to reload a module that was already loaded
Python 3.x Python 3.x
File: C:\Python3\Lib\MyModule.py File: test.py
def Sum(x,y): import importlib
return x+y
print ("MyModule loaded") m = importlib.import_module("MyModule")
print (m.Sum(10,20))
Output
MyModule loaded
30
DYNAMIC CODE
Python has a keyword (exec) that can be used to dynamically compile and execute
python code.
The format is exec (code, [global],[local] ) where [global] and [local] represents a list
of global and local definition that should be used when executing the code.
Python 3.x
data = [0x65, 0x66, 0x67, 0x21, 0x54, 0x76, 0x6E, 0x62, 0x29, 0x79,
0x2D, 0x7A, 0x2D, 0x7B, 0x2A, 0x3B, 0x0E, 0x0B, 0x0A, 0x73,
0x66, 0x75, 0x76, 0x73, 0x6F, 0x21, 0x79, 0x2C, 0x7A, 0x2C,
0x7B]
s = ""
for i in data:
s += chr(i-1) Output
exec(s)
print(Suma(1,2,3)) 6
DYNAMIC CODE
Because of this keyword, python code can obfuscate or modify itself during runtime.
Python 3.x
data = [0x65, 0x66, 0x67, 0x21, 0x54, 0x76, 0x6E, 0x62, 0x29, 0x79,
0x2D, 0x7A, 0x2D, 0x7B, 0x2A, 0x3B, 0x0E, 0x0B, 0x0A, 0x73,
0x66, 0x75, 0x76, 0x73, 0x6F, 0x21, 0x79, 0x2C, 0x7A, 0x2C,
0x7B]
s = ""
for i in data: def Suma(x,y,z):
s += chr(i-1) return x+y+z Output
exec(s)
6
print(Suma(1,2,3))
DYNAMIC CODE
Multiple layers of encryption are also possible:
Python 3.x
buf =
[0x74,0x21,0x3E,0x21,0x23,0x67,0x68,0x69,0x5D,0x23,0x76,0x2B,0x64,0x2F,0x65,
0x2C,0x3D,0x5D,0x23,0x75,0x68,0x77,0x78,0x75,0x71,0x5D,0x23,0x64,0x2E,0x65,0
x23,0x0E,0x0B,0x74,0x33,0x21,0x3E,0x21,0x23,0x23,0x0E,0x0B,0x67,0x70,0x73,0x
21,0x64,0x21,0x6A,0x6F,0x21,0x74,0x3B,0x0E,0x0B,0x0A,0x74,0x33,0x21,0x2C,0x3
E,0x21,0x64,0x69,0x73,0x29,0x70,0x73,0x65,0x29,0x64,0x2A,0x2E,0x33,0x2A,0x0E
,0x0B,0x66,0x79,0x66,0x64,0x29,0x74,0x33,0x2A]
s = ""
for i in buf:
s += chr(i-1) Output
exec(s)
30
print(s(10,20))
DYNAMIC CODE
Multiple layers of encryption are also possible:
Python 3.x
buf =
[0x74,0x21,0x3E,0x21,0x23,0x67,0x68,0x69,0x5D,0x23,0x76,0x2B,0x64,0x2F,0x65,
0x2C,0x3D,0x5D,0x23,0x75,0x68,0x77,0x78,0x75,0x71,0x5D,0x23,0x64,0x2E,0x65,0
x23,0x0E,0x0B,0x74,0x33,0x21,0x3E,0x21,0x23,0x23,0x0E,0x0B,0x67,0x70,0x73,0x
21,0x64,0x21,0x6A,0x6F,0x21,0x74,0x3B,0x0E,0x0B,0x0A,0x74,0x33,0x21,0x2C,0x3
E,0x21,0x64,0x69,0x73,0x29,0x70,0x73,0x65,0x29,0x64,0x2A,0x2E,0x33,0x2A,0x0E
s = "fgh\"u*c.d+<\"tgvwtp\"c-d"
,0x0B,0x66,0x79,0x66,0x64,0x29,0x74,0x33,0x2A]
s2 = ""
s = "" for c in s:
for i in buf: s2 += chr(ord(c)-2)
s +=exec(s2)
chr(i-1) Output
exec(s)
30
print(s(10,20))
DYNAMIC CODE
Multiple layers of encryption are also possible:
Python 3.x
buf =
[0x74,0x21,0x3E,0x21,0x23,0x67,0x68,0x69,0x5D,0x23,0x76,0x2B,0x64,0x2F,0x65,
0x2C,0x3D,0x5D,0x23,0x75,0x68,0x77,0x78,0x75,0x71,0x5D,0x23,0x64,0x2E,0x65,0
x23,0x0E,0x0B,0x74,0x33,0x21,0x3E,0x21,0x23,0x23,0x0E,0x0B,0x67,0x70,0x73,0x
21,0x64,0x21,0x6A,0x6F,0x21,0x74,0x3B,0x0E,0x0B,0x0A,0x74,0x33,0x21,0x2C,0x3
E,0x21,0x64,0x69,0x73,0x29,0x70,0x73,0x65,0x29,0x64,0x2A,0x2E,0x33,0x2A,0x0E
s = "fgh\"u*c.d+<\"tgvwtp\"c-d"
,0x0B,0x66,0x79,0x66,0x64,0x29,0x74,0x33,0x2A]
s2 = ""
s = "" for c in s:
for i in buf: s2 += chr(ord(c)-2)
s +=exec(s2)
chr(i-1) Output
exec(s)
def s(a,b): return a+b 30
print(s(10,20))