Skip to content

Instantly share code, notes, and snippets.

View JakeTheCorn's full-sized avatar

Jake Corn JakeTheCorn

  • Indianapolis, IN
View GitHub Profile
@JakeTheCorn
JakeTheCorn / hex.py
Last active February 22, 2021 15:25
import unittest
# todo: make this a "parameterized" test
HEX_NUGGET_MAP = {
'0': '0000',
'1': '0001',
'2': '0010',
'3': '0011',
'4': '0100',
@JakeTheCorn
JakeTheCorn / get_all_key_values.py
Last active February 5, 2021 16:24
little util to grab all values from collection matching a key
import unittest
def get_all_key_values(
key,
container
):
result_set = set()
if not isiterable(container):
return result_set
@JakeTheCorn
JakeTheCorn / lazy_eval_looping.py
Last active July 31, 2020 14:15
Leaving this for my own record. how to implement a lazy eval loop in python
import unittest
class Container:
def __init__(self, items):
self.__cur = 0
self.__items = items
def __iter__(self):
self.__cur = 0
@JakeTheCorn
JakeTheCorn / Comparison.py
Created July 26, 2020 02:24
An idea I had to code comparisons... The idea is that you can't "have" a comparison without two things to compare, a, b... so it could go in a constructor. This could also be useful as a template method pattern.
class Comparison:
def __init__(self, a, b, **kwargs):
self.a = a
self.b = b
self.kwargs = kwargs
def compare(self):
""" .compare() -> Union[1, -1, 0]"""
if self._a_has_higher_precedence():
return 1
@JakeTheCorn
JakeTheCorn / prop_based_example.py
Created July 16, 2020 18:32
prop based testing sample (not a great one)
import unittest
from hypothesis import given, example, settings, reproduce_failure
import hypothesis.strategies as st
def is_even(v):
return v % 2 == 0
class Test(unittest.TestCase):
@JakeTheCorn
JakeTheCorn / dict_has_path.py
Created June 27, 2020 23:55
util for seeing if dictionary has path
def dict_has_path(d, path):
if not isinstance(d, dict):
return None, 'dict_has_path expected type dict for d parameter. called with %s' % d.__class__.__name__
if not isinstance(path, str):
return None, 'dict_has_path expected type str for path parameter. called with %s' % path.__class__.__name__
if path in d:
return True, None
if '.' not in path:
return False, None
sub_paths = path.split('.')
# the in operator looks in a class's __contains__ method
class A:
def __init__(self, numbers=[]):
self.__numbers = numbers
def __contains__(self, item):
return item in self.__numbers
a = A(numbers=[1, 2, 3])
@JakeTheCorn
JakeTheCorn / join_strings_with_newline_pg.sql
Created June 9, 2020 22:36
Join strings with newline character in postgres.
select concat_ws(E'\n', 'name=bob', 'age=45')
@JakeTheCorn
JakeTheCorn / errorable.js
Created June 7, 2020 18:09
little idea for a data structure that forces callers to deal with error state
class Errorable {
constructor({ value = null, error = null }) {
this.__value = value
// should this be only instance of Error or null validated?
this.__err = error
this.__err_accessed = false
}
static wrapSync(fn) {
return (...args) => {
@JakeTheCorn
JakeTheCorn / Snippet.tsx
Created May 1, 2020 13:50
lil Primitive ts code snippet display
// todo: write tests
interface SnippetProps {
val: string
}
export const Snippet = ({
val
}: SnippetProps): JSX.Element => {
const elements = []