-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
110 lines (79 loc) · 2.31 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
import operator
import random
import itertools
import contextlib
def _getattr(name, default=None):
return lambda obj: getattr(obj, name, default)
class xgetitem:
class _(type):
def __getitem__(cls, item):
return cls(idx=item)
def __call__(self, idx=None, obj=None):
assert (idx is None) ^ bool(obj is None)
if idx: return lambda object: operator.getitem(object, idx)
elif obj: return lambda index: operator.getitem(obj, index)
__metaclass__= _
xget = xgetitem
def xrun(func_name, *args):
def wrap(obj):
func = getattr(obj, func_name)
return func(*args)
return wrap
def xwith(path, func, flag='', *args, **kwargs):
with open(path, flag) as f:
result = func(f, *args, **kwargs)
return result
def xgetattr(attr):
def wrap(obj):
return getattr(obj, attr)
return wrap
def elvis(obj, default):
return obj if obj else default
@contextlib.contextmanager
def nested_break_contextmanager():
class NestedBreakException(Exception):
pass
try:
yield NestedBreakException
except NestedBreakException:
pass
def take_some(array, indexes):
return [array[index] for index in indexes if index < len(array)]
def gen_some(array, indexes):
for index in indexes:
if index < len(array):
yield array[index]
def get_reflection(dim, coord):
x, y = dim
cx, cy = coord
return x-cx-1, y-cy-1
choice_from_set = lambda l: random.sample(l, 1)[0]
class Count(object):
def __init__(self, start=0, step=None):
self._step = 1 if step is None else step
self._start = start
self._current = None
@property
def current(self):
assert self._current is not None
return self._current
def __iter__(self):
return self
def next(self):
if self._current is None:
self._current = self._start
return self.current
else:
self._current += self._step
return self.current
def choice_and_pop(array):
if not array:
return None
_choice = None
if isinstance(array, set):
_choice = choice_from_set
elif isinstance(array, list):
_choice = random.choice
item = _choice(array)
array.remove(item)
return item