Python Forum
Restrict / Limit variable size
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Restrict / Limit variable size
#1
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 ?
Reply
#2
if -999999 > number > 999999:
    # do something
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(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
Reply
#4
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
Reply
#5
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  restrict user input to numerical values MCL169 2 869 Apr-08-2023, 05:40 PM
Last Post: MCL169
  Tkinter Entry size limit vman44 3 6,395 Dec-22-2022, 06:58 AM
Last Post: vman44
  How to rotate log and limit the size maiya 0 1,723 Aug-29-2020, 12:41 AM
Last Post: maiya
  How to limit the fie size in logging. maiya 2 7,150 Jul-29-2020, 06:49 AM
Last Post: maiya
  size of set vs size of dict zweb 0 2,117 Oct-11-2019, 01:32 AM
Last Post: zweb
  How can i restrict the number of characters in an input? kevencript 1 3,984 May-23-2018, 05:14 AM
Last Post: micseydel
  CSV file created is huge in size. How to reduce the size? pramoddsrb 0 10,434 Apr-26-2018, 12:38 AM
Last Post: pramoddsrb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020