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

🐍 Be more lenient when accepting args to file I/O #1819

Merged
merged 10 commits into from
Feb 10, 2022

Conversation

thorstenhater
Copy link
Contributor

@thorstenhater thorstenhater commented Jan 27, 2022

PyArbor I/O routines now try to cast filename arguments to a string.
This allows passing pathlib.Path objects.

Example

here = Path(__file__).parent
cat = A.load_catalogue(here / 'local-catalogue.so')

Copy link
Member

@bcumming bcumming left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small suggestion to improve robustness.

namespace py = pybind11;

arborio::cable_cell_component load_component(py::object fn) {
const std::string fname = py::str(fn);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are using the py::object as though it can be converted to a std::string, without checking.

The errors when a type that can't be converted to string, or converts to an odd string, is passed.

How about writing a little helper function that tests the type and converts it to a string, or throws an exception with a helpful message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought you might bring up this argument. Essentially we are calling foo.__str__ here, which is essentially in line with Python's behaviour. We have two options:

  1. Restrict to a known good set of py-types (String, Path, ...) trying each in turn
  2. Accept all the odd things that can happen when calling eg arbor.load_swc(42)

I tend to option 1 as well, but wanted to have a neutral verdict first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... option 1!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Pythonic way is to try and handle errors (which can just be "Unknown input provided, aborting...").

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we should be taking FileHandles, including StringIO. But that is probably not possible.

std::ifstream fid{fname};
if (!fid.good()) {
throw pyarb_error("Can't open file '{}'" + fname);
throw pyarb_error(util::pprintf("Can't open file '{}'", fname));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be happy to have a new-style typed error here, but I'll leave that up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, I should remember my own demands.

@bcumming bcumming merged commit 2176e15 into arbor-sim:master Feb 10, 2022
@thorstenhater thorstenhater deleted the py/pathlib branch August 1, 2022 07:40
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

Successfully merging this pull request may close these issues.

4 participants