GithubHelp home page GithubHelp logo

pyyaml-future's Introduction

pyyaml-future

Use YAML 1.3 Features in PyYAML (YAML 1.1)

Synopsis

import yaml
from yamlfuture import Loader

# Define your own anchors:
Loader.anchors = {'name': 'YAML'}
# !import relative to this:
Loader.filepath = __file__

yamlstream = """\
---
greeting: !+ |
  Hello {*name}.
  Welcome to the future!

copyright: !import [../data.yaml, /copyright/year]
"""

print(yaml.dump(yaml.load(yamlstream, Loader))

Status

This module is very ALPHA.

YAML 1.3 is still being defined. As the various features of the YAML 1.3 evolve, yamlfuture will evolve.

Use with caution for now.

Description

YAML 1.3 is bringing exciting new features to YAML (while keeping the YAML you know and love (or hate) the same).

This package lets you use the new features (or close approximations) in PyYAML now.

This is a very early release. More features and documentation coming soon.

Features

YAML 1.3 adds optional "language features" that you can request that a parser use. Some of the planned ones are:

  • Standard Library node functions:

    - @merge(*base)
      foo: 42
    - stuff: @import stuff.yaml
    
  • String expansion (interpolation):

    greeting: `Hello {*name}`
    
  • Extended aliases with YPath:

    total: @sum(*nums/x, $*nums/y)
    

In current PyYAML we can emulate these with tags and implicit typing, as long as we don't use invalid YAML 1.1 syntax.

For @func(1,2) we use !func [1,2]

For *alias/path/value we use +*alias/path/value.

For interpolation we use !+ Hello {*name} instead of backticks.

YAML 1.3 actually parses these new things into the same event model. In other words, a 1.3 parser would report @foo(*bar,42) as !foo [*bar, 42]. Just with a cleaner syntax.

yamlfuture current features

  • Merge a sequence of mappings:

    merged: !merge [*map1, *map2, foo: bar]
    
  • Define anchors (from Python) outside the YAML stream:

    Loader.anchors = {"name": "world"}
    yaml.load("hello: *name", Loader)
    
  • Use aliases with paths:

    value: &foo
      yaml: future
    when: +*foo/yaml
    absolute: +*/foo/0/bar
    siblings:
      a: 2
      b: 4
      all: [+*:a, +*:b]
    foo: { bar: +*../../siblings/a }
    
  • String interpolation:

    greeting: !+ Hello {*name}!
    
  • Import other YAML files into any node:

    foo: !import foo.yaml
    bar: !import [foo.yaml, /0/bar]
    
  • Import/render external "template" yaml files:

    # file 'template.yaml'
    foo:
      bar: *value
    
    # file 'main.yaml'
    bar: !render [template.yaml, value: baz]
    

License & Copyright

This project is licensed under the terms of the MIT license. See LICENSE for more details.

Copyright 2021 Ingy döt Net [email protected]

pyyaml-future's People

Contributors

ingydotnet avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.