I am working on a simple library that will help me work with prescription drugs. For instance, DrugBank contains a list of FDA approved drugs, and, importantly, how these drugs interact with one another. So, for instance, we might have drug A, which might then interact with drugs B and C in some way.
In a previous project doing something similar, my approach consisted of two steps: 1. I would load and construct the objects from the file, and store the interacting drug IDs into some list. Then 2., after constructing all the objects and putting them in a global dictionary, I would iterate over each object and replace the string IDs with a reference to the actual object.
I am writing my code in Python, but that's not really important, I just want to know what a good way to structure this might be.
So, in Python, some pseudocode might look something like this:
- Read and create the drug objects
class Drug:
def __init__(self, id, name, interactions):
self.id = id
self.name = name
self.interactions = self.interactions # list[str]
so, I might construct my example drugs A, B, and C like this
drug_a = Drug("A", ["B", "C"])
drug_b = Drug("B", [])
drug_c = Drug("C", [])
and have them stored in some global dictionary e.g.
DRUGS = {"A": drug_a, "B": drug_b, "C": drug_c}
- Replace the drug ID strings in each Drug object with references to the actual drug objects
for drug in DRUGS.values():
drug.interactions = [DRUGS[d_id] for d_id in drug.interactions]
This works well enough, but I'm wondering whether there is a better way, or an established design pattern for something like this. This two-step approach seems suboptimal and very much ad-hoc. But I can't really see a better way of doing things, so any insight would be interesting.