Repr Formatting API¶
Scikit-learn style repr and HTML formatting.
ReprMixin¶
pyreprint.formatting.repr.ReprMixin
¶
Mixin providing scikit-learn style repr and repr_html.
This mixin automatically generates a formatted repr based on the class's init parameters. It also provides repr_html for rich display in Jupyter notebooks.
To use, simply inherit from this mixin:
Example
class MyEstimator(ReprMixin): ... def init(self, alpha=1.0, max_iter=100): ... self.alpha = alpha ... self.max_iter = max_iter est = MyEstimator(alpha=0.5) print(est) MyEstimator(alpha=0.5, max_iter=100)
Attributes:
| Name | Type | Description |
|---|---|---|
_repr_max_line_length |
int
|
Maximum line length for repr. |
_repr_compact |
bool
|
Whether to use compact format when possible. |
_repr_html_style |
str
|
CSS style for HTML repr. |
Functions¶
__repr__()
¶
get_params(deep=True)
¶
Get parameters for this instance (sklearn-compatible).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
deep
|
bool
|
Whether to return nested object params. |
True
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary of parameter names to values. |
Source code in pyreprint/formatting/repr.py
set_params(**params)
¶
Set parameters for this instance (sklearn-compatible).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**params
|
Any
|
Parameters to set. |
{}
|
Returns:
| Type | Description |
|---|---|
'ReprMixin'
|
self |
Source code in pyreprint/formatting/repr.py
format_repr¶
pyreprint.formatting.repr.format_repr(obj, params=None, max_line_length=80, compact=True)
¶
Format an object's repr in scikit-learn style.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to format. |
required |
params
|
Optional[Dict[str, Any]]
|
Dictionary of parameter names to values. If None, attempts to extract from object. |
None
|
max_line_length
|
int
|
Maximum line length before wrapping. |
80
|
compact
|
bool
|
Whether to use compact single-line format when possible. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted repr string. |
Example
class MyClass: ... def init(self, a=1, b="hello"): ... self.a = a ... self.b = b obj = MyClass(a=42, b="world") print(format_repr(obj)) MyClass(a=42, b='world')
Source code in pyreprint/formatting/repr.py
ReprConfig¶
pyreprint.formatting.repr.ReprConfig(max_line_length=80, compact=True, show_types=False, max_value_length=40)
¶
Configuration for repr formatting.
This class allows customizing the default repr behavior.
Attributes:
| Name | Type | Description |
|---|---|---|
max_line_length |
Maximum line length before wrapping. |
|
compact |
Whether to use compact format when possible. |
|
show_types |
Whether to show type annotations. |
|
max_value_length |
Maximum length for value representations. |
Source code in pyreprint/formatting/repr.py
HTML Formatting¶
html_repr¶
pyreprint.formatting.html.html_repr(obj, style='sklearn', params=None, collapsible=False)
¶
Generate HTML representation of an object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to format. |
required |
style
|
str
|
Style theme ('sklearn', 'dark', 'minimal'). |
'sklearn'
|
params
|
Optional[Dict[str, Any]]
|
Dictionary of parameter names to values. If None, attempts to extract from object. |
None
|
collapsible
|
bool
|
Whether to make the repr collapsible. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
HTML string representation. |
Example
class MyClass: ... def init(self, a=1): ... self.a = a obj = MyClass() html = html_repr(obj)
Source code in pyreprint/formatting/html.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
HTMLFormatter¶
pyreprint.formatting.html.HTMLFormatter(style='sklearn', collapsible=False, max_value_length=40)
¶
Configurable HTML formatter for objects.
This class provides a reusable formatter with customizable settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
style
|
str
|
Style theme ('sklearn', 'dark', 'minimal'). |
'sklearn'
|
collapsible
|
bool
|
Whether to make repr collapsible by default. |
False
|
max_value_length
|
int
|
Maximum length for value representations. |
40
|
Example
formatter = HTMLFormatter(style='dark', collapsible=True) html = formatter.format(my_object)
Source code in pyreprint/formatting/html.py
Functions¶
__call__(obj, **kwargs)
¶
format(obj, params=None, collapsible=None)
¶
Format an object as HTML.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to format. |
required |
params
|
Optional[Dict[str, Any]]
|
Optional parameter dictionary override. |
None
|
collapsible
|
Optional[bool]
|
Override default collapsible setting. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
HTML string representation. |
Source code in pyreprint/formatting/html.py
display_html¶
pyreprint.formatting.html.display_html(obj, **kwargs)
¶
Display an object's HTML repr in Jupyter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to display. |
required |
**kwargs
|
Any
|
Arguments passed to html_repr. |
{}
|