Created
February 3, 2016 16:32
-
-
Save asilachev/91bd40b07bc40e3e2f2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
# Defining alphabet of custom base as [a-z] + [A-Z] + [0-9] | |
alphabet = map(chr, range(97, 123) + range(65, 91)) + map(str, range(0, 10)) | |
base = len(alphabet) | |
def encode(str): | |
reversed_str = str[::-1] | |
pow_num = 0 | |
result = 0 | |
for i in range(0, len(reversed_str)): | |
char = reversed_str[i] | |
char_value = alphabet.index(char) | |
result += char_value * pow(base, pow_num) | |
pow_num += 1 | |
return result | |
def decode(i): | |
result = '' | |
if i < 0: | |
raise ValueError('Must be a positive integer.') | |
elif not i: | |
result = alphabet[0] | |
while i > 0: | |
result = alphabet[i % base] + result | |
i /= base | |
return result | |
def get_url(short_url): | |
return "http://%s/%s" % (settings.PROXY_DOMAIN, short_url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment