2 Replies - 1905 Views - Last Post: 10 November 2010 - 07:59 PM Rate Topic: -----

#1 WTFsandwich  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 17
  • Joined: 02-December 09

Rectangle Class: 5 is not an integer?

Posted 09 November 2010 - 06:42 PM

I'm attempting to make a rectangle class. This class needs to use __setattr__ and __getattr__ to get and set attributes. The length and width of the rectangle object must be between 0 and 20, or a ValueError is raised. They must also be either an integer or a float, or TypeError is raised.

I've attempted this in two different ways, but both approaches raise the TypeError when I attempt to assign a value to length or width during my testing of it. I've tried both 5, and 5.0, and somehow, neither are integers or floats.

Here is my code, with both approaches for __setattr__ in it, one being commented out:
class Rectangle:
    def __init__(self):
        self.__width = 1
        self.__length = 1

        
    def __setattr__(self,name,value):
        if name == "__width":
            if ((value is float) or (value is int)) and (0.0<=value<=20.0):
                self.__width = value
            elif ((0.0<=value<=20.0) == False):
                raise ValueError
            elif ((value is float) or (value is int)) == False:
                raise TypeError
            else:
                raise Exception("An unexpected error has occured. Please try again.")
        if name == "__length":
            if ((value is float) or (value is int)) and (0.0<=value<=20.0):
                self.__length = value
            elif ((0.0<=value<=20.0) == False):
                raise ValueError
            elif ((value is float) or (value is int)) == False:
                raise TypeError
            else:
                raise Exception("An unexpected error has occured. Please try again.")

            
##    def __setattr__(self,name,value):
##        if name == "__width":
##            if type(value) != type(float) and (type(value) != type(int)):
##                raise TypeError('Must be float or integer')
##            elif 0.0 <= value <= 20.0:
##                raise ValueError('Must be between 0 and 20')
##            else:
##                self.__width = value
##        elif name == "__length":
##            if (type(value) != type(float)) and (type(value) != type(int)):
##                raise TypeError('Must be float or integer')
##            elif 0.0 <= value <= 20.0:
##                raise ValueError("Must be between 0 and 20")
##            else:
##                self.__length = value
##        else:
##            self.__dict__[name] = value

            
    def __getattr__(self, name):
        if name == "__width":
            return self.__width
        elif name == "__length":
            return self.__length
        else:
            raise AttributeError("Specified attribute not found or defined.")
    def calcPerim(self):
        return "The perimeter is",((2*self.__width)+(2*self.__length)),"units"
    def calcArea(self):
        return "The area is",(self.__width*self.__length),"square units"

x = Rectangle()
x.__length = 5.0
x.__width = 7
x.calcPerim()
x.calcArea()



The error I get is:

Traceback (most recent call last):
File "C:\Python27\RectangleClass.py", line 60, in <module>
x.__length = 5.0
File "C:\Python27\RectangleClass.py", line 23, in __setattr__
raise TypeError
TypeError

I feel like the issue is somewhere in my if/elif/else statements logic more than likely, but I'm mediocre with debugging, at best, so I have no idea.

Any input on a better approach, or towards a solution with my current code would be greatly appreciated.


For clarification, here is the problem specification I must follow:

Exercise 2 (25 points)
Write a class Rectangle that has attributes __length and __width, each of which defaults
to 1. It also has methods that calculate the perimeter and the area of the rectangle. It has
set and get methods for both __length and __width. The set methods should verify hat
__length and __width are each floating-point numbers larger than 0.0 and less than 20.0.
the method should raise special exceptions if the numbers are not in the correct range
(between 0 and 20) or the used input is not of type float.
Write a small program to test the class.

Is This A Good Question/Topic? 0
  • +

Replies To: Rectangle Class: 5 is not an integer?

#2 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Rectangle Class: 5 is not an integer?

Posted 09 November 2010 - 11:28 PM

I think your type checking is flawed. Here is a little playing I did:

>>> type(5)
<type 'int'>
>>> type(5) is int
True
>>> type(5.0) is int
False
>>> 5 is float
False
>>> 5.0 is float
False
>>> 


EDIT: For clarity I mean in __setattr__

This post has been edited by SpeedisaVirus: 09 November 2010 - 11:30 PM

Was This Post Helpful? 0
  • +
  • -

#3 WTFsandwich  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 17
  • Joined: 02-December 09

Re: Rectangle Class: 5 is not an integer?

Posted 10 November 2010 - 07:59 PM

Okay, so using the commented out section for my __setattr__ with a that way of type checking, it now passes that portion with no TypeError.
However, now I'm getting a value error, so I guess something is wrong with my compound statement?

EDIT: Just noticed, I'm raising exception if it IS a proper value, that was a silly mistake on my part.

Thank you for your help.

This post has been edited by WTFsandwich: 10 November 2010 - 08:00 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1