Code Monkey home page Code Monkey logo

strictyaml's Introduction

StrictYAML

StrictYAML is a type-safe YAML parser built atop ruamel.yaml that parses a restricted subset of the YAML specificaton.

Priorities:

  • Readability of YAML.
  • Ease of use of API.
  • Secure by default.
  • Strict validation of markup and straightforward type casting.
  • Clear, human readable exceptions with line numbers.
  • Acting as a drop in replacement for pyyaml, ruamel.yaml or poyo.
  • Letting you worry about more interesting things than parsing config files.

Simple example:

name: Ford Prefect
age: 42
posessions:
  - Towel

Default parse result:

>>> strictyaml.load(yaml) \
  == {"name": "Ford Prefect", "age": "42", "possessions": ["Towel", ]}   # All data is str, list or dict

Example using optional validator - using mapping, sequence, string and integer:

>>> from strictyaml import load, Map, Str, Int, Seq
>>> load(yaml, Map({"name": Str(), "age": Int(), "possessions": Seq(Str())})) \
  == {"name": "Ford Prefect", "age": 42, "possessions": ["Towel", ]}     # 42 is now an int

Install It

$ pip install strictyaml

FAQ

From learning programmers:

If you're looking at this and thinking "why not do/use X instead?" that's a healthy response, and you deserve answers. These are probably the questions you're asking:

Map Patterns

If you're not sure what the key name is going to be in a map but you know what type the keys and values will be, use "MapPattern".

>>> from strictyaml import load, Map, MapPattern, Str
>>> load(yaml, Map({"emails": MapPattern({Str(), Str()})}) \
  == {"emails": {"arthur": "[email protected]", "zaphod": "[email protected]", "ford": "[email protected]"}}

Optional values

If you want to use a mapping with a number of required keys and a number of optional keys use "Optional":

arthur:
  email: [email protected]
zaphod:
  email: [email protected]
  job: President of the Galaxy
ford:
  email: [email protected]
  job: Freelance "journalist"

This would be parsed like so:

>>> from strictyaml import load, MapPattern, Map, Str, Optional
>>> load(yaml, MapPattern(Str(), Map({"email": Str(), Optional("job"): Str()}))) \
  == {
         "arthur": {'email': '[email protected]',},
         "zaphod": {'email': '[email protected]', 'job': 'President of the Galaxy'},
         "ford": {'email': '[email protected]', 'job': 'Freelance "journalist"'},
     }

Either/Or

If, for example, you want to parse something as a list of strings or an individual string, you can use a pipe operator to distinguish between them - like so: |

zaphod:
  email: [email protected]
  victims: Good taste
ford:
  email: [email protected]
  victims: Journalistic integrity
arthur:
  email: [email protected]
  victims:
    - A bowl of petunias
    - Agrajag
    - A sperm whale

This would be parsed like so:

>>> from strictyaml import load, Seq, Map, Str, Optional
>>> load(yaml, MapPattern(Str(), Map({"email": Str(), "victims": Str() | Seq(Str())}))) \
  == {
         "zaphod": {'email': '[email protected]', 'victims': 'President of the Galaxy'},
         "arthur": {'email': '[email protected]', 'victims': 'Journalistic integrity'},
         "ford": {'email': '[email protected]', 'victims': ['A bowl of petunias', 'Agrajag', 'A sperm whale', ]},
     }

Numbers

StrictYAML will parse a string into integers, floating point or decimal (non-floating point) numbers if you specify it:

>>> import from strictyaml import load, Map
>>> load("int: 42", Map({"int": strictyaml.Int()})) == {"int": 42}
>>> load("float: 42.3333", Map({"float": strictyaml.Float()})) == {"float": 42.3333}
>>> load("price: 35.42811", Map({"price": strictyaml.Decimal()})) == {"price": decimal.Decimal('35.42811')}

Booleans

Upper case or lower case - it doesn't matter. Yes, on and true are treated as True and no, off and false are treated as False.

>>> load("booltrue: yes", Map({"booltrue": strictyaml.Bool()})) == {"booltrue": True}
>>> load("boolfalse: no", Map({"boolfalse": strictyaml.Bool()})) == {"booltrue": True}
>>> load("booltrue: true", Map({"booltrue": strictyaml.Bool()})) == {"booltrue": True}
>>> load("boolfalse: False", Map({"boolfalse": strictyaml.Bool()})) == {"booltrue": False}

Empty values

Empty values can be be validated and returned as None, {} and []:

>>> load("empty: ", Map({"empty": strictyaml.EmptyNone()})) == {"empty": None}
>>> load("empty: ", Map({"empty": strictyaml.EmptyDict()})) == {"empty": {}}
>>> load("empty: ", Map({"empty": strictyaml.EmptyList()})) == {"empty": []}

Enums

>>> load("day: monday", Map({"day": strictyaml.Enum(["monday", "tuesday", "wednesday"])})) == {"day": "monday"}

Datetimes

This datetime parser uses dateutil's datetime parser, which is used, among others, by arrow, delorean and pendulum.

>>> load("date: 2016-10-22T14:23:12+00:00", Map({"date": strictyaml.Datetime()})) == {"date": datetime(2016, 10, 22, 14, 23, 12)}

Comma Separated Values

Scalar strings can be parsed a second time into comma separated values.

>>> load("a: 1, 2, 3", Map({"a": strictyaml.CommaSepaeated(strictyaml.Int())})) == {"a": [1, 2, 3, ]}

Custom scalar types

COMING SOON

Using YAML Valdation

See: What is kwalify and when should I use it?

COMING SOON

Roundtripping YAML

COMING SOON

Contributors

  • @gvx
  • @ AlexandreDecan
  • @lots0logs

strictyaml's People

Contributors

crdoconnor avatar lots0logs avatar gvx avatar

Watchers

Henrik Holmboe avatar James Cloos 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.