Quantcast
Channel: Active questions tagged config - Stack Overflow
Viewing all articles
Browse latest Browse all 5058

How do you include complex/imaginary values in config (*.toml, *.ini) files?

$
0
0

I'm migrating my project configurations into a separate config file so that different parts of my project can access them independently. I've been using *.toml files for this lately and I quite like them.

In this project, for the first time, I have some constants that are complex numbers, or even arrays of complex numbers. I just discovered that *.toml doesn't support complex values. Nothing turns up on google or stackoverflow when I search for "toml complex numbers," so I seem to be the first person to encounter this problem.

How does anyone keep a human-readable config file with complex numbers in it? I can certainly create 2 values, one for the real part and one for the imaginary part, but I wouldn't consider that "human readable" for my purposes. I am not married to the *.toml format and am willing to switch to another config file format that supports this.

Below is a MWE of what I'm trying to do:

>>> import toml>>> pretend_file = 'my_real_number = 3'>>> data = toml.loads(pretend_file)>>> print(data){'my_real_number': 3}>>> new_pretend_file = 'my_complex_number = 3 + 3j'>>> data = toml.loads(new_pretend_file)raise TomlDecodeError(str(err), original, pos)toml.decoder.TomlDecodeError: invalid literal for int() with base 0: '3 + 3j' (line 1 column 1 char 0)

Edit: Based on a suggestion by Brian, I suppose one could use a string. Would it be poor form to use ast.literal_eval? For example:

>>> import toml>>> from ast import literal_eval>>> pretend_file = "my_complex_array = '[2+2j, 3+3j]'">>> data = toml.loads(pretend_file)>>> np.array(literal_eval(data['my_complex_array']), dtype=complex)array([2.+2.j, 3.+3.j])

Viewing all articles
Browse latest Browse all 5058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>