Skip to main content

Python 3

Check if object is of valid type

hasattr(object, var_or_method_name)
  • Arguments are an object and a string.
  • Recommended way compatible with duck typing.

Conda

Create virtual env with certain packages (passed as last arguments)

conda create -n venvName pip
  • Add python=3.x after venvName to specify interpreter version.

Remove entire virtual env

conda remove -n venvName --all

Update all packages in a virtual env

conda update --all --prefix /path/to/prefix

Display current settings

conda config --show

Add value to a setting

conda config --append envs_dirs /path

Remove value from a setting

conda config --remove envs_dirs /path

Clean cached files (may fix http errors)

conda clean --all

Update anaconda

conda update conda
conda update anaconda

Don't execute code inside if block when module is imported

if __name__ == '__main__':

Flask

Set app file (shell command)

export FLASK_APP=fileName.py

Start development webserver

flask run

Looping methods

Loop over a list while keeping track of indexes with enumerate

for index, name in enumerate(pokemons, start=1):
print("Pokemon {}: {}".format(index, name))
  • Using 1 as first index, 0 by default if not passed.

Loop over multiple lists at the same time with zip

for name, id in zip(names, ids):
print("Name: {}; ID: {}".format(name, id))

Unpack dict in a for loop

for key, value in person_dictionary.items():
print(f"Key {key} has value {value}")

Math

Division

Common division (always returns a float value)

/

Floor div (without reminder)

//

Multiple assignment

Assign first element of iterable to x, second to y

x, y = 'hi'
x, y = [10, 20]

Capture remaining items after unpacking. The two lines are equivalent

head, middle, last = numbers[0], numbers[1:-1], numbers[-1]

head, *middle, last = numbers

Pip

Install common packages used in development

pip install black flake8 mypy pytest

Install local package (from its root directory)

pip install .

Install package from a VCS URL

pip install git+git://github.com/sample/package

Install package in editable mode

pip install -e <path/url>
  • Changes to the source files will be available without needing to reinstall the package (i.e. setuptools "develop mode").

Install package in the user's home directory

pip install --user pkgName
  • By default packages are installed in a system directory that requires root access.

Install packages specified in requirements.txt

pip install -r requirements.txt

Update installed package

pip install -U pkgName

Pipenv

Create new venv associated with current dir using specified python version

pipenv --python 3.7

Generate a Pipfile.lock file

pipenv lock

Afterwards the created Pipfile and Pipfile.lock files can be commited into git and others can run the command below in the cloned repository to get the exact same environment.

Install package and add it to Pipfile

pipenv install [OPTIONS] [PACKAGES]
  • If no package names are given attempts to read packages to install from Pipfile.

Remove virtualenv (inferred from current directory)

pipenv --rm

Run a shell in the venv

pipenv shell

Run command in the venv

pipenv run command args
  • Environment variables can be passed, i. e. PYTHONPATH=../ pipenv run command.

Uninstall package

pipenv uninstall [OPTIONS] [PACKAGES]

Property notation

@property
def name(self):
return self.__name

@name.setter
def name(self, val):
self.__name = val

PyQt

  • Fedora packages: qt5-designer python-qt5.

After saving ui file with qt designer, convert it to python

pyuic5 gui.ui > gui.py

Then it can be imported into the python project.

Read command line input into var

path = input("Enter path and press enter: ")

Read environment variable (i.e. db credentials)

import os
env_var = os.getenv("VARNAME")

Simple HTTP server

python -m http.server
  • Useful for sharing files over the network if encryption is not needed. Run command from the directory containing target files.
  • Incoming traffic on port 8000 needs to be allowed in the firewall.

Slice notation

sequence = [1, 2, 3]
# First element.
sequence[0]

# Last element.
sequence[-1]

# Second last element.
sequence[-2]

# Tail of the sequence (starting after first or nth elem).
sequence[1:]

# Tail of the sequence (starting after last or nth elem).
sequence[:-1]

SQLAlchemy

Connect to database using independent sessions

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine("DATABASE_URL")
db = scoped_session(sessionmaker(bind=engine))

String formatting

name = "tst"
# Format string with format method.
print("hello, {}".format(name))

# Format string with prefix f (3.6+).
print(f"hello, {name}")

# Multi-line literal (newline can be escaped with \).
"""first line
second"""

Testing

Pytest

Run tests

pytest testsdir/
  • Requires PyPI package pytest.

Run tests with coverage report in a venv

python -m venv my_virtenv
source my_virtenv/bin/activate
pip install pytest-cov
pytest --cov-report term-missing --cov=srcdir/ testsdir/
  • Requires PyPI package pytest-cov.

Unittest

Run test module in terminal

python -m unittest -v test_module

Use discovery to find and run tests in dir

python -m unittest discover -v '/dir'
  • If dir name is not quoted, it will not work if it contains spaces.