Skip to content

Instantly share code, notes, and snippets.

View philhanna's full-sized avatar

Phil Hanna philhanna

View GitHub Profile
@philhanna
philhanna / champions.ly
Created March 3, 2023 06:20
Comparison of Rite of Spring intro to "We Are The Champions"
\version "2.20.0"
#(ly:set-option 'midi-extension "mid")
\paper {
#(set-paper-size "letter")
top-margin = 2\cm
left-margin = 2\cm
right-margin = 2\cm
ragged-bottom = ##t
@philhanna
philhanna / EmulateSet.go
Last active June 30, 2023 18:18
Emulate a Python set type in Go
package main
import "fmt"
func ListToSet[T comparable](list []T) []T {
m := make(map[T]bool)
for _, r := range list {
m[r] = true
}
set := make([]T, 0)
@philhanna
philhanna / __init__.py
Last active November 15, 2022 04:59
Test data root for unit test classes
from pathlib import Path
project_root = Path(__file__).parent.parent
testdata = project_root.joinpath("testdata")
__all__ = [
'testdata',
]
@philhanna
philhanna / logging.py
Last active September 11, 2022 17:06
Basic logging
import logging
import sys
logging.basicConfig(level=logging.INFO,
format="%(levelname)s %(asctime)s %(module)s:%(lineno)d %(funcName)s() %(message)s",
datefmt="%H:%M:%S")
ch = logging.StreamHandler(stream=sys.stdout)
ch.flush()
@philhanna
philhanna / abstract_bass_class.py
Created August 12, 2022 18:06
How to use abstract base classes
import logging
from abc import ABC, abstractmethod
class Animal(ABC):
""" Abstract base class for animals """
@abstractmethod
def saying(self) -> str:
""" What sound does this animal make? """
@philhanna
philhanna / uigrid.py
Created July 10, 2020 00:10
delete in SQLAlchemy
userid = 1 # TODO remove hard-coded userID
query = DBGrid.query.filter_by(userid=userid, gridname=gridname)
oldgrid = query.first()
if oldgrid:
db.session.delete(oldgrid)
db.session.commit()
@philhanna
philhanna / uigrid.py
Created July 10, 2020 00:01
Insert vs. update in SQLAlchemy
# Save the file
userid = 1 # TODO Replace hard coded user id
query = DBGrid.query.filter_by(userid=userid, gridname=gridname)
if not query.all():
# No grid in the database. This is an insert
logging.debug(f"Inserting grid '{gridname}' into grids table")
created = modified = datetime.now().isoformat()
newgrid = DBGrid(userid=userid,
gridname=gridname,
created=created,
@philhanna
philhanna / gist:38ca6fb02a87ffe84a30e928d4f85cd0
Created July 2, 2020 05:09
Recovering from git reset --HARD
Yes, YOU CAN RECOVER from a hard reset in git.
Use:
git reflog
to get the identifier of your commit. Then use:
git reset --hard <commit-id-retrieved-using-reflog>
This trick saved my life a couple of times.
@philhanna
philhanna / undo.py
Last active April 27, 2023 16:25
Logic for undo/redo
class UndoRedo:
""" Provides coordinated support for undo and redo """
def __init__(self):
self.current_value = None
self.undo_stack = Stack()
self.redo_stack = Stack()
# Set a new value
def set_value(self, new_value):
@philhanna
philhanna / gist:e46f23f144fab1a4a13baf65cdb4617a
Created June 19, 2020 12:43
Remove __pycache__ directories recursively
find . -name '__pycache__' | xargs -i rm -rf {}