Skip to content

Instantly share code, notes, and snippets.

@brettcannon
brettcannon / fluent.py
Created December 4, 2023 22:54
Provide a function that lets you use a fluent-style API when a method returns e.g. `None` instead of `self`
def fluent(bound_method, /, *args, **kwargs):
"""Call a bound method and return `self` from that method."""
bound_method(*args, **kwargs)
return bound_method.__self__
@brettcannon
brettcannon / metadata.py
Last active October 18, 2022 03:13
`RawMetadata` with caching functions to access normalized values
import functools
from typing import Any, Callable, Dict, Iterable, List, Optional, TypeVar
from .requirements import Requirement
from .specifiers import SpecifierSet
from .utils import NormalizedName, canonicalize_name
from .version import Version
_T = TypeVar("_T")
@brettcannon
brettcannon / metadata.py
Created October 16, 2022 18:00
`packaging.metadata` with lazy data validation
import pathlib
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
from .requirements import Requirement
from .specifiers import SpecifierSet
from .utils import NormalizedName, canonicalize_name
from .version import Version
class Metadata:
@brettcannon
brettcannon / back-in-my-day.py
Created November 26, 2021 22:31
Make Jeff feel better about Python packaging
"""Back in my day, we only needed **one** command. 🧓"""
import runpy
import sys
def run(command):
sys.argv[0] = command
runpy.run_module(command, run_name="__main__")
@brettcannon
brettcannon / config.site
Last active February 2, 2022 22:49
WASI-SDK w/ CPython
cross_compiling=yes
ac_cv_file__dev_ptmx=no
ac_cv_file__dev_ptc=no
@brettcannon
brettcannon / stdlib_modules.json
Created February 20, 2021 23:41
Stdlib modules
{
"__future__": [],
"_xxsubinterpreters": [],
"abc": ["_abc", "_py_abc"],
"aifc": [],
"antigravity": [],
"argparse": [],
"array": [],
"ast": ["_ast"],
"asynchat": [],
@brettcannon
brettcannon / pow_test.py
Created August 23, 2020 19:32
Demonstration of how **= does not follow the data model appropriately
class PowTest:
def __init__(self, name):
self.name = name
def __ipow__(self, _):
print(f"{self.name}.__ipow__")
return NotImplemented
def __pow__(self, _):
print(f"{self.name}.__pow__")
@brettcannon
brettcannon / stats_tables.sql
Last active June 26, 2020 22:37
Database schema for repo statistics
CREATE TABLE issues (
recording_date TEXT NOT NULL DEFAULT CURRENT_DATE,
gh_num INTEGER NOT NULL CHECK(gh_num >= 1),
url TEXT NOT NULL,
title TEXT NOT NULL,
created TEXT NOT NULL, -- Date of issue creation
by_team INT NOT NULL DEFAULT 0 CHECK(upvotes >= 0),
assignee TEXT NULL, -- Who the issue is assigned to
last_OP_comment TEXT NULL, -- When the last comment from the OP was
@brettcannon
brettcannon / steps.yml
Created January 24, 2020 22:56
GitHub Actions steps to cache VS Code stable when running extension tests
- name: Get VS Code versions
run: curl --output vscode-stable-versions.json https://update.code.visualstudio.com/api/releases/stable
- uses: actions/cache@v1
with:
path: .vscode-test/
key: ${{ runner.os }}-vscode-test-${{ hashFiles('vscode-stable-versions.json') }}
restore-keys: |
${{ runner.os }}-vscode-test-
@brettcannon
brettcannon / parse_wheel_filename.py
Created May 8, 2019 15:23
Parse a wheel filename using packaging.tags
import packaging.tags
def parse_wheel_filename(path):
name = os.path.splitext(_compat.fspath(path))[0]
index = len(name)
for _ in range(3): # interpreter, ABI, platform.
index = name.rindex("-", 0, index)
return packaging.tags.parse_tag(name[index + 1 :])