Compare commits

..

2 Commits

Author SHA1 Message Date
830eda6e1c GIAT convert_mass example from class 2025-10-22 13:21:46 -05:00
68d6df3a4f add __name__ == '__main__' block in fancy math 2025-10-22 13:19:34 -05:00
3 changed files with 37 additions and 2 deletions

View File

@@ -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) - *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/) - [PyData Sphinx Theme](https://pydata-sphinx-theme.readthedocs.io/en/stable/)
- [Sphinx AutoAPI](https://sphinx-autoapi.readthedocs.io/en/latest/) - [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
View 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)))

View File

@@ -13,3 +13,10 @@ def fact(n: int) -> int:
return 1 return 1
else: else:
return n * fact(n - 1) return n * fact(n - 1)
if __name__ == '__main__':
n = 5
assert fact(n) == 120
assert add(5, 6) == 11
print("Tests passed")