Skip to content

Files

Latest commit

 

History

History
90 lines (60 loc) · 4.07 KB

usage.md

File metadata and controls

90 lines (60 loc) · 4.07 KB

PyPformat - Usage


Related pages:



Basic usage

The core element of the PyPformat package is the PrettyFormatter class which can be used to format/serialize data to a string representation.

import pformat as pf

formatter = pf.PrettyFormater()
data = ...
formatted_str = formatter(data) # or formatter.format(data)
print(formatted_str)

Optionally, you can set the depth: int parameter for the call of formatter (or formatter.format), which will set the nesting level of the input data.



Format options

The PrettyFormatter class can be customized using formatting options, which alter the behaviour of the PrettyFormatter.

Note

The options are stored in a FormatOptions dataclass, which is defined in the format_options.py file.

This can be done in two ways:

  • Using the FormatOptions class directly:

    fmt_opts = pf.FormatOptions(<options>)
    formatter = pf.PrettyFormatter(fmt_opts)
  • Using the new method:

    formatter = pf.PrettyFormatter.new(<options>)

Both of these allow for specifying the same options, which are described in the Options overview section.


Options overview

The table below contains a brief overview of all available formatting options.

Name Type Default value Description
compact bool False If True, the pretty formatter will try to fit the elements in a single line within the constaints of the width parameter.
width int 50 Specifies the limit of the compact packing of the formatted elements.
If the length of the formatted string is greater than the parameter's value, the pretty formatter will try to split the string into multiple lines.
indent_type IndentType IndentType.NONE() Specifies the type of the indentation markers used for nested elements in collections and mappings.
text_style TextStyle TextStyle() Specifies the style, which will be applied to the text when formatting.
style_entire_text bool False If True, the pretty formatter will apply the given style to the entire text.
If False, the style will only be applied to individual values.
exact_type_matching bool False If True, the pretty formatter will apply the projections and formatters to items based on the isinstance checks.
If False, type(item) is <specified-type> checks will be used.
projections Iterable[TypeProjection]
(Optional)
None A collection of TypeProjection objects, which will be applied to each item with a matching type before formatting.
formatters MutableSequence[TypeFormatter]
(Optional)
None A mutable sequence of TypeFormatter objects, which is prepended to a list of predefined type formatters and then sorted in an inheritance-wise order (the child types precede their parent types in the ordering). Then, the preprocessed sequence is traveresed in this order to match the type of an input element to a corresponding formatter object.

Warning

In the current version the text style specified in the formatting options may not be applied for types formatted using custom formatters (passed through the formatters parameter). Therefore the style must be applied manually within the custom formatter.



Examples

In the examples directory, you can find short demo programs demonstating the usage of the PyPformat package:

  • simpla_data.py - integers, floats, strings, etc.
  • complex_data.py - collections, mappings, etc.

The formatting configurations used in the example programs are provided in the common.py file.