Skip to content

Instantly share code, notes, and snippets.

@Ogaday
Ogaday / README.md
Last active March 9, 2020 12:36
Tranpose Dict

Dictionary puzzle of the week:

The objective is to write an expression or function that, given a dictionary in which each value is a list, transpose it so the result is list of dictionaries, where:

  • The keys of each new dictionary are the same as the keys of the original dictionary
  • The values of the ith dictionary correspond to the ith elements of the values of the original dictionary

If that doesn't make sense, here's an example:

# Input:
@Ogaday
Ogaday / date_range.py
Created March 9, 2020 13:05
Date Range
from datetime import datetime, timedelta
from typing import Iterator
def date_range(
start: datetime,
stop: datetime,
step: timedelta
) -> Iterator[datetime]:
"""
@Ogaday
Ogaday / accents.md
Created April 29, 2020 18:48
Accents on linux

On my set up at least:

shift + alt gr ' <vowel>: áéíóú  
shitf + alt gr ` <vowel>: àèìòù  
shift + alt gr - <vowel>: āēīōū
@Ogaday
Ogaday / README.md
Created July 20, 2020 10:25
Parsing RFC 822 timestamps

Comparison using the python date format spec to parse RFC-822 date strings vs using dateutil.

In [1]: from timeit import timeit 
   ...: from time_rfc_822 import parse_RFC_822_naive, parse_RFC_822_dateutil 
   ...: sample_date_string = 'Fri, 06 Mar 2020 14:22:22 GMT' 
   ...: %timeit parse_RFC_822_naive(sample_date_string) 
   ...: %timeit parse_RFC_822_dateutil(sample_date_string)                                                                                                                                                                                                                                
17.5 µs ± 805 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
154 µs ± 12.6 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
@Ogaday
Ogaday / mlp.py
Last active October 30, 2020 12:34
Regression on toy data using Keras
# Python 3.5
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.optimizers import SGD
from sklearn.metrics import mean_squared_error
@Ogaday
Ogaday / README.md
Last active January 13, 2022 12:59
Mocking HTTP requests in Click

Mocking HTTP Requests in Click

Simple MVP to test mocking of HTTP requests made in Click commands using the responses library.

In simple_post.py we have a Click command that makes a request to https://httpbin.org, a useful API for testing requests.

In test_simple_post.py we have a test suite which can be run by PyTest. In the first

@Ogaday
Ogaday / README.md
Last active August 7, 2022 13:33
Prefect on Dask on Kubernetes
@Ogaday
Ogaday / argmax.py
Last active December 1, 2022 19:20
Native Argmax
"""
A native implementation of argmax.
https://towardsdatascience.com/there-is-no-argmax-function-for-python-list-cd0659b05e49
Tests::
pytest --doctest-modules -vvv argmax.py
Linting::
@Ogaday
Ogaday / REGISTER.rst
Last active December 6, 2022 14:49
Function registry

Function Register

A Lightweigt function registry implementation.

Eg:

register = Register()

@register("hello")
@Ogaday
Ogaday / COMPOSE.rst
Last active December 6, 2022 15:24
Implemtation of function composition

Function Composition

A minimal compose implementation.

Given a list of functions, apply them squentially to an argument. This is equivalent to:

funcs = [h, ..., g]
compose(x, funcs) == g(...(h(x)))