Created
August 3, 2020 22:33
-
-
Save gwennlbh/c3046683b9b0704ace528cacefcb845a to your computer and use it in GitHub Desktop.
A experiment to decide on which shortid formats to use for schoolsyst
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
import random | |
from rich import print | |
from rich.columns import Columns | |
from rich.console import Console | |
from rich.table import Table | |
from schoolsyst_api.database import COLLECTIONS | |
base59 = lambda len: "".join([random.choice(charset) for _ in range(len)]) | |
cons = Console() | |
charset = ( | |
[chr(c + ord("a")) for c in range(ord("z") + 1 - ord("a"))] | |
+ [chr(c + ord("A")) for c in range(ord("Z") + 1 - ord("A"))] | |
+ list("2345678901") | |
) # + list('-_') | |
charset = [c for c in charset if c not in "0OlI1"] | |
print("".join(charset)) | |
base57 = base59 | |
del base59 | |
def see(min_cap: float, title: str) -> int: | |
t = Table(show_edge=False) | |
t.add_column("Length") | |
t.add_column("Example", justify="left") | |
t.add_column("Capacity", justify="right") | |
t.add_column("Enough capacity?") | |
t.row_styles = ["bold", "dim"] | |
t.title = title | |
enough = None | |
for i in range(1, 30): | |
if not enough and len(charset) ** i > min_cap: | |
enough = i | |
t.add_row( | |
str(i), | |
f"{len(charset)**i:,}".replace(",", " "), | |
base57(i), | |
("[red]no[/red]" if len(charset) ** i <= min_cap else "[green]yes"), | |
) | |
cons.print(t) | |
return enough | |
U_MIN_CAP = 10e9 | |
O_MIN_CAP = 10e3 * 365 | |
U_enough = see(U_MIN_CAP, "Usernames") | |
print("\n" * 4) | |
O_enough = see(O_MIN_CAP, "Owned resources") | |
print("\n" * 4) | |
print( | |
f" with [bold yellow]{U_MIN_CAP:,}[/bold yellow] [u]usernames[/u] " | |
f"and [bold yellow]{O_MIN_CAP:,}[/bold yellow] [u]owned resources[/u]\n" | |
+ " " | |
+ f"[bold]charest of length [cyan]{len(charset)}[/bold][/cyan] \n" | |
+ " " | |
f"[dim]{{[/dim] {' '.join(charset)} [dim]}}[/dim]" | |
) | |
print("") | |
for c in COLLECTIONS: | |
print( | |
" " * 18 | |
+ f"{c:>15}" | |
+ "[dim not bold]/[not dim]" | |
+ base57(U_enough) | |
+ "[dim not bold]/[not dim]" | |
+ base57(O_enough) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment