cengal.data_manipulation.conversion.reinterpret_cast.versions.v_0.reinterpret_cast

 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
19__all__ = ['reinterpret_cast_attempt', 'reinterpret_cast', 'temporary_reinterpret_cast', 'TemporaryReinterpretCast']
20
21
22"""
23Module Docstring
24Docstrings: http://www.python.org/dev/peps/pep-0257/
25"""
26
27__author__ = "ButenkoMS <gtalk@butenkoms.space>"
28__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
29__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
30__license__ = "Apache License, Version 2.0"
31__version__ = "4.4.1"
32__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
33__email__ = "gtalk@butenkoms.space"
34# __status__ = "Prototype"
35__status__ = "Development"
36# __status__ = "Production"
37
38
39from contextlib import contextmanager
40from typing import Type, Any, Generator, TypeVar
41
42
43T = TypeVar('T')
44
45
46def reinterpret_cast_attempt(new_type: Type[T], value: Any) -> bool:
47    result: bool = True
48    try:
49        value.__class__ = new_type
50    except TypeError:
51        result = False
52    
53    return result
54
55
56def reinterpret_cast(new_type: Type[T], value: Any) -> T:
57    value.__class__ = new_type
58    return value
59
60
61@contextmanager
62def temporary_reinterpret_cast(temporary_type: Type[T], value: Any) -> Generator[T, None, None]:
63    original_class = value.__class__
64    value.__class__ = temporary_type
65    try:
66        yield value
67    finally:
68        value.__class__ = original_class
69
70
71class TemporaryReinterpretCast:
72    def __init__(self, temporary_type: Type[T], value: Any):
73        self.temporary_type: Type[T] = temporary_type
74        self.value: Any = value
75        self.original_class = value.__class__
76    
77    def __enter__(self):
78        value = self.value
79        value.__class__ = self.temporary_type
80        return value
81    
82    def __exit__(self, type, value, traceback):
83        self.value.__class__ = self.original_class
def reinterpret_cast_attempt(new_type: Type[~T], value: Any) -> bool:
47def reinterpret_cast_attempt(new_type: Type[T], value: Any) -> bool:
48    result: bool = True
49    try:
50        value.__class__ = new_type
51    except TypeError:
52        result = False
53    
54    return result
def reinterpret_cast(new_type: Type[~T], value: Any) -> ~T:
57def reinterpret_cast(new_type: Type[T], value: Any) -> T:
58    value.__class__ = new_type
59    return value
@contextmanager
def temporary_reinterpret_cast( temporary_type: Type[~T], value: Any) -> Generator[~T, NoneType, NoneType]:
62@contextmanager
63def temporary_reinterpret_cast(temporary_type: Type[T], value: Any) -> Generator[T, None, None]:
64    original_class = value.__class__
65    value.__class__ = temporary_type
66    try:
67        yield value
68    finally:
69        value.__class__ = original_class
class TemporaryReinterpretCast:
72class TemporaryReinterpretCast:
73    def __init__(self, temporary_type: Type[T], value: Any):
74        self.temporary_type: Type[T] = temporary_type
75        self.value: Any = value
76        self.original_class = value.__class__
77    
78    def __enter__(self):
79        value = self.value
80        value.__class__ = self.temporary_type
81        return value
82    
83    def __exit__(self, type, value, traceback):
84        self.value.__class__ = self.original_class
TemporaryReinterpretCast(temporary_type: Type[~T], value: Any)
73    def __init__(self, temporary_type: Type[T], value: Any):
74        self.temporary_type: Type[T] = temporary_type
75        self.value: Any = value
76        self.original_class = value.__class__
temporary_type: Type[~T]
value: Any
original_class