Skip to content

Commit

Permalink
Merge pull request #31 from jetavator/issue-6-Implement_copy_and_deep…
Browse files Browse the repository at this point in the history
…copy_without_relying_on_dict_and_list_implementations

Implement and test copy and deepcopy
  • Loading branch information
jtv8 authored May 22, 2020
2 parents ca84721 + b6a661a commit f70718c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
2 changes: 2 additions & 0 deletions features/dict.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions features/steps/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import importlib.util
import yaml
import json
import copy


@given("the Python module {module}.py")
Expand Down
19 changes: 18 additions & 1 deletion wysdom/dom/DOMDict.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
7 changes: 6 additions & 1 deletion wysdom/dom/DOMList.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 8 additions & 1 deletion wysdom/dom/DOMObject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f70718c

Please sign in to comment.