Giter VIP home page Giter VIP logo

Comments (2)

vberlier avatar vberlier commented on July 21, 2024

The idea is that path objects and strings representing path objects should be interchangeable. When you instantiate or concatenate a path with a string, the string represents a full nbt path, not an individual key, the string actually gets parsed into a path object. But when you index into a path you extend it by an individual key. That's why you can use numbers too, the string you use to subscript is a key. Path + str and Path[str] are not equivalent.

>>> Path("a.b") + Path("c.d")
Path('a.b.c.d')
>>> Path("a.b") + "c.d"
Path('a.b.c.d')
>>> Path("a.b")["c.d"]
Path('a.b."c.d"')

To me it makes sense to keep the meaning clear. We can magically make it work like you suggested but it will lead to inconsistencies.

from nbtlib.

MestreLion avatar MestreLion commented on July 21, 2024

I really *love Path's design and features, it's undoubtedly one of the most amazing features of nbtlib. But I don't see by your example and explanation why wouldn't int fit that design or create any inconsistency.

If I understood correctly, the design principles you mentioned are:

  • Path(x) constructor takes a full NBT path x, represented by a string or Path.
  • path + x extends a path by concatenating it with another full NBT path x, represented by a string or Path.
  • path[x] indexing appends a single key (or index) x, represented by a string (or int)

Is that correct?

  • What about indexing using another full NBT path? Currently we have Path("a.b")[Path("c.d")] == Path("a.b.c.d"). But anyway, that's not my point.

My question is: what inconsistency would emerge from allowing an int, say 1, to be treated like "[1]"? Sure, an int is only able to represent a single component, and always an index (not a key). So what? That's a limitation of int, and Path shouldn't care. Just like any single component is accepted as a full path, be it "a", "[1]", or even "", so could an int.

My proposal is to handle integers the same way you handle "[x]", no more, no less. You can even convert 1 to "[1]" upfront in __new__, __add__ and __radd__, and let it be parsed as a string.

I believe such approach would not break the API in any way, nor create any inconsistency. The key here (no pun intended) is that an int is always an index

from nbtlib.

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.