-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
44 lines (32 loc) · 1.11 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import math
import sys
from flask import render_template
""" base62 mapping """
base_map="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
def base62_encode(number):
"""Encode a base10 number to base62 string"""
result = ''
while number > 0:
result = base_map[number % 62] + result
number = number // 62
return result
def base62_decode(string):
"""Decode a base62 string to base10 number"""
result = 0
index = len(string)
for c in string:
result = result + base_map.index(c) * pow(62, index-1)
index = index-1
return result
def apology(message, code=400):
"""Render message as an apology to user."""
def escape(s):
"""
Escape special characters.
https://github.com/jacebrowning/memegen#special-characters
"""
for old, new in [("-", "--"), (" ", "-"), ("_", "__"), ("?", "~q"),
("%", "~p"), ("#", "~h"), ("/", "~s"), ("\"", "''")]:
s = s.replace(old, new)
return s
return render_template("apology.html", top=code, bottom=escape(message)), code