Skip to content

Instantly share code, notes, and snippets.

View mpkocher's full-sized avatar

M. Kocher mpkocher

View GitHub Profile
@mpkocher
mpkocher / leap_year.py
Last active March 15, 2020 19:12
Example of a functional-ish centric design encoding logic in a rules based model #LevelUp
#!/usr/bin/env python3
"""
Example of a functional-ish centric design and encoding logic in rules.
This uses determination of a leap year as an simple example.
This approach could be used in more involved cases to encode business logic.
"""
import operator as op
@mpkocher
mpkocher / boiler.py
Last active March 30, 2021 02:55
MK common Python utils
# Common core utils are too small that don't warrant creating a package
from argparse import ArgumentParser, Namespace, ArgumentDefaultsHelpFormatter
import csv
import functools
import logging
import sys
from typing import Callable as F
from typing import List, TypeVar, Type, Iterator
from pydantic import BaseModel
#!/usr/bin/env python3
from collections import deque
def to_tail(it, max_items):
"""
https://twitter.com/Wildvasa/status/1207383049387810816
Get the last N elements from an iterable
#!/usr/bin/env python3
import sys
from collections import Counter
# https://docs.python.org/3.8/library/collections.html?highlight=counter#collections.Counter
# Example of using collections.Counter to count item
# https://twitter.com/DavidMezzetti/status/1204493772274438151
@mpkocher
mpkocher / pylint_W0102.out
Created December 4, 2019 00:21
Netflix Metaflow pylint out of W0102 (pylint metaflow | grep W0102)
metaflow/package.py:24:4: W0102: Dangerous default value DEFAULT_SUFFIXES (builtins.list) as argument (dangerous-default-value)
metaflow/runtime.py:431:4: W0102: Dangerous default value [] as argument (dangerous-default-value)
metaflow/flowspec.py:130:4: W0102: Dangerous default value {} as argument (dangerous-default-value)
metaflow/flowspec.py:130:4: W0102: Dangerous default value [] as argument (dangerous-default-value)
metaflow/flowspec.py:256:4: W0102: Dangerous default value [] as argument (dangerous-default-value)
metaflow/pylint_wrapper.py:26:4: W0102: Dangerous default value [] as argument (dangerous-default-value)
metaflow/plugins/aws/batch/batch.py:96:4: W0102: Dangerous default value {} as argument (dangerous-default-value)
metaflow/plugins/aws/batch/batch.py:96:4: W0102: Dangerous default value {} as argument (dangerous-default-value)
metaflow/metadata/service.py:40:4: W0102: Dangerous default value [] as argument (dangerous-default-value)
metaflow/metadata/service.py:40:4: W0102: Dangerous defau
@mpkocher
mpkocher / typeddict_pad.py
Last active October 2, 2019 05:08
Exploring TypedDict in Python 3
#!/usr/bin/env python3
"""Output from mypy 0.711
typeddict_pad.py:28: error: Module 'typing' has no attribute 'TypedDict'; maybe "_TypedDict"?
typeddict_pad.py:28: error: Name 'TypedDict' already defined (possibly by an import)
typeddict_pad.py:90: error: Unsupported operand types for + ("str" and "int")
typeddict_pad.py:107: error: Argument 2 has incompatible type "int"; expected "str"
typeddict_pad.py:110: error: Unsupported operand types for + ("int" and "str")
typeddict_pad.py:118: error: "Movie" has no attribute "clear"
typeddict_pad.py:128: error: Argument 1 to "update" of "TypedDict" has incompatible type "Dict[str, object]"; expected "TypedDict({'name'?: str, 'year'?: int})"
typeddict_pad.py:137: error: Argument 1 to "update" of "TypedDict" has incompatible type "Movie"; expected "TypedDict({'name'?: str, 'year'?: int})"
#!/usr/bin/env python3
"""
https://twitter.com/AlSweigart/status/1170907280285417473
Refactored from https://pastebin.com/raw/hU1m440m by Al Sweigart [email protected]
"""
import re
import operator as op
import sys
from typing import NamedTuple, List, Any
@mpkocher
mpkocher / walrus_fstring_example.py
Last active May 9, 2020 21:35
Exploring Py 3.8 Walrus + F-string Tweet By Raymond Hettinger
#!/usr/bin/env python3
"""
Requires Python 3.8
Expanding on a Tweet from RH using walrus and f-strings
https://twitter.com/raymondh/status/1153085050650423296
"""
import logging
@mpkocher
mpkocher / environment.yml
Last active June 26, 2019 17:32
Pyviz Panel. Kicking the Tires
dependencies:
- panel>=0.6.0
- holoviews
- hvplot
- param
- matplotlib
- scipy
- conda-forge::altair
- conda-forge::plotly
- conda-forge::vtk=8.1.1
@mpkocher
mpkocher / DataClasses.ipynb
Created May 25, 2019 01:55
Overview of Dataclasses, namedtuple, typing.NamedTuple, attrs and pydantic
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.