Skip to content

Instantly share code, notes, and snippets.

View baal.py
import math
import statistics
import ast
class NotSafe(Exception):
pass
class DangerLister(ast.NodeVisitor):
@isidentical
isidentical / context-manager.py
Created Feb 12, 2019
Simple contextlib.contextmanager implementation
View context-manager.py
class ContextManager:
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
self.gen = self.func(*args, **kwargs)
return self
def __enter__(self):
return next(self.gen)
View some.c
PyObject *
hookify_PyFunction_FastCallKeywords(PyObject *func, PyObject *const *stack,
Py_ssize_t nargs, PyObject *kwnames)
{
__asm__("NOP");
PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
PyObject *globals = PyFunction_GET_GLOBALS(func);
PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
PyObject *kwdefs, *closure, *name, *qualname;
PyObject **d;
View custom.c
PyObject *
custom_PyFunction_FastCallKeywords(PyObject *func, PyObject *const *stack,
Py_ssize_t nargs, PyObject *kwnames)
{
__asm__("NOP");
PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
PyObject *globals = PyFunction_GET_GLOBALS(func);
PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
PyObject *kwdefs, *closure, *name, *qualname;
PyObject **d;
View fx.py
import parser
import math
class Function:
def __init__(self, expr):
self.code = parser.expr(expr).compile()
def __call__(self, **kwargs):
return eval(self.code, {'pi': math.pi, 'e': math.e, 'cos': math.cos}, kwargs)
f = Function('2*x')
View kheb.py
from itertools import repeat
from functools import partial
from copy import deepcopy
PREVIEW_SPACING = 15
PREVIEW_ENDING = 2
preview_print = partial(print, end="\n" * PREVIEW_ENDING)
def preview(area):
@isidentical
isidentical / osmoz.py
Created Mar 11, 2019
Catlizor in 18 lines!
View osmoz.py
def exc(self, result):
for callback in self.hook_spec[result.condition][1]:
try: callback(result)
except CallbackStop: break
def meth_wrapper(function, catlizor):
def wrapper(*args, **kwargs):
if HookConditions.PRE in catlizor.tracked(function.__name__): catlizor.exc(Result(function, args, kwargs, HookConditions.PRE))
res = function(*args, **kwargs)
if HookConditions.ON_CALL in catlizor.tracked(function.__name__): catlizor.exc(Result(function, args, kwargs, HookConditions.ON_CALL, res))
if HookConditions.POST in catlizor.tracked(function.__name__): catlizor.exc(Result(function, args, kwargs, HookConditions.POST, kwargs))
View different_approach.py
import operator
from collections import UserString
from enum import Enum, auto, unique
from functools import partial
from pprint import pprint
from queue import SimpleQueue
from typing import Callable, Dict, List
@unique
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):
View initsubclass_example.py
class Parent:
items = []
def __init_subclass__(cls):
cls.items = Parent.items.copy()
class Child(Parent):
pass