Skip to content

Instantly share code, notes, and snippets.

View dutc's full-sized avatar

James Powell dutc

View GitHub Profile
@dutc
dutc / haskey.md
Created February 3, 2021 20:50
“Python Expert” Newsletter (Feb 3, 2021): How do you tell if a key/value pair exists in a Python dictionary?

How do you tell if a key/value pair exists in a Python dictionary?

Here are a couple of choices, from reasonable (i) to pointless (ii) to potentially incorrect (iii) to absurd (iv) ~ (ix).

d = {}
k = object()

# i.
@dutc
dutc / npy_alloc_cache.1.txt
Created January 13, 2021 19:14
“Python Expert” Newsletter (Jan 13, 2021): ‘mutating tuples’ results (comparative backtraces for `tuple_alloc` and `npy_alloc_cache`)
Reading symbols from python3...
Breakpoint 1 (-function malloc) pending.
Breakpoint 2 (-function tuple_alloc) pending.
Breakpoint 3 (-function npy_alloc_cache) pending.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
[New Thread 0x7ffff43a8640 (LWP 96530)]
[New Thread 0x7ffff3ba7640 (LWP 96531)]
[New Thread 0x7fffe33a6640 (LWP 96532)]
[New Thread 0x7fffdaba5640 (LWP 96533)]
@dutc
dutc / ndarray-tuple.tsv
Created January 13, 2021 19:12
“Python Expert” Newsletter (Jan 13, 2021): ‘mutating tuples’ results (memory allocation tests)
We can't make this file beautiful and searchable because it's too large.
id(xs) = 0x7f9b_5e1a_82b0 xs.__array_interface__["data"][0] = 0x556b_581f_c4d0 id(t) = 0x7f9b_5e28_a580 args.size = 1
id(xs) = 0x7f05_d486_14e0 xs.__array_interface__["data"][0] = 0x55e1_12fa_a4d0 id(t) = 0x7f05_d495_1580 args.size = 1
id(xs) = 0x7f1d_574f_f8f0 xs.__array_interface__["data"][0] = 0x55c8_86a5_24d0 id(t) = 0x7f1d_58a7_1580 args.size = 1
id(xs) = 0x7f10_58e9_e300 xs.__array_interface__["data"][0] = 0x55ee_d01f_d4d0 id(t) = 0x7f10_58f8_0580 args.size = 1
id(xs) = 0x7fb4_ddb3_0260 xs.__array_interface__["data"][0] = 0x5653_496d_64d0 id(t) = 0x7fb4_ddc1_1580 args.size = 1
id(xs) = 0x7f68_602d_5530 xs.__array_interface__["data"][0] = 0x55b7_215a_f4d0 id(t) = 0x7f68_603c_7580 args.size = 1
id(xs) = 0x7f1d_b0f8_2300 xs.__array_interface__["data"][0] = 0x558a_0542_d4d0 id(t) = 0x7f1d_b106_4580 args.size = 1
id(xs) = 0x7f90_51dd_5260 xs.__array_interface__["data"][0] = 0x557b_cef8_d4d0 id(t) = 0x7f90_51eb_6580 args.size = 1
id(xs) = 0x7f9c_fb91_2850 xs.__array_interface__["data"][0] = 0x5580_e397_94d0 i
@dutc
dutc / analysis.py
Last active January 16, 2021 10:39
“Python Expert” Newsletter (Jan 13, 2021): ‘mutating tuples’ code
#!/usr/bin/env python3
from matplotlib.pyplot import show
from pandas import read_csv, to_numeric, DataFrame
from collections import namedtuple
class Analysis(namedtuple('Analysis', 'raw df')):
TUP_PATTERN = r'id\(t\) = (?P<addr>0x[\da-z_]+)'
NDARR_PATTERN = r'xs.__array_interface__\["data"\]\[0\] = (?P<addr>0x[\da-z_]+)'
@classmethod
@dutc
dutc / .emacs
Last active December 24, 2020 01:46
NumFOCUS Telethon 2020 Stretch Goal: Thomas Caswell's (@tacaswell, Matplotlib) .emacs
(require 'package)
(package-initialize)
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/"))
; (add-to-list 'package-archives
; '("elpy" . "https://jorgenschaefer.github.io/packages/"))
; (add-to-list 'load-path "~/.emacs.d/site-lisp/magit/lisp")
; (add-to-list 'load-path "~/.emacs.d/site-lisp/mdboom_gh")
(add-to-list 'load-path "~/.emacs.d/site-lisp/ox-rst")
@dutc
dutc / callgraph.py
Last active December 15, 2020 20:43
Call Graph Generation (as seen on the Don't Use This Code mailing list)
#!/usr/bin/env python3
from networkx import DiGraph
from networkx.drawing.nx_pydot import to_pydot
from contextlib import contextmanager
from sys import setprofile
from collections import namedtuple
class Ident(namedtuple('IdentBase', 'filename lineno name')):
def __str__(self):
@dutc
dutc / app.py
Created February 15, 2019 16:31
Do What I Mean, Not What I Say
#!/usr/bin/env python3
from nwis import dwim
@dwim
def func(x, y):
return x @ y
if __name__ == '__main__':
print(f'func(1, 2) = {func(1, 2)}')
@dutc
dutc / Makefile
Last active August 6, 2018 05:35
simple, slow, probably buggy `git init+add+commit` in <167 lines Python
.PHONY: test clean
test:
seq 1 10 > seq1
seq 10 20 > seq2
mkdir -p dir
seq 20 30 > dir/seq3
python git-init+add+commit.py seq1 seq2 dir/seq3 || (git init . ; git add seq1 seq2 dir ; git commit -am 'first commit')
git status
git log
@dutc
dutc / defaults.py
Created June 14, 2018 21:36
PEP 563 -- Postponed Evaluation of Annotations (https://www.python.org/dev/peps/pep-0563/)
from __future__ import annotations
from inspect import signature
from functools import wraps
from collections import namedtuple
opt = namedtuple('Optional', '')()
def better_defaults(f):
sig = signature(f)
@wraps(f)
def func(*args, **kwargs):
@dutc
dutc / solver.py
Last active June 10, 2018 03:04
code improvement exercise: solver.py
from numpy.random import random
from numpy import array, isclose, zeros_like, sign
from numpy.linalg import lstsq
def solve(func, values, atol=0.1):
while True:
result = func(values)
if isclose(result, 0, atol=atol):
break
values -= sign(result) * random(values.size)