itf-py 0.2.2__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of itf-py might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: itf-py
3
- Version: 0.2.2
3
+ Version: 0.4.0
4
4
  Summary: Python library to parse and emit Apalache/Quint traces in ITF JSON
5
5
  Keywords: itf,trace,parser,tlaplus,apalache,quint
6
6
  Author: Igor Konnov
@@ -10,17 +10,28 @@ Classifier: Programming Language :: Python :: 3
10
10
  Classifier: Programming Language :: Python :: 3.12
11
11
  Classifier: Programming Language :: Python :: 3.13
12
12
  Requires-Dist: frozendict (>=2.4.6,<3.0.0)
13
- Requires-Dist: frozenlist (>=1.7.0,<2.0.0)
14
13
  Project-URL: Homepage, https://github.com/konnov/itf-py
15
14
  Description-Content-Type: text/markdown
16
15
 
17
16
  # ITF-py: Parser and Encoder for the ITF Trace Format
18
17
 
19
- Python library to parse and emit Apalache ITF traces. Refer to
20
- [ADR015](https://apalache-mc.org/docs/adr/015adr-trace.html) for the format. ITF
21
- traces are emitted by [Apalache](https://github.com/apalache-mc/apalache) and
22
- [Quint](https://github.com/informalsystems/quint).
18
+ Python library to parse and emit Apalache ITF traces. Refer to [ADR015][] for
19
+ the format. ITF traces are emitted by [Apalache][] and [Quint][].
23
20
 
24
21
  **Intentionally minimalistic.** We keep this library intentionally minimalistic.
25
22
  You can use it in your projects without worrying about pulling dozens of
26
- dependencies.
23
+ dependencies. The package depends on `frozendict`.
24
+
25
+ **Why?** It's much more convenient to manipulate with trace data in an
26
+ interactive prompt, similar to SQL.
27
+
28
+ See usage examples on the [itf-py][] page on Github.
29
+
30
+ **Alternatives.** If you need to deserialize/serialize ITF traces in Rust, check
31
+ [itf-rs][].
32
+
33
+ [ADR015]: https://apalache-mc.org/docs/adr/015adr-trace.html
34
+ [Apalache]: https://github.com/apalache-mc/apalache
35
+ [Quint]: https://github.com/informalsystems/quint
36
+ [itf-rs]: https://github.com/informalsystems/itf-rs
37
+ [itf-py]: https://github.com/konnov/itf-py
itf_py-0.4.0/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # ITF-py: Parser and Encoder for the ITF Trace Format
2
+
3
+ Python library to parse and emit Apalache ITF traces. Refer to [ADR015][] for
4
+ the format. ITF traces are emitted by [Apalache][] and [Quint][].
5
+
6
+ **Intentionally minimalistic.** We keep this library intentionally minimalistic.
7
+ You can use it in your projects without worrying about pulling dozens of
8
+ dependencies. The package depends on `frozendict`.
9
+
10
+ **Why?** It's much more convenient to manipulate with trace data in an
11
+ interactive prompt, similar to SQL.
12
+
13
+ See usage examples on the [itf-py][] page on Github.
14
+
15
+ **Alternatives.** If you need to deserialize/serialize ITF traces in Rust, check
16
+ [itf-rs][].
17
+
18
+ [ADR015]: https://apalache-mc.org/docs/adr/015adr-trace.html
19
+ [Apalache]: https://github.com/apalache-mc/apalache
20
+ [Quint]: https://github.com/informalsystems/quint
21
+ [itf-rs]: https://github.com/informalsystems/itf-rs
22
+ [itf-py]: https://github.com/konnov/itf-py
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "itf-py"
3
- version = "0.2.2"
3
+ version = "0.4.0"
4
4
  description = "Python library to parse and emit Apalache/Quint traces in ITF JSON"
5
5
  homepage = "https://github.com/konnov/itf-py"
6
6
  authors = ["Igor Konnov <igor@konnov.phd>"]
@@ -11,7 +11,6 @@ packages = [{include = "itf_py", from = "src"}]
11
11
  [tool.poetry.dependencies]
12
12
  python = "^3.12"
13
13
  frozendict = "^2.4.6"
14
- frozenlist = "^1.7.0"
15
14
 
16
15
  [tool.poetry.group.dev.dependencies]
17
16
  pytest = "^8.0.0"
@@ -3,8 +3,8 @@ Python library to parse and emit Apalache ITF traces.
3
3
  """
4
4
 
5
5
  from .itf import (
6
- ITFState,
7
- ITFTrace,
6
+ State,
7
+ Trace,
8
8
  state_from_json,
9
9
  state_to_json,
10
10
  trace_from_json,
@@ -15,8 +15,8 @@ from .itf import (
15
15
 
16
16
  __version__ = "0.2.1"
17
17
  __all__ = [
18
- "ITFState",
19
- "ITFTrace",
18
+ "State",
19
+ "Trace",
20
20
  "value_to_json",
21
21
  "value_from_json",
22
22
  "state_to_json",
@@ -0,0 +1,188 @@
1
+ from collections import namedtuple
2
+ from dataclasses import dataclass
3
+ from typing import Any, Dict, Iterable, List, NoReturn, Optional, SupportsIndex
4
+
5
+ from frozendict import frozendict
6
+
7
+
8
+ @dataclass
9
+ class State:
10
+ """A single state in an ITF trace as a Python object."""
11
+
12
+ meta: Dict[str, Any]
13
+ values: Dict[str, Any]
14
+
15
+
16
+ @dataclass
17
+ class Trace:
18
+ """An ITF trace as a Python object."""
19
+
20
+ meta: Dict[str, Any]
21
+ params: List[str]
22
+ vars: List[str]
23
+ states: List[State]
24
+ loop: Optional[int]
25
+
26
+
27
+ class ImmutableList(list):
28
+ """An immutable wrapper around list that supports hashing,
29
+ yet displays as a list."""
30
+
31
+ # frozenlist.FrozenList is what we want, but it does not display
32
+ # nicely in pretty-printing.
33
+
34
+ def __init__(self, items: Iterable[Any]):
35
+ super().__init__(items)
36
+
37
+ def __hash__(self) -> int: # type: ignore
38
+ return hash(tuple(self))
39
+
40
+ def _forbid_modification(self) -> NoReturn:
41
+ """Forbid modification of the list."""
42
+ raise TypeError("This list is immutable and cannot be modified.")
43
+
44
+ def __setitem__(self, _key: Any, _value: Any) -> NoReturn:
45
+ self._forbid_modification()
46
+
47
+ def __delitem__(self, _key: Any) -> NoReturn:
48
+ self._forbid_modification()
49
+
50
+ def append(self, _value: Any) -> NoReturn:
51
+ self._forbid_modification()
52
+
53
+ def extend(self, _values: Iterable[Any]) -> NoReturn:
54
+ self._forbid_modification()
55
+
56
+ def insert(self, _index: SupportsIndex, _value: Any) -> None:
57
+ self._forbid_modification()
58
+
59
+ def pop(self, _index: SupportsIndex = -1) -> NoReturn:
60
+ self._forbid_modification()
61
+
62
+ def remove(self, _value: Any) -> NoReturn:
63
+ self._forbid_modification()
64
+
65
+ def clear(self) -> NoReturn:
66
+ self._forbid_modification()
67
+
68
+ def reverse(self) -> NoReturn:
69
+ self._forbid_modification()
70
+
71
+
72
+ class ImmutableDict(frozendict):
73
+ """A wrapper around frozendict that displays dictionaries as
74
+ `{k1: v_1, ..., k_n: v_n}`."""
75
+
76
+ def __new__(cls, items: Dict[str, Any]) -> Any:
77
+ return super().__new__(cls, items)
78
+
79
+
80
+ ImmutableDict.__str__ = ( # type: ignore
81
+ dict.__str__
82
+ ) # use the default dict representation in pretty-printing
83
+
84
+ ImmutableDict.__repr__ = ( # type: ignore
85
+ dict.__repr__
86
+ ) # use the default dict representation in pretty-printing
87
+
88
+
89
+ @dataclass
90
+ class ITFUnserializable:
91
+ """A placeholder for unserializable values."""
92
+
93
+ value: str
94
+
95
+
96
+ def value_from_json(val: Any) -> Any:
97
+ """Deserialize a Python value from JSON"""
98
+ if isinstance(val, dict):
99
+ if "#bigint" in val:
100
+ return int(val["#bigint"])
101
+ elif "#tup" in val:
102
+ return tuple(value_from_json(v) for v in val["#tup"])
103
+ elif "#set" in val:
104
+ return frozenset(value_from_json(v) for v in val["#set"])
105
+ elif "#map" in val:
106
+ d = {value_from_json(k): value_from_json(v) for (k, v) in val["#map"]}
107
+ return ImmutableDict(d)
108
+ elif "#unserializable" in val:
109
+ return ITFUnserializable(value=val["#unserializable"])
110
+ else:
111
+ ks = val.keys()
112
+ if len(ks) == 2 and "tag" in ks and "value" in ks:
113
+ # This is a tagged union, e.g., {"tag": "Banana", "value": {...}}.
114
+ # Produce Banana(...)
115
+ union_type = namedtuple(val["tag"], val["value"].keys()) # type: ignore
116
+ return union_type(
117
+ **{k: value_from_json(v) for k, v in val["value"].items()}
118
+ )
119
+ else:
120
+ # This is a general record, e.g., {"field1": ..., "field2": ...}.
121
+ rec_type = namedtuple("Rec", val.keys()) # type: ignore
122
+ return rec_type(**{k: value_from_json(v) for k, v in val.items()})
123
+ elif isinstance(val, list):
124
+ return ImmutableList([value_from_json(v) for v in val])
125
+ else:
126
+ return val # int, str, bool
127
+
128
+
129
+ def value_to_json(val: Any) -> Any:
130
+ """Serialize a Python value into JSON"""
131
+ if isinstance(val, bool):
132
+ return val
133
+ elif isinstance(val, int):
134
+ return {"#bigint": str(val)}
135
+ elif isinstance(val, tuple) and not hasattr(val, "_fields"):
136
+ return {"#tup": [value_to_json(v) for v in val]}
137
+ elif isinstance(val, frozenset):
138
+ return {"#set": [value_to_json(v) for v in val]}
139
+ elif isinstance(val, dict):
140
+ return {"#map": [[value_to_json(k), value_to_json(v)] for k, v in val.items()]}
141
+ elif isinstance(val, list):
142
+ return [value_to_json(v) for v in val]
143
+ elif hasattr(val, "__dict__"):
144
+ # An object-like structure, e.g., a record, or a union.
145
+ # Note that we cannot distinguish between a record and a tagged union here.
146
+ return {k: value_to_json(v) for k, v in val.__dict__.items()}
147
+ elif isinstance(val, tuple) and hasattr(val, "_fields"):
148
+ # namedtuple
149
+ return {k: value_to_json(v) for k, v in val._asdict().items()} # type: ignore
150
+ elif isinstance(val, str):
151
+ return val
152
+ else:
153
+ return ITFUnserializable(value=str(val))
154
+
155
+
156
+ def state_from_json(raw_state: Dict[str, Any]) -> State:
157
+ """Deserialize a single State from JSON"""
158
+ state_meta = raw_state["#meta"] if "#meta" in raw_state else {}
159
+ values = {k: value_from_json(v) for k, v in raw_state.items() if k != "#meta"}
160
+ return State(meta=state_meta, values=values)
161
+
162
+
163
+ def state_to_json(state: State) -> Dict[str, Any]:
164
+ """Serialize a single State to JSON"""
165
+ result = {"#meta": state.meta}
166
+ for k, v in state.values.items():
167
+ result[k] = value_to_json(v)
168
+ return result
169
+
170
+
171
+ def trace_from_json(data: Dict[str, Any]) -> Trace:
172
+ """Deserialize a Trace from JSON"""
173
+ meta = data["#meta"] if "#meta" in data else {}
174
+ params = data.get("params", [])
175
+ vars_ = data["vars"]
176
+ loop = data.get("loop", None)
177
+ states = [state_from_json(s) for s in data["states"]]
178
+ return Trace(meta=meta, params=params, vars=vars_, states=states, loop=loop)
179
+
180
+
181
+ def trace_to_json(trace: Trace) -> Dict[str, Any]:
182
+ """Serialize a Trace to JSON"""
183
+ result: Dict[str, Any] = {"#meta": trace.meta}
184
+ result["params"] = trace.params
185
+ result["vars"] = trace.vars
186
+ result["loop"] = trace.loop
187
+ result["states"] = [state_to_json(s) for s in trace.states]
188
+ return result
itf_py-0.2.2/README.md DELETED
@@ -1,10 +0,0 @@
1
- # ITF-py: Parser and Encoder for the ITF Trace Format
2
-
3
- Python library to parse and emit Apalache ITF traces. Refer to
4
- [ADR015](https://apalache-mc.org/docs/adr/015adr-trace.html) for the format. ITF
5
- traces are emitted by [Apalache](https://github.com/apalache-mc/apalache) and
6
- [Quint](https://github.com/informalsystems/quint).
7
-
8
- **Intentionally minimalistic.** We keep this library intentionally minimalistic.
9
- You can use it in your projects without worrying about pulling dozens of
10
- dependencies.
@@ -1,117 +0,0 @@
1
- from collections import namedtuple
2
- from dataclasses import dataclass
3
- from typing import Any, Dict, List, Optional
4
-
5
- from frozendict import frozendict
6
- from frozenlist import FrozenList
7
-
8
-
9
- @dataclass
10
- class ITFState:
11
- """A single state in an ITF trace as a Python object."""
12
-
13
- meta: Dict[str, Any]
14
- values: Dict[str, Any]
15
-
16
-
17
- @dataclass
18
- class ITFTrace:
19
- """An ITF trace as a Python object."""
20
-
21
- meta: Dict[str, Any]
22
- params: List[str]
23
- vars: List[str]
24
- states: List[ITFState]
25
- loop: Optional[int]
26
-
27
-
28
- @dataclass
29
- class ITFUnserializable:
30
- """A placeholder for unserializable values."""
31
-
32
- value: str
33
-
34
-
35
- def value_from_json(val: Any) -> Any:
36
- """Deserialize a Python value from JSON"""
37
- if isinstance(val, dict):
38
- if "#bigint" in val:
39
- return int(val["#bigint"])
40
- elif "#tup" in val:
41
- return tuple(value_from_json(v) for v in val["#tup"])
42
- elif "#set" in val:
43
- return frozenset(value_from_json(v) for v in val["#set"])
44
- elif "#map" in val:
45
- d = {value_from_json(k): value_from_json(v) for (k, v) in val["#map"]}
46
- return frozendict(d) # immutable dictionary
47
- elif "#unserializable" in val:
48
- return ITFUnserializable(value=val["#unserializable"])
49
- else:
50
- tup_type = namedtuple("ITFRecord", val.keys()) # type: ignore
51
- return tup_type(**{k: value_from_json(v) for k, v in val.items()})
52
- elif isinstance(val, list):
53
- lst = FrozenList([value_from_json(v) for v in val])
54
- lst.freeze() # make it immutable
55
- return lst
56
- else:
57
- return val # int, str, bool
58
-
59
-
60
- def value_to_json(val: Any) -> Any:
61
- """Serialize a Python value into JSON"""
62
- if isinstance(val, bool):
63
- return val
64
- elif isinstance(val, int):
65
- return {"#bigint": str(val)}
66
- elif isinstance(val, tuple) and not hasattr(val, "_fields"):
67
- return {"#tup": [value_to_json(v) for v in val]}
68
- elif isinstance(val, frozenset):
69
- return {"#set": [value_to_json(v) for v in val]}
70
- elif isinstance(val, dict):
71
- return {"#map": [[value_to_json(k), value_to_json(v)] for k, v in val.items()]}
72
- elif isinstance(val, list) or isinstance(val, FrozenList):
73
- return [value_to_json(v) for v in val]
74
- elif hasattr(val, "__dict__"):
75
- return {k: value_to_json(v) for k, v in val.__dict__.items()}
76
- elif isinstance(val, tuple) and hasattr(val, "_fields"):
77
- # namedtuple
78
- return {k: value_to_json(v) for k, v in val._asdict().items()} # type: ignore
79
- elif isinstance(val, str):
80
- return val
81
- else:
82
- return ITFUnserializable(value=str(val))
83
-
84
-
85
- def state_from_json(raw_state: Dict[str, Any]) -> ITFState:
86
- """Deserialize a single ITFState from JSON"""
87
- state_meta = raw_state["#meta"] if "#meta" in raw_state else {}
88
- values = {k: value_from_json(v) for k, v in raw_state.items() if k != "#meta"}
89
- return ITFState(meta=state_meta, values=values)
90
-
91
-
92
- def state_to_json(state: ITFState) -> Dict[str, Any]:
93
- """Serialize a single ITFState to JSON"""
94
- result = {"#meta": state.meta}
95
- for k, v in state.values.items():
96
- result[k] = value_to_json(v)
97
- return result
98
-
99
-
100
- def trace_from_json(data: Dict[str, Any]) -> ITFTrace:
101
- """Deserialize an ITFTrace from JSON"""
102
- meta = data["#meta"] if "#meta" in data else {}
103
- params = data.get("params", [])
104
- vars_ = data["vars"]
105
- loop = data.get("loop", None)
106
- states = [state_from_json(s) for s in data["states"]]
107
- return ITFTrace(meta=meta, params=params, vars=vars_, states=states, loop=loop)
108
-
109
-
110
- def trace_to_json(trace: ITFTrace) -> Dict[str, Any]:
111
- """Serialize an ITFTrace to JSON"""
112
- result: Dict[str, Any] = {"#meta": trace.meta}
113
- result["params"] = trace.params
114
- result["vars"] = trace.vars
115
- result["loop"] = trace.loop
116
- result["states"] = [state_to_json(s) for s in trace.states]
117
- return result