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 / 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]:
@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
"""
### 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 / 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 / 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 / find-duplicate-files.bash
Last active December 29, 2018 03:43 — forked from OndraZizka/find-duplicate-files.bash
Finds duplicate files. An alternative to `fdupes -r -S .`
find -type f -size +3M -print0 | while IFS= read -r -d '' i; do
#echo $i
echo -n '.'
if grep -q "$i" md5-partial.txt; then
echo -n ':'; #-e "\n$i ---- Already counted, skipping.";
continue;
fi
#md5sum "$i" >> md5.txt
MD5=`dd bs=1M count=1 if="$i" status=none | md5sum`
MD5=`echo $MD5 | cut -d' ' -f1`
@TomFaulkner
TomFaulkner / guuid.py
Created March 7, 2019 19:56
Generate x UUID4s
import sys
from uuid import uuid4
def make(number):
for _ in range(number):
print(str(uuid4()))
if __name__ == '__main__':
@TomFaulkner
TomFaulkner / dynamo_upgrade.py
Created May 10, 2019 16:37
Dynamo (or other noSQL) record upgrades
import logging
LOGGER = logging.getLogger(__name__)
class SchemaUpgradeException(Exception):
""""""
# 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 / 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