Skip to content

Instantly share code, notes, and snippets.

@gvx
gvx / switch.py
Created November 8, 2008 08:17
A switch function for Python
#By Robin Wellner (gvx)
#I hereby waive copyright and related or neighboring rights to this work
#See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
def switch(expression, cases, default=None):
try:
ret = cases[expression]
except KeyError:
ret = default
if callable(ret): ret = ret()
@gvx
gvx / mutable.py
Created November 12, 2008 15:27
Immutable objects made mutable
#By Robin Wellner (gvx)
#I hereby waive copyright and related or neighboring rights to this work
#See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
class mutable(object):
def __init__(self, obj):
self._obj = obj
def __repr__(self):
return 'mutable('+repr(self._obj)+')'
def __call__(self, obj=None):
@gvx
gvx / ROT13.py
Created November 16, 2008 08:04
A ROT13 function for Pyton.
#By Robin Wellner (gvx)
#I hereby waive copyright and related or neighboring rights to this work
#See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
def rotn(intext, n):
outtext = []
for char in intext:
if 65 <= ord(char) < (65+26):
char = chr(((ord(char) - 65) +n) % 26 + 65)
elif 97 <= ord(char) < (97+26):
@gvx
gvx / RNDABC.TIbasic
Created November 16, 2008 08:44
A simple number guessing game.
PROGRAM:RNDABC
:Input D:randInt
(1,D)→A:randInt(
1,D)→B:randInt(1
,D)→C:Disp "AB",
AB,"AC",AC,"BC",
BC:Pause :Disp "
{A,B,C}",{A,B,C
@gvx
gvx / roturn.py
Created November 17, 2008 19:14
Decode ROT13'ed block of text
#ROTurn
#Decode a ROT13'ed block of text.
#Python version of Perl script by Jay Kominek
#By Robin Wellner (gvx)
#I hereby waive copyright and related or neighboring rights to this work
#See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
"""
"I recently wrote a Perl script, which, using letter frequency, attempts
to guess whether a given block of text has been ROT13ed or not. I've found
@gvx
gvx / Rename.py
Created December 26, 2008 10:39
A function to rename variables
#By Robin Wellner (gvx)
#I hereby waive copyright and related or neighboring rights to this work
#See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
def rename(oldname, newname, scope):
scope[newname] = scope[oldname]
if hasattr(scope[newname], '__name__'): scope[newname].__name__ = newname
del scope[oldname]
@gvx
gvx / namespace.py
Created January 21, 2009 17:52
Simple module simulation
#namespace
#By Robin Wellner (gvx)
#I hereby waive copyright and related or neighboring rights to this work
#See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
#Simulation of modules containing one or more function/class/object.
#Warning: modules are named differently than what you would expect.
def namespace(name):
class namespace:
@gvx
gvx / listtools.py
Created January 30, 2009 16:44
Functions for lists
#Listtools 0.1
#By Robin Wellner (gvx)
#Functions for lists
#I hereby waive copyright and related or neighboring rights to this work
#See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
def append(lst, object):
"listtools.append(lst, object) -- append object to end"
lst.append(object)
@gvx
gvx / defdef.lua
Created February 16, 2009 13:59
default definitions for Lua
-- defdef.lua
-- default definitions for lua
-- By Robin Wellner (gvx)
-- I hereby waive copyright and related or neighboring rights to this work
-- See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
-- Evaluate truth like in Python
function is_true (arg)
if arg == '' or arg == 0 then
@gvx
gvx / patch.py
Created May 3, 2009 21:53
A simple patch script in Python
#By Robin Wellner (gvx)
#I hereby waive copyright and related or neighboring rights to this work
#See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
import sys
diff_file = sys.stdin.read().split('\n')
if len(sys.argv) > 1:
filename = sys.argv[1]
else: