clean it up

This commit is contained in:
2020-11-19 23:11:42 +01:00
parent d11ef52c6c
commit 6632a02bf4

View File

@@ -1,15 +1,14 @@
import os import os
import sys import sys
import imp import imp
import itertools
__all__ = [
'__script__',
'__ruby_script__',
'__py_script__',
]
def to_i(num, b):
"""String to integer."""
n = 0
for d in num:
if int(d) > b:
return 0
n = b * n + int(d)
return n
def to_c(digit): def to_c(digit):
"""Integer to char.""" """Integer to char."""
@@ -17,6 +16,7 @@ def to_c(digit):
return str(digit) return str(digit)
return chr(ord('a') + digit - 10) return chr(ord('a') + digit - 10)
def to_s(number, base): def to_s(number, base):
"""Integer to string.""" """Integer to string."""
if number < 0: if number < 0:
@@ -26,16 +26,18 @@ def to_s(number, base):
return to_s(d, base) + to_c(m) return to_s(d, base) + to_c(m)
return to_c(m) return to_c(m)
def chunks(li, n): def chunks(li, n):
r = [] r = []
for i in range(0, len(li), n): for i in range(0, len(li), n):
r.append(li[i:i+n]) r.append(li[i:i+n])
return r return r
def __script__(src): def __script__(src):
"""For documentation, consult the README.""" """For documentation, consult the README."""
code = [] code = []
src = [list(to_s(ord(c), 6).rjust(3, "0")) for c in src] src = [list(str(ord(c), 6).rjust(3, "0")) for c in src]
src = [int(x, base=6)+1 for sub in src for x in sub] src = [int(x, base=6)+1 for sub in src for x in sub]
for n in src: for n in src:
if code: if code:
@@ -45,39 +47,32 @@ def __script__(src):
else: code.append("_" * n) else: code.append("_" * n)
return "\n".join(code) return "\n".join(code)
def __ruby_script__(src): def __ruby_script__(src):
return "require '_'\n\n" + __script__(src) return "require '_'\n\n" + __script__(src)
def __py_script__(src): def __py_script__(src):
return "import _\n\n" + __script__(src) return "import _\n\n" + __script__(src)
def to_be_exec(name): def to_be_exec(name):
"""Decodes _ to a fragment.""" """Decodes _ to a fragment."""
global code return [str(len(i) - 1) for i in name.split()]
global fragment
code.extend(fragment)
del fragment[:]
for i in name.split():
fragment.append(str((len(i)-1)))
code = [] code = []
fragment = [] fragment = []
def _(): def _(code):
"""Decodes all fragments to code and executes it.""" """Decodes all fragments to code and executes it."""
global code if not code:
global fragment
if not code and not fragment:
return return
code.extend(fragment) eval(bytes([int(c, 6) for c in chunks(''.join(code), 3)]))
eval(''.join([chr(to_i(c, 6)) for c in chunks(''.join(code), 3)]))
code, fragment = [], []
def _to_code(filename): def _to_code(filename):
"""Code generator for an _ module.""" """Code generator for an _ module."""
doc = open(filename, "rb").read() doc = open(filename, "rb").read()
for i in doc.split(): _(itertools.chain(*(to_be_exec(i) for i in doc.split())))
to_be_exec(i)
_()
def _install_importer(): def _install_importer():
"""Installs the module finder.""" """Installs the module finder."""