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 / omit.py
Last active February 7, 2021 15:02
lil' python omit util
# todo: write non-recursive form
import unittest
import copy
from typing import Union, List
def omit(v: dict, paths: List[str]) -> (Union[dict, None], Union[str, None]):
if not isinstance(v, dict):
msg = 'omit expected dict type, received %s' % v.__class__.__name__
return None, msg
@JakeTheCorn
JakeTheCorn / from_obj_path.py
Last active April 12, 2020 23:02
lil' python obj plucking util
import unittest
from typing import List
FALLBACK_FLAG = '_______FALLBACK_FLAG'
def from_obj_path(obj, path, fallback = None):
""" from_obj_path({'name': {'first': 'bo'}}, 'name.first') -> 'bo'"""
d = dict(obj)
if '.' not in path:
val = d.get(path, FALLBACK_FLAG)
@JakeTheCorn
JakeTheCorn / visitor_traverser.py
Last active April 5, 2020 16:53
This is for traversing arbitrary values, maybe trees, and applying a transformation to each node.
import inspect
from typing import (Callable, Optional, Any)
import unittest
def build_traverser(*, visitor: Callable[[Any, Optional[str]], Any]) -> Callable[[Any], Any]:
func = visitor
# would prefer to insert a map here instead positional args. Don't have time for the moment on applying this here.
if len(inspect.signature(func).parameters.values()) < 2:
def visitor_wrapper(val, _key=None):
return visitor(val)
@JakeTheCorn
JakeTheCorn / nullify_empty_sub_dicts.py
Created March 29, 2020 05:02
nullifies empty sub dicts
import unittest
def nullify(v: dict) -> dict or None:
if not isinstance(v, dict):
return v
if not bool(v):
return None
d = {}
@JakeTheCorn
JakeTheCorn / up.sh
Created February 18, 2020 01:44
bash up function usage: up 3 instead of cd ../../.. (not sure where I got this)
up() {
local n=$1
for (( i=1; i<=$n; i++ ))
do
cd ..
done
}
@JakeTheCorn
JakeTheCorn / map.js
Created February 12, 2020 18:48
todo: write tests. little recursive function fun.
function map(fn) {
let newArr = []
let idx = 0
return function mapper(arr) {
if (arr.length === idx) {
return newArr
}
const item = fn(arr[idx])
newArr.push(item)
idx++
function isInt(v) {
if (typeof v !== 'number') {
return false;
}
return v % 1 === 0;
}
// todo: write tests
@JakeTheCorn
JakeTheCorn / toPrecisionString.ts
Created January 16, 2020 20:28
Utility that returns decimal strings to a certain precision.
function toPrecisionString(num: number, precision: number): string {
if (typeof num !== 'number' || typeof precision !== 'number') {
throw new TypeError('toPrecisionString(n: number, precision: number): string => was called with non-number arguments')
}
if (isInt(num)) {
let extension = ''
if (precision === 0) {
return String(num)
} else {
extension += '.'
@JakeTheCorn
JakeTheCorn / isInt.ts
Created January 16, 2020 19:09
Utility function that returns true if it receives a valid int -- not an int string.
function isInt(subject: unknown): boolean {
if (typeof subject !== 'number') {
return false
}
const str = String(subject)
return /^[0-9]+$/.test(str)
}
// tests
@JakeTheCorn
JakeTheCorn / isFloatString.ts
Created January 16, 2020 19:03
Utility function for asserting if a string is a valid float. This assumes integers are not floats.
function isFloatString(subject: unknown): boolean {
if (typeof subject !== 'string') {
return false
}
return /^[+-]?\d+(\.\d+)$/.test(subject)
}
// tests
const tables = [