Skip to content

Commit fbbec2d

Browse files
committed
Improved docstrings
1 parent 7d30d96 commit fbbec2d

7 files changed

Lines changed: 102 additions & 7 deletions

File tree

simvue/api/objects/events.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ def get(
108108
-------
109109
Generator[EventSet]
110110
111+
Raises
112+
------
113+
RuntimeError
114+
if the expected 'data' key was not found in server response
115+
111116
"""
112117
_class_instance = cls(_read_only=True, _local=True)
113118
_count: int = 0
@@ -191,6 +196,16 @@ def histogram(
191196
window: int,
192197
filters: list[str] | None,
193198
) -> list[dict[str, str | int]]:
199+
"""Retrieve events as a histogram.
200+
201+
Parameters
202+
----------
203+
timestamp_begin : datetime.datetime
204+
start point for event time range
205+
timestamp_end : datetime.datetime
206+
end point for event time range
207+
208+
"""
194209
if timestamp_end - timestamp_begin <= datetime.timedelta(seconds=window):
195210
raise ValueError(
196211
"Invalid arguments for datetime range, "

simvue/api/objects/folder.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,23 @@ def _set_favourite(self, *, starred: bool) -> dict:
326326
def get_folder_from_path(
327327
path: typing.Annotated[str, pydantic.Field(pattern=FOLDER_REGEX)],
328328
) -> Folder:
329+
"""Retrieve a folder from its path.
330+
331+
Parameters
332+
----------
333+
path : str
334+
folder path on server
335+
336+
Returns
337+
-------
338+
Folder
339+
object representing folder on the server
340+
341+
Raises
342+
------
343+
ObjectNotFoundError
344+
if no match was found for this path
345+
"""
329346
_folders = Folder.get(filters=json.dumps([f"path == {path}"]), count=1)
330347

331348
if not (_first_entry := next(_folders, None)):

simvue/api/objects/grids.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,34 @@
3939

4040

4141
def check_ordered_array(
42-
axis_ticks: list[list[float]] | np.ndarray,
42+
array: list[list[float]] | np.ndarray,
4343
) -> list[list[float]]:
44-
"""Returns if array is ordered or reverse ordered."""
45-
if isinstance(axis_ticks, np.ndarray):
46-
axis_ticks = axis_ticks.tolist()
47-
for i, array in enumerate(axis_ticks):
44+
"""Returns if a 2D array is ordered or reverse ordered.
45+
46+
Parameters
47+
----------
48+
array : list[list[float]] | np.ndarray
49+
array to check for ordering
50+
51+
Returns
52+
-------
53+
list[list[float]]
54+
original array as list
55+
56+
Raises
57+
------
58+
ValueError
59+
if the values are unordered
60+
"""
61+
if isinstance(array, np.ndarray):
62+
array = array.tolist()
63+
for i, array in enumerate(array):
4864
_array = np.array(array)
4965
if not np.all(np.sort(_array) == _array) or np.all(
5066
reversed(np.sort(_array)) == _array,
5167
):
5268
raise ValueError(f"Axis {i} has unordered values.")
53-
return axis_ticks
69+
return array
5470

5571

5672
class Grid(SimvueObject):
@@ -127,6 +143,11 @@ def on_reconnect(self, id_mapping: dict[str, str]) -> None:
127143
id_mapping : dict[str, str]
128144
mapping from offline identifier to new online identifier.
129145
146+
Raises
147+
------
148+
RuntimeError
149+
If metrics could not be attached to uploaded runs on reconnect.
150+
130151
"""
131152
_online_runs = (
132153
(id_mapping[run_id], metric_name)

simvue/api/objects/run.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,11 @@ def abort(self, reason: str) -> dict[str, typing.Any]:
836836
dict[str, Any]
837837
server response after updating abort status.
838838
839+
Raises
840+
------
841+
RuntimeError
842+
if no server URL has been specified or found
843+
839844
"""
840845
if not self._abort_url:
841846
raise RuntimeError("Cannot abort run, no endpoint defined")

simvue/api/objects/tag.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626

2727
class TagSort(Sort):
28+
"""Class defining the sorting of tag data when retrieved from the server."""
29+
2830
@pydantic.field_validator("column")
2931
@classmethod
3032
def check_column(cls, column: str) -> str:

simvue/models.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,25 @@
2626

2727

2828
def validate_timestamp(timestamp: str, *, raise_except: bool = True) -> bool:
29-
"""Validate a user-provided timestamp."""
29+
"""Validate a user-provided timestamp.
30+
31+
Parameters
32+
-----------
33+
timestamp : str
34+
timestamp to check
35+
raise_except : bool, optional
36+
whether to raise an exception on failure, default True
37+
38+
Returns
39+
-------
40+
bool
41+
if check passed successfully
42+
43+
Raises
44+
------
45+
ValueError
46+
if exception throwing is enabled and the check fails
47+
"""
3048
try:
3149
_ = datetime.datetime.strptime(timestamp, DATETIME_FORMAT).astimezone()
3250
except ValueError:
@@ -73,6 +91,8 @@ def simvue_timestamp(
7391

7492
# Pydantic class to validate run.init()
7593
class RunInput(pydantic.BaseModel):
94+
"""Validate inputs for user run."""
95+
7696
model_config = pydantic.ConfigDict(extra="forbid")
7797
name: str | None = pydantic.Field(None, pattern=NAME_REGEX)
7898
metadata: dict[MetadataKeyString, str | int | float | None] | None = None
@@ -84,6 +104,8 @@ class RunInput(pydantic.BaseModel):
84104

85105

86106
class MetricSet(pydantic.BaseModel):
107+
"""Model for a set of metrics retrieved from the server."""
108+
87109
model_config = pydantic.ConfigDict(extra="forbid")
88110
time: float | int
89111
timestamp: typing.Annotated[str | None, pydantic.BeforeValidator(simvue_timestamp)]
@@ -92,6 +114,8 @@ class MetricSet(pydantic.BaseModel):
92114

93115

94116
class GridMetricSet(pydantic.BaseModel):
117+
"""Model for a set of grid metrics retrieved from the server."""
118+
95119
model_config = pydantic.ConfigDict(
96120
arbitrary_types_allowed=True,
97121
extra="forbid",
@@ -110,12 +134,15 @@ def serialize_array(
110134
value: np.ndarray | list[float] | list[list[float]],
111135
*_,
112136
) -> list[float] | list[list[float]]:
137+
"""Serialize a numpy array."""
113138
if isinstance(value, list):
114139
return value
115140
return value.tolist()
116141

117142

118143
class EventSet(pydantic.BaseModel):
144+
"""Model for a set of events retrieved from the server."""
145+
119146
model_config = pydantic.ConfigDict(extra="forbid")
120147
message: str
121148
log_level: (

simvue/run.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@
9191
def check_run_initialised(
9292
function: typing.Callable[..., typing.Any],
9393
) -> typing.Callable[..., typing.Any]:
94+
"""Decorator to ensure that the run object has been initialised.
95+
96+
A lot of functionality within the Run class only works if the object has first
97+
been initialised by the user, this decorator catches the invalid case as soon
98+
as possible.
99+
100+
"""
101+
94102
@functools.wraps(function)
95103
def _wrapper(self: Self, *args: typing.Any, **kwargs: typing.Any) -> typing.Any:
96104
# Tidy pydantic errors

0 commit comments

Comments
 (0)