Skip to content

Latest commit

 

History

History

python.d

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

python

Debugging

  • Debugging can be done by import pdb
  • pdb.breakpoint() or pdb.set_trace() in the code
  • python -m pdb my.py for execution

Linting and performance profiling

  • Either use black, pylint or mypy
  • pyling my.py to generate detailed report about potential issues, coding convention violations and more
  • You can built in profile and snakeviz to generate profiling results and listing them in a webbrowser
    • Or package line_profiler
    • Or package memory_profiler

Decorators

  • Decorators are functions that are wrapping another function call
  • Decorators have to support one parameter which is the original called function

Module, packages and subpackages

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__.py files 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 app directory 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.py file. As it is inside a Python package (a directory with a file __init__.py), it is a module of that package: app.main
  • There's also an app/dependencies.py file, just like app/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.py is 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.py is 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

Links