grid-parity-exchange / Egret

Tools for building power systems optimization problems
Other
134 stars 54 forks source link

Empty dictionary returned in place of an error message #222

Open esrawli opened 3 years ago

esrawli commented 3 years ago

Sample code:

md = create_ModelData(matpower_fname)
dict(md.elements(element_type='hgfhgfjfjhfjhgfjhgfhgfhg'))

That produces an empty dictionary instead of throwing an error when the element_type argument does not exist.

esrawli commented 3 years ago

@carldlaird Here is the issue we discussed.

bknueven commented 3 years ago

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)
carldlaird commented 3 years ago

@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?

bknueven commented 3 years ago

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