class TestPolygon(TestCase): def setUp(self): self.polygon = Polygon(10, 20, 30, 50) self.polygon.set_a(20) def test_calculate_square(self): self.assertNotEqual(self.polygon.calculate_square(), 5456.366088464047) def test_get_n(self): self.assertEqual(self.polygon.get_n(), 30) self.assertNotEqual(self.polygon.get_n(), 40) @patch("polygon.polygon.Polygon.set_n") def test_set_n(self, set_n): self.polygon.set_n(20) self.assertTrue(set_n.called) self.assertEqual(set_n.call_args[0][0], 20) def test_get_a(self): self.assertNotEqual(self.polygon.get_n(), 60) self.assertNotEqual(self.polygon.get_n(), 40) @patch("polygon.polygon.Polygon.set_a") def test_set_a(self, set_a): self.polygon.set_a(40) self.assertTrue(set_a.called) self.assertEqual(set_a.call_args[0][0], 40)
class Circle(Shape): def __init__(self, t: "turtle.Turtle"): super().__init__(t) self.__t = t self.__radius = self.__get_radius() self.__circumference = 2 * pi * self.__radius self.__n_polygon_sides = self.__get_polygon_sides() self.__polygon_side_length = self.__get_polygon_side_length() self.__circle = Polygon( self.__t, n_sides=self.__n_polygon_sides, side_length=self.__polygon_side_length ) def draw(self): self.__circle.draw() def __get_radius(self): radius = input("Enter a valid radius (min: 100, max: 200): ") try: radius = int(radius) if not (100 <= radius <= 200): raise ValueError except ValueError: return self.__get_radius() return radius def __get_polygon_sides(self): return int(self.__circumference / 3) + 3 def __get_polygon_side_length(self): return self.__circumference / self.__n_polygon_sides
def __init__(self, t: "turtle.Turtle"): super().__init__(t) self.__t = t self.__radius = self.__get_radius() self.__circumference = 2 * pi * self.__radius self.__n_polygon_sides = self.__get_polygon_sides() self.__polygon_side_length = self.__get_polygon_side_length() self.__circle = Polygon( self.__t, n_sides=self.__n_polygon_sides, side_length=self.__polygon_side_length )
def test_polygon(): n, R = 3, 1 p = Polygon(n, R) assert str(p) == "Polygon(n=3,R=1)", f"actual: {str(p)}" assert p.count_vertices == n, f"actual: {p.count_vertices} expected {n}" assert p.count_edges == n assert p.circumradius == R assert p.interior_angle == 60 n = 4 R = 1 p = Polygon(n, R) assert p.area == 2.0, f"actual: {p.area}, expected 2.0" assert p.side_length, math.sqrt(2) assert p.interior_angle == 90 p1 = Polygon(3, 10) p2 = Polygon(10, 10) p3 = Polygon(15, 10) p4 = Polygon(15, 100) p5 = Polygon(15, 100) assert p2 > p1 assert p2 < p3 assert p3 != p4 assert p1 != p4 assert p4 == p5
class Proxy(PolygonInterface): def __init__(self, x: int, y, n, a): self.polygon = Polygon(x, y, n, a) def get_n(self): return self.polygon.get_n() def get_a(self): return self.polygon.get_a() def calculate_square(self): return self.polygon.calculate_square() def set_n(self, n: int): raise Exception("You can't use a setter function") def set_a(self, a): raise Exception("You can't use a setter function")
def get_polygons(self): to_skip = [] n = len(self._contours) for i in range(n): if i in to_skip: continue if i == 18: print("18") holes_index = self._hierarchy.get_holes_index(i) [to_skip.append(_) for _ in holes_index] poly = self._contours[i] holes = [self._contours[_] for _ in holes_index] print(i, " start") yield Polygon(poly, holes) print(i, " ok")
def setUp(self): self.polygon = Polygon(10, 20, 30, 50) self.polygon.set_a(20)
def __init__(self, x: int, y, n, a): self.polygon = Polygon(x, y, n, a)
# -*- coding: utf-8 -*- # Indentation: Visual Studio ''' Polygon class ''' __version__ = 1.0 __author__ = "Sourav Raj" __author_email__ = "*****@*****.**" import sys sys.path.append(r'G:\PythonProjects\python\classes\classAndObjectBasics') from polygon.polygon import Polygon square = Polygon(4, 'square') print(square.name, square.sides) square.draw()