🌱 Tim's Dev Wiki

Search IconIcon to open search

Python

Last updated October 10, 2022.

Python is a dynamically-typed high-level programming language whose design philosophy centres around readability and terseness. It’s used frequently for data science and machine learning, backend development, scripts and CLIs.

Code is sometimes described as pythonic, which just means it exploits Python’s language features and design to produce something very readable and elegant.


# Dunder Methods

TODO.

# Context Manager

with

TODO: see https://www.pythoncheatsheet.org/cheatsheet/context-manager.

# Virtual Environments

Always use a virtual environment. source

To prevent bloating the base Python installation with project dependencies and have reproducible/portable setups, use virtual environments.

1
2
3
4
5
6
pip install virtualenv
python -m venv ./venv     # Create a virtual environment in the new folder called `venv`.
source venv/bin/activate  # Use the virtual environment's Python installation.
deactivate                # Deactivate the current virtual environment.

pip freeze > requirements.txt   # Dumps all the current dependencies into `requirements.txt`.

# Import and Export

1
import foo
1
import foo.bar
1
2
3
4
5
from foo import bar, baz
from a import (            # Syntax for splitting a long from-import statement into multiple lines.
    b,
    c
)

# Relative Imports

Relative imports are generally discouraged since they’re less readable, less understood, and easy to break.

All relative imports are done with from _ import _. The import _ statement is always absolute.

1
2
from . import foo     # From the current package, import `foo`.
from .bar import baz  # From the `bar` module in the current package, import `baz`.

Having trouble? Some crucial details to note:

# __init__.py

The presence of this file (even if empty) indicates that the containing folder is a package, not a regular directory. This rule was dropped for subpackages, however, to improve developer experience.

Whatever you import inside __init__.py becomes accessible directly under the package name for consumers. Eg. in the example below, consumers can just do from foo import baz. This works a bit similarly to the index.js file exporting variables in JavaScript.

1
2
# foo/__init__.py
from foo.bar import baz

# Classes

Take notes from here: https://stackoverflow.com/questions/9056957/correct-way-to-define-class-variables-in-python

To make a member private, prefix its name with an underscore _. This doesn’t actually do anything, it’s just an agreed upon standard for documenting something should be private. There’s nothing stopping users of the class from invoking private methods.

TODO: @classmethod decorator. See https://stackoverflow.com/questions/54264073/what-is-the-use-and-when-to-use-classmethod-in-python

# Getter and Setter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Foo:
    def __init__(self):
        self._foo = ""

    @property
    def foo(self):
        """ Getter. """
        return self._foo

    @foo.setter
    def foo(self, new_foo):
        """ Setter. """
        self._foo = new_foo

# Type System

Python is dynamically-typed and will always stay that way, however you can still opt to add static types wherever you find it useful, like how you might mix JavaScript and TypeScript code. Static typing is massively helpful in large projects as opposed to one-off scripts.

# Type Hints

Type hints do nothing at runtime. You have to use a static type checker such as Mypy, or PyLance with VSCode, for example.

Typing variables and functions in Python is very similar to how it’s done in TypeScript.

1
2
3
4
5
def make_greeting(name: str, age: int = 42) -> str:
    return f"{name} {age}"

greeting: str = make_greeting("Andrew")    # Although the type can be inferred, annotating the type is still great for documentation.
print(greeting)

# typing Module

Python 3.5 supplies the typing built-in module brings in a lot of advanced static typing utilities such as those seen in TypeScript.

# Standard Built-In Modules

See standard built-in modules.

# File Manipulation

Python has built-in top-level functions for file manipulation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Reading.
my_file = open(filename, "r")
lines: List[str] = my_file.readlines()
my_file.close()

# Alternatively, using `with`:
with open(filename, "r") as my_file:
    lines: List[str] = my_file.readlines()

# Writing.
with open(filename, "w") as my_file:
    my_file.write("Hi.")
    my_file.writelines(["Hello", "World"])

# OS

os provides a bunch of useful functions for working with the filesystem.

1
2
3
4
5
os.getcwd()
os.path.exists(path)
os.path.isdir(path)
os.path.join(*path_fragments)   # Forms a complete path in a cross-OS way (since Windows uses backslash separators).
os.makedirs(path)                 # Like `mkdir -p`, which creates all non-existent directories along the path.

# Regex

See regex.

Use raw strings r"..." when specifying regex patterns to avoid being confused about what characters are being escaped.

1
2
3
4
5
regex = re.compile(r"...")
match = regex.search(haystack)  # `re.Match` object contains info about the search. If no match was found, then `match == None`.

# Equivalent to the above, but you can't reuse the compiled regex.
result = re.match(r"...", haystack)

Capture Groups:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
match = re.match(r"(\w+) (\w+)", "Linus Torvalds")

match[0]        # The original string, "Linus Torvalds".
match.group(0)  #   Equivalent to above.

match[1]        # First capture group, "Linus".
match.group(1)  #   Equivalent to above.

match[2]        # Second capture group, "Torvalds".
match.group(2)  #   Equivalent to above.

# Dates

Use datetime to work with dates. Use time for lower-level operations involving time.

Convert between strings and dates:

1
2
3
4
5
6
7
8
9
from datetime import datetime

# strptime: str → datetime
date_str = "2022-09-20"
date_obj = datetime.strptime(date_str, "%Y-%m-%d")

# strftime: datetime → str
date_obj = datetime.now()
date_str = date_obj.strftime("%Y-%m-%d")   # → YYYY-MM-DD

Date arithmetic and comparison: Use timedelta to add/subtract time from a date. You can directly use comparison operators on datetime objects.

1
2
3
4
5
6
from datetime import datetime, timedelta

today = datetime.now()
yesterday = today - timedelta(days=1)

assert(yesterday < today)