Last active
October 31, 2022 03:57
-
-
Save MayankFawkes/7946a7d34738400cb81a2308d804f2fb to your computer and use it in GitHub Desktop.
A simple serial generator, it can be used in renaming images on cdn or as any uniform id generator, scalable with shading.
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
# -*- coding: utf-8 -*- | |
#!/usr/bin/env python3 | |
''' | |
A simple serial generator, it can be used in renaming images on cnd or any purpose | |
>>> series = AlphaSeries() | |
>>> series = series + (1_000_000_000 * 1_000_000_000 * 1_000_000_000) # billion * billion * billion | |
>>> print(series, series.int()) | |
BSnRvvkli4KYvXUE 1000000000000000000000000000 | |
>>> print(hex(1_000_000_000 * 1_000_000_000 * 1_000_000_000)[2:]) | |
33b2e3c9fd0803ce8000000 | |
>>> # smaller than hex | |
>>> series = AlphaSeries() | |
>>> series = series + 1_000_000_000 | |
>>> print(series, series.int()) | |
BFp3qQ 1000000000 | |
>>> series = AlphaSeries() | |
>>> series = series + 100 | |
>>> print(series, series.int()) | |
Bm 100 | |
>>> series = series - 1_000_000 | |
>>> print(series, series.int()) | |
A 0 | |
>>> for n in series: | |
... print(n) | |
B | |
C | |
D | |
E | |
F | |
G | |
H | |
I | |
J | |
continues... | |
''' | |
__author__ = 'Mayank Gupta' | |
class AlphaSeries: | |
def __init__(self, start:str="A", shard:int=None): | |
self._possible_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" | |
self._series_invert = { | |
key:val for key, val in enumerate(self._possible_chars) | |
} | |
self._series = { | |
val:key for key, val in enumerate(self._possible_chars) | |
} | |
self._shard_id = shard | |
if shard != None: | |
self._shard = AlphaSeries() + shard | |
self._shard_str = str(self._shard) + "_" | |
else: | |
self._shard_str = "" | |
self._max_size = len(self._series) | |
self._n = [self.decode(n) for n in start] | |
@classmethod | |
def from_int(cls, n:int, shard:int=None): | |
return cls(shard=shard) + n | |
@classmethod | |
def from_list(cls, n:list, shard:int=None): | |
self = cls(shard=shard) | |
self._n = n | |
return self | |
def __add__(self, other:int): | |
if isinstance(other, AlphaSeries): | |
carry = other.int() | |
else: | |
carry = other | |
new_n = [] | |
for n in self._n[::-1]: | |
if carry+n >= self._max_size: | |
carry, mod = divmod(carry+n, self._max_size) | |
new_n.append(mod) | |
else: | |
new_n.append(carry+n) | |
carry = 0 | |
while carry: | |
if carry >= self._max_size: | |
carry, mod = divmod(carry, self._max_size) | |
new_n.append(mod) | |
else: | |
if carry == 1: | |
new_n.append(carry-1) | |
else: | |
new_n.append(carry) | |
carry = 0 | |
new_n = new_n[::-1] | |
return AlphaSeries.from_list(new_n, self._shard_id) | |
def __sub__(self, other:int): | |
if isinstance(other, AlphaSeries): | |
new_id = self.int() - other.int() | |
new_id = self.int() - other | |
if new_id < 0: | |
new_id = 0 | |
return AlphaSeries.from_int(new_id, self._shard_id) | |
def __eq__(self, other:object): | |
if isinstance(other, AlphaSeries): | |
return self.int() == other.int() | |
return False | |
def __lt__(self, other:object): | |
if isinstance(other, AlphaSeries): | |
return self.int() < other.int() | |
return False | |
def __gt__(self, other:object): | |
if isinstance(other, AlphaSeries): | |
return self.int() > other.int() | |
return False | |
def __le__(self, other:object): | |
if isinstance(other, AlphaSeries): | |
return self.int() <= other.int() | |
return False | |
def __ge__(self, other:object): | |
if isinstance(other, AlphaSeries): | |
return self.int() >= other.int() | |
return False | |
def __ne__(self, other:object): | |
if isinstance(other, AlphaSeries): | |
return self.int() != other.int() | |
return False | |
def __next__(self): | |
return self + 1 | |
def __iter__(self): | |
count = 0 | |
while True: | |
yield self + count+1 | |
count += 1 | |
def __hash__(self): | |
return hash(self.int()) | |
def iter_with_limit(self, max_count:int): | |
count = 0 | |
while max_count > count: | |
yield self + count+1 | |
count += 1 | |
def raw(self): | |
return self._n | |
def _factorial(self, n): | |
return 1 if (n==1 or n==0) else n * self.factorial(n - 1); | |
def int(self): | |
val = 0 | |
for i, n in enumerate(self._n[::-1]): | |
if n == 0 and i == (len(self._n)-1) and len(self._n) != 1: | |
val += (pow(self._max_size, i) * 1) | |
else: | |
val += pow(self._max_size, i) * n | |
return val | |
def __str__(self): | |
return self._shard_str + "".join(self.encode(n) for n in self._n) | |
def __repr__(self): | |
return self.__str__() | |
def decode(self, n:str): | |
return self._series[n] | |
def encode(self, n:int): | |
return self._series_invert[n] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment