Added code
This commit is contained in:
0
modules/__init__.py
Normal file
0
modules/__init__.py
Normal file
1
modules/config/__init__.py
Normal file
1
modules/config/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .config import charset
|
||||
11
modules/config/config.py
Normal file
11
modules/config/config.py
Normal file
@@ -0,0 +1,11 @@
|
||||
import json
|
||||
|
||||
try:
|
||||
config = json.load(open('config.json', encoding='utf-8'))
|
||||
except:
|
||||
config = {
|
||||
"charset": "abcdefghijklmnopqrstuvwxyz"
|
||||
}
|
||||
json.dump(config, open('config.json', 'w', encoding='utf-8'), indent=4)
|
||||
|
||||
charset = ''.join(sorted(list(set(' ' + config['charset']))))
|
||||
1
modules/generator/__init__.py
Normal file
1
modules/generator/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .pages import Page, Index
|
||||
42
modules/generator/pages.py
Normal file
42
modules/generator/pages.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from modules.config import charset
|
||||
from ezzthread import threaded
|
||||
|
||||
|
||||
def encode(n):
|
||||
try:
|
||||
return charset[n]
|
||||
except IndexError:
|
||||
raise Exception(f"Cannot encode {n}")
|
||||
|
||||
|
||||
def decode(s):
|
||||
try:
|
||||
return charset.index(s)
|
||||
except ValueError:
|
||||
raise Exception(f"Cannot decode {s}")
|
||||
|
||||
|
||||
def dec_to_base(dec=0, base=16):
|
||||
if dec < base:
|
||||
return encode(dec)
|
||||
else:
|
||||
return dec_to_base(dec // base, base) + encode(dec % base)
|
||||
|
||||
|
||||
def base_to_dec(s, base=16, rec_pow=0):
|
||||
if s == str():
|
||||
return 0
|
||||
else:
|
||||
return decode(s[-1]) * (base ** rec_pow) + base_to_dec(s[0:-1], base, rec_pow + 1)
|
||||
|
||||
|
||||
class Page:
|
||||
@staticmethod
|
||||
def from_index(index: int) -> str:
|
||||
return dec_to_base(index, len(charset))
|
||||
|
||||
|
||||
class Index:
|
||||
@staticmethod
|
||||
def from_page(page: str) -> int:
|
||||
return base_to_dec(page, len(charset))
|
||||
Reference in New Issue
Block a user