Skip to content

Instantly share code, notes, and snippets.

Avatar

Batuhan Taskaya isidentical

View GitHub Profile
View log.py
import evdev
from evdev import InputDevice, categorize, ecodes
devices = [evdev.InputDevice(path) for path in evdev.list_devices()]
for device in devices:
print(device.path, device.name, device.phys)
device = devices[int(input(">> "))]
for event in dev.read_loop():
if event.type == ecodes.EV_KEY:
print(categorize(event))
View onelinegoto.py
__import__("sys").settrace(lambda frame, event, arg: (setattr(frame, "f_lineno", (getattr(frame, "f_lineno") + tuple(__import__("dis").Bytecode(frame.f_code))[(frame.f_lasti // 2) + 1].argval)) if event == "line" and tuple(__import__("dis").Bytecode(frame.f_code))[(frame.f_lasti // 2)].argval == "goto" else None) or (lambda frame, event, arg: (setattr(frame, "f_lineno", (getattr(frame, "f_lineno") + tuple(__import__("dis").Bytecode(frame.f_code))[(frame.f_lasti // 2) + 1].argval)) if event == "line" and tuple(__import__("dis").Bytecode(frame.f_code))[(frame.f_lasti // 2)].argval == "goto" else None)))
View astopt.py
import ast
import inspect
from astpretty import pprint
def simple_name_equal(elt, target):
if not (isinstance(elt, ast.Name) and isinstance(target, ast.Name)):
return False
return elt.id == target.id
class Optimizer(ast.NodeTransformer):
@isidentical
isidentical / valf_demo.py
Last active Jul 17, 2019
Valf implementation spec (valf is not written, yet!)
View valf_demo.py
import valf
class Grammar(valf.Grammar):
class Terminals:
AUTO_HANDLE = ("int",)
END_OF_STMT = valf.Const(";")
class Compiler(valf.Visitor):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
View recreate_mutable_attributes_meta.py
import collections.abc
from itertools import chain
MUTABLE_TYPES = tuple(
filter(
lambda item: isinstance(item, type) and item.__name__.startswith("Mutable"),
collections.abc.__dict__.values(),
)
)
View recreate_mutable_attributes.py
import collections.abc
MUTABLE_TYPES = tuple(
filter(
lambda item: isinstance(item, type) and item.__name__.startswith("Mutable"),
collections.abc.__dict__.values(),
)
)
class Parent:
View filter_mutables.py
mutable_attrs = filter(
lambda attr: not attr[0].startswith("__") and isinstance(attr[1], MUTABLE_TYPES),
vars(Parent).items()
)
View get_mutable.py
import collections.abc
MUTABLE_TYPES = tuple(
filter(
lambda item: isinstance(item, type) and item.__name__.startswith("Mutable"),
collections.abc.__dict__.values(),
)
)
View initsubclass_example.py
class Parent:
items = []
def __init_subclass__(cls):
cls.items = Parent.items.copy()
class Child(Parent):
pass
View metaclass_example.py
class ParentMeta(type):
def __new__(cls, name, bases, cls_dict):
cls = super().__new__(cls, name, bases, cls_dict)
cls.items = cls.items.copy()
return cls
class Parent(metaclass=ParentMeta):
items = []
class Child(Parent):