cengal.entities.copyable.versions.v_0.copyable

  1#!/usr/bin/env python
  2# coding=utf-8
  3
  4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>
  5# 
  6# Licensed under the Apache License, Version 2.0 (the "License");
  7# you may not use this file except in compliance with the License.
  8# You may obtain a copy of the License at
  9# 
 10#     http://www.apache.org/licenses/LICENSE-2.0
 11# 
 12# Unless required by applicable law or agreed to in writing, software
 13# distributed under the License is distributed on an "AS IS" BASIS,
 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15# See the License for the specific language governing permissions and
 16# limitations under the License.
 17
 18__all__ = ['copy__impl', 'CopyMixin', 'deepcopy__impl', 'DeepCopyMixin', 'CopyMethodsMixin', 'CopyableMixin', 'get_dict_key_with_callable_default']
 19
 20"""
 21Module Docstring
 22Docstrings: http://www.python.org/dev/peps/pep-0257/
 23"""
 24
 25__author__ = "ButenkoMS <gtalk@butenkoms.space>"
 26__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
 27__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
 28__license__ = "Apache License, Version 2.0"
 29__version__ = "4.4.1"
 30__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
 31__email__ = "gtalk@butenkoms.space"
 32# __status__ = "Prototype"
 33__status__ = "Development"
 34# __status__ = "Production"
 35
 36
 37from copy import deepcopy
 38from typing import Callable, Dict, Hashable, Any
 39from cengal.data_manipulation.get_dict_key_with_callable_default import get_dict_key_with_callable_default
 40
 41
 42def copy__impl(self):
 43    cls = self.__class__
 44    result = cls.__new__(cls)
 45    try:
 46        result.__dict__.update(self.__dict__)
 47        return result
 48    except AttributeError:
 49        pass
 50
 51    for field in self.__slots__:
 52        setattr(result, field, getattr(self, field))
 53    
 54    return result
 55
 56
 57class CopyMixin:
 58    def __copy__(self):
 59        return copy__impl(self)
 60
 61
 62def deepcopy__impl(self, memo):
 63    cls = self.__class__
 64    result = cls.__new__(cls)
 65    memo[id(self)] = result
 66    try:
 67        for k, v in self.__dict__.items():
 68            setattr(result, k, deepcopy(v, memo))
 69        
 70        return result
 71    except AttributeError:
 72        pass
 73
 74    for field in self.__slots__:
 75        setattr(result, field, getattr(self, field))
 76    
 77    return result
 78
 79
 80class DeepCopyMixin:
 81    def __deepcopy__(self, memo):
 82        return deepcopy__impl(self, memo)
 83
 84
 85
 86class CopyMethodsMixin:
 87    def copy(self):
 88        """Should make relevant copy of an object (not so general and deep as a deepcopy()). should copy only known object fields.
 89        Example:
 90            def copy(self):
 91                cls = self.__class__
 92                result = cls.__new__(cls)
 93                result.__dict__['dimension'] = self.dimension
 94                result.__dict__['_point'] = self._point.copy()
 95                return result
 96
 97        Raises:
 98            NotImplementedError: _description_
 99        """
100        raise NotImplementedError
101
102    def shallow_copy(self):
103        """Same as copy.copy(self), but should copy only known object fields.
104        Example:
105            def shallow_copy(self):
106                cls = self.__class__
107                result = cls.__new__(cls)
108                result.__dict__['dimension'] = self.dimension
109                result.__dict__['_point'] = self._point
110                return result
111
112        Raises:
113            NotImplementedError: _description_
114        """
115        raise NotImplementedError
116
117    def updated_copy(self, update: Dict):
118        """Will make updated copy of an object. Other behavior should be the same as in the `def copy(self)` method.
119        Example:
120            # from cengal.data_manipulation.get_dict_key_with_callable_default import get_dict_key_with_callable_default
121            from cengal.entities.copyable import CopyableMixin, get_dict_key_with_callable_default
122            
123            def updated_copy(self, update: Dict):
124                cls = self.__class__
125                result = cls.__new__(cls)
126                result.__dict__['dimension'] = update.get('dimension', self.dimension)
127                result.__dict__['_point'] = get_dict_key_with_callable_default(update, '_point', lambda: self._point.copy())
128                return result
129
130        Raises:
131            NotImplementedError: _description_
132        """
133        raise NotImplementedError
134
135
136class CopyableMixin(CopyMixin, DeepCopyMixin, CopyMethodsMixin):
137    pass
def copy__impl(self):
43def copy__impl(self):
44    cls = self.__class__
45    result = cls.__new__(cls)
46    try:
47        result.__dict__.update(self.__dict__)
48        return result
49    except AttributeError:
50        pass
51
52    for field in self.__slots__:
53        setattr(result, field, getattr(self, field))
54    
55    return result
class CopyMixin:
58class CopyMixin:
59    def __copy__(self):
60        return copy__impl(self)
def deepcopy__impl(self, memo):
63def deepcopy__impl(self, memo):
64    cls = self.__class__
65    result = cls.__new__(cls)
66    memo[id(self)] = result
67    try:
68        for k, v in self.__dict__.items():
69            setattr(result, k, deepcopy(v, memo))
70        
71        return result
72    except AttributeError:
73        pass
74
75    for field in self.__slots__:
76        setattr(result, field, getattr(self, field))
77    
78    return result
class DeepCopyMixin:
81class DeepCopyMixin:
82    def __deepcopy__(self, memo):
83        return deepcopy__impl(self, memo)
class CopyMethodsMixin:
 87class CopyMethodsMixin:
 88    def copy(self):
 89        """Should make relevant copy of an object (not so general and deep as a deepcopy()). should copy only known object fields.
 90        Example:
 91            def copy(self):
 92                cls = self.__class__
 93                result = cls.__new__(cls)
 94                result.__dict__['dimension'] = self.dimension
 95                result.__dict__['_point'] = self._point.copy()
 96                return result
 97
 98        Raises:
 99            NotImplementedError: _description_
100        """
101        raise NotImplementedError
102
103    def shallow_copy(self):
104        """Same as copy.copy(self), but should copy only known object fields.
105        Example:
106            def shallow_copy(self):
107                cls = self.__class__
108                result = cls.__new__(cls)
109                result.__dict__['dimension'] = self.dimension
110                result.__dict__['_point'] = self._point
111                return result
112
113        Raises:
114            NotImplementedError: _description_
115        """
116        raise NotImplementedError
117
118    def updated_copy(self, update: Dict):
119        """Will make updated copy of an object. Other behavior should be the same as in the `def copy(self)` method.
120        Example:
121            # from cengal.data_manipulation.get_dict_key_with_callable_default import get_dict_key_with_callable_default
122            from cengal.entities.copyable import CopyableMixin, get_dict_key_with_callable_default
123            
124            def updated_copy(self, update: Dict):
125                cls = self.__class__
126                result = cls.__new__(cls)
127                result.__dict__['dimension'] = update.get('dimension', self.dimension)
128                result.__dict__['_point'] = get_dict_key_with_callable_default(update, '_point', lambda: self._point.copy())
129                return result
130
131        Raises:
132            NotImplementedError: _description_
133        """
134        raise NotImplementedError
def copy(self):
 88    def copy(self):
 89        """Should make relevant copy of an object (not so general and deep as a deepcopy()). should copy only known object fields.
 90        Example:
 91            def copy(self):
 92                cls = self.__class__
 93                result = cls.__new__(cls)
 94                result.__dict__['dimension'] = self.dimension
 95                result.__dict__['_point'] = self._point.copy()
 96                return result
 97
 98        Raises:
 99            NotImplementedError: _description_
100        """
101        raise NotImplementedError

Should make relevant copy of an object (not so general and deep as a deepcopy()). should copy only known object fields. Example: def copy(self): cls = self.__class__ result = cls.__new__(cls) result.__dict__['dimension'] = self.dimension result.__dict__['_point'] = self._point.copy() return result

Raises: NotImplementedError: _description_

def shallow_copy(self):
103    def shallow_copy(self):
104        """Same as copy.copy(self), but should copy only known object fields.
105        Example:
106            def shallow_copy(self):
107                cls = self.__class__
108                result = cls.__new__(cls)
109                result.__dict__['dimension'] = self.dimension
110                result.__dict__['_point'] = self._point
111                return result
112
113        Raises:
114            NotImplementedError: _description_
115        """
116        raise NotImplementedError

Same as copy.copy(self), but should copy only known object fields. Example: def shallow_copy(self): cls = self.__class__ result = cls.__new__(cls) result.__dict__['dimension'] = self.dimension result.__dict__['_point'] = self._point return result

Raises: NotImplementedError: _description_

def updated_copy(self, update: typing.Dict):
118    def updated_copy(self, update: Dict):
119        """Will make updated copy of an object. Other behavior should be the same as in the `def copy(self)` method.
120        Example:
121            # from cengal.data_manipulation.get_dict_key_with_callable_default import get_dict_key_with_callable_default
122            from cengal.entities.copyable import CopyableMixin, get_dict_key_with_callable_default
123            
124            def updated_copy(self, update: Dict):
125                cls = self.__class__
126                result = cls.__new__(cls)
127                result.__dict__['dimension'] = update.get('dimension', self.dimension)
128                result.__dict__['_point'] = get_dict_key_with_callable_default(update, '_point', lambda: self._point.copy())
129                return result
130
131        Raises:
132            NotImplementedError: _description_
133        """
134        raise NotImplementedError

Will make updated copy of an object. Other behavior should be the same as in the def copy(self) method. Example: # from cengal.data_manipulation.get_dict_key_with_callable_default import get_dict_key_with_callable_default from cengal.entities.copyable import CopyableMixin, get_dict_key_with_callable_default

def updated_copy(self, update: Dict):
    cls = self.__class__
    result = cls.__new__(cls)
    result.__dict__['dimension'] = update.get('dimension', self.dimension)
    result.__dict__['_point'] = get_dict_key_with_callable_default(update, '_point', lambda: self._point.copy())
    return result

Raises: NotImplementedError: _description_

class CopyableMixin(CopyMixin, DeepCopyMixin, CopyMethodsMixin):
137class CopyableMixin(CopyMixin, DeepCopyMixin, CopyMethodsMixin):
138    pass
def get_dict_key_with_callable_default(data: typing.Dict, key: typing.Hashable, default: typing.Callable) -> Any:
43def get_dict_key_with_callable_default(data: Dict, key: Hashable, default: Callable) -> Any:
44    if key in data:
45        return data[key]
46    else:
47        return default()