def perimeter(self): 'Circumference corrected for the rubber on the tire' return Circle.perimeter( self) * self.RUBBER_RATIO # Extending using a direct reference return super( Tire, self).perimeter() * self.RUBBER_RATIO # Extending using super() return 2.0 * math.pi * self.radius * self.RUBBER_RATIO # Overriding
def customer_1(): '''Customer 1: Local rubber sheet company company that cuts rounds sheets of rubber ''' cuts = [0.7, 0.3, 0.5] circles = [Circle(radius) for radius in cuts] for c in circles: print 'A circle with radius', c.radius print 'has an area {:.2f}'.format(c.area()) print 'and a perimeter {:.2f}'.format(c.circumference()) c.radius *= 1.1 print 'and a warm area {:.2f}'.format(c.area())
def customer_0(): ''' Customer 0: Our academic friends :return: ''' print 'Aproposal to reserach the areas of circles' print 'with Circuitous(tm)', Circle.version n = 5 seed(42) #circles = [Circle(random()) for i in xrange(n)] # with parenthesis it becomes a generator circles = (Circle(random()) for i in xrange(n)) # areas = [c.area() for c in circles] areas = (c.area() for c in circles) print 'The average aread of', n , 'random circles' print 'seeded with the Answer to Life and Everything' print 'is', sum(areas) / n
Our customers' code. ''' from circuitous import Circle from random import random,seed # Customer 1: Our academic friends. print 'Proposal to research the areas of circles' print 'using Circuitous(tm)', Circle.version n = 1000 seed(42) #circles = [Circle(random()) for i in range(n)] #areas = [circle.area() for circle in circles] #use generator instead circles = (Circle(random()) for i in xrange(n)) areas = (circle.area() for circle in circles) print 'The average area of', n, 'random circles' print 'seeded on the Answer to Life, and Everything' print 'is {:.2f}'.format(sum(areas) / n) print # the {} works by specifying a name:spcifier # so we are specifying no name, but the '.2f' means # "float to digits only", hence the answer generated was 1.09 # Customer 2: local rubber sheet company cuts = [0.7, 0.3, 0.5] circles = [Circle(radius) for radius in cuts]
''' Our customers' code. ''' from circuitous import Circle from random import random, seed # Customer 0: Our academic friends print 'A proposal to research the areas of circles' print 'with Circuitous(tm)', Circle.version n = 5 seed(42) circles = (Circle(random()) for i in xrange(n)) areas = (c.area() for c in circles) print 'The average area of', n, 'random circles' print 'seeded with the Answer to Life and Everything' print 'is', sum(areas) / n print # Customer 1: Local rubber sheet company cuts = [0.7, 0.3, 0.5] circles = [Circle(radius) for radius in cuts] for c in circles: print 'A circle with radius', c.radius print 'has a cold area %.2f' % c.area() print 'and a perimeter {:.2f}'.format(c.circumference()) c.radius *= 1.1 # c.set_radius(c.get_radius() * 1.1) print 'and a warm area %.2f' % c.area()
def circumference(self): 'adjusted perimeter for width of tyre' return 1.25 * Circle.circumference(self)
"Show-off the Circuitous code from the user's point of view" from __future__ import division from circuitous import Circle print u'Tutorial from Circuitous\N{trade mark sign}' print 'Circle class version %d.%d' % Circle.version[:2] c = Circle(10) print 'A circle with a radius of', c.radius print 'has an area of', c.area() print ## Academia ################################################ from random import seed, random from pprint import pprint n = 10000 jenny = 8675309 seed(jenny) print 'DARPA Grant Proposal to study the average area of random circles' print 'using Circuitous(tm) version %d.%d' % Circle.version[:2] print 'preliminary study of {n} random circles'.format(n=n) print "seed with Jenny's number: {jenny}".format(jenny=jenny) circles = [Circle(random()) for i in xrange(n)] areas = [circle.area() for circle in circles] average_area = sum(areas) / n print 'The average area is %.5f' % average_area print ## Rubber Sheet Company ####################################
"Show-off the ciruitous code from the user's point of view" from __future__ import division from circuitous import Circle import math print u'Tutorial for Circuitous\N{trade mark sign}' print 'Circle version %d.%d' % Circle.version[:2] c = Circle(10) print 'A circle with a radius of', c.radius print 'has an area of', c.area() print ## Academic Friends ###################################### from random import seed, random from pprint import pprint n = 10 jenny = 8675309 print 'DARPA Grant Proposal' print 'to study the average area of random circles' print 'using Circuitous(tm) version %d.%d' % Circle.version[:2] print 'preliminary study using %d random circles' % n print "seeded using Jenny's number: %d" % jenny seed(jenny) circles = [Circle(random()) for i in xrange(n)] areas = [circle.area() for circle in circles] average_area = sum(areas) / n print 'The average area is %.1f' % average_area
def perimeter(self): "adjusted perimeter for width of tire" return Circle.perimeter(self) * 1.25
def perimeter(self): return Circle.perimeter(self) * self.RUBBER_RATIO # Extending return 2.0 * 3.14 * self.radius * self.RUBBER_RATIO # Overriding
def circumference(self): 'adjusted perimeter for width of tyre' # unbound method call return 1.25 * Circle.circumference(self)
def customer_4(): bbd = 10 c = Circle.from_bbd(bbd) print 'a circle with bounding box diagonal of {}'.format(bbd) print 'has a radius of {:.2f}'.format(c.radius) print 'has an area of {:.2f}'.format(c.area())
def perimeter(self): 'Odometer correct perimeter that accounts for the rubber on tire' # Overriding -- don't call the parent method # Extending -- call the parent method and then modify it's result return Circle.perimeter(self) * self.RUBBER_RATIO
from __future__ import division from circuitous import Circle c = Circle(10) print u'Tutorial for Circuitous\N{trade mark sign}' print 'Version %d.%d'% Circle.version print 'A circle with a radius of', c.radius print 'has an area of', c.area() print ## Academic Friends #### from random import random, seed from pprint import pprint jenny = 8675309 n = 10 print 'DARPA Grant Proposal to compute the average area of random circles' print 'Proof of concept with %d randomly chosen circles' % n print "Seeded with Jenny's number:", jenny seed(jenny) circles = [Circle(random()) for i in xrange(n)] areas = [c.area() for c in circles] average_area = sum(areas) / n print 'The average area is %.1f' % average_area print ## Rubber Sheet ####### print 'Rubber sheet cut template spec sheet'
def perimeter(self): 'Perimeter adjusted for width of tire' return Circle.perimeter(self) * 1.25
class Tire(Circle): def perimeter(self): 'Perimeter adjusted for width of tire' return Circle.perimeter(self) * 1.25 t = Tire(22) print 'a tire with radius', t.radius print 'has an area %.0f' % t.area() print 'and a perimeter %.0f' % t.perimeter() print # Customer 4: national trucking company print 'a hill with inclinometer reading 7 degrees' print 'is a percent grade: %.2f%%' % Circle.angle_to_grade(7) print # Customer 5: international graphics company # We have money and power! # We want the constructor to be based on the bounding box diagonal! c = Circle.from_bbd(10) print 'a circle with a bounding box diagonal 10' print 'has a radius %.2f' % c.radius print 'and an area %.2f' % c.area() print # Customer 6: the Feds # We like to micromanage: # we will tell you not just WHAT to do, but also HOW to do it
def perimeter(self): return Circle.perimeter(self) * self.RUBBER_RATIO # Extending
def customer_3(): angle = 7.0 print 'a hill with {:.0f} degree incline'.format(angle) print 'is a {:.0f}% grade'.format(Circle.angle_to_grade(7))
def circumference(self): 'adjusted for width of tire' return Circle.circumference(self) * 1.25
"Show-off the ciruitous code from the user's point of view" from __future__ import division from circuitous import Circle print u'Tutorial for Circuitous\N{trade mark sign}' print 'Circle version %d.%d' % Circle.version[:2] c = Circle(10) print 'A circle with a radius of', c.radius print 'has an area of', c.area() print ## Academic Friends ###################################### from random import seed, random from pprint import pprint n = 100000 jenny = 8675309 print 'DARPA Grant Proposal' print 'to study the average area of random circles' print 'using Circuitous(tm) version %d.%d' % Circle.version[:2] print 'preliminary study using %d random circles' % n print "seeded using Jenny's number: %d" % jenny seed(jenny) circles = [Circle(random()) for i in xrange(n)] areas = [circle.area() for circle in circles] average_area = sum(areas) / n print 'The average area is %.5f' % average_area print
def perimeter(self): 'adjusted perimeter for width of tire' return Circle.perimeter(self) * 1.25 __perimeter = perimeter t = Tire(22) print ' tire with radius 22' print 'has an area %.2f' % t.area() print 'and a perimeter %.2f' % t.perimeter() print # Customer 4: National Trucking Co. print 'an incline of 7 degres' print 'is a %d percent grade' % Circle.angle_to_grade(7) print # Customer 5: International Graphics Co. print 'We have money and power' print 'We build circles with counding boxes not radius' c = Circle.from_bdd(10) print 'a Circle with a bounding box diagonal 10' print 'has a radius', c.radius print 'and an area', c.area() print # Customer 6: Federal Government # We like to micromanage! # We will tell you not only WHAT to do # But also HOW to do it