Skip to content

Instantly share code, notes, and snippets.

@tapyu
Last active August 25, 2025 16:27
Show Gist options
  • Select an option

  • Save tapyu/3e44aa7d087bf8f2673533f0da9cb3cf to your computer and use it in GitHub Desktop.

Select an option

Save tapyu/3e44aa7d087bf8f2673533f0da9cb3cf to your computer and use it in GitHub Desktop.
General Python snippets

General Python snippets

OBS: These snippets will not run correctly since it was taken from the code in the way it was written. Therefore, the variables do not convey any meaning. Use it just a guide and not a copy-and-paste reference.

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.6.8
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.11.2' # Use the sha / tag you want to point at
hooks:
- id: mypy
- repo: https://github.com/asottile/pyupgrade
rev: v3.17.0
hooks:
- id: pyupgrade
- repo: local
hooks:
- id: pep8-naming-convention
name: PEP 8 snake case naming convention
entry: File and dir names must follow PEP 8 snake case naming convention.
language: fail
files: '^(?![_a-z0-9/]+\.py$).*\.py'
- repo: local
hooks:
- id: pytest-check
name: pytest-check
entry: pytest
language: system
pass_filenames: false
always_run: true
# `mypy` configuration
# CAVEAT: you should add this to `pyproject.py` if you are using it in your project
[tool.mypy]
ignore_missing_imports = true
disable_error_code = ["import-untyped"]
# `pytest` configuration
# CAVEAT: you should add this to `pyproject.py` if you are using it in your project
[tool.pytest.ini_options]
markers = [
"unit: Mark a test as a unit test",
"integration: Mark a test as an integration test",
]
# `ruff` configuration
# CAVEAT: If `pyproject.py` is present, you should add this code to it instead of ruff.toml
[tool.ruff.lint]
select = [
"ANN", # `flake8-annotations` (ANN) - Type annotation checks (e.g., missing type annotations for functions and arguments)
"B", # `flake8-bugbear` (B) - common bugs and design issues
"C", # Pylint (PL) - Convention (C) - Complexity checks (e.g., McCabe complexity)
"D", # `pydocstyle` (D) - Docstring conventions (e.g., missing or incorrectly formatted docstrings)
"E", # Pylint (PL) - Error (E) - (e.g., syntax, whitespace)
"W", # Pylint (PL) - Warning (W) - Warnings for certain Python style guide deviations
"F", # Pyflakes (F) - Checks for undefined or unused variables (`flake8`-like warnings)
"I", # isort (I) - Import-related checks (e.g., checking for order or unused imports)
"Q", # `flake8-quotes` (Q) - Checks for quotes consistency (single vs. double quotes)
]
task-tags = ["TODO", "FIXME", "NOTE", "HACK", "CAVEAT", "BUG", "SEE", "TODOC", "???"] # add codetags (default is just "TODO", "FIXME", "XXX")
# McCabe complexity checker configuration.
[tool.ruff.lint.mccabe]
max-complexity = 10
# Pydocstyle convention for docstrings.
[tool.ruff.lint.pydocstyle]
convention = "numpy"
[tool.ruff.lint.pycodestyle]
ignore-overlong-task-comments = true # ignore lengthy lines finished with codetags
# Formatting settings.
[tool.ruff.format]
docstring-code-format = true
docstring-code-line-length = 20
import hashlib
import numpy as np
def md5_of_array(arr):
arr_c = np.ascontiguousarray(arr) # ensure memory layout consistency
return hashlib.md5(arr_c.tobytes()).hexdigest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment