elements 3.1.2__tar.gz → 3.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {elements-3.1.2/elements.egg-info → elements-3.3.0}/PKG-INFO +5 -5
- {elements-3.1.2 → elements-3.3.0}/README.md +4 -4
- {elements-3.1.2 → elements-3.3.0}/elements/__init__.py +2 -1
- elements-3.3.0/elements/tree.py +41 -0
- elements-3.3.0/elements/utils.py +9 -0
- {elements-3.1.2 → elements-3.3.0/elements.egg-info}/PKG-INFO +5 -5
- {elements-3.1.2 → elements-3.3.0}/elements.egg-info/SOURCES.txt +3 -1
- elements-3.3.0/tests/test_tree.py +70 -0
- elements-3.1.2/elements/tree.py +0 -31
- {elements-3.1.2 → elements-3.3.0}/LICENSE +0 -0
- {elements-3.1.2 → elements-3.3.0}/MANIFEST.in +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements/agg.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements/checkpoint.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements/config.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements/counter.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements/flags.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements/fps.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements/logger.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements/path.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements/plotting.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements/printing.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements/rwlock.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements/timer.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements/usage.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements/uuid.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements/when.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements.egg-info/dependency_links.txt +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements.egg-info/requires.txt +0 -0
- {elements-3.1.2 → elements-3.3.0}/elements.egg-info/top_level.txt +0 -0
- {elements-3.1.2 → elements-3.3.0}/requirements-optional.txt +0 -0
- {elements-3.1.2 → elements-3.3.0}/requirements.txt +0 -0
- {elements-3.1.2 → elements-3.3.0}/setup.cfg +0 -0
- {elements-3.1.2 → elements-3.3.0}/setup.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/tests/test_basics.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/tests/test_flags.py +0 -0
- {elements-3.1.2 → elements-3.3.0}/tests/test_path.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: elements
|
|
3
|
-
Version: 3.
|
|
3
|
+
Version: 3.3.0
|
|
4
4
|
Summary: Building blocks for productive research.
|
|
5
5
|
Home-page: http://github.com/danijar/elements
|
|
6
6
|
Classifier: Intended Audience :: Science/Research
|
|
@@ -228,14 +228,14 @@ print(cp.model)
|
|
|
228
228
|
### `elements.Timer`
|
|
229
229
|
|
|
230
230
|
Collect timing statistics about the run time of different parts of a program.
|
|
231
|
-
Measures code inside
|
|
232
|
-
count, execution time, fraction of overall program time, and more.
|
|
233
|
-
resulting statisticse can be added to the logger.
|
|
231
|
+
Measures code inside sections and can wrap methods into sections. Returns
|
|
232
|
+
execution count, execution time, fraction of overall program time, and more.
|
|
233
|
+
The resulting statisticse can be added to the logger.
|
|
234
234
|
|
|
235
235
|
```python
|
|
236
236
|
timer = Timer()
|
|
237
237
|
|
|
238
|
-
timer.
|
|
238
|
+
timer.section('foo'):
|
|
239
239
|
time.sleep(10)
|
|
240
240
|
|
|
241
241
|
timer.wrap('name', obj, ['method1', 'method2'])
|
|
@@ -217,14 +217,14 @@ print(cp.model)
|
|
|
217
217
|
### `elements.Timer`
|
|
218
218
|
|
|
219
219
|
Collect timing statistics about the run time of different parts of a program.
|
|
220
|
-
Measures code inside
|
|
221
|
-
count, execution time, fraction of overall program time, and more.
|
|
222
|
-
resulting statisticse can be added to the logger.
|
|
220
|
+
Measures code inside sections and can wrap methods into sections. Returns
|
|
221
|
+
execution count, execution time, fraction of overall program time, and more.
|
|
222
|
+
The resulting statisticse can be added to the logger.
|
|
223
223
|
|
|
224
224
|
```python
|
|
225
225
|
timer = Timer()
|
|
226
226
|
|
|
227
|
-
timer.
|
|
227
|
+
timer.section('foo'):
|
|
228
228
|
time.sleep(10)
|
|
229
229
|
|
|
230
230
|
timer.wrap('name', obj, ['method1', 'method2'])
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
__version__ = '3.
|
|
1
|
+
__version__ = '3.3.0'
|
|
2
2
|
|
|
3
3
|
from .agg import Agg
|
|
4
4
|
from .checkpoint import Checkpoint
|
|
@@ -14,6 +14,7 @@ from .rwlock import RWLock
|
|
|
14
14
|
from .timer import Timer
|
|
15
15
|
from .usage import Usage
|
|
16
16
|
from .uuid import UUID
|
|
17
|
+
from .utils import timestamp
|
|
17
18
|
|
|
18
19
|
from . import logger
|
|
19
20
|
from . import plotting
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from . import printing
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def map(fn, *trees, isleaf=None):
|
|
5
|
+
assert trees, 'Provide one or more nested Python structures'
|
|
6
|
+
kw = dict(isleaf=isleaf)
|
|
7
|
+
first = trees[0]
|
|
8
|
+
try:
|
|
9
|
+
assert all(isinstance(x, type(first)) for x in trees)
|
|
10
|
+
if isleaf and isleaf(trees[0]):
|
|
11
|
+
return fn(*trees)
|
|
12
|
+
if isinstance(first, list):
|
|
13
|
+
assert all(len(x) == len(first) for x in trees)
|
|
14
|
+
return [map(
|
|
15
|
+
fn, *[t[i] for t in trees], **kw) for i in range(len(first))]
|
|
16
|
+
if isinstance(first, tuple):
|
|
17
|
+
assert all(len(x) == len(first) for x in trees)
|
|
18
|
+
return tuple([map(
|
|
19
|
+
fn, *[t[i] for t in trees], **kw) for i in range(len(first))])
|
|
20
|
+
if isinstance(first, dict):
|
|
21
|
+
assert all(set(x.keys()) == set(first.keys()) for x in trees)
|
|
22
|
+
return {k: map(fn, *[t[k] for t in trees], **kw) for k in first}
|
|
23
|
+
if hasattr(first, 'keys') and hasattr(first, 'get'):
|
|
24
|
+
assert all(set(x.keys()) == set(first.keys()) for x in trees)
|
|
25
|
+
return type(first)(
|
|
26
|
+
{k: map(fn, *[t[k] for t in trees], **kw) for k in first})
|
|
27
|
+
except AssertionError:
|
|
28
|
+
raise TypeError(printing.format_(trees))
|
|
29
|
+
return fn(*trees)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def flatten(tree, isleaf=None):
|
|
33
|
+
leaves = []
|
|
34
|
+
map(lambda x: leaves.append(x), tree, isleaf=isleaf)
|
|
35
|
+
structure = map(lambda x: None, tree, isleaf=isleaf)
|
|
36
|
+
return tuple(leaves), structure
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def unflatten(leaves, structure):
|
|
40
|
+
leaves = iter(tuple(leaves))
|
|
41
|
+
return map(lambda x: next(leaves), structure)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: elements
|
|
3
|
-
Version: 3.
|
|
3
|
+
Version: 3.3.0
|
|
4
4
|
Summary: Building blocks for productive research.
|
|
5
5
|
Home-page: http://github.com/danijar/elements
|
|
6
6
|
Classifier: Intended Audience :: Science/Research
|
|
@@ -228,14 +228,14 @@ print(cp.model)
|
|
|
228
228
|
### `elements.Timer`
|
|
229
229
|
|
|
230
230
|
Collect timing statistics about the run time of different parts of a program.
|
|
231
|
-
Measures code inside
|
|
232
|
-
count, execution time, fraction of overall program time, and more.
|
|
233
|
-
resulting statisticse can be added to the logger.
|
|
231
|
+
Measures code inside sections and can wrap methods into sections. Returns
|
|
232
|
+
execution count, execution time, fraction of overall program time, and more.
|
|
233
|
+
The resulting statisticse can be added to the logger.
|
|
234
234
|
|
|
235
235
|
```python
|
|
236
236
|
timer = Timer()
|
|
237
237
|
|
|
238
|
-
timer.
|
|
238
|
+
timer.section('foo'):
|
|
239
239
|
time.sleep(10)
|
|
240
240
|
|
|
241
241
|
timer.wrap('name', obj, ['method1', 'method2'])
|
|
@@ -19,6 +19,7 @@ elements/rwlock.py
|
|
|
19
19
|
elements/timer.py
|
|
20
20
|
elements/tree.py
|
|
21
21
|
elements/usage.py
|
|
22
|
+
elements/utils.py
|
|
22
23
|
elements/uuid.py
|
|
23
24
|
elements/when.py
|
|
24
25
|
elements.egg-info/PKG-INFO
|
|
@@ -28,4 +29,5 @@ elements.egg-info/requires.txt
|
|
|
28
29
|
elements.egg-info/top_level.txt
|
|
29
30
|
tests/test_basics.py
|
|
30
31
|
tests/test_flags.py
|
|
31
|
-
tests/test_path.py
|
|
32
|
+
tests/test_path.py
|
|
33
|
+
tests/test_tree.py
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import elements
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class TestTree:
|
|
6
|
+
|
|
7
|
+
def test_map_identity(self):
|
|
8
|
+
tree = {'a': 12, 'b': ['c', (1, 2, 3)]}
|
|
9
|
+
copy = elements.tree.map(lambda x: x, tree)
|
|
10
|
+
assert copy == tree
|
|
11
|
+
assert copy is not tree
|
|
12
|
+
|
|
13
|
+
def test_map_double(self):
|
|
14
|
+
tree = {'a': 12, 'b': ['c', (1, 2, 3)]}
|
|
15
|
+
ref = {'a': 24, 'b': ['cc', (2, 4, 6)]}
|
|
16
|
+
res = elements.tree.map(lambda x: x * 2, tree)
|
|
17
|
+
assert ref == res
|
|
18
|
+
|
|
19
|
+
def test_map_multiple(self):
|
|
20
|
+
tree1 = {'a': 1, 'b': ['foo', (1, 2, 3)]}
|
|
21
|
+
tree2 = {'a': 2, 'b': ['bar', (10, 20, 30)]}
|
|
22
|
+
ref = {'a': 3, 'b': ['foobar', (11, 22, 33)]}
|
|
23
|
+
res = elements.tree.map(lambda x, y: x + y, tree1, tree2)
|
|
24
|
+
assert ref == res
|
|
25
|
+
with pytest.raises(TypeError):
|
|
26
|
+
tree1 = {'a': 1, 'b': ['foo', (1, 2, 3)]}
|
|
27
|
+
tree2 = {'b': 2}
|
|
28
|
+
res = elements.tree.map(lambda x, y: x + y, tree1, tree2)
|
|
29
|
+
|
|
30
|
+
def test_map_isleaf(self):
|
|
31
|
+
tree = {'a': 1, 'b': ['foo', (1, 2, 3)]}
|
|
32
|
+
isleaf = lambda x: isinstance(x, list)
|
|
33
|
+
leaves = []
|
|
34
|
+
elements.tree.map(lambda x: leaves.append(x), tree, isleaf=isleaf)
|
|
35
|
+
assert leaves == [1, ['foo', (1, 2, 3)]]
|
|
36
|
+
|
|
37
|
+
def test_map_degenerate(self):
|
|
38
|
+
assert elements.tree.map(lambda x: 2 * x, ()) == ()
|
|
39
|
+
assert elements.tree.map(lambda x: 2 * x, 12) == 24
|
|
40
|
+
assert elements.tree.map(lambda x: 2 * x, [[{}, ()]]) == [[{}, ()]]
|
|
41
|
+
|
|
42
|
+
def test_flatten_basic(self):
|
|
43
|
+
tree = {'a': 12, 'b': ['c', (1, 2, 3)]}
|
|
44
|
+
leaves, structure = elements.tree.flatten(tree)
|
|
45
|
+
assert leaves == (12, 'c', 1, 2, 3)
|
|
46
|
+
assert structure == {'a': None, 'b': [None, (None, None, None)]}
|
|
47
|
+
|
|
48
|
+
def test_flatten_isleaf(self):
|
|
49
|
+
tree = {'a': 12, 'b': ['c', (1, 2, 3)]}
|
|
50
|
+
isleaf = lambda x: isinstance(x, list)
|
|
51
|
+
leaves, structure = elements.tree.flatten(tree, isleaf=isleaf)
|
|
52
|
+
assert leaves == (12, ['c', (1, 2, 3)])
|
|
53
|
+
assert structure == {'a': None, 'b': None}
|
|
54
|
+
|
|
55
|
+
def test_flatten_degenerate(self):
|
|
56
|
+
assert elements.tree.flatten(()) == ((), ())
|
|
57
|
+
assert elements.tree.flatten(12) == ((12,), None)
|
|
58
|
+
assert elements.tree.flatten([[{}, ()]]) == ((), [[{}, ()]])
|
|
59
|
+
|
|
60
|
+
def test_unflatten(self):
|
|
61
|
+
trees = [
|
|
62
|
+
{'a': 12, 'b': ['c', (1, 2, 3)]},
|
|
63
|
+
(),
|
|
64
|
+
12,
|
|
65
|
+
[[{}, ()]],
|
|
66
|
+
]
|
|
67
|
+
for tree in trees:
|
|
68
|
+
leaves, structure = elements.tree.flatten(tree)
|
|
69
|
+
copy = elements.tree.unflatten(leaves, structure)
|
|
70
|
+
assert copy == tree
|
elements-3.1.2/elements/tree.py
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
from . import printing
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
def map_(fn, *trees, isleaf=None):
|
|
5
|
-
assert trees, 'Provide one or more nested Python structures'
|
|
6
|
-
kw = dict(isleaf=isleaf)
|
|
7
|
-
first = trees[0]
|
|
8
|
-
assert all(isinstance(x, type(first)) for x in trees)
|
|
9
|
-
if isleaf and isleaf(trees):
|
|
10
|
-
return fn(*trees)
|
|
11
|
-
if isinstance(first, list):
|
|
12
|
-
assert all(len(x) == len(first) for x in trees), printing.format_(trees)
|
|
13
|
-
return [map_(
|
|
14
|
-
fn, *[t[i] for t in trees], **kw) for i in range(len(first))]
|
|
15
|
-
if isinstance(first, tuple):
|
|
16
|
-
assert all(len(x) == len(first) for x in trees), printing.format_(trees)
|
|
17
|
-
return tuple([map_(
|
|
18
|
-
fn, *[t[i] for t in trees], **kw) for i in range(len(first))])
|
|
19
|
-
if isinstance(first, dict):
|
|
20
|
-
assert all(set(x.keys()) == set(first.keys()) for x in trees), (
|
|
21
|
-
printing.format_(trees))
|
|
22
|
-
return {k: map_(fn, *[t[k] for t in trees], **kw) for k in first}
|
|
23
|
-
if hasattr(first, 'keys') and hasattr(first, 'get'):
|
|
24
|
-
assert all(set(x.keys()) == set(first.keys()) for x in trees), (
|
|
25
|
-
printing.format_(trees))
|
|
26
|
-
return type(first)(
|
|
27
|
-
{k: map_(fn, *[t[k] for t in trees], **kw) for k in first})
|
|
28
|
-
return fn(*trees)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
map = map_
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|