mirror of
https://github.com/kennethreitz/python-github3.git
synced 2026-06-05 23:10:17 +00:00
cleaner attar defines
This commit is contained in:
+18
-5
@@ -26,7 +26,13 @@ def is_collection(obj):
|
||||
|
||||
|
||||
# from arc90/python-readability-api
|
||||
def to_python(obj, in_dict, string_keys=None, date_keys=None, object_map=None, **kwargs):
|
||||
def to_python(obj,
|
||||
in_dict,
|
||||
str_keys=None,
|
||||
date_keys=None,
|
||||
int_keys=None,
|
||||
object_map=None,
|
||||
bool_keys=None, **kwargs):
|
||||
"""Extends a given object for API Consumption.
|
||||
|
||||
:param obj: Object to extend.
|
||||
@@ -36,9 +42,8 @@ def to_python(obj, in_dict, string_keys=None, date_keys=None, object_map=None, *
|
||||
:param object_map: Dict of {key, obj} map, for nested object results.
|
||||
"""
|
||||
|
||||
if string_keys:
|
||||
for in_key in string_keys:
|
||||
# print in_key
|
||||
if str_keys:
|
||||
for in_key in str_keys:
|
||||
obj.__dict__[in_key] = in_dict.get(in_key)
|
||||
|
||||
if date_keys:
|
||||
@@ -52,6 +57,14 @@ def to_python(obj, in_dict, string_keys=None, date_keys=None, object_map=None, *
|
||||
|
||||
obj.__dict__[in_key] = out_date
|
||||
|
||||
if int_keys:
|
||||
for in_key in int_keys:
|
||||
obj.__dict__[in_key] = int(in_dict.get(in_key))
|
||||
|
||||
if bool_keys:
|
||||
for in_key in bool_keys:
|
||||
obj.__dict__[in_key] = bool(in_dict.get(in_key))
|
||||
|
||||
if object_map:
|
||||
|
||||
for (k, v) in object_map.items():
|
||||
@@ -63,7 +76,7 @@ def to_python(obj, in_dict, string_keys=None, date_keys=None, object_map=None, *
|
||||
|
||||
|
||||
# from arc90/python-readability-api
|
||||
def to_api(in_dict, int_keys=None, date_keys=None):
|
||||
def to_api(in_dict, int_keys=None, date_keys=None, bool_keys=None):
|
||||
"""Extends a given object for API Production."""
|
||||
|
||||
# Cast all int_keys to int()
|
||||
|
||||
+8
-5
@@ -11,10 +11,10 @@ from .helpers import to_python, to_api
|
||||
class BaseResource(object):
|
||||
"""A BaseResource object."""
|
||||
|
||||
_strings = []
|
||||
_strs = []
|
||||
_ints = []
|
||||
_datetimes = []
|
||||
_booleans = []
|
||||
_dates = []
|
||||
_bools = []
|
||||
_map = {}
|
||||
|
||||
|
||||
@@ -37,16 +37,19 @@ class BaseResource(object):
|
||||
def _bootstrap(self):
|
||||
"""Bootstraps the model object based on configured values."""
|
||||
|
||||
for attr in (self._strings + self._ints + self._datetimes + self._booleans + self._map.keys()):
|
||||
for attr in (self._strs + self._ints + self._dates + self._bools + self._map.keys()):
|
||||
setattr(self, attr, None)
|
||||
|
||||
|
||||
@classmethod
|
||||
def new_from_dict(cls, d, gh=None):
|
||||
|
||||
return to_python(
|
||||
obj=cls(), in_dict=d,
|
||||
string_keys = cls._strings,
|
||||
str_keys = cls._strings,
|
||||
int_keys = cls._ints,
|
||||
date_keys = cls._datetimes,
|
||||
bool_keys = cls._booleans,
|
||||
_gh = gh
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user