Fixed dump benedict object to yaml not working correctly. #43
This commit is contained in:
parent
261bc8aa52
commit
cdcc638758
|
@ -13,106 +13,109 @@ class BaseDict(dict):
|
|||
self._dict = None
|
||||
super(BaseDict, self).__init__(*args, **kwargs)
|
||||
|
||||
def _is_pointer(self):
|
||||
return self._dict is not self and self._dict is not None
|
||||
|
||||
def __contains__(self, key):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
return key in self._dict
|
||||
return super(BaseDict, self).__contains__(key)
|
||||
|
||||
def __delitem__(self, key):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
del self._dict[key]
|
||||
return
|
||||
super(BaseDict, self).__delitem__(key)
|
||||
|
||||
def __eq__(self, other):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
return self._dict == other
|
||||
return super(BaseDict, self).__eq__(other)
|
||||
|
||||
def __getitem__(self, key):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
return self._dict[key]
|
||||
return super(BaseDict, self).__getitem__(key)
|
||||
|
||||
def __iter__(self):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
return iter(self._dict)
|
||||
return super(BaseDict, self).__iter__()
|
||||
|
||||
def __len__(self):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
return len(self._dict)
|
||||
return super(BaseDict, self).__len__()
|
||||
|
||||
def __repr__(self):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
return repr(self._dict)
|
||||
return super(BaseDict, self).__repr__()
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
self._dict[key] = value
|
||||
return
|
||||
super(BaseDict, self).__setitem__(key, value)
|
||||
|
||||
def __str__(self):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
return str(self._dict)
|
||||
return super(BaseDict, self).__str__()
|
||||
|
||||
def __unicode__(self):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
return unicode(self._dict)
|
||||
return super(BaseDict, self).__unicode__()
|
||||
|
||||
def clear(self):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
self._dict.clear()
|
||||
return
|
||||
super(BaseDict, self).clear()
|
||||
|
||||
def copy(self):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
return self._dict.copy()
|
||||
return super(BaseDict, self).copy()
|
||||
|
||||
def dict(self):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
return self._dict
|
||||
return self
|
||||
|
||||
def get(self, key, default=None):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
return self._dict.get(key, default)
|
||||
return super(BaseDict, self).get(key, default)
|
||||
|
||||
def items(self):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
return self._dict.items()
|
||||
return super(BaseDict, self).items()
|
||||
|
||||
def keys(self):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
return self._dict.keys()
|
||||
return super(BaseDict, self).keys()
|
||||
|
||||
def pop(self, key, *args):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
return self._dict.pop(key, *args)
|
||||
return super(BaseDict, self).pop(key, *args)
|
||||
|
||||
def setdefault(self, key, default=None):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
return self._dict.setdefault(key, default)
|
||||
return super(BaseDict, self).setdefault(key, default)
|
||||
|
||||
def update(self, other):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
self._dict.update(other)
|
||||
return
|
||||
super(BaseDict, self).update(other)
|
||||
|
||||
def values(self):
|
||||
if self._dict is not None:
|
||||
if self._is_pointer():
|
||||
return self._dict.values()
|
||||
return super(BaseDict, self).values()
|
||||
|
|
|
@ -159,7 +159,7 @@ class IODict(BaseDict):
|
|||
"""
|
||||
kwargs['subformat'] = subformat
|
||||
kwargs['encoding'] = encoding
|
||||
return self._encode(self, 'base64', **kwargs)
|
||||
return self._encode(self.dict(), 'base64', **kwargs)
|
||||
|
||||
def to_csv(self, key='values', columns=None, columns_row=True, **kwargs):
|
||||
"""
|
||||
|
@ -171,7 +171,7 @@ class IODict(BaseDict):
|
|||
"""
|
||||
kwargs['columns'] = columns
|
||||
kwargs['columns_row'] = columns_row
|
||||
return self._encode(self[key], 'csv', **kwargs)
|
||||
return self._encode(self.dict()[key], 'csv', **kwargs)
|
||||
|
||||
def to_json(self, **kwargs):
|
||||
"""
|
||||
|
@ -181,7 +181,7 @@ class IODict(BaseDict):
|
|||
Return the encoded string and optionally save it at 'filepath'.
|
||||
A ValueError is raised in case of failure.
|
||||
"""
|
||||
return self._encode(self, 'json', **kwargs)
|
||||
return self._encode(self.dict(), 'json', **kwargs)
|
||||
|
||||
def to_pickle(self, **kwargs):
|
||||
"""
|
||||
|
@ -192,7 +192,7 @@ class IODict(BaseDict):
|
|||
Return the encoded string and optionally save it at 'filepath'.
|
||||
A ValueError is raised in case of failure.
|
||||
"""
|
||||
return self._encode(self, 'pickle', **kwargs)
|
||||
return self._encode(self.dict(), 'pickle', **kwargs)
|
||||
|
||||
def to_plist(self, **kwargs):
|
||||
"""
|
||||
|
@ -202,7 +202,7 @@ class IODict(BaseDict):
|
|||
Return the encoded string and optionally save it at 'filepath'.
|
||||
A ValueError is raised in case of failure.
|
||||
"""
|
||||
return self._encode(self, 'plist', **kwargs)
|
||||
return self._encode(self.dict(), 'plist', **kwargs)
|
||||
|
||||
def to_query_string(self, **kwargs):
|
||||
"""
|
||||
|
@ -210,7 +210,7 @@ class IODict(BaseDict):
|
|||
Return the encoded string and optionally save it at 'filepath'.
|
||||
A ValueError is raised in case of failure.
|
||||
"""
|
||||
return self._encode(self, 'query_string', **kwargs)
|
||||
return self._encode(self.dict(), 'query_string', **kwargs)
|
||||
|
||||
def to_toml(self, **kwargs):
|
||||
"""
|
||||
|
@ -220,7 +220,7 @@ class IODict(BaseDict):
|
|||
Return the encoded string and optionally save it at 'filepath'.
|
||||
A ValueError is raised in case of failure.
|
||||
"""
|
||||
return self._encode(self, 'toml', **kwargs)
|
||||
return self._encode(self.dict(), 'toml', **kwargs)
|
||||
|
||||
def to_xml(self, **kwargs):
|
||||
"""
|
||||
|
@ -230,7 +230,7 @@ class IODict(BaseDict):
|
|||
Return the encoded string and optionally save it at 'filepath'.
|
||||
A ValueError is raised in case of failure.
|
||||
"""
|
||||
return self._encode(self, 'xml', **kwargs)
|
||||
return self._encode(self.dict(), 'xml', **kwargs)
|
||||
|
||||
def to_yaml(self, **kwargs):
|
||||
"""
|
||||
|
@ -240,4 +240,4 @@ class IODict(BaseDict):
|
|||
Return the encoded string and optionally save it at 'filepath'.
|
||||
A ValueError is raised in case of failure.
|
||||
"""
|
||||
return self._encode(self, 'yaml', **kwargs)
|
||||
return self._encode(self.dict(), 'yaml', **kwargs)
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from benedict import benedict
|
||||
|
||||
import unittest
|
||||
|
||||
|
||||
class github_issue_0043_test_case(unittest.TestCase):
|
||||
|
||||
"""
|
||||
https://github.com/fabiocaccamo/python-benedict/issues/43
|
||||
|
||||
To run this specific test:
|
||||
- Run python -m unittest tests.github.test_issue_0043
|
||||
"""
|
||||
|
||||
def test_to_yaml(self):
|
||||
b = benedict({"level1": {"level2": "Hello world"}})
|
||||
s = b.to_yaml()
|
||||
r = """level1:
|
||||
level2: Hello world
|
||||
"""
|
||||
self.assertEqual(s, r)
|
Loading…
Reference in New Issue