Skip to content

Instantly share code, notes, and snippets.

View spinfish's full-sized avatar
🙂
RIP discord.py

alex spinfish

🙂
RIP discord.py
View GitHub Profile
@spinfish
spinfish / contextmanager_decorator_but_better™.py
Last active September 10, 2021 01:51
The docstrings are only there so I look like a cool n professional Pythonista or whatever, look at me go with the module level dunders too woo, god I am so cringe but yeh anyway this is a decorator I made whilst feeling very bored 😎
"""
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"
@spinfish
spinfish / shitty_switch_equivalent.py
Last active September 15, 2021 03:08
This was a stupid idea, and is going to be useless with the new match case PEP lol
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
@spinfish
spinfish / coroclass.py
Last active September 15, 2021 03:04
Coroutine utility class with before and after invoke hooks, kind of like a discord.py command
"""
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"
@spinfish
spinfish / allows_you_to_access_builtin_descriptors_as_dunders_via_a_metaclass.py
Last active September 10, 2021 01:52
On this day, 03/01/2020, I think I may have finally understood how metaclasses work. At least I think so. Here are some silly ones I made on that day to test out my new found knowledge.
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.
"""
@spinfish
spinfish / oneline_madness.py
Last active November 27, 2020 01:37
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_
@spinfish
spinfish / uwu_context_manager.py
Last active September 15, 2021 03:12
A context manager class that can uwuify text.
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.
"""
@spinfish
spinfish / evil.py
Last active September 6, 2021 08:49
Very epic discord bot that sends "Hello !" when you type "!hi"
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__(),
[],
@spinfish
spinfish / getter_setter_deleter_example.py
Last active September 18, 2020 01:17
A basic example of the Python getter, setter, and deleter methods for properties.
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
@spinfish
spinfish / subclass_example_and_more.py
Last active March 2, 2023 21:05
Here we have an example of how subclasses work in Python (how to override methods of a parent class), how to set attributes for the subclass, how class methods/static methods work and how they can be useful, and a basic coverage of what the property decorator can do.
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):