Skip to content

Instantly share code, notes, and snippets.

@dutc
dutc / npy_alloc_cache.1.txt
Created Jan 13, 2021
“Python Expert” Newsletter (Jan 13, 2021): ‘mutating tuples’ results (comparative backtraces for `tuple_alloc` and `npy_alloc_cache`)
View npy_alloc_cache.1.txt
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 Jan 13, 2021
“Python Expert” Newsletter (Jan 13, 2021): ‘mutating tuples’ results (memory allocation tests)
View ndarray-tuple.tsv
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
@dutc
dutc / analysis.py
Last active Jan 16, 2021
“Python Expert” Newsletter (Jan 13, 2021): ‘mutating tuples’ code
View analysis.py
#!/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 Dec 24, 2020
NumFOCUS Telethon 2020 Stretch Goal: Thomas Caswell's (@tacaswell, Matplotlib) .emacs
View .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 Dec 15, 2020
Call Graph Generation (as seen on the Don't Use This Code mailing list)
View callgraph.py
#!/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 Feb 15, 2019
Do What I Mean, Not What I Say
View app.py
#!/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 Aug 6, 2018
simple, slow, probably buggy `git init+add+commit` in <167 lines Python
View Makefile
.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 Jun 14, 2018
PEP 563 -- Postponed Evaluation of Annotations (https://www.python.org/dev/peps/pep-0563/)
View defaults.py
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 Jun 10, 2018
code improvement exercise: solver.py
View 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)
@dutc
dutc / battleplayer.py
Last active May 22, 2018
simple program for playing Battleship
View battleplayer.py
#!/usr/bin/env python3
from itertools import product
from battleserver import Board
if __name__ == '__main__':
board = Board()
for x, y in product(range(5), range(5)):
action = x, y
result = board(x, y)
print(f'{action}{result}')
You can’t perform that action at this time.