Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. import math
  2.  
  3. '''
  4. Circuitous, LLC -
  5. An Advanced Circle Analytics Company
  6. Summary: Toolset for New-Style Classes
  7. 1. Inherit from object.
  8. 2. Instance variables for information unique to an instance.
  9. 3. Class variables for data shared among all instances.
  10. 4. Regular methods need "self" to operate on instance data.
  11. 5. Class methods implement alternative constructors. They need "cls"
  12. so they can create subclass instances as well.
  13. 6. Static methods attach functions to class. They don't need
  14. either "self" or "cls".
  15. Static methods improve discoverability and require context to be specified.
  16. 7. The __method() is a class local reference, making sure the method refers
  17. to this class's method, not its childrens'.
  18. 8. A property() lets getter and setter methods be invoked automatically by
  19. attribute access. This allows Python classes to freely expose their
  20. instance variables.
  21. 9. The "__slots__" variable implements the Flyweight Design Pattern by
  22. suppressing instance dictionaries
  23. '''
  24.  
  25. class Circle(object):
  26. 'An advanced circle analysis toolkit'
  27. version = '0.7'
  28. #Add slots only at the end of development cycle.
  29. __slots__ = ['diameter']
  30.  
  31. def __init__(self, radius):
  32. self.radius = radius
  33.  
  34. #Getter method. Only use if needed
  35. @property
  36. def radius(self):
  37. return self.diameter / 2.0
  38.  
  39. #Setter method. Only use if needed
  40. @radius.setter
  41. def radius(self, radius):
  42. self.diameter = radius
  43.  
  44. def area(self):
  45. #class local reference. This expands to _Circle__parameter by process named as "Mangling"
  46. p = self.__perimeter()
  47. r = p / math.pi / 2.0
  48. return math.pi * r ** 2.0
  49.  
  50. def perimeter(self):
  51. return math.pi * 2.0 * self.radius
  52.  
  53. @classmethod
  54. def from_bbd(cls, bbd):
  55. #the cls refers to the class which calls this.
  56. radius = bbd / 2.0 / math.sqrt(2.0)
  57. return cls(radius)
  58.  
  59. @staticmethod
  60. def angle_to_grade(angle):
  61. return math.tan(math.radians(angle)) * 100.0
  62.  
  63. #Local copy for class local reference. This line is missing in video
  64. __perimeter = perimeter
  65.  
  66.  
  67. # Academia
  68. from random import random, seed
  69.  
  70. seed(8675309)
  71. print(f"Using circuituous version {Circle.version}")
  72. n = 1000000
  73. circles = [Circle(random()) for i in range(n)]
  74. avg = sum([c.area() for c in circles]) / n
  75. print(f"The average are of {n} random circles is {avg}")
  76.  
  77. # Rubber Sheet Company
  78.  
  79. cuts = [0.1, 0.7, 0.8]
  80. circles = [Circle(r) for r in cuts]
  81.  
  82. for c in circles:
  83. print(f"A circlet with a radius of {c.radius}, has a perimerer of {c.perimeter()}, and a cold area od {c.area()}")
  84. c.radius *= 1.1
  85. print(f", and a warm area of {c.area()}")
  86.  
  87.  
  88. # Tire Company
  89.  
  90. class Tire(Circle):
  91. 'Tires are just circles with corrected parameter'
  92.  
  93. def perimeter(self):
  94. 'Circumference corrected for the rubber'
  95. return Circle.perimeter(self) * 1.25
  96.  
  97.  
  98. t = Tire(22)
  99. print(
  100. f"A tire of radius {t.radius}, has an inner area of {t.area()} and an odometer corrected perimeter of {t.perimeter()}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement