Object-Oriented Programming Part-2 ================================== .. toctree:: :maxdepth: 1 Indices and tables ------------------ * :ref:`genindex` * :ref:`modindex` * :ref:`search` Introduction ------------ In the last topic, we defined a base class for batteries. Using inheritance, we can create different types of batteries, and specialize the characteristics of the base class battery to match. .. _section_heading-KR6_Battery: KR6_Battery ----------- The KR6 battery type is AA sized and uses NiCD chemistry. We can specialize the Battery base class for the KR6 battery as follows: * Derive a new class from the Battery base class * Reimplement the class variables with KR6 specific values * Reimplement the ``get_voltage()`` method with KR6 specific formula .. literalinclude:: BatteryOrigPkg/KR6.py :linenos: For very little work, we were able to create a new battery that took advantage of previously created logic for all but 5 of the class attributes. :numref:`figure-battery_class_diagram` represents the class diagram of the KR6 class that has been created: .. _figure-kr6_class_diagram: .. figure:: BatteryOrigPkg/kr6_class_diagram.png :scale: 80 % :align: center KR6 Class Diagram And we can see the fruits of our labor, a customized battery type! >>> # >>> # Import the battery type >>> # >>> from BatteryOrigPkg.KR6 import KR6 >>> # >>> # Create a KR6 battery >>> # batt0 = KR6("K7811") >>> # >>> # Get the eval'able form >>> # >>> repr(batt0) 'KR6("K7811", 0)' >>> # >>> # Get the human readable form >>> # >>> print(batt0) 0.9v (Charge:0% Size:AA Type:NiCd SN:"K7811") >>> # >>> # Change it's charge level >>> # >>> batt0.set_percent_charged(50) >>> print(batt0) 1.05v (Charge:50% Size:AA Type:NiCd SN:"K7811") >>> batt0.set_percent_charged(100) >>> print(batt0) 1.2v (Charge:100% Size:AA Type:NiCd SN:"K7811") .. _section_heading-HR14_Battery: HR14_Battery ------------ The HR14 battery type is C sized and uses NiMH chemistry. For this battery, we use an exponential curve to represent the discharge curve, which is easily handled using a customized ``get_voltage()`` method. .. literalinclude:: BatteryOrigPkg/HR14.py :linenos: Taking the HR14 battery for a spin... >>> # >>> # Import the battery type >>> # >>> from BatteryOrigPkg.HR14 import HR14 >>> # >>> # Create a HR14 battery >>> # batt1 = HR14("AF4194", 35) >>> # >>> # Get the eval'able form >>> # >>> repr(batt1) 'HR14("AF4194", 35)' >>> # >>> # Get the human readable form >>> # >>> print(batt1) 1.0244985104v (Charge:35% Size:C Type:NiMH SN:"AF4194") >>> # >>> # Change it's charge level >>> # >>> batt1.set_percent_charged(50) >>> print(batt1) 1.04999696v (Charge:50% Size:C Type:NiMH SN:"AF4194") >>> batt1.set_percent_charged(100) >>> print(batt1) 1.19998784v (Charge:100% Size:C Type:NiMH SN:"AF4194") .. _section_heading-LR4_Battery: LR4_Battery ----------- The LR4 battery type is AA sized using Alkaline chemistry. For this battery, the conversion from charge to voltage is based on a piecewise curve. Again, this is easily handled using a customized ``get_voltage()`` method. .. literalinclude:: BatteryOrigPkg/LR4.py :linenos: Taking the LR4 battery for a spin... >>> # >>> # Import the battery type >>> # >>> from BatteryOrigPkg.LR4 import LR4 >>> # >>> # Create a LR4 battery >>> # batt2 = LR4("GK245-01", 25) >>> # >>> # Get the eval'able form >>> # >>> repr(batt2) 'LR4("GK245-01", 25)' >>> # >>> # Get the human readable form >>> # >>> print(batt2) 1.042v (Charge:25% Size:AA Type:Alkaline SN:"GK245-01") >>> # >>> # Change it's charge level >>> # >>> batt2.set_percent_charged(50) >>> print(batt2) 1.124v (Charge:50% Size:AA Type:Alkaline SN:"GK245-01") >>> batt2.set_percent_charged(100) >>> print(batt2) 1.49v (Charge:100% Size:AA Type:Alkaline SN:"GK245-01") .. _section_heading-Playing_With_Batteries: Playing with Batteries ---------------------- Above, we created 3 batteries (one for each type). Lets try out the various operations on them: >>> # >>> # Getting properties of the battery >>> # >>> batt0.get_num_created() 3 >>> # Remember, the instance counter is >>> # on the base class and was never >>> # reimplemented, so, '3' makes sense. >>> # because we made 3 batteries. >>> # >>> batt0.get_form_factor() 'AA' >>> batt0.get_chemistry() 'NiCd' >>> batt0.get_serial_numer() 'K7811; >>> batt0.get_percent_charged() 100 >>> batt0.get_voltage() 1.2 >>> batt1.get_voltage() 1.1998784 >>> batt2.get_voltage() 1.49 >>> # >>> # Comparison Operations >>> # >>> batt0 == batt1 False >>> batt0 != batt1 True >>> batt0 <= batt1 False >>> batt0 < batt1 False >>> batt0 >= batt1 True >>> batt0 > batt1 True >>> # >>> # Numerical Operations >>> # >>> batt0 + batt1 2.3999878399999997 >>> batt0 - batt2 -0.29000000000000004 >>> int(batt0) 1 >>> float(batt0) 1.2