Skip to content

Instantly share code, notes, and snippets.

@rsj217
Last active August 29, 2015 14:02
Show Gist options
  • Save rsj217/3641d98b08a3e5f7ad61 to your computer and use it in GitHub Desktop.
Save rsj217/3641d98b08a3e5f7ad61 to your computer and use it in GitHub Desktop.
def extract_params(request):
if not isinstance(request, HTTPServerRequest):
request = request.request
parse_url = urlparse.urlparse(request.uri)
path, params, query, fragment = parse_url.path, parse_url.params, parse_url.query, parse_url.fragment
uri = urlparse.urlunparse((request.protocol, request.host, path, params, query, fragment))
http_method = request.method
headers = request.headers
if 'wsgi.input' in headers:
del headers['wsgi.input']
if 'wsgi.errors' in headers:
del headers['wsgi.errors']
if 'HTTP_AUTHORIZATION' in headers:
headers['Authorization'] = headers['HTTP_AUTHORIZATION']
body = request.body
# if body:
# body = map(lambda x: x.split('='), request.body.split('&'))
# body = urllib.urlencode(map(lambda x: tuple(x), body))
return uri, http_method, body, headers
UNICODE_ASCII_CHARACTER_SET = ('abcdefghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'0123456789')
def generate_token(length=30, chars=UNICODE_ASCII_CHARACTER_SET):
"""Generates a non-guessable OAuth token
OAuth (1 and 2) does not specify the format of tokens except that they
should be strings of random characters. Tokens should not be guessable
and entropy when generating the random characters is important. Which is
why SystemRandom is used instead of the default random.choice method.
"""
rand = random.SystemRandom()
return ''.join(rand.choice(chars) for x in range(length))
t = (
(1, -1, 'python'),
(2, -1, 'ruby'),
(3, -1, 'php'),
(4, -1, 'lisp'),
(5, 1, 'flask'),
(6, 1, 'django'),
(7, 1, 'webpy'),
(8, 2, 'rails'),
(9, 3, 'zend'),
(10, 6, 'dblog')
)
l = []
entries = {id: dict(id=id, fid=fid, title=title) for id, fid, title in t}
for e_id, entry in entries.iteritems():
if entry['fid'] == -1:
l.append(entry)
else:
entries[entry['fid']].setdefault('son', []).append(entry)
from pprint import pprint
pprint(l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment