Python Forum
Restrict / Limit variable size
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Restrict / Limit variable size
#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


Messages In This Thread
Restrict / Limit variable size - by mln4python - Aug-12-2019, 10:04 AM
RE: Restrict / Limit variable size - by wavic - Aug-12-2019, 10:34 AM
RE: Restrict / Limit variable size - by snippsat - Aug-12-2019, 11:22 AM
RE: Restrict / Limit variable size - by ThomasL - Aug-12-2019, 11:40 AM
RE: Restrict / Limit variable size - by mln4python - Aug-13-2019, 07:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  restrict user input to numerical values MCL169 2 931 Apr-08-2023, 05:40 PM
Last Post: MCL169
  Tkinter Entry size limit vman44 3 6,775 Dec-22-2022, 06:58 AM
Last Post: vman44
  How to rotate log and limit the size maiya 0 1,765 Aug-29-2020, 12:41 AM
Last Post: maiya
  How to limit the fie size in logging. maiya 2 7,321 Jul-29-2020, 06:49 AM
Last Post: maiya
  size of set vs size of dict zweb 0 2,153 Oct-11-2019, 01:32 AM
Last Post: zweb
  How can i restrict the number of characters in an input? kevencript 1 4,085 May-23-2018, 05:14 AM
Last Post: micseydel
  CSV file created is huge in size. How to reduce the size? pramoddsrb 0 10,495 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