Skip to content

Instantly share code, notes, and snippets.

View L3viathan's full-sized avatar
🦊

Jonathan Oberländer L3viathan

🦊
View GitHub Profile
@L3viathan
L3viathan / README.md
Last active October 15, 2022 20:43
character-level coverage tracking

Proof-of-concept: character-based coverage tool

image

As of 3.11, code objects have a new co_positions() method that yields tuples of the form (lineno_start, lineno_end, char_start, char_end) for each bytecode instruction. Combined with setting f_trace_opcodes to True on a frame, trace functions can theoretically track coverage on a character level.

There are a bunch of issues though, shown in part in the code:

  • Some instructions correctly cover a wide range of code: JUMP_* instructions from branching code basically seems to span the entire length of the jump, i.e. from the if to the end of the indented block. MAKE_FUNCTION covers the entire function definition. These issues seem to be easy to resolve on first glance, because you can just ignore the corresponding opcodes.
@L3viathan
L3viathan / algebraic_effects.py
Created September 20, 2022 19:15
Simple demo of algebraic effects in Python. This doesn't show the full potential; it's just a proof-of-concept
import sys
class Effect:
def __enter__(self):
self.local_handlers = sys._getframe().f_back.f_locals.setdefault("_effect_handlers", [])
self.local_handlers.append(self)
def __exit__(self, *args):
self.local_handlers.pop()
@L3viathan
L3viathan / overload.py
Created August 18, 2022 08:50
Multiple dispatch via type annotations
def overload(fn, overloads={}, nope=object()):
def wrapper(*args, **kwargs):
for sig, func in overloads.get(fn.__qualname__, []):
try:
bound = sig.bind(*args, **kwargs)
except TypeError:
continue
for name, value in bound.arguments.items():
if not isinstance(value, func.__annotations__[name]):
break
@L3viathan
L3viathan / defer.py
Created July 29, 2022 10:31
Go-like defer statements
import sys
import inspect
from contextlib import contextmanager
from collections import defaultdict
deferred_lines = defaultdict(list)
def defer_trace(frame, event, arg):
@L3viathan
L3viathan / conceal-type-hints.vim
Last active August 19, 2022 12:21
Conceal type hints
set conceallevel=2
syntax match Normal '\v ?(:|-\>) ?[^,):]+' conceal
" this doesn't always work, only with simple types (not stuff with commas in them)
" if you know how to deal with those (virtual text?), tell me please
@L3viathan
L3viathan / pass.py
Created February 4, 2022 22:10
body-free loops :)
for type(
type.__name__,
(),
{
type.__name__: type(
type.__name__,
(),
{
"__{0}__".format(
type({0}).__name__
import fishhook
split_map = {"w": "v"}
@fishhook.hook(str)
def __getitem__(self, something):
if not isinstance(something, slice):
return fishhook.orig(self, something)
start, stop, step = something.start, something.stop, something.step
split_start = start % 1 == 0.5 if start else False
@L3viathan
L3viathan / atomic_contextmanager.py
Last active May 2, 2024 11:49
Go backwards in time when something went wrong
import os
import sys
import inspect
import signal
from contextlib import contextmanager
def _kill_self(signum, frame):
os._exit(0)
@contextmanager
@L3viathan
L3viathan / alias_of.py
Created December 14, 2021 23:39
Class attribute aliases
class AliasableMeta(type):
aliases = {}
class Aliasable(metaclass=AliasableMeta): pass
class AliasOfMeta(type):
def __getattr__(self, value):
return self(value)
class alias_of(metaclass=AliasOfMeta):
def __init__(self, other=None):
@L3viathan
L3viathan / demo.py
Created November 24, 2021 19:50
Allow changing tuple elements
>>> import mutable_tuples
>>> t = (1, 2, 3)
>>> t[1] = 23
>>> t
(1, 23, 3)