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?

Community
  • 1
  • 1
jtlz2
  • 7,700
  • 9
  • 64
  • 114
  • 1
    What does your config.ini look like? Could you post an excerpt? – Jivan Jun 09 '16 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. – Dilettant Jun 09 '16 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. http://docs.python-cerberus.org/en/stable/usage.html – chishaku Jun 09 '16 at 16:22

0 Answers0