MCPcopy Create free account
hub / github.com/PyTables/PyTables / __setitem__

Method __setitem__

tables/table.py:3415–3465  ·  view source on GitHub ↗

Set a row or a range of rows in a table or nested column. If key argument is an integer, the corresponding row is set to value. If key is a slice, the range of rows determined by it is set to value. Examples -------- :: table.cols[4] = r

(self, key: int | slice, value: Any)

Source from the content-addressed store, hash-verified

3413 raise TypeError(f"invalid index or slice: {key!r}")
3414
3415 def __setitem__(self, key: int | slice, value: Any) -> None:
3416 """Set a row or a range of rows in a table or nested column.
3417
3418 If key argument is an integer, the corresponding row is set to
3419 value. If key is a slice, the range of rows determined by it is set to
3420 value.
3421
3422 Examples
3423 --------
3424 ::
3425
3426 table.cols[4] = record
3427 table.cols.Info[4:1000:2] = recarray
3428
3429 Those statements are equivalent to::
3430
3431 table.modify_rows(4, rows=record)
3432 table.modify_column(4, 1000, 2, colname='Info', column=recarray)
3433
3434 Here you can see how a mix of natural naming, indexing and slicing
3435 can be used as shorthands for the :meth:`Table.modify_rows` and
3436 :meth:`Table.modify_column` methods.
3437
3438 """
3439 table = self._v_table
3440 nrows = table.nrows
3441 if is_idx(key):
3442 key = operator.index(key)
3443
3444 # Index out of range protection
3445 if key >= nrows:
3446 raise IndexError("Index out of range")
3447 if key < 0:
3448 # To support negative values
3449 key += nrows
3450 start, stop, step = table._process_range(key, key + 1, 1)
3451 elif isinstance(key, slice):
3452 start, stop, step = table._process_range(
3453 key.start, key.stop, key.step
3454 )
3455 else:
3456 raise TypeError(f"invalid index or slice: {key!r}")
3457
3458 # Actually modify the correct columns
3459 colgroup = self._v_desc._v_pathname
3460 if colgroup == "": # The root group
3461 table.modify_rows(start, stop, step, rows=value)
3462 else:
3463 table.modify_column(
3464 start, stop, step, colname=colgroup, column=value
3465 )
3466
3467 def _g_close(self) -> None:
3468 # First, close the columns (ie possible indices open)

Callers

nothing calls this directly

Calls 5

is_idxFunction · 0.85
indexMethod · 0.80
modify_rowsMethod · 0.80
modify_columnMethod · 0.80
_process_rangeMethod · 0.45

Tested by

no test coverage detected