Open esrawli opened 3 years ago
@carldlaird Here is the issue we discussed.
I'm not certain Egret should throw an error here, despite what the docstring says. Prescient currently exploits this behavior to iterate over empty dictionaries when certain elements do not exist. For example, storage
is a perfectly valid element_type
which may or may not exist in a given ModelData
dictionary.
An aside: if you're running on current main
, the ModelData
constructor is now smart enough to select the right parser.
from egret.data.model_data import ModelData
## matpower_fname is a path to a matpower *.m file
md = ModelData(matpower_fname)
@bknueven What if we adopted a similar interface to the dictionary "get" method where you can specify a default if it doesn't exist. Then the call would look something like the following to get the behavior you requested and throw an error otherwise?
storage_elements = md.elements(element_type='storage', default=list())
thoughts?
Since ModelData.elements
is supposed to be a generator, I think we could have a simpler optional argument error_on_empty
(or a better name), which may default to True
. E.g.
# empty loop, the "bad" code is never executed
for en, e in md.elements(element_type='hgfhgfjfjhfjhgfjhgfhgfhg', error_on_empty=False):
foo = bar
# raises Exception since there is no element_type 'hgfhgfjfjhfjhgfjhgfhgfhg'
for en, e in md.elements(element_type='hgfhgfjfjhfjhgfjhgfhgfhg'):
pass
Sample code:
That produces an empty dictionary instead of throwing an error when the
element_type
argument does not exist.