Skip to content

Commit 9a65645

Browse files
author
Kenneth Reitz
committed
add packing to key_diff
1 parent 673c864 commit 9a65645

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

github3/helpers.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def is_collection(obj):
2525
return val
2626

2727

28-
def key_diff(source, update):
28+
def key_diff(source, update, pack=False):
2929
"""Given two dictionaries, returns a list of the changed keys."""
3030

3131
source = dict(source)
@@ -39,7 +39,15 @@ def key_diff(source, update):
3939
if (v != u_v) and (u_v is not None):
4040
changed.append(k)
4141

42-
return changed
42+
if pack is False:
43+
return changed
44+
45+
d = dict()
46+
47+
for k in changed:
48+
d[k] = update[k]
49+
50+
return d
4351

4452

4553
# from arc90/python-readability-api
@@ -59,9 +67,11 @@ def to_python(obj,
5967
:param object_map: Dict of {key, obj} map, for nested object results.
6068
"""
6169

70+
d = dict()
71+
6272
if str_keys:
6373
for in_key in str_keys:
64-
obj.__dict__[in_key] = in_dict.get(in_key)
74+
d[in_key] = in_dict.get(in_key)
6575

6676
if date_keys:
6777
for in_key in date_keys:
@@ -71,25 +81,30 @@ def to_python(obj,
7181
except TypeError:
7282
out_date = None
7383

74-
obj.__dict__[in_key] = out_date
84+
d[in_key] = out_date
7585

7686
if int_keys:
7787
for in_key in int_keys:
7888
if (in_dict is not None) and (in_dict.get(in_key) is not None):
79-
obj.__dict__[in_key] = int(in_dict.get(in_key))
89+
d[in_key] = int(in_dict.get(in_key))
8090

8191
if bool_keys:
8292
for in_key in bool_keys:
8393
if in_dict.get(in_key) is not None:
84-
obj.__dict__[in_key] = bool(in_dict.get(in_key))
94+
d[in_key] = bool(in_dict.get(in_key))
8595

8696
if object_map:
8797
for (k, v) in object_map.items():
8898
if in_dict.get(k):
89-
obj.__dict__[k] = v.new_from_dict(in_dict.get(k))
99+
d[k] = v.new_from_dict(in_dict.get(k))
90100

101+
obj.__dict__.update(d)
91102
obj.__dict__.update(kwargs)
92103

104+
# Save the dictionary, for write comparisons.
105+
obj._cache = d
106+
obj.__cache = in_dict
107+
93108
return obj
94109

95110

0 commit comments

Comments
 (0)