Skip to content

Instantly share code, notes, and snippets.

View dutc's full-sized avatar

James Powell dutc

View GitHub Profile
@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 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(),
)
@dutc
dutc / nwise.py
Created December 1, 2021 14:42
`nwise` and friends!
#!/usr/bin/env python3
from itertools import tee, islice, zip_longest, chain, repeat
nwise = lambda g, *, n=2: zip(*(islice(g, i, None) for i, g in enumerate(tee(g, n))))
nwise_longest = lambda g, *, n=2, fv=object(): zip_longest(*(islice(g, i, None) for i, g in enumerate(tee(g, n))), fillvalue=fv)
first = lambda g, *, n=1: zip(chain(repeat(True, n), repeat(False)), g)
last = lambda g, *, m=1, s=object(): ((xs[-1] is s, x) for x, *xs in nwise_longest(g, n=m+1, fv=s))
if __name__ == '__main__':
@dutc
dutc / sample.py
Last active October 18, 2021 21:43
sample `pandas` operations affected by function-call penalty
from pandas import Series
from numpy.random import default_rng
from string import ascii_lowercase
rng = default_rng(0)
s = Series(
rng.integers(-10, 10, size=(size := 10_000)),
index=rng.choice([*ascii_lowercase], size=(size, length := 10)).view(f'<U{length}').ravel(),
)
@dutc
dutc / class.py
Created August 31, 2021 14:48
“Python Expert” Newsletter (Sep 22, 2021): Learning Corner
def foo(self, _):
pass
class T:
from json import dumps
from itertools import repeat
foo = foo
def bar(self, _):
pass
@dutc
dutc / getattr.py
Created August 31, 2021 14:47
“Python Expert” Newsletter (Sep 15, 2021): Learning Corner
class A:
x = 1
class B(A):
y = 20
class D:
def __get__(self, instance, owner):
return 50_000
@dutc
dutc / mro.py
Created August 31, 2021 14:45
“Python Expert” Newsletter (Sep 8, 2021): Learning Corner
class A:
def foo(self):
return 'A.foo',
class B(A):
def foo(self):
return 'B.foo', *super().foo()
class C(A):
def foo(self):
@dutc
dutc / protocols.py
Created August 31, 2021 14:44
“Python Expert” Newsletter (Sep 1, 2021): Learning Corner
from pandas import DataFrame
class Base:
def __len__(self):
return 1
class Derived(Base):
def __len__(self):
return super().__len__() + 1