Skip to content

Instantly share code, notes, and snippets.

View rusek's full-sized avatar

Krzysztof Rusek rusek

View GitHub Profile
@rusek
rusek / randbytes.py
Created October 11, 2015 14:35
Random bytes generation in Python
import random as random_mod
try:
import ctypes
_long_as_byte_array = ctypes.pythonapi._PyLong_AsByteArray
_long_as_byte_array.argtypes = [
ctypes.py_object,
ctypes.POINTER(ctypes.c_char),
@rusek
rusek / delegate.c
Created October 10, 2015 17:57
Interpreter path relative to script location in shebang
/*
Helper program for resolving the path to the interpreter executable
relative to the script location in shebangs.
Sample usage:
#!/usr/bin/delegate ../my-virtualenv/bin/python
import sys
def main():
@rusek
rusek / tbpack.py
Last active October 1, 2015 18:14
Stupid way to pickle traceback objects in Python
import sys
import types
def _patched_name(code, name):
# argument order is described in help(types.CodeType)
return types.CodeType(
code.co_argcount,
code.co_nlocals,
code.co_stacksize,
@rusek
rusek / enumc.py
Created September 26, 2015 09:52
Python3 Enum with a separate class for storing constants
from collections import OrderedDict
class EnumMeta(type):
def __init__(cls, name, bases, attrs):
super(EnumMeta, cls).__init__(name, bases, attrs)
if 'Consts' not in attrs:
cls.Consts = type(
'Consts',
(Enum.Consts, ),