Python
Progamatically list installed packages
import pkg_resources
for package_name in [package.key for package in pkg_resources.working_set]:
print(f"{package_name}: {pkg_resources.get_distribution(package_name).version}")
Dataclass from dict
Source: https://stackoverflow.com/a/54769644/511976
def dataclass_from_dict(klass, d):
try:
fieldtypes = {f.name: f.type for f in fields(klass)}
return klass(**{f: dataclass_from_dict(fieldtypes[f], d[f]) for f in d})
except Exception:
return d # Not a dataclass field
Start an ad-hoc web server
Run:
List the loggers and make some of them shut up
# List loggers
loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict]
print(loggers)
# STFU Py4J
logging.getLogger("py4j.clientserver").setLevel(logging.ERROR)
Decorative Python
Primer on Python Decorators: https://realpython.com/primer-on-python-decorators/
Sync/Async
A simple walk-through of the interaction between sync and async: https://www.aeracode.org/2018/02/19/python-async-simplified
Installing pyenv in Windows
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"
Add <HOME_DIR>\.pyenv\pyenv-win\bin
to PATH
pyenv --version
to check if the installation was successful.- Run
pyenv install -l
to check a list of Python versions supported by pyenv-win - Run
pyenv install <version>
to install the supported version - Run
pyenv global <version>
to set a Python version as the global version -
Check which Python version you are using and its path
-
Check that Python is working