__init__.py 45.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
import doctest
import collections

# TODO - finish doc tests

# TODO - unit tests needed for get/set and Box named tuple

__version__ = '0.1.4'


# Constants for rectangle attributes:
TOP = 'top'
BOTTOM = 'bottom'
LEFT = 'left'
RIGHT = 'right'
TOPLEFT = 'topleft'
TOPRIGHT = 'topright'
BOTTOMLEFT = 'bottomleft'
BOTTOMRIGHT = 'bottomright'
MIDTOP = 'midtop'
MIDRIGHT = 'midright'
MIDLEFT = 'midleft'
MIDBOTTOM = 'midbottom'
CENTER = 'center'
CENTERX = 'centerx'
CENTERY = 'centery'
WIDTH = 'width'
HEIGHT = 'height'
SIZE = 'size'
BOX = 'box'
AREA = 'area'

Box = collections.namedtuple('Box', 'left top width height')
Point = collections.namedtuple('Point', 'x y')
Size = collections.namedtuple('Size', 'width height')

class PyRectException(Exception):
    """
    This class exists for PyRect exceptions. If the PyRect module raises any
    non-PyRectException exceptions, this indicates there's a bug in PyRect.
    """
    pass


def _checkForIntOrFloat(arg):
    """Raises an exception if arg is not an int or float. Always returns None."""
    if not isinstance(arg, (int, float)):
        raise PyRectException('argument must be int or float, not %s' % (arg.__class__.__name__))


def _checkForInt(arg):
    """Raises an exception if arg is not an int. Always returns None."""
    if not isinstance(arg, int):
        raise PyRectException('argument must be int or float, not %s' % (arg.__class__.__name__))


def _checkForTwoIntOrFloatTuple(arg):
    try:
        if not isinstance(arg[0], (int, float)) or \
           not isinstance(arg[1], (int, float)):
            raise PyRectException('argument must be a two-item tuple containing int or float values')
    except:
        raise PyRectException('argument must be a two-item tuple containing int or float values')


def _checkForFourIntOrFloatTuple(arg):
    try:
        if not isinstance(arg[0], (int, float)) or \
           not isinstance(arg[1], (int, float)) or \
           not isinstance(arg[2], (int, float)) or \
           not isinstance(arg[3], (int, float)):
            raise PyRectException('argument must be a four-item tuple containing int or float values')
    except:
        raise PyRectException('argument must be a four-item tuple containing int or float values')


def _collides(rectOrPoint1, rectOrPoint2):
    """Returns True if rectOrPoint1 and rectOrPoint2 collide with each other."""


def _getRectsAndPoints(rectsOrPoints):
    points = []
    rects = []
    for rectOrPoint in rectsOrPoints:
        try:
            _checkForTwoIntOrFloatTuple(rectOrPoint)
            points.append(rectOrPoint)
        except PyRectException:
            try:
                _checkForFourIntOrFloatTuple(rectOrPoint)
            except:
                raise PyRectException('argument is not a point or a rect tuple')
            rects.append(rectOrPoint)
    return (rects, points)


'''
def collideAnyBetween(rectsOrPoints):
    """Returns True if any of the (x, y) or (left, top, width, height) tuples
    in rectsOrPoints collides with any other point or box tuple in rectsOrPoints.

    >>> p1 = (50, 50)
    >>> p2 = (100, 100)
    >>> p3 = (50, 200)
    >>> r1 = (-50, -50, 20, 20)
    >>> r2 = (25, 25, 50, 50)
    >>> collideAnyBetween([p1, p2, p3, r1, r2]) # p1 and r2 collide
    True
    >>> collideAnyBetween([p1, p2, p3, r1])
    False
    """
    # TODO - needs to be complete

    # split up
    rects, points = _getRectsAndPoints(rectsOrPoints)

    # compare points with each other
    if len(points) > 1:
        for point in points:
            if point != points[0]:
                return False

    # TODO finish
'''



'''
def collideAllBetween(rectsOrPoints):
    """Returns True if any of the (x, y) or (left, top, width, height) tuples
    in rectsOrPoints collides with any other point or box tuple in rectsOrPoints.

    >>> p1 = (50, 50)
    >>> p2 = (100, 100)
    >>> p3 = (50, 200)
    >>> r1 = (-50, -50, 20, 20)
    >>> r2 = (25, 25, 50, 50)
    >>> collideAllBetween([p1, p2, p3, r1, r2])
    False
    >>> collideAllBetween([p1, p2, p3, r1])
    False
    >>> collideAllBetween([p1, r2]) # Everything in the list collides with each other.
    True
    """

    # Check for valid arguments
    try:
        for rectOrPoint in rectsOrPoints:
            if len(rectOrPoint) == 2:
                _checkForTwoIntOrFloatTuple(rectOrPoint)
            elif len(rectOrPoint) == 4:
                _checkForFourIntOrFloatTuple(rectOrPoint)
            else:
                raise PyRectException()
    except:
        raise PyRectException('Arguments in rectsOrPoints must be 2- or 4-integer/float tuples.')

    raise NotImplementedError # return a list of all rects or points that collide with any other in the argument
'''

class Rect(object):
    def __init__(self, left=0, top=0, width=0, height=0, enableFloat=False, readOnly=False, onChange=None, onRead=None):
        _checkForIntOrFloat(width)
        _checkForIntOrFloat(height)
        _checkForIntOrFloat(left)
        _checkForIntOrFloat(top)

        self._enableFloat = bool(enableFloat)
        self._readOnly = bool(readOnly)

        if onChange is not None and not callable(onChange):
            raise PyRectException('onChange argument must be None or callable (function, method, etc.)')
        self.onChange = onChange

        if onRead is not None and not callable(onRead):
            raise PyRectException('onRead argument must be None or callable (function, method, etc.)')
        self.onRead = onRead

        if enableFloat:
            self._width  = float(width)
            self._height = float(height)
            self._left   = float(left)
            self._top    = float(top)
        else:
            self._width  = int(width)
            self._height = int(height)
            self._left   = int(left)
            self._top    = int(top)


    # OPERATOR OVERLOADING / DUNDER METHODS
    def __repr__(self):
        """Return a string of the constructor function call to create this Rect object."""
        return '%s(left=%s, top=%s, width=%s, height=%s)' % (self.__class__.__name__, self._left, self._top, self._width, self._height)


    def __str__(self):
        """Return a string representation of this Rect object."""
        return '(x=%s, y=%s, w=%s, h=%s)' % (self._left, self._top, self._width, self._height)


    def callOnChange(self, oldLeft, oldTop, oldWidth, oldHeight):
        # Note: callOnChange() should be called *after* the attribute has been changed.
        # Note: This isn't thread safe; the attributes can change between the calling of this function and the code in the function running.
        if self.onChange is not None:
            self.onChange(Box(oldLeft, oldTop, oldWidth, oldHeight), Box(self._left, self._top, self._width, self._height))


    @property
    def enableFloat(self):
        """
        A Boolean attribute that determines if this rectangle uses floating point
        numbers for its position and size. False, by default.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.enableFloat
        False
        >>> r.enableFloat = True
        >>> r.top = 3.14
        >>> r
        Rect(left=0.0, top=3.14, width=10.0, height=20.0)
        """
        return self._enableFloat

    @enableFloat.setter
    def enableFloat(self, value):
        if not isinstance(value, bool):
            raise PyRectException('enableFloat must be set to a bool value')
        self._enableFloat = value

        if self._enableFloat:
            self._left = float(self._left)
            self._top = float(self._top)
            self._width = float(self._width)
            self._height = float(self._height)
        else:
            self._left = int(self._left)
            self._top = int(self._top)
            self._width = int(self._width)
            self._height = int(self._height)


    # LEFT SIDE PROPERTY
    @property
    def left(self):
        """
        The x coordinate for the left edge of the rectangle. `x` is an alias for `left`.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.left
        0
        >>> r.left = 50
        >>> r
        Rect(left=50, top=0, width=10, height=20)
        """
        if self.onRead is not None:
            self.onRead(LEFT)
        return self._left

    @left.setter
    def left(self, newLeft):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForIntOrFloat(newLeft)
        if newLeft != self._left: # Only run this code if the size/position has changed.
            originalLeft = self._left
            if self._enableFloat:
                self._left = newLeft
            else:
                self._left = int(newLeft)
            self.callOnChange(originalLeft, self._top, self._width, self._height)

    x = left # x is an alias for left


    # TOP SIDE PROPERTY
    @property
    def top(self):
        """
        The y coordinate for the top edge of the rectangle. `y` is an alias for `top`.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.top
        0
        >>> r.top = 50
        >>> r
        Rect(left=0, top=50, width=10, height=20)
        """
        if self.onRead is not None:
            self.onRead(TOP)
        return self._top

    @top.setter
    def top(self, newTop):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForIntOrFloat(newTop)
        if newTop != self._top: # Only run this code if the size/position has changed.
            originalTop = self._top
            if self._enableFloat:
                self._top = newTop
            else:
                self._top = int(newTop)
            self.callOnChange(self._left, originalTop, self._width, self._height)

    y = top # y is an alias for top


    # RIGHT SIDE PROPERTY
    @property
    def right(self):
        """
        The x coordinate for the right edge of the rectangle.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.right
        10
        >>> r.right = 50
        >>> r
        Rect(left=40, top=0, width=10, height=20)
        """
        if self.onRead is not None:
            self.onRead(RIGHT)
        return self._left + self._width

    @right.setter
    def right(self, newRight):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForIntOrFloat(newRight)
        if newRight != self._left + self._width: # Only run this code if the size/position has changed.
            originalLeft = self._left
            if self._enableFloat:
                self._left = newRight - self._width
            else:
                self._left = int(newRight) - self._width
            self.callOnChange(originalLeft, self._top, self._width, self._height)


    # BOTTOM SIDE PROPERTY
    @property
    def bottom(self):
        """The y coordinate for the bottom edge of the rectangle.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.bottom
        20
        >>> r.bottom = 30
        >>> r
        Rect(left=0, top=10, width=10, height=20)
        """
        if self.onRead is not None:
            self.onRead(BOTTOM)
        return self._top + self._height

    @bottom.setter
    def bottom(self, newBottom):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForIntOrFloat(newBottom)
        if newBottom != self._top + self._height: # Only run this code if the size/position has changed.
            originalTop = self._top
            if self._enableFloat:
                self._top = newBottom - self._height
            else:
                self._top = int(newBottom) - self._height
            self.callOnChange(self._left, originalTop, self._width, self._height)


    # TOP LEFT CORNER PROPERTY
    @property
    def topleft(self):
        """
        The x and y coordinates for the top right corner of the rectangle, as a tuple.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.topleft
        (0, 0)
        >>> r.topleft = (30, 30)
        >>> r
        Rect(left=30, top=30, width=10, height=20)
        """
        if self.onRead is not None:
            self.onRead(TOPLEFT)
        return Point(x=self._left, y=self._top)

    @topleft.setter
    def topleft(self, value):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForTwoIntOrFloatTuple(value)
        newLeft, newTop = value
        if (newLeft != self._left) or (newTop != self._top): # Only run this code if the size/position has changed.
            originalLeft = self._left
            originalTop = self._top
            if self._enableFloat:
                self._left = newLeft
                self._top = newTop
            else:
                self._left = int(newLeft)
                self._top = int(newTop)
            self.callOnChange(originalLeft, originalTop, self._width, self._height)


    # BOTTOM LEFT CORNER PROPERTY
    @property
    def bottomleft(self):
        """
        The x and y coordinates for the bottom right corner of the rectangle, as a tuple.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.bottomleft
        (0, 20)
        >>> r.bottomleft = (30, 30)
        >>> r
        Rect(left=30, top=10, width=10, height=20)
        """
        if self.onRead is not None:
            self.onRead(BOTTOMLEFT)
        return Point(x=self._left, y=self._top + self._height)

    @bottomleft.setter
    def bottomleft(self, value):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForTwoIntOrFloatTuple(value)
        newLeft, newBottom = value
        if (newLeft != self._left) or (newBottom != self._top + self._height): # Only run this code if the size/position has changed.
            originalLeft = self._left
            originalTop = self._top
            if self._enableFloat:
                self._left = newLeft
                self._top = newBottom - self._height
            else:
                self._left = int(newLeft)
                self._top = int(newBottom) - self._height
            self.callOnChange(originalLeft, originalTop, self._width, self._height)


    # TOP RIGHT CORNER PROPERTY
    @property
    def topright(self):
        """
        The x and y coordinates for the top right corner of the rectangle, as a tuple.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.topright
        (10, 0)
        >>> r.topright = (30, 30)
        >>> r
        Rect(left=20, top=30, width=10, height=20)
        """
        if self.onRead is not None:
            self.onRead(TOPRIGHT)
        return Point(x=self._left + self._width, y=self._top)

    @topright.setter
    def topright(self, value):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForTwoIntOrFloatTuple(value)
        newRight, newTop = value
        if (newRight != self._left + self._width) or (newTop != self._top): # Only run this code if the size/position has changed.
            originalLeft = self._left
            originalTop = self._top
            if self._enableFloat:
                self._left = newRight - self._width
                self._top = newTop
            else:
                self._left = int(newRight) - self._width
                self._top = int(newTop)
            self.callOnChange(originalLeft, originalTop, self._width, self._height)


    # BOTTOM RIGHT CORNER PROPERTY
    @property
    def bottomright(self):
        """
        The x and y coordinates for the bottom right corner of the rectangle, as a tuple.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.bottomright
        (10, 20)
        >>> r.bottomright = (30, 30)
        >>> r
        Rect(left=20, top=10, width=10, height=20)
        """
        if self.onRead is not None:
            self.onRead(BOTTOMRIGHT)
        return Point(x=self._left + self._width, y=self._top + self._height)

    @bottomright.setter
    def bottomright(self, value):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForTwoIntOrFloatTuple(value)
        newRight, newBottom = value
        if (newBottom != self._top + self._height) or (newRight != self._left + self._width): # Only run this code if the size/position has changed.
            originalLeft = self._left
            originalTop = self._top
            if self._enableFloat:
                self._left = newRight - self._width
                self._top = newBottom - self._height
            else:
                self._left = int(newRight) - self._width
                self._top = int(newBottom) - self._height
            self.callOnChange(originalLeft, originalTop, self._width, self._height)


    # MIDDLE OF TOP SIDE PROPERTY
    @property
    def midtop(self):
        """
        The x and y coordinates for the midpoint of the top edge of the rectangle, as a tuple.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.midtop
        (5, 0)
        >>> r.midtop = (40, 50)
        >>> r
        Rect(left=35, top=50, width=10, height=20)
        """
        if self.onRead is not None:
            self.onRead(MIDTOP)
        if self._enableFloat:
            return Point(x=self._left + (self._width / 2.0), y=self._top)
        else:
            return Point(x=self._left + (self._width // 2), y=self._top)

    @midtop.setter
    def midtop(self, value):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForTwoIntOrFloatTuple(value)
        newMidTop, newTop = value
        originalLeft = self._left
        originalTop = self._top
        if self._enableFloat:
            if (newMidTop != self._left + self._width / 2.0) or (newTop != self._top): # Only run this code if the size/position has changed.
                self._left = newMidTop - (self._width / 2.0)
                self._top = newTop
                self.callOnChange(originalLeft, originalTop, self._width, self._height)
        else:
            if (newMidTop != self._left + self._width // 2) or (newTop != self._top): # Only run this code if the size/position has changed.
                self._left = int(newMidTop) - (self._width // 2)
                self._top = int(newTop)
                self.callOnChange(originalLeft, originalTop, self._width, self._height)


    # MIDDLE OF BOTTOM SIDE PROPERTY
    @property
    def midbottom(self):
        """
        The x and y coordinates for the midpoint of the bottom edge of the rectangle, as a tuple.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.midbottom
        (5, 20)
        >>> r.midbottom = (40, 50)
        >>> r
        Rect(left=35, top=30, width=10, height=20)
        """
        if self.onRead is not None:
            self.onRead(MIDBOTTOM)
        if self._enableFloat:
            return Point(x=self._left + (self._width / 2.0), y=self._top + self._height)
        else:
            return Point(x=self._left + (self._width // 2), y=self._top + self._height)

    @midbottom.setter
    def midbottom(self, value):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForTwoIntOrFloatTuple(value)
        newMidBottom, newBottom = value
        originalLeft = self._left
        originalTop = self._top
        if self._enableFloat:
            if (newMidBottom != self._left + self._width / 2.0) or (newBottom != self._top + self._height): # Only run this code if the size/position has changed.
                self._left = newMidBottom - (self._width / 2.0)
                self._top = newBottom - self._height
                self.callOnChange(originalLeft, originalTop, self._width, self._height)
        else:
            if (newMidBottom != self._left + self._width // 2) or (newBottom != self._top + self._height): # Only run this code if the size/position has changed.
                self._left = int(newMidBottom) - (self._width // 2)
                self._top = int(newBottom) - self._height
                self.callOnChange(originalLeft, originalTop, self._width, self._height)


    # MIDDLE OF LEFT SIDE PROPERTY
    @property
    def midleft(self):
        """
        The x and y coordinates for the midpoint of the left edge of the rectangle, as a tuple.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.midleft
        (0, 10)
        >>> r.midleft = (40, 50)
        >>> r
        Rect(left=40, top=40, width=10, height=20)
        """
        if self.onRead is not None:
            self.onRead(MIDLEFT)
        if self._enableFloat:
            return Point(x=self._left, y=self._top + (self._height / 2.0))
        else:
            return Point(x=self._left, y=self._top + (self._height // 2))


    @midleft.setter
    def midleft(self, value):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForTwoIntOrFloatTuple(value)
        newLeft, newMidLeft = value
        originalLeft = self._left
        originalTop = self._top
        if self._enableFloat:
            if (newLeft != self._left) or (newMidLeft != self._top + (self._height / 2.0)): # Only run this code if the size/position has changed.
                self._left = newLeft
                self._top = newMidLeft - (self._height / 2.0)
                self.callOnChange(originalLeft, originalTop, self._width, self._height)
        else:
            if (newLeft != self._left) or (newMidLeft != self._top + (self._height // 2)): # Only run this code if the size/position has changed.
                self._left = int(newLeft)
                self._top = int(newMidLeft) - (self._height // 2)
                self.callOnChange(originalLeft, originalTop, self._width, self._height)


    # MIDDLE OF RIGHT SIDE PROPERTY
    @property
    def midright(self):
        """
        The x and y coordinates for the midpoint of the right edge of the rectangle, as a tuple.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.midright
        (10, 10)
        >>> r.midright = (40, 50)
        >>> r
        Rect(left=30, top=40, width=10, height=20)
        """
        if self.onRead is not None:
            self.onRead(MIDRIGHT)
        if self._enableFloat:
            return Point(x=self._left + self._width, y=self._top + (self._height / 2.0))
        else:
            return Point(x=self._left + self._width, y=self._top + (self._height // 2))


    @midright.setter
    def midright(self, value):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForTwoIntOrFloatTuple(value)
        newRight, newMidRight = value
        originalLeft = self._left
        originalTop = self._top
        if self._enableFloat:
            if (newRight != self._left + self._width) or (newMidRight != self._top + self._height / 2.0): # Only run this code if the size/position has changed.
                self._left = newRight - self._width
                self._top = newMidRight - (self._height / 2.0)
                self.callOnChange(originalLeft, originalTop, self._width, self._height)
        else:
            if (newRight != self._left + self._width) or (newMidRight != self._top + self._height // 2): # Only run this code if the size/position has changed.
                self._left = int(newRight) - self._width
                self._top = int(newMidRight) - (self._height // 2)
                self.callOnChange(originalLeft, originalTop, self._width, self._height)


    # CENTER POINT PROPERTY
    @property
    def center(self):
        """
        The x and y coordinates for the center of the rectangle, as a tuple.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.center
        (5, 10)
        >>> r.center = (40, 50)
        >>> r
        Rect(left=35, top=40, width=10, height=20)
        """
        if self.onRead is not None:
            self.onRead(CENTER)
        if self._enableFloat:
            return Point(x=self._left + (self._width / 2.0), y=self._top + (self._height / 2.0))
        else:
            return Point(x=self._left + (self._width // 2), y=self._top + (self._height // 2))

    @center.setter
    def center(self, value):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForTwoIntOrFloatTuple(value)
        newCenterx, newCentery = value
        originalLeft = self._left
        originalTop = self._top
        if self._enableFloat:
            if (newCenterx != self._left + self._width / 2.0) or (newCentery != self._top + self._height / 2.0): # Only run this code if the size/position has changed.
                self._left = newCenterx - (self._width / 2.0)
                self._top = newCentery - (self._height / 2.0)
                self.callOnChange(originalLeft, originalTop, self._width, self._height)
        else:
            if (newCenterx != self._left + self._width // 2) or (newCentery != self._top + self._height // 2): # Only run this code if the size/position has changed.
                self._left = int(newCenterx) - (self._width // 2)
                self._top = int(newCentery) - (self._height // 2)
                self.callOnChange(originalLeft, originalTop, self._width, self._height)


    # X COORDINATE OF CENTER POINT PROPERTY
    @property
    def centerx(self):
        """
        The x coordinate for the center of the rectangle, as a tuple.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.centerx
        5
        >>> r.centerx = 50
        >>> r
        Rect(left=45, top=0, width=10, height=20)
        """
        if self.onRead is not None:
            self.onRead(CENTERX)
        if self._enableFloat:
            return self._left + (self._width / 2.0)
        else:
            return self._left + (self._width // 2)

    @centerx.setter
    def centerx(self, newCenterx):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForIntOrFloat(newCenterx)
        originalLeft = self._left
        if self._enableFloat:
            if (newCenterx != self._left + self._width / 2.0): # Only run this code if the size/position has changed.
                self._left = newCenterx - (self._width / 2.0)
                self.callOnChange(originalLeft, self._top, self._width, self._height)
        else:
            if (newCenterx != self._left + self._width // 2): # Only run this code if the size/position has changed.
                self._left = int(newCenterx) - (self._width // 2)
                self.callOnChange(originalLeft, self._top, self._width, self._height)


    # Y COORDINATE OF CENTER POINT PROPERTY
    @property
    def centery(self):
        """
        The y coordinate for the center of the rectangle, as a tuple.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.centery
        10
        >>> r.centery = 50
        >>> r
        Rect(left=0, top=40, width=10, height=20)
        """
        if self.onRead is not None:
            self.onRead(CENTERY)
        if self._enableFloat:
            return self._top + (self._height / 2.0)
        else:
            return self._top + (self._height // 2)

    @centery.setter
    def centery(self, newCentery):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForIntOrFloat(newCentery)
        originalTop = self._top
        if self._enableFloat:
            if (newCentery != self._top + self._height / 2.0): # Only run this code if the size/position has changed.
                self._top = newCentery - (self._height / 2.0)
                self.callOnChange(self._left, originalTop, self._width, self._height)
        else:
            if (newCentery != self._top + self._height // 2): # Only run this code if the size/position has changed.
                self._top = int(newCentery) - (self._height // 2)
                self.callOnChange(self._left, originalTop, self._width, self._height)


    # SIZE PROPERTY (i.e. (width, height))
    @property
    def size(self):
        """
        The width and height of the rectangle, as a tuple.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.size
        (10, 20)
        >>> r.size = (40, 50)
        >>> r
        Rect(left=0, top=0, width=40, height=50)
        """
        if self.onRead is not None:
            self.onRead(SIZE)
        return Size(width=self._width, height=self._height)

    @size.setter
    def size(self, value):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForTwoIntOrFloatTuple(value)
        newWidth, newHeight = value
        if newWidth != self._width or newHeight != self._height:
            originalWidth = self._width
            originalHeight = self._height
            if self._enableFloat:
                self._width = newWidth
                self._height = newHeight
            else:
                self._width = int(newWidth)
                self._height = int(newHeight)
            self.callOnChange(self._left, self._top, originalWidth, originalHeight)


    # WIDTH PROPERTY
    @property
    def width(self):
        """
        The width of the rectangle. `w` is an alias for `width`.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.width
        10
        >>> r.width = 50
        >>> r
        Rect(left=0, top=0, width=50, height=20)
        """
        if self.onRead is not None:
            self.onRead(WIDTH)
        return self._width

    @width.setter
    def width(self, newWidth):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForIntOrFloat(newWidth)
        if (newWidth != self._width): # Only run this code if the size/position has changed.
            originalWidth = self._width
            if self._enableFloat:
                    self._width = newWidth
            else:
                self._width = int(newWidth)
            self.callOnChange(self._left, self._top, originalWidth, self._height)

    w = width


    # HEIGHT PROPERTY
    @property
    def height(self):
        """
        The height of the rectangle. `h` is an alias for `height`

        >>> r = Rect(0, 0, 10, 20)
        >>> r.height
        20
        >>> r.height = 50
        >>> r
        Rect(left=0, top=0, width=10, height=50)
        """
        if self.onRead is not None:
            self.onRead(HEIGHT)
        return self._height

    @height.setter
    def height(self, newHeight):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForIntOrFloat(newHeight)
        if (newHeight != self._height): # Only run this code if the size/position has changed.
            originalHeight = self._height
            if self._enableFloat:
                self._height = newHeight
            else:
                self._height = int(newHeight)
            self.callOnChange(self._left, self._top, self._width, originalHeight)

    h = height


    # AREA PROPERTY
    @property
    def area(self):
        """The area of the `Rect`, which is simply the width times the height.
        This is a read-only attribute.

        >>> r = Rect(0, 0, 10, 20)
        >>> r.area
        200
        """
        if self.onRead is not None:
            self.onRead(AREA)
        return self._width * self._height


    # BOX PROPERTY
    @property
    def box(self):
        """A tuple of four integers: (left, top, width, height).

        >>> r = Rect(0, 0, 10, 20)
        >>> r.box
        (0, 0, 10, 20)
        >>> r.box = (5, 15, 100, 200)
        >>> r.box
        (5, 15, 100, 200)"""
        if self.onRead is not None:
            self.onRead(BOX)
        return Box(left=self._left, top=self._top, width=self._width, height=self._height)

    @box.setter
    def box(self, value):
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForFourIntOrFloatTuple(value)
        newLeft, newTop, newWidth, newHeight = value
        if (newLeft != self._left) or (newTop != self._top) or (newWidth != self._width) or (newHeight != self._height):
            originalLeft = self._left
            originalTop = self._top
            originalWidth = self._width
            originalHeight = self._height
            if self._enableFloat:
                self._left   = float(newLeft)
                self._top    = float(newTop)
                self._width  = float(newWidth)
                self._height = float(newHeight)
            else:
                self._left   = int(newLeft)
                self._top    = int(newTop)
                self._width  = int(newWidth)
                self._height = int(newHeight)
            self.callOnChange(originalLeft, originalTop, originalWidth, originalHeight)


    def get(self, rectAttrName):
        # Access via the properties so that it triggers onRead().
        if rectAttrName == TOP:
            return self.top
        elif rectAttrName == BOTTOM:
            return self.bottom
        elif rectAttrName == LEFT:
            return self.left
        elif rectAttrName == RIGHT:
            return self.right
        elif rectAttrName == TOPLEFT:
            return self.topleft
        elif rectAttrName == TOPRIGHT:
            return self.topright
        elif rectAttrName == BOTTOMLEFT:
            return self.bottomleft
        elif rectAttrName == BOTTOMRIGHT:
            return self.bottomright
        elif rectAttrName == MIDTOP:
            return self.midtop
        elif rectAttrName == MIDBOTTOM:
            return self.midbottom
        elif rectAttrName == MIDLEFT:
            return self.midleft
        elif rectAttrName == MIDRIGHT:
            return self.midright
        elif rectAttrName == CENTER:
            return self.center
        elif rectAttrName == CENTERX:
            return self.centerx
        elif rectAttrName == CENTERY:
            return self.centery
        elif rectAttrName == WIDTH:
            return self.width
        elif rectAttrName == HEIGHT:
            return self.height
        elif rectAttrName == SIZE:
            return self.size
        elif rectAttrName == AREA:
            return self.area
        elif rectAttrName == BOX:
            return self.box
        else:
            raise PyRectException("'%s' is not a valid attribute name" % (rectAttrName))


    def set(self, rectAttrName, value):
        # Set via the properties so that it triggers onChange().
        if rectAttrName == TOP:
            self.top = value
        elif rectAttrName == BOTTOM:
            self.bottom = value
        elif rectAttrName == LEFT:
            self.left = value
        elif rectAttrName == RIGHT:
            self.right = value
        elif rectAttrName == TOPLEFT:
            self.topleft = value
        elif rectAttrName == TOPRIGHT:
            self.topright = value
        elif rectAttrName == BOTTOMLEFT:
            self.bottomleft = value
        elif rectAttrName == BOTTOMRIGHT:
            self.bottomright = value
        elif rectAttrName == MIDTOP:
            self.midtop = value
        elif rectAttrName == MIDBOTTOM:
            self.midbottom = value
        elif rectAttrName == MIDLEFT:
            self.midleft = value
        elif rectAttrName == MIDRIGHT:
            self.midright = value
        elif rectAttrName == CENTER:
            self.center = value
        elif rectAttrName == CENTERX:
            self.centerx = value
        elif rectAttrName == CENTERY:
            self.centery = value
        elif rectAttrName == WIDTH:
            self.width = value
        elif rectAttrName == HEIGHT:
            self.height = value
        elif rectAttrName == SIZE:
            self.size = value
        elif rectAttrName == AREA:
            raise PyRectException('area is a read-only attribute')
        elif rectAttrName == BOX:
            self.box = value
        else:
            raise PyRectException("'%s' is not a valid attribute name" % (rectAttrName))


    def move(self, xOffset, yOffset):
        """Moves this Rect object by the given offsets. The xOffset and yOffset
        arguments can be any integer value, positive or negative.
        >>> r = Rect(0, 0, 100, 100)
        >>> r.move(10, 20)
        >>> r
        Rect(left=10, top=20, width=100, height=100)
        """
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        _checkForIntOrFloat(xOffset)
        _checkForIntOrFloat(yOffset)
        if self._enableFloat:
            self._left += xOffset
            self._top += yOffset
        else:
            self._left += int(xOffset)
            self._top += int(yOffset)


    def copy(self):
        """Return a copied `Rect` object with the same position and size as this
        `Rect` object.

        >>> r1 = Rect(0, 0, 100, 150)
        >>> r2 = r1.copy()
        >>> r1 == r2
        True
        >>> r2
        Rect(left=0, top=0, width=100, height=150)
        """
        return Rect(self._left, self._top, self._width, self._height, self._enableFloat, self._readOnly)


    def inflate(self, widthChange=0, heightChange=0):
        """Increases the size of this Rect object by the given offsets. The
        rectangle's center doesn't move. Negative values will shrink the
        rectangle.

        >>> r = Rect(0, 0, 100, 150)
        >>> r.inflate(20, 40)
        >>> r
        Rect(left=-10, top=-20, width=120, height=190)
        """
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        originalCenter = self.center
        self.width += widthChange
        self.height += heightChange
        self.center = originalCenter


    def clamp(self, otherRect):
        """Centers this Rect object at the center of otherRect.

        >>> r1 =Rect(0, 0, 100, 100)
        >>> r2 = Rect(-20, -90, 50, 50)
        >>> r2.clamp(r1)
        >>> r2
        Rect(left=25, top=25, width=50, height=50)
        >>> r1.center == r2.center
        True
        """
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        self.center = otherRect.center

    '''
    def intersection(self, otherRect):
        """Returns a new Rect object of the overlapping area between this
        Rect object and otherRect.

        `clip()` is an alias for `intersection()`.
        """
        pass

    clip = intersection
    '''

    def union(self, otherRect):
        """Adjusts the width and height to also cover the area of `otherRect`.

        >>> r1 = Rect(0, 0, 100, 100)
        >>> r2 = Rect(-10, -10, 100, 100)
        >>> r1.union(r2)
        >>> r1
        Rect(left=-10, top=-10, width=110, height=110)
        """

        # TODO - Change otherRect so that it could be a point as well.

        unionLeft   = min(self._left, otherRect._left)
        unionTop    = min(self._top, otherRect._top)
        unionRight  = max(self.right, otherRect.right)
        unionBottom = max(self.bottom, otherRect.bottom)

        self._left   = unionLeft
        self._top    = unionTop
        self._width  = unionRight - unionLeft
        self._height = unionBottom - unionTop


    def unionAll(self, otherRects):
        """Adjusts the width and height to also cover all the `Rect` objects in
        the `otherRects` sequence.

        >>> r = Rect(0, 0, 100, 100)
        >>> r1 = Rect(0, 0, 150, 100)
        >>> r2 = Rect(-10, -10, 100, 100)
        >>> r.unionAll([r1, r2])
        >>> r
        Rect(left=-10, top=-10, width=160, height=110)
        """

        # TODO - Change otherRect so that it could be a point as well.

        otherRects = list(otherRects)
        otherRects.append(self)

        unionLeft   = min([r._left  for r in otherRects])
        unionTop    = min([r._top   for r in otherRects])
        unionRight  = max([r.right  for r in otherRects])
        unionBottom = max([r.bottom for r in otherRects])

        self._left   = unionLeft
        self._top    = unionTop
        self._width  = unionRight - unionLeft
        self._height = unionBottom - unionTop

    """
    def fit(self, other):
        pass # TODO - needs to be complete
    """


    def normalize(self):
        """Rect objects with a negative width or height cover a region where the
        right/bottom edge is to the left/above of the left/top edge, respectively.
        The `normalize()` method sets the `width` and `height` to positive if they
        were negative.

        The Rect stays in the same place, though with the `top` and `left`
        attributes representing the true top and left side.

        >>> r = Rect(0, 0, -10, -20)
        >>> r.normalize()
        >>> r
        Rect(left=-10, top=-20, width=10, height=20)
        """
        if self._readOnly:
            raise PyRectException('Rect object is read-only')

        if self._width < 0:
            self._width = -self._width
            self._left -= self._width
        if self._height < 0:
            self._height = -self._height
            self._top -= self._height
        # Note: No need to intify here, since the four attributes should already be ints and no multiplication was done.


    def __contains__(self, value): # for either points or other Rect objects. For Rects, the *entire* Rect must be in this Rect.
        if isinstance(value, Rect):
            return value.topleft in self and value.topright in self and value.bottomleft in self and value.bottomright in self

        # Check if value is an (x, y) sequence or a (left, top, width, height) sequence.
        try:
            len(value)
        except:
            raise PyRectException('in <Rect> requires an (x, y) tuple, a (left, top, width, height) tuple, or a Rect object as left operand, not %s' % (value.__class__.__name__))

        if len(value) == 2:
            # Assume that value is an (x, y) sequence.
            _checkForTwoIntOrFloatTuple(value)
            x, y = value
            return self._left < x < self._left + self._width and self._top < y < self._top + self._height

        elif len(value) == 4:
            # Assume that value is an (x, y) sequence.
            _checkForFourIntOrFloatTuple(value)
            left, top, width, height = value
            return (left, top) in self and (left + width, top) in self and (left, top + height) in self and (left + width, top + height) in self
        else:
            raise PyRectException('in <Rect> requires an (x, y) tuple, a (left, top, width, height) tuple, or a Rect object as left operand, not %s' % (value.__class__.__name__))


    def collide(self, value):
        """Returns `True` if value collides with this `Rect` object, where value can
        be an (x, y) tuple, a (left, top, width, height) box tuple, or another `Rect`
        object. If value represents a rectangular area, any part of that area
        can collide with this `Rect` object to make `collide()` return `True`.
        Otherwise, returns `False`."""

        # Note: This code is similar to __contains__(), with some minor changes
        # because __contains__() requires the rectangular are to be COMPELTELY
        # within the Rect object.
        if isinstance(value, Rect):
            return value.topleft in self or value.topright in self or value.bottomleft in self or value.bottomright in self

        # Check if value is an (x, y) sequence or a (left, top, width, height) sequence.
        try:
            len(value)
        except:
            raise PyRectException('in <Rect> requires an (x, y) tuple, a (left, top, width, height) tuple, or a Rect object as left operand, not %s' % (value.__class__.__name__))

        if len(value) == 2:
            # Assume that value is an (x, y) sequence.
            _checkForTwoIntOrFloatTuple(value)
            x, y = value
            return self._left < x < self._left + self._width and self._top < y < self._top + self._height

        elif len(value) == 4:
            # Assume that value is an (x, y) sequence.
            left, top, width, height = value
            return (left, top) in self or (left + width, top) in self or (left, top + height) in self or (left + width, top + height) in self
        else:
            raise PyRectException('in <Rect> requires an (x, y) tuple, a (left, top, width, height) tuple, or a Rect object as left operand, not %s' % (value.__class__.__name__))


    '''
    def collideAny(self, rectsOrPoints):
        """Returns True if any of the (x, y) or (left, top, width, height)
        tuples in rectsOrPoints is inside this Rect object.

        >> r = Rect(0, 0, 100, 100)
        >> p1 = (150, 80)
        >> p2 = (100, 100) # This point collides.
        >> r.collideAny([p1, p2])
        True
        >> r1 = Rect(50, 50, 10, 20) # This Rect collides.
        >> r.collideAny([r1])
        True
        >> r.collideAny([p1, p2, r1])
        True
        """
        # TODO - needs to be complete
        pass # returns True or False
        raise NotImplementedError
'''

    '''
    def collideAll(self, rectsOrPoints):
        """Returns True if all of the (x, y) or (left, top, width, height)
        tuples in rectsOrPoints is inside this Rect object.
        """

        pass # return a list of all rects or points that collide with any other in the argument
        raise NotImplementedError
'''

    # TODO - Add overloaded operators for + - * / and others once we can determine actual use cases for them.

    """NOTE: All of the comparison magic methods compare the box tuple of Rect
    objects. This is the behavior of the pygame Rect objects. Originally,
    I thought about having the <, <=, >, and >= operators compare the area
    of Rect objects. But at the same time, I wanted to have == and != compare
    not just area, but all four left, top, width, and height attributes.
    But that's weird to have different comparison operators comparing different
    features of a rectangular area. So I just defaulted to what Pygame does
    and compares the box tuple. This means that the == and != operators are
    the only really useful comparison operators, so I decided to ditch the
    other operators altogether and just have Rect only support == and !=.
    """

    def __eq__(self, other):
        if isinstance(other, Rect):
            return other.box == self.box
        else:
            raise PyRectException('Rect objects can only be compared with other Rect objects')


    def __ne__(self, other):
        if isinstance(other, Rect):
            return other.box != self.box
        else:
            raise PyRectException('Rect objects can only be compared with other Rect objects')



if __name__ == '__main__':
    print(doctest.testmod())