demopkg.report
Report classes and the functions that build them.
This module holds the bulk of the fixture surface: a base class, a subclass that inherits and overrides members, overloaded methods, a static method, a class method, a property with a setter, and the package’s exception types.
Classes
| Name | Description |
|---|---|
BaseReport | Common behaviour shared by every report. |
Report | A named collection of scored sections. |
ReportError | Raised when a report cannot be generated. |
ReportWarning | Warned when a report is generated with incomplete data. |
Functions
| Name | Description |
|---|---|
generate_report | Build a report from a source object. |
old_generate | Build a report the old way. |
BaseReportclass#
class BaseReportCommon behaviour shared by every report.
Attributes
| Name | Type | Description |
|---|---|---|
format | str | File extension used when a report is saved without one. |
formatattributeclass attributeinstance attribute#
format: str = 'txt'Default output format, as a bare file extension.
savemethod#
def save(path: pathlib.Path) -> NoneWrite the rendered report to path.
Parameters
| Name | Type | Description |
|---|---|---|
path | pathlib.Path | Destination file. Parent directories must exist. |
Raises
OSError- If the file cannot be written.
validatemethod#
def validate() -> boolCheck the report for structural problems.
Returns
bool- True when the report is well formed.
Reportclass#
class Report(name: str, scores: dict[str, float] | None = None)Bases: BaseReport
A named collection of scored sections.
Parameters
Attributes
nameattributeinstance attribute#
name = nameThe report name.
scoresattributeinstance attribute#
scores: dict[str, float] = scores or {}Mapping of metric name to score.
from_mappingmethodclassmethod#
def from_mapping(data: Mapping[str, float], *, name: str = 'report') -> ReportBuild a report from an existing mapping of scores.
Parameters
Returns
Report- A new report holding a copy of
data.
generatemethod#
def generate(*sections: str, title: str | None = None, timeout: float = DEFAULT_TIMEOUT, **options: Any) -> pathlib.PathRender the report and return the path it was written to.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
*sections | str | () | Section names to include, in order. When empty every known section is rendered. |
title | str | None | None | Overrides the report title. Defaults to the report name. |
timeout | float | DEFAULT_TIMEOUT | Seconds to wait before giving up. |
**options | Any | {} | Extra renderer options, passed through untouched. |
Returns
pathlib.Path- The path of the file that was written.
Raises
ReportError- If a requested section does not exist.
TimeoutError- If rendering takes longer than
timeout.
Examples
>>> Report("weekly").generate("summary", title="Weekly")PosixPath('weekly.txt')rendermethod#
def render(value: str | list[str]) -> str | list[str]Render one section or a list of sections.
supported_formatsmethodstaticmethod#
def supported_formats() -> tuple[str, ...]List the formats a report can be saved as.
Inherited from demopkg.report.BaseReport
formatattributeclass attributeinstance attribute#
demopkg.report.BaseReportView sourceformat: str = 'txt'Default output format, as a bare file extension.
is_validproperty#
demopkg.report.BaseReportView sourceis_valid: boolWhether validate passes.
savemethod#
demopkg.report.BaseReportView sourcedef save(path: pathlib.Path) -> NoneWrite the rendered report to path.
Parameters
| Name | Type | Description |
|---|---|---|
path | pathlib.Path | Destination file. Parent directories must exist. |
Raises
OSError- If the file cannot be written.
validatemethod#
demopkg.report.BaseReportView sourcedef validate() -> boolCheck the report for structural problems.
Returns
bool- True when the report is well formed.
ReportErrorclass#
class ReportError(Exception)Bases: Exception
Raised when a report cannot be generated.
ReportWarningclass#
class ReportWarning(UserWarning)Bases: UserWarning
Warned when a report is generated with incomplete data.
generate_reportfunction#
def generate_report(source, /, name: str, *, fmt: str = 'md') -> ReportBuild a report from a source object.
Exercises a positional-only parameter (source, before the /) and a
keyword-only one (fmt, after the *).
Returns a Report whose generate method writes a pathlib.Path. A reference nothing resolves, such as [nosuchpkg.Thing][], is left exactly as it was written.
Parameters
Returns
Report- A populated report.
Raises
ReportError- If
sourcecannot be read.
old_generatefunctionDeprecated#
def old_generate(name: str) -> ReportBuild a report the old way.
Parameters
| Name | Type | Description |
|---|---|---|
name | str | Name for the resulting report. |
Returns
Report- A populated report.