summaryrefslogtreecommitdiff
path: root/_doc/example.rst
diff options
context:
space:
mode:
Diffstat (limited to '_doc/example.rst')
-rw-r--r--_doc/example.rst211
1 files changed, 0 insertions, 211 deletions
diff --git a/_doc/example.rst b/_doc/example.rst
deleted file mode 100644
index a2cdf78..0000000
--- a/_doc/example.rst
+++ /dev/null
@@ -1,211 +0,0 @@
-********
-Examples
-********
-
-Basic round trip of parsing YAML to Python objects, modifying
-and generating YAML::
-
- import sys
- from ruamel.yaml import YAML
-
- inp = """\
- # example
- name:
- # details
- family: Smith # very common
- given: Alice # one of the siblings
- """
-
- yaml = YAML()
- code = yaml.load(inp)
- code['name']['given'] = 'Bob'
-
- yaml.dump(code, sys.stdout)
-
-Resulting in::
-
- # example
- name:
- # details
- family: Smith # very common
- given: Bob # one of the siblings
-
-with the old API::
-
- from __future__ import print_function
-
- import sys
- import ruamel.yaml
-
- inp = """\
- # example
- name:
- # details
- family: Smith # very common
- given: Alice # one of the siblings
- """
-
- code = ruamel.yaml.load(inp, ruamel.yaml.RoundTripLoader)
- code['name']['given'] = 'Bob'
-
- ruamel.yaml.dump(code, sys.stdout, Dumper=ruamel.yaml.RoundTripDumper)
-
- # the last statement can be done less efficient in time and memory with
- # leaving out the end='' would cause a double newline at the end
- # print(ruamel.yaml.dump(code, Dumper=ruamel.yaml.RoundTripDumper), end='')
-
-Resulting in ::
-
- # example
- name:
- # details
- family: Smith # very common
- given: Bob # one of the siblings
-
-----
-
-YAML handcrafted anchors and references as well as key merging
-are preserved. The merged keys can transparently be accessed
-using ``[]`` and ``.get()``::
-
- from ruamel.yaml import YAML
-
- inp = """\
- - &CENTER {x: 1, y: 2}
- - &LEFT {x: 0, y: 2}
- - &BIG {r: 10}
- - &SMALL {r: 1}
- # All the following maps are equal:
- # Explicit keys
- - x: 1
- y: 2
- r: 10
- label: center/big
- # Merge one map
- - <<: *CENTER
- r: 10
- label: center/big
- # Merge multiple maps
- - <<: [*CENTER, *BIG]
- label: center/big
- # Override
- - <<: [*BIG, *LEFT, *SMALL]
- x: 1
- label: center/big
- """
-
- yaml = YAML()
- data = yaml.load(inp)
- assert data[7]['y'] == 2
-
-
-The ``CommentedMap``, which is the ``dict`` like construct one gets when round-trip loading,
-supports insertion of a key into a particular position, while optionally adding a comment::
-
- import sys
- from ruamel.yaml import YAML
-
- yaml_str = """\
- first_name: Art
- occupation: Architect # This is an occupation comment
- about: Art Vandelay is a fictional character that George invents...
- """
-
- yaml = YAML()
- data = yaml.load(yaml_str)
- data.insert(1, 'last name', 'Vandelay', comment="new key")
- yaml.dump(data, sys.stdout)
-
-gives::
-
- first_name: Art
- last name: Vandelay # new key
- occupation: Architect # This is an occupation comment
- about: Art Vandelay is a fictional character that George invents...
-
-Please note that the comment is aligned with that of its neighbour (if available).
-
-The above was inspired by a `question <http://stackoverflow.com/a/36970608/1307905>`_
-posted by *demux* on StackOverflow.
-
-----
-
-By default ``ruamel.yaml`` indents with two positions in block style, for
-both mappings and sequences. For sequences the indent is counted to the
-beginning of the scalar, with the dash taking the first position of the
-indented "space".
-
-The following program with three dumps::
-
- import sys
- from ruamel.yaml import YAML
-
- data = {1: {1: [{1: 1, 2: 2}, {1: 1, 2: 2}], 2: 2}, 2: 42}
-
- yaml = YAML()
- yaml.explicit_start = True
- yaml.dump(data, sys.stdout)
- yaml.indent = 4
- yaml.block_seq_indent = 2
- yaml.dump(data, sys.stdout)
-
-
- def sequence_indent_four(s):
- # this will fail on direclty nested lists: {1; [[2, 3], 4]}
- levels = []
- ret_val = ''
- for line in s.splitlines(True):
- ls = line.lstrip()
- indent = len(line) - len(ls)
- if ls.startswith('- '):
- if not levels or indent > levels[-1]:
- levels.append(indent)
- elif levels:
- if indent < levels[-1]:
- levels = levels[:-1]
- # same -> do nothing
- else:
- if levels:
- if indent <= levels[-1]:
- while levels and indent <= levels[-1]:
- levels = levels[:-1]
- ret_val += ' ' * len(levels) + line
- return ret_val
-
- yaml = YAML()
- yaml.explicit_start = True
- yaml.dump(data, sys.stdout, transform=sequence_indent_four)
-
-gives as output::
-
- ---
- 1:
- 1:
- - 1: 1
- 2: 2
- - 1: 1
- 2: 2
- 2: 2
- 2: 42
- ---
- 1:
- 1:
- - 1: 1
- 2: 2
- - 1: 1
- 2: 2
- 2: 2
- 2: 42
- ---
- 1:
- 1:
- - 1: 1
- 2: 2
- - 1: 1
- 2: 2
- 2: 2
- 2: 42
-
-The transform example was inspired by a `question
-<https://stackoverflow.com/q/44388701/1307905>`_ posted by *nowox* on
-StackOverflow.