def test_radius_changes_logged(self): circle = Circle(2) self.assertEqual(circle.radius_changes, [2]) circle.radius = 3 self.assertEqual(circle.radius_changes, [2, 3]) circle.diameter = 3 self.assertEqual(circle.radius_changes, [2, 3, 1.5])
def test_case_4(self): ans = "\n" + inspect.stack()[0][3] + ": \n" c1 = Circle(0, 0, radius=10) c2 = Circle(30, 40, radius=10) d = c1.dist(c2) ans += "dist = {:.1f}".format(d) + "\n" self.check(ans, 1703, True)
def test_change_diameter(): c = Circle(4) assert c.diameter == 8 c.diameter = 8 assert c.radius == 4 assert c.diameter == 8
def test_change_radius(): c = Circle(4) assert c.diameter == 8 c.radius = 5 assert c.radius == 5 assert c.diameter == 10
def res(): circle = Circle(1, 2) triangle = Triangle(Point(0, 1), Point(2, 3), Point(10, 2)) square = Square(Point(0, 1), Point(4, 4)) spis_figure = [] spis_figure.append(circle.ploshad()) spis_figure.append(triangle.ploshad()) spis_figure.append(square.ploshad()) return spis_figure
def test_comparability(self): circleA = Circle(1) circleB = Circle(2) circleC = Circle(3) self.assertTrue(circleA < circleB < circleC) self.assertTrue(circleA <= circleB <= circleC) self.assertFalse(circleB < circleA) self.assertFalse(circleC < circleB) self.assertTrue(circleC > circleB > circleA) self.assertTrue(circleC >= circleB >= circleA) self.assertFalse(circleA > circleB) self.assertFalse(circleB > circleC)
def test_equality(self): circleA = Circle(2) circleB = Circle(2) circleC = Circle(1) self.assertTrue(circleA == circleB) self.assertTrue(circleB != circleC != circleA) self.assertFalse(circleA != circleB) self.assertFalse(circleA == circleC) self.assertFalse(circleB == circleC) circleC.radius = 2 self.assertTrue(circleA == circleB == circleC) self.assertFalse(circleB != circleC) self.assertFalse(circleA != circleC)
def test_case_2(self): """test Circle.__contains__""" # test Circle.__contains__ ans = "\n" + inspect.stack()[0][3] + ": \n" c1 = Circle(0, 0, radius=10) c2 = Circle(5, 0, radius=5) c3 = Circle(10, 0, radius=5) b2 = c2 in c1 b3 = c3 in c1 ans += "c2 inside c1 = {:s}.\n".format(str(b2)) ans += "c3 inside c1 = {:s}.\n".format(str(b3)) self.check(ans, 4001, True)
def main(): point_a = Point(2, 4) point_b = Point(6, 11) point_c = Point(3, 4) triangle_1 = Triangle(point_a, point_b, point_c) print(triangle_1.triangle_S()) print(triangle_1.triangle_P()) print('=' * 20) square_1 = Square(point_a, point_b) print(square_1.square_S()) print(square_1.square_P()) print('=' * 20) circle_1 = Circle(point_a, 15) print(circle_1.circle_S()) print(circle_1.circle_P())
def symbolFactory(playerNumber, x, y): symbolTypes = { 1 : Circle(Program.canvas, x=x, y=y), 2 : Cross(Program.canvas, x=x, y=y), 3 : Square(Program.canvas, x=x, y=y) } return symbolTypes[playerNumber]
def test_case_3(self): ans = "\n" + inspect.stack()[0][3] + ": \n" c = Circle(0, 0, radius=10) c.relocate(2, 2) c.moveBy(1, 1) ans += str(c) + "\n" ans += str(c.details()) + "\n" self.check(ans, 4792, True)
def mutateSimpleShape(shape): ran = random.uniform(0, 99) if ran > 60: nshape = random.choice([ Square(shape.children), Circle(shape.children), Triangle(shape.children) ]) if nshape.name == shape.name: return mutateSimpleShape(shape) return nshape else: return shape
def create(pt, r=None): n = len(pt) if (r == None): if n == 1: x, y = pt[0] return Point(x, y) elif n == 2: return Segment(pt) elif n == 3: return Triangle(pt) else: return Polygon(pt) elif n == 1: x, y = pt[0] if (r > 0): return Circle(x, y, r) else: return Point(x, y)
def test_set_area(): c = Circle(10) with pytest.raises(AttributeError): c.area = 100
def test_circle_area(): c = Circle(4) assert c.area == (4**2 * math.pi)
def test_diameter_changes(self): circle = Circle(2) self.assertEqual(circle.diameter, 4) circle.radius = 3 self.assertEqual(circle.diameter, 6)
def test_set_diameter(self): circle = Circle(2) self.assertEqual(circle.diameter, 4) circle.diameter = 3 self.assertEqual(circle.radius, 1.5)
from classes import Cat, Circle, Rectangle, Square cats = [{"name": "Барон", "sex": "мальчик", "age": 2}, {"name": "Сэм", "sex": "мальчик", "age": 2}] for cat in cats: obj_cat = Cat(name=cat.get("name"), sex=cat.get("sex"), age=cat.get("age")) print(obj_cat.getName(), obj_cat.getSex(), obj_cat.getAge()) print() r1, r2 = Rectangle(10, 5), Rectangle(12, 5) sq1, sq2 = Square(5), Square(7) c1, c2 = Circle(5), Circle(8) figures = [r1, r2, sq1, sq2, c1, c2] for figure in figures: if isinstance(figure, Circle): print( "Площадь окружности с радиусом {} равна {}".format(figure.getRadius(), round(figure.get_area_circle(), 4))) elif isinstance(figure, Square): print("Площадь квадрата со стороной {} равна {}".format(figure.getA(), figure.getAreaSquare())) else: print("Площадь прямоугольника с шириной {}, высотой {} равна {}".format(figure.getWidth(), figure.getHeigth(), figure.getArea())
def atomconvert(text, nts): parr = [] # eg.[a 1 b 2 ] atomparr = [] # eg.[1 2 3] parr.clear() atomparr.clear() sumflag = 0 for chip in reversed(text): # print(chip) if "]" in chip: sumflag = sumflag + chip.count("]") # print(sumflag) chip = chip.replace("[", "") chip = chip.replace("]", "") # print(chip) if is_number(chip): # 如果是数字 atomparr.append(float(chip)) else: # 如果是字母 list.reverse(atomparr) if chip == "a" or chip == "alpha": parr.append(Alpha(*atomparr)) elif chip == "b" or chip == "brightness": parr.append(Brightness(*atomparr)) elif chip == "sat" or chip == "saturation": parr.append(Saturation(*atomparr)) elif chip == "h" or chip == "hue": parr.append(Hue(*atomparr)) elif chip == "y": parr.append(Y(*atomparr)) elif chip == "r" or chip == "rotate": parr.append(Rotate(*atomparr)) elif chip == "f" or chip == "flip": parr.append(Flip(*atomparr)) elif chip == "x": parr.append(X(*atomparr)) elif chip == "s" or chip == "size": parr.append(Size(*atomparr)) elif chip == "trans" or chip == "transform": parr.append(Transform(*atomparr)) elif chip == "skew": parr.append(Skew(*atomparr)) elif chip == "..": parr.append(randRange(*atomparr)) # 统计完毕一个参数,清空数字列表 atomparr.clear() list.reverse(parr) if sumflag == 2: # print("GG") parr = [Leftbrac()] + parr + [Rightbrac()] if text[0] == "CIRCLE": return Circle(parr) elif text[0] == "TRIANGLE": return Triangle(parr) elif text[0] == "SQUARE": return Square(parr) else: ntName = text[0] for n in nts: if ntName == n.name: return RuleCall(n, parr) newNT = NonTerminal(ntName, []) nts.append(newNT) return RuleCall(newNT, parr)
from classes import Person p1 = Person('Hans Petter Langtangen', office_phone='67828283', email='*****@*****.**') p2 = Person('Aslak Tveito', office_phone='67828282') p2.add_email('*****@*****.**') phone_book = [p1, p2] # list for person in phone_book: person.dump() phone_book = {'Langtangen': p1, 'Tveito': p2} # dict is better for person in sorted(phone_book): phone_book[person].dump() from classes import Circle c = Circle(2, -1, 5) print 'A circle with radius %g at (%g, %g) has area %g' % \ (c.R, c.x0, c.y0, c.area()) from classes import Derivative from math import * df = Derivative(sin) print dir(df) x = pi df(x) cos(x) # exact def g(t): return t**3 dg = Derivative(g) t = 1
from classes import Point, Triangle, Circle, Square point1 = Point(x=0, y=2) point2 = Point(x=1, y=0) point3 = Point(x=5, y=0) triangle_1 = Triangle(point_1=point1, point_2=point2, point_3=point3) point1 = Point(x=0, y=2) point2 = Point(x=2, y=0) square_1 = Square(point_1=point1, point_2=point2) circle_1 = Circle(radius=5, center_point=5) figures_list = [triangle_1, square_1, circle_1] for figure in figures_list: print( f'Периметр {figure.__class__.__name__} равен {figure.get_perimeter()}') print(f'Площадь {figure.__class__.__name__} равна {figure.get_square()}')
def test_radius(self): circle = Circle(5) self.assertEqual(circle.radius, 5)
def test_area(self): circle = Circle(2) self.assertEqual(circle.area, math.pi * 4)
def test_case_3(self): ans = "\n" + inspect.stack()[0][3] + ": \n" c = Circle(0, 0, radius=10) c.relocate(2, 2) c.moveBy(1, 1) ans += str(c) + "\n" ans += str(c.details()) + "\n" self.check(ans, 4792, True) TestShapeMethod.test_case_3 = test_case unittest.main(argv=[''], verbosity=0, exit=False) s = """ Expected outputs (except the id numbers and the spaces could be different test_case: Circle(id=366) Circle(id=366): area=314, radius: 10.0, center: (3,3) test_case: pass! ] """ # test Circle.dist def test_case_4(self): ans = "\n" + inspect.stack()[0][3] + ": \n"
def test_init(): Circle(5) assert True
def test_delete_diameter(): c = Circle(10) with pytest.raises(AttributeError): del c.diameter
from classes import Person p1 = Person('Hans Petter Langtangen', office_phone='67828283', email='*****@*****.**') p2 = Person('Aslak Tveito', office_phone='67828282') p2.add_email('*****@*****.**') phone_book = [p1, p2] # list for person in phone_book: person.dump() phone_book = {'Langtangen': p1, 'Tveito': p2} # dict is better for person in sorted(phone_book): phone_book[person].dump() from classes import Circle c = Circle(2, -1, 5) print 'A circle with radius %g at (%g, %g) has area %g' % \ (c.R, c.x0, c.y0, c.area()) from classes import Derivative from math import * df = Derivative(sin) print dir(df) x = pi df(x) cos(x) # exact def g(t): return t**3
def test_default_radius(self): circle = Circle() self.assertEqual(circle.radius, 1)
def test_radius(): c = Circle(5) assert c.radius == 5
lumpy = Lumpy() v = AccountP('John Doe', '93351545761', 1000) lumpy.object_diagram() lumpy.class_diagram() del v lumpy = Lumpy() v = Person('John Doe', email='*****@*****.**') v.add_mobile_phone('18805614211') lumpy.object_diagram() lumpy.class_diagram() del v lumpy = Lumpy() v = Circle(-1.5, 2, 3.5) lumpy.object_diagram() lumpy.class_diagram() del v lumpy = Lumpy() def g(x): return 1 + (1-x)**4 d = Derivative(g) lumpy.object_diagram() lumpy.class_diagram() del g, d lumpy = Lumpy() lumpy.make_reference()
def test_no_negative_radius(self): circle = Circle(2) with self.assertRaises(ValueError) as context: circle.radius = -10 self.assertEqual(str(context.exception), "Radius cannot be negative")