Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 <[email protected]>
# Latest version at: http://gist.github.com/polyvertex (pydump)
import sys
@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):