View zodb_undo.py
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 | |
# zodb_undo.py - Snippet to undo zodb transactions upto a specific point in the past | |
zodb_file = "var/karl.db" | |
undo_back_to = (2011, 6, 15, 11, 00, 00, 00, 00, 00) # time tuple | |
import time | |
import transaction | |
from ZODB import FileStorage, DB |
View print_table.py
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 needed to print out ascii tables from data in the form: | |
# [ ('column 0 title', 'column 1 title' ..), | |
# ('row 0, column 0 data', 'row 0, column 1 data' ...) ...] | |
# | |
# and surprisingly it got complicated because of variable lengths of the data. | |
# I googled for 'standard' ways of doing this and I found suggestions like: | |
# http://stackoverflow.com/questions/5909873/python-pretty-printing-ascii-tables | |
# ...which were a bit dated and hard to read or full scale modules like: | |
# | |
# http://pypi.python.org/pypi/texttable/ |
View pythonrc.py
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 -*- | |
# The MIT License (MIT) | |
# | |
# Copyright (c) 2015 Steven Fernandez | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
View guitar_scales.py
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 | |
notes = ['a', 'a#', 'b', 'c', 'c#', 'd', 'd#', 'e', 'f', 'f#', 'g', 'g#'] * 2 | |
chroma = lambda x: [ i for i in notes[ notes.index(x): ] + notes[ :notes.index(x) ] ] + [x] | |
major = lambda x: ( chroma(x)[:5] + [chroma(x)[5]] + chroma(x)[5:12] )[::2] + [x] | |
major_penta = lambda x: [ i for idx, i in enumerate(major(x)) if idx not in (3, 6) ] | |
minor_penta = lambda x: ( major_penta( notes[ notes.index(x) + 3 ])[:-1] * 2)[-6:] | |
""" | |
# for example |
View ShowGitHubURL
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
function ShowGitHubURL() | |
" Function to echo the github URL for the currently open file at the | |
" current line number. This is useful if for example, you want to quickly | |
" share the URL for the file you're working on via, email / IRC etc. | |
" | |
" This can also be easily extended to open the browser with the displayed | |
" URL using tips such as http://vim.wikia.com/wiki/VimTip306 | |
" | |
" You may add a mapping like this to bind this function to a keystroke: | |
" map <Leader>gh :call ShowGitHubURL()<CR> |
View Git dot files management
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 |
View custom_sftp.py
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 |
View example.py
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 |
View locked_open.py
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 |
View dlt-log-app.py
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'] |
OlderNewer