Skip to content

Instantly share code, notes, and snippets.

View ZechCodes's full-sized avatar
🏄‍♂️
Surfing on a space-time bubble

Zech Zimmerman ZechCodes

🏄‍♂️
Surfing on a space-time bubble
View GitHub Profile
@ZechCodes
ZechCodes / challenge-24.md
Created June 10, 2020 23:51
Challenge #24 - Remove Extras

Challenge #24 - Remove Extras

Create a function that takes two arguments: a list and a number. In the list (the first argument), if an element occurs more than N times (the second argument), remove the extra occurrences and return the result.

Examples

delete_occurrences([1, 1, 1, 1], 2) ➞ [1, 1]

delete_occurrences([13, True, 13, None], 1) ➞ [13, True, None]
@ZechCodes
ZechCodes / fancy_enums.py
Last active September 20, 2023 15:04
Experimenting with creating enums with auto values dynamically without having to assign `auto()`. This is a terrible idea and results in code that at times could be ambiguous. A fun excersize.
import inspect
from enum import Enum, auto, EnumType
class EnumCaptureDict(dict):
def __getitem__(self, item):
if item in self or (item.startswith("__") and item.endswith("__")):
return super().__getitem__(item)
frame = inspect.currentframe().f_back
from typing import Type, TypeVar
class Sentinel:
def __new__(cls):
instance = super().__new__(cls)
cls.__new__ = lambda _: instance
return instance
@ZechCodes
ZechCodes / unordered_tree.py
Created May 21, 2023 13:35
A simple unordered tree implementation in Python for the weekly coding prompt on the Beginner.Codes Discord server. Join now https://beginner.codes/discord
from typing import Generic, TypeVar
T = TypeVar("T")
class Node(Generic[T]):
def __init__(self, value: T, parent: "Node[T] | None" = None):
self._children = tuple()
self._parent = parent
@ZechCodes
ZechCodes / linked-list.py
Created April 30, 2023 13:35
A simple linked list implementation in Python for the weekly coding prompt on the Beginner.Codes Discord server. Join now https://beginner.codes/discord
from typing import Generic, TypeVar
T = TypeVar("T")
class Node(Generic[T]):
def __init__(self, value: T, head: "Node | None" = None, next_node: "Node | None" = None):
self.head = self if head is None else head
self.next = next_node
@ZechCodes
ZechCodes / pil_line_writer.py
Last active December 27, 2022 11:05
Simple class for writing multiline textcentered on a PIL image with even linespacing.
"""
PIL Line Writer
Simple class for writing multiline text centered on an image with even line
spacing.
To use create a line writer object by passing a PIL image object and a padding
value. Then to write text call the LineWriter's text method with the text you
would like centered on the image over multiple lines. It also takes an optional
line height argument which will scale the line heights. Additionally you can
@ZechCodes
ZechCodes / counting_bs_after_as.py
Created August 15, 2022 02:51
A crazy little async state machine that's intended to read like english as much as possible. Just download both file and run the `count_bs_after_as.py` to see it count all the B's it finds after A's in the string.
import asyncio
from state_machine import StateMachine, State
class CountBsAfterAs(StateMachine):
def __init__(self):
super().__init__()
self.num_bs_after_as = 0
@State
@ZechCodes
ZechCodes / mutation_descriptors.py
Last active March 20, 2022 15:43
Apply Mutations on Dataclass Fields
from __future__ import annotations
import json
from dataclasses import dataclass, MISSING, field as _field
from typing import Any, Callable
class MutationDescriptor:
def __init__(self, mutator: Callable[[Any], Any]):
self._mutator = mutator
@ZechCodes
ZechCodes / set_vs_list.md
Last active May 2, 2021 16:52
Sets vs Lists in Python

Sets vs Lists in Python

This is just a fun demo of how lists and sets handle the in lookup.

Code

class Testing:
    def __init__(self, name):
        self.name = name
@ZechCodes
ZechCodes / jwt_demo.py
Created April 19, 2020 15:35
Playing with RSA JWTs
import jwt
def create_token(payload, private_key):
return jwt.encode(payload, private_key, algorithm="RS256")
def decode_token(token, public_key, default=None):
try:
return jwt.decode(token, public_key, algorithms="RS256")