0

I want to parse a config.ini file into a dictionary a la Automatically read configuration values in Python, but automatically identifying the data types - as much as possible - in some clever fashion. Is there a way to do this, or must it be coded by hand, and if so what is a good way to go? Thanks!

Here's an example:

[multinest]
n_live_points       = 500 ; or 1000
multimodal          = False ; Posterior unlikely to be multimodal
max_modes           = 1
seed                = 1234  ; [-1 for clock]
do_ins              = False
max_iter            = 0
evidence_tolerance  = 0.5
mode_tolerance      = -1e90
outstem             = 1-

I would like a loader that can differentiate the main standard python types: strings, floats, integers, booleans, lists, dictionaries, plus numpy arrays.

These are also helpful (but old?):

Automatically Type Cast Parameters In Python

automatic detection/conversion of data types?

3
  • 1
    What does your config.ini look like? Could you post an excerpt? Commented Jun 9, 2016 at 15:49
  • 1
    Hm, ini files are all about easy editable and parseable key value storage for strings. Without further details on your application's world view (data / domain model) I do not see a general solution. If you add some constraining details to the question, there might be someone willing to explain a clever idea ;-) Sample: Fixed, finite set of allowed names, use the "matching" type conversion functors stored in a map. Commented Jun 9, 2016 at 15:53
  • PyYAML will infer all of those types except numpy arrays. You can also explicitly define your schema with a validator such as cerberus and pass your ini-derived dict through. docs.python-cerberus.org/en/stable/usage.html Commented Jun 9, 2016 at 16:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.