2
2
"""
3
3
Copyright (C) 2015, marazt. All rights reserved.
4
4
"""
5
- from inspect import getmembers , isroutine
5
+ from inspect import getmembers , isroutine , signature
6
6
from datetime import date , datetime
7
7
8
8
from mapper .casedict import CaseDict
@@ -166,10 +166,6 @@ def map(self, from_obj, to_type=type(None), ignore_case=False, allow_none=False,
166
166
.format (key_from .__module__ , key_from .__name__ , to_type .__module__ , to_type .__name__ ))
167
167
key_to = to_type
168
168
custom_mappings = self .mappings [key_from ][key_to ][1 ]
169
-
170
- # Currently, all target class data members need to have default value
171
- # Object with __init__ that carries required non-default arguments are not supported
172
- inst = key_to ()
173
169
174
170
def not_private (s ):
175
171
return not s .startswith ('_' )
@@ -183,6 +179,12 @@ def is_included(s, mapping):
183
179
from_obj_attributes = getmembers (from_obj , lambda a : not isroutine (a ))
184
180
from_obj_dict = {k : v for k , v in from_obj_attributes }
185
181
182
+ # support __init__ by passing arguments by keyword when instantiating the key_to
183
+ sig = signature (key_to .__init__ )
184
+ kwargs = {x : from_obj_dict [x ] for x in sig .parameters if x not in ('self' , 'kwargs' )}
185
+
186
+ inst = key_to (** kwargs )
187
+
186
188
to_obj_attributes = getmembers (inst , lambda a : not isroutine (a ))
187
189
to_obj_dict = {k : v for k , v in to_obj_attributes if not_excluded (k ) and (not_private (k ) or is_included (k , custom_mappings ))}
188
190
0 commit comments