Last active
November 22, 2019 10:59
-
-
Save L3viathan/92addec9501969ae628c90b9100f3177 to your computer and use it in GitHub Desktop.
Email addresses as valid python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Mail: | |
def __init__(self, **bindings): | |
self.bindings = bindings | |
def send(self, **bindings): | |
self.bindings.update(bindings) | |
return """Sending email with: | |
{}""".format( | |
"\n ".join(f"{key}: {val}" for key, | |
val in self.bindings.items()) | |
) | |
def mail_magic(bytecode, consts, names, varnames): | |
byterator = iter(bytecode) | |
stack = [] | |
bindings = {} | |
try: | |
while True: | |
code = next(byterator) | |
if code == ord(b"t"): # LOAD_GLOBAL | |
register = next(byterator) | |
name = names[register] | |
stack.append(name) | |
elif code == ord(b"j"): # LOAD_ATTR | |
register = next(byterator) | |
name = names[register] | |
stack.append("{}.{}".format(stack.pop(), name)) | |
elif code == ord(b"\x10"): # MATMUL | |
assert next(byterator) == 0 | |
second = stack.pop() | |
first = stack.pop() | |
stack.append(f"{first}@{second}") | |
elif code == ord(b"}"): # STORE_FAST | |
register = next(byterator) | |
variable = varnames[register] | |
value = stack.pop() | |
bindings[variable] = value | |
elif code == ord(b"d"): # LOAD_CONST | |
register = next(byterator) | |
const = consts[register] | |
stack.append(const) | |
elif code == ord(b"S"): # RETURN | |
register = next(byterator) | |
assert register == 0 | |
elif code == ord(b"\x17"): # ADD | |
assert next(byterator) == 0 | |
second = stack.pop() | |
first = stack.pop() | |
stack.append(f"{first}+{second}") | |
elif code == ord(b"\x18"): # MINUS | |
assert next(byterator) == 0 | |
second = stack.pop() | |
first = stack.pop() | |
stack.append(f"{first}-{second}") | |
elif code == ord(b"\x16"): # MODULO | |
assert next(byterator) == 0 | |
second = stack.pop() | |
first = stack.pop() | |
stack.append(f"{first}%{second}") | |
elif code == ord(b"\x14"): # TIMES | |
assert next(byterator) == 0 | |
second = stack.pop() | |
first = stack.pop() | |
stack.append(f"{first}*{second}") | |
elif code == ord(b"B"): # BINOR | |
assert next(byterator) == 0 | |
second = stack.pop() | |
first = stack.pop() | |
stack.append(f"{first}|{second}") | |
elif code == ord(b"\x01"): | |
assert next(byterator) == 0 | |
else: | |
print("Unknown symbol", code, repr(code)) | |
break | |
except StopIteration: | |
pass | |
return bindings, stack | |
def mail(fn): | |
code = fn.__code__ | |
bindings, stack = mail_magic( | |
code.co_code, | |
code.co_consts, | |
code.co_names, | |
code.co_varnames, | |
) | |
if bindings: | |
return Mail(**bindings) | |
return stack[0] | |
if __name__ == '__main__': | |
def some_email(): | |
sender = me@some.company.io | |
recipient = your.name+spam@gmail.com | |
subject = "Here's a message for you:" | |
def other_mail(): orga@de.pycon.org | |
print(some_email.send(body="Hello")) | |
print("Address:", mail(lambda: me@foo-bar%bat*boo|spam.com)) | |
print("Address:", other_mail) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment