pygx.formatting¶
String formatting primitives shared across PyGX.
formatting
¶
String formatting primitives shared across PyGX.
pygx.formatting houses the Formattable mixin used by every symbolic value
to produce its __str__ / __repr__ output, alongside helpers for bracket
glyphs, key-value rendering, markdown-quoting, pluralization, and ANSI-coloring
of terminal output. It sits below pygx.utils and pygx.symbolic in the
dependency stack so those layers can rely on it without introducing cycles.
BracketType
¶
Bases: IntEnum
Bracket types used for complex type formatting.
Formattable
¶
Interface for classes whose instances can be pretty-formatted.
This interface overrides the default __repr__ and __str__ method, thus
all Formattable objects can be printed nicely.
All symbolic types implement this interface.
format
abstractmethod
¶
Formats this object into a string representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compact
|
bool
|
If True, this object will be formatted into a single line. |
False
|
verbose
|
bool
|
If True, this object will be formatted with verbosity.
Subclasses should define |
True
|
root_indent
|
int
|
The start indent level for this object if the output is a multi-line string. |
0
|
**kwargs
|
Any
|
Subclass specific keyword arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
A string of formatted object. |
Source code in pygx/formatting/_formatting.py
RawText
¶
auto_plural
¶
Use singular form if number is 1, otherwise use plural form.
bracket_chars
¶
bracket_chars(bracket_type: BracketType) -> tuple[str, str]
camel_to_snake
¶
Returns the snake case version of a camel case string.
Source code in pygx/formatting/_formatting.py
comma_delimited_str
¶
format
¶
format(
value: Any,
compact: bool = False,
verbose: bool = True,
root_indent: int = 0,
list_wrap_threshold: int = 80,
strip_object_id: bool = False,
include_keys: set[str] | None = None,
exclude_keys: set[str] | None = None,
markdown: bool = False,
max_str_len: int | None = None,
max_bytes_len: int | None = None,
*,
custom_format: CustomFormatFn | None = None,
memo: set[int] | None = None,
**kwargs: Any
) -> str
Formats a (maybe) hierarchical value with flags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to format. |
required |
compact
|
bool
|
If True, this object will be formatted into a single line. |
False
|
verbose
|
bool
|
If True, this object will be formatted with verbosity. Subclasses
should define |
True
|
root_indent
|
int
|
The start indent level for this object if the output is a multi-line string. |
0
|
list_wrap_threshold
|
int
|
A threshold in number of characters for wrapping a list value in a single line. |
80
|
strip_object_id
|
bool
|
If True, format object as ' |
False
|
include_keys
|
set[str] | None
|
A set of keys to include from the top-level dict or object. |
None
|
exclude_keys
|
set[str] | None
|
A set of keys to exclude from the top-level dict or object.
Applicable only when |
None
|
markdown
|
bool
|
If True, use markdown notion to quote the formatted object. |
False
|
max_str_len
|
int | None
|
The max length of the string to be formatted. If the string is longer than this length, it will be truncated. |
None
|
max_bytes_len
|
int | None
|
The max length of the bytes to be formatted. If the bytes is longer than this length, it will be truncated. |
None
|
custom_format
|
CustomFormatFn | None
|
An optional custom format function, which will be applied to
each value (and child values) in kvlist. If the function returns None, it
will fall back to the default |
None
|
memo
|
set[int] | None
|
A set of object ids that have been formatted. Used to avoid infinite recursion in the formatting process. |
None
|
**kwargs
|
Any
|
Keyword arguments that will be passed through unto child
|
{}
|
Returns:
| Type | Description |
|---|---|
str
|
A string representation for |
Source code in pygx/formatting/_formatting.py
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 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 | |
kvlist_str
¶
kvlist_str(
kvlist: list[tuple[str, Any, Any]],
compact: bool = True,
verbose: bool = False,
root_indent: int = 0,
*,
label: str | None = None,
bracket_type: BracketType = ROUND,
custom_format: CustomFormatFn | None = None,
memo: set[int] | None = None,
**kwargs: Any
) -> str
Formats a list key/value pairs into a comma delimited string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kvlist
|
list[tuple[str, Any, Any]]
|
List of tuples in format of (key, value, default_value or a tuple of default values) |
required |
compact
|
bool
|
If True, format value in kvlist in compact form. |
True
|
verbose
|
bool
|
If True, format value in kvlist in verbose. |
False
|
root_indent
|
int
|
The indent should be applied for values in kvlist if they are multi-line. |
0
|
label
|
str | None
|
(Optional) If not None, add label to brace all kv pairs. |
None
|
bracket_type
|
BracketType
|
Bracket type used for embracing the kv pairs. Applicable only
when |
ROUND
|
custom_format
|
CustomFormatFn | None
|
An optional custom format function, which will be applied to
each value (and child values) in kvlist. If the function returns None, it
will fall back to the default |
None
|
memo
|
set[int] | None
|
A set of object ids that have been formatted. Used to avoid infinite recursion in the formatting process. |
None
|
**kwargs
|
Any
|
Keyword arguments that will be passed through unto child
|
{}
|
Returns: A formatted string from a list of key/value pairs delimited by comma.
Source code in pygx/formatting/_formatting.py
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 | |
maybe_markdown_quote
¶
Maybe quote the formatted string with markdown.
printv
¶
quote_if_str
¶
repr_format
¶
Context manager for setting the default format kwargs for repr.
str_format
¶
Context manager for setting the default format kwargs for str.
colored
¶
colored(
text: str,
color: str | None = None,
background: str | None = None,
styles: list[str] | None = None,
) -> str
Returns the colored text with ANSI color characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
A string that may or may not already has ANSI color characters. |
required |
color
|
str | None
|
A string for text colors. Applicable values are: 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'. |
None
|
background
|
str | None
|
A string for background colors. Applicable values are: 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'. |
None
|
styles
|
list[str] | None
|
A list of strings for applying styles on the text. Applicable values are: 'bold', 'dark', 'underline', 'blink', 'reverse', 'concealed'. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A string with ANSI color characters embracing the entire text. |
Source code in pygx/formatting/_text_color.py
colored_block
¶
colored_block(
text: str,
block_start: str,
block_end: str,
color: str | None = None,
background: str | None = None,
styles: list[str] | None = None,
) -> str
Apply colors to text blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
A string that may or may not already has ANSI color characters. |
required |
block_start
|
str
|
A string that signals the start of a block. E.g. '{{' |
required |
block_end
|
str
|
A string that signals the end of a block. E.g. '}}'. |
required |
color
|
str | None
|
A string for text colors. Applicable values are: 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'. |
None
|
background
|
str | None
|
A string for background colors. Applicable values are: 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'. |
None
|
styles
|
list[str] | None
|
A list of strings for applying styles on the text. Applicable values are: 'bold', 'dark', 'underline', 'blink', 'reverse', 'concealed'. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A string with ANSI color characters embracing the matched text blocks. |
Source code in pygx/formatting/_text_color.py
options: show_root_heading: false show_root_toc_entry: false members_order: source heading_level: 2