Skip to content

Instantly share code, notes, and snippets.

Carl Smith carlsmith

Block or report user

Report or block carlsmith

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@carlsmith
carlsmith / launcher.py
Last active Aug 29, 2015
Launch one SL4A script from inside another one.
View launcher.py
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'
@carlsmith
carlsmith / wrapping_cursor.py
Last active Aug 11, 2016
IPython 5 startup file that makes the cursor wrap around lines when editing multiline input.
View wrapping_cursor.py
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 / maestro.py
Last active Aug 12, 2016
IPython line magic for running a Keyboard Maestro macro by name.
View maestro.py
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 Jan 19, 2017
IPython line magic for launching a static fileserver. It uses Ruby as the Python fileserver sucks a bit.
View fileserver.py
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 / automain.py
Last active Jan 19, 2017
Main function decorator (for Python 2 and 3).
View automain.py
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 / ipython.jsf
Created Feb 23, 2017
IPython Syntax Highlighting for `ne` (the Nice Editor) and JOE (Joe's Own Editor).
View ipython.jsf
# IPython Highlighting for ne and JOE.
=Idle
=Comment white
=Docstring green
=DocstringLabel magenta
=DocEscape bold yellow
=StringLiteral green
=Constant magenta
=Escape bold yellow
@carlsmith
carlsmith / nyan.py
Last active Apr 5, 2017
IPython line magic that adds highlighting to `less` using `pygmentize` - basically a coloured `cat` that scrolls well.
View nyan.py
@line_magic
def nyan(path): os.system("pygmentize -g {0} | less -R".format(path))
@carlsmith
carlsmith / barcode.py
Last active Mar 27, 2018
Interleaved 2 of 5, SVG Barcode Generator
View barcode.py
def render(digits):
'''This function converts its input, a string of decimal digits, into a
barcode, using the interleaved 2 of 5 format. The input string must not
contain an odd number of digits. The output is an SVG string.
Wikipedia [ITF Format]: http://en.wikipedia.org/wiki/Interleaved_2_of_5
'''
top = '<svg height="58" width="{0}" style="background:white">'
@carlsmith
carlsmith / fetch_phaser_world.py
Last active May 2, 2018
This Python function downloads a given range of Phaser World back issues.
View fetch_phaser_world.py
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 / docs.py
Last active May 25, 2018
[ALPHA] Hypertext Markup Engine | Taking another pop at generating HTML5 documents in Python 2
View docs.py
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.'
You can’t perform that action at this time.