def test_area(self): print("Testing area method") A = Rectangle(0, 0, -3.5, 0.0, -3.5, -7.8, 0, -7.8) B = Rectangle(0, 0.0, -5, 0.0, -5, -5, 0, -5) self.assertEqual(A.area(), 27.3) self.assertEqual(B.area(), 25) self.assertNotEqual(B.area(), 0) self.assertEqual(Rectangle(8, 8, 3, 8, 3, 5, 8, 5).area(), 15) print("Done testing area method successfully")
def traverse_to(level, key): page = self.db.get(key) min_lat, min_lon, max_lat, max_lon, pickled_ids = page.split(":") page_bounds = BBox( min_lat=float(min_lat), min_lon=float(min_lon), max_lat=float(max_lat), max_lon=float(max_lon) ) ids = pickle.loads(pickled_ids) if level == 0: # We're at the best leaf, add it if node.id in ids: print "Warning: not adding duplicate id to spatial index" return # Get the potentially expanded bounds rect = Rectangle() rect.node_ids = ids rect.min_lat = float(min_lat) rect.min_lon = float(min_lon) rect.max_lat = float(max_lat) rect.max_lon = float(max_lon) rect.include(node.lat, node.lon, node.id) # Update the record self.db.put(key, str(rect)) else: # We're at an intermediate node, keep going best = None for id in ids: page = self.db.get(id) min_lat, min_lon, max_lat, max_lon, pickled_ids = page.split(":") rect = Rectangle() rect.node_ids = pickle.loads(pickled_ids) rect.min_lat = float(min_lat) rect.min_lon = float(min_lon) rect.max_lat = float(max_lat) rect.max_lon = float(max_lon) area_before = rect.area() rect.include(node.lat, node.lon) area_after = rect.area() growth = area_after - area_before if not best or growth < best[1] or (growth == best[1] and area_after < best[2]): best = (id, growth, area_after, rect) if best[1] > 1e-8: # This rectangle needs to grow self.db.put(best[0], str(best[3])) traverse_to(level - 1, best[0])
class TestRectangle(TestCase): def setUp(self) -> None: self.r1 = Rectangle(6, 4, 2, 4, 2, 1, 6, 1) self.r2 = Rectangle(6, 4, 2, 4, 2, 1, 6, 1) self.r3 = Rectangle(5, 3, 1, 3, 1, 0, 5, 0) def test_eq(self): self.assertTrue(self.r1.__eq__(self.r2)) self.assertFalse(self.r1.__eq__(self.r3)) self.assertFalse(self.r3.__eq__(self.r2)) def test_ne(self): self.assertTrue(self.r1.__ne__(self.r3)) self.assertTrue(self.r2.__ne__(self.r3)) self.assertFalse(self.r1.__ne__(self.r2)) def test_str(self): self.assertEqual(self.r1.__str__(), "(6, 4), (2, 4), (2, 1), (6, 1)") def test_center(self): self.assertEqual(self.r1.center(), TwoDPoint(4, 2.5)) def test_area(self): self.assertEqual(self.r1.area(), 12) def test__is_member(self): self.assertRaises(TypeError, (Rectangle, (6, 4, 2, 4, 2, 1, 6, 1))) self.assertRaises(TypeError, (Rectangle, ))
def main(args): length = int(args[1]) breadth = int(args[2]) print("length and breadth of rectangle is %d and %d" % (length, breadth)) r1 = Rectangle(length, breadth) print("Perimeter: ", r1.perimeter()) print("Area: ", r1.area())
def main(): points = [] points.append(Point(0, 0)) points.append(Point(0, 5)) points.append(Point(3, 0)) points.append(Point(3, 5)) rectangle1 = Rectangle(points) print("The width of the rectangle is:", rectangle1.width()) print("The height of the rectangle is:", rectangle1.height()) print(rectangle1.area(), "\n") points = [] points.append(Point(0, 0)) points.append(Point(0, 9)) points.append(Point(6, 0)) points.append(Point(6, 9)) rectangle2 = Rectangle(points) print("The width of the rectangle is:", rectangle2.width()) print("The height of the rectangle is:", rectangle2.height()) print(rectangle2.area())
def main(cls, arg): width = 2 length = 3 radius = 10 rectangle = Rectangle(width, length) print("Rectangle with:", width, "Length", length, "Rectangle area", rectangle.area(), rectangle.perimeter()) circle = Circle(radius) print("Circle radius", radius, "Circle area", circle.area, "Circle Perimeter", circle.perimeter())
def containment(rect1: Rectangle, rect2: Rectangle): '''Determine if one rectangle fully contains another''' # Only check if larger contains the smaller. Equal areas cannot be contained if rect1.area() == rect2.area(): return None elif rect1.area() > rect2.area(): large_rect = rect1 small_rect = rect2 else: large_rect = rect2 small_rect = rect1 # Check for containment within X and Y axes hor_contain = (small_rect.left > large_rect.left and small_rect.right < large_rect.right) ver_contain = (small_rect.top > large_rect.top and small_rect.bottom < large_rect.bottom) if hor_contain and ver_contain: return (large_rect, small_rect) return None
class TestRectangle(unittest.TestCase): def setUp(self) -> None: self.r1 = Rectangle(2, 1, 0, 1, 0, 0, 2, 0) self.r2 = Rectangle(2, 2, 0, 2, 0, 0, 2, 0) def test_get__is_member(self): self.assertEqual(self.r1.get_is_member(), True) self.assertEqual(self.r2.get_is_member(), True) def test_center(self): self.assertEqual(self.r1.center(), TwoDPoint(1, .5)) self.assertEqual(self.r2.center(), TwoDPoint(1, 1)) def test_area(self): self.assertEqual(self.r1.area(), 2) self.assertEqual(self.r2.area(), 4) def test___eq__(self): self.assertEqual(self.r1 == self.r2, False) self.assertEqual(self.r1 == self.r1, True)
class RectangleTest(unittest.TestCase): def setUp(self): self.rectangle = Rectangle(width=3, height=2) def test_it_has_width_and_height(self): self.rectangle.width |should| equal_to(3) self.rectangle.height |should| equal_to(2) def test_it_changes_its_width(self): self.rectangle.width = 5 self.rectangle.width |should| equal_to(5) def test_it_changes_its_height(self): self.rectangle.height = 5 self.rectangle.height |should| equal_to(5) def test_it_calculates_its_area(self): self.rectangle.area() |should| equal_to(6) def test_it_calculates_its_perimeter(self): self.rectangle.perimeter() |should| equal_to(10)
def test_area(self): # Test area method rect = Rectangle(3, 3) self.assertEqual(rect.area(), 9)
from rectangle import Rectangle from triangle import Triangle rect = Rectangle() tri = Triangle() rect.set_values(50, 40) tri.set_values(50, 40) rect.set_color('red') tri.set_color('blue') print(rect.area()) print(tri.area()) print(rect.get_color()) print(tri.get_color())
def test_AreaOfARectangleWithTestVectors(): rectangle = Rectangle() assert rectangle.area() == 0 assert rectangle.perimeter() == 0
import circle from rectangle import Rectangle radius = 5 smallCircle = circle.Circle(radius) print("Area of Circle is {}".format(smallCircle.area())) rectangle = Rectangle(6, 5) print("Area os a rectangle is {}".format(rectangle.area()))
def rectangle(base = 0, height = 0): rec = Rectangle(base,height) return "Area of rectangle is {}".format(rec.area())
from rectangle import Rectangle from square import Square _rectangle = Rectangle(5, 6) _square = Square(5) print "rectangle" print ("perimeter " + str(_rectangle.perimeter())) print ("area " + str(_rectangle.area())) print "------------------" print "square" print ("perimeter " + str(_square.perimeter())) print ("area " + str(_square.area())) print "------------------"
def test_AreaOfARectangleWithLength5AndBreadth10(): rectangle = Rectangle(5, 10) assert rectangle.area() == 50, "Value should be 50"
def test_area(self): newRect = Rectangle(2.0, 1.0, 0.0, 1.0, 0.0, 0.0, 2.0, 0.0) self.assertEqual(2.0, newRect.area()) newRect2 = Rectangle(2.0, 2.0, -2.0, 2.0, -2.0, -2.0, 2.0, -2.0) self.assertEqual(16.0, newRect2.area())
def __str__(self): return ('%s: (%d, %d), area=%.2f, s=%d' % (self.name, self.centre_x, self.centre_y, Rectangle.area(self), self.width))
def test_area(self): r1 = Rectangle(0, 4, 6, 4, 6, 0, 0, 0) self.assertTrue(r1.area() == 24)
def test_area(self): r1 = Rectangle(2, 1.0, 0.0, 1.0, 0.0, 0.0, 2.0, 0.0) self.assertEqual(r1.area(), 2.0) r3 = Rectangle(1, 1.0, 0, 1, 0, 0, 1, 0) # Square self.assertEqual(r3.area(), 1.0)
def test_area(self): a = Rectangle(5, 1, 1, 1, 1, 0, 5, 0) self.assertEqual(a.area(), 4)
def test_AreaOfARectangleWithDefaultSide(): rectangle = Rectangle() assert rectangle.area() == 50, "Value should be 50"
from rectangle import Rectangle ob = Rectangle(1, 2) ob1 = Rectangle(3, 4) ob.area()
def test_CheckForPythonVersion(): rectangle = Rectangle() assert rectangle.area() == 50, "Value should be 50"
from triangle import Triangle from rectangle import Rectangle from Shape import Shape rect = Rectangle() tri = Triangle() rect.set_values(70, 40) tri.set_values(50, 40) rect.set_color('Red') tri.set_color('blue') print(rect.area(), " Area of Rectangle") print(tri.area(), " Area of Triangle") print(rect.get_color()) print(tri.get_color())
from rectangle import Rectangle ######################################################## # valid values ######################################################## r = Rectangle() print(r) r.setData(3, 4) print (r) print (r.area()) ######################################################## # test the methods with invalid values ######################################################## rec_01 = Rectangle() try: rec_01.setData("3", "error") except ValueError: print ("can't set the Rectangle to a negative value") except TypeError: print ("can't set the Rectangle to a non-integer value") print(rec_01) print (rec_01.area()) ######################################################## # test the methods with string values ######################################################## rec_01 = Rectangle() try:
from rectangle import Rectangle from triangle import Triangle rec = Rectangle() rec.setValues(50, 40) rec.setColor("Black") print(rec.area()) print(rec.getColor()) print("--------------------------") tri = Triangle() tri.setValues(50, 40) tri.setColor("Red") print(tri.area()) print(tri.getColor())
def test_AreaOfARectangleWithNonDefaultSide(): rectangle = Rectangle(10, 20) assert rectangle.area() == 200, "Value should be 200"
from square import Square from rectangle import Rectangle # an abstract class with an abstract method cannot be instantiated # cannot create an object s = Square(4) r = Rectangle(6, 3) print(s.area()) print(r.area()) # Square IS-A Shape # Rectangle IS-a Shape
def testImageSingleObject(imgPath, training_data, present_items, k, gt_box, params): target = present_items[k] others = [name for name in present_items if name != target] print("Training for {}".format(target)) print("Negative examples: {}".format(others)) if target not in training_data: # No positive examples! return [] positive = training_data[target] negative = [] if params['tailor']: for name, data in training_data.items(): if name in others: print("Negative: {} ({} samples)".format(name, len(data))) negative += data print("Absolute negatives: {}".format(len(training_data['negative']))) negative += training_data['negative'] else: for name, data in training_data.items(): if name != target: negative += data print("X vstack (pos: {}, neg: {})".format(len(positive), len(negative))) X = np.vstack((np.array(positive), np.array(negative))) print("y vstack") y = np.hstack((np.repeat(1, len(positive)), np.repeat(-1, len(negative)))) if params['balance']: balancing = 'balanced' else: balancing = None clf = LinearSVC(class_weight=balancing, C=params['svm_c']) print("Training with {} samples".format(X.shape)) clf.fit(X, y) print("Prediction") img_bboxes = np.fromfile(os.path.join(imgPath, params['bbox_source']), np.float32).reshape((NUM_PROPOSALS, 4)) img_features = np.fromfile(os.path.join(imgPath, 'rgb.png.feat.bin'), np.float32).reshape((NUM_PROPOSALS, 4096)) img_scores = np.fromfile(os.path.join(imgPath, 'rgb.png.score.bin'), np.float32).reshape((NUM_PROPOSALS, 1)) boxes = [] for k in range(1000): # Bounding boxes are in xc, yc, w, h format, but in the 180° rotated image bbox_data = WIDTH / 720.0 * img_bboxes[k, :] bbox = Rectangle(WIDTH - 1 - bbox_data[0], HEIGHT - 1 - bbox_data[1], bbox_data[2], bbox_data[3]) # Discard bbox if it does not intersect the box enough if bbox.intersection(gt_box).area() / bbox.area() < 0.85: continue feature_vector = np.hstack((img_features[k, :], img_scores[k])) prob = clf.decision_function(feature_vector.reshape(1, -1)) boxes.append((prob, bbox)) boxes.sort(key=lambda pair: pair[0], reverse=True) return boxes[:3]
""" Program for user to create a rectangle shape """ from rectangle import Rectangle rectangle = Rectangle( 0, 0, 3, 2 ) # four parameters are passed to __init__ constructor to create an instance of shape rectangle.move(5, 15) # move method is inherited from base class print(rectangle) print(rectangle.area()) from circle_class import Circle circle = Circle(3, 6, 1) print(circle) print(circle.area())
def test_zero_area(self): r = Rectangle(1, 1) a = r.area() assert (a == 0)