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

Make io functions accept file like objects #107

Closed
rwl opened this issue Dec 13, 2020 · 4 comments
Closed

Make io functions accept file like objects #107

rwl opened this issue Dec 13, 2020 · 4 comments
Assignees

Comments

@rwl
Copy link
Contributor

rwl commented Dec 13, 2020

Is your feature request related to a problem? Please describe.
Currently, if I have data in memory that I wish to parse into a System it has to be written out to a temporary file and the path passed to a read function from an io module.

Describe the solution you'd like
It would be good if the io modules had read_file functions that would accept file-like objects (e.g. StringIO). These could be called from the existing read functions that would still take paths and handle opening/closing files.

Describe alternatives you've considered
The xlsx module already supports this as the Pandas ExcelFile constructor accepts file-like objects. The json module is simple enough to just copy, but the matpower and psse modules call open.

@cuihantao
Copy link
Collaborator

Typed on phone, please excuse the formatting.

I will work on refactoring the parsers.

For the xlsx reader, I have to switch back to pandas.read_excel to specify openpyxl as the engine, since xlrd deprecated the support for xlsx file. But that shouldn’t make a difference for the purpose of reading from a file object.

@cuihantao
Copy link
Collaborator

cuihantao commented Dec 13, 2020

Fixed it in dfeea93.

Added a wrapper read_file_like(infile) in andes.io.__init__ to accept a file-like or a str for path.
Modified each parser to use it instead of directly calling open.

To create a system object and load from file, use

import andes

fd = open(andes.get_case("npcc/npcc.raw"))

ss = andes.System()  # args can go here.
ss.undill()
andes.io.psse.read(ss, fd)
ss.setup()

fd.close()

In order for Andes to output correctly, you will need to call ss.files.set("presumed_case_name") manually. I hope you won't mind checking the code in andes/variables/fileman.py.

Let me know if you have other suggestions.

@rwl
Copy link
Contributor Author

rwl commented Dec 14, 2020

Thank you very much for making these changes so quickly. This will be much more convenient than writing out to temporary files.

@rwl rwl closed this as completed Dec 14, 2020
@cuihantao
Copy link
Collaborator

You are welcome. As always, thank you for the suggestion.

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

2 participants