Skip to content

Instantly share code, notes, and snippets.

View AndrewOwenMartin's full-sized avatar

Andrew Owen Martin AndrewOwenMartin

View GitHub Profile
@AndrewOwenMartin
AndrewOwenMartin / decorator_example.py
Created November 9, 2021 14:43
Example of a Python decorator which accepts parameters.
import functools
import inspect
# my_meta = {
# "a": "ay",
# "b": "bee",
# }
#
#
# def metadecorator(name, meta=None):
@AndrewOwenMartin
AndrewOwenMartin / mypy_example.py
Created November 9, 2021 14:42
Example of Protocol class in Mypy
from typing import Any, Union, Dict
from typing_extensions import TypedDict, Protocol
class FooMeta:
meta = dict(a=1)
class BarMeta:
@AndrewOwenMartin
AndrewOwenMartin / concordance_dat.py
Created September 9, 2020 10:50
A reader for the concordance DAT file format
import collections
import logging, pathlib
import csv
log = logging.getLogger(__name__)
def read_dat(path):
with path.open("r", encoding="latin1") as f:
@AndrewOwenMartin
AndrewOwenMartin / string_search.py
Created August 31, 2020 14:59
An implementation of String Search using the SDS Library.
import sds
import random
import functools
def microtest(hyp, offset, search_space, model):
""" Performs a partial evaluation of a hypothesis.
- hyp: A location in the search space.
- offset: The part of the hypothesis to be evaluated.
- search_space: The full search space.
@AndrewOwenMartin
AndrewOwenMartin / simple_sds.py
Last active August 31, 2020 14:35
A simple Python3 implementation of SDS String Search.
import random
from collections import Counter
search_space = "xxxhelxxxelloxxxxxxhelloxxxxx"
model = "hello"
tests = [
lambda hyp: search_space[hyp] == model[0],
lambda hyp: search_space[hyp + 1] == model[1],
@AndrewOwenMartin
AndrewOwenMartin / fibonacci.py
Created May 4, 2020 22:34
Four ways of generating the Fibonacci sequence in Python
# Tail recursive fibonacci
def get_fib(final, num=1, current=1, prev=0):
if final == 0:
return 0
if num < final:
prev, current = current, prev + current
return get_fib(final, num + 1, current, prev)
else:
return current
@AndrewOwenMartin
AndrewOwenMartin / poison.py
Created May 4, 2020 22:32
Poison in Python
import random
class Poison:
_artist = "Alice Cooper"
_album = "Trash"
_released = 1989