This repository was archived by the owner on Jun 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
This repository was archived by the owner on Jun 9, 2021. It is now read-only.
Bug in json_saveables Dict handling #183
Copy link
Copy link
Closed
Description
The following pull requests are prerequisites to reproducing the problem with my code:
My Donors() class employs a dict of dicts which appears not be be handled by the json_saveables code. I'm trying to debug it, but so far haven't been able to get my head around it.
The objects we are starting with:
In [4]: z
Out[4]: Donors( Donor( did='70e3d2b8-159e-11e8-b256-0800274b0a84', first_name='Jerico', last_name='Jonez', created='2018-02-19T17:58:07.779796Z' ) )
In [5]:
When converting the Donors() object into a dict, it looks good to me (yes, Donors() is a dict, with a uuid key):
{'__obj_type': 'Donors',
'_donors': {'ada5118e-159a-11e8-b256-0800274b0a84': {'__obj_type': 'Donor',
'_did': 'ada5118e-159a-11e8-b256-0800274b0a84',
'_donations': [],
'_first_name': 'Jerico',
'_last_name': 'Jonez',
'_middle_name': '',
'_suffix': '',
'audit': [{'action': 'did',
'args': ('4a311ba2-159a-11e8-b256-0800274b0a84',),
'kwargs': {},
'time': '2018-02-19T17:28:24.868073Z',
'user': 'fred johnes'},
{'action': 'first_name',
'args': ('jerico',),
'kwargs': {},
'time': '2018-02-19T17:28:24.868225Z',
'user': 'fred johnes'},
{'action': 'middle_name',
'args': ('',),
'kwargs': {},
'time': '2018-02-19T17:28:24.868241Z',
'user': 'fred johnes'},
{'action': 'last_name',
'args': ('jonez',),
'kwargs': {},
'time': '2018-02-19T17:28:24.868255Z',
'user': 'fred johnes'},
{'action': 'suffix',
'args': ('',),
'kwargs': {},
'time': '2018-02-19T17:28:24.868267Z',
'user': 'fred johnes'},
{'action': 'add_donor',
'args': (Donor( did='4a311ba2-159a-11e8-b256-0800274b0a84', first_name='Jerico', last_name='Jonez', created='2018-02-19T17:28:24.868199Z' ),),
'kwargs': {},
'time': '2018-02-19T17:28:31.606493Z',
'user': 'fred johnes'},
{'action': 'did',
'args': ('4a311ba2-159a-11e8-b256-0800274b0a84',),
'kwargs': {},
'time': '2018-02-19T17:29:59.691606Z',
'user': 'fred johnes'},
{'action': 'first_name',
'args': ('Jerico',),
'kwargs': {},
'time': '2018-02-19T17:29:59.691661Z',
'user': 'fred johnes'},
{'action': 'last_name',
'args': ('Jonez',),
'kwargs': {},
'time': '2018-02-19T17:29:59.691677Z',
'user': 'fred johnes'},
{'action': 'middle_name',
'args': ('',),
'kwargs': {},
'time': '2018-02-19T17:29:59.691690Z',
'user': 'fred johnes'},
{'action': 'suffix',
'args': ('',),
'kwargs': {},
'time': '2018-02-19T17:29:59.691703Z',
'user': 'fred johnes'},
{'action': 'did',
'args': ('ada5118e-159a-11e8-b256-0800274b0a84',),
'kwargs': {},
'time': '2018-02-19T17:31:11.722453Z',
'user': 'fred johnes'},
{'action': 'first_name',
'args': ('jerico',),
'kwargs': {},
'time': '2018-02-19T17:31:11.722521Z',
'user': 'fred johnes'},
{'action': 'middle_name',
'args': ('',),
'kwargs': {},
'time': '2018-02-19T17:31:11.722538Z',
'user': 'fred johnes'},
{'action': 'last_name',
'args': ('jonez',),
'kwargs': {},
'time': '2018-02-19T17:31:11.722553Z',
'user': 'fred johnes'},
{'action': 'suffix',
'args': ('',),
'kwargs': {},
'time': '2018-02-19T17:31:11.722566Z',
'user': 'fred johnes'},
{'action': 'add_donor',
'args': (Donor( did='ada5118e-159a-11e8-b256-0800274b0a84', first_name='Jerico', last_name='Jonez', created='2018-02-19T17:31:11.722495Z' ),),
'kwargs': {},
'time': '2018-02-19T17:31:11.722657Z',
'user': 'fred johnes'}],
'created': '2018-02-19T17:31:11.722495Z'}},
'audit': [{'action': 'did',
'args': ('4a311ba2-159a-11e8-b256-0800274b0a84',),
'kwargs': {},
'time': '2018-02-19T17:28:24.868073Z',
'user': 'fred johnes'},
{'action': 'first_name',
'args': ('jerico',),
'kwargs': {},
'time': '2018-02-19T17:28:24.868225Z',
'user': 'fred johnes'},
{'action': 'middle_name',
'args': ('',),
'kwargs': {},
'time': '2018-02-19T17:28:24.868241Z',
'user': 'fred johnes'},
{'action': 'last_name',
'args': ('jonez',),
'kwargs': {},
'time': '2018-02-19T17:28:24.868255Z',
'user': 'fred johnes'},
{'action': 'suffix',
'args': ('',),
'kwargs': {},
'time': '2018-02-19T17:28:24.868267Z',
'user': 'fred johnes'},
{'action': 'add_donor',
'args': (Donor( did='4a311ba2-159a-11e8-b256-0800274b0a84', first_name='Jerico', last_name='Jonez', created='2018-02-19T17:28:24.868199Z' ),),
'kwargs': {},
'time': '2018-02-19T17:28:31.606493Z',
'user': 'fred johnes'},
{'action': 'did',
'args': ('4a311ba2-159a-11e8-b256-0800274b0a84',),
'kwargs': {},
'time': '2018-02-19T17:29:59.691606Z',
'user': 'fred johnes'},
{'action': 'first_name',
'args': ('Jerico',),
'kwargs': {},
'time': '2018-02-19T17:29:59.691661Z',
'user': 'fred johnes'},
{'action': 'last_name',
'args': ('Jonez',),
'kwargs': {},
'time': '2018-02-19T17:29:59.691677Z',
'user': 'fred johnes'},
{'action': 'middle_name',
'args': ('',),
'kwargs': {},
'time': '2018-02-19T17:29:59.691690Z',
'user': 'fred johnes'},
{'action': 'suffix',
'args': ('',),
'kwargs': {},
'time': '2018-02-19T17:29:59.691703Z',
'user': 'fred johnes'},
{'action': 'did',
'args': ('ada5118e-159a-11e8-b256-0800274b0a84',),
'kwargs': {},
'time': '2018-02-19T17:31:11.722453Z',
'user': 'fred johnes'},
{'action': 'first_name',
'args': ('jerico',),
'kwargs': {},
'time': '2018-02-19T17:31:11.722521Z',
'user': 'fred johnes'},
{'action': 'middle_name',
'args': ('',),
'kwargs': {},
'time': '2018-02-19T17:31:11.722538Z',
'user': 'fred johnes'},
{'action': 'last_name',
'args': ('jonez',),
'kwargs': {},
'time': '2018-02-19T17:31:11.722553Z',
'user': 'fred johnes'},
{'action': 'suffix',
'args': ('',),
'kwargs': {},
'time': '2018-02-19T17:31:11.722566Z',
'user': 'fred johnes'},
{'action': 'add_donor',
'args': (Donor( did='ada5118e-159a-11e8-b256-0800274b0a84', first_name='Jerico', last_name='Jonez', created='2018-02-19T17:31:11.722495Z' ),),
'kwargs': {},
'time': '2018-02-19T17:31:11.722657Z',
'user': 'fred johnes'}]}
But when we try to turn the dict back into Python, we observe the following error:
In [1]: from mailroom import security
...: from mailroom.donors import Donors, Donor
...: from mailroom import security
...: security.user = "fred johnes"
...: x = Donor(full_name="jerico jonez")
...: z = Donors(x)
...: j = z.to_json_compat()
...: z2 = Donor.from_json_dict(j)
...:
> /home/johnn/uwpython/IntroPython-2017/solutions/metaprogramming/json_save/json_save/json_save_meta.py(110)from_json_dict()
-> obj = cls.__new__(cls)
(Pdb) n
> /home/johnn/uwpython/IntroPython-2017/solutions/metaprogramming/json_save/json_save/json_save_meta.py(111)from_json_dict()
-> for attr, typ in cls._attrs_to_save.items():
(Pdb) n
> /home/johnn/uwpython/IntroPython-2017/solutions/metaprogramming/json_save/json_save/json_save_meta.py(112)from_json_dict()
-> setattr(obj, attr, typ.to_python(dic[attr]))
(Pdb) attr
'created'
(Pdb) obj
Donor( donations=[{'time': '2018-02-19T17:58:07.779622Z', 'user': 'fred johnes', 'action': 'did', 'args': ('70e3d2b8-159e-11e8-b256-0800274b0a84',), 'kwargs': {}}, {'time': '2018-02-19T17:58:07.779827Z', 'user': 'fred johnes', 'action': 'first_name', 'args': ('jerico',), 'kwargs': {}}, {'time': '2018-02-19T17:58:07.779845Z', 'user': 'fred johnes', 'action': 'middle_name', 'args': ('',), 'kwargs': {}}, {'time': '2018-02-19T17:58:07.779862Z', 'user': 'fred johnes', 'action': 'last_name', 'args': ('jonez',), 'kwargs': {}}, {'time': '2018-02-19T17:58:07.779875Z', 'user': 'fred johnes', 'action': 'suffix', 'args': ('',), 'kwargs': {}}, {'time': '2018-02-19T17:58:07.779970Z', 'user': 'fred johnes', 'action': 'add_donor', 'args': (Donor( did='70e3d2b8-159e-11e8-b256-0800274b0a84', first_name='Jerico', last_name='Jonez', created='2018-02-19T17:58:07.779796Z' ),), 'kwargs': {}}] )
(Pdb) dic[attr]
*** KeyError: 'created'
(Pdb)
(Pdb) typ
<json_save.saveables.String object at 0x7fb560056a20>
(Pdb) cls
<class 'mailroom.donors.Donor'>
(Pdb)
> /home/johnn/uwpython/IntroPython-2017/solutions/metaprogramming/json_save/json_save/json_save_meta.py(112)from_json_dict()
-> setattr(obj, attr, typ.to_python(dic[attr]))
(Pdb) l
107 """
108 # create a new object
109 import pdb; pdb.set_trace()
110 obj = cls.__new__(cls)
111 for attr, typ in cls._attrs_to_save.items():
112 -> setattr(obj, attr, typ.to_python(dic[attr]))
113 # make sure it gets initialized
114 # obj.__init__()
115 return obj
116
117 def to_json(self, fp=None, indent=4):
(Pdb)
(Pdb) cls._attrs_to_save.items()
dict_items([('created', <json_save.saveables.String object at 0x7ff4f8bd99e8>), ('audit', <json_save.saveables.List object at 0x7ff4f8beebe0>), ('_did', <json_save.saveables.String object at 0x7ff4f8beec18>), ('_first_name', <json_save.saveables.String object at 0x7ff4f8beec50>), ('_last_name', <json_save.saveables.String object at 0x7ff4f8beec88>), ('_middle_name', <json_save.saveables.String object at 0x7ff4f8beecf8>), ('_suffix', <json_save.saveables.String object at 0x7ff4f8beecc0>), ('_donations', <json_save.saveables.List object at 0x7ff4f8756048>)])
(Pdb)
Metadata
Metadata
Assignees
Labels
No labels