Skip to content

Commit c527c1d

Browse files
author
Kenneth Reitz
committed
cleaner attar defines
1 parent a111e50 commit c527c1d

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

github3/helpers.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ def is_collection(obj):
2626

2727

2828
# from arc90/python-readability-api
29-
def to_python(obj, in_dict, string_keys=None, date_keys=None, object_map=None, **kwargs):
29+
def to_python(obj,
30+
in_dict,
31+
str_keys=None,
32+
date_keys=None,
33+
int_keys=None,
34+
object_map=None,
35+
bool_keys=None, **kwargs):
3036
"""Extends a given object for API Consumption.
3137
3238
:param obj: Object to extend.
@@ -36,9 +42,8 @@ def to_python(obj, in_dict, string_keys=None, date_keys=None, object_map=None, *
3642
:param object_map: Dict of {key, obj} map, for nested object results.
3743
"""
3844

39-
if string_keys:
40-
for in_key in string_keys:
41-
# print in_key
45+
if str_keys:
46+
for in_key in str_keys:
4247
obj.__dict__[in_key] = in_dict.get(in_key)
4348

4449
if date_keys:
@@ -52,6 +57,14 @@ def to_python(obj, in_dict, string_keys=None, date_keys=None, object_map=None, *
5257

5358
obj.__dict__[in_key] = out_date
5459

60+
if int_keys:
61+
for in_key in int_keys:
62+
obj.__dict__[in_key] = int(in_dict.get(in_key))
63+
64+
if bool_keys:
65+
for in_key in bool_keys:
66+
obj.__dict__[in_key] = bool(in_dict.get(in_key))
67+
5568
if object_map:
5669

5770
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, *
6376

6477

6578
# from arc90/python-readability-api
66-
def to_api(in_dict, int_keys=None, date_keys=None):
79+
def to_api(in_dict, int_keys=None, date_keys=None, bool_keys=None):
6780
"""Extends a given object for API Production."""
6881

6982
# Cast all int_keys to int()

github3/models.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
class BaseResource(object):
1212
"""A BaseResource object."""
1313

14-
_strings = []
14+
_strs = []
1515
_ints = []
16-
_datetimes = []
17-
_booleans = []
16+
_dates = []
17+
_bools = []
1818
_map = {}
1919

2020

@@ -37,16 +37,19 @@ def __dir__(self):
3737
def _bootstrap(self):
3838
"""Bootstraps the model object based on configured values."""
3939

40-
for attr in (self._strings + self._ints + self._datetimes + self._booleans + self._map.keys()):
40+
for attr in (self._strs + self._ints + self._dates + self._bools + self._map.keys()):
4141
setattr(self, attr, None)
4242

43+
4344
@classmethod
4445
def new_from_dict(cls, d, gh=None):
4546

4647
return to_python(
4748
obj=cls(), in_dict=d,
48-
string_keys = cls._strings,
49+
str_keys = cls._strings,
50+
int_keys = cls._ints,
4951
date_keys = cls._datetimes,
52+
bool_keys = cls._booleans,
5053
_gh = gh
5154
)
5255

0 commit comments

Comments
 (0)