Skip to content

Instantly share code, notes, and snippets.

View ptmcg's full-sized avatar

Paul McGuire ptmcg

View GitHub Profile
@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 / 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 / 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 / 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
[
{"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
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(
@ptmcg
ptmcg / plusminus_dice_roller_demo.html
Created June 1, 2022 23:07
Pyscript+plusminus Dice Roller
<html>
<head>
<title>Plusminus Dice Roller</title>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<py-env>
- plusminus
</py-env>
</head>
@ptmcg
ptmcg / pyscript_littletable_iris.html
Last active May 19, 2022 03:28
Demonstration of CSV import using littletable in pyscript
<html>
<head>
<script defer src="https://pyscript.net/alpha/pyscript.min.js"></script>
<py-env>
- littletable
</py-env>
</head>
<body>
<h2>Iris data</h2>
@ptmcg
ptmcg / pyscript_pyparsing_arith_eval.html
Last active May 19, 2022 21:36
PyScript demo of a pyparsing arithmetic parser/evaluator
<html>
<head>
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
<table cellpadding=30 border=2>
<tr>
<td>
@ptmcg
ptmcg / pyparsing_diagram.html
Last active May 28, 2022 03:07
PyScript runs pyparsing code to generate railroad diagram
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<py-env>
- pyparsing==3.0.9
- railroad-diagrams
- jinja2
</py-env>
<py-script>