Removed duplicated code and benedicton decorator.
This commit is contained in:
parent
d2da6a26a7
commit
e57527b55e
|
@ -1,6 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from benedict.dicts import benedict, benediction
|
from benedict.dicts import benedict
|
||||||
from benedict.metadata import (
|
from benedict.metadata import (
|
||||||
__author__, __copyright__, __description__,
|
__author__, __copyright__, __description__,
|
||||||
__license__, __title__, __version__,
|
__license__, __title__, __version__,
|
||||||
|
|
|
@ -6,15 +6,6 @@ from benedict.dicts.parse import ParseDict
|
||||||
from benedict.utils import dict_util
|
from benedict.utils import dict_util
|
||||||
|
|
||||||
|
|
||||||
def benediction(method):
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
value = method(*args, **kwargs)
|
|
||||||
if isinstance(value, dict) and not isinstance(value, benedict):
|
|
||||||
return benedict(value)
|
|
||||||
return value
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
class benedict(IODict, KeypathDict, ParseDict):
|
class benedict(IODict, KeypathDict, ParseDict):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
@ -26,9 +17,8 @@ class benedict(IODict, KeypathDict, ParseDict):
|
||||||
def clone(self):
|
def clone(self):
|
||||||
return dict_util.clone(self)
|
return dict_util.clone(self)
|
||||||
|
|
||||||
@benediction
|
|
||||||
def copy(self):
|
def copy(self):
|
||||||
return super(benedict, self).copy()
|
return benedict(super(benedict, self).copy())
|
||||||
|
|
||||||
def deepcopy(self):
|
def deepcopy(self):
|
||||||
return self.clone()
|
return self.clone()
|
||||||
|
@ -45,46 +35,6 @@ class benedict(IODict, KeypathDict, ParseDict):
|
||||||
def flatten(self, separator='_'):
|
def flatten(self, separator='_'):
|
||||||
return dict_util.flatten(self, separator)
|
return dict_util.flatten(self, separator)
|
||||||
|
|
||||||
@classmethod
|
|
||||||
@benediction
|
|
||||||
def fromkeys(cls, sequence, value=None):
|
|
||||||
return KeypathDict.fromkeys(sequence, value)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
@benediction
|
|
||||||
def from_base64(s, subformat='json', encoding='utf-8', **kwargs):
|
|
||||||
return IODict.from_base64(s, subformat=subformat, encoding=encoding, **kwargs)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
@benediction
|
|
||||||
def from_csv(s, columns=None, columns_row=True, **kwargs):
|
|
||||||
return IODict.from_csv(s, columns=columns, columns_row=columns_row, **kwargs)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
@benediction
|
|
||||||
def from_json(s, **kwargs):
|
|
||||||
return IODict.from_json(s, **kwargs)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
@benediction
|
|
||||||
def from_query_string(s, **kwargs):
|
|
||||||
return IODict.from_query_string(s, **kwargs)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
@benediction
|
|
||||||
def from_toml(s, **kwargs):
|
|
||||||
return IODict.from_toml(s, **kwargs)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
@benediction
|
|
||||||
def from_xml(s, **kwargs):
|
|
||||||
return IODict.from_xml(s, **kwargs)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
@benediction
|
|
||||||
def from_yaml(s, **kwargs):
|
|
||||||
return IODict.from_yaml(s, **kwargs)
|
|
||||||
|
|
||||||
def invert(self, flat=False):
|
def invert(self, flat=False):
|
||||||
return dict_util.invert(self, flat)
|
return dict_util.invert(self, flat)
|
||||||
|
|
||||||
|
|
|
@ -52,37 +52,37 @@ class IODict(dict):
|
||||||
io_util.write_file(filepath, s)
|
io_util.write_file(filepath, s)
|
||||||
return s
|
return s
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def from_base64(s, subformat='json', encoding='utf-8', **kwargs):
|
def from_base64(cls, s, subformat='json', encoding='utf-8', **kwargs):
|
||||||
kwargs['subformat'] = subformat
|
kwargs['subformat'] = subformat
|
||||||
kwargs['encoding'] = encoding
|
kwargs['encoding'] = encoding
|
||||||
return IODict._decode(s, 'base64', **kwargs)
|
return cls(IODict._decode(s, 'base64', **kwargs))
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def from_csv(s, columns=None, columns_row=True, **kwargs):
|
def from_csv(cls, s, columns=None, columns_row=True, **kwargs):
|
||||||
kwargs['columns'] = columns
|
kwargs['columns'] = columns
|
||||||
kwargs['columns_row'] = columns_row
|
kwargs['columns_row'] = columns_row
|
||||||
return IODict._decode(s, 'csv', **kwargs)
|
return cls(IODict._decode(s, 'csv', **kwargs))
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def from_json(s, **kwargs):
|
def from_json(cls, s, **kwargs):
|
||||||
return IODict._decode(s, 'json', **kwargs)
|
return cls(IODict._decode(s, 'json', **kwargs))
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def from_query_string(s, **kwargs):
|
def from_query_string(cls, s, **kwargs):
|
||||||
return IODict._decode(s, 'query_string', **kwargs)
|
return cls(IODict._decode(s, 'query_string', **kwargs))
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def from_toml(s, **kwargs):
|
def from_toml(cls, s, **kwargs):
|
||||||
return IODict._decode(s, 'toml', **kwargs)
|
return cls(IODict._decode(s, 'toml', **kwargs))
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def from_xml(s, **kwargs):
|
def from_xml(cls, s, **kwargs):
|
||||||
return IODict._decode(s, 'xml', **kwargs)
|
return cls(IODict._decode(s, 'xml', **kwargs))
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def from_yaml(s, **kwargs):
|
def from_yaml(cls, s, **kwargs):
|
||||||
return IODict._decode(s, 'yaml', **kwargs)
|
return cls(IODict._decode(s, 'yaml', **kwargs))
|
||||||
|
|
||||||
def to_base64(self, subformat='json', encoding='utf-8', **kwargs):
|
def to_base64(self, subformat='json', encoding='utf-8', **kwargs):
|
||||||
kwargs['subformat'] = subformat
|
kwargs['subformat'] = subformat
|
||||||
|
|
|
@ -124,8 +124,9 @@ class KeypathDict(dict):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def fromkeys(cls, sequence, value=None):
|
def fromkeys(cls, sequence, value=None):
|
||||||
d = KeypathDict()
|
f = dict.fromkeys(sequence, value)
|
||||||
for key in sequence:
|
d = cls()
|
||||||
|
for key, value in f.items():
|
||||||
d[key] = value
|
d[key] = value
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from benedict import benedict, benediction
|
from benedict import benedict
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
|
@ -9,33 +9,6 @@ import unittest
|
||||||
|
|
||||||
class benedict_test_case(unittest.TestCase):
|
class benedict_test_case(unittest.TestCase):
|
||||||
|
|
||||||
def test_benediction_decorator(self):
|
|
||||||
@benediction
|
|
||||||
def f1():
|
|
||||||
return None
|
|
||||||
self.assertEqual(f1(), None)
|
|
||||||
|
|
||||||
@benediction
|
|
||||||
def f2():
|
|
||||||
return 1
|
|
||||||
self.assertEqual(f2(), 1)
|
|
||||||
|
|
||||||
@benediction
|
|
||||||
def f3():
|
|
||||||
return 'ok'
|
|
||||||
self.assertEqual(f3(), 'ok')
|
|
||||||
|
|
||||||
@benediction
|
|
||||||
def f4():
|
|
||||||
return []
|
|
||||||
self.assertEqual(f4(), [])
|
|
||||||
|
|
||||||
@benediction
|
|
||||||
def f5():
|
|
||||||
return {}
|
|
||||||
self.assertEqual(f5(), {})
|
|
||||||
self.assertTrue(isinstance(f5(), benedict))
|
|
||||||
|
|
||||||
def test_clean(self):
|
def test_clean(self):
|
||||||
d = {
|
d = {
|
||||||
'a': {},
|
'a': {},
|
||||||
|
|
Loading…
Reference in New Issue