def __init__(self, json): if JSONParser._parser(json) == 1: raise ValueError("Bad json") shapes = [] self.screen = dict() self.palette = dict() for i in json["Palette"]: self.palette[i] = JSONParser.return_color(dict(), json["Palette"][i]) self.screen["width"] = json["Screen"]["width"] self.screen["height"] = json["Screen"]["height"] self.screen["bg_color"] = JSONParser.return_color( self.palette, json["Screen"]["bg_color"]) self.screen["fg_color"] = JSONParser.return_color( self.palette, json["Screen"]["fg_color"]) for i in json["Figures"]: type = i["type"] if type == "point": if "color" in i: color = JSONParser.return_color(self.palette, i["color"]) else: color = self.screen["fg_color"] shapes.append(Shape.Point(i["x"], i["y"], color)) elif type == "polygon": if "color" in i: color = JSONParser.return_color(self.palette, i["color"]) else: color = self.screen["fg_color"] shapes.append(Shape.Polygon(i["points"], color)) elif type == "rectangle": if "color" in i: color = JSONParser.return_color(self.palette, i["color"]) else: color = self.screen["fg_color"] shapes.append( Shape.Rectangle(i["x"], i["y"], i["width"], i["height"], color)) elif type == "square": if "color" in i: color = JSONParser.return_color(self.palette, i["color"]) else: color = self.screen["fg_color"] shapes.append(Shape.Square(i["x"], i["y"], i["size"], color)) elif type == "circle": if "color" in i: color = JSONParser.return_color(self.palette, i["color"]) else: color = self.screen["fg_color"] shapes.append( Shape.Circle(i["x"], i["y"], i["radius"], color=color)) else: print("Bad type of figure") raise ValueError("Bad type of figure") self.shapes = shapes
# -*- coding: utf-8 -*- # # File name : test_Shape.py # Description : Test and verify the Shape.py module # import Shape p = Shape.Point(3, 9) repr(p) q = eval(p.__module__ + "." + repr(p)) print(repr(q)) print(str(q)) c = Shape.Circle(5, 28, 45) print(p.distance_from_origin()) print(c.distance_from_origin())
import Shape import math a = Shape.Point() # print "repr(a)" repr(a) print("a") # a print(a) # (0, 0) b = Shape.Point(3, 4) repr(b) print(b) # (3, 4) print(repr(b)) # Point(3, 4) #b.distance_from_origin() b.x = -19 str(b) print(str(b)) # (-19, 4) print(str(a)) # (0, 0) a == b, a != b print(a == b, a != b) # False True
import Shape # use intermediate variables a = Shape.Point() repr(a) b = Shape.Point(3, 4) str(b) b.distance_from_origin() b.x = -19 str(b) # no intermediate variables distance = Shape.Point(10, 20).distance_from_origin() print(distance) # object itself does not and cannot be claimed in the method print(a.eq(b))
import Shape point = Shape.Point(6, 10) print(point)