-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
executable file
·157 lines (131 loc) · 4.61 KB
/
utils.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__all__ = [
'reversed_dict',
'Singleton',
'ConstDotDictify',
'align_size',
'roundup',
'get_terminal_size',
'xstr',
'make_list_of_dict_from_arrays',
'has_all_keys',
'has_any_keys',
'joiniterable',
'transposelist',
'RingLooper',
'urlscheme',
'cut_integer',
'count_set_bits',
'isv2',
'isv3',
'inversed_textwrap',
'streamistty',
'fuzzy_match',
'substr_match',
'has_fuzzy_matched',
'has_substr_matched',
'namedtupledictify',
]
import math, os, sys, itertools, urllib, difflib
# Swap keys for values
reversed_dict = lambda d: dict(zip(d.values(), d.keys()))
class Singleton(type):
__instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls.__instances:
cls.__instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls.__instances[cls]
class ConstDotDictify():
def __init__(self, *args, **kwargs):
self.__dict__.update(*args, **kwargs)
def __getitem__(self, key):
return self.__dict__[key]
__getattr__ = __getitem__
def __readonly__(self, *args, **kwargs):
raise AttributeError("Cannot reassign members.")
__setattr__ = __readonly__
__setitem__ = __readonly__
del __readonly__
# Only to be used to align on a power of 2 boundary.
align_size = lambda size, boundary: size + (boundary - 1) & ~(boundary - 1) if math.log(boundary, 2).is_integer() else None
roundup = lambda n, b: int(math.ceil(n / b)) * b
def get_terminal_size(file = sys.stdout):
try:
_ = os.get_terminal_size(file.fileno())
columns, lines = _.columns, _.lines
except OSError:
columns, lines = 0, 0
return columns, lines
# Convert None, 0, False, or any falsy value to empty string.
xstr = lambda s: str(s or '')
def make_list_of_dict_from_arrays(keys, *values):
l = []
if keys:
for i in [[[k, v] for k, v in zip(keys, vals)] for vals in values ]:
l.append(dict(i))
return l
has_all_keys = lambda dict, keys: all(_ in dict for _ in keys)
has_any_keys = lambda dict, keys: any(_ in dict for _ in keys)
joiniterable = lambda sep, seq: sep.join(map(str, seq))
transposelist = lambda l: list(map(list, zip(*l)))
class RingLooper():
def __init__(self, *array):
self.__array = array
def __iter__(self):
self.__cycler = itertools.cycle(range(len(self.__array)))
return self
def __next__(self):
if self.__array:
return self.__array[next(self.__cycler)]
else:
raise StopIteration
urlscheme = lambda url: urllib.parse.urlparse(url).scheme
cut_integer = lambda num, bits: num & ((1<< bits) - 1)
def count_set_bits(n):
count = 0
while (n):
n = n & (n - 1)
count += 1
return count
isv2 = lambda : True if sys.version_info[0] == 2 else False
isv3 = lambda : True if sys.version_info[0] == 3 else False
"""
>>> inversed_textwrap('1234567890', 3)
['1', '234', '567', '890']
"""
inversed_textwrap = lambda text, wrapwidth: list(map(lambda x:x[::-1], textwrap.wrap(text[::-1], wrapwidth)[::-1]))
# Check filestream is attached to terminal.
streamistty = lambda file: True if file.isatty() else False
_DEFAULT_THRESHOLD = 0.7
def fuzzy_match(needle, haystack, ignore_case = False, threshold = _DEFAULT_THRESHOLD):
if ignore_case:
matching = lambda entry: difflib.SequenceMatcher(a = needle.lower(), b = entry.lower()).ratio() >= threshold
else:
matching = lambda entry: difflib.SequenceMatcher(a = needle, b = entry).ratio() >= threshold
return filter(match, haystack)
def substr_match(needle, haystack, ignore_case = False):
if ignore_case:
matching = lambda entry: needle.lower() in entry.lower()
else:
matching = lambda entry: needle in entry
return filter(match, haystack)
def has_fuzzy_matched(needle, haystack, ignore_case = False, threshold = _DEFAULT_THRESHOLD):
if ignore_case:
matching = lambda entry: difflib.SequenceMatcher(a = needle.lower(), b = entry.lower()).ratio() >= threshold
else:
matching = lambda entry: difflib.SequenceMatcher(a = needle, b = entry).ratio() >= threshold
for entry in haystack:
if matching(entry):
return True
return False
def has_substr_matched(needle, haystack, ignore_case = False):
if ignore_case:
matching = lambda entry: needle.lower() in entry.lower()
else:
matching = lambda entry: needle in entry
for entry in haystack:
if matching(entry):
return True
return False
namedtupledictify = lambda nt: dict(nt._asdict())