Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import textwrap
- import smtplib
- from email.mime.text import MIMEText
- class NameThingy:
- def __init__(self, name):
- self.name = name
- def __getattribute__(self, attrname):
- name = object.__getattribute__(self, 'name')
- return NameThingy(f'{name}.{attrname}')
- def __matmul__(self, other):
- name = object.__getattribute__(self, 'name')
- othername = object.__getattribute__(other, 'name')
- return NameThingy(f'{name}@{othername}')
- class EmailMetaNamespace(dict):
- def __missing__(self, index):
- if isinstance(index, str) and not index.startswith('_'):
- return NameThingy(index)
- raise KeyError(index)
- class EmailMeta(type):
- @classmethod
- def __prepare__(cls, name, bases, **kwargs):
- return EmailMetaNamespace()
- def __new__(cls, name, bases, namespace, **kwargs):
- annotations = namespace['__annotations__']
- subject = annotations['Subject']
- from_ = object.__getattribute__(annotations['From'], 'name')
- to = object.__getattribute__(annotations['To'], 'name')
- body = textwrap.dedent(annotations['Body'])
- msg = MIMEText(body)
- msg['Subject'] = subject
- msg['From'] = from_
- msg['To'] = to
- print('Pretending to send the following email:')
- print(msg.as_string())
- Email = type.__new__(EmailMeta, 'Email', (), {})
- class Message(Email):
- From: me@gmail.com
- To: you@gmail.com
- Subject: "Let's get nachos"
- Body: '''\
- Man, wouldn't nachos be nice?
- Let's go get some.'''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement