Skip to content

Instantly share code, notes, and snippets.


Nathan Hoad nhoad

View GitHub Profile
import cffi
ffi = cffi.FFI()
ffi.cdef('int PyDict_DelItemString(void *, const char *key);')
lib = ffi.dlopen('')
d = {'foo': 'bar'}
print('before', d)
d_p = ffi.cast('void*', id(d))
lib.PyDict_DelItemString(d_p,'char[]', b'foo'))
View tracking attribute accesses
from unittest import mock
class AttributeTrackMock(mock.Mock):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.attr_accesses = []
def __getattr__(self, attr):
res = super().__getattr__(attr)
nhoad /
Last active Aug 29, 2015
asynchronous object initialisation
import asyncio
class AsyncInitMeta(type):
def __call__(cls, *args, **kwargs):
self = cls.__new__(cls)
yield from self.__init__(*args, **kwargs)
return self
View jake
#!/usr/bin/env python2
import argparse
import contextlib
import logging
import re
import os
import sys
import types
View output
from overload import overload
def hello(name: str):
print('Hello, %s!' % name)
def hello(name: int):
nhoad / gist:8966377
Last active Jun 23, 2020
Async stdio with asyncio
View gist:8966377
import os
import asyncio
import sys
from asyncio.streams import StreamWriter, FlowControlMixin
reader, writer = None, None
def stdio(loop=None):
nhoad / gist:6128860
Last active Dec 20, 2015
The horror of collections.OrderedDict
View gist:6128860
>>> f.keys()
>>> f._OrderedDict__root[0][0][0][0][0][0][0][0][0][0]
[[...], [...], None]
>>> f._OrderedDict__root[0][1][0][1][0][1][0][1][0][1][0]
[[...], [...], None]