Basic utils documentation¶
Core¶
-
basic_utils.core.
slurp
(fname)[source]¶ Reads a file and all its contents, returns a single string
Return type: str
-
basic_utils.core.
clear
()[source]¶ Clears the terminal screen from python, operating system agnostic
Return type: None
-
basic_utils.core.
to_string
(objects, sep=', ')[source]¶ Converts a list of objects into a single string
>>> to_string([1, 2, 3]) '1, 2, 3'
Return type: str
-
basic_utils.core.
getattrs
(obj, keys)[source]¶ Supports getting multiple attributes from a model at once
Return type: Tuple
[Any
, ...]
-
basic_utils.core.
map_getattr
(attr, object_seq)[source]¶ Returns a map to retrieve a single attribute from a sequence of objects
Return type: Tuple
[Any
, ...]
Date Helpers¶
Dict Helpers¶
-
basic_utils.dict_helpers.
get_keys
(d, keys, default=None)[source]¶ Returns multiple values for keys in a dictionary
Empty key values will be None by default
>>> d = {'x': 24, 'y': 25} >>> get_keys(d, ('x', 'y', 'z')) (24, 25, None)
Return type: Tuple
-
basic_utils.dict_helpers.
get_in_dict
(d, keys)[source]¶ Retrieve nested key from dictionary
>>> d = {'a': {'b': {'c': 3}}} >>> get_in_dict(d, ('a', 'b', 'c')) 3
Return type: Any
Seq Helpers¶
-
basic_utils.seq_helpers.
first
(seq)[source]¶ Returns first element in a sequence.
>>> first([1, 2, 3]) 1
Return type: Any
-
basic_utils.seq_helpers.
last
(seq)[source]¶ Returns the last item in a Sequence
>>> last([1, 2, 3]) 3
Return type: Any
-
basic_utils.seq_helpers.
butlast
(seq)[source]¶ Returns all but the last item in sequence
>>> butlast([1, 2, 3]) [1, 2]
Return type: Sequence
[+T_co]
-
basic_utils.seq_helpers.
rest
(seq)[source]¶ Returns remaining elements in a sequence
>>> rest([1, 2, 3]) [2, 3]
Return type: Any
-
basic_utils.seq_helpers.
reverse
(seq)[source]¶ Returns sequence in reverse order
>>> reverse([1, 2, 3]) [3, 2, 1]
Return type: Sequence
[+T_co]
-
basic_utils.seq_helpers.
cons
(item, seq)[source]¶ Adds item to beginning of sequence.
>>> list(cons(1, [2, 3])) [1, 2, 3]
Return type: chain
-
basic_utils.seq_helpers.
flatten
(seq)[source]¶ Returns a flatten version of sequence.
>>> flatten([1, [2, [3, [4, 5], 6], 7]]) [1, 2, 3, 4, 5, 6, 7]
Return type: Iterable
[+T_co]
-
basic_utils.seq_helpers.
partial_flatten
(seq)[source]¶ Returns partially flattened version of sequence.
>>> partial_flatten(((1,), [2, 3], (4, [5, 6]))) (1, 2, 3, 4, [5, 6])
Return type: Iterable
[+T_co]
-
basic_utils.seq_helpers.
sorted_index
(seq, item, key=None)[source]¶ >>> sorted_index([10, 20, 30, 40, 50], 35) 3
Return type: int
-
basic_utils.seq_helpers.
dedupe
(seq, key=None)[source]¶ Removes duplicates from a sequence while maintaining order
>>> dedupe([1, 5, 2, 1, 9, 1, 5, 10]) [1, 5, 2, 9, 10]
Return type: Iterable
[+T_co]
-
basic_utils.seq_helpers.
concat
(seqX, seqY)[source]¶ Joins two sequences together, returning a single combined sequence. Preserves the type of passed arguments.
>>> concat((1, 2, 3), (4, 5, 6)) (1, 2, 3, 4, 5, 6)
Return type: Sequence
[+T_co]
-
basic_utils.seq_helpers.
take
(n, iterable)[source]¶ Return first n items of the iterable as a list.
>>> take(2, range(1, 10)) [1, 2]
Return type: Iterable
[+T_co]
-
basic_utils.seq_helpers.
nth
(iterable, n, default=None)[source]¶ Returns the nth item or a default value.
>>> nth([1, 2, 3], 1) 2
Return type: Any
-
basic_utils.seq_helpers.
all_equal
(iterable)[source]¶ Returns True if all the elements are equal to each other.
>>> all_equal([True, True]) True
Return type: bool
-
basic_utils.seq_helpers.
quantify
(iterable, pred=<class 'bool'>)[source]¶ Returns count of how many times the predicate is true.
>>> quantify([True, False, True]) 2
Return type: int
-
basic_utils.seq_helpers.
head
(seq)¶ Returns first element in a sequence.
>>> first([1, 2, 3]) 1
Return type: Any
-
basic_utils.seq_helpers.
tail
(seq)¶ Returns remaining elements in a sequence
>>> rest([1, 2, 3]) [2, 3]
Return type: Any
-
basic_utils.seq_helpers.
init
(seq)¶ Returns all but the last item in sequence
>>> butlast([1, 2, 3]) [1, 2]
Return type: Sequence
[+T_co]
Primitives¶
-
basic_utils.primitives.
natural_nums
(start=0, end=None)[source]¶ Yields a lazy sequence of natural numbers
>>> from itertools import islice >>> list(islice(natural_nums(5), 3)) [5, 6, 7]
Return type: Iterator
[int
]
-
basic_utils.primitives.
identity
(x)[source]¶ Returns the same values passed as arguments
>>> x = (10, 20) >>> identity(x) (10, 20)
Return type: Any
-
basic_utils.primitives.
comp
(*funcs)[source]¶ Takes a set of functions and returns a fn that is the composition of those functions
Return type: Callable
-
basic_utils.primitives.
complement
(fn)[source]¶ Takes a function fn and returns a function that takes the same arguments as fn with the opposite truth value.
>>> not_five = complement(lambda x: x == 5) >>> not_five(6) True
Return type: Callable
-
basic_utils.primitives.
compose
(*funcs)¶ Takes a set of functions and returns a fn that is the composition of those functions
Return type: Callable