From 4541c7cd8499ed22306059c2434e09e29ec57e36 Mon Sep 17 00:00:00 2001 From: Sokkka Date: Tue, 15 Dec 2020 00:00:59 +1100 Subject: [PATCH 1/3] Added support for python version 3.3 and higher, added support for classes with different meta class parents, and support for dataclasses --- mapper/casedict.py | 6 +++++- mapper/object_mapper.py | 23 +++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/mapper/casedict.py b/mapper/casedict.py index b1cb499..7704b85 100644 --- a/mapper/casedict.py +++ b/mapper/casedict.py @@ -3,8 +3,12 @@ Copyright (c) 2013 Optiflows https://bitbucket.org/optiflowsrd/obelus/src/tip/obelus/casedict.py """ +import sys -from collections import MutableMapping +if sys.version_info.major > 3 and sys.version_info.minor >= 3: + from collections.abc import MutableMapping +else: + from collections import MutableMapping _sentinel = object() diff --git a/mapper/object_mapper.py b/mapper/object_mapper.py index 381a524..e6e8627 100644 --- a/mapper/object_mapper.py +++ b/mapper/object_mapper.py @@ -2,8 +2,9 @@ """ Copyright (C) 2015, marazt. All rights reserved. """ -from inspect import getmembers, isroutine +from inspect import getmembers, isroutine, isclass from datetime import date, datetime +from dataclasses import is_dataclass, MISSING from mapper.casedict import CaseDict from mapper.object_mapper_exception import ObjectMapperException @@ -101,11 +102,11 @@ def create_map(self, type_from, type_to, mapping=None): :return: None """ - if (type(type_from) is not type): - raise ObjectMapperException("type_from must be a type") + if not isclass(type_from): + raise ObjectMapperException("type_from must be a class") - if (type(type_to) is not type): - raise ObjectMapperException("type_to must be a type") + if not isclass(type_to): + raise ObjectMapperException("type_to must be a class") if (mapping is not None and not isinstance(mapping, dict)): raise ObjectMapperException("mapping, if provided, must be a Dict type") @@ -169,7 +170,17 @@ def map(self, from_obj, to_type=type(None), ignore_case=False, allow_none=False, # Currently, all target class data members need to have default value # Object with __init__ that carries required non-default arguments are not supported - inst = key_to() + if is_dataclass(key_to): + def default_val(v): + if v.default is not MISSING: + return v.default + if v.default_factory is not MISSING: + return v.default_factory() + return None + inst = key_to(**{k: default_val(v) for k, v in key_to.__dataclass_fields__.items()}) + else: + inst = key_to() + def not_private(s): return not s.startswith('_') From a29be959ccec5b646d2a58ef24422a91e1860904 Mon Sep 17 00:00:00 2001 From: Sokkka Date: Tue, 15 Dec 2020 15:10:03 +1100 Subject: [PATCH 2/3] removed unecassary version check --- mapper/casedict.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/mapper/casedict.py b/mapper/casedict.py index 7704b85..5a8b762 100644 --- a/mapper/casedict.py +++ b/mapper/casedict.py @@ -3,12 +3,7 @@ Copyright (c) 2013 Optiflows https://bitbucket.org/optiflowsrd/obelus/src/tip/obelus/casedict.py """ -import sys - -if sys.version_info.major > 3 and sys.version_info.minor >= 3: - from collections.abc import MutableMapping -else: - from collections import MutableMapping +from collections import MutableMapping _sentinel = object() From 32f85d7fb40ff709aa3c19d63b067dbae9e8a97e Mon Sep 17 00:00:00 2001 From: Sokkka Date: Tue, 15 Dec 2020 15:11:00 +1100 Subject: [PATCH 3/3] removed line change --- mapper/casedict.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mapper/casedict.py b/mapper/casedict.py index 5a8b762..b1cb499 100644 --- a/mapper/casedict.py +++ b/mapper/casedict.py @@ -3,6 +3,7 @@ Copyright (c) 2013 Optiflows https://bitbucket.org/optiflowsrd/obelus/src/tip/obelus/casedict.py """ + from collections import MutableMapping