0

I've got a dict of settings for each server in a Discord bot. I've stored the expected type for each setting so that it can be parsed to the correct value when a server admin changes a setting for their server (since parameters are passed as strings). Is there a Python library that allows you do something like:

parse(strValOfSetting, typeOfSetting)

and then get the correct type back? Or do I have to do something like

def parse(strVal, expectedType):
    if isinstance(expectedType, int):
        # parse strVal as int
        ...
    if isinstance(expectedType, tuple):
        ...
    ...

Parsing with a cast like typeOfSetting(strVal) doesn't work for things like booleans because anything other than "" parses to True. That's just an example.

If there's a library that would be cool. If not, I'll just homebrew something.

Thanks

Snow Sailor
  • 323
  • 3
  • 13
  • I think the problem sounds simple enough that using a library (if there's one) is not worth the trouble. You just need a few functions like `parse_bool(string)`, `parse_int(string)`, etc. that take a `str` as a parameter and return a parsed value. – Greg Navis Jul 06 '17 at 06:01
  • Alright. I'll probably just do that. – Snow Sailor Jul 06 '17 at 07:05
  • Depends on what types are supported, but you may be able to attempt to cast directly using `typeOfSetting` and then add a special case for `bool` – Daniel Corin Jul 06 '17 at 07:20

0 Answers0