ConfigSummary   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 21

4 Methods

Rating   Name   Duplication   Size   Complexity  
B ensure_coherence() 0 11 7
A __init__() 0 9 1
B update_add() 0 12 7
B update_from() 0 12 6
1
#!/usr/bin/env python
2
# coding=utf-8
3
from __future__ import division, print_function, unicode_literals
4
5
from sacred.utils import iter_prefixes, join_paths
6
7
8
class ConfigSummary(dict):
9
    def __init__(self, added=(), modified=(), typechanged=(),
10
                 ignored_fallbacks=(), docs=()):
11
        super(ConfigSummary, self).__init__()
12
        self.added = set(added)
13
        self.modified = set(modified)  # TODO: test for this member
14
        self.typechanged = dict(typechanged)
15
        self.ignored_fallbacks = set(ignored_fallbacks)  # TODO: test
16
        self.docs = dict(docs)
17
        self.ensure_coherence()
18
19
    def update_from(self, config_mod, path=''):
20
        added = config_mod.added
21
        updated = config_mod.modified
22
        typechanged = config_mod.typechanged
23
        self.added &= {join_paths(path, a) for a in added}
24
        self.modified |= {join_paths(path, u) for u in updated}
25
        self.typechanged.update({join_paths(path, k): v
26
                                 for k, v in typechanged.items()})
27
        self.ensure_coherence()
28
        for k, v in config_mod.docs.items():
29
            if not self.docs.get(k, ''):
30
                self.docs[k] = v
31
32
    def update_add(self, config_mod, path=''):
33
        added = config_mod.added
34
        updated = config_mod.modified
35
        typechanged = config_mod.typechanged
36
        self.added |= {join_paths(path, a) for a in added}
37
        self.modified |= {join_paths(path, u) for u in updated}
38
        self.typechanged.update({join_paths(path, k): v
39
                                 for k, v in typechanged.items()})
40
        self.docs.update({join_paths(path, k): v
41
                          for k, v in config_mod.docs.items()
42
                          if path == '' or k != 'seed'})
43
        self.ensure_coherence()
44
45
    def ensure_coherence(self):
46
        # make sure parent paths show up as updated appropriately
47
        self.modified |= {p for a in self.added for p in iter_prefixes(a)}
48
        self.modified |= {p for u in self.modified for p in iter_prefixes(u)}
49
        self.modified |= {p for t in self.typechanged
50
                          for p in iter_prefixes(t)}
51
52
        # make sure there is no overlap
53
        self.added -= set(self.typechanged.keys())
54
        self.modified -= set(self.typechanged.keys())
55
        self.modified -= self.added
56