2. Creating Custom Pharmacy Python Modules (.py Packages)
A Python module is simply a file containing Python definitions and statements ending with the `.py` extension. Grouping related mathematical formulas into modular files (e.g., `posology.py`, `pharmacokinetics.py`) allows clean imports into any project using `import posology` or `from posology import youngs_rule`.
Demonstrating how a standalone file `pharmatools.py` is written and imported.
# File: pharmatools.py
def half_life(ke):
"""Returns elimination half-life from Ke"""
return 0.693 / ke
def body_surface_area(weight_kg, height_cm):
"""Mosteller BSA Formula: sqrt((Height_cm * Weight_kg) / 3600)"""
import math
return math.sqrt((height_cm * weight_kg) / 3600.0)
# In another main script:
# import pharmatools
# bsa = pharmatools.body_surface_area(weight_kg=70, height_cm=175)
# print(f"Patient BSA: {bsa:.2f} m^2")- `import module_name`: Imports entire module into namespace.
- `from module_name import specific_func`: Imports selected functions directly.
- `if __name__ == '__main__':`: Prevents test code from running when the file is imported elsewhere.
कस्टम मॉड्यूल (.py फाइल) बनाकर हम अपने बनाए फॉर्मूलों को किसी भी दूसरे प्रोग्राम में `import` करके आसानी से इस्तेमाल कर सकते हैं।