Python is Magic

From Esolang
Jump to navigation Jump to search

Python is Magic is a very restricted version of Python 3. On Python is Magic, only magic functions can be used.

Warm-up

This is a valid Python is Magic code:

__name__.__class__.__name__.__len__().__sub__(__name__.__class__.__name__.__len__())

That's good, but not pretty. To prettify it, you can remove the underscores:

name.class.name.len().sub(name.class.name.len())

It will result in the same code as above.

Literals, normal if-else, declaring classes normally, anything that is not magic functions are not allowed in Python is Magic. Other than that, it's just an unmodified Python 3.

Programming in Python is Magic

Constants

Here are the constants of Python is Magic.

Constant Code
False __name__.__len__().__bool__().__xor__(__name__.__len__().__bool__())
True __name__.__len__().__bool__()
1 __name__.__len__().__bool__().__int__()
7 __name__.__len__.__name__.__len__()
a name.iter().class.name.getitem(name.len().bool().class.name.len().add(name.len().bool().class.name.len()))
() __name__.__getnewargs__().__class__()
[] __name__.__dir__().__class__()

Variables

AFAIK, there is no way to store variables the normal way. You can, however, use __builtins__ to store data.

Example codes

Hello, world!

__builtins__.__getattribute__(__doc__.__repr__.__name__.__getitem__(__doc__.__class__.__class__.__name__.__len__()).__add__(__doc__.__str__.__name__.__getitem__(__doc__.__class__.__class__.__name__.__len__()).__add__(__name__.__len__().__class__.__name__)))(__builtins__.__getattribute__(__annotations__.__class__.__name__.__getitem__(__annotations__.__str__().__len__()).__add__(__doc__.__hash__.__name__.__getitem__(__annotations__.__str__().__len__()).__add__(__name__.__class__.__name__.__getitem__(__annotations__.__str__().__len__()))))(__name__.__len__().__mul__(__name__.__len__().__invert__().__neg__())).__add__(__doc__.__repr__.__name__.__getitem__(__name__.__class__.__name__.__len__()).__add__(__name__.__mul__.__name__.__getitem__(__doc__.__class__.__class__.__name__.__len__()).__mul__(__annotations__.__str__().__len__()).__add__(__doc__.__bool__.__name__.__getitem__(__name__.__class__.__name__.__len__()).__add__(__builtins__.__getattribute__(__doc__.__dir__().__class__.__name__)(__name__).__str__().__getitem__(__doc__.__class__.__class__.__name__.__len__()).__add__(__name__.__eq__.__doc__.__getitem__(__name__.__eq__.__name__.__len__()).__add__(__builtins__.__dir__().__getitem__(__name__.__class__.__name__.__len__().__mul__(__doc__.__class__.__class__.__name__.__len__().__mul__(__name__.__sizeof__.__name__.__len__()))).__getitem__(__doc__.__class__.__class__.__name__.__len__()).__add__(__doc__.__bool__.__name__.__getitem__(__name__.__class__.__name__.__len__()).__add__(__doc__.__str__.__name__.__getitem__(__doc__.__class__.__class__.__name__.__len__()).__add__(__name__.__mul__.__name__.__getitem__(__doc__.__class__.__class__.__name__.__len__()).__add__(__name__.__mod__.__name__.__getitem__(__doc__.__class__.__class__.__name__.__len__()).__add__(__builtins__.__getattribute__(__annotations__.__class__.__name__.__getitem__(__annotations__.__str__().__len__()).__add__(__doc__.__hash__.__name__.__getitem__(__annotations__.__str__().__len__()).__add__(__name__.__class__.__name__.__getitem__(__annotations__.__str__().__len__()))))(__name__.__class__.__name__.__len__().__mul__(__name__.__getitem__.__name__.__len__()))))))))))))))

Implementation

The following is a basic implementation of Python is Magic:

import re, sys
allowed = r"A-Za-z().,_"
regex = r"(?:__)?([{}]+)(?:__)?".format(allowed)
banned = r"[^{}]".format(allowed)
script = open(sys.argv[1]).read()

if re.findall(banned, script): 
	raise SyntaxError("Only letters, parenthesis, dot and underscore are allowed.")
exec(re.sub(regex,r"__\1__",script))