From b6a661a5b04d2a078f911a875a0d5bf12c5886d8 Mon Sep 17 00:00:00 2001 From: Joe Taylor Date: Fri, 22 May 2020 19:22:33 +0200 Subject: [PATCH] Implement and test copy and deepcopy --- features/dict.feature | 2 ++ features/steps/steps.py | 1 + wysdom/dom/DOMDict.py | 19 ++++++++++++++++++- wysdom/dom/DOMList.py | 7 ++++++- wysdom/dom/DOMObject.py | 9 ++++++++- 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/features/dict.feature b/features/dict.feature index 150a5f3..de52959 100644 --- a/features/dict.feature +++ b/features/dict.feature @@ -109,6 +109,8 @@ Feature: Test JSON DOM objects schema(example).is_valid(example_dict_input) schema(example).jsonschema_dict == expected_schema example_dict_output == example_dict_input + copy.copy(example).to_builtin() == example_dict_input + copy.deepcopy(example).to_builtin() == example_dict_input """ Scenario: Test bad input string diff --git a/features/steps/steps.py b/features/steps/steps.py index a51ad38..4aade48 100644 --- a/features/steps/steps.py +++ b/features/steps/steps.py @@ -6,6 +6,7 @@ import importlib.util import yaml import json +import copy @given("the Python module {module}.py") diff --git a/wysdom/dom/DOMDict.py b/wysdom/dom/DOMDict.py index 5baffbe..a601178 100644 --- a/wysdom/dom/DOMDict.py +++ b/wysdom/dom/DOMDict.py @@ -1,10 +1,13 @@ from __future__ import annotations -from typing import Generic, TypeVar, Optional, Any +from typing import Generic, TypeVar, Optional, Any, Dict from collections.abc import Mapping +from copy import deepcopy + from ..base_schema import Schema, SchemaAnything +from .DOMElement import DOMElement from .DOMObject import DOMObject from . import DOMInfo from .DOMProperties import DOMProperties @@ -32,3 +35,17 @@ def __init__( def __getitem__(self, key: str) -> T_co: return super().__getitem__(key) + + def __deepcopy__(self, memo: Dict[int, DOMElement]) -> DOMDict: + cls = self.__class__ + # noinspection PyArgumentList + result = cls( + value={ + k: deepcopy(v, memo) + for k, v in self.items() + }, + json_dom_info=self.__json_dom_info__, + _item_type=self.__json_schema_properties__.additional_properties + ) + memo[id(self)] = result + return result diff --git a/wysdom/dom/DOMList.py b/wysdom/dom/DOMList.py index 17fad82..f17c324 100644 --- a/wysdom/dom/DOMList.py +++ b/wysdom/dom/DOMList.py @@ -121,6 +121,11 @@ def __copy__(self) -> DOMList: def __deepcopy__(self, memo: Dict[int, DOMElement]) -> DOMList: cls = self.__class__ - result = cls(deepcopy(list(self), memo)) + # noinspection PyArgumentList + result = cls( + value=(deepcopy(x, memo) for x in self), + json_dom_info=self.__json_dom_info__, + _item_type=self.item_type + ) memo[id(self)] = result return result diff --git a/wysdom/dom/DOMObject.py b/wysdom/dom/DOMObject.py index 1e3ae90..dea1588 100644 --- a/wysdom/dom/DOMObject.py +++ b/wysdom/dom/DOMObject.py @@ -104,6 +104,13 @@ def __copy__(self) -> DOMObject: def __deepcopy__(self, memo: Dict[int, DOMElement]) -> DOMObject: cls = self.__class__ - result = cls(deepcopy(dict(self), memo)) + # noinspection PyArgumentList + result = cls( + value={ + k: deepcopy(v, memo) + for k, v in self.items() + }, + json_dom_info=self.__json_dom_info__ + ) memo[id(self)] = result return result