Skip to content

Instantly share code, notes, and snippets.

View dutc's full-sized avatar

James Powell dutc

View GitHub Profile
@dutc
dutc / region.py
Created October 29, 2022 00:11
Longest consecutive region meeting a specified predicate (in `pandas`)
#!/usr/bin/env python3
from numpy.random import default_rng
from pandas import Series, date_range, MultiIndex, Index, to_timedelta
from string import ascii_lowercase
rng = default_rng(0)
s = Series(
@dutc
dutc / timer.py
Last active October 21, 2022 21:16
A timing context manager.
from dataclasses import dataclass, field, replace
from collections import namedtuple, defaultdict
from itertools import count
from collections.abc import Generator
from contextlib import contextmanager
from time import perf_counter
from networkx import DiGraph
@dataclass(frozen=True)
class timer:
@dutc
dutc / instance_context.py
Created October 20, 2022 17:33
Legitimately Bad Idea (defining data model methods using instance-level modalities via `__class__`-patching)
from dataclasses import dataclass
@dataclass
class Ctx:
mode : bool = True
def __post_init__(self):
if (mode := self.mode):
cls = type(self)
self.__class__ = type(
@dutc
dutc / 1.StopIteration.zsh
Last active September 7, 2022 18:17
`raise StopIteration` in generator body results in `RuntimeError` in Python ≥3.7
#!/bin/zsh
code="$(<<-EOF
#!/usr/bin/env python3
from logging import getLogger, INFO, basicConfig
from sys import version_info
def g():
raise StopIteration()
@dutc
dutc / memory-analysis-case-studies.py
Last active August 21, 2022 06:56
Case Studies in Python Memory Analysis using High-Watermark Testing
#!/usr/bin/env python3
from collections import namedtuple
from functools import wraps
from inspect import signature
from itertools import islice, tee
from math import isclose
from shlex import quote
from subprocess import check_call, DEVNULL, CalledProcessError
from textwrap import dedent
@dutc
dutc / versions.py
Last active August 18, 2022 03:38
Plotting Version Numbers
from pandas import MultiIndex, date_range, Series, Categorical, merge, get_dummies, date_range, DataFrame
from itertools import product
from numpy import arange, zeros, newaxis, tile, log2
from numpy.random import default_rng
from string import ascii_lowercase
from contextlib import contextmanager
from time import perf_counter
from matplotlib.cm import get_cmap
from matplotlib.pyplot import subplots, show
@dutc
dutc / notes.md
Last active July 1, 2022 20:57
CPython workshop

themes

  1. CPython for greater understanding of the Python programming language (but "reference implementations always overspecify") Reading source to solve problems
  2. getting involved, contributing to the project

introduction

This workshop will cover the basics of the CPython runtime and interpreter. There is an enormous amount of material to cover, and I'll try to to rush through as much as I can.

@dutc
dutc / 0-question
Last active July 1, 2022 20:57
larger principles of CPython debugging
> I'm able to follow most of your instructions in the tutorial. However,
> there are things that are not yet obvious to me. If, for instance, I
> want to find the implementation of a specific built-in, where do I go?
> The `dis()` decompilation of `id()`, which I want to study, isn't
> instructive. Using `find ... -print0 | xargs -0 grep ...` also doesn't
> seem to get me anything useful. But I don't even see "builtins" file or
> a section of http://docs.python.org/3.3/c-api/index.html dealing with
> built-ins at all. Where should I be looking; how should I generalize
> this type of search for the future?
@dutc
dutc / notes.md
Last active May 27, 2022 07:29
PyCon LT 2022 Keynote—“`int` is to `list` as `float` is to `tuple`”

int is to list as float is to tuple

Event: PyCon Lithuania

Date: Thu May 26, 2022

Speaker: James Powell

Twitter: @dontusethiscode

@dutc
dutc / day01-part1.py
Last active December 22, 2021 13:48
Advent of Code using `numpy` & `pandas`
from pandas import read_csv
s = read_csv('data.txt', squeeze=True, header=None)
print(
(s.diff() > 0).sum(),
)