MCPcopy Index your code
hub / github.com/datacamp/pythonwhat / TargetVars

Class TargetVars

pythonwhat/parsing.py:24–64  ·  view source on GitHub ↗

Immutable ordered mapping from target variables to their values.

Source from the content-addressed store, hash-verified

22
23
24class TargetVars(Mapping):
25 """Immutable ordered mapping from target variables to their values."""
26
27 EMPTY = EmptyTargetVar()
28
29 def __init__(self, target_vars=tuple(), is_empty=True):
30 if is_empty:
31 target_vars = [(v, self.EMPTY) for v in target_vars]
32
33 self._od = OrderedDict(target_vars)
34
35 # getitem, len, iter wrap OrderedDict behavior
36 def __getitem__(self, k):
37 return self._od.__getitem__(k)
38
39 def __len__(self):
40 return self._od.__len__()
41
42 def __iter__(self):
43 return self._od.__iter__()
44
45 def update(self, *args, **kwargs):
46 cpy = self.copy()
47 cpy._od.update(*args, **kwargs)
48 return cpy
49
50 def copy(self):
51 return self.__class__(self._od)
52
53 def __str__(self):
54 """Format target vars for printing"""
55 if len(self) > 1:
56 return "({})".format(", ".join(self._od.keys()))
57 else:
58 return "".join(self._od.keys())
59
60 def defined_items(self):
61 """Return copy of instance, omitting entries that are EMPTY"""
62 return self.__class__(
63 [(k, v) for k, v in self.items() if v is not self.EMPTY], is_empty=False
64 )
65
66
67class IndexedDict(Mapping):

Callers 5

__init__Method · 0.90
get_target_varsMethod · 0.85
parse_nodeMethod · 0.85
visit_WithMethod · 0.85
parse_handlerMethod · 0.85

Calls 1

EmptyTargetVarClass · 0.85

Tested by

no test coverage detected