Python Forum

Full Version: Restrict / Limit variable size
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can I restrict / limit a variable size in python ?

For example, I want to use an int variable which can hold ONLY upto 6 digit values, i.e., -999999 thru 999999. If I assign some value outside this range, I would like to catch the exception.

Is this possible ?
if -999999 > number > 999999:
    # do something
(Aug-12-2019, 10:04 AM)mln4python Wrote: [ -> ]If I assign some value outside this range, I would like to catch the exception.
def clamp(n, min_n=-999999, max_n=999999):
    if n < min_n:
        raise Exception(f'To low <{n}> min number is -999999')
    elif n > max_n:
        raise Exception(f'To high <{n}> max number is 999999')
    else:
        return n
Test:
Output:
>>> clamp(100) 100 >>> clamp(-10099) -10099 >>> >>> clamp(99999977) Traceback (most recent call last): File "<interactive input>", line 1, in <module> File "<module1>", line 5, in clamp Exception: To high <99999977> max number is 999999 >>> >>> clamp(-9999990) Traceback (most recent call last): File "<interactive input>", line 1, in <module> File "<module1>", line 3, in clamp Exception: To low <-9999990> min number is -999999
class Limited:
    
    def __init__(self, value=None):
        self._value = value

    @property
    def limited(self):
        return self._value

    @limited.setter
    def limited(self, value):
        if value < -999999:
            raise ValueError('Value out of bounds: less than -999999')
        elif value > 999999:
            raise ValueError('Value out of bounds: greater than 999999')
        else:
            self._value = value

integer = Limited()  # or Limited(0)
integer.limited = 1000
print(integer.limited)
integer.limited = -11000000
Output:
100 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-5-2400afb78752> in <module> 21 integer.limited = 100 22 print(integer.limited) ---> 23 integer.limited = -11000000 <ipython-input-5-2400afb78752> in limited(self, value) 12 def limited(self, value): 13 if value < -999999: ---> 14 raise ValueError('Value out of bounds: less than -999999') 15 elif value > 999999: 16 raise ValueError('Value out of bounds: greater than 999999') ValueError: Value out of bounds: less than -999999
(Aug-12-2019, 11:40 AM)ThomasL Wrote: [ -> ]
class Limited:
    
    def __init__(self, value=None):
        self._value = value

    @property
    def limited(self):
        return self._value

    @limited.setter
    def limited(self, value):
        if value < -999999:
            raise ValueError('Value out of bounds: less than -999999')
        elif value > 999999:
            raise ValueError('Value out of bounds: greater than 999999')
        else:
            self._value = value

integer = Limited()  # or Limited(0)
integer.limited = 1000
print(integer.limited)
integer.limited = -11000000
Output:
100 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-5-2400afb78752> in <module> 21 integer.limited = 100 22 print(integer.limited) ---> 23 integer.limited = -11000000 <ipython-input-5-2400afb78752> in limited(self, value) 12 def limited(self, value): 13 if value < -999999: ---> 14 raise ValueError('Value out of bounds: less than -999999') 15 elif value > 999999: 16 raise ValueError('Value out of bounds: greater than 999999') ValueError: Value out of bounds: less than -999999

Thanks for the help Thomas! This is what I was looking for... I am able to do additional coding as per my requirement. Thanks again!