Skip to content

Instantly share code, notes, and snippets.

View carlsmith's full-sized avatar

Carl Smith carlsmith

  • Cambridgeshire, UK
View GitHub Profile
@carlsmith
carlsmith / docs.py
Last active May 25, 2018 03:13
[ALPHA] Hypertext Markup Engine | Taking another pop at generating HTML5 documents in Python 2
from htme import *
subpages = {
"unknown": {
"type": 'Unknown User',
"body": 'Your <a href="{URL}">Google Account</a> is unknown.'
},
"denial": {
"type": 'Unauthorized User',
"body": 'Your rank is too low to access this part of the app.'
@carlsmith
carlsmith / fetch_phaser_world.py
Last active May 2, 2018 02:55
This Python function downloads a given range of Phaser World back issues.
def download(issues):
"""This function downloads a given range of Phaser World back
issues into the current working directory. Any arguments will
be passed to the `range` function to define the list of issue
numbers that will be downloaded."""
import os
url_template = "https://phaser.io/images/newsletter/pdf/issue{}.pdf"
@carlsmith
carlsmith / replace.py
Created April 5, 2017 02:08
A Python function that does multiple string replace ops in a single pass.
import re
def replace(string, substitutions):
substrings = sorted(substitutions, key=len, reverse=True)
regex = re.compile('|'.join(map(re.escape, substrings)))
return regex.sub(lambda match: substitutions[match.group(0)], string)
@carlsmith
carlsmith / nyan.py
Last active April 5, 2017 02:04
IPython line magic that adds highlighting to `less` using `pygmentize` - basically a coloured `cat` that scrolls well.
@line_magic
def nyan(path): os.system("pygmentize -g {0} | less -R".format(path))
@carlsmith
carlsmith / maestro.py
Last active March 9, 2024 06:03
IPython line magic for running a Keyboard Maestro macro by name.
import os
from IPython.core.magic import register_line_magic as line_magic
@line_magic
def maestro(name):
quote = lambda text: '"{}"'.format(text)
command = " osascript -e 'tell application {0} to do script {1}' "
os.system(command.format(quote("Keyboard Maestro Engine"), quote(name)))
@carlsmith
carlsmith / fileserver.py
Last active January 19, 2017 16:38
IPython line magic for launching a static fileserver. It uses Ruby as the Python fileserver sucks a bit.
import os
from IPython.core.magic import register_line_magic as line_magic
@line_magic
def fileserver(args):
path, port = args.split(" ") if args else (".", "8080")
os.system('ruby -run -ehttpd "{}" -p{}'.format(path, port))
@carlsmith
carlsmith / common.coffee
Last active February 5, 2019 17:27
A small collection of generic CoffeeScript helper functions.
# shorthand wrapper for `console.log`
put = (args...) -> console.log args...
# initialise undefined vars: `[a, b, c] = init 3`
init = (amount) -> undefined for n in [1..amount]
# decorator for defining constructors (see example.coffee)
factory = (mutator) -> (args...) ->
mutator (self = Object.create null), args...
return self
@carlsmith
carlsmith / wrapping_cursor.py
Last active August 11, 2016 20:24
IPython 5 startup file that makes the cursor wrap around lines when editing multiline input.
def set_terminal_keybindings():
"""If IPython is running in the classic terminal mode, this function
is called once, and is not called otherwise. We could have just used
an if-block, but this way also keeps the variables local."""
from prompt_toolkit.keys import Keys
from prompt_toolkit.filters import Filter
class StartOfLine(Filter):
@carlsmith
carlsmith / automain.py
Last active December 8, 2022 22:43
Main function decorator (for Python 2 and 3).
import inspect
def main(function):
locale = inspect.stack()[1][0].f_locals
module = locale.get("__name__", None)
if module == "__main__":
locale[function.__name__] = function
function()
@carlsmith
carlsmith / launcher.py
Last active August 29, 2015 14:10
Launch one SL4A script from inside another one.
from android import Android
droid = Android()
def launch_script(path, visible=False):
visibilty = 'FORE' if visible else 'BACK'
activity = 'com.googlecode.android_scripting.action.LAUNCH_{0}GROUND_SCRIPT'.format(visibilty)
extras = {'com.googlecode.android_scripting.extra.SCRIPT_PATH': path}
package = 'com.googlecode.android_scripting'
classname = 'com.googlecode.android_scripting.activity.ScriptingLayerServiceLauncher'