Skip to content
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

Empty dictionary returned in place of an error message #222

Open
esrawli opened this issue Apr 19, 2021 · 4 comments
Open

Empty dictionary returned in place of an error message #222

esrawli opened this issue Apr 19, 2021 · 4 comments

Comments

@esrawli
Copy link
Contributor

esrawli commented Apr 19, 2021

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
Copy link
Contributor Author

esrawli commented Apr 19, 2021

@carldlaird Here is the issue we discussed.

@bknueven
Copy link
Collaborator

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
Copy link
Collaborator

@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
Copy link
Collaborator

bknueven commented Apr 19, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants