Giter VIP home page Giter VIP logo

Comments (13)

ch-yx avatar ch-yx commented on August 22, 2024 1

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.

#35

from nbtlib.

vberlier avatar vberlier commented on August 22, 2024 1

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.

vberlier avatar vberlier commented on August 22, 2024 1

from nbtlib.

ch-yx avatar ch-yx commented on August 22, 2024 1

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.

ch-yx avatar ch-yx commented on August 22, 2024

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.

ch-yx avatar ch-yx commented on August 22, 2024

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.

ch-yx avatar ch-yx commented on August 22, 2024

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.

ch-yx avatar ch-yx commented on August 22, 2024

Could you please have a look at my code? @vberlier

from nbtlib.

ch-yx avatar ch-yx commented on August 22, 2024

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.

ch-yx avatar ch-yx commented on August 22, 2024

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.

ch-yx avatar ch-yx commented on August 22, 2024

I forgot a usage ![{}]
I will fix it asap.

from nbtlib.

ch-yx avatar ch-yx commented on August 22, 2024

done

from nbtlib.

vberlier avatar vberlier commented on August 22, 2024

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)

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.