Use if-elif instead of multiple if statements to check types
Use if-elif instead of multiple if statements to check types when converting from MongoDB BaseDict and BaseList to python dict and list types. Once the value is converted, use another if-elif block to recursively evaluate and convert the values of dict and list.
@@ -19,16 +19,16 @@ import six
def mongodb_to_python_types(value):
+ # Convert MongoDB BaseDict and BaseList types to python dict and list types.
if isinstance(value, mongoengine.base.datastructures.BaseDict):
value = dict(value)
-
- if isinstance(value, mongoengine.base.datastructures.BaseList):
+ elif isinstance(value, mongoengine.base.datastructures.BaseList):
value = list(value)
+ # Recursively traverse the dict and list to convert values.
if isinstance(value, dict):
value = {k: mongodb_to_python_types(v) for k, v in six.iteritems(value)}
-
- if isinstance(value, list):
+ elif isinstance(value, list):
value = [mongodb_to_python_types(v) for v in value]
return value
