Skip to main content

A python package to read and edit nbt data

Project description

Build Status PyPI Version Python Version

A python library to read and edit nbt data. Also provides an api to define compound tag schemas in order to save some typing with recurring tag hierarchies. Requires python 3.6.

Installation

The package can be installed with pip.

$ pip install nbtlib

Basic usage

The following examples will give you a very basic overview of what you can do. For more advanced examples, check out the “Usage” notebook in the docs folder.

Reading files

Reading files can be done directly with the load() function. The root property contains the root nbt tag. Every nbt tag inherits from its python counterpart so you can use all the usual builtin operations on nbt tags.

from nbtlib import nbt

nbt_file = nbt.load('bigtest.nbt')
assert nbt_file.root['intTest'] == 2147483647

Editing files

You can use nbt files as context managers in order to save modifications automatically at the end of the with block.

from nbtlib import nbt
from nbtlib.tag import *

with nbt.load('demo.nbt') as demo:
    demo.root['counter'] = Int(demo.root['counter'] + 1)

You can also use the save() method.

from nbtlib import nbt
from nbtlib.tag import *

demo = nbt.load('demo.nbt')
demo.root['counter'] = Int(demo.root['counter'] + 1)
demo.save()

For more details check out the “Usage” notebook.

Using schemas

A schema lets you create compound tags that enforce a specific tag type for any given key.

from nbtlib import schema
from nbtlib.tag import *

MySchema = schema('MySchema', {
    'foo': String,
    'bar': Short
})

my_object = MySchema({'foo': 'hello world', 'bar': 21})
assert isinstance(my_object['foo'], String)

Nbt literals

nbtlib also defines utilities to deal with literal nbt data. For instance, you can parse nbt literals using the parse_nbt() function.

from nbtlib import parse_nbt
from nbtlib.tag import *

my_compound = parse_nbt('{foo:[hello,world],bar:[I;1,2,3]}')
assert my_compound == Compound({
    'foo': List[String](['hello', 'world']),
    'bar': IntArray([1, 2, 3])
})

Contributing

Contributions are welcome. Unit tests are built with pytest. You can run the test suite with:

$ python -m pytest tests

License: MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nbtlib-0.1.12-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file nbtlib-0.1.12-py3-none-any.whl.

File metadata

File hashes

Hashes for nbtlib-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 4533b00ddf23f647f4b058ba830d2099173ac3a5bd549eb5c184486c385bd107
MD5 37e3b91c4b3b64d0962fdba182033b8a
BLAKE2b-256 9d95418f917b2687a6db5dc2d7ca7a6c013167055551f8e3f3c3605a7f56afd6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page