This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" tracer.py: Quick and dirty python 'strace' | |
""" | |
import os | |
import os.path | |
import sys | |
import linecache | |
from functools import wraps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Minimal example of writing logs to a dlt-daemon from a python app. The | |
# message will look like this when printed out using | |
# "dlt-receive -a localhost": | |
# 2016/03/10 10:24:42.772153 290716359 016 ECU1 DLTD INTM log info V 1 [ApplicationID 'APPY' registered for PID 29, Description=APPY service] | |
# 2016/03/10 10:24:42.772178 290716360 017 ECU1 DLTD INTM log info V 1 [ContextID 'APPY' registered for ApplicationID 'APPY', Description=Context for APPY] | |
# 2016/03/10 10:24:42.772182 290716359 000 ECU1 APPY APPY log info V 1 [This is a log message] | |
# 2016/03/10 10:24:42.772185 290716360 018 ECU1 DLTD INTM log info V 1 [Unregistered ContextID 'APPY' for ApplicationID 'APPY'] | |
# 2016/03/10 10:24:42.772190 290716360 019 ECU1 DLTD INTM log info V 1 [Unregistered ApplicationID 'APPY'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import fcntl | |
from contextlib import contextmanager | |
@contextmanager | |
def locked_open(filename, mode='r'): | |
"""locked_open(filename, mode='r') -> <open file object> | |
Context manager that on entry opens the path `filename`, using `mode` | |
(default: `r`), and applies an advisory write lock on the file which |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
def page_category_filter(bid_request, config): | |
if config["mode"] == "whitelist": | |
return bool( | |
bid_request["categories"] & set(config["categories"]) | |
) | |
else: | |
return bool( | |
config["categories"] and not |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# A more detailed explaination will come as a blog post soon, but in brief, | |
# here is the problem that led to this: | |
# | |
# For various reasons we have a requirement to implement an 'sftp' like | |
# abstraction/interface for a service that we provide. Basically we need to | |
# present objects in our application as a filesystem accessible over sftp. | |
# | |
# The way we have decided to go about this is to replace the 'standard' openssh | |
# sftp subsystem command entry in /etc/ssh/sshd_config on our servers with our |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I like to manage dotfiles without having to mess with silly symlinks or having | |
to install/configure specific dotfile managament tools. So here's what I did: | |
$ cd ~ | |
$ git init . | |
$ echo '*' > .gitignore # ignore all files by default | |
$ echo '!.bashrc' >> .gitignore # ...and then tell git what files not to *not* ignore | |
$ # ...add other files you may want to track to *not* ignore | |
$ git add .bashrc # now actually add the files to git | |
$ git add .gitignore # add the .gitignore to git |
NewerOlder