Performing arithmetic operation in YAML?

Lucas picture Lucas · Aug 27, 2014 · Viewed 13.8k times · Source

Sometimes I have to specify the time (in seconds) in the configuration file, and it's quite annoying to write exact seconds amount - instead I would like to perform arithmetics so I could use:

some_time: 1 * 24 * 60 * 60

instead of exact:

some_time: 86400

Unfortunately, while using this line: some_time: 1 * 24 * 60 * 60, it will treat that configuration line as a string. Of course, I can use - eval(config['some_time']) but I am rather wondering if that is possible to perform arithmetics in YAML?

Answer

Rafael Almeida picture Rafael Almeida · Aug 27, 2014

I don't think there is. At least not on spec (http://yaml.org/spec/1.2/spec.html). People add non-official tags to yaml (and wikipedia seems to say there's proposal for a yield tag, though they don't say who proposed or where: http://en.wikipedia.org/wiki/YAML#cite_note-16), but nothing like you need seems to be available in pyyaml.

Looking at pyyaml specific tags there doesn't seem to be anything of interest. Though !!timestamp '2014-08-26' may be handy in some of your scenarios (http://pyyaml.org/wiki/PythonTagScheme).