12

I am not sure what I am doing wrong. Previously, the code was this:

volume = min(60, max(30, volume))

However, after trying with configparser, I keep getting a 500 error on my Twilio Server.

volume_min = configParser.get('config_searchandplay', 'volume_min')
volume_max = configParser.get('config_searchandplay', 'volume_max')
volume = min(volume_max, max(volume_min, volume)) # Max Volume Spam Protection

CONFIG.ini:

[config_searchandplay]
#Volume Protection
volume_max = 90
volume_min = 10 
0

3 Answers 3

21

You should use

ConfigParser.getint(section, option)

rather than casting.

volume_max = configParser.getint('config_searchandplay', 'volume_max')
volume_min = configParser.getint('config_searchandplay', 'volume_min')
volume = min(volume_max, max(volume_min, volume)) # Max Volume Spam Protection
9

The problem of your method is that ConfigParser.get gives you a (unicode) string. So you should convert the values first to number (using int() or float()):

vol_max = int(configParser.get('config_searchandplay', 'volume_max'))
vol_min = int(configParser.get('config_searchandplay', 'volume_min'))
volume = min(vol_max, max(vol_min, volume))

Or use the respective convenience methods: ConfigParser.getint or ConfigParser.getfloat:

vol_max = configParser.getint('config_searchandplay', 'volume_max')
vol_min = configParser.getint('config_searchandplay', 'volume_min')

Although min works on strings:

>>> min(u'90',u'10')
u'10'

It will not always give the answer you are looking for since it does a string comparison. The following is what you want to avoid:

>>> min(u'9',u'10')
u'10'

Therefore you need to convert the string to a number:

>>> min(int(u'9'),(u'90'))
9
1
  • Thank you very much agold, will be testing out soon!
    – Ace NA
    Commented Nov 11, 2015 at 17:15
0

I prefer to convert any string to a number if it is possible (Note, you need very rear a string represented number). Here is my helper function from here.

def number(a, just_try=False):
    try:
        # First, we try to convert to integer.
        # (Note, that all integer can be interpreted as float and hex number.)
        return int(a)
    except Exception:
        # The integer convertion has failed because `a` contains others than digits [0-9].
        # Next try float, because the normal form (eg: 1E3 = 1000) can be converted to hex also.
        # But if we need hex we will write 0x1E3 (= 483) starting with 0x
        try:
            return float(a)
        except Exception:
            try:
                return int(a, 16)
            except Exception:
                if just_try:
                    return a
                else:
                    raise

def number_config(config):
    ret_cfg = {}
    for sk, sv in config._sections.items():
        ret_cfg[sk] = {k:number(v, True) for k,v in sv.items()}
    return ret_cfg

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.