Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import division
- from geopy.distance import vincenty
- import math
- from random import randint
- class Car(object):
- def __init__(self):
- '''
- Initialize class level variables here. If we needed to pass in objects (instances of classes)
- we could do that too.
- '''
- self.color = "white"
- self.engine = "gasoline"
- self.driver = None
- self.fuelTankCapacity = 10
- self.fuelUsed = 0
- self.fuelGauge = 100
- self.fuelLevel = 10
- self.milesPerGallon = 10
- def updateFuelGauge(self):
- '''
- This function controls the update of the fuel gauge. Since we need to update the fuel gauge from many places
- in the code we want to make this a function that we can call versus duplicating the code everywhere/
- '''
- self.fuelGauge = int((self.fuelLevel/self.fuelTankCapacity)*100)
- def burnGas(self, miles):
- '''
- Each time the PID is run this is how we tell the car to update how much gas it has burned. We also update the fuel gauge for
- convenience sake.
- '''
- burntGallons = miles/self.milesPerGallon
- self.fuelUsed += burntGallons
- self.fuelLevel -= burntGallons
- self.updateFuelGauge()
- def howMuchGasIsLeft(self):
- """
- This Class level function returns the fuel level value
- """
- return self.fuelGauge
- def fillGasTank(self, gallons):
- """
- This function refills the gas tank until it is full without regard to how much you are trying to
- put into the tank.
- """
- if (self.fuelLevel + gallons) >= self.fuelTankCapacity:
- self.fuelLevel = self.fuelTankCapacity
- else:
- self.fuelLevel += gallons
- self.updateFuelGauge()
- class Driver(object):
- def __init__(self, drivername, car):
- '''
- Here in the initialization of the class, I am passing in an instance of a car, as well
- as the driver's name. I need the car instance to observe when I need to get gas. Since the driver
- drives the car, this is where the PID loop (aka Driving) is logically located. I also set up
- a class level variable to let me know how far I have driven the car. This could also be an odometer on the
- car object, but we are using this like a trip mileage counter.
- '''
- self.name = drivername
- self.car = car
- self.milesDriven = 0
- def drive(self, route):
- interval = 10
- for i in range(0, route.distance, interval):
- '''
- This is the main PID loop for the "drive the car" concept.
- This loop will run from the starting point, over the distance, but we only execute this code below
- over the interval. Right now, I have it set to only report every 10 miles.
- '''
- self.milesDriven += interval
- self.car.burnGas(interval)
- print("%i miles driven "%self.milesDriven)
- print("Fuel gauge: %i gallons used overall on this trip, %i percent left in tank"%(self.car.fuelUsed, self.car.howMuchGasIsLeft()))
- self.checkGasLevel()
- print("Driving......")
- def checkGasLevel(self):
- '''
- Checking to see if we need to get gas or not; typically I like to get gas when I am about 1/4 tank.
- '''
- if self.car.howMuchGasIsLeft() <= 25:
- print("%i percent of gas left! Running OUT OF GAS!!!"%self.car.howMuchGasIsLeft())
- self.getGas()
- def getGas(self):
- '''
- for fun, I only put from 1 to 8 gallons in my tank at a time. Here I operate on the car object instance
- telling the car that I am filling the gas tank.
- '''
- print("Stopping for gas.")
- boughtGallons = randint(1, 8)
- print("Bought %i gallons of gas"%boughtGallons)
- self.car.fillGasTank(boughtGallons)
- class GoSomewhere(object):
- def __init__(self, pointA, pointB):
- self.pointA = pointA
- self.pointB = pointB
- #floor the distance result and then make it an integer explicitly, because python
- self.distance = int(math.floor( vincenty(newport_ri, cleveland_oh).miles))
- '''
- This codeblock below is the magic...
- It allows you to construct the needed classes and operate on them
- It is magic, because we use this when testing, but when in production there is other magic.
- Essentially it allows us to test theses classes in a standalone fashion
- '''
- if __name__ == '__main__':
- '''
- This make the execution of the code very easy to understand.
- '''
- #Create an instance of a car
- car = Car()
- #Create an instance of a driver
- driver = Driver("John", car)
- #Set up a waypoint
- newport_ri = (41.49008, -71.312796)
- #and another
- cleveland_oh = (41.499498, -81.695391)
- #create a route to go somewhere
- gotoCleveland = GoSomewhere(newport_ri, cleveland_oh)
- #drive the car
- print("Hi! I am %s and I am driving to Cleveland"%(driver.name))
- driver.drive(gotoCleveland)
- #Go back home
- print("Going back home")
- goHome = GoSomewhere(cleveland_oh, newport_ri)
- driver.drive(goHome)
- print("Arrived at home.")
Add Comment
Please, Sign In to add comment