Talk:Python is Magic

From Esolang
Jump to navigation Jump to search

Equivalent to Python

So this esolang can do anything that Python can. Here's my proof:

""           = name.class.call()
True         = name.len().bool()
0            = "".len()
1            = True.int()
2            = 1 .add(1)
3            = 2 .add(1)
and so on, to create any nonnegative integer.
"type"       = name.class.class.name
"e"          = "type".getitem(3)
"__invert__" = 0 .invert.name
"v"          = "__invert__".getitem(4)
0.0          = 0 .truediv(1)
"float"      = 0.0.class.name
"a"          = "float".getitem(3)
"l"          = "float".getitem(1)
"eval"       = "e".add("v").add("a").add("l")
eval         = builtins.getattribute("eval")
"__call__"   = name.class.call.name
"c"          = "__call__".getitem(2)
"__hash__"   = name.hash.name
"h"          = "__hash__".getitem(2)
"__rmul__"   = name.rmul.name
"r"          = "__rmul__".getitem(2)
"chr"        = "c".add("h").add("r")
chr          = builtins.getattribute("chr")
Apply chr to each integer to get Unicode characters.
Concatenate characters with .add, as with "eval" and "chr".
Use eval on the concatenated string to evaluate any expression.
To run any code, simply wrap it in exec in the expression to evaluate!

QED. Oh, and I fixed your interpreter.

import re, sys
with open(sys.argv[1]) as f: src = f.read()
crange = lambda b: set(chr(c) for c in range(ord(b[0]), ord(b[1])+1))
assert set(src) <= crange("AZ") | crange("az") | set("._(), "), "Invalid code"
exec(re.compile(r"((?:__)?)(\w+)\1").sub(r"__\2__", src))

--Monochromeninja, Python programmer (talk) 13:26, 4 April 2022 (UTC) I made a generator according to this:

def getint1(x):
    one='__name__.__len__().__bool__().__int__()'
    one+=('.__add__('+one+')')*(x-1)
    return one
def getint2(x):
    one = '__name__.__len__().__bool__().__int__()'
    two=one+'.__add__('+one+')'
    if x==1:
        return one
    if x==2:
        return two
    return getint2(x>>1)+'.__mul__('+two+')'+('.__add__('+one+')')*(x&1)
def getint(x):
    g1,g2=getint1(x),getint2(x)
    if len(g1)<len(g2):
        return g1
    return g2
def getchar(x):
    two=getint(2)
    c='__name__.__class__.__call__.__name__.__getitem__('+two+')'
    h='__name__.__hash__.__name__.__getitem__('+two+')'
    r='__name__.__rmul__.__name__.__getitem__('+two+')'
    cchr='__builtins__.__getattribute__('+c+'.__add__('+h+').__add__('+r+'))'
    return cchr+'.__call__('+x+')'
def geteval(x):
    one,three,four=getint(1),getint(3),getint(4)
    e='__name__.__class__.__class__.__name__.__getitem__('+three+')'
    v='__name__.__class__.__call__().__len__().__invert__.__name__.__getitem__('+four+')'
    a='__name__.__class__.__call__().__len__().__truediv__('+one+').__class__.__name__.__getitem__('+three+')'
    l = '__name__.__class__.__call__().__len__().__truediv__(' + one + ').__class__.__name__.__getitem__(' + one + ')'
    eeval = '__builtins__.__getattribute__(' + e + '.__add__(' + v + ').__add__(' + a + ').__add__('+l+'))'
    return eeval+'.__call__('+x+')'
def magicify(code):
    if not code:
        return ''
    code='exec('+repr(code)+')'
    res=getchar(getint(ord(code[0])))
    for i,j in enumerate(code):
        if i==0:
            continue
        res=res+'.__add__('+getchar(getint(ord(j)))+')'
    return geteval(res)

converts Python code to Python is Magic code. --None1 (talk) 10:54, 19 February 2024 (UTC)

The Hello World program actually prints Hello, iorld!

--None1 (talk) 11:18, 30 January 2024 (UTC)

I'd like to correct you on that, it prints Hello, korld!. Anyway, I'll go make a better program now. --PkmnQ (talk) 12:15, 30 January 2024 (UTC)

This esolang is interesting, I'm currently working on a Python obfuscator and notice that this might be an idea.--None1 (talk) 10:24, 1 February 2024 (UTC)