Python
/
Goodies
- 1 Language 9
-
Hello World S
-
Variables S
-
Functions S
-
Conditional A S
-
Operators S
-
While S
-
Turtle S
-
Script Mode S
-
Debugging S
- 2 Strings 7
-
Slice S
-
Raw Strings S
-
Regex A S
-
Validation S
-
Config S
-
Security S
-
Encrypt A S
- 3 Collections 5
-
Lists S
-
Dictionaries S
-
Efficiency S
-
Tuples S
-
References S
- 4 Functions 5
-
Recursion S
-
Factorial S
-
Modulus S
-
Reassignment S
-
Approximate S
- 5 Storage 8
-
Files S
-
Databases S
-
Pipes S
-
With open S
-
Shelve A S
-
Zip S
-
Csv S
-
Json S
- 6 Class 4
-
Definition S
-
Attributes S
-
Functional S
-
Methods S
- 7 Goodies 5
-
Conditional Expression S
-
List Comprehension A S
-
Generator S
-
Named Tuple S
-
Modules S
- 8 Applications 5
-
Pythagora A S
-
Palindrome A S
-
Binary Search A S
-
Conway Game A S
-
Coin Flip A S
- 9 Scheduler 4
-
Time S
-
Multithreading A S
-
Subprocess S
-
Logging S
- 10 Packages 6
-
Clipboard A S
-
Ocr A S
-
Socket S
-
Image S
-
Virtualenv S
-
Jupyter S
S
R
Q
Python Goodies Modules
A module is any file that contain Python code Module test code, not used after import from mymodule import mysum myprint(mysum(1,2))
Modules
p256 A module is any file that contain Python code.
"""Define a module:
When you import a module, it defines new functions, but doesn't run them.
If the program is running as a script, the test code runs.
If module is imported the test code is skipped.
"""
DIR = "/var/www/"
def myprint(str):
print('Print from my module: %s' % str)
print(DIR)
def mysum(a, b):
return a + b
assert mysum(1,2) == 3
assert mysum(-1,2) == 1
"""Test code"""
if __name__ == '__main__':
print('Load my module ' + __name__)
print(DIR)
# /var/www/
Import
You can import the new module in your programs.
"""Import module:
Any file that contains Python code can be imported as module.
Test code (defined in mymodule) is not displayed.
.pyc files are created automatically by the GraalVM Python runtime ...
when you import custom mdules.
"""
import sys
sys.dont_write_bytecode = True # no .pyc
import mymodule
mymodule.DIR = "/usr/local"
mymodule.myprint('Hello World')
# Print from my module: Hello World: __main__
# /usr/local
from mymodule import mysum
from mymodule import myprint
myprint(mysum(1,2))
# Print from my module: 3
# /usr/local
➥ Questions
Last update: 104 days ago