From 3bc49e7856570eb26dcc548f87ec206dcdf1fd98 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Tue, 12 Jul 2022 11:28:55 -0500 Subject: [PATCH 01/14] added fuelType member variable to class Vehicle to match design doc --- fleet.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/fleet.py b/fleet.py index 16e1015..05f8874 100644 --- a/fleet.py +++ b/fleet.py @@ -31,7 +31,8 @@ class Vehicle(ABC): # https://www.geeksforgeeks.org/inheritance-and-composition-in-python/ @abstractmethod - def __init__(self, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid): + + def __init__(self, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid, _fuelType ): self.vehicleObject = Fleet(vtype,movspeed, travelmed, vehicleid) self.gpsPosition = _gpsPosition self.velocity = _velocity @@ -39,6 +40,7 @@ class Vehicle(ABC): self.weightType = _velocityType self.weight = _weightType self.status = _status + self.fuelType = _fuelType class GroundVehicleInterface(ABC): '''Resource on Python interfaces. This is duck typed. https://realpython.com/python-interface/ Interfaces make a contract that a method must be implemented if an interface is inherited and allows differing implementations of the same method.''' @@ -49,8 +51,8 @@ class GroundVehicleInterface(ABC): class GroundVehicle(Vehicle, GroundVehicleInterface): @abstractmethod - def __init__(self, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid): - super().__init__(_gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid) + def __init__(self, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype, movspeed, travelmed, vehicleid, _fuelType): + super().__init__(_gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid, _fuelType) self.isModular = _isModular # modifier to GPS position self.moveGroundVehicle = _moveGroundVehicle @@ -58,8 +60,8 @@ class GroundVehicle(Vehicle, GroundVehicleInterface): class RoadVehicle(GroundVehicle): @abstractmethod - def __init__(self, _axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid): - super().__init__(_isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid) + def __init__(self, _axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid, _fuelType): + super().__init__(_isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid, _fuelType) self.axleCount = _axlecount class Train(GroundVehicle): @@ -83,10 +85,10 @@ Train1.displayLocomotiveCount() # Always encapsulate parent constructors into child classes to make those variables available class semi_Truck(RoadVehicle): - def __init__(self, _noOfTrailersAttached, _axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid): + def __init__(self, _noOfTrailersAttached, _axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid, _fuelType): self.noOfTrailersAttached = _noOfTrailersAttached # super in Python returns an object representing the parent class and - super().__init__(_axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid) + super().__init__(_axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid, _fuelType) # Don't modify passed in values unless absolutely necessary def displaySemiAttributes(self): print(f"The semi has {self.noOfTrailersAttached} trailers attached", f"and {self.axleCount} axles.") @@ -94,7 +96,7 @@ class semi_Truck(RoadVehicle): print(self.status) -STruck = semi_Truck(2, 4, True, [40.0000, 40.0000], [56.0000, 89.0000], 60, 90000, "mph", "lbs,", "Ready", "Semi Truck", 100, "Ground", "1F8HNG7") +STruck = semi_Truck(2, 4, True, [40.0000, 40.0000], [56.0000, 89.0000], 60, 90000, "mph", "lbs,", "Ready", "Semi Truck", 100, "Ground", "1F8HNG7", "diesel") # When possible, build your argument requirements from the top down so that by the time you implement your concrete classes, you know all the data points that you need to include. # Remember to be careful to check that you are running the method on an instance of the class, not on the class definition. -- 2.47.3 From 87e2d0e3e3df2e26fb64d2b851e7e7664c3e9375 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Tue, 12 Jul 2022 11:37:42 -0500 Subject: [PATCH 02/14] Demonstrated accessing a member variable several levels of inheiritance above my instantiated object --- fleet.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fleet.py b/fleet.py index 05f8874..f95e727 100644 --- a/fleet.py +++ b/fleet.py @@ -102,6 +102,8 @@ STruck = semi_Truck(2, 4, True, [40.0000, 40.0000], [56.0000, 89.0000], 60, 9000 # Remember to be careful to check that you are running the method on an instance of the class, not on the class definition. STruck.displaySemiAttributes() +print(STruck.isModular) + # Next - implement abstracted methods and constructor classes. Demonstrate use of super if Python supports it. # Use these values in instances and show that values can be accessed regardless of what level they live on. -- 2.47.3 From 2d696b0ae61f5de03bbc0b05940e9e2c5766563d Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Tue, 12 Jul 2022 11:53:21 -0500 Subject: [PATCH 03/14] Removed vtype, speed and travelmed variables as they were unecessary and added before design doc was built. --- fleet.py | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/fleet.py b/fleet.py index f95e727..22030d0 100644 --- a/fleet.py +++ b/fleet.py @@ -4,36 +4,25 @@ class Fleet(): fleet_members = [] - def __init__(self, vtype, movspeed, travelmed, vehicleid): - self.vehicletype = vtype - self.movementspeed = movspeed - self.travelmedium = travelmed + def __init__(self, vehicleid): self.uuid = vehicleid def displayAllFleetMembers(self): for i in self.vehicles: print(i) - - def displayVehicleType(self): - print(self.vehicletype) def addVehicle(self, newVehicleId): self.fleet_members.append(newVehicleId) - - - - - class Vehicle(ABC): '''Defines the properties of vehicles''' # https://www.geeksforgeeks.org/inheritance-and-composition-in-python/ @abstractmethod - def __init__(self, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid, _fuelType ): - self.vehicleObject = Fleet(vtype,movspeed, travelmed, vehicleid) + def __init__(self, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType ): + self.vehicleObject = Fleet(vehicleid) self.gpsPosition = _gpsPosition self.velocity = _velocity self.velocityType = _weight @@ -51,8 +40,8 @@ class GroundVehicleInterface(ABC): class GroundVehicle(Vehicle, GroundVehicleInterface): @abstractmethod - def __init__(self, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype, movspeed, travelmed, vehicleid, _fuelType): - super().__init__(_gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid, _fuelType) + def __init__(self, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status,vehicleid, _fuelType): + super().__init__(_gpsPosition, _velocity, _weight, _velocityType, _weightType, _status,vehicleid, _fuelType) self.isModular = _isModular # modifier to GPS position self.moveGroundVehicle = _moveGroundVehicle @@ -60,8 +49,8 @@ class GroundVehicle(Vehicle, GroundVehicleInterface): class RoadVehicle(GroundVehicle): @abstractmethod - def __init__(self, _axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid, _fuelType): - super().__init__(_isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid, _fuelType) + def __init__(self, _axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType): + super().__init__(_isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType) self.axleCount = _axlecount class Train(GroundVehicle): @@ -85,10 +74,10 @@ Train1.displayLocomotiveCount() # Always encapsulate parent constructors into child classes to make those variables available class semi_Truck(RoadVehicle): - def __init__(self, _noOfTrailersAttached, _axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid, _fuelType): + def __init__(self, _noOfTrailersAttached, _axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType): self.noOfTrailersAttached = _noOfTrailersAttached # super in Python returns an object representing the parent class and - super().__init__(_axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vtype,movspeed, travelmed, vehicleid, _fuelType) + super().__init__(_axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType) # Don't modify passed in values unless absolutely necessary def displaySemiAttributes(self): print(f"The semi has {self.noOfTrailersAttached} trailers attached", f"and {self.axleCount} axles.") @@ -96,7 +85,7 @@ class semi_Truck(RoadVehicle): print(self.status) -STruck = semi_Truck(2, 4, True, [40.0000, 40.0000], [56.0000, 89.0000], 60, 90000, "mph", "lbs,", "Ready", "Semi Truck", 100, "Ground", "1F8HNG7", "diesel") +STruck = semi_Truck(2, 4, True, [40.0000, 40.0000], [56.0000, 89.0000], 60, 90000, "mph", "lbs,", "Ready", "1F8HNG7", "diesel") # When possible, build your argument requirements from the top down so that by the time you implement your concrete classes, you know all the data points that you need to include. # Remember to be careful to check that you are running the method on an instance of the class, not on the class definition. -- 2.47.3 From a0b3380997550bf518ff9beff5f75cce903b8d61 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Tue, 12 Jul 2022 11:56:43 -0500 Subject: [PATCH 04/14] added design doc to source control --- Fleet.drawio.html | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Fleet.drawio.html diff --git a/Fleet.drawio.html b/Fleet.drawio.html new file mode 100644 index 0000000..1db98e0 --- /dev/null +++ b/Fleet.drawio.html @@ -0,0 +1,12 @@ + + + + +Fleet.drawio.html + + + +
+ + + -- 2.47.3 From 013db0c19747c98cb5e3f1e9976c6590a4796a50 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Tue, 12 Jul 2022 13:40:32 -0500 Subject: [PATCH 05/14] created fuelType enum --- Fleet.drawio.html | 2 +- fleet.py | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Fleet.drawio.html b/Fleet.drawio.html index 1db98e0..fb658d5 100644 --- a/Fleet.drawio.html +++ b/Fleet.drawio.html @@ -6,7 +6,7 @@ -
+
diff --git a/fleet.py b/fleet.py index 22030d0..4f3d41c 100644 --- a/fleet.py +++ b/fleet.py @@ -1,4 +1,5 @@ from abc import ABC, abstractmethod +import enum # ABC is imported from the standard library since Python does not natively support abstract methods and classes. class Fleet(): @@ -17,6 +18,7 @@ class Fleet(): class Vehicle(ABC): '''Defines the properties of vehicles''' + # _fuelType = ("Diesel", "Gasoline", "Aviation Gas", "Electricity", "Nuclear", "100LL") # https://www.geeksforgeeks.org/inheritance-and-composition-in-python/ @abstractmethod @@ -25,12 +27,24 @@ class Vehicle(ABC): self.vehicleObject = Fleet(vehicleid) self.gpsPosition = _gpsPosition self.velocity = _velocity - self.velocityType = _weight - self.weightType = _velocityType - self.weight = _weightType + self.velocityType = _velocityType + self.weight = _weight + self.weightType = _weightType self.status = _status self.fuelType = _fuelType + + def loadCargo(self, _weight): + self.status = "Loading cargo" + # How do I access the set value in the init above? + self.weight = _weight + input(f"Enter cargo weight.") +class fuelTypes(enum.Enum): + Diesel = 1 + Gasoline = 2 + Aviation_Kerosene = 3 + Electricity = 4 + Nuclear = 5 + Aviation_Gas = 6 class GroundVehicleInterface(ABC): '''Resource on Python interfaces. This is duck typed. https://realpython.com/python-interface/ Interfaces make a contract that a method must be implemented if an interface is inherited and allows differing implementations of the same method.''' @abstractmethod -- 2.47.3 From f75a64208e184f75d74378c2f42f0d51dcf1e89c Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Tue, 12 Jul 2022 15:10:35 -0500 Subject: [PATCH 06/14] saving progress --- fleet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fleet.py b/fleet.py index 4f3d41c..3e6d1db 100644 --- a/fleet.py +++ b/fleet.py @@ -91,7 +91,7 @@ class semi_Truck(RoadVehicle): def __init__(self, _noOfTrailersAttached, _axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType): self.noOfTrailersAttached = _noOfTrailersAttached # super in Python returns an object representing the parent class and - super().__init__(_axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType) + super().__init__(_axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType, "Semi Truck") # Don't modify passed in values unless absolutely necessary def displaySemiAttributes(self): print(f"The semi has {self.noOfTrailersAttached} trailers attached", f"and {self.axleCount} axles.") -- 2.47.3 From 9c94fcf30e7245700be036253b6e90decdd6667b Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Thu, 14 Jul 2022 10:11:04 -0500 Subject: [PATCH 07/14] Thurs morning initial commit --- fleet.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fleet.py b/fleet.py index 3e6d1db..693897c 100644 --- a/fleet.py +++ b/fleet.py @@ -10,7 +10,7 @@ class Fleet(): def displayAllFleetMembers(self): - for i in self.vehicles: + for i in self.fleet_members: print(i) def addVehicle(self, newVehicleId): @@ -51,7 +51,7 @@ class GroundVehicleInterface(ABC): def travel(self): pass -class GroundVehicle(Vehicle, GroundVehicleInterface): +class GroundVehicle(Vehicle): @abstractmethod def __init__(self, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status,vehicleid, _fuelType): @@ -67,7 +67,7 @@ class RoadVehicle(GroundVehicle): super().__init__(_isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType) self.axleCount = _axlecount -class Train(GroundVehicle): +class Train(GroundVehicle, GroundVehicleInterface): def __init__(self, _noOfCarsAttached, _locomotiveCount): self.noOfCarsAttached = _noOfCarsAttached self.locomotiveCount = _locomotiveCount @@ -87,7 +87,7 @@ Train1.displayLocomotiveCount() # At this point, my code should print out the values from the car count and the locomotive count. Further demonstration of abstract and concrete classes to follow. # Always encapsulate parent constructors into child classes to make those variables available -class semi_Truck(RoadVehicle): +class semi_Truck(RoadVehicle, GroundVehicleInterface): def __init__(self, _noOfTrailersAttached, _axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType): self.noOfTrailersAttached = _noOfTrailersAttached # super in Python returns an object representing the parent class and -- 2.47.3 From 22f5d8b172e012d25bdc5cede3d21cb074c30e77 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Thu, 14 Jul 2022 11:07:24 -0500 Subject: [PATCH 08/14] Added calling a parent abstract method, trying to demonstrate overriding as well. Will check implemenation. Currently encountering TypeError despite error handling added that should resolve the default string input to an int, but error persists. --- fleet.py | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/fleet.py b/fleet.py index 693897c..9e059cb 100644 --- a/fleet.py +++ b/fleet.py @@ -33,10 +33,9 @@ class Vehicle(ABC): self.status = _status self.fuelType = _fuelType + @abstractmethod def loadCargo(self, _weight): - self.status = "Loading cargo" - # How do I access the set value in the init above? - self.weight = _weight + input(f"Enter cargo weight.") + pass class fuelTypes(enum.Enum): Diesel = 1 @@ -78,6 +77,16 @@ class Train(GroundVehicle, GroundVehicleInterface): print(f"The train has {self.locomotiveCount} locomotives attached.") def travel(self): print("Speed 60mph, Direction E") + def loadCargo(self, _weight, _noOfCarsAttached): + super().loadCargo(_weight) + self.weight = _weight + input(f"Enter cargo weight (integer values only).") + try: + weight = int(weight) + except ValueError: + self.weight = _weight + input(f"That entry was not an integer. Enter cargo weight in integers only.") + + + # Pattern used for loadCargo - https://stackoverflow.com/questions/38987072/how-to-force-integer-input-in-python-3-x Train1 = Train(20, 2) @@ -91,13 +100,31 @@ class semi_Truck(RoadVehicle, GroundVehicleInterface): def __init__(self, _noOfTrailersAttached, _axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType): self.noOfTrailersAttached = _noOfTrailersAttached # super in Python returns an object representing the parent class and - super().__init__(_axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType, "Semi Truck") + super().__init__(_axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType) # Don't modify passed in values unless absolutely necessary def displaySemiAttributes(self): print(f"The semi has {self.noOfTrailersAttached} trailers attached", f"and {self.axleCount} axles.") def travel(self): print(self.status) - + def loadCargo(self, _weight): + super().loadCargo(_weight) + self.status = "Loading cargo" + # How do I access the set value in the init above? + self.weight = _weight + input(f"Enter cargo weight (integer values only).") + try: + weight = int(weight) + except ValueError: + self.weight = _weight + input(f"That entry was not an integer. Enter cargo weight in integers only.") + ''' + Error message + Enter cargo weight (integer values only).70000 +Traceback (most recent call last): + File "/Users/jason.ross/workspace/python_oop_practice/vehicle_oop_example/fleet.py", line 126, in + STruck.loadCargo(90000) + File "/Users/jason.ross/workspace/python_oop_practice/vehicle_oop_example/fleet.py", line 113, in loadCargo + self.weight = _weight + input(f"Enter cargo weight (integer values only).") +TypeError: unsupported operand type(s) for +: 'int' and 'str' + ''' STruck = semi_Truck(2, 4, True, [40.0000, 40.0000], [56.0000, 89.0000], 60, 90000, "mph", "lbs,", "Ready", "1F8HNG7", "diesel") @@ -105,7 +132,7 @@ STruck = semi_Truck(2, 4, True, [40.0000, 40.0000], [56.0000, 89.0000], 60, 9000 # Remember to be careful to check that you are running the method on an instance of the class, not on the class definition. STruck.displaySemiAttributes() -print(STruck.isModular) +STruck.loadCargo(90000) # Next - implement abstracted methods and constructor classes. Demonstrate use of super if Python supports it. -- 2.47.3 From 75c182c85bfb2c3dd902f94dd9a9cf1fe373aa9b Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Fri, 15 Jul 2022 12:56:15 -0500 Subject: [PATCH 09/14] presentation push --- fleet.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/fleet.py b/fleet.py index 9e059cb..4003e09 100644 --- a/fleet.py +++ b/fleet.py @@ -33,9 +33,8 @@ class Vehicle(ABC): self.status = _status self.fuelType = _fuelType - @abstractmethod def loadCargo(self, _weight): - pass + self.status = "loading cargo" class fuelTypes(enum.Enum): Diesel = 1 @@ -108,13 +107,12 @@ class semi_Truck(RoadVehicle, GroundVehicleInterface): print(self.status) def loadCargo(self, _weight): super().loadCargo(_weight) - self.status = "Loading cargo" # How do I access the set value in the init above? - self.weight = _weight + input(f"Enter cargo weight (integer values only).") try: - weight = int(weight) + self.weight = _weight + int(input(f"Enter cargo weight (integer values only).")) + print(f"The semi truck is {self.status}.", f"Cargo weight is {self.weight}.") except ValueError: - self.weight = _weight + input(f"That entry was not an integer. Enter cargo weight in integers only.") + self.weight = _weight + int(input(f"That entry was not an integer. Enter cargo weight in integers only.")) ''' Error message Enter cargo weight (integer values only).70000 @@ -135,6 +133,7 @@ STruck.displaySemiAttributes() STruck.loadCargo(90000) + # Next - implement abstracted methods and constructor classes. Demonstrate use of super if Python supports it. # Use these values in instances and show that values can be accessed regardless of what level they live on. # Make vehicle objects, add them to the fleet, and then have displayAllVehicles show the newly added vehicle. Stretch/further off, have different types of vehicles travel regardless of type. -- 2.47.3 From e4e9c8c7093d29835faacc734aeee82553ebec13 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Fri, 15 Jul 2022 12:59:59 -0500 Subject: [PATCH 10/14] fixed except statement --- fleet.py | 1 + 1 file changed, 1 insertion(+) diff --git a/fleet.py b/fleet.py index 4003e09..bc5a1eb 100644 --- a/fleet.py +++ b/fleet.py @@ -113,6 +113,7 @@ class semi_Truck(RoadVehicle, GroundVehicleInterface): print(f"The semi truck is {self.status}.", f"Cargo weight is {self.weight}.") except ValueError: self.weight = _weight + int(input(f"That entry was not an integer. Enter cargo weight in integers only.")) + print(f"The semi truck is {self.status}.", f"Cargo weight is {self.weight}.") ''' Error message Enter cargo weight (integer values only).70000 -- 2.47.3 From dd2eeaaf5cd51af192cb41c59e9543f7fb1b5911 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Wed, 20 Jul 2022 10:40:22 -0500 Subject: [PATCH 11/14] beta factory class pattern --- Fleet.drawio.html | 2 +- fleet.py | 58 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/Fleet.drawio.html b/Fleet.drawio.html index fb658d5..867f068 100644 --- a/Fleet.drawio.html +++ b/Fleet.drawio.html @@ -6,7 +6,7 @@ -
+
diff --git a/fleet.py b/fleet.py index bc5a1eb..af401ce 100644 --- a/fleet.py +++ b/fleet.py @@ -35,6 +35,7 @@ class Vehicle(ABC): def loadCargo(self, _weight): self.status = "loading cargo" + print(self.status) class fuelTypes(enum.Enum): Diesel = 1 @@ -43,6 +44,19 @@ class fuelTypes(enum.Enum): Electricity = 4 Nuclear = 5 Aviation_Gas = 6 +class VehicleTypes(enum.Enum): +# This enum exists to provide easy access to the user creating a new object type + semi_truck = 1 + train = 2 + truck = 3 + van = 4 + car = 5 + hovercraft = 6 + barge = 7 + ship = 8 + airplane = 9 + helicopter = 10 + class GroundVehicleInterface(ABC): '''Resource on Python interfaces. This is duck typed. https://realpython.com/python-interface/ Interfaces make a contract that a method must be implemented if an interface is inherited and allows differing implementations of the same method.''' @abstractmethod @@ -87,10 +101,10 @@ class Train(GroundVehicle, GroundVehicleInterface): # Pattern used for loadCargo - https://stackoverflow.com/questions/38987072/how-to-force-integer-input-in-python-3-x -Train1 = Train(20, 2) +# Train1 = Train(20, 2) -Train1.displayCarCount() -Train1.displayLocomotiveCount() +# Train1.displayCarCount() +# Train1.displayLocomotiveCount() # At this point, my code should print out the values from the car count and the locomotive count. Further demonstration of abstract and concrete classes to follow. @@ -114,24 +128,27 @@ class semi_Truck(RoadVehicle, GroundVehicleInterface): except ValueError: self.weight = _weight + int(input(f"That entry was not an integer. Enter cargo weight in integers only.")) print(f"The semi truck is {self.status}.", f"Cargo weight is {self.weight}.") - ''' - Error message - Enter cargo weight (integer values only).70000 -Traceback (most recent call last): - File "/Users/jason.ross/workspace/python_oop_practice/vehicle_oop_example/fleet.py", line 126, in - STruck.loadCargo(90000) - File "/Users/jason.ross/workspace/python_oop_practice/vehicle_oop_example/fleet.py", line 113, in loadCargo - self.weight = _weight + input(f"Enter cargo weight (integer values only).") -TypeError: unsupported operand type(s) for +: 'int' and 'str' - ''' + +# https://realpython.com/factory-method-python/ +class VehicleSerializer(): + +# Create object first before asking for input. Then insert object + def inputFunc(user_input): + listVehicleTypes = print(map(VehicleTypes)) + user_input = [input("Enter vehicle weight"), ] + return listVehicleTypes, user_input + + def serialize(self, user_input, inputFunc): + serializer = inputFunc(user_input) + return serializer(user_input) -STruck = semi_Truck(2, 4, True, [40.0000, 40.0000], [56.0000, 89.0000], 60, 90000, "mph", "lbs,", "Ready", "1F8HNG7", "diesel") +# STruck = semi_Truck(2, 4, True, [40.0000, 40.0000], [56.0000, 89.0000], 60, 90000, "mph", "lbs,", "Ready", "1F8HNG7", "diesel") # When possible, build your argument requirements from the top down so that by the time you implement your concrete classes, you know all the data points that you need to include. # Remember to be careful to check that you are running the method on an instance of the class, not on the class definition. -STruck.displaySemiAttributes() +# STruck.displaySemiAttributes() -STruck.loadCargo(90000) +# STruck.loadCargo(90000) @@ -158,4 +175,13 @@ Semi_Truck1 = semi_Truck(f"This truck has {2} trailers attached", f"and {4} axle # addVehicle = Fleet("hovercraft", 50, "ground/water", 0) # vehicle1.displayAllFleetMembers() + +Error message + Enter cargo weight (integer values only).70000 +Traceback (most recent call last): + File "/Users/jason.ross/workspace/python_oop_practice/vehicle_oop_example/fleet.py", line 126, in + STruck.loadCargo(90000) + File "/Users/jason.ross/workspace/python_oop_practice/vehicle_oop_example/fleet.py", line 113, in loadCargo + self.weight = _weight + input(f"Enter cargo weight (integer values only).") +TypeError: unsupported operand type(s) for +: 'int' and 'str' ''' \ No newline at end of file -- 2.47.3 From 9792918f29e6a2a223d6763b8b9cf5a0c6f7bb91 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Thu, 21 Jul 2022 10:35:12 -0500 Subject: [PATCH 12/14] saving resources found on factories --- fleet.py | 96 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 34 deletions(-) diff --git a/fleet.py b/fleet.py index af401ce..5b9c441 100644 --- a/fleet.py +++ b/fleet.py @@ -1,4 +1,5 @@ from abc import ABC, abstractmethod +import random import enum # ABC is imported from the standard library since Python does not natively support abstract methods and classes. class Fleet(): @@ -46,7 +47,7 @@ class fuelTypes(enum.Enum): Aviation_Gas = 6 class VehicleTypes(enum.Enum): # This enum exists to provide easy access to the user creating a new object type - semi_truck = 1 + semi_Truck = 1 train = 2 truck = 3 van = 4 @@ -130,17 +131,30 @@ class semi_Truck(RoadVehicle, GroundVehicleInterface): print(f"The semi truck is {self.status}.", f"Cargo weight is {self.weight}.") # https://realpython.com/factory-method-python/ +# Factory method patterns, both standard and polymorphic - https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Factory.html#preventing-direct-creation class VehicleSerializer(): + Vehicle(object) + def vehicleGen(n): + for i in range(n): + yield random.choice([semi_Truck, Train]) + + def factory(type, vehicleGen): + try: + for vehicle in VehicleTypes: + if type == vehicle: + vehicleGen() + elif type == False: + "Bad vehicle creation: " + type + + except TypeError: + assert 0, "Bad vehicle creation: " + type + finally: + "Unexpected object creation error" + + # Create object first before asking for input. Then insert object - def inputFunc(user_input): - listVehicleTypes = print(map(VehicleTypes)) - user_input = [input("Enter vehicle weight"), ] - return listVehicleTypes, user_input - def serialize(self, user_input, inputFunc): - serializer = inputFunc(user_input) - return serializer(user_input) # STruck = semi_Truck(2, 4, True, [40.0000, 40.0000], [56.0000, 89.0000], 60, 90000, "mph", "lbs,", "Ready", "1F8HNG7", "diesel") @@ -156,32 +170,46 @@ class VehicleSerializer(): # Use these values in instances and show that values can be accessed regardless of what level they live on. # Make vehicle objects, add them to the fleet, and then have displayAllVehicles show the newly added vehicle. Stretch/further off, have different types of vehicles travel regardless of type. -''' -Code scratchpad + ''' + Code scratchpad -For the travel method implemented from the interface + For the travel method implemented from the interface -Vehicle.velocity = self.moveGroundVehicle - print("Traveling") + Vehicle.velocity = self.moveGroundVehicle + print("Traveling") + + Object implementation + + Semi_Truck1 = semi_Truck(f"This truck has {2} trailers attached", f"and {4} axles.", True, [49.8900000, 50.900000]) + + # vehicle1 = Fleet("car", 120, "ground", Fleet.vehicles[0]) + + # vehicle1.displayAllFleetMembers() + + # addVehicle = Fleet("hovercraft", 50, "ground/water", 0) + + # vehicle1.displayAllFleetMembers() + + Error message + Enter cargo weight (integer values only).70000 + Traceback (most recent call last): + File "/Users/jason.ross/workspace/python_oop_practice/vehicle_oop_example/fleet.py", line 126, in + STruck.loadCargo(90000) + File "/Users/jason.ross/workspace/python_oop_practice/vehicle_oop_example/fleet.py", line 113, in loadCargo + self.weight = _weight + input(f"Enter cargo weight (integer values only).") + TypeError: unsupported operand type(s) for +: 'int' and 'str' + + + Scrapped factory class + + def inputFunc(self, user_input, listVehicleTypes): + def _listVtypes(): + listVehicleTypes = print(map(VehicleTypes)) + return listVehicleTypes + user_input = [input("Enter vehicle weight"), ] + return listVehicleTypes, user_input -Object implementation - -Semi_Truck1 = semi_Truck(f"This truck has {2} trailers attached", f"and {4} axles.", True, [49.8900000, 50.900000]) - -# vehicle1 = Fleet("car", 120, "ground", Fleet.vehicles[0]) - -# vehicle1.displayAllFleetMembers() - -# addVehicle = Fleet("hovercraft", 50, "ground/water", 0) - -# vehicle1.displayAllFleetMembers() - -Error message - Enter cargo weight (integer values only).70000 -Traceback (most recent call last): - File "/Users/jason.ross/workspace/python_oop_practice/vehicle_oop_example/fleet.py", line 126, in - STruck.loadCargo(90000) - File "/Users/jason.ross/workspace/python_oop_practice/vehicle_oop_example/fleet.py", line 113, in loadCargo - self.weight = _weight + input(f"Enter cargo weight (integer values only).") -TypeError: unsupported operand type(s) for +: 'int' and 'str' -''' \ No newline at end of file + def serialize(self, user_input, inputFunc): + serializer = inputFunc(user_input, VehicleTypes) + return serializer(user_input) + ''' \ No newline at end of file -- 2.47.3 From e78c84d2050a0d8c15ffcf458dac9528500781c3 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Fri, 22 Jul 2022 13:00:15 -0500 Subject: [PATCH 13/14] completed basic factory --- fleet.py | 79 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 32 deletions(-) diff --git a/fleet.py b/fleet.py index 5b9c441..b7ebff5 100644 --- a/fleet.py +++ b/fleet.py @@ -5,14 +5,10 @@ import enum class Fleet(): fleet_members = [] - - def __init__(self, vehicleid): - self.uuid = vehicleid - - + def displayAllFleetMembers(self): for i in self.fleet_members: - print(i) + print(str(i)) def addVehicle(self, newVehicleId): self.fleet_members.append(newVehicleId) @@ -22,10 +18,12 @@ class Vehicle(ABC): # _fuelType = ("Diesel", "Gasoline", "Aviation Gas", "Electricity", "Nuclear", "100LL") # https://www.geeksforgeeks.org/inheritance-and-composition-in-python/ + + def __str__(self): + return str(self.vehicleType) @abstractmethod - def __init__(self, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType ): - self.vehicleObject = Fleet(vehicleid) + def __init__(self, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType, _vehicleType): self.gpsPosition = _gpsPosition self.velocity = _velocity self.velocityType = _velocityType @@ -33,6 +31,7 @@ class Vehicle(ABC): self.weightType = _weightType self.status = _status self.fuelType = _fuelType + self.vehicleType = _vehicleType def loadCargo(self, _weight): self.status = "loading cargo" @@ -67,8 +66,8 @@ class GroundVehicleInterface(ABC): class GroundVehicle(Vehicle): @abstractmethod - def __init__(self, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status,vehicleid, _fuelType): - super().__init__(_gpsPosition, _velocity, _weight, _velocityType, _weightType, _status,vehicleid, _fuelType) + def __init__(self, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status,vehicleid, _fuelType, _vehicleType): + super().__init__(_gpsPosition, _velocity, _weight, _velocityType, _weightType, _status,vehicleid, _fuelType, _vehicleType) self.isModular = _isModular # modifier to GPS position self.moveGroundVehicle = _moveGroundVehicle @@ -76,12 +75,14 @@ class GroundVehicle(Vehicle): class RoadVehicle(GroundVehicle): @abstractmethod - def __init__(self, _axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType): - super().__init__(_isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType) + def __init__(self, _axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType, _vehicleType): + super().__init__(_isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType, _vehicleType) self.axleCount = _axlecount + # Put class specific variables at the end of the constructor/super call class Train(GroundVehicle, GroundVehicleInterface): - def __init__(self, _noOfCarsAttached, _locomotiveCount): + def __init__(self, _noOfCarsAttached, _locomotiveCount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType): + super().__init__(_isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType, VehicleTypes.train) self.noOfCarsAttached = _noOfCarsAttached self.locomotiveCount = _locomotiveCount @@ -114,7 +115,7 @@ class semi_Truck(RoadVehicle, GroundVehicleInterface): def __init__(self, _noOfTrailersAttached, _axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType): self.noOfTrailersAttached = _noOfTrailersAttached # super in Python returns an object representing the parent class and - super().__init__(_axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType) + super().__init__(_axlecount, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status, vehicleid, _fuelType, VehicleTypes.semi_Truck) # Don't modify passed in values unless absolutely necessary def displaySemiAttributes(self): print(f"The semi has {self.noOfTrailersAttached} trailers attached", f"and {self.axleCount} axles.") @@ -132,24 +133,38 @@ class semi_Truck(RoadVehicle, GroundVehicleInterface): # https://realpython.com/factory-method-python/ # Factory method patterns, both standard and polymorphic - https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Factory.html#preventing-direct-creation -class VehicleSerializer(): - Vehicle(object) - def vehicleGen(n): - for i in range(n): - yield random.choice([semi_Truck, Train]) +class VehicleFactory(): + def factory(self, _type): + # try: + if _type == VehicleTypes.semi_Truck: + + truckToBuild = semi_Truck(2, 4, True, [40.0000, 50.0000], [30.0000, 20.000], 60, 90000, "mph", "lbs", "traveling", "307UNHL", fuelTypes.Diesel) + return truckToBuild + elif _type == VehicleTypes.train: + return Train(4, 5,True, [40.0000, 50.0000], [30.0000, 20.000], 60, 90000, "mph", "lbs", "traveling", "307UNHL", fuelTypes.Diesel ) + else: + "Bad vehicle creation: " + str(type) + # except TypeError: + # print("Unexpected object creation error") - def factory(type, vehicleGen): - try: - for vehicle in VehicleTypes: - if type == vehicle: - vehicleGen() - elif type == False: - "Bad vehicle creation: " + type - - except TypeError: - assert 0, "Bad vehicle creation: " + type - finally: - "Unexpected object creation error" +ShippingFleet = Fleet() + +factory1 = VehicleFactory() + +SemiTruck1 = factory1.factory(VehicleTypes.semi_Truck) + +# print(SemiTruck1) + +Train1 = factory1.factory(VehicleTypes.train) + +ShippingFleet.addVehicle(Train1) + +ShippingFleet.addVehicle(SemiTruck1) + +# ShippingFleet.displayAllFleetMembers() + +ShippingFleet.displayAllFleetMembers() + @@ -170,7 +185,7 @@ class VehicleSerializer(): # Use these values in instances and show that values can be accessed regardless of what level they live on. # Make vehicle objects, add them to the fleet, and then have displayAllVehicles show the newly added vehicle. Stretch/further off, have different types of vehicles travel regardless of type. - ''' +''' Code scratchpad For the travel method implemented from the interface -- 2.47.3 From 4f40a79085b347efd4adb52ebc614a3ec4926d96 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Tue, 9 Aug 2022 10:33:02 -0500 Subject: [PATCH 14/14] saving final changes --- fleet.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/fleet.py b/fleet.py index b7ebff5..ad174d4 100644 --- a/fleet.py +++ b/fleet.py @@ -69,9 +69,8 @@ class GroundVehicle(Vehicle): def __init__(self, _isModular, _moveGroundVehicle, _gpsPosition, _velocity, _weight, _velocityType, _weightType, _status,vehicleid, _fuelType, _vehicleType): super().__init__(_gpsPosition, _velocity, _weight, _velocityType, _weightType, _status,vehicleid, _fuelType, _vehicleType) self.isModular = _isModular - # modifier to GPS position self.moveGroundVehicle = _moveGroundVehicle - + # modifier to GPS position class RoadVehicle(GroundVehicle): @abstractmethod @@ -137,13 +136,14 @@ class VehicleFactory(): def factory(self, _type): # try: if _type == VehicleTypes.semi_Truck: - + truckToBuild = semi_Truck(2, 4, True, [40.0000, 50.0000], [30.0000, 20.000], 60, 90000, "mph", "lbs", "traveling", "307UNHL", fuelTypes.Diesel) return truckToBuild + # Make these parameters and not hard coded values elif _type == VehicleTypes.train: - return Train(4, 5,True, [40.0000, 50.0000], [30.0000, 20.000], 60, 90000, "mph", "lbs", "traveling", "307UNHL", fuelTypes.Diesel ) + return Train(4, 5, True, [40.0000, 50.0000], [30.0000, 20.000], 60, 90000, "mph", "lbs", "traveling", "307UNHL", fuelTypes.Diesel ) else: - "Bad vehicle creation: " + str(type) + print("Bad vehicle creation: " + str(type)) # except TypeError: # print("Unexpected object creation error") -- 2.47.3