Skip to main content

release-engineers/y

Status: Beta PyPI version

A tool like yq (and jq) but without their feature of completely cleaning up your YAML documents. yq and jq are great tools especially for making changes to documents that are not intended to be human-readable. However, when you want to make changes programmatically to documents and still be able to read them afterwards, they're probably not the best tools for the job.

y aims to be a tool familiar for users of yq and jq while preserving your YAML's comments, block & flow style, key ordering, blank lines & more.

Install the y CLI utility and Python package with;

pip install re-y

Usage

y comes with an expression language similar to yq which can be used to select and edit parts of a YAML document. Here's examples for a YAML document 'sample.yml';

---
a:
  b:
    # comments and whitespace are preserved
    # unless the object owning them is removed or replaced
    c:
      - d: 1
      # replacing the array element `d: 1` above would remove this comment (and any potential whitespace around it)
      - e: 2
      - f: 3
#!/usr/bin/env bash
# modify a file in-place
y --inplace --file docs/sample.yml -- '.a.b.c[0] = 123'
#!/usr/bin/env bash
# write to stdout
y --file docs/sample.yml -- '.a.b.c[0] = 123'

Expressions

Selectors Description Example
. Select the root of the expression context y --file docs/sample.yml '.'
.key Select field key within the context y --file docs/sample.yml '.a.b.c'
[index] Select element at index within the context y --file docs/sample.yml '.a.b.c[0]'
$ Select the root of the program context. y --file docs/sample.yml '$'
Operators Description Example
expression = expression Assign a value a given reference y --file docs/sample.yml '.a = .a.b'
expression + expression Mathematical operators +, -, /, *, % and ^ y '.1 + 2 * 4'
expression | expression Pipe operator; pass the output of the left-hand expression as context for the right-hand expression. y '2 | . * 2'
(expression) Precedence operator; evaluate the expression within parentheses first. y '(2 | . * 2)'

ℹ️ Regarding mathematical operators; ^ is evaluated first, then *, / and % are evaluated left-to-right, and finally + and - are evaluated left-to-right.

⚠️ Assigning precedence to mathematical operators with parentheses is not supported.

ℹ️ The pipe operator | modifies the expression context (.) of the right-hand expression to the output of the left-hand expression, hence the availability of the $ operator to select the root of the program context.

Constants Description Example
true Boolean values true and false y 'true'
null Null value y 'null'
123 Numbers y '123'
"abc" Strings y '"abc"'

Contributing

This is a Python Poetry project using Fire. See Poetry for more information.

Development requires:

This project was created using template-poetry.

Metadata

Release files for re-y 2.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for re-y 2.1.7
File Interpreter ABI Platform
re_y-2.1.7-py3-none-any.whl Python 3 none any Details

Release files / re_y-2.1.7-py3-none-any.whl

Download URL re_y-2.1.7-py3-none-any.whl
Size 17.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
986e8e84848c26ff91064e4ff2415cda731e9ded56699b676ab9dfe26fe26ed4
BLAKE2b-256 checksum
How to use checksums
59b617d84426c507e2440344b448e144a672b9d499693b6dbe51b8e37d7fc9fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.4 Linux/5.15.0-1041-azure

Release history Release notifications | RSS feed

This release

2.1.7 This release

1 release file

2.1.6

1 release file

2.1.5

1 release file

2.1.4

1 release file

2.1.3

1 release file

2.1.2

1 release file

2.0.0

1 release file

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page