Skip to content

Instantly share code, notes, and snippets.

View TomFaulkner's full-sized avatar
:octocat:
GitHub activity feed is almost as exciting as early Twitter.

Tom Faulkner TomFaulkner

:octocat:
GitHub activity feed is almost as exciting as early Twitter.
View GitHub Profile
@TomFaulkner
TomFaulkner / ubuntu18.04-vfio.md
Last active February 3, 2024 15:28
VFIO Setup on Ubuntu 18.04
@TomFaulkner
TomFaulkner / Python Patch Parent.py
Created July 24, 2018 20:38
Patch the parents of a class for unit testing
# source: https://stackoverflow.com/a/24577389/9893423
# The MockFoo class still has the methods of the Foo class and it doesn't
# have the methods defined in the parent because the parent is now a Mock class.
from contextlib import contextmanager
@contextmanager
def patch_parent(class_):
"""
Mock the bases
# spread
# Author:- Rohit Tanwar
# https://github.com/kriadmin/30-seconds-of-python-code
# Contributors:-Rohit Tanwar
# Implements javascript's [].concat(...arr). Flattens the list(non-deep) and returns an list.
def spread(arg):
ret = []
for i in arg:
@TomFaulkner
TomFaulkner / AutoVivication.py
Last active February 27, 2018 21:36
Instant nested dicts
# Borrowed from:
# https://stackoverflow.com/questions/2600790/multiple-levels-of-collection-defaultdict-in-python
class AutoVivification(dict):
"""Implementation of perl's autovivification feature."""
def __getitem__(self, item):
try:
return dict.__getitem__(self, item)
except KeyError:
value = self[item] = type(self)()
return value
@TomFaulkner
TomFaulkner / background_loop.py
Created February 11, 2018 00:31
Monitor with a daemon thread
"""Background Loop for running a monitor process
Uses a daemon thread, so no delays in exiting.
Don't use this for things that involve writing data, as weird things
may happen if the daemon dies while writing.
Pass in an a function to call as "action"
This does what I need it for at the moment, for other uses one might
need to extend the action call to pass on parameters or *args, **kwargs.
@TomFaulkner
TomFaulkner / perpetual_timer.py
Created February 11, 2018 00:28
Python Perpetual (Repeating) Timer
"""
PerpetualTimer: Run a function every x seconds until PerpetualTimer.cancel().
Source, with some modificiations:
https://stackoverflow.com/questions/12435211/python-threading-timer-repeat-function-every-n-seconds
Example usage:
def printer():
print('ipsem lorem')
@TomFaulkner
TomFaulkner / extendable_enum.py
Last active May 10, 2019 16:41
Extendable Enum
"""An extendable Enum
Though this one is probably better:
https://pypi.python.org/pypi/aenum
"""
from enum import Enum
class ExtendableEnum(Enum):
@classmethod
### Keybase proof
I hereby claim:
* I am tomfaulkner on github.
* I am tomfaulkner (https://keybase.io/tomfaulkner) on keybase.
* I have a public key ASC4oGzsjuO8XVLPSyBWTYJemCFTjO4q91585EtPyS1KRwo
To claim this, I am signing this object:
@TomFaulkner
TomFaulkner / filtering.py
Last active July 15, 2017 23:12
Include and exclude filter for exact matches or regex matches
"""
Function to filter a string using an optional exact match whitelist (include),
optional whitelist using regex (include_regex),
exact match blacklist (exclude), and blacklist regex (exclude_regex)
See unittest below for usage examples
Code is from https://github.com/TomFaulkner/pypihole/blob/master/pypihole/helpers/filtering.py,
check there for updates
"""
@TomFaulkner
TomFaulkner / beautiful_idiomatic_python.md
Created April 26, 2017 05:09 — forked from mongoose11235813/beautiful_idiomatic_python.md
Transforming Code into Beautiful, Idiomatic Python: notes from Raymond Hettinger's talk at pycon US 2013. The code examples and direct quotes are all from Raymond's talk. I've reproduced them here for my own edification and the hopes that others will find them as handy as I have!

Transforming Code into Beautiful, Idiomatic Python

Notes from Raymond Hettinger's talk at pycon US 2013 video, slides.

The code examples and direct quotes are all from Raymond's talk. I've reproduced them here for my own edification and the hopes that others will find them as handy as I have!

Looping over a range of numbers

for i in [0, 1, 2, 3, 4, 5]: