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