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.typing 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__ methods,
routing both through pg.format so all Formattable objects print
nicely and honor the pg.str_format / pg.repr_format scopes.
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. What verbose output includes is defined by each subclass. |
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
¶
Bases: Formattable
A string wrapper that formats verbatim, ignoring all format flags.
Source code in pygx/formatting/_formatting.py
auto_plural
¶
Returns the singular form if number is 1, otherwise the plural form.
Source code in pygx/formatting/_formatting.py
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.
Primitives are rendered via repr; lists, tuples and dicts are rendered
recursively; Formattable values (all symbolic types) delegate to their
own format method, which receives the flags below plus any extra
keyword arguments.
Example:
class A(pg.Object):
x: int = 1
y: str = 'foo'
a = A(x=2)
# Multi-line (default).
assert pg.format(a) == "A(\n x = 2,\n y = 'foo'\n)"
# Single line.
assert pg.format(a, compact=True) == "A(x=2, y='foo')"
# Multi-line, Python-evaluable form (a flag understood by symbolic
# types): no spaces around '='.
assert pg.format(a, python_format=True) == "A(\n x=2,\n y='foo'\n)"
# Omit fields that equal their default value.
assert pg.format(a, compact=True, exclude_defaults=True) == 'A(x=2)'
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. What
extra information verbose output includes is defined per
|
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, quote the formatted value as markdown inline code or a code block. Applied only at the outermost level. |
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 |
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
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 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 | |
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 of key/value pairs into a comma-delimited string.
Pairs whose value equals their default value are omitted. This is the
workhorse behind most Formattable.format implementations.
Example:
assert pg.formatting.kvlist_str(
[
('x', 1, None), # Included: 1 != None.
('y', 'foo', 'foo'), # Omitted: value equals the default.
],
label='Point',
) == "Point(x=1)"
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). A pair is omitted when its value equals the default (or any of the defaults). |
required |
compact
|
bool
|
If True, format each value in kvlist in compact form. |
True
|
verbose
|
bool
|
If True, format each value in kvlist in verbose form. |
False
|
root_indent
|
int
|
The indent to apply to 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:
| Type | Description |
|---|---|
str
|
A formatted string from a list of key/value pairs delimited by comma. |
Source code in pygx/formatting/_formatting.py
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 | |
maybe_markdown_quote
¶
Quotes s as markdown when markdown is True.
Single-line strings become inline code; multi-line strings become a fenced code block.
Source code in pygx/formatting/_formatting.py
printv
¶
Prints values formatted with pg.format (exposed as pg.print).
Each positional argument is formatted with pg.format using the given
keyword arguments; file (default sys.stdout) is passed through to
the builtin print.
Source code in pygx/formatting/_formatting.py
quote_if_str
¶
repr_format
¶
Context manager overriding the format kwargs used by __repr__.
The __repr__ counterpart of pg.str_format.
Source code in pygx/formatting/_formatting.py
str_format
¶
Context manager overriding the format kwargs used by __str__.
Within the scope, str() on any Formattable object applies the
given pg.format kwargs on top of the object's own defaults.
Example:
Source code in pygx/formatting/_formatting.py
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.
Coloring requires the optional termcolor package; when it is not
installed, text is returned unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
A string that may or may not already have 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
Applies colors to the text blocks delimited by markers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
A string that may or may not already have 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