def __str__(self): """ If the ``data`` attribute is a dictionary, then the paths are given along with their corresponding (non-dict) value, in the form "path=key". If ``data`` isn't a dict, then this just returns the string representation of the ``data``. """ data_list = None if isinstance(self.data, dict): data_list = [self.data] if isinstance(self.data, (list, tuple)) and \ all([isinstance(x, dict) for x in self.data]): data_list = self.data if data_list is None: result = str(self.data) elif self.key is None: result = "\n".join(["{}={}".format(k, v) for d in data_list for k, v in flatten_dict(d, self.separator).items() ] ) else: result = "\n".join(["{}{}{}={}".format(self.key, self.separator, k, v) for d in data_list for k, v in flatten_dict(d, self.separator).items() ] ) return result
def __str__(self): """ If the ``data`` attribute is a dictionary, then the paths are given along with their corresponding (non-dict) value, in the form "path=key". If ``data`` isn't a dict, then this just returns the string representation of the ``data``. """ data_list = None if isinstance(self.data, dict): data_list = [self.data] if isinstance(self.data, (list, tuple)) and \ all([isinstance(x, dict) for x in self.data]): data_list = self.data if data_list is None: result = str(self.data) elif self.key is None: result = "\n".join([ "{}={}".format(k, v) for d in data_list for k, v in flatten_dict(d, self.separator).items() ]) else: result = "\n".join([ "{}{}{}={}".format(self.key, self.separator, k, v) for d in data_list for k, v in flatten_dict(d, self.separator).items() ]) return result
def unset(self, key): config_data = flatten_dict(self.read().data) del config_data[key] with tempfile.NamedTemporaryFile() as temp: tmp_config = self.__class__.temp_clone(name=temp.name, base_file=self) key = [x for x in config_data.keys()] value = [config_data[x] for x in key] tmp_config.write(key=key, value=value, read_existing_data=False) shutil.copyfile(temp.name, self.name)
def test_flatten(self): self.assertTrue(dict_equal(flatten_dict(dict_unflat), dict_flat))