-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
80 lines (61 loc) · 2 KB
/
Copy pathutils.py
File metadata and controls
80 lines (61 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import functools as ft
import itertools as it
import operator
import typing
import types
from typing import (
Union, List, Callable, Iterable, Iterator,)
import funct
ixget = operator.itemgetter
partial = ft.partial
nt = typing.NamedTuple
ns = types.SimpleNamespace
const = types.SimpleNamespace
# Take a look at an element from iterable
peek = funct.compose(next, iter)
class ModLoad:
''' Helper to load from another Python project.
Really shouldn't be used, but sometimes useful for Jupyter prototyping.
ex: ModLoad('/home/toni/PythonProj')
'''
def __init__(self, where):
from pathlib import Path
self.where = Path(
where.where if isinstance(where, ModLoad)
else where)
def __call__(self, modname):
'''Load module `modname` from `self.where/modname.py`'''
import importlib.util
modname = modname.strip('.py')
spec = importlib.util.spec_from_file_location(
modname, self.where / f'{modname}.py')
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def make(pathobj: const):
for name, val in pathobj.__dict__.items():
if name != 'base':
setattr(pathobj, name, ModLoad(val))
return pathobj
def flatmap(func: Callable, iterable: Iterable):
return it.chain.from_iterable(map(func, *iterable))
def zip2lst(list_of_zips: Union[List, Iterator]):
if not isinstance(list_of_zips, list):
list_of_zips = list(list_of_zips)
def accumulate(result, ziprow):
for lst, el in zip(result, ziprow):
lst.append(el)
return result
return ft.reduce(accumulate, list_of_zips, tuple([] for _ in list_of_zips))
def fuzzymatch(search_term_cased):
'''Ignore-case simple fuzzy matching.'''
search_term = search_term_cased.lower()
def fuzzy_inner(test_against):
test_against = test_against.lower()
iterm = 0
for letter in test_against:
iterm += (letter == search_term[iterm])
if iterm == len(search_term):
return True
return False
return fuzzy_inner