Skip to content

Instantly share code, notes, and snippets.

View sj26's full-sized avatar
🤹‍♂️

Samuel Cochran sj26

🤹‍♂️
View GitHub Profile
@sj26
sj26 / phpserialize.py
Created February 2, 2010 09:53
Python port of PHP serialize() and unserialize()
#!/usr/bin/python -u
"""
Python port of PHP serialize() and unserialize()
by Samuel Cochran <[email protected]>
I couldn't find a nice, fairly efficient implementation of serialize/unserialize for Python... so I wrote one. I didn't use str().partition because I wanted Python <2.5 compatibility.
TODO: Performance review. There's an awful lot of string copying. >.>
<?php
/**
* Really simple, concise and fairly efficient bencode/bdecode functions
* for manipulating BitTorrent metadata within PHP.
* Lookout for a C PHP extension coming to a server near you!
* by Samuel Cochran <[email protected]>
*/
function bdecode($stream) {
# Simple routes implementation in Python
# TODO: Decouple from app framework
# TODO: Reverse routes
import re
from exceptions import HTTPException, HTTPNotFound, HTTPMethodNotAllowed
class Route(object):
''' Turns a textual route into a usable dispatching route. '''
def formatrfc2822datetime(value=None, usegmt=False):
"""Returns a date string as specified by RFC 2822 section 3.3., e.g.:
Fri, 09 Nov 2001 01:08:47 +0900
Optional value may be a datetime value. Timezone is respected.
"""
# Note: we cannot use strftime() because that honors the locale and RFC
# 2822 requires that day and month names be the English abbreviations.
if value is None:
import random
__all__ = ('corpus', 'words', 'lorem')
'''
Simple Lorem Ipsum generator for python with some probabilistic punctuation smarts.
The corpus is the full text of De finibus bonorum et malorum/Liber Primus from wikisource:
http://la.wikisource.org/wiki/De_finibus_bonorum_et_malorum/Liber_Primus
require 'active_support/all'
class Blah
def foo
'foo!'
end
end
module Things
def self.included(base)
require 'active_support/all'
class Blah
def foo
'foo!'
end
end
module Things
def self.included(base)
@sj26
sj26 / gist:633794
Created October 19, 2010 07:46
Checking alias eqivalency
class A
def a
end
def b
end
alias c b
end
@sj26
sj26 / gist:650640
Created October 28, 2010 04:34
Re-opening Enumerable
module Enumerable
def thing
'blah'
end
end
[].thing
# => "blah"
@sj26
sj26 / gist:650811
Created October 28, 2010 07:05
MRO
class A
def initialize
puts "A initialized"
end
end
module B
def initialize_with_b
puts "B initialized"
end