Skip to content

Add support for python 3 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mapperpy/attributes_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
def get_attributes(obj):

if isinstance(obj, dict):
return obj.keys()
return list(obj.keys())

attributes = inspect.getmembers(obj, lambda a: not(inspect.isroutine(a)))
return [attr[0] for attr in attributes if not(attr[0].startswith('__') and attr[0].endswith('__'))]
Expand Down
4 changes: 2 additions & 2 deletions mapperpy/object_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def __get_explicit_mapping(cls, input_mapping):
mapping = {}
rev_mapping = {}

for left, right in input_mapping.items():
for left, right in list(input_mapping.items()):
if right is None:
# user requested to suppress implicit mapping for k
mapping[left] = rev_mapping[left] = None
Expand All @@ -164,7 +164,7 @@ def __split_converters(self, converters_dict):
to_right_converters = {}
to_left_converters = {}

for left_attr_name, converters_tuple in converters_dict.iteritems():
for left_attr_name, converters_tuple in converters_dict.items():
if not isinstance(converters_tuple, tuple) or len(converters_tuple) != 2:
raise ValueError("Converters for {} should be provided in a 2-element tuple".format(left_attr_name))

Expand Down
26 changes: 17 additions & 9 deletions mapperpy/one_way_mapper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collections
from datetime import datetime
from enum import Enum

Expand All @@ -8,6 +9,12 @@
__author__ = 'lgrech'


try:
basestring
except NameError:
basestring = str


class OneWayMapper(object):

def __init__(self, target_class, target_prototype_obj=None, attributes_cache_provider=AttributesCache):
Expand Down Expand Up @@ -87,7 +94,8 @@ def target_value_converters(self, converters_dict):
self.__target_value_converters.update(converters_dict)
return self

def options(self, (setting_name, setting_value)):
def options(self, settings):
(setting_name, setting_value) = settings
self.__general_settings[setting_name] = setting_value
return self

Expand All @@ -100,7 +108,7 @@ def __try_create_target_object(self, param_dict):
return self.__target_class(**param_dict)
except TypeError as er:
raise AttributeError("Error when initializing class {} with params: {}\n{}".format(
self.__target_class.__name__, param_dict, er.message))
self.__target_class.__name__, param_dict, er.args[0]))

def __get_mapped_params_dict(self, obj_from):

Expand All @@ -111,12 +119,12 @@ def __get_mapped_params_dict(self, obj_from):
mapped_params_dict.update(self.__apply_initializers(obj_from))
return mapped_params_dict
except AttributeError as er:
raise AttributeError("Unknown attribute: {}".format(er.message))
raise AttributeError("Unknown attribute: {}".format(er.args[0]))

def __apply_initializers(self, source_obj):
initialized_params_dict = {}

for attr_name, init_func in self.__target_initializers.items():
for attr_name, init_func in list(self.__target_initializers.items()):
initialized_params_dict[attr_name] = init_func(source_obj)

return initialized_params_dict
Expand All @@ -125,7 +133,7 @@ def __apply_mapping(self, source_obj, attr_name_mapping):

mapped_params_dict = {}

for attr_name_from, attr_name_to in attr_name_mapping.items():
for attr_name_from, attr_name_to in list(attr_name_mapping.items()):
# skip since mapping is suppressed by user (attribute_name = None)
if not (attr_name_from and attr_name_to):
continue
Expand Down Expand Up @@ -177,7 +185,7 @@ def __apply_type_conversion(cls, from_type, to_type, attr_value):
return cls.__get_conversion_from_enum(attr_value, to_type)
elif issubclass(to_type, Enum):
return cls.__get_conversion_to_enum(attr_value, from_type, to_type)
elif issubclass(from_type, datetime) and issubclass(to_type, basestring):
elif issubclass(from_type, datetime) and issubclass(to_type, str):
return cls.__get_conversion_from_datetime(attr_value)
elif issubclass(from_type, basestring) and issubclass(to_type, datetime):
return cls.__get_conversion_to_datetime(attr_value)
Expand All @@ -192,7 +200,7 @@ def __get_conversion_to_datetime(cls, attr_value):
return datetime.strptime(attr_value, "%Y-%m-%dT%H:%M:%S")
except ValueError as e2:
raise ValueError("Could not create datetime object from string: {}. {}. {}".format(
attr_value, e1.message, e2.message))
attr_value, e1.args[0], e2.args[0]))

@classmethod
def __get_conversion_from_datetime(cls, attr_value):
Expand Down Expand Up @@ -270,8 +278,8 @@ def __get_setting(self, mapper_option, default_val):

@staticmethod
def __verify_if_callable(name_callable_map, error_message_template):
for name, obj in name_callable_map.iteritems():
if not callable(obj):
for name, obj in name_callable_map.items():
if not isinstance(obj, collections.Callable):
raise ValueError(error_message_template.format(name))

def __repr__(self):
Expand Down
8 changes: 4 additions & 4 deletions mapperpy/test/conversions/test_custom_value_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_target_value_converters_when_converter_not_function_should_raise_except
OneWayMapper.for_target_class(TestClassSomePropertyEmptyInit1).target_value_converters(
{"some_property_02": 7}
)
assert_that(context.exception.message).contains("some_property_02")
assert_that(context.exception.args[0]).contains("some_property_02")

def test_map_for_single_converter(self):
# given
Expand Down Expand Up @@ -70,21 +70,21 @@ def test_object_mapper_value_converters_when_converters_not_in_tuple_should_rais
ObjectMapper.from_class(TestClassSomePropertyEmptyInit1, TestClassSomePropertyEmptyInit2).value_converters(
{"some_property_02": lambda val: 7})

assert_that(context.exception.message).contains("some_property_02")
assert_that(context.exception.args[0]).contains("some_property_02")

def test_object_mapper_value_converters_when_converter_not_in_2_element_tuple_should_raise_exception(self):
with self.assertRaises(ValueError) as context:
ObjectMapper.from_class(TestClassSomePropertyEmptyInit1, TestClassSomePropertyEmptyInit2).value_converters(
{"some_property_02": (lambda val: 7,)})

assert_that(context.exception.message).contains("some_property_02")
assert_that(context.exception.args[0]).contains("some_property_02")

def test_object_mapper_value_converters_when_converter_not_callable_should_raise_exception(self):
with self.assertRaises(ValueError) as context:
ObjectMapper.from_class(TestClassSomePropertyEmptyInit1, TestClassSomePropertyEmptyInit2).value_converters(
{"some_property_02": (lambda val: 7, "not_a_function")})

assert_that(context.exception.message).contains("some_property_02")
assert_that(context.exception.args[0]).contains("some_property_02")

def test_object_mapper_map_with_value_converter_and_default_mapping(self):
# given
Expand Down
7 changes: 6 additions & 1 deletion mapperpy/test/conversions/test_datetime_conversion.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import unittest
from assertpy import assert_that

Expand All @@ -9,6 +10,10 @@
__author__ = 'lgrech'


if sys.version_info > (3, 0):
unicode = str


class DateTimeConversionTest(unittest.TestCase):

def test_map_from_datetime_it_target_type_not_known(self):
Expand Down Expand Up @@ -50,7 +55,7 @@ def test_map_from_string_to_datetime_wrong_format_should_raise_exception(self):
mapper.map(TestClassSomePropertyEmptyInit1(some_property_02="wrong_date_format"))

# then
assert_that(context.exception.message).contains("wrong_date_format")
assert_that(context.exception.args[0]).contains("wrong_date_format")

def test_map_from_string_to_datetime(self):
# given
Expand Down
5 changes: 5 additions & 0 deletions mapperpy/test/conversions/test_enum_conversion.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import unittest
from assertpy import assert_that
from enum import Enum
Expand All @@ -9,6 +10,10 @@
__author__ = 'lgrech'


if sys.version_info > (3, 0):
unicode = str


class EnumConversionTest(unittest.TestCase):

def test_enum_to_int_mapping_should_map_to_enum_value(self):
Expand Down
16 changes: 8 additions & 8 deletions mapperpy/test/test_dict_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ def test_map_when_ambiguous_nested_mapping_should_raise_exception(self):
root_mapper.map(dict(some_property=TestClassSomePropertyEmptyInit1().__dict__))

# then
assert_that(context.exception.message).contains("some_property")
assert_that(context.exception.message).contains("dict")
assert_that(context.exception.args[0]).contains("some_property")
assert_that(context.exception.args[0]).contains("dict")

def test_map_explicit_when_ambiguous_nested_mapping_should_raise_exception(self):
# given
Expand All @@ -237,9 +237,9 @@ def test_map_explicit_when_ambiguous_nested_mapping_should_raise_exception(self)
root_mapper.map(dict(some_property=dict()))

# then
assert_that(context.exception.message).contains("some_property")
assert_that(context.exception.message).contains("mapped_property")
assert_that(context.exception.message).contains("dict")
assert_that(context.exception.args[0]).contains("some_property")
assert_that(context.exception.args[0]).contains("mapped_property")
assert_that(context.exception.args[0]).contains("dict")

def test_map_with_multiple_nested_mappings_when_no_matching_mapper_for_target_type_should_raise_exception(self):
# given
Expand All @@ -256,9 +256,9 @@ def test_map_with_multiple_nested_mappings_when_no_matching_mapper_for_target_ty
root_mapper.map(dict(some_property=dict(some_property_02="nested_value_02")))

# then
assert_that(context.exception.message).contains("some_property")
assert_that(context.exception.message).contains("dict")
assert_that(context.exception.message).contains("TestClassMappedPropertyEmptyInit")
assert_that(context.exception.args[0]).contains("some_property")
assert_that(context.exception.args[0]).contains("dict")
assert_that(context.exception.args[0]).contains("TestClassMappedPropertyEmptyInit")

def test_map_with_multiple_nested_mappings_for_one_attribute_when_target_type_known(self):
# given
Expand Down
Loading