Skip to content

Instantly share code, notes, and snippets.

@lonetwin
lonetwin / random_functions.sh
Last active April 26, 2016 13:59
Collection of bashrc functions (and their bash completions) I find useful
# execute an interactive python shell on a remote machine,
# over ssh, after coping my local pythonrc to that machine
# (my pythonrc available here: https://gist.github.com/lonetwin/5902720)
function rpython {
DEST='/tmp/.pythonrc.py'
scp -q $PYTHONSTARTUP $1:$DEST
ssh -t $1 -- "PYTHONSTARTUP=$DEST python"
ssh $1 "rm $DEST"
}
# - setup completion for rpython
@lonetwin
lonetwin / pip_meta_path_finder.py
Created May 16, 2016 13:15
Auto-install missing python modules
import sys
import pip
from importlib import import_module
from importlib.abc import MetaPathFinder
class PipMetaPathFinder(MetaPathFinder):
"""A importlib.abc.MetaPathFinder to auto-install missing modules using pip
"""
def find_spec(fullname, path, target=None):
@lonetwin
lonetwin / simple_test.py
Created September 6, 2017 22:47
Difference in parameterized tests with nose and nose2
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# With nose, this test will (incorrectly) pass
# With nose2, this test will (correctly) fail
import unittest
class TestSomething(unittest.TestCase):
def validate(self, x, y):
@lonetwin
lonetwin / tracer.py
Created April 14, 2018 20:41
tracer.py - Quick and dirty python 'strace'
#!/usr/bin/python
""" tracer.py: Quick and dirty python 'strace'
"""
import os
import os.path
import sys
import linecache
from functools import wraps
@lonetwin
lonetwin / text_transforms.py
Created August 17, 2021 10:16
Transform a piece of text by appling an ordered list of string transformations to input text
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
from itertools import chain
from functools import reduce, partial
def apply_substitutions(text, substitutions):
"""Apply an ordered list of string transformations to input text