-1

I'm setting up a script where I need to take several variable from my config at the same time. I've set my config as: config = configparser.ConfigParser().

I've tried to take these variables using * symbol.

[part1]
variable1 = 10
variable2 = 20

I've tried to take it with:

variable = config.get('part1', 'variable*')

but this way doesn't work.

I search a way to realize that, in order to get it into a list for example. Thanks in advance.

  • 3
    What's `config`? A dictionary? – Right leg Jul 05 '19 at 14:25
  • the `get` method will only return one value. And what should `variable` be? a list? the first value found? a dictionnary? – olinox14 Jul 05 '19 at 14:25
  • if it is a dictionary you can use the dict comprehension – Kaies LAMIRI Jul 05 '19 at 14:25
  • Do you not know how to get the individual variables, or how to know what the variables are, or something else? – Scott Hunter Jul 05 '19 at 14:25
  • Possible duplicate of [Lists in ConfigParser](https://stackoverflow.com/questions/335695/lists-in-configparser) –  Jul 05 '19 at 14:27
  • 1
    @Rightleg my `config` is just another script where I have all my configuration variables, I'll update my question. –  Jul 05 '19 at 14:27
  • 1
    @olinox14 `variable` is just a string that I want to return. In fact, I want to get all the variable which begins by the string `variable` in order to get `variable1` and `variable2`. –  Jul 05 '19 at 14:31
  • 1
    @ScottHunter I know how I can get variable by variable but I search a way to obtain all the variables which begin by `variable` (for example) in a single list. –  Jul 05 '19 at 14:33
  • 1
    @KaiesLAMIRI this is not a dict. –  Jul 05 '19 at 14:34

2 Answers2

2

Do

cfg_items = config.items('part1')

to get all the configs in the section.

Then filter the dictionary keys using the regex you defined.

You can do it in a single step

cfg_list = [val for var, val in config.items('part1') if var.startswith('variable')]
absolutelydevastated
  • 1,657
  • 1
  • 11
  • 28
1

I would just use a list comprehension to iterate over the variables you want.

to_get = ('variable1', 'variable2')
values = [config.get('part1', x) for x in to_get]

The problem with variable* is that it assumes the config file will only contain the options you expect. Be explicit, and only fetch the ones you want.

ConfigParser supports the mapping protocol in Python 3.2 or later:

values = [config['part1'][x] for x in to_get]

which opens the door for using operator.itemgetter as well. Said use can range from ugly to useful, depending on how you refactor. The straightforward approach would look like

values = operator.itemgetter(*to_get)(config['part1'])
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    In my case, I'm trying to establish something configurable. How can I use this method If I don't know how many `variable` I have in my `part1` for example? –  Jul 05 '19 at 14:47
  • 1
    I would redesign your configuration so the that you *do* know how many variables there are. Require something like `variable_count = 3`, then you know that `variable1`, `variable2`, and `variable3` are to be read. If that's not an option, just start sequentially reading variables until you hit one that does not exist. – chepner Jul 05 '19 at 15:04
  • 1
    Better yet, use a better file format that supports lists. (JSON, YAML, TOML, dhall, etc) – chepner Jul 05 '19 at 15:05