Comments (13)
I have Implemented nbt paths and its methods.
but it need optimizing.
I have implemented attach parts parent name str repr join match(NBT)
and a small parser.
from nbtlib.
You're fast! I'm going to check it out as soon as possible but I'm wondering why the bug you mentioned would be related to this. I mean the compound literal isn't part of the path itself, right?
In my mind, the path api would simply make it possible to access deeply nested values in nbt structures and provide a seamless way to generate new path strings. We could also implement a few ways of validating a path against nbt data or schemas but other than that I don't think there would be a lot to it.
path = Path('foo[0].bar')
data = parse_nbt('{foo: [{bar: "Hello, world!"}]}')
# Access deep values with indexing
assert data[path] == 'Hello, world!'
# Manipulate the path (.parent and indexing)
assert path.parent['spam'][Path('egg[0]')] == 'foo[0].spam.egg[0]'
# Maybe also add something to check if the selected field exists
assert data.get(path) != None
assert path in data
# Would be nice to make it work with schemas too
DataSchema = schema('DataSchema', {'foo': List[schema('Entry', {'bar': String})]})
assert path in DataSchema
from nbtlib.
from nbtlib.
I wrote a new parser function and i hope it is better than the older one.:smile:
please replace the old one and check if there any difference between this and minecraft.
def parser(string):
if string.startswith("{"):
tag,string=slice_nbt(string)
yield (None,tag)
elif string!=".":
string="."+string
while string and string!=".":
if string.startswith("[") or string.startswith(".["):#[]
r=re.match("\.?\[(-?\d+)?\]",string)
x=r.groups()[0]
if x:x=int(x)
string=string[r.end():]
yield x
else:
if string.startswith('."'):#""
r=re.match(r'\.\"((\\\"|\\\\|[^\\\"])*)\"',string)
t=r.groups()[0]
t="\"".join("\\".join(t.split("\\\\")).split("\\\""))
string=string[r.end():]
else:
r=re.match('\\.[^\[\]\{\}\. \"]+',string)
t=r.group()[1:]
string=string[r.end():]
if string.startswith('{'):
tag,string=slice_nbt(string)
yield (t,tag)
else:
yield (t,None)
from nbtlib.
I was reading minecraft source code just now.
It seems that un-quoted keys name can contain any character but
space " [ ] { } .
and can only be quoted by double quote mark.
And.Please note that there is a WAI behaviour:
bugs.mojang.com/browse/MC-139625
Please note its comments too.some of comments are made by mojang.
from nbtlib.
But,I have already implemented compound literal in the path object......
add that right after a key's name to make sure it has some key-value pair....
just like in minecraft commands.
I used -
to join 2 path together(maybe I should change to "|"?),and use []
to attach something to a path.
I use NBT_Path().match(
some nbt)
to test... return a list of all matched NBT object.
I did not come up with in
you just said..I will probably write that soon
from nbtlib.
You can do that already:(maybe I should change "-"to "|"?)
>>> path = nbtlib.path.NBT_Path('foo[0].bar')
>>> data = nbtlib.parse_nbt('{foo: [{bar: "Hello, world!"}]}')
>>> path.parent
NBT_Path('foo.[0].')
>>> path.parent-"egg[0]" #join
NBT_Path('foo.[0].egg.[0].')
>>> path.match(data) #match NBT
[String('Hello, world!')]
>>> path.parent-"egg[0]"==path.parent["egg"][0] #attach
True
>>> str(path.parent-"egg[0]")
'foo.[0].egg.[0].'
from nbtlib.
Could you please have a look at my code? @vberlier
from nbtlib.
well :/
I think it is not possible to support schema as it is often unpossible to determine if the path is in the schema.
I will leave it like that.
from nbtlib.
and i found there are some difference between my parser and minecraft in some edge cases.
(all vilid spath can be parsed,but some not vilid ones can too)
i will try to fix it.
i dont know if i could do that but i will try.
from nbtlib.
I forgot a usage ![{}]
I will fix it asap.
from nbtlib.
done
from nbtlib.
Hello! I'm back from vacation and I took some time to think about the problem. I also reviewed your pull request. I think in the end, mimicking pathlib doesn't really bring any benefit to the API. In pathlib, paths are analogous to strings but here it's more helpful to think of them as collections of object accessors.
Using this approach I managed to easily implement get, set and delete operations and integrate them into the tag API.
# Regular string key
key = 'spam'
data = parse_nbt('{spam: {egg: 42}}')
print(data[key]) # {egg: 42}
# Same code but the key is a path instead
key = Path('spam.egg')
data = parse_nbt('{spam: {egg: 42}}')
print(data[key]) # 42
# Also works seamlessly for setting and deleting
data[key] = Int(0)
print(data) # {spam: {egg: 0}}
del data[key]
print(data) # {spam: {}}
# Also this is what I mean by "collections of accessors"
assert tuple(key) == (NamedKey('spam'), NamedKey('egg'))
assert tuple(Path('hello[0].world{foo: "bar"}')) == (
NamedKey('hello'),
ListIndex(0),
NamedKey('world'),
CompoundMatch(Compound({'foo': String('bar')}))
)
The pull request for this is here feel free to try it out #38. I think the API design and implementation are significantly better than what I originally had in mind.
from nbtlib.
Related Issues (20)
- servers.dat issues. HOT 1
- Root creates an spurious Compound that is not in actual NBT data HOT 19
- `Path` cannot add or init integers, only strings and other Paths HOT 2
- 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.