diff options
Diffstat (limited to '_doc/basicuse.rst')
-rw-r--r-- | _doc/basicuse.rst | 45 |
1 files changed, 21 insertions, 24 deletions
diff --git a/_doc/basicuse.rst b/_doc/basicuse.rst index 56a28d6..f99d609 100644 --- a/_doc/basicuse.rst +++ b/_doc/basicuse.rst @@ -9,11 +9,10 @@ the process of being fleshed out*. Please pin your dependency to You load a YAML document using:: - from ruamel.yaml import YAML - - yaml=YAML(typ='safe') # default if not specfied is round-trip - - yaml.load(doc) + from ruamel.yaml import YAML + + yaml=YAML(typ='safe') # default if not specfied is round-trip + yaml.load(doc) in this ``doc`` can be a file pointer (i.e. an object that has the `.read()` method, a string or a ``pathlib.Path()``. `typ='safe'` @@ -24,11 +23,11 @@ when possible/available) Dumping works in the same way:: - from ruamel.yaml import YAML - - yaml=YAML() - yaml.default_flow_style = False - yaml.dump({a: [1, 2]}, s) + from ruamel.yaml import YAML + + yaml=YAML() + yaml.default_flow_style = False + yaml.dump({'a': [1, 2]}, s) in this ``s`` can be a file pointer (i.e. an object that has the `.write()` method, or a ``pathlib.Path()``. If you want to display @@ -37,26 +36,24 @@ your output, just stream to `sys.stdout`. If you need to transform a string representation of the output provide a function that takes a string as input and returns one:: - def tr(s): - return s.replace('\n', '<\n') # such output is not valid YAML! - - yaml.dump(data, sys.stdout, transform=tr) - + def tr(s): + return s.replace('\n', '<\n') # such output is not valid YAML! + + yaml.dump(data, sys.stdout, transform=tr) More examples ------------- - Using the C based SafeLoader (at this time is inherited from libyaml/PyYAML and e.g. loads ``0o52`` as well as ``052`` load as integer ``42``):: - from ruamel.yaml import YAML - - yaml=YAML(typ="safe") - yaml.load("""a:\n b: 2\n c: 3\n""") + from ruamel.yaml import YAML + + yaml=YAML(typ="safe") + yaml.load("""a:\n b: 2\n c: 3\n""") Using the Python based SafeLoader (YAML 1.2 support, ``052`` loads as ``52``):: - from ruamel.yaml import YAML - - yaml=YAML(typ="safe", pure=True) - yaml.load("""a:\n b: 2\n c: 3\n""") + from ruamel.yaml import YAML + + yaml=YAML(typ="safe", pure=True) + yaml.load("""a:\n b: 2\n c: 3\n""") |