Skip to content

Commit

Permalink
Make data property internal
Browse files Browse the repository at this point in the history
Now that we are not using UserDict we can name the properties
anything we like. Now make it explicitly internal that we
store a dict inside Config.
  • Loading branch information
timj committed Sep 13, 2018
1 parent bb948de commit 867bfad
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
46 changes: 23 additions & 23 deletions python/lsst/daf/butler/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ class Config(collections.abc.MutableMapping):
constructing keys for external use (see `Config.names()`)."""

def __init__(self, other=None):
self.data = {}
self._data = {}

if other is None:
return

if isinstance(other, Config):
self.data = copy.deepcopy(other.data)
self._data = copy.deepcopy(other._data)
elif isinstance(other, collections.Mapping):
self.update(other)
elif isinstance(other, str):
Expand All @@ -182,19 +182,19 @@ def ppprint(self):
s : `str`
A prettyprint formatted string representing the config
"""
return pprint.pformat(self.data, indent=2, width=1)
return pprint.pformat(self._data, indent=2, width=1)

def __repr__(self):
return f"{type(self).__name__}({self.data!r})"
return f"{type(self).__name__}({self._data!r})"

def __str__(self):
return self.ppprint()

def __len__(self):
return len(self.data)
return len(self._data)

def __iter__(self):
return iter(self.data)
return iter(self._data)

def copy(self):
return type(self)(self)
Expand Down Expand Up @@ -240,7 +240,7 @@ def __initFromYaml(self, stream):
content = yaml.load(stream, Loader=Loader)
if content is None:
content = {}
self.data = content
self._data = content
return self

@staticmethod
Expand Down Expand Up @@ -307,7 +307,7 @@ def _getKeyHierarchy(self, name):
as a key in the Config it will be used regardless of the presence
of any nominal delimiter.
"""
if name in self.data:
if name in self._data:
keys = [name, ]
else:
keys = self._splitIntoKeys(name)
Expand All @@ -333,7 +333,7 @@ def _findInHierarchy(self, keys, create=False):
`True` if the full hierarchy exists and the final element
in ``hierarchy`` is the value of relevant value.
"""
d = self.data
d = self._data

def checkNextItem(k, d, create):
"""See if k is in d and if it is return the new child"""
Expand Down Expand Up @@ -379,7 +379,7 @@ def __getitem__(self, name):
# Override the split for the simple case where there is an exact
# match. This allows `Config.items()` to work via a simple
# __iter__ implementation that returns top level keys of
# self.data.
# self._data.
keys = self._getKeyHierarchy(name)

hierarchy, complete = self._findInHierarchy(keys)
Expand All @@ -398,13 +398,13 @@ def __setitem__(self, name, value):
keys = self._getKeyHierarchy(name)
last = keys.pop()
if isinstance(value, Config):
value = copy.deepcopy(value.data)
value = copy.deepcopy(value._data)

hierarchy, complete = self._findInHierarchy(keys, create=True)
if hierarchy:
data = hierarchy[-1]
else:
data = self.data
data = self._data

try:
data[last] = value
Expand All @@ -424,7 +424,7 @@ def __delitem__(self, key):
if hierarchy:
data = hierarchy[-1]
else:
data = self.data
data = self._data
del data[last]
else:
raise KeyError(f"{key} not found in Config")
Expand Down Expand Up @@ -456,7 +456,7 @@ def doUpdate(d, u):
else:
d[k] = v
return d
doUpdate(self.data, other)
doUpdate(self._data, other)

def merge(self, other):
"""Like Config.update, but will add keys & values from other that
Expand All @@ -471,7 +471,7 @@ def merge(self, other):
"""
otherCopy = copy.deepcopy(other)
otherCopy.update(self)
self.data = otherCopy.data
self._data = otherCopy._data

def nameTuples(self, topLevelOnly=False):
"""Get tuples representing the name hierarchies of all keys.
Expand Down Expand Up @@ -506,7 +506,7 @@ def getKeysAsTuples(d, keys, base):
if isinstance(val, (collections.Mapping, collections.Sequence)) and not isinstance(val, str):
getKeysAsTuples(val, keys, levelKey)
keys = []
getKeysAsTuples(self.data, keys, None)
getKeysAsTuples(self._data, keys, None)
return keys

def names(self, topLevelOnly=False, delimiter=None):
Expand Down Expand Up @@ -608,13 +608,13 @@ def asArray(self, name):

def __eq__(self, other):
if isinstance(other, Config):
other = other.data
return self.data == other
other = other._data
return self._data == other

def __ne__(self, other):
if isinstance(other, Config):
other = other.data
return self.data != other
other = other._data
return self._data != other

#######
# i/o #
Expand All @@ -631,7 +631,7 @@ def dump(self, output):
# specific order for readability.
# After the expected/ordered keys are weritten to the stream the
# remainder of the keys are written to the stream.
data = copy.copy(self.data)
data = copy.copy(self._data)
keys = []
for key in keys:
try:
Expand Down Expand Up @@ -797,7 +797,7 @@ def __init__(self, other=None, validate=True, mergeDefaults=True, searchPaths=No
if doubled in externalConfig:
externalConfig = externalConfig[doubled]
elif self.component in externalConfig:
externalConfig.data = externalConfig.data[self.component]
externalConfig._data = externalConfig._data[self.component]

# Default files read to create this configuration
self.filesRead = []
Expand Down Expand Up @@ -948,6 +948,6 @@ def validate(self):
Ignored if ``requiredKeys`` is empty."""
# Validation
missing = [k for k in self.requiredKeys if k not in self.data]
missing = [k for k in self.requiredKeys if k not in self._data]
if missing:
raise KeyError(f"Mandatory keys ({missing}) missing from supplied configuration for {type(self)}")
2 changes: 1 addition & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def testHierarchy(self):
self.assertIsInstance(a, list)

# Check we get the same top level keys
self.assertEqual(set(c.names(topLevelOnly=True)), set(c.data.keys()))
self.assertEqual(set(c.names(topLevelOnly=True)), set(c._data.keys()))

# Check that we can iterate through items
for k, v in c.items():
Expand Down

0 comments on commit 867bfad

Please sign in to comment.