Grid¶
dummypy.grid
¶
The :class:Grid value type for the dummypy analytics library.
Responsibility split:
- :func:
_build_gridsowns the data-generation concern — turning a sizeninto the two coordinate DataFrames. It is a pure function ofn. - :class:
Gridowns the model concern — validatingn, holding the generatedx/yframes, and exposing behaviour (:meth:Grid.diff).
Keeping generation in a standalone function keeps Grid a thin, testable
value type rather than a class that both stores and manufactures its data.
Grid
¶
A grid representing data points for analytics calculations.
Holds two coordinate DataFrames, x and y (with x == y.T),
generated from the grid size n by :func:_build_grids.
Instances are immutable. x and y are derived from n, so
letting any of the three be reassigned would break the invariants the
validator and :func:_build_grids establish at construction: a new x
need not be y.T, and a new n would not rebuild the frames it is
supposed to describe. Build a new :class:Grid instead.
The frames are protected from in-place edits too. x and y hand out
a fresh, lazily-copied view of read-only data on every access, so a write
such as grid.x.iloc[0, 1] = 99 can never corrupt the grid: pandas 2
rejects it with ValueError, and pandas 3's copy-on-write confines it to
the copy that was handed out. Equality and hashing use n alone, which
is sound because both frames are derived from it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
Maximum size for the grid (default: 10). Must be a non-negative integer. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Examples:
Both frames are square with side n + 1, and x is the transpose
of y:
Only n is part of the repr, equality and hash, since x and
y are derived:
>>> grid
Grid(n=3)
>>> Grid(n=3) == grid, Grid(n=4) == grid
(True, False)
>>> len({Grid(n=3), Grid(n=3)})
1
A negative size is rejected, and so is a float or a bool:
>>> Grid(n=-1)
Traceback (most recent call last):
...
ValueError: Grid size n must be non-negative, got -1
>>> Grid(n=2.0)
Traceback (most recent call last):
...
TypeError: Grid size n must be an integer, got float
Instances are immutable — build a new grid rather than reassigning:
Source code in src/dummypy/grid.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
x
property
¶
The x-coordinate frame, y transposed.
Returns:
| Type | Description |
|---|---|
DataFrame
|
A shallow copy of the grid's read-only frame. The copy is cheap |
DataFrame
|
(no data is duplicated), and writing to it cannot affect the grid. |
y
property
¶
The y-coordinate frame, each row equal to 0..n.
Returns:
| Type | Description |
|---|---|
DataFrame
|
A shallow copy of the grid's read-only frame. The copy is cheap |
DataFrame
|
(no data is duplicated), and writing to it cannot affect the grid. |
__attrs_post_init__()
¶
Populate the x and y coordinate frames from n.
Uses :func:object.__setattr__ because the class is frozen — the
standard attrs idiom for a derived attribute on an immutable class.
Keeping the single :func:_build_grids call here preserves the
generation-vs-model split described in the module docstring.
Source code in src/dummypy/grid.py
diff()
¶
Returns a grid of differences.
Returns:
| Type | Description |
|---|---|
DataFrame
|
A fresh DataFrame of element-wise differences (x - y), computed |
DataFrame
|
anew on each call. |
Examples:
The value at (i, j) is i - j, so the frame is antisymmetric
and its diagonal is zero:
>>> grid = Grid(n=3)
>>> grid.diff().loc["2", "1"]
np.int64(1)
>>> grid.diff().loc["1", "2"]
np.int64(-1)
Each call returns a fresh frame, so mutating one cannot corrupt the grid it came from: