A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://pypi.python.org/pypi/pyaml below:

pyaml · PyPI

PyYAML-based python module to produce a bit more pretty and human-readable YAML-serialized data.

This module is for serialization only, see ruamel.yaml module for literate YAML parsing (keeping track of comments, spacing, line/column numbers of values, etc).

(side-note: to dump stuff parsed by ruamel.yaml with this module, use only YAML(typ='safe') there)

It’s a small module, and for projects that only need part of its functionality, I’d recommend copy-pasting that in, instead of adding janky dependency.

Contents

Repository URLs:

Warning

Prime goal of this module is to produce human-readable output that can be easily diff’ed, manipulated and re-used, but maybe with occasional issues.

So please do not rely on the thing to produce output that can always be deserialized exactly to what was exported, at least - use PyYAML directly for that (but maybe with options from the next section).

Output also isn’t guaranteed to be stable between module versions, as new representation tweaks get added occasionally, but these can usually be disabled via module parameters, to get output stability over improvements.

What this module does and why

YAML is generally nice and easy format to read if it was written by humans.

PyYAML can a do fairly decent job of making stuff readable, and the best combination of parameters for such output that I’ve seen so far is probably this one:

>>> m = [123, 45.67, {1: None, 2: False}, 'some text']
>>> data = dict(a='asldnsa\nasldpáknsa\n', b='whatever text', ma=m, mb=m)
>>> yaml.safe_dump( data, sys.stdout,
  width=100, allow_unicode=True, default_flow_style=False )
a: 'asldnsa

  asldpáknsa

  '
b: whatever text
ma: &id001
- 123
- 45.67
- 1: null
  2: false
- some text
mb: *id001

pyaml (this module) tries to improve on that a bit, with the following tweaks:

Result for the (rather meaningless) example above:

>>> pyaml.p(data, force_embed=False, vspacing=dict(split_lines=10))

a: |
  asldnsa
  asldpáknsa

b: whatever text

ma: &ma
  - 123
  - 45.67
  - 1:
    2: no
  - some text

mb: *ma

(force_embed=False enabled deduplication with &ma anchor, vspacing is adjusted to split even this tiny output)

Extended example:

>>> pyaml.dump(data, vspacing=dict(split_lines=10))

destination:

  encoding:
    xz:
      enabled: yes
      min_size: 5120
      options:
      path_filter:
        - \.(gz|bz2|t[gb]z2?|xz|lzma|7z|zip|rar)$
        - \.(rpm|deb|iso)$
        - \.(jpe?g|gif|png|mov|avi|ogg|mkv|webm|mp[34g]|flv|flac|ape|pdf|djvu)$
        - \.(sqlite3?|fossil|fsl)$
        - \.git/objects/[0-9a-f]+/[0-9a-f]+$

  result:
    append_to_file:
    append_to_lafs_dir:
    print_to_stdout: yes

  url: http://localhost:3456/uri

filter:
  - /(CVS|RCS|SCCS|_darcs|\{arch\})/$
  - /\.(git|hg|bzr|svn|cvs)(/|ignore|attributes|tags)?$
  - /=(RELEASE-ID|meta-update|update)$

http:
  ca_certs_files: /etc/ssl/certs/ca-certificates.crt
  debug_requests: no
  request_pool_options:
    cachedConnectionTimeout: 600
    maxPersistentPerHost: 10
    retryAutomatically: yes

logging:

  formatters:
    basic:
      datefmt: '%Y-%m-%d %H:%M:%S'
      format: '%(asctime)s :: %(name)s :: %(levelname)s: %(message)s'

  handlers:
    console:
      class: logging.StreamHandler
      formatter: basic
      level: custom
      stream: ext://sys.stderr

  loggers:
    twisted:
      handlers:
        - console
      level: 0

  root:
    handlers:
      - console
    level: custom

Note that unless there are many moderately wide and deep trees of data, which are expected to be read and edited by people, it might be preferrable to directly use PyYAML regardless, as it won’t introduce another (rather pointless in that case) dependency and a point of failure.

Features and Tricks Installation

It’s a regular Python 3.8+ module/package, published on PyPI (as pyaml).

Module uses PyYAML for processing of the actual YAML files and should pull it in as a dependency.

Dependency on unidecode module is optional and only used with force_embed=False keyword (defaults to True), and same-id objects or recursion within serialized data - i.e. only when generating &some_key_id anchors is actually needed. If module is unavailable at runtime, anchor ids might be less like their keys and maybe not as nice.

Using pip is how you generally install it, usually coupled with venv usage (which will also provide “pip” tool itself):

% pip install pyaml

Current-git version can be installed like this:

% pip install git+https://github.com/mk-fg/pretty-yaml

pip will default to installing into currently-active venv, then user’s home directory (under ~/.local/lib/python...), and maybe system-wide when running as root (only useful in specialized environments like docker containers).

There are many other python packaging tools - pipenv, poetry, pdm, etc - use whatever is most suitable for specific project/environment. pipx can be used to install command-line script without a module.

More general info on python packaging can be found at packaging.python.org.

When changing code, unit tests can be run with python -m unittest from the local repository checkout.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4