Skip to content

Instantly share code, notes, and snippets.

@pfmoore
pfmoore / sqlmagic.py
Created October 11, 2012 14:42
Simple ipython SQL magic
# This code can be put in any Python module, it does not require IPython
# itself to be running already. It only creates the magics subclass but
# doesn't instantiate it yet.
from IPython.core.magic import (Magics, magics_class, line_magic,
cell_magic, line_cell_magic)
import sqlite3
# The class MUST call this class decorator at creation time
@magics_class
class MyMagics(Magics):
@pfmoore
pfmoore / pathfix.py
Created March 15, 2013 15:00
Import this module at the start of a script to add version-specific subdirectories to sys.path. This lets you write generic scripts that use version-specific modules
import sys, os
here = os.path.dirname(os.path.abspath(__file__))
def add(name):
fullname = os.path.join(here, name)
if os.path.isdir(fullname):
sys.path.append(fullname)
add('py{0[0]}'.format(sys.version_info))
add('py{0[0]}{0[1]}'.format(sys.version_info))
@pfmoore
pfmoore / make_virtualenv_wrapper.py
Last active December 15, 2015 10:49
Virtualenv wrapper maker that (among other things) lets you specify things like -p 3.2 on Windows.
#!/usr/bin/env python
import os
import sys
import shutil
import tempfile
import subprocess
from glob import glob
from argparse import ArgumentParser
@pfmoore
pfmoore / loader_sample.py
Created March 28, 2013 15:39
Dummy importlib-based importer sample
from importlib.abc import PathEntryFinder, SourceLoader, FileLoader
from importlib.util import module_for_loader
class MyFinder(PathEntryFinder):
def __init__(self, pathentry):
"""Create a finder for the given `pathentry`"""
if pathentry == 'foo':
return
raise ImportError
def find_loader(self, fullname):
@pfmoore
pfmoore / dummyloader.py
Last active September 21, 2022 20:59
A dummy finder/loader that returns *any* module name under a given prefix.
"""A dummy loader implementation.
This is a deliberate attempt to implement an absolutely minimal finder/loader
combination.
The spec is:
1. If I add an entry dummy:XXX to sys.path, then that entry will be picked
up by my finder and used to satisfy import requests for any modules
starting with 'XXX'.
@pfmoore
pfmoore / ExecRange.vim
Created April 3, 2013 13:54
Vim script/function to pipe selection through a command (or run it as Vim script)
function! ExecRange(cmd) range
let l:old_a = @a
exec a:firstline . ',' . a:lastline . 'yank a'
if a:cmd == ''
exec @a
else
echo system(a:cmd, @a)
endif
let @a = l:old_a
endfunction
@pfmoore
pfmoore / wheelinst.py
Created July 12, 2013 16:56
Standalone wheel installer
"""Standalone wheel file installer.
Wheels are a binary install format for Python. The wheel package provides
hooks to allow pip to install wheels, as well as a "wheel install" command for
standalone use. However, it is still necessary to have the wheel package
installed if you want to install a wheel. Given that the wheel format is
intended to be "truly easy to install", it seems reasonable to expect to be
able to install a wheel without needing any special tools.
Hence this utility.
@pfmoore
pfmoore / basic.cfg
Created April 30, 2014 15:15
Ubuntu server preseed file
# Default User
d-i passwd/user-fullname string XXXXXXXX
d-i passwd/username string XXXXXXXX
d-i passwd/user-password-crypted password XXXXXXXX
d-i user-setup/encrypt-home boolean false
d-i user-setup/allow-password-weak boolean true
# Time Zone, system clock is UTC
d-i time/zone string Europe/London
d-i clock-setup/utc-auto boolean true
@pfmoore
pfmoore / factorio-recipe-parser.lua
Last active September 10, 2021 15:42
Parse the Factorio recipe files to create a CSV of recipes
data = {}
data["extend"] = function (data, t)
for n, recipe in ipairs(t) do
for i, component in ipairs(recipe["ingredients"]) do
cname = component[1] or component["name"]
camt = component[2] or component["amount"]
print('"' .. recipe["name"] .. '","' .. cname .. '",' .. camt)
end
end
end
@pfmoore
pfmoore / list.patch
Created August 21, 2014 19:48
pip list command patch
diff --git a/pip/commands/list.py b/pip/commands/list.py
index 3a0330a..3e4fcc7 100644
--- a/pip/commands/list.py
+++ b/pip/commands/list.py
@@ -45,6 +45,12 @@ class ListCommand(Command):
help=('If in a virtualenv that has global access, do not list '
'globally-installed packages.'),
)
+ cmd_opts.add_option(
+ '-L', '--location',