Created
October 3, 2025 17:03
-
-
Save xtornasol512/f28c16eac65bbc368ce5612de5e3ed05 to your computer and use it in GitHub Desktop.
Base62 on python class alike
This file contains hidden or 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
| 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 |
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
Author
Some Use cases
- Have an intermedial action
For example counting views
When a person makes a get on the link shortener , and counter add one view everytime
- external: User gets: www.urlshort.com/2Bi
- internal: View get the request, and the base62 code "2Bi"
- ACTION: You can count the view, counter_view ++
- ACTION: Decode "2Bi", then get object
- external: Then redirect to the actual url "https://gist.github.com/xtornasol512/f28c16eac65bbc368ce5612de5e3ed05"
Author
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
Uh oh!
There was an error while loading. Please reload this page.