Skip to content

Instantly share code, notes, and snippets.

@xtornasol512
Created October 3, 2025 17:03
Show Gist options
  • Select an option

  • Save xtornasol512/f28c16eac65bbc368ce5612de5e3ed05 to your computer and use it in GitHub Desktop.

Select an option

Save xtornasol512/f28c16eac65bbc368ce5612de5e3ed05 to your computer and use it in GitHub Desktop.
Base62 on python class alike
class Base62Tools:
"""
Utils to manage Base62 encode and decode
Base on this snippet https://gist.github.com/agharbeia/5d3dc0e6998c0f1458dd9b35e57118db
"""
digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
# TODO enhance removing example = "lL10oO"
# digits = '23456789ABCDEFGHJKMNPQRSTWXYZabcdefghjkmnpqrstwxyz'
# See more details in Crockford's Base32
radix = len(digits)
@staticmethod
def encode(number):
""" Encode a number in base10 to string Base62 """
ZERO = 0
if not isinstance(number, int):
raise Exception("[Base62Tools ERROR] Not number received: {}".format(number))
if number < ZERO:
raise Exception("[Base62Tools ERROR] Negative number {} received".format(number))
if number == ZERO:
return str(ZERO)
result = ''
while number != ZERO:
result = (Base62Tools.digits[number % Base62Tools.radix]) + result
number = int(number / Base62Tools.radix)
return result
@staticmethod
def decode(base62_string):
""" Decode a string in base62 to a number in base10 """
base62_string = str(base62_string)
if isinstance(base62_string, int):
base62_string = str(base62_string)
if not isinstance(base62_string, str):
raise Exception("[Base62Tools ERROR] Not string received: {}".format(base62_string))
number_result, exp = 0, 1
for character in reversed(base62_string):
number_result += exp * Base62Tools.digits.index(character)
exp *= Base62Tools.radix
return number_result
@xtornasol512
Copy link
Author

xtornasol512 commented Oct 3, 2025

TABLE URLS_SHORTENING
ID 10000 
URL https://gist.github.com/xtornasol512/f28c16eac65bbc368ce5612de5e3ed05
METADATA Views, etc, etc
BASE62 computada Base62Tools.encode(10000) -> '2Bi'




GET: myshorturl/2Bi
DECODE Base62Tools.decode('2Bi') -> 10000
SEARCH (QUERIE): myobject = URLS_SHORTENING.all(id=10000)

print("URL: {myobject.url}")
>> URL: https://gist.github.com/xtornasol512/f28c16eac65bbc368ce5612de5e3ed05

@xtornasol512
Copy link
Author

Requirements and constraints

  • Have a numeric ID incremental or similar, on your main table
  • You can add all other fields you required as URL, CODE_REF, TIME_EXP
  • That's it

@xtornasol512
Copy link
Author

xtornasol512 commented Oct 3, 2025

Some Use cases

  1. Have an intermedial action
    For example counting views

When a person makes a get on the link shortener , and counter add one view everytime

@xtornasol512
Copy link
Author

xtornasol512 commented Oct 3, 2025

PGRouting case

Requirements

  • Minimum an integer ID
  • Whatever data or metadata
  • Know very well which components are nodes, and which ones are edges

Use case
Table
ID NUM INCR example 1000
URL "app.com/donwload/{short_code(base62)}"
CODE_REF "WHATEVER_CODE_001"
METADATA { user_type: "EMP", "app_id": 320, "show_company_tab": true|false, etc}
user_type EMPL | COMP | ETC
Base62 -> Bc9

insert how to create the point or node or edge
< QUERY TO INSERT using PGROUTING>

insert how to make a querie
< QUERY TO SEARCH using PGROUTING>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment