-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathutils.py
More file actions
158 lines (127 loc) · 3.71 KB
/
utils.py
File metadata and controls
158 lines (127 loc) · 3.71 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
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
158
from __future__ import absolute_import, division, print_function
import tempfile
import os
import inspect
import datetime
from itertools import islice
from contextlib import contextmanager
from collections import Iterator
from multiprocessing.pool import ThreadPool
from into.utils import tmpfile, filetext, filetexts, raises, keywords, ignoring
import psutil
import numpy as np
# Imports that replace older utils.
from .compatibility import map, zip
from .dispatch import dispatch
thread_pool = ThreadPool(psutil.NUM_CPUS)
def nth_list(n, seq):
"""
>>> tuple(nth_list([0, 1, 4], 'Hello'))
('H', 'e', 'o')
>>> tuple(nth_list([4, 1, 0], 'Hello'))
('o', 'e', 'H')
>>> tuple(nth_list([0, 0, 0], 'Hello'))
('H', 'H', 'H')
"""
seq = iter(seq)
sn = sorted(n)
result = []
old = 0
item = next(seq)
for index in sorted(n):
for i in range(index - old):
item = next(seq)
result.append(item)
old = index
order = [x[1] for x in sorted(zip(n, range(len(n))))]
return (result[i] for i in order)
def get(ind, coll, lazy=False):
"""
>>> get(0, 'Hello')
'H'
>>> get([1, 0], 'Hello')
('e', 'H')
>>> get(slice(1, 4), 'Hello')
('e', 'l', 'l')
>>> get(slice(1, 4), 'Hello', lazy=True) # doctest: +SKIP
<itertools.islice object at 0x25ac470>
"""
if isinstance(ind, list):
result = nth_list(ind, coll)
elif isinstance(ind, slice):
result = islice(coll, ind.start, ind.stop, ind.step)
else:
if isinstance(coll, Iterator):
result = nth(ind, coll)
else:
result = coll[ind]
if lazy==False and isinstance(result, Iterator):
result = tuple(result)
return result
def ndget(ind, data):
"""
Get from N-Dimensional getable
Can index with elements, lists, or slices. Mimic's numpy fancy indexing on
generic indexibles.
>>> data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
>>> ndget(0, data)
[[1, 2], [3, 4]]
>>> ndget((0, 1), data)
[3, 4]
>>> ndget((0, 0, 0), data)
1
>>> ndget((slice(0, 2), [0, 1], 0), data)
((1, 3), (5, 7))
"""
if isinstance(ind, tuple) and len(ind) == 1:
ind = ind[0]
if not isinstance(ind, tuple):
return get(ind, data)
result = get(ind[0], data)
if isinstance(ind[0], (list, slice)):
return type(result)(ndget(ind[1:], row) for row in result)
else:
return ndget(ind[1:], result)
def normalize_to_date(dt):
if isinstance(dt, datetime.datetime) and not dt.time():
return dt.date()
else:
return dt
def assert_allclose(lhs, rhs):
for tb in map(zip, lhs, rhs):
for left, right in tb:
if isinstance(left, (np.floating, float)):
# account for nans
assert np.all(np.isclose(left, right, equal_nan=True))
continue
if isinstance(left, datetime.datetime):
left = normalize_to_date(left)
if isinstance(right, datetime.datetime):
right = normalize_to_date(right)
assert left == right
def example(filename, datapath=os.path.join('examples', 'data')):
import blaze
return os.path.join(os.path.dirname(blaze.__file__), datapath, filename)
def available_memory():
return psutil.virtual_memory().available
def listpack(x):
"""
>>> listpack(1)
[1]
>>> listpack((1, 2))
[1, 2]
>>> listpack([1, 2])
[1, 2]
"""
if isinstance(x, tuple):
return list(x)
elif isinstance(x, list):
return x
else:
return [x]
@dispatch(datetime.datetime)
def json_dumps(dt):
s = dt.isoformat()
if not dt.tzname():
s = s + 'Z'
return s