Skip to content

Instantly share code, notes, and snippets.

@garcia
garcia / gifpalettes.py
Created September 17, 2012 06:47
Losslessly rotate a GIF's hue 180 degrees
#!/usr/bin/env python
import colorsys
import struct
import sys
f_in = open(sys.argv[1], 'rb')
f_out = open(sys.argv[2], 'wb')
# check header
if f_in.read(3) != 'GIF':
raise TypeError('not a GIF')
@garcia
garcia / autotumble.py
Last active February 9, 2018 15:16
Automatically follow Tumblr users who post in the #follow-back tag and reblog posts from the Dashboard. Requires python-oauth2, a Tumblr blog, a Tumblr appication's consumer key/secret pair, and an authentication key/secret pair for the blog / application. Potentially useful for future social engineering escapades.
#!/usr/bin/env python
import itertools
import json
import logging
import sys
import time
import traceback
# oauth2 is available at https://github.com/simplegeo/python-oauth2
import oauth2
@garcia
garcia / theultimateoneliner.py
Last active December 12, 2015 09:39
A one-liner that converts any Python script to a one-liner. There are no literal semicolons in this script or its output (i.e. it doesn't cheat).
print'exec '+(lambda L,R:lambda C:R(L,R,C))((lambda L,R,c:'type((lambda:0).__code__)(%s)'%','.join(R(L,R,getattr(c,'co_'+n))for n in'argcount:nlocals:stacksize:flags:code:consts:names:varnames:filename:name:firstlineno:lnotab:freevars:cellvars'.split(':'))),(lambda L,R,o:L(L,R,o)if type(o).__name__=='code'else'('+''.join(R(L,R,n)+','for n in o)+')'if type(o).__name__=='tuple'else repr(o)))((lambda n:compile(open(n,'r').read(),n,'exec'))(__import__('sys').argv[-1]))
@garcia
garcia / imgurwords.py
Created July 12, 2013 05:55
Source for imgurwords.tumblr.com. Written in 30 minutes. Reuses some components of autotumble.py.
#!/usr/bin/env python
import json
import pickle
import pprint
import sys
import time
import traceback
import oauth2
import requests
@garcia
garcia / savephotos.py
Last active December 4, 2018 00:27
Save all photos from the tumblelog specified on the command-line using the Tumblr API v1. Requires the Python 'requests' module. Updated for a breaking API change and Python 3 support in 2018.
#!/usr/bin/env python
import argparse
import errno
import itertools
import json
import os
import requests
import time
WIDTHS = (1280, 500, 400, 250, 100, 75)
@garcia
garcia / oppositeday.py
Created November 2, 2013 22:29
Swaps the values of False and True under CPython 2.7.5. Unlike the usual "False, True = True, False" prank, this alters the hardcoded values so that even built-in operations which return a boolean (like 1 == 1) return the opposite.
import ctypes
import sys
for b, boolean in enumerate((False, True)):
(ctypes.c_char * sys.getsizeof(boolean)).from_address(id(boolean))[8] = chr(1 - b)
@garcia
garcia / selfreferencingtuple.py
Last active December 31, 2015 23:58
Self-referencing tuple in 32-bit CPython 2.7.
import ctypes
import struct
import sys
# Goal: obtain something like "outer = (outer,)", even though that's impossible with vanilla Python
inner = ()
outer = (inner,)
# Get CPython's internal representation of the outer tuple
c_outer = (ctypes.c_char * sys.getsizeof(outer)).from_address(id(outer))
#!/usr/bin/env python
def curry(f, args=None):
def curried(arg):
if args is None:
args_ = []
else:
args_ = args
args_.append(arg)
if len(args_) == f.__code__.co_argcount:
@garcia
garcia / plusorminus.py
Created January 31, 2015 04:37
Implements a plus-or-minus "operator", +OR-
import itertools
import math
class _OR(object):
def __radd__(self, lhs):
if isinstance(lhs, PlusOrMinus):
copy = PlusOrMinus(*lhs)
else:
copy = PlusOrMinus(lhs)
copy.plusorminus = True

Keybase proof

I hereby claim:

  • I am garcia on github.
  • I am garcia (https://keybase.io/garcia) on keybase.
  • I have a public key whose fingerprint is D67C C76B 1CAE 1DB0 E55E 3465 EA5C 38CB F9A6 0617

To claim this, I am signing this object: