This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
def nullify(v: dict) -> dict or None: | |
if not isinstance(v, dict): | |
return v | |
if not bool(v): | |
return None | |
d = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
up() { | |
local n=$1 | |
for (( i=1; i<=$n; i++ )) | |
do | |
cd .. | |
done | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isInt(v) { | |
if (typeof v !== 'number') { | |
return false; | |
} | |
return v % 1 === 0; | |
} | |
// todo: write tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 += '.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isInt(subject: unknown): boolean { | |
if (typeof subject !== 'number') { | |
return false | |
} | |
const str = String(subject) | |
return /^[0-9]+$/.test(str) | |
} | |
// tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isFloatString(subject: unknown): boolean { | |
if (typeof subject !== 'string') { | |
return false | |
} | |
return /^[+-]?\d+(\.\d+)$/.test(subject) | |
} | |
// tests | |
const tables = [ |