Skip to content

Instantly share code, notes, and snippets.

View ptmcg's full-sized avatar

Paul McGuire ptmcg

View GitHub Profile
import typing
from functools import lru_cache
T = typing.TypeVar("T")
PredicateFunction = typing.Callable[[T], bool]
SourceIterable = typing.Iterable[T]
ObjectList = typing.List[T]
def splitby(
[
{"id": "1", "title": "PEP Purpose and Guidelines", "authors": "Warsaw, Hylton, Goodger, Coghlan", "discussions_to": null, "status": "Active", "type": "Process", "topic": "", "created": "13-Jun-2000", "python_version": null, "post_history": "21-Mar-2001, 29-Jul-2002, 03-May-2003, 05-May-2012, 07-Apr-2013", "resolution": null, "requires": null, "replaces": null, "superseded_by": null, "url": "https://peps.python.org/pep-0001/", "abstract": ""},
{"id": "2", "title": "Procedure for Adding New Modules", "authors": "Cannon, Faassen", "discussions_to": null, "status": "Active", "type": "Process", "topic": "", "created": "07-Jul-2001", "python_version": null, "post_history": "07-Jul-2001, 09-Mar-2002", "resolution": null, "requires": null, "replaces": null, "superseded_by": null, "url": "https://peps.python.org/pep-0002/", "abstract": "\nIntroduction\nThe Python Standard Library contributes significantly to Python's\nsuccess. The language comes with \"batteries included\", so it is easy\nfor people to become produ
@ptmcg
ptmcg / events.md
Last active May 19, 2023 17:08
Textual Class Diagrams

Diagrams

Events

classDiagram
    
    class Message {
        time: float
        is_forwarded: bool
        handler_name: str
@ptmcg
ptmcg / job_state.py
Last active October 2, 2023 19:00
State pattern in Python
import contextlib
class InvalidStateActionError(NotImplementedError):
pass
class InvalidStateTransitionError(InvalidStateActionError):
pass
@ptmcg
ptmcg / generators_of_generators.py
Last active November 29, 2023 23:36
Wrapping iterators in generators in generators in ...
# remove pesky trailing newlines from iterating over a text file
def denewlining(line_iterator):
for line in line_iterator:
yield line.rstrip("\n\r")
with open(__file__) as infile:
for line in denewlining(infile):
print(f"{line!r} <-- look ma! no trailing newlines!")
@ptmcg
ptmcg / logger_handler_demo.py
Last active March 11, 2024 04:46
Demonstration of setting log level in a handler overrides setting log level in a logger
# Demonstrates that setting the log level in a handler overrides any setting of log level in the logger
#
# The purpose of this is to support adding handlers that take extra action or route log messages to other
# destinations for any logs at higher levels. For instance, creating and adding a handler to the root
# logger where the handler's level is set to ERROR, and streams its logs to a notification service queue.
# Even if the logger's level is set to a lower level of logging, the specialized handler only routes
# ERROR or higher log messages to the notification service.
#
# The problem arises if the handler that is set to a particular log level is the root logger's sole
# stdout handler. In this case, it becomes difficult to change the log level on the logger, because the
@ptmcg
ptmcg / iter_sieve.py
Last active July 16, 2024 16:55
Sieve of Eratosthenes - using filtering iterators
import functools
import itertools
from collections.abc import Iterator
def is_multiple_of(p: int, x: int) -> bool:
# if x % p == 0:
# print("discarding", x, "multiple of", p)
return x % p == 0