Formatter¶
Convert device responses to different output formats (table, JSON, CSV, TSV, etc.). ResponseView provides two perspectives of the data (nested and flattened), and ResponseFormatter outputs them in your chosen format.
Architecture¶
Class Diagram¶
classDiagram
class DeviceResponse {
+type: str
+status: str
+received_us: int
+...dynamic_fields: Any
}
class ResponseView {
-raw: DeviceResponse
-flat: DeviceResponse
+__init__(response: DeviceResponse)
}
class ResponseFormatter {
-supported_formats: list
+display(response: DeviceResponse, format: str)
-_format_table(response: DeviceResponse) str
-_format_json(response: DeviceResponse) str
-_format_jsonl(response: DeviceResponse) str
-_format_csv(response: DeviceResponse) str
-_format_ssv(response: DeviceResponse) str
-_format_tsv(response: DeviceResponse) str
-_format_ltsv(response: DeviceResponse) str
}
ResponseView --> DeviceResponse: uses (raw)
ResponseView --> DeviceResponse: uses (flat)
ResponseFormatter --> DeviceResponse: formats
Response Perspective Transformation¶
flowchart LR
A["DeviceResponse<br/>(nested structure)"] --> B["ResponseView"]
B --> C["raw perspective<br/>(original nested)"]
B --> D["flat perspective<br/>(underscore-separated keys)"]
C --> E["ResponseFormatter"]
D --> E
E --> F["Output Format<br/>table/json/csv/etc"]
style A fill:#e1f5ff
style C fill:#fff3e0
style D fill:#f3e5f5
style F fill:#e8f5e9
Supported Output Formats¶
graph TD
A["ResponseFormatter"] --> B["table"]
A --> C["json"]
A --> D["jsonl"]
A --> E["csv"]
A --> F["ssv<br/>space-separated"]
A --> G["tsv<br/>tab-separated"]
A --> H["ltsv<br/>labeled tsv"]
style B fill:#fff3e0
style C fill:#f3e5f5
style D fill:#e3f2fd
style E fill:#e8f5e9
style F fill:#fce4ec
style G fill:#f1f8e9
style H fill:#ede7f6
ResponseView¶
kazunoko.formatter.ResponseView
dataclass
¶
Response view wrapper providing both raw and flattened perspectives.
Provides convenient access to raw (nested) and flat (flattened) representations of a DeviceResponse for developers. Automatically flattens nested structures in post_init.
Attributes:
| Name | Type | Description |
|---|---|---|
raw |
DeviceResponse
|
Original DeviceResponse with nested structure |
flat |
DeviceResponse
|
Flattened version of the response as DeviceResponse |
Example
from kazunoko import Command, connect
with connect() as device:
cmd = Command(device)
dev_resp = cmd.status()
# Wrap in ResponseView to get both perspectives
rv = ResponseView(raw=dev_resp)
# Access raw nested structure
raw_data = rv.raw.model_dump()
# Access flattened structure (type-safe)
flat_data = rv.flat
# Use with ResponseFormatter
fmt = ResponseFormatter(rv.flat)
fmt.display("table")
Source code in src/kazunoko/formatter.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
__post_init__()
¶
Initialize flat representation from raw response.
Source code in src/kazunoko/formatter.py
58 59 60 61 62 | |
ResponseFormatter¶
kazunoko.formatter.ResponseFormatter
¶
Format and render device responses in multiple output formats
Provides methods to convert DeviceResponse objects into various output formats (Rich table, JSONL, CSV) for display or export.
The formatter accepts DeviceResponse objects. For dual perspective support (raw/flat), use ResponseView to access both perspectives, then format the desired one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
DeviceResponse
|
DeviceResponse object to format |
required |
console
|
Console | None
|
Optional Rich Console instance for output (default: new Console) |
None
|
Example
from kazunoko import Command, ResponseFormatter, ResponseView, connect
with connect() as device:
cmd = Command(device)
dev_resp = cmd.status()
# Wrap response to get both perspectives
rv = ResponseView(raw=dev_resp)
# Format flattened view (type-safe)
fmt = ResponseFormatter(rv.flat)
fmt.display() # prints as table
# Format raw view (type-safe)
fmt = ResponseFormatter(rv.raw)
fmt.display()
# Format as JSONL
jsonl_str = fmt.to_jsonl()
print(jsonl_str)
# Explicit format selection
fmt.display(format="csv")
Source code in src/kazunoko/formatter.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | |
__init__(response, console=None)
¶
Initialize ResponseFormatter
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
DeviceResponse
|
DeviceResponse object to format |
required |
console
|
Console | None
|
Optional Rich Console instance for output |
None
|
Source code in src/kazunoko/formatter.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
display(format='jsonl')
¶
Display response in specified format
Convenience method for displaying formatted output to console.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format
|
Literal['table', 'jsonl', 'json', 'csv', 'ssv', 'tsv', 'ltsv']
|
Output format ("table", "jsonl", "json", "csv", "ssv", "tsv", or "ltsv") - "jsonl": Single-line JSON (default) - "table": Rich formatted table - "json": Pretty-printed JSON with syntax highlighting - "csv": CSV values only - "ssv": Space-separated values - "tsv": Tab-separated values - "ltsv": Labeled tab-separated values (key=value pairs) |
'jsonl'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If format is not recognized |
Example
fmt = ResponseFormatter(response)
fmt.display() # JSONL (default)
fmt.display("table") # Rich formatted table
fmt.display("json") # pretty-printed JSON
fmt.display("csv") # CSV format
fmt.display("ssv") # space-separated values
fmt.display("tsv") # tab-separated values
fmt.display("ltsv") # labeled tab-separated values
Source code in src/kazunoko/formatter.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | |
to_csv()
¶
Convert response to CSV format (values only, no header)
Returns CSV-formatted values separated by commas.
For consistency with the read command, outputs only values
without column headers. Use to_csv_with_header() if headers
are needed.
Returns:
| Type | Description |
|---|---|
str
|
CSV string with values only (one line) |
Example
fmt = ResponseFormatter(response)
csv_str = fmt.to_csv()
print(csv_str)
Source code in src/kazunoko/formatter.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
to_json_pretty()
¶
Convert response to pretty-printed JSON format
Returns indented JSON string suitable for human reading.
Returns:
| Type | Description |
|---|---|
str
|
Pretty-printed JSON string |
Example
fmt = ResponseFormatter(resp.flat)
json_str = fmt.to_json_pretty()
print(json_str)
Source code in src/kazunoko/formatter.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
to_jsonl()
¶
Convert response to JSONL format (single-line JSON)
Returns a single JSON object as a string (not pretty-printed).
Returns:
| Type | Description |
|---|---|
str
|
JSONL string (single JSON object) |
Example
fmt = ResponseFormatter(resp.flat)
jsonl_str = fmt.to_jsonl()
print(jsonl_str)
Source code in src/kazunoko/formatter.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
to_ltsv()
¶
Convert response to LTSV format (labeled tab-separated values)
Returns LTSV-formatted output with key=value pairs separated by tabs. LTSV is useful for structured logging and easy parsing.
Returns:
| Type | Description |
|---|---|
str
|
LTSV string with key=value pairs (one line) |
Example
fmt = ResponseFormatter(resp.flat)
ltsv_str = fmt.to_ltsv()
print(ltsv_str)
Source code in src/kazunoko/formatter.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
to_ssv()
¶
Convert response to SSV format (space-separated values)
Returns SSV-formatted values separated by spaces. For consistency with CSV, outputs only values without column headers.
Returns:
| Type | Description |
|---|---|
str
|
SSV string with values only (one line) |
Example
fmt = ResponseFormatter(response)
ssv_str = fmt.to_ssv()
print(ssv_str)
Source code in src/kazunoko/formatter.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
to_table()
¶
Convert response to Rich Table format
Creates a formatted table with field names and values.
Returns:
| Type | Description |
|---|---|
Table
|
Rich Table object ready for display |
Example
fmt = ResponseFormatter(resp.flat)
table = fmt.to_table()
console.print(table)
Source code in src/kazunoko/formatter.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
to_tsv()
¶
Convert response to TSV format (tab-separated values)
Returns TSV-formatted values separated by tabs. For consistency with CSV, outputs only values without column headers.
Returns:
| Type | Description |
|---|---|
str
|
TSV string with values only (one line) |
Example
fmt = ResponseFormatter(response)
tsv_str = fmt.to_tsv()
print(tsv_str)
Source code in src/kazunoko/formatter.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |