Collection of references to Table columns. Created by Table.slice method, or automatically by using left/right/this constructs. Supports basic column manipulation methods. Example: >>> import pathway as pw >>> t1 = pw.debug.table_from_markdown(''' ... age | owner | pet
| 14 | |
| 15 | |
| 16 | class TableSlice: |
| 17 | """Collection of references to Table columns. |
| 18 | Created by Table.slice method, or automatically by using left/right/this constructs. |
| 19 | Supports basic column manipulation methods. |
| 20 | |
| 21 | Example: |
| 22 | |
| 23 | >>> import pathway as pw |
| 24 | >>> t1 = pw.debug.table_from_markdown(''' |
| 25 | ... age | owner | pet |
| 26 | ... 10 | Alice | dog |
| 27 | ... 9 | Bob | dog |
| 28 | ... 8 | Alice | cat |
| 29 | ... 7 | Bob | dog |
| 30 | ... ''') |
| 31 | >>> t1.slice.without("age").with_suffix("_col") |
| 32 | TableSlice({'owner_col': <table1>.owner, 'pet_col': <table1>.pet}) |
| 33 | """ |
| 34 | |
| 35 | _mapping: dict[str, ColumnReference] |
| 36 | _table: Table |
| 37 | |
| 38 | def __init__(self, mapping, table): |
| 39 | self._mapping = mapping |
| 40 | self._table = table |
| 41 | |
| 42 | def __iter__(self): |
| 43 | return iter(self._mapping.values()) |
| 44 | |
| 45 | def __repr__(self): |
| 46 | return f"TableSlice({self._mapping})" |
| 47 | |
| 48 | def keys(self): |
| 49 | return self._mapping.keys() |
| 50 | |
| 51 | @overload |
| 52 | def __getitem__(self, args: str | ColumnReference) -> ColumnReference: ... |
| 53 | |
| 54 | @overload |
| 55 | def __getitem__(self, args: list[str | ColumnReference]) -> TableSlice: ... |
| 56 | |
| 57 | @trace_user_frame |
| 58 | def __getitem__( |
| 59 | self, arg: str | ColumnReference | list[str | ColumnReference] |
| 60 | ) -> ColumnReference | TableSlice: |
| 61 | if isinstance(arg, (ColumnReference, str)): |
| 62 | return self._mapping[self._normalize(arg)] |
| 63 | else: |
| 64 | return TableSlice({self._normalize(k): self[k] for k in arg}, self._table) |
| 65 | |
| 66 | @trace_user_frame |
| 67 | def __getattr__(self, name: str) -> ColumnReference: |
| 68 | from pathway.internals import Table |
| 69 | |
| 70 | if hasattr(Table, name) and name != "id": |
| 71 | raise ValueError( |
| 72 | f"{name!r} is a method name. It is discouraged to use it as a column" |
| 73 | + f" name. If you really want to use it, use [{name!r}]." |