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

detect Path objects in argument iterable and auto-use ns.P #16

Closed
anntzer opened this issue Dec 18, 2014 · 4 comments
Closed

detect Path objects in argument iterable and auto-use ns.P #16

anntzer opened this issue Dec 18, 2014 · 4 comments

Comments

@anntzer
Copy link

anntzer commented Dec 18, 2014

It'd be nice if natsort autodetects whether its arguments are all Path objects (from the new stdlib's pathlib), converted them to strs, and turned on ns.P automatically (and possibly ns.IC on Windows too). I don't have a strong opinion of what to do if some arguments are Paths and others are not (I guess this should fail with TypeError since they are not technically comparable).

@SethMMorton
Copy link
Owner

I am very reluctant to have natsort do any automatic modification of the algorithm settings based on the input data because I don't want to remove control from the user. Also, scanning the input data and doing type checking like this for a less common use case would undo most of the optimizations I have added in the most recent releases.

I am wondering, is there a reason that you aren't sure the type of the incoming data? If you know that you want to sort paths, you can just always add the ns.P modifier since non-path strings will still get sorted correctly, it will just eat up some runtime.

Would the following be acceptable alternatives?

>>> # Convert to string if the input is a Path object.
>>> natsorted(mylist, key=lambda x: str(x) if isinstance(x, pathlib.Path) else x, alg=ns.P)

or

>>> # Just convert everything to strings.
>>> natsorted(mylist, key=lambda x: str(x), alg=ns.P)

I might be open to adding an option that will do the checking and turning on of the path algorithm, but the user has to request this explicitly.

@anntzer
Copy link
Author

anntzer commented Dec 19, 2014

Oh, of course I know the type of the data and key argument works. At the end it's all about practicality vs. speed. What about using the first key argument (convert only Path instances to str) when ns.P is on?

@SethMMorton
Copy link
Owner

I will look into this.

@SethMMorton
Copy link
Owner

I have fixed this in a recent commit. I will release as an incremental update (version 3.5.2) in a few days.

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

No branches or pull requests

2 participants