Skip to content

Instantly share code, notes, and snippets.

View justanr's full-sized avatar
🏴

anr (they/them) justanr

🏴
View GitHub Profile
@justanr
justanr / _core.py
Last active December 14, 2023 02:47
Clean Architecture In Python
from abc import ABC, ABCMeta, abstractmethod
from collections import namedtuple
from itertools import count
PayloadFactory = namedtuple('PayloadFactory', [
'good', 'created', 'queued', 'unchanged', 'requires_auth',
'permission_denied', 'not_found', 'invalid', 'error'
])
"""
@justanr
justanr / deprecate.py
Last active April 16, 2018 03:03
Setting up deprecations of Pluggy Hooks
import sys
import warnings
import inspect
from pluggy import HookimplMarker, HookspecMarker, PluginManager, _HookCaller, normalize_hookimpl_opts, HookImpl
class MetadataHookspecMarker(HookspecMarker):
"""
Allows storing arbitrary metadata on the hookspec options
instead of what Pluggy sets by default.
"""
@justanr
justanr / app.py
Last active February 15, 2018 12:30
Quotly
from flask import Flask, request
from flask.ext.restful import Resource, Api
from flask.ext.sqlalchemy import SQLAlchemy
from marshmallow import Schema, post_dump
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///quotes.db'
api = Api(app)
from datetime import datetime
SUNDAY = 6
class UncleVernonMiddleware(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
if environ['REQUEST_METHOD'].lower() == 'post' and datetime.now().weekday() == SUNDAY:
from abc import ABCMeta, abstractmethod
ABC = ABCMeta('ABC', (object,), {})
class ConversionStrategy(ABC):
@abstractmethod
def convert(self, a):
raise NotImplementedError
class IntegerConversionStrategy(ConversionStrategy):
@justanr
justanr / money.py
Last active July 20, 2016 11:43
Example of a small value object representing money.
import decimal
from functools import total_ordering
from numbers import Real
class Context(object):
def __init__(self, **kwargs):
self.context = decimal.Context(**kwargs)
def __enter__(self):
with decimal.localcontext(self.context) as c:
@justanr
justanr / example.py
Last active June 15, 2016 02:34
Proof of Concept for adding kinda Implicits to Python
class Thing:
def r(self):
pass
class HasWhatever(metaclass=ImplicitMeta, companion=Thing):
whatever = lambda s: print(s, 1)
@property
def nine(self):
return 'nine'
@justanr
justanr / base_repository.py
Last active May 24, 2016 04:13
Toy Repository
from operator import attrgetter
from hashlib import md5 # because it's fast
class BaseRepository(object):
def find(self, pk):
raise NotImplementedError()
def find_by(self, **keys):
raise NotImplementedError()
@justanr
justanr / commandbus.py
Last active May 24, 2016 04:10
Basic Command Bus example
import inspect
class HandlerNotFound(Exception):
pass
class BasicCommandBus(object):
def execute(self, command):
[tox]
envlist = cov-init,py27,py34,py35,py36,cov-report
[testenv]
usedevelop = True
setenv =
COVERAGE_FILE = .coverage.{envname}
PYTHONDONTWRITEBYTECODE = pls
commands =
py.test --cov={toxinidir}/flask_sleepy --cov-report term-missing