Skip to content

Instantly share code, notes, and snippets.

View elnygren's full-sized avatar

el3ng elnygren

  • Helsinki, Finland
View GitHub Profile
@elnygren
elnygren / test.py
Created August 8, 2021 13:43
Testing DI enabled Python code.
import pytest
from unittest.mock import MagicMock
@pytest.fixture()
def patch_user_service():
"""
reusable patcher,
handy when there are lots of test cases
"""
user_connector = MagicMock()
@elnygren
elnygren / patch.py
Created August 8, 2021 12:15
Patching in Python with pytest using monkeypatch and requests_mock
import requests
API_URL = "https://example.com" # often imported from settings
#
# Code
#
def get_user_with_org(id: str):
"""Gets a User object with the User's Organisation"""
@elnygren
elnygren / DI.py
Last active August 8, 2021 13:24
Simple Dependency Injection in Python
# I'm using Django models as inspiration in this example
from mymodels import User, Org
# You could also call these Getters or DAOs etc
# The point is that they perform some kind of
# side effect like talking to a DB or an API
@dataclass(frozen=True)
class UserConnector:
def get_user(id: str) -> User:
@elnygren
elnygren / DI.py
Last active August 12, 2021 15:01
Functional Dependency Injection in TypeScript
# dataclass saves the trouble of writing __init__ implementations
# frozen=True makes the class instances immutable (so no hidden mutable state)
@dataclass(frozen=True)
class UserConnector:
def get_user(id: str) -> User:
return User.objects.get(id=id)
@dataclass(frozen=True)
class OrgConnector:
def get_org(id: str) -> Org:
@elnygren
elnygren / Assignment.md
Last active November 3, 2020 14:42
FlatMapFilter

Assignment 1

Code filter function by using Array.map and Array.flat

Example usage:

> filter([1,2,3], x => x !== 2)
[1,3]
@elnygren
elnygren / id.sql
Last active April 2, 2020 13:47
PLPGSQL Order ID Calculation
-- needed for `gen_random_bytes`
CREATE EXTENSION IF NOT EXISTS pgcrypto;
/**
Generate a unique Order ID based on current_timestamp, base32 and two bits of entropy.
If two Orders are created at the exact same millisecond, we rely on the entropy to provide uniqueness.
Usage:
> select * from generate_order_id();
@elnygren
elnygren / snowflake.sql
Last active November 17, 2022 21:14
PostgreSQL generate unique sorted IDs for timestamps
-- taken from https://rob.conery.io/2014/05/28/a-better-id-generator-for-postgresql/
create schema idgen;
create sequence idgen.global_id_sequence;
CREATE OR REPLACE FUNCTION idgen.id_generator(
myts timestamp,
OUT result bigint) AS $$
DECLARE
our_epoch bigint := 1314220021721;
@elnygren
elnygren / MyGraphql.re
Created July 30, 2019 10:01
Using ReasonApollo with a configurable wrapper
module type MyGraphQLMutation = {
module GQL: ReasonApolloTypes.Config;
include (module type of ReasonApolloMutation.Make(GQL));
};
module type MyGraphQLQuery = {
module GQL: ReasonApolloTypes.Config;
include (module type of ReasonApolloQuery.Make(GQL));
};
@elnygren
elnygren / I18n.re
Last active January 16, 2020 15:48
Simple ReasonML i18n internationalization example with strong static typing and JS/TS support with GenType
/** Schema for translations, with @genType for TS/JS usage */
[@genType]
type translations = {
helloMessage: string,
helloMessageWithVariable: (~name: string) => string,
};
/** English translations */
[@genType]
let translationsEN = {