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