Rico's Nerd Cluster

「离开世界之前 一切都是过程」

Python - Data Structures

Dict

Dictionary setdefault if value does exist, return the existing value; Otherwise, set the key with a new value. 1 2 3 4 5 counts = {} value = counts.setdefault("apples", 0) print(value) # 0...

Python - Python Subprocessing

Subprocess

Subprocess Module Reference The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle. For more advanced use cases, the underlying Popen inter...

Python - Python Packaging System

Packaging, Pip

Setuptools Entrypoints Entrypoints are a way to specify components in your package to the rest of the python space, so they can be dynamically discovered. So this enables: Console scripts (co...

Python - Poetry Packaging System and Pip

Poetry, Pip

Introduction Using Poetry is highly recommended because it simplifies dependency management and packaging, even when not using virtual environments. Environments without Poetry can install poetry ...

Python - How To Write Pytest

Pytest Is The New Black (Compared To Unittest)

Run Pytest Run a specific test file pytest path/to/test_file.py Run a test in it: pytest path/to/test_file.py::test_function_name Assert For integer assertions: 1 assert (1==1) ...

Python - Functools

lru_cache

lru_cache One subtle but very real bug when using functools.lru_cache with mutable objects is cache poisoning caused by mutability. The cache stores and returns the exact same object references ...

Python - Immutable And Memory View

Mutable vs Immutable Objects Python objects are either mutable or immutable: Mutable Immutable bytearray, list, dict, set str, bytes, int, tuple ...

Python - Python Mixin Classes

MRO

Mixins A mixin is a small, focused class that provides methods to be “mixed in” to other classes via multiple inheritance. Mixins let you bundle reusable behaviors and add them to any class, keepi...

Python - File Management

os

File Path Processing os.path.basename("/home/foo/Downloads/000000.pcd") gives the string after the last “/”. So here we get: 000000.pcd os.path.splitext("/home/foo/Downloads/000000.pcd") give...

Python - Enums, dataclass

Enum, IntEnum

IntEnum IntEnum’s members are ints, while enum instance’s members are its own class Enum: Base class for creating enumerated constants. IntEnum: Base class for creating enumera...