- Debugging can be done by
import pdb pdb.breakpoint()orpdb.set_trace()in the codepython -m pdb my.pyfor execution
- Either use
black,pylintormypy pyling my.pyto generate detailed report about potential issues, coding convention violations and more- You can built in
profileandsnakevizto generate profiling results and listing them in a webbrowser- Or package
line_profiler - Or package
memory_profiler
- Or package
- Decorators are functions that are wrapping another function call
- Decorators have to support one parameter which is the original called function
ref: https://docs.python.org/3/tutorial/modules.html#tut-modules A module is a file containing Python definitions and statements.
ref: https://www.freecodecamp.org/news/how-to-build-a-singleton-in-python-and-why-you-probably-shouldnt/ Import a module, and you get the same instance every time. [...] when you import a module, Python executes it once and caches the result in sys.modules. Every subsequent import returns the cached module object, not a new one.
ref: https://docs.python.org/3/tutorial/modules.html#packages Packages are a way of structuring Python’s module namespace by using “dotted module names”. ... The
__init__.pyfiles are required to make Python treat directories containing the file as packages.
# ref: https://fastapi.tiangolo.com/tutorial/bigger-applications/
.
├── app # "app" is a Python package
│ ├── __init__.py # this file makes "app" a "Python package"
│ ├── main.py # "main" module, e.g. import app.main
│ ├── dependencies.py # "dependencies" module, e.g. import app.dependencies
│ └── routers # "routers" is a "Python subpackage"
│ │ ├── __init__.py # makes "routers" a "Python subpackage"
│ │ ├── items.py # "items" submodule, e.g. import app.routers.items
│ │ └── users.py # "users" submodule, e.g. import app.routers.users
│ └── internal # "internal" is a "Python subpackage"
│ ├── __init__.py # makes "internal" a "Python subpackage"
│ └── admin.py # "admin" submodule, e.g. import app.internal.admin- The
appdirectory contains everything- It has an empty file
app/__init__.py, so it is a "Python package" (a collection of "Python modules"):app - It contains an
app/main.pyfile. As it is inside a Python package (a directory with a file__init__.py), it is a module of that package:app.main
- It has an empty file
- There's also an
app/dependencies.pyfile, just likeapp/main.py, it is a module:app.dependencies - There's a subdirectory
app/routers/with another file__init__.py, so it's a Python subpackage:app.routers - The file
app/routers/items.pyis inside a package,app/routers/, so, it's a submodule:app.routers.items - The same with
app/routers/users.py, it's another submodule:app.routers.users - There's also a subdirectory
app/internal/with another file__init__.py, so it's another Python subpackage:app.internal - And the file
app/internal/admin.pyis another submodule:app.internal.admin
import routers.items # This imports the module namespace into your file
from routers.items import BaseItemClass # This imports names from the module directly into your local namespace
# This does not introduce the module name into your local namespace, routers is not defined
# To call a method fomr a module namespace, you have to call it with the module namespace
users.create(name="Max Power") # Assuming module users has a function called create- Dockstring Convetions: python.org - 20230425
- Learning Python: python-guide.org - 20230424
- Python For Beginners: python.org - 20230424
- Python debuggung handbook: freecodecamp.org - 20240126
- Secure Coding Guide for Python: openssf.org - 20260313
- Style Guide - 20230425
- The Python Decorator Handbook: freecodecamp.org - 20240129
- The Python Tutorial: python.org - 20230424
- What is new in Python 3.8: python.org - 20230425
- What is new in Python 3.9: python.org - 20230425
- What is new in Python 3.10: python.org - 20230425
- What is new in Python 3.11: python.org - 20230425
- What is new in Python 3.12: python.org - 20240129