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
""" | |
I'm so bored right now please help | |
Anyways this module provides a pure Python context manager decorator, | |
slightly more efficient than the contextlib ones and the one deco supports | |
both sync and async functions. | |
""" | |
__all__ = ("contextmanager",) | |
__license__ = "MIT" |
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 Switch: | |
"""A shitty equivalent of the `switch` statement.""" | |
__slots__ = ("__dictionary", "__otherwise") | |
def __init__(self, dictionary, *, otherwise=None): | |
if not all(callable(value) for value in dictionary.values()): | |
raise TypeError("all values of the dictionary must be callables") | |
self.__dictionary = dictionary |
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
""" | |
Module level docstring go brrrrrrr | |
Anyways this module provides a special coroutine utility class (wrapped by a | |
decorator) that allows your coro to have a custom name and before and after | |
invoke hooks. | |
""" | |
__all__ = ("coroutine",) | |
__license__ = "MIT" |
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
from types import MappingProxyType as _Mapping | |
class _DescriptorDunderMeta(type): | |
""" | |
A metaclass that allows us to access all the builtin descriptors | |
used in the class (:class:`property`, :class:`classmethod`, | |
:class:`staticmethod`) collected into 3 groups of dunder attributes. | |
""" |
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
# 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_ |
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
from uwuify import uwu | |
class UwUifer: | |
""" | |
Extremely simple example of a context manager class which has | |
the dunder enter and __exit__ methods implemented, as well as | |
a ``do_uwu`` method to uwuify text. | |
""" |
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
AsyncioModule = __import__( | |
'asyncio'[slice(None.__new__(type(None)), None.__new__(type(None)), None.__new__(type(None)))], | |
globals.__call__(), locals.__call__(), | |
[], | |
0 | |
) | |
BuiltInStuffModule = __import__( | |
'builtins'[slice(None.__new__(type(None)), None.__new__(type(None)), None.__new__(type(None)))], | |
globals.__call__(), locals.__call__(), | |
[], |
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 Person: | |
# Here our __init__ constructor takes a single parameter: `name`. | |
# Since we have not given a default value here, if we tried to instantiate the class without passing | |
# a `name` argument, TypeError would be raised: "__init__() missing 1 required positional argument: 'name'". | |
def __init__(self, name): | |
self._name = name | |
# Here with the @property decorator we are creating a `name` attribute for our Person instance. | |
@property |
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 Car: | |
"""A class written to showcase what instance/static/class methods/properties are.""" | |
# All of the methods defined below (including __init__) will be inherited by subclasses. | |
# Here in the __init__ constructor there is a positional parameter, `name`, | |
# followed by two keyword-only arguments (or "kwargs"), `doors` and `max_speed`, | |
# which have default values of 4 and 150, respectively. | |
# In Python, any parameters following a * in a function definition will be kwargs. | |
def __init__(self, name: str = None, *, doors: int = 4, max_speed: int = 150): |