|
23 | 23 | import six |
24 | 24 |
|
25 | 25 |
|
26 | | -def chunker(iterable, n, fillvalue=None): |
27 | | - """Like a grouper but last tuple is shorter. |
28 | | -
|
29 | | - >>> list(chunker([1, 2, 3, 4, 5], 3)) |
30 | | - [[1, 2, 3], [4, 5]] |
31 | | - """ |
32 | | - if n < 1: |
33 | | - raise ValueError("can't chunk by n=%d" % n) |
34 | | - args = [iter(iterable)] * n |
35 | | - return ( |
36 | | - [e for e in t if e is not None] |
37 | | - for t in izip_longest(*args, fillvalue=fillvalue) |
38 | | - ) |
39 | | - |
40 | 26 | # TODO: maybe automagically get these from http://prefix.cc ? |
41 | 27 | # TODO: make this configurable |
42 | 28 | _nsm = NamespaceManager(rdflib.Graph()) |
@@ -216,44 +202,6 @@ def inner(*args, **kwds): |
216 | 202 | return outer |
217 | 203 |
|
218 | 204 |
|
219 | | -def get_path(nested, key_path, default=None): |
220 | | - """Walks given nested by key_path returning default on any LookupError. |
221 | | -
|
222 | | - :param nested: a nested dictionary or list like structure |
223 | | - :param key_path: a list resembling a nested path of keys |
224 | | - :param default: returned if any key in the path isn't found |
225 | | - :return: value of nested[k0][k1]...[kn] or default on error |
226 | | -
|
227 | | - >>> get_path({'foo':[{'bar':3}]}, ['foo'], 'not found') |
228 | | - [{'bar': 3}] |
229 | | - >>> get_path({'foo':[{'bar':3}]}, ['foo', 'bar'], 'not found') |
230 | | - 'not found' |
231 | | - >>> get_path({'foo':[{'bar':3}]}, ['foo', 0, 'bar'], 'not found') |
232 | | - 3 |
233 | | - >>> get_path({'foo':[{'bar':3}]}, ['foo', 0], 'not found') |
234 | | - {'bar': 3} |
235 | | - """ |
236 | | - while len(key_path) > 0: |
237 | | - key, rest = key_path[0], key_path[1:] |
238 | | - try: |
239 | | - nested = nested[key] |
240 | | - except (LookupError, TypeError): |
241 | | - return default |
242 | | - key_path = rest |
243 | | - return nested |
244 | | - |
245 | | - |
246 | | -def run_once(func): |
247 | | - """Decorator that causes a function to be executed only once.""" |
248 | | - @wraps(func) |
249 | | - def wrapper(*args, **kwds): |
250 | | - if not wrapper.ran: |
251 | | - wrapper.ran = True |
252 | | - return func(*args, **kwds) |
253 | | - wrapper.ran = False |
254 | | - return wrapper |
255 | | - |
256 | | - |
257 | 205 | def sample_from_list(l, probs, max_n=None): |
258 | 206 | """Sample list according to probs. |
259 | 207 |
|
@@ -340,18 +288,3 @@ def dict_to_rdflib(d): |
340 | 288 | res_bindings_rdflib.append(tmp) |
341 | 289 |
|
342 | 290 | return res_bindings_rdflib |
343 | | - |
344 | | - |
345 | | - |
346 | | -def time(func, *args, **kwds): |
347 | | - """Evaluates function with given args and returns |
348 | | -
|
349 | | - :param func: function to be evaluated |
350 | | - :param args: args for func |
351 | | - :param kwds: kwds for func |
352 | | - :return: a tuple: (timediff, func(*args, **kwds) |
353 | | - """ |
354 | | - start = timer() |
355 | | - res = func(*args, **kwds) |
356 | | - stop = timer() |
357 | | - return stop - start, res |
0 commit comments