Coverage for python/lsst/daf/butler/formatters/pickle.py: 66%
28 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-04-07 00:58 -0700
« prev ^ index » next coverage.py v6.5.0, created at 2023-04-07 00:58 -0700
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22from __future__ import annotations
24"""Formatter associated with Python pickled objects."""
26__all__ = ("PickleFormatter",)
28import pickle
29from typing import Any, Optional, Type
31from .file import FileFormatter
34class PickleFormatter(FileFormatter):
35 """Interface for reading and writing Python objects to and from pickle
36 files.
37 """
39 extension = ".pickle"
41 unsupportedParameters = None
42 """This formatter does not support any parameters"""
44 def _readFile(self, path: str, pytype: Optional[Type[Any]] = None) -> Any:
45 """Read a file from the path in pickle format.
47 Parameters
48 ----------
49 path : `str`
50 Path to use to open the file.
51 pytype : `class`, optional
52 Not used by this implementation.
54 Returns
55 -------
56 data : `object`
57 Either data as Python object read from the pickle file, or None
58 if the file could not be opened.
59 """
60 try:
61 with open(path, "rb") as fd:
62 data = self._fromBytes(fd.read(), pytype)
63 except FileNotFoundError:
64 data = None
66 return data
68 def _writeFile(self, inMemoryDataset: Any) -> None:
69 """Write the in memory dataset to file on disk.
71 Parameters
72 ----------
73 inMemoryDataset : `object`
74 Object to serialize.
76 Raises
77 ------
78 Exception
79 The file could not be written.
80 """
81 with open(self.fileDescriptor.location.path, "wb") as fd:
82 pickle.dump(inMemoryDataset, fd, protocol=-1)
84 def _fromBytes(self, serializedDataset: bytes, pytype: Optional[Type[Any]] = None) -> Any:
85 """Read the bytes object as a python object.
87 Parameters
88 ----------
89 serializedDataset : `bytes`
90 Bytes object to unserialize.
91 pytype : `class`, optional
92 Not used by this implementation.
94 Returns
95 -------
96 inMemoryDataset : `object`
97 The requested data as a object, or None if the string could
98 not be read.
99 """
100 try:
101 data = pickle.loads(serializedDataset)
102 except pickle.PicklingError:
103 data = None
105 return data
107 def _toBytes(self, inMemoryDataset: Any) -> bytes:
108 """Write the in memory dataset to a bytestring.
110 Parameters
111 ----------
112 inMemoryDataset : `object`
113 Object to serialize
115 Returns
116 -------
117 serializedDataset : `bytes`
118 Bytes object representing the pickled object.
120 Raises
121 ------
122 Exception
123 The object could not be pickled.
124 """
125 return pickle.dumps(inMemoryDataset, protocol=-1)