Skip to content

Instantly share code, notes, and snippets.

@spinfish
Last active November 27, 2020 01:37
Show Gist options
  • Save spinfish/666f4c3f28cdf5bf5b5438834e223f84 to your computer and use it in GitHub Desktop.
Save spinfish/666f4c3f28cdf5bf5b5438834e223f84 to your computer and use it in GitHub Desktop.
random oneline stuff
# 1: class with custom metaclass and many of the dunder methods defined
exec("""import asyncio\nclass A(metaclass=meta):\n\tasync def __aenter__(self): return self\n\tasync def __aexit__(self, *args): return False\n\t__abs__ = abs_\n\t__add__ = add\n\t__aiter__ = aiter_\n\tasync def __anext__(self):\n\t\tself._a_increasing += 1\n\t\tif self._a_increasing > 10: raise StopAsyncIteration\n\t\treturn self._a_increasing\n\t__await__ = await_\n\t__bool__ = bool_\n\t__bytes__ = bytes_\n\t__call__ = call_\n\t__complex__ = complex_\n\t__contains__ = contains_\n\t__delattr__ = delattr_\n\t__delitem__ = delitem_\n\t__dir__ = dir_\n\t__enter__ = enter_\n\t__eq__ = eq\n\t__exit__ = exit_\n\t__float__ = float_\n\t__getattribute__ = getattribute_\n\t__getitem__ = getitem\n\t__index__ = index_\n\t__init__ = init\n\t__int__ = int_\n\t__iter__ = iter_\n\t__getitem__ = getitem\n\tdef __next__(self):\n\t\tself._s_increasing += 1\n\t\tif self._s_increasing > 10: raise StopIteration\n\t\treturn self._s_increasing\n\t__len__ = len_\n\t__reversed__ = reversed_\n\t__setattr__ = setattr_\n\t__setitem__ = setitem_\n\t__slots__ = slots\n\t__repr__ = repr_\n\t__str__ = str_\n\tasync def _awaitable(self):\n\t\tprint('why hello there')\n\tprop = prop_\n\tprint('hello before __new__')\nasync def main():\n\tasync with A() as ins:\n\t\tawait ins._awaitable()\n\tprint(abs(A()))\n\tprint(A() + 'rgh')\n\tasync for i in A():\n\t\tprint(i)\n\tawait A()\n\tprint(bool(A()))\n\tprint(bytes(A()))\n\tA()()\n\tprint(complex(A()))\n\tprint('a' in A())\n\ttry:\n\t\tdelattr(A(), 'x')\n\texcept AttributeError:\n\t\tprint('encountered attributeerror when tryna delete ``x``')\n\ttry:\n\t\tdel A()['a']\n\texcept KeyError:\n\t\tprint('encountered keyerror when doing ``del A()["a"]``')\n\tprint(dir(A()))\n\twith A() as ins2:\n\t\tawait ins2._awaitable()\n\tprint(float(A()))\n\tprint(A().prop)\n\ttry:\n\t\tprint(A()['a'])\n\texcept KeyError:\n\t\tprint('encountered keyerror when doing ``A()["a"]``')\n\tprint(int(A()))\n\tprint(len(A()))\n\tprint(reversed(A()))\n\tprint(repr(A()))\n\tprint(str(A()))\nasyncio.run(main())""", {'abs_': (lambda self: 1), 'add': (lambda self, other: (str(self) + other if isinstance(other, str) else str(self) + other.__class__.__name__)), 'aiter_': (lambda self: self), 'await_': (lambda self: self._awaitable().__await__()), 'bool_': (lambda self: True), 'bytes_': (lambda self: b'aaaaa'), 'call_': (lambda self, *args, **kwargs: print('i do nothing special lol')), 'complex_': (lambda self: complex(self.__index__())), 'contains_': (lambda self, item: item in self.__dict__), 'delattr_': (lambda self, item: super(object, self).__delattr__(item)), 'delitem_': (lambda self, key: self.__dict__.pop(key)), 'dir_': (lambda self: super(object, self).__dir__()), 'enter_' : (lambda self: self), 'eq': (lambda self, other: super(object, self).__eq__(other)), 'exit_': (lambda self, *args: False), 'float_': (lambda self: 420.69), 'getattribute_': (lambda self, name: object.__getattribute__(self, name)), 'getitem': (lambda self, key: self.__dict__[key]), 'index_': (lambda self: 69), 'init': (lambda self, prop='hidden': (setattr(self, '_prop', prop) or setattr(self, '_a_increasing', 0) or setattr(self, '_s_increasing', 0))), 'int_': (lambda self: 420), 'iter_': (lambda self: self), 'len_': (lambda self: 69420), 'repr_': (lambda self: f'<{self.__class__.__name__} prop={self.prop} special={self.special}>'), 'reversed_': (lambda self: reversed(dir(self))), 'setattr_': (lambda self, name, value: object.__setattr__(self, name, value)), 'setitem_': (lambda self, key, value: self.__dict__.update({key: value})), 'slots': ('__dict__', '_prop'), 'str_': (lambda self: self.prop), 'prop_': property(lambda self: self._prop), 'meta': (type('special_metaclass', (type,), {'__new__': (lambda cls, *args: (lambda returned: (setattr(returned, 'special', property(lambda self: "i'm special")) or print('this happens after class execution') or returned))(type.__new__(cls, *args))), '__prepare__': classmethod(lambda cls, *args, **kwargs: (print('this happens before class execution') or super(cls, cls).__prepare__(cls, *args, **kwargs)))}))})

1

output:

this happens before class execution
hello before __new__
this happens after class execution
why hello there
1
hiddenrgh
1
2
3
4
5
6
7
8
9
10
why hello there
True
b'aaaaa'
i do nothing special lol
(69+0j)
False
encountered attributeerror when tryna delete ``x``
encountered keyerror when doing ``del A()["a"]``
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__self_class__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__thisclass__']
why hello there
420.69
hidden
encountered keyerror when doing ``A()["a"]``
420
69420
<list_reverseiterator object at 0x7fb800b611c0>
<A prop=hidden special=i'm special>
@Mad880
Copy link

Mad880 commented Nov 26, 2020

wtf lol

@spinfish
Copy link
Author

wtf lol

yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment