Code Monkey home page Code Monkey logo

Comments (4)

SethMMorton avatar SethMMorton commented on August 13, 2024

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.

from natsort.

anntzer avatar anntzer commented on August 13, 2024

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?

from natsort.

SethMMorton avatar SethMMorton commented on August 13, 2024

I will look into this.

from natsort.

SethMMorton avatar SethMMorton commented on August 13, 2024

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

from natsort.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.