Skip to main content

A YAML parser that reads only a restricted version of YAML.

Project description

Dumb YAML is a a restricted YAML parser for python with an emphasis on cutting out the features that surprise people and inhibit readability.

  • Everything parsed as a string, list or dict. There’s no implicit typing (e.g. yes == “yes” != True).

  • Flow style YAML ( { } [ ] ) is explicitly disallowed.

  • Node anchors and references are explicitly disallowed.

See Dumb YAML versus Regular YAML for a full side by side comparison.

It was built for use with the hitch testing framework’s test description language.

If the YAML you are parsing uses explicitly disallowed features, exceptions will be raised.

If all of your YAML is already dumb, everything should just work the same way as it did before and now you will have additional type protection against users of your YAML who want to show off how smart they are.

Usage

It’s built atop pyyaml (it’s a dependency) and has the same API. If you are already using pyyaml you don’t have to change a lot.

Install:

pip install dumbyaml

Replace:

import yaml

With:

import dumbyaml as yaml

Then, anywhere where you are using data parsed from YAML into a list, dict, list of dicts or dict of lists or whatever, you will need to add an explicit type conversion in your code from the string to your desired type.

E.g:

answer_to_question = yamlresult['answer'].lower() in ("yes", "y", "true")
number_of_twinkies = int(yamlresult['Number of twinkies'])
cost_of_space_station = float(yamlresult['Cost of space station'])

Why?

YAML is arguably the tersest, cleanest markup language for marking up hierarchical data. It handles lists, associations and block literals beautifully and readably with a minimalist syntax:

See beautifulyaml

However, the more powerful features are often confusing and break things:

See uglyyaml

The features that made the ugly yaml were examples of the language trying to be way too smart and way too powerful.

Hence “dumb yaml”.

As Tim Berners Lee said:

Computer Science spent the last forty years making languages which
were as powerful as possible. Nowadays we have to appreciate the reasons
for picking not the most powerful solution but the least powerful.

And, as Guido van Rossum said:

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Readability counts.
There should be one-- and preferably only one --obvious way to do it.

They weren’t wrong.

If you strip these YAML features away, you can still achieve much the same result.

  • Thing that are parsed as strings can be

What do I substitute for those features if I was already using them?

  • The implicit type conversions can be replaced by explicit type conversions in your code.

  • Flow style can be replaced by block style.

  • Node anchors that made your YAML DRY can be replaced by a templating language like jinja2.

  • Binary encoding (!!binary) can be replaced by base64 and an explicit type conversion.

What YAML really needs some stronger typing

Absolutely agree. That’s what pykwalify is for.

This is why you should use XML/JSON/TOML/INI/etc. instead!

Since cleanliness and readability are somewhat a matter of opinion and the net effects of choosing a markup language is not always clear, objective side by side comparisons are probably the best way of making the right choice.

If you feel tempted to start a flamewar over your favorite markup language, please redirect your anger into creating a side by side comparison, forking this repo and issuing a pull request. Thanks!

Rules:

  • Put the differentiating features at the top.

  • Side by side comparisons must represent the same data.

Hacking

If you want to hack:

sudo pip install hitch
cd dumbyaml/tests
hitch init
hitch test run.test --settings tdd.test

The py.test unit tests are in dumbyaml/unittests.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dumbyaml-0.9.tar.gz (4.8 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page