{"id":32917107,"url":"https://github.com/konnov/itf-py","last_synced_at":"2025-11-10T21:30:56.316Z","repository":{"id":306400966,"uuid":"1025665508","full_name":"konnov/itf-py","owner":"konnov","description":"Python library to parse and emit Apalache/Quint traces as JSON ITF","archived":false,"fork":false,"pushed_at":"2025-11-07T16:30:18.000Z","size":67,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-11-07T17:12:29.004Z","etag":null,"topics":["apalache","json","python","quint","tlaplus","trace"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/konnov.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-24T15:50:15.000Z","updated_at":"2025-11-07T16:30:22.000Z","dependencies_parsed_at":"2025-07-25T14:12:10.102Z","dependency_job_id":"d7a8c486-d873-4718-bc1d-f204355adcb6","html_url":"https://github.com/konnov/itf-py","commit_stats":null,"previous_names":["konnov/itf-py"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/konnov/itf-py","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/konnov%2Fitf-py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/konnov%2Fitf-py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/konnov%2Fitf-py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/konnov%2Fitf-py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/konnov","download_url":"https://codeload.github.com/konnov/itf-py/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/konnov%2Fitf-py/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":283758755,"owners_count":26889582,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-11-10T02:00:06.292Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apalache","json","python","quint","tlaplus","trace"],"created_at":"2025-11-10T21:30:55.674Z","updated_at":"2025-11-10T21:30:56.310Z","avatar_url":"https://github.com/konnov.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ITF-py: Parser and Encoder for the ITF Trace Format\n\n[![CI](https://github.com/konnov/itf-py/actions/workflows/ci.yml/badge.svg)](https://github.com/konnov/itf-py/actions/workflows/ci.yml)\n[![codecov](https://codecov.io/gh/konnov/itf-py/branch/main/graph/badge.svg)](https://codecov.io/gh/konnov/itf-py)\n[![Python 3.12+](https://img.shields.io/badge/python-3.12+-blue.svg)](https://www.python.org/downloads/)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\nPython library to parse and emit Apalache ITF traces. Refer to [ADR015][] for\nthe format. ITF traces are emitted by [Apalache][] and [Quint][].\n\n**Intentionally minimalistic.** We keep this library intentionally minimalistic.\nYou can use it in your projects without worrying about pulling dozens of\ndependencies. The package depends on `frozendict`.\n\n**Why?** It's much more convenient to manipulate with trace data in an\ninteractive prompt, similar to SQL.\n\n**Alternatives.** If you need to (de-)serialize ITF traces in Rust, check\n[itf-rs][].\n\n## Installation\n\nSimply use: `pip install itf-py`.\n\n## Usage\n\n### Deserializing and serializing traces\n\nAssume that you have the following JSON trace stored in the variable\n`trace_json`, e.g., as produced by [Apalache][] or [Quint][]:\n\n\u003c!-- name: test_trace --\u003e\n```python\ntrace_json = {\n    \"#meta\": {\"id\": 23},\n    \"params\": [\"N\"],\n    \"vars\": [\"pc\", \"x\"],\n    \"loop\": 0,\n    \"states\": [\n        {\n            \"#meta\": {\"no\": 0},\n            \"N\": {\"#bigint\": \"3\"},\n            \"pc\": \"idle\",\n            \"x\": {\"#bigint\": \"42\"},\n        },\n        {\n            \"#meta\": {\"no\": 1},\n            \"pc\": \"lock\",\n            \"x\": {\"#bigint\": \"43\"},\n        },\n    ],\n}\n```\n\nWe simply import the required function and parse the input:\n\n\u003c!-- name: test_trace --\u003e\n```python\nfrom itf_py import State, Trace, trace_from_json\noutput = trace_from_json(trace_json)\ntrace = Trace(\n    meta={\"id\": 23},\n    params=[\"N\"],\n    vars=[\"pc\", \"x\"],\n    loop=0,\n    states=[\n        State(meta={\"no\": 0}, values={\"N\": 3, \"pc\": \"idle\", \"x\": 42}),\n        State(meta={\"no\": 1}, values={\"pc\": \"lock\", \"x\": 43}),\n    ],\n)\nassert output == trace, f\"{output} != {trace}\"\n```\n\nWe serialize `trace` back to its JSON form:\n\n\u003c!-- name: test_trace --\u003e\n```python\nfrom itf_py import trace_to_json\noutput = trace_to_json(trace)\nassert output == trace_json, f\"{output} != {trace_json}\"\n```\n\n### De-(serializing) states\n\nSometimes, you do not want to deal with whole traces, but only with a single\nstate. In this case, you can (de-)serialize states via\n`state_from_json` and `state_to_json`:\n\n\u003c!-- name: test_state --\u003e\n```python\nfrom itf_py import State, state_from_json, state_to_json\nstate_json = {\n    \"#meta\": {\"no\": 1},\n    \"pc\": \"lock\",\n    \"x\": {\"#bigint\": \"43\"},\n}\noutput = state_from_json(state_json)\nstate = State(meta={\"no\": 1}, values={\"pc\": \"lock\", \"x\": 43})\nassert output == state, f\"{output} != {state}\"\n\noutput = state_to_json(state)\nassert output == state_json, f\"{output} != {state_json}\"\n```\n\n### (De-)serializing values\n\nFinally, you can work at the level of individual values. The following examples\ndemonstrate how values of different types are (de-)serialized:\n\n\u003c!-- name: test_values --\u003e\n```python\nfrom itf_py import value_from_json, value_to_json\n\n# primitive values are easy, except integers are wrapped\nassert value_to_json(\"hello\") == \"hello\"\nassert value_from_json(\"hello\") == \"hello\"\nassert value_from_json(True) == True\nassert value_to_json(True) == True\nassert value_to_json(3) == {\"#bigint\": \"3\"}\nassert value_from_json({\"#bigint\": \"3\"}) == 3\n\n# lists are serialized as JSON arrays\nassert value_to_json([\"a\", \"b\", \"c\"]) == [\"a\", \"b\", \"c\"]\n# ...and deserialized as immutable lists\nassert value_from_json([\"a\", \"b\", \"c\"]) == [\"a\", \"b\", \"c\"]\n\n# tuples are wrapped JSON arrays\nassert value_to_json((\"a\", \"b\", \"c\")) == {\"#tup\": [\"a\", \"b\", \"c\"]}\nassert value_from_json({\"#tup\": [\"a\", \"b\", \"c\"]}) == (\"a\", \"b\", \"c\")\n\n# Sets are serialized as wrapped JSON arrays.\n# Be careful, the order in the JSON array may differ!\nj = value_to_json(frozenset([\"a\", \"b\", \"c\"]))\nassert \"#set\" in j and len(j[\"#set\"]) == 3\nassert \"a\" in j[\"#set\"] and \"b\" in j[\"#set\"] and \"c\" in j[\"#set\"]\n# ...and deserialized as frozen sets\nassert value_from_json({\"#set\": [\"a\", \"b\", \"c\"]}) == frozenset([\"a\", \"b\", \"c\"])\n\n# data classes are serialized as JSON objects\nfrom dataclasses import dataclass\n@dataclass(frozen=True)\nclass User:\n    name: str\n    age: int\n    active: bool\n\noutput = value_to_json(User(name=\"Alice\", age=30, active=True))\nassert output[\"name\"] == \"Alice\"\nassert output[\"age\"] == {\"#bigint\": \"30\"}\nassert output[\"active\"] == True\n\n# named tuples are serialized as JSON objects too\nfrom collections import namedtuple\nUser = namedtuple(\"User\", [\"name\", \"age\", \"active\"])\nuser = User(name=\"Bob\", age=33, active=False)\noutput = value_to_json(user)\nassert output[\"name\"] == \"Bob\"\nassert output[\"age\"] == {\"#bigint\": \"33\"}\nassert output[\"active\"] == False\n\n# ...and deserialized as immutable named tuples\noutput = value_from_json(output)\nassert output.name == user.name\nassert output.age == user.age\nassert output.active == user.active\n\n# Dictionaries are serialized as wrapped JSON arrays of key-value pairs.\n# Be careful, the order in the JSON array may differ!\nj = value_to_json({\"key1\": \"val1\", \"key2\": \"val2\"})\nassert \"#map\" in j and len(j[\"#map\"]) == 2\nassert [\"key1\", \"val1\"] in j[\"#map\"]\nassert [\"key2\", \"val2\"] in j[\"#map\"]\n# ...and deserialized back as frozen dictionaries\nfrom frozendict import frozendict\noutput = value_from_json({\"#map\": [[\"key1\", \"val1\"], [\"key2\", \"val2\"]]})\nassert output == frozendict({\"key1\": \"val1\", \"key2\": \"val2\"})\n\n# Apalache tagged unions are deserialized as special named tuples.\n# They have a special attribute '_itf_variant' to distinguish them from\n# regular named tuples.\noutput = value_from_json({\"tag\": \"Banana\", \"value\": {\"length\": 5, \"color\": \"yellow\"}})\nassert output.__class__.__name__ == \"Banana\"\nassert hasattr(output.__class__, '_itf_variant') is True\nassert output.length == 5\nassert output.color == \"yellow\"\n\n# ...but take care when the value is not a record!\n# In this case, it simply has the single field 'value'.\noutput = value_from_json({\"tag\": \"Init\", \"value\": \"u_OF_UNIT\"})\nassert output.__class__.__name__ == \"Init\"\nassert hasattr(output.__class__, '_itf_variant') is True\nassert output.value == \"u_OF_UNIT\"\n\n# To construct such variants, use itf_variant decorator.\nfrom itf_py import itf_variant\n\n@itf_variant\n@dataclass(frozen=True)\nclass Apple:\n    length: int\n    color: str\n\noutput = value_to_json(Apple(length=10, color=\"green\"))\nassert output[\"tag\"] == \"Apple\"\n#assert output[\"value\"] == {\"length\": 10, \"color\": \"green\"}\n\n# finally, unserializable values have special representation\nfrom itf_py.itf import ITFUnserializable\noutput = value_from_json({\"#unserializable\": \"custom-repr\"})\nassert output == ITFUnserializable(\"custom-repr\")\n```\n\n### Pretty-printing\n\nThe deserialized values support nice pretty-printing:\n\n\u003c!-- name: test_values --\u003e\n```python\nfrom pprint import pp, pformat\n\npp(value_from_json({\"#set\": [\"a\", \"b\", \"c\"]}))\n# prints frozenset({'a', 'b', 'c'})\n# ...or frozenset in another order\n\ns = pformat(value_from_json({\"#map\": [[\"a\", \"b\"], [\"c\", \"d\"]]}))\nassert s == \"{'a': 'b', 'c': 'd'}\"\n\ns = pformat(value_from_json([\"a\", \"b\", \"c\", \"d\"]))\nassert s == \"['a', 'b', 'c', 'd']\"\n\ns = pformat(value_from_json({\"name\": \"Alice\", \"age\": 30, \"active\": True}))\nassert s == \"Rec(name='Alice', age=30, active=True)\", f\"unexpected: {s}\"\n```\n\nTagged unions have beatified output:\n\n\u003c!-- name: test_values --\u003e\n```python\nj = {\"tag\": \"Banana\", \"value\": {\"length\": 5, \"color\": \"yellow\"}}\ns = pformat(value_from_json(j))\nassert s == \"Banana(length=5, color='yellow')\", f\"unexpected: {s}\"\n```\n\n### Colorized pretty-printing\n\nThis module offers no special support for colorized output. However,\nit works out-of-the-box with [rich][]. If you are already using \n[IPython][], it's really easy:\n\n```python\nfrom rich import pretty\nfrom itf_py import value_from_json\n\npretty.install()\nvalue_from_json({\"name\": \"Alice\", \"age\": 30, \"active\": True})\n# Rec(name='Alice', age=30, active=True)\n#   in nice colors!\n```\n\n\n[ADR015]: https://apalache-mc.org/docs/adr/015adr-trace.html\n[Apalache]: https://github.com/apalache-mc/apalache\n[Quint]: https://github.com/informalsystems/quint\n[itf-rs]: https://github.com/informalsystems/itf-rs\n[rich]: https://pypi.org/project/rich/\n[IPython]: https://ipython.org/","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkonnov%2Fitf-py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkonnov%2Fitf-py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkonnov%2Fitf-py/lists"}