GIAT convert_mass example from class
This commit is contained in:
@@ -30,3 +30,4 @@ $ pip install -e .
|
||||
- *Python Typing is for Humans*: [Unital.dev blog](https://www.unital.dev/blog/python-typing-is-for-humans/) or [LinkedIn](https://www.linkedin.com/pulse/python-typing-humans-corran-webster-u66ee)
|
||||
- [PyData Sphinx Theme](https://pydata-sphinx-theme.readthedocs.io/en/stable/)
|
||||
- [Sphinx AutoAPI](https://sphinx-autoapi.readthedocs.io/en/latest/)
|
||||
- Find the refactoring Give It A Try in `files/convert_mass.py`
|
||||
|
||||
27
files/convert_mass.py
Normal file
27
files/convert_mass.py
Normal file
@@ -0,0 +1,27 @@
|
||||
''' Refactor the following code
|
||||
mass_oz = [0.5, 0.6, 1.2, 3.4]
|
||||
mass_g = [mass * 28.3496 for mass in mass_oz]
|
||||
mass_kg = [mass * 28.3496 / 1000 for mass in mass_oz]
|
||||
'''
|
||||
|
||||
# Refactor Step 1
|
||||
G_OZ = 28.3496
|
||||
KG_OZ = G_OZ / 1000
|
||||
|
||||
mass_oz = [0.5, 0.6, 1.2, 3.4]
|
||||
mass_g = [mass * G_OZ for mass in mass_oz]
|
||||
mass_kg = [mass * KG_OZ for mass in mass_oz]
|
||||
|
||||
# Refactor Step 2
|
||||
def convert_mass(mass_oz, conv_factor):
|
||||
return [m * conv_factor for m in mass_oz]
|
||||
|
||||
# Refactor Step 3
|
||||
if __name__ == "__main__":
|
||||
mass_oz = [0.5, 0.6, 1.2, 3.4, 5.6, 7.8, 9.1]
|
||||
print("Ounces")
|
||||
print(", ".join(str(m) for m in mass_oz))
|
||||
print("Grams")
|
||||
print(", ".join(str(m) for m in convert_mass(mass_oz, G_OZ)))
|
||||
print("Kilograms")
|
||||
print(", ".join(str(m) for m in convert_mass(mass_oz, KG_OZ)))
|
||||
Reference in New Issue
Block a user