-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Syntactic Sugar for Templated Classes #13
Labels
Comments
One possible solution with dicts: OffLatticeSimulation = {2: {2: OffLatticeSimulation2_2}}
VertexBasedCellPopulation = {2: VertexBasedCellPopulation2} |
Perhaps override simulation = OffLatticeSimulation[2, 2](cell_population) |
I like that, as long as it isn't too confusing in looking like an array, but makes a lot of sense compared to the ones above! |
One possible solution could be: class TemplateClassDict:
def __init__(self, template_dict):
self._dict = template_dict
def __getitem__(self, key):
return self._dict[key]
OffLatticeSimulation = TemplateClassDict(
{
(2, 2): OffLatticeSimulation_2_2,
(3, 3): OffLatticeSimulation_3_3,
}
) Which can be used like simulation = OffLatticeSimulation[2, 2](cell_population) |
kwabenantim
added a commit
that referenced
this issue
Sep 23, 2024
kwabenantim
added a commit
that referenced
this issue
Sep 23, 2024
kwabenantim
added a commit
that referenced
this issue
Sep 23, 2024
This was referenced Sep 24, 2024
kwabenantim
added a commit
that referenced
this issue
Sep 28, 2024
kwabenantim
added a commit
that referenced
this issue
Sep 28, 2024
Merged
kwabenantim
added a commit
that referenced
this issue
Sep 28, 2024
kwabenantim
added a commit
that referenced
this issue
Sep 28, 2024
This has been implemented as: class TemplateClassDict:
def __init__(self, template_dict):
self._dict = {}
for arg_tuple, cls in template_dict.items():
if not inspect.isclass(cls):
raise TypeError("Expected class, got {}".format(type(cls)))
if not isinstance(arg_tuple, Iterable):
arg_tuple = (arg_tuple,)
key = tuple(
arg.__name__ if inspect.isclass(arg) else str(arg) for arg in arg_tuple
)
self._dict[key] = cls
def __getitem__(self, arg_tuple):
if not isinstance(arg_tuple, Iterable):
arg_tuple = (arg_tuple,)
key = tuple(
arg.__name__ if inspect.isclass(arg) else str(arg) for arg in arg_tuple
)
return self._dict[key] An example of this is: CellsGenerator = TemplateClassDict(
{
("AlwaysDivideCellCycleModel", "2"): CellsGenerator_AlwaysDivideCellCycleModel_2,
("AlwaysDivideCellCycleModel", "3"): CellsGenerator_AlwaysDivideCellCycleModel_3,
("BernoulliTrialCellCycleModel", "2"): CellsGenerator_BernoulliTrialCellCycleModel_2,
("BernoulliTrialCellCycleModel", "3"): CellsGenerator_BernoulliTrialCellCycleModel_3,
}
) Which can be used like: gen = CellsGenerator["AlwaysDivideCellCycleModel", "2"]() # recommended
# OR
gen = CellsGenerator["AlwaysDivideCellCycleModel", 2]()
# OR
gen = CellsGenerator[AlwaysDivideCellCycleModel, "2"]()
# OR
gen = CellsGenerator[AlwaysDivideCellCycleModel, 2]() |
kwabenantim
added a commit
that referenced
this issue
Oct 8, 2024
kwabenantim
added a commit
that referenced
this issue
Oct 8, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Summary
Add a way to use instantiated C++ template classes from Python that looks a bit more like the C++ syntax.
For example:
The text was updated successfully, but these errors were encountered: