1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
"""
This module provides a traceset implementation based on Python's pickle format. This implementation of the
traceset is thus very generic.
"""
import pickle
from io import BufferedIOBase, RawIOBase
from pathlib import Path
from typing import Union, BinaryIO
from public import public
from .base import TraceSet
@public
class PickleTraceSet(TraceSet):
"""Pickle-based traceset format."""
@classmethod
def read(cls, input: Union[str, Path, bytes, BinaryIO]) -> "PickleTraceSet":
if isinstance(input, bytes):
return pickle.loads(input)
elif isinstance(input, (str, Path)):
with open(input, "rb") as f:
return pickle.load(f)
elif isinstance(input, (RawIOBase, BufferedIOBase, BinaryIO)):
return pickle.load(input)
raise TypeError
@classmethod
def inplace(cls, input: Union[str, Path, bytes, BinaryIO]) -> "PickleTraceSet":
raise NotImplementedError
def write(self, output: Union[str, Path, BinaryIO]):
if isinstance(output, (str, Path)):
with open(output, "wb") as f:
pickle.dump(self, f)
elif isinstance(output, (RawIOBase, BufferedIOBase, BinaryIO)):
pickle.dump(self, output)
else:
raise TypeError
|