Skip to content

Instantly share code, notes, and snippets.

View pyokagan's full-sized avatar

Paul Tan pyokagan

View GitHub Profile
@pyokagan
pyokagan / tempy.py
Created April 3, 2014 13:32
Simple python templates
r"""Inline python
USAGE
------
`render()` compiles and renders a template, returning a string:
>>> render(r'Hello {{name}}!', name='World')
'Hello World!'
`compile()` compiles a template into a function for later use:
@pyokagan
pyokagan / code2tex.py
Created February 5, 2014 22:22
code2tex - Converts a list of code files into a LaTeX document.
#! /usr/bin/python
"""Converts a list of code files into a LaTeX document."""
from argparse import ArgumentParser
import re
import os
import subprocess
import pygments
import itertools
import sys
import signal
@pyokagan
pyokagan / fun.js
Last active December 16, 2015 07:59
fun.js -- injecting 100% more fun and excitement into nodejs programming by implementing module reloading, code swapping, classes with C3 MRO, state saving, loading and replacing.
/* This fun module implements 100% more fun and excitement in
* nodejs coding.
*
* Implements module reloading, code swapping, classes with C3 MRO,
* state saving (pickling) and loading (unpickling) and replacing.
*
* This is extracted from the bb4 source code, licensed under the MIT License.
* bb4 is available at <https://github.com/pyokagan/bb4>
*
* Requires node-weak to be installed. (npm install weak)
@pyokagan
pyokagan / mozreplcurl.py
Created December 11, 2012 09:35
mozreplcurl - Curl wrapper that uses Firefox's cookies database
#! /usr/bin/env python3.2
"""
mozreplcurl - Wrapper around curl that adds on firefox's cookies, and updates firefox's cookie db after curl finishes running.
To update firefox's cookies db when firefox is running, mozrepl is required to be installed.
"""
import sys
import re
from telnetlib import Telnet
from argparse import ArgumentParser
@pyokagan
pyokagan / gist:4250200
Created December 10, 2012 11:58
For future reference: How to properly modify the URL of a urllib.request.Request
import urllib.request
from urllib.parse import urlsplit, urlunsplit, unquote
class ModifyUrlHandler(urllib.request.BaseHandler):
def __init__(self, target):
x = urlsplit(target)
self.full_url = urlunsplit(x._replace(fragment=""))
self.fragment = x.fragment
#Remove port from x.netloc if present for origin_req_host
self.origin_req_host = x.netloc.partition(":")[0]
@pyokagan
pyokagan / tumblr.py
Created November 19, 2012 14:39
Tumblr oAuth Non-compliance
import urllib,hmac,time,hashlib,base64,httplib,sys,json,urlparse
## This is just a simple example that is self contained.
## You will need to modified it to make it work
##
## creds - need to be filled out
## blognmae - needs to be defined
##
## reads in image files from the command line and posts to your blog
@pyokagan
pyokagan / googlecurl.py
Created November 2, 2012 16:21
googlecurl - Wrapper around oauth2curl providing automatic pagination of items in google API responses. Supports gdata json, youtube jsonc and new-style google API responses.
#! /usr/bin/python3 -u
"""A wrapper around oauth2curl. Makes multiple
calls to oauth2curl. Returns items one item
per line."""
from argparse import ArgumentParser
from subprocess import Popen, PIPE
import sys
from urllib.parse import urlsplit, parse_qs, urlencode, urlunsplit
import json
import signal
@pyokagan
pyokagan / code2tex.py
Created October 31, 2012 01:01
Converts a list of code files into a latex document.
#! /usr/bin/python3
"""Converts a list of code files into a latex document."""
from argparse import ArgumentParser
import re
import os
from pygments.lexers import get_lexer_for_filename
def filter_tex(x):
def escape(x):
#! /usr/bin/python2.6
"""
trunc -n LENGTH -k 0,1,2,3... COMPONENTS...
"""
from __future__ import print_function,unicode_literals
from argparse import ArgumentParser
from math import floor
def shorten(length, x, suffix = "..."):
if len(x) <= length:
@pyokagan
pyokagan / mozrepl-inspect.py
Created October 22, 2012 11:10
Prints HTML contents of inspected node in Firebug. Requires firebug and mozrepl to be installed in Firefox. For Python 2.6, requires python-argparse.
#! /usr/bin/python2.6
#NOTE: Also python3 compatible.
from __future__ import print_function,unicode_literals
import time, json, sys, re
from argparse import ArgumentParser
from telnetlib import Telnet
class Mozrepl:
def __init__(self, host="127.0.0.1", port=4242):
self.host = host