/zish_python_antlr

A Python ANTLR implementation of the Zish data serialization format

Primary LanguagePythonMIT LicenseMIT

Zish Python ANTLR

A Python library for the Zish format format, released under the MIT-0 licence.

Build Status

  • Create a virtual environment: python3 -m venv venv
  • Activate the virtual environment: source venv/bin/activate
  • Install: pip install zish_antlr

To go from a Python object to an Zish string use zish.dumps. To go from a Zish string to a Python object use zish.loads. Eg.

>>> from zish import loads, dumps
>>> from datetime import datetime, timezone
>>> from decimal import Decimal
>>>
>>> # Take a Python object
>>> book = {
...     'title': 'A Hero of Our Time',
...     'read_date': datetime(2017, 7, 16, 14, 5, tzinfo=timezone.utc),
...     'would_recommend': True,
...     'description': None,
...     'number_of_novellas': 5,
...     'price': Decimal('7.99'),
...     'weight': 6.88,
...     'key': b'kshhgrl',
...     'tags': [
...       'russian',
...       'novel',
...       '19th century',
...     ],
... }
>>>
>>> # Output it as an Zish string
>>> zish_str = dumps(book)
>>> print(zish_str)
{
  "description": null,
  "key": 'a3NoaGdybA==',
  "number_of_novellas": 5,
  "price": 7.99,
  "read_date": 2017-07-16T14:05:00Z,
  "tags": [
    "russian",
    "novel",
    "19th century",
  ],
  "title": "A Hero of Our Time",
  "weight": 6.88,
  "would_recommend": true,
}
>>>
>>> # Load the Zish string, to give us back the Python object
>>> reloaded_book = loads(zish_str)
>>>
>>> # Print the title
>>> print(reloaded_book['title'])
A Hero of Our Time
Python To Zish Type Mapping
Python Type Zish Type
bool bool
int integer
str string
datetime.datetime timestamp
dict map
decimal.Decimal decimal
float decimal
bytearray bytes
bytes bytes
list list
tuple list

Useful link:

To run the tests:

  • Change to the zish_python_antlr directory: cd zish_python_antlr
  • Create a virtual environment: python3 -m venv venv
  • Activate the virtual environment: source venv/bin/activate
  • Install tox: pip install tox
  • Run tox: tox

The core parser is created using ANTLR from the Zish grammar. To create the parser files, go to the zish/antlr directory and download the ANTLR jar and then run the following command:

java -jar antlr-4.11.1-complete.jar -Dlanguage=Python3 Zish.g4

  • Run tox to make sure all tests pass
  • Update the Release Notes section.
  • Ensure build and twine are installed: pip install wheel twine

Then do:

git tag -a x.y.z -m "version x.y.z"
rm -r dist
python -m build
twine upload --sign dist/*
  • Trailing commas in list and maps are now allowed.
  • Rename to zish_antlr to distinguish it from zish.
  • Upload to PyPI failed for previous release.
  • Allow lists and sets as keys to maps.
  • Fix bug where int was being parsed as Decimal.
  • Make bytes type return a bytes rather than a bytearray.
  • Container types aren't allowed as map keys.
  • Performance improvements.
  • Fix bug with UTC timestamp formatting.
  • Fix bug in timestamp formatting.
  • Add note about comments.
  • Fix bug where dumps fails for a tuple.
  • Simplify integer types.
  • Fixed bug where interpreter couldn't find the zish.antlr package in eggs.
  • Removed a few superfluous escape sequences.
  • Now uses RFC3339 for timestamps.
  • Fix bug where an EOF could cause an infinite loop.
  • First public release. Passes all the tests.