Skip to content

Commit

Permalink
docs: improve docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Apr 19, 2023
1 parent 3ebc1c6 commit 75c59e8
Showing 1 changed file with 76 additions and 10 deletions.
86 changes: 76 additions & 10 deletions src/safeds/data/tabular/containers/_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@


class Row:
"""A row is a collection of values, where each value is associated with a column name."""
"""
A row is a collection of values, where each value is associated with a column name.
To create a row manually, use the static method [from_dict][safeds.data.tabular.containers._row.Row.from_dict].
"""

# ------------------------------------------------------------------------------------------------------------------
# Creation
Expand Down Expand Up @@ -40,6 +44,20 @@ def from_dict(data: dict[str, Any]) -> Row:
# ------------------------------------------------------------------------------------------------------------------

def __init__(self, data: pl.DataFrame, schema: Schema | None = None):
"""
Initialize a row from a `polars.DataFrame`.
**Do not use this method directly.** It is not part of the public interface and may change in the future
without a major version bump. Use the static method
[from_dict][safeds.data.tabular.containers._row.Row.from_dict] instead.
Parameters
----------
data : polars.DataFrame
The data.
schema : Schema | None
The schema. If None, the schema is inferred from the data.
"""
self._data: pl.DataFrame = data

self._schema: Schema
Expand All @@ -57,12 +75,47 @@ def __eq__(self, other: Any) -> bool:
return self._schema == other._schema and self._data.frame_equal(other._data)

def __getitem__(self, column_name: str) -> Any:
"""
Return the value of a specified column.
Parameters
----------
column_name : str
The column name.
Returns
-------
value : Any
The value of the column.
Raises
------
UnknownColumnNameError
If the row does not contain the specified column.
Examples
--------
>>> from safeds.data.tabular.containers import Row
>>> row = Row.from_dict({"a": 1, "b": 2})
>>> row["a"]
1
"""

return self.get_value(column_name)

def __iter__(self) -> Iterator[Any]:
return iter(self.get_column_names())

def __len__(self) -> int:
"""
Return the number of columns in this row.
Returns
-------
count : int
The number of columns.
"""

return self._data.shape[1]

def __repr__(self) -> str:
Expand Down Expand Up @@ -110,13 +163,20 @@ def get_value(self, column_name: str) -> Any:
Returns
-------
value :
value : Any
The value of the column.
Raises
------
UnknownColumnNameError
If the row does not contain the specified column.
Examples
--------
>>> from safeds.data.tabular.containers import Row
>>> row = Row.from_dict({"a": 1, "b": 2})
>>> row.get_value("a")
1
"""
if not self.has_column(column_name):
raise UnknownColumnNameError([column_name])
Expand All @@ -127,25 +187,31 @@ def has_column(self, column_name: str) -> bool:
"""
Return whether the row contains a given column.
Alias for self.schema.hasColumn(column_name: str) -> bool.
Parameters
----------
column_name : str
The name of the column.
Returns
-------
contains : bool
has_column : bool
True, if row contains the column.
Examples
--------
>>> from safeds.data.tabular.containers import Row
>>> row = Row.from_dict({"a": 1, "b": 2})
>>> row.has_column("a")
True
>>> row.has_column("c")
False
"""
return self._schema.has_column(column_name)

def get_column_names(self) -> list[str]:
"""
Return a list of all column names saved in this schema.
Alias for self.schema.get_column_names() -> list[str].
Return a list of all column names in the row.
Returns
-------
Expand All @@ -170,8 +236,8 @@ def get_type_of_column(self, column_name: str) -> ColumnType:
Raises
------
ColumnNameError
If the specified target column name does not exist.
UnknownColumnNameError
If the row does not contain the specified column.
"""
return self._schema.get_type_of_column(column_name)

Expand Down

0 comments on commit 75c59e8

Please sign in to comment.