feat: add sorting utility functions for names and app IDs
This commit is contained in:
35
src/utils/sortgenerator.py
Normal file
35
src/utils/sortgenerator.py
Normal file
@@ -0,0 +1,35 @@
|
||||
alphabet = "abcdefghijklmnopqrstuvwxyzäöüß"
|
||||
alphabet = [c for c in alphabet]
|
||||
|
||||
|
||||
def name_sort(name: str, index_len: int = 8) -> str:
|
||||
"""
|
||||
name_to_index converts a name to an index.
|
||||
"""
|
||||
name = name.lower()
|
||||
sort_number = []
|
||||
for i, c in enumerate(name):
|
||||
if i < index_len:
|
||||
num = alphabet.index(c) + 1
|
||||
sort_number += str(num)
|
||||
|
||||
res = ""
|
||||
for i in sort_number:
|
||||
res += str(i)
|
||||
# get the first 8 characters
|
||||
res = res[:index_len]
|
||||
return res
|
||||
|
||||
|
||||
def app_sort(app_id: int) -> str:
|
||||
if not isinstance(app_id, int):
|
||||
raise ValueError("app_id must be an integer")
|
||||
if app_id >= 180:
|
||||
raise ValueError("app_id must be smaller than 180")
|
||||
length = 4
|
||||
app_id = str(app_id)
|
||||
# left pad with zeros to length
|
||||
app_id_length = len(app_id)
|
||||
if app_id_length < length:
|
||||
app_id = "0" * (length - app_id_length - 1) + app_id + "0"
|
||||
return str(app_id)
|
||||
Reference in New Issue
Block a user