ftb-snbt-lib


Nameftb-snbt-lib JSON
Version 0.4.0 PyPI version JSON
download
home_pageNone
SummaryA python library to parse, edit, and save FTB snbt tag, which is a variant of the "vanilla" snbt tag.
upload_time2024-06-10 07:54:57
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseNone
keywords minecraft ftb snbt parser library
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # ftb-snbt-lib

![GitHub Release](https://img.shields.io/github/v/release/peunsu/ftb-snbt-lib?style=for-the-badge)

**A python library to parse, edit, and save FTB snbt tag.**

The FTB snbt tag is a variant of the "vanilla" snbt tag. It has no commas at end of lines, different suffixes for numeric values, and no support for array data type.

This is the example of the FTB snbt tag:

```python
{
    some_tag: "some_value"
    another_tag: 1b
    list_tag: [
        "a"
        "b"
        "c"
    ]
}
```

**This library is only for the FTB snbt tag**. If you are finding the snbt library for the "vanilla" snbt tag, use [nbtlib](https://github.com/vberlier/nbtlib) by [vberlier](https://github.com/vberlier).

## Installation

The package can be installed with ``pip``.

```bash
$ pip install ftb-snbt-lib
```

## Getting Started

* Import the library.

```python
>>> import ftb_snbt_lib as slib
```

* ``load(fp)``: Load the ftb snbt tag from a file (``fp``).<br>
The type of returned value is ``Compound``, a dictionary-like object.<br>
The ``Compound`` is containing values with **[tag data types](#data-types)** provided by this library.

```python
>>> some_snbt = slib.load(open("tests/some_file.snbt", "r", encoding="utf-8"))
>>> type(some_snbt)
<class 'ftb_snbt_lib.tag.Compound'>
>>> print(some_snbt)
Compound({'some_tag': String('some_value'), 'another_tag': Byte(1)})
```

* ``dump(tag, fp, comma_sep=False)``: Dump the ftb snbt tag to a file (``fp``).<br>
If you set ``comma_sep`` parameter to ``True``, the output snbt has comma separator ``,\n`` instead of non-comma separator ``\n``.

```python
>>> slib.dump(some_snbt, open("tests/some_file_copy.snbt", "w", encoding="utf-8"))
# File Output:
# {
#    some_tag: "some_value"
#    another_tag: 1b
# }
```

```python
>>> slib.dump(some_snbt, open("tests/some_file_copy.snbt", "w", encoding="utf-8"), comma_sep=True)
# File Output:
# {
#    some_tag: "some_value",
#    another_tag: 1b
# }
```

* ``loads(s)``: Load the ftb snbt tag from a string ``s``.<br>
The type of returned value is ``Compound``.

```python
>>> another_snbt = slib.loads('''
... {
...     some_tag: "some_value"
...     another_tag: 1b
... }
... ''')
>>> type(another_snbt)
<class 'ftb_snbt_lib.tag.Compound'>
>>> print(another_snbt)
Compound({'some_tag': String('some_value'), 'another_tag': Byte(1)})
```

* ``dumps(tag, comma_sep=False)``: Dump the ftb snbt tag to a string.<br>
If you set ``comma_sep`` parameter to ``True``, the output snbt has comma separator ``,\n`` instead of non-comma separator ``\n``.

```python
>>> dumped_snbt = slib.dumps(another_snbt)
>>> print(dumped_snbt)
{
    some_tag: "some_value"
    another_tag: 1b
}
```

```python
>>> dumped_snbt = slib.dumps(another_snbt, comma_sep=True)
>>> print(dumped_snbt)
{
    some_tag: "some_value",
    another_tag: 1b
}
```

* Edit the snbt tag. As its type is ``Compound``, it can be edited like a dictionary.<br>
The inserted or replace values should be any of **[tag data types](#data-types)** provided by this library.

```python
>>> another_snbt["some_tag"] = slib.String("another_value")
```

* When editing the ``List``, a list-like object, its elements must have **the same type**.<br>
For instance, ``List[Byte(1), Byte(2), Byte(3)]`` must contain **only** the ``Byte`` type object, so the other types like ``Integer`` or ``String`` **cannot be added or replaced** in it.

* When editing the ``Array``, a list-like object with **a fixed length**, its elements must have **the same type**, and that type must **match the element type defined in the array**.<br>
For instance, ``IntArray`` with a length of 3 must contain **three** ``Integer`` type objects, so **adding new objects, removing existing objects, and replacing with other type objects are not allowed**.

* Save the edited snbt tag to a json file.

```python
>>> import json
>>> json.dump(another_snbt, open("tests/test.json", "w", encoding="utf-8"), indent=4, ensure_ascii=False)
```

## Data Types

| Type | Description | Format | Example |
| - | - | - | - |
| Byte | A signed 8-bit integer.<br>Range: ``-128`` ~ ``127`` | ``<number>b`` | ``12b``, ``-35b`` |
| Short | A signed 16-bit integer.<br>Range: ``-32,768`` ~ ``32,767`` | ``<number>s`` | ``132s``, ``-243s`` |
| Integer | A signed 32-bit integer.<br>Range: ``-2,147,483,647`` ~ ``2,147,483,647`` | ``<number>`` | ``12345`` |
| Long | A signed 64-bit integer.<br>Range: ``9,223,372,036,854,775,808`` ~ ``9,223,372,036,854,775,807`` | ``<number>L`` | ``12345L`` |
| Float | A 32-bit, single-precision floating-point number.<br>Range: ``-3.4E+38`` ~ ``+3.4E+38`` | ``<number>f`` | ``12.345f``, ``1.0E-6f`` |
| Double | A 64-bit, double-precision floating-point number.<br>Range: ``-1.7E+308`` ~ ``+1.7E+308`` | ``<number>d`` | ``12.345d``, ``1.0E-6d`` |
| Bool | A boolean data type.<br>``0`` for ``false``, ``1`` for ``true``. | ``false``, ``true`` | ``true`` |
| String | A sequence of characters. | A string enclosed in **double quotes ``""``**.<br>Nested double quotes can be included within a string using a **escaping character ``\"``**. | `"Hello, World!"`,<br>`"Say \"Hello, World!\""` |
| List | An ordered list of tags.<br>The tags must be of **the same type**, determined by the first tag in the list. | Unnamed tags enclosed in square brackets and delimited by **newline** characters (``\n``). | <pre>[<br>    3.2d<br>    1.4d<br>    ...<br>]</pre> |
| Array | An ordered list of 8-bit(``ByteArray``), 32-bit(``IntArray``), 64-bit(``LongArray``) integers. | ``<array_prefix>;`` followed by an ordered list of tags enclosed in square brackets and delimited by **newline** characters (``\n``).<br>Valid array prefixes are ``B``(Byte), ``I``(Integer), and ``L``(Long). | <pre>[B;<br>    12b<br>    -35b<br>    ...<br>]</pre> |
| Compound | An ordered list of attribute-value pairs.<br>Each tag can be of **any type**. | Named tags enclosed in curly braces and delimited by commas or **newline** characters (``\n``).<br>The key (tag name) can be unquoted if it contains only ``0-9``, ``A-Z``, ``a-z``, ``_``, ``-``, ``.``, and ``+``. Otherwise the key should be quoted, using the format of ``String`` type. | <pre>[<br>    tag1: "string"<br>    tag2: 12b<br>    \"quoted:tag\": 3.5d<br>    ...<br>]</pre> |

## References

* [PLY - Python Lex-Yacc](https://github.com/dabeaz/ply) by [David Beazley](https://www.dabeaz.com)
* [nbtlib](https://github.com/vberlier/nbtlib) by [vberlier](https://github.com/vberlier)
* [NBT format - Minecraft Wiki (fandom)](https://minecraft.fandom.com/wiki/NBT_format)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "ftb-snbt-lib",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "minecraft, ftb, snbt, parser, library",
    "author": null,
    "author_email": "peunsu <peunsu55@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/34/c6/cb2b3da94d2e789821de3b9797ea131c5d5e213873a510d4765597c0bf5d/ftb_snbt_lib-0.4.0.tar.gz",
    "platform": null,
    "description": "# ftb-snbt-lib\n\n![GitHub Release](https://img.shields.io/github/v/release/peunsu/ftb-snbt-lib?style=for-the-badge)\n\n**A python library to parse, edit, and save FTB snbt tag.**\n\nThe FTB snbt tag is a variant of the \"vanilla\" snbt tag. It has no commas at end of lines, different suffixes for numeric values, and no support for array data type.\n\nThis is the example of the FTB snbt tag:\n\n```python\n{\n    some_tag: \"some_value\"\n    another_tag: 1b\n    list_tag: [\n        \"a\"\n        \"b\"\n        \"c\"\n    ]\n}\n```\n\n**This library is only for the FTB snbt tag**. If you are finding the snbt library for the \"vanilla\" snbt tag, use [nbtlib](https://github.com/vberlier/nbtlib) by [vberlier](https://github.com/vberlier).\n\n## Installation\n\nThe package can be installed with ``pip``.\n\n```bash\n$ pip install ftb-snbt-lib\n```\n\n## Getting Started\n\n* Import the library.\n\n```python\n>>> import ftb_snbt_lib as slib\n```\n\n* ``load(fp)``: Load the ftb snbt tag from a file (``fp``).<br>\nThe type of returned value is ``Compound``, a dictionary-like object.<br>\nThe ``Compound`` is containing values with **[tag data types](#data-types)** provided by this library.\n\n```python\n>>> some_snbt = slib.load(open(\"tests/some_file.snbt\", \"r\", encoding=\"utf-8\"))\n>>> type(some_snbt)\n<class 'ftb_snbt_lib.tag.Compound'>\n>>> print(some_snbt)\nCompound({'some_tag': String('some_value'), 'another_tag': Byte(1)})\n```\n\n* ``dump(tag, fp, comma_sep=False)``: Dump the ftb snbt tag to a file (``fp``).<br>\nIf you set ``comma_sep`` parameter to ``True``, the output snbt has comma separator ``,\\n`` instead of non-comma separator ``\\n``.\n\n```python\n>>> slib.dump(some_snbt, open(\"tests/some_file_copy.snbt\", \"w\", encoding=\"utf-8\"))\n# File Output:\n# {\n#    some_tag: \"some_value\"\n#    another_tag: 1b\n# }\n```\n\n```python\n>>> slib.dump(some_snbt, open(\"tests/some_file_copy.snbt\", \"w\", encoding=\"utf-8\"), comma_sep=True)\n# File Output:\n# {\n#    some_tag: \"some_value\",\n#    another_tag: 1b\n# }\n```\n\n* ``loads(s)``: Load the ftb snbt tag from a string ``s``.<br>\nThe type of returned value is ``Compound``.\n\n```python\n>>> another_snbt = slib.loads('''\n... {\n...     some_tag: \"some_value\"\n...     another_tag: 1b\n... }\n... ''')\n>>> type(another_snbt)\n<class 'ftb_snbt_lib.tag.Compound'>\n>>> print(another_snbt)\nCompound({'some_tag': String('some_value'), 'another_tag': Byte(1)})\n```\n\n* ``dumps(tag, comma_sep=False)``: Dump the ftb snbt tag to a string.<br>\nIf you set ``comma_sep`` parameter to ``True``, the output snbt has comma separator ``,\\n`` instead of non-comma separator ``\\n``.\n\n```python\n>>> dumped_snbt = slib.dumps(another_snbt)\n>>> print(dumped_snbt)\n{\n    some_tag: \"some_value\"\n    another_tag: 1b\n}\n```\n\n```python\n>>> dumped_snbt = slib.dumps(another_snbt, comma_sep=True)\n>>> print(dumped_snbt)\n{\n    some_tag: \"some_value\",\n    another_tag: 1b\n}\n```\n\n* Edit the snbt tag. As its type is ``Compound``, it can be edited like a dictionary.<br>\nThe inserted or replace values should be any of **[tag data types](#data-types)** provided by this library.\n\n```python\n>>> another_snbt[\"some_tag\"] = slib.String(\"another_value\")\n```\n\n* When editing the ``List``, a list-like object, its elements must have **the same type**.<br>\nFor instance, ``List[Byte(1), Byte(2), Byte(3)]`` must contain **only** the ``Byte`` type object, so the other types like ``Integer`` or ``String`` **cannot be added or replaced** in it.\n\n* When editing the ``Array``, a list-like object with **a fixed length**, its elements must have **the same type**, and that type must **match the element type defined in the array**.<br>\nFor instance, ``IntArray`` with a length of 3 must contain **three** ``Integer`` type objects, so **adding new objects, removing existing objects, and replacing with other type objects are not allowed**.\n\n* Save the edited snbt tag to a json file.\n\n```python\n>>> import json\n>>> json.dump(another_snbt, open(\"tests/test.json\", \"w\", encoding=\"utf-8\"), indent=4, ensure_ascii=False)\n```\n\n## Data Types\n\n| Type | Description | Format | Example |\n| - | - | - | - |\n| Byte | A signed 8-bit integer.<br>Range: ``-128`` ~ ``127`` | ``<number>b`` | ``12b``, ``-35b`` |\n| Short | A signed 16-bit integer.<br>Range: ``-32,768`` ~ ``32,767`` | ``<number>s`` | ``132s``, ``-243s`` |\n| Integer | A signed 32-bit integer.<br>Range: ``-2,147,483,647`` ~ ``2,147,483,647`` | ``<number>`` | ``12345`` |\n| Long | A signed 64-bit integer.<br>Range: ``9,223,372,036,854,775,808`` ~ ``9,223,372,036,854,775,807`` | ``<number>L`` | ``12345L`` |\n| Float | A 32-bit, single-precision floating-point number.<br>Range: ``-3.4E+38`` ~ ``+3.4E+38`` | ``<number>f`` | ``12.345f``, ``1.0E-6f`` |\n| Double | A 64-bit, double-precision floating-point number.<br>Range: ``-1.7E+308`` ~ ``+1.7E+308`` | ``<number>d`` | ``12.345d``, ``1.0E-6d`` |\n| Bool | A boolean data type.<br>``0`` for ``false``, ``1`` for ``true``. | ``false``, ``true`` | ``true`` |\n| String | A sequence of characters. | A string enclosed in **double quotes ``\"\"``**.<br>Nested double quotes can be included within a string using a **escaping character ``\\\"``**. | `\"Hello, World!\"`,<br>`\"Say \\\"Hello, World!\\\"\"` |\n| List | An ordered list of tags.<br>The tags must be of **the same type**, determined by the first tag in the list. | Unnamed tags enclosed in square brackets and delimited by **newline** characters (``\\n``). | <pre>[<br>    3.2d<br>    1.4d<br>    ...<br>]</pre> |\n| Array | An ordered list of 8-bit(``ByteArray``), 32-bit(``IntArray``), 64-bit(``LongArray``) integers. | ``<array_prefix>;`` followed by an ordered list of tags enclosed in square brackets and delimited by **newline** characters (``\\n``).<br>Valid array prefixes are ``B``(Byte), ``I``(Integer), and ``L``(Long). | <pre>[B;<br>    12b<br>    -35b<br>    ...<br>]</pre> |\n| Compound | An ordered list of attribute-value pairs.<br>Each tag can be of **any type**. | Named tags enclosed in curly braces and delimited by commas or **newline** characters (``\\n``).<br>The key (tag name) can be unquoted if it contains only ``0-9``, ``A-Z``, ``a-z``, ``_``, ``-``, ``.``, and ``+``. Otherwise the key should be quoted, using the format of ``String`` type. | <pre>[<br>    tag1: \"string\"<br>    tag2: 12b<br>    \\\"quoted:tag\\\": 3.5d<br>    ...<br>]</pre> |\n\n## References\n\n* [PLY - Python Lex-Yacc](https://github.com/dabeaz/ply) by [David Beazley](https://www.dabeaz.com)\n* [nbtlib](https://github.com/vberlier/nbtlib) by [vberlier](https://github.com/vberlier)\n* [NBT format - Minecraft Wiki (fandom)](https://minecraft.fandom.com/wiki/NBT_format)\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A python library to parse, edit, and save FTB snbt tag, which is a variant of the \"vanilla\" snbt tag.",
    "version": "0.4.0",
    "project_urls": {
        "documentation": "https://github.com/peunsu/ftb-snbt-lib",
        "homepage": "https://github.com/peunsu/ftb-snbt-lib",
        "repository": "https://github.com/peunsu/ftb-snbt-lib"
    },
    "split_keywords": [
        "minecraft",
        " ftb",
        " snbt",
        " parser",
        " library"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3057ceede01fec499b9c5ef354bfe9a1b89cf0750a0efe477af1aaeec17093ab",
                "md5": "2a32e69798638e255019114223ef09a0",
                "sha256": "3886ac2d5c2f7a4b041756c79d74523cd65cd0e017483cef89925c8a348d81f1"
            },
            "downloads": -1,
            "filename": "ftb_snbt_lib-0.4.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2a32e69798638e255019114223ef09a0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 58876,
            "upload_time": "2024-06-10T07:54:56",
            "upload_time_iso_8601": "2024-06-10T07:54:56.350268Z",
            "url": "https://files.pythonhosted.org/packages/30/57/ceede01fec499b9c5ef354bfe9a1b89cf0750a0efe477af1aaeec17093ab/ftb_snbt_lib-0.4.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "34c6cb2b3da94d2e789821de3b9797ea131c5d5e213873a510d4765597c0bf5d",
                "md5": "921d0eedfb0728f18c0bd69c6eb8b238",
                "sha256": "3e30a09295df7dc52461b7e5f4e484a1999ee56ef0a31cc9aa425643b5bcb5f7"
            },
            "downloads": -1,
            "filename": "ftb_snbt_lib-0.4.0.tar.gz",
            "has_sig": false,
            "md5_digest": "921d0eedfb0728f18c0bd69c6eb8b238",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 58156,
            "upload_time": "2024-06-10T07:54:57",
            "upload_time_iso_8601": "2024-06-10T07:54:57.843173Z",
            "url": "https://files.pythonhosted.org/packages/34/c6/cb2b3da94d2e789821de3b9797ea131c5d5e213873a510d4765597c0bf5d/ftb_snbt_lib-0.4.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-10 07:54:57",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "peunsu",
    "github_project": "ftb-snbt-lib",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "ftb-snbt-lib"
}
        
Elapsed time: 4.69824s