Skip to content

Commit 8a6fd14

Browse files
author
direbearform
committed
python 2.7 compatibility fixes
1 parent d956462 commit 8a6fd14

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ target/
5858
# Idea
5959
.idea
6060
.python-version
61+
62+
# VSCode
63+
.vscode

.vscode/settings.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

mapper/object_mapper.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"""
33
Copyright (C) 2015, marazt. All rights reserved.
44
"""
5-
from typing import List, Dict
65
from mapper.object_mapper_exception import ObjectMapperException
76
from mapper.casedict import CaseDict
87
from inspect import getmembers, isroutine
@@ -88,7 +87,8 @@ class 'B' with attributes 'name' and 'age' and we want to map 'A' to 'B' in a wa
8887
self.mappings = {}
8988
pass
9089

91-
def create_map(self, type_from: type, type_to: type, mapping: Dict =None):
90+
def create_map(self, type_from, type_to, mapping=None):
91+
# type: (type, type, Dict) -> None
9292
"""Method for adding mapping definitions
9393
9494
:param type_from: source type
@@ -100,16 +100,16 @@ def create_map(self, type_from: type, type_to: type, mapping: Dict =None):
100100
"""
101101

102102
if (type(type_from) is not type):
103-
raise ObjectMapperException(f"type_from must be a type")
103+
raise ObjectMapperException("type_from must be a type")
104104

105105
if (type(type_to) is not type):
106-
raise ObjectMapperException(f"type_to must be a type")
106+
raise ObjectMapperException("type_to must be a type")
107107

108-
if (mapping is not None and not isinstance(mapping, Dict)):
109-
raise ObjectMapperException(f"mapping, if provided, must be a Dict type")
108+
if (mapping is not None and not isinstance(mapping, dict)):
109+
raise ObjectMapperException("mapping, if provided, must be a Dict type")
110110

111-
key_from = f"{type_from.__module__}.{type_from.__name__}"
112-
key_to = f"{type_to.__module__}.{type_to.__name__}"
111+
key_from = "{0}.{1}".format(type_from.__module__, type_from.__name__)
112+
key_to = "{0}.{1}".format(type_to.__module__, type_to.__name__)
113113

114114
if key_from in self.mappings:
115115
inner_map = self.mappings[key_from]
@@ -122,7 +122,8 @@ def create_map(self, type_from: type, type_to: type, mapping: Dict =None):
122122
self.mappings[key_from][key_to] = (type_to, mapping)
123123

124124

125-
def map(self, from_obj, to_type: type=type(None), ignore_case: bool=False, allow_none: bool=False, excluded: List[str]=None, included: List[str]=None, allow_unmapped: bool=False):
125+
def map(self, from_obj, to_type=type(None), ignore_case=False, allow_none=False, excluded=None, included=None, allow_unmapped=False):
126+
# type: (object, type, bool, bool, List[str], List[str], bool) -> object
126127
"""Method for creating target object instance
127128
128129
:param from_obj: source object to be mapped from
@@ -141,10 +142,10 @@ def map(self, from_obj, to_type: type=type(None), ignore_case: bool=False, allow
141142
# one of the tests is explicitly checking for an attribute error on __dict__ if it's not set
142143
from_obj.__dict__
143144

144-
key_from = f"{from_obj.__class__.__module__}.{from_obj.__class__.__name__}"
145+
key_from = "{0}.{1}".format(from_obj.__class__.__module__, from_obj.__class__.__name__)
145146

146147
if key_from not in self.mappings:
147-
raise ObjectMapperException(f"No mapping defined for {key_from}")
148+
raise ObjectMapperException("No mapping defined for {0}".format(key_from))
148149

149150
custom_mappings = None
150151
key_to_cls = type(None)
@@ -154,14 +155,14 @@ def map(self, from_obj, to_type: type=type(None), ignore_case: bool=False, allow
154155
# if this is a nested call and we do not currently support more than one to_types
155156
assert(len(self.mappings[key_from]) > 0)
156157
if len(self.mappings[key_from]) > 1:
157-
raise ObjectMapperException(f"Ambiguous type mapping exists for {key_from}, must specifiy to_type explicitly")
158+
raise ObjectMapperException("Ambiguous type mapping exists for {0}, must specifiy to_type explicitly".format(key_from))
158159
the_only_key_to = next(iter(self.mappings[key_from]))
159160
key_to_cls = self.mappings[key_from][the_only_key_to][0]
160161
custom_mappings = self.mappings[key_from][the_only_key_to][1]
161162
else:
162-
key_to = f"{to_type.__module__}.{to_type.__name__}"
163+
key_to = "{0}.{1}".format(to_type.__module__, to_type.__name__)
163164
if key_to not in self.mappings[key_from]:
164-
raise ObjectMapperException(f"No mapping defined for {key_from} to {key_to}")
165+
raise ObjectMapperException("No mapping defined for {0} to {1}".format(key_from, key_to))
165166
key_to_cls = self.mappings[key_from][key_to][0]
166167
custom_mappings = self.mappings[key_from][key_to][1]
167168

@@ -194,7 +195,7 @@ def is_included(s, mapping):
194195
def map_obj(o, allow_unmapped):
195196
if o is not None:
196197
key_from_child_cls = o.__class__
197-
key_from_child = f"{key_from_child_cls.__module__}.{key_from_child_cls.__name__}"
198+
key_from_child = "{0}.{1}".format(key_from_child_cls.__module__, key_from_child_cls.__name__)
198199
if (key_from_child in self.mappings):
199200
# if key_to has a mapping defined, nests the map() call
200201
return self.map(o, type(None), ignore_case, allow_none, excluded, included, allow_unmapped)
@@ -206,7 +207,7 @@ def map_obj(o, allow_unmapped):
206207
if allow_unmapped:
207208
return o
208209
else:
209-
raise ObjectMapperException(f"No mapping defined for {key_from_child}")
210+
raise ObjectMapperException("No mapping defined for {0}".format(key_from_child))
210211
else:
211212
return None
212213

@@ -230,7 +231,7 @@ def map_obj(o, allow_unmapped):
230231
elif prop in from_props:
231232
# try find property with the same name in the source
232233
from_obj_child = from_props[prop]
233-
if isinstance(from_obj_child, List):
234+
if isinstance(from_obj_child, list):
234235
val = [map_obj(from_obj_child_i, allow_unmapped) for from_obj_child_i in from_obj_child]
235236
else:
236237
val = map_obj(from_obj_child, allow_unmapped)

tests/object_mapper_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def test_mapping_included_field_by_mapping(self):
387387
#Arrange
388388
from_class = FromTestClass()
389389
mapper = ObjectMapper()
390-
mapper.create_map(FromTestClass, ToTestClass, mapping={'_actor_name': lambda o: f"{o.name} acted by {o._actor_name}"})
390+
mapper.create_map(FromTestClass, ToTestClass, mapping={'_actor_name': lambda o: "{0} acted by {1}".format(o.name, o._actor_name)})
391391

392392
#Act
393393
result = mapper.map(FromTestClass())
@@ -397,7 +397,7 @@ def test_mapping_included_field_by_mapping(self):
397397
self.assertTrue(isinstance(result, ToTestClass), "Type must be ToTestClass")
398398
self.assertEqual(result.name, from_class.name, "Name mapping must be equal")
399399
self.assertEqual(result.date, from_class.date, "Date mapping must be equal")
400-
self.assertEqual(result._actor_name, f"{from_class.name} acted by {from_class._actor_name}", "Private is copied if explicitly mapped")
400+
self.assertEqual(result._actor_name, "{0} acted by {1}".format(from_class.name, from_class._actor_name), "Private is copied if explicitly mapped")
401401
self.assertNotIn("surname", result.__dict__, "To class must not contain surname")
402402

403403
def test_mapping_creation_complex_without_mappings_correct(self):

0 commit comments

Comments
 (0)