Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- '''
- Circuitous, LLC -
- An Advanced Circle Analytics Company
- Summary: Toolset for New-Style Classes
- 1. Inherit from object.
- 2. Instance variables for information unique to an instance.
- 3. Class variables for data shared among all instances.
- 4. Regular methods need "self" to operate on instance data.
- 5. Class methods implement alternative constructors. They need "cls"
- so they can create subclass instances as well.
- 6. Static methods attach functions to class. They don't need
- either "self" or "cls".
- Static methods improve discoverability and require context to be specified.
- 7. The __method() is a class local reference, making sure the method refers
- to this class's method, not its childrens'.
- 8. A property() lets getter and setter methods be invoked automatically by
- attribute access. This allows Python classes to freely expose their
- instance variables.
- 9. The "__slots__" variable implements the Flyweight Design Pattern by
- suppressing instance dictionaries
- '''
- class Circle(object):
- 'An advanced circle analysis toolkit'
- version = '0.7'
- #Add slots only at the end of development cycle.
- __slots__ = ['diameter']
- def __init__(self, radius):
- self.radius = radius
- #Getter method. Only use if needed
- @property
- def radius(self):
- return self.diameter / 2.0
- #Setter method. Only use if needed
- @radius.setter
- def radius(self, radius):
- self.diameter = radius
- def area(self):
- #class local reference. This expands to _Circle__parameter by process named as "Mangling"
- p = self.__perimeter()
- r = p / math.pi / 2.0
- return math.pi * r ** 2.0
- def perimeter(self):
- return math.pi * 2.0 * self.radius
- @classmethod
- def from_bbd(cls, bbd):
- #the cls refers to the class which calls this.
- radius = bbd / 2.0 / math.sqrt(2.0)
- return cls(radius)
- @staticmethod
- def angle_to_grade(angle):
- return math.tan(math.radians(angle)) * 100.0
- #Local copy for class local reference. This line is missing in video
- __perimeter = perimeter
- # Academia
- from random import random, seed
- seed(8675309)
- print(f"Using circuituous version {Circle.version}")
- n = 1000000
- circles = [Circle(random()) for i in range(n)]
- avg = sum([c.area() for c in circles]) / n
- print(f"The average are of {n} random circles is {avg}")
- # Rubber Sheet Company
- cuts = [0.1, 0.7, 0.8]
- circles = [Circle(r) for r in cuts]
- for c in circles:
- print(f"A circlet with a radius of {c.radius}, has a perimerer of {c.perimeter()}, and a cold area od {c.area()}")
- c.radius *= 1.1
- print(f", and a warm area of {c.area()}")
- # Tire Company
- class Tire(Circle):
- 'Tires are just circles with corrected parameter'
- def perimeter(self):
- 'Circumference corrected for the rubber'
- return Circle.perimeter(self) * 1.25
- t = Tire(22)
- print(
- 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