Skip to content

Instantly share code, notes, and snippets.

@polyvertex
polyvertex / pyastdump
Created February 1, 2015 14:11
Python AST Pretty Printer
import ast
def dbg_astdump(node, annotate_fields=True, include_attributes=False, indent=' '):
"""
AST Pretty Printer
Code copied and slightly modified from:
http://alexleone.blogspot.com/2010/01/python-ast-pretty-printer.html
"""
def _format(node, level=0):
if isinstance(node, ast.AST):
@polyvertex
polyvertex / pydump
Last active January 24, 2022 18:20
A Python3 pretty-printer that also does introspection to detect the original name of the passed variables
#!/usr/bin/env python3
#
# pydump
# A Python3 pretty-printer that also does introspection to detect the original
# name of the passed variables
#
# Jean-Charles Lefebvre <polyvertex@gmail.com>
# Latest version at: http://gist.github.com/polyvertex (pydump)
import sys
@polyvertex
polyvertex / pyunbuf
Created February 7, 2015 09:58
Unbuffered stream emulation in Python
class Unbuffered:
"""
Unbuffered stream emulation
Usage: sys.stdout = Unbuffered(sys.stdout)
"""
def __init__(self, stream):
self.stream = stream
def write(self, data):
self.stream.write(data)
self.stream.flush()
@polyvertex
polyvertex / pyrematch
Last active January 21, 2016 03:50
Small Python class to if/elif re.match()
class ReMatch():
"""
Small class to re.match() and to hold its result.
Convenient if you want to "if re.match(): ... elif re.match(): ..."
Usage:
rem = ReMatch()
if rem.match(...):
print(rem.group(1, "default value here"))
elif rem.match(...):
...
@polyvertex
polyvertex / pygetopts
Created February 20, 2015 09:18
A small command line arguments parser in Python
def getopts(args=None, opts=[]):
"""
A small command line arguments parser.
args is the list of the arguments to parse. sys.argv[1:] by default.
opts is a list of definitions of the options to interpret.
Each element in the list is a string defining one option and its properties.
Definition format is as follows:
{name}[,name2[,...]][(properties)][={value_type}]
@polyvertex
polyvertex / mathexp.py
Last active February 16, 2016 15:07 — forked from miohtama/gist:34a83d870a14aa7e580d
Safe evaluation of math expressions in Python, using byte code verifier and eval()
""""
The orignal author: Alexer / #python.fi
"""
import opcode
import dis
import sys
import multiprocessing
@polyvertex
polyvertex / iglob_hidden.py
Created March 26, 2016 11:01
A glob.iglob that include dot files and hidden files
import glob
def iglob_hidden(*args, **kwargs):
"""A glob.iglob that include dot files and hidden files"""
old_ishidden = glob._ishidden
glob._ishidden = lambda x: False
try:
yield from glob.iglob(*args, **kwargs)
finally:
glob._ishidden = old_ishidden
@polyvertex
polyvertex / wtypes.py
Last active August 27, 2020 04:34
A wrapper over ctypes for the Windows API
import ctypes as ct
from ctypes.wintypes import *
import uuid
def has_func(ctypes_dll, func_name):
if hasattr(ctypes_dll, func_name):
return func_name
if not func_name.endswith("W") and hasattr(ctypes_dll, func_name + "W"):
return func_name + "W"
return None
@polyvertex
polyvertex / super_decay.cpp
Created December 30, 2016 15:56
An almighty std::decay<>
template <typename T>
struct super_decay
{
typedef
typename std::remove_cv<
typename std::remove_pointer<
typename std::remove_reference<
typename std::remove_extent<
typename std::decay<T>::type>::type>::type>::type>::type type;
};
@polyvertex
polyvertex / char_type_of.cpp
Last active December 31, 2016 09:59
Deduce the char type of virtually any literal or container (C++11)
// char_type_of: deduce the char type of virtually any literal or
// container (C++11 SFINAE at its finest)
// Original author: Jean-Charles Lefebvre <polyvertex@gmail.com>
// util: an almighty version of std::decay<>, with super powers
template <typename T>
struct super_decay
{
typedef
typename std::remove_cv<