Jsonnet: Add std.parseFloat

Created on 20 Jan 2020  路  5Comments  路  Source: google/jsonnet

I would like to suggest a feature to parse strings into float numbers. I haven't found std.parseFloat() function in https://jsonnet.org/ref/stdlib.html. Since std.extVar returns a string it's impossible to use float numbers as external variables.

In my case, it would be useful since I used jsonnet to describe experiments in AllenNLP framework as sometimes I pass hyperparameters like learning rate as an environmental variable. Although I could obviously pass them using a top-level function, some platforms like Polyaxon requires to pass them as environmental variables.

enhancement stdlib

Most helpful comment

Workaround: use std.parseJson, which can also parse a float as a special case:

That's what we've done. It's not easy to discover that workaround the first time, but it does work well enough so far.

All 5 comments

Since std.extVar returns a string it's impossible to use float numbers as external variables.

Not necessarily. You can do something like:

21x.jsonnet:

std.extVar('foo') * 21

Command to run (see that --ext-code is used rather than --ext-var):

jsonnet --ext-code foo=2 21x.jsonnet

That said, parseFloat would be a useful addition to stdlib.

Workaround: use std.parseJson, which can also parse a float as a special case:

$ ./jsonnet -e 'std.parseJson("3.14")'
3.1400000000000001

Thank you for the workaround!

Workaround: use std.parseJson, which can also parse a float as a special case:

That's what we've done. It's not easy to discover that workaround the first time, but it does work well enough so far.

Was this page helpful?
0 / 5 - 0 ratings