Comments (2)
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.
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 pathx
, represented by a string or Path.path + x
extends a path by concatenating it with another full NBT pathx
, 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)
- servers.dat issues. HOT 1
- Root creates an spurious Compound that is not in actual NBT data HOT 19
- Preserve type on List.copy() and slicing HOT 5
- Improve `read_numeric()` to vastly increase `parse()` performance for all tags HOT 4
- Array tags are immutable views, not editable ndarrays HOT 9
- `File` is not writing the trailing End tag HOT 4
- `nbtlib 2.0` HOT 16
- Add `Path.from_parts()` or similar to allow tuple constructor like `pathlib.Path` HOT 8
- Investigate schema improvements/autocast HOT 7
- How to add a server in servers.dat? HOT 2
- Iterate NBT HOT 4
- Can't read FTB Quests NBT HOT 2
- How to get JSON info as intuitively as CLI? HOT 1
- if just have one value in list ,i got error HOT 2
- Use modified utf-8 for encoding and decoding strings HOT 2
- nbtlib.Long can not set 64bytes value HOT 1
- a minor issue of index of list.
- How to use/initialize nbtlib.ByteArray? HOT 1
- Jython compatibility
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nbtlib.