Wrapper
Bases: ABC
flowchart TD
pyochain.abc.constructors.Wrapper[Wrapper]
click pyochain.abc.constructors.Wrapper href "" "pyochain.abc.constructors.Wrapper"
Source code in pyochain/abc/constructors.pyi
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
wrap(wrapped)
abstractmethod
staticmethod
Create the instance from a reference to an existing data structure corresponding to this pyochain type.
E.g, Vec.wrap(list) or Dict.wrap(dict).
If you have an Iterator, prefer using from_iter instead of Wrapper.wrap(wrapped_type), as it's more verbose and won't be really more efficient.
It guarantees no-copy behavior, regardless of the mutability of the underlying data structure.
Thus, it is the most efficient way to create a non-empty pyochain wrapper from an existing corresponding data structure.
Warning
No-copy behavior means that mutable collections will be shared between wrapper <-> wrapped.
Hence, modifying one will affect the other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wrapped
|
Iterable[W]
|
The object to wrap. |
required |
Returns:
| Type | Description |
|---|---|
Wrapper[W]
|
Wrapper[W]: A new instance wrapping the provided |
Example
from pyochain import Vec, Seq, SetMut, Dict
from pyochain.collections import StableSet, Deque
from collections import deque
original_list = [1, 2, 3]
vec = Vec.wrap(original_list)
assert vec == Vec(1, 2, 3)
vec[0] = 10
assert original_list == [10, 2, 3]
original_tuple = (1, 2, 3)
assert Seq.wrap(original_tuple) == Seq(1, 2, 3)
py_dict = {"Alice": 30, "Bob": 25, "Charlie": 35}
set_obj = StableSet.wrap(py_dict)
assert set_obj == StableSet("Alice", "Bob", "Charlie")
py_dict["David"] = 40
assert set_obj == StableSet("Alice", "Bob", "Charlie", "David")
original = deque([1, 2, 3])
deque_obj = Deque.wrap(original)
assert deque_obj == Deque(1, 2, 3)
original.append(4)
assert deque_obj == Deque(1, 2, 3, 4)
original_set = {1, 2, 3}
set_obj = SetMut.wrap(original_set)
assert set_obj == SetMut(1, 2, 3)
original_set.add(4)
assert set_obj == SetMut(1, 2, 3, 4)
original_dict = {"a": 1, "b": 2, "c": 3}
ref_dict = Dict.wrap(original_dict)
assert ref_dict == Dict(a=1, b=2, c=3)
assert ref_dict.insert("a", 100).unwrap() == 1
assert original_dict == {"a": 100, "b": 2, "c": 3}
Source code in pyochain/abc/constructors.pyi
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |