Coverage for python/lsst/daf/butler/formatters/pickle.py: 84%
28 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-25 10:48 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-25 10:48 +0000
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 software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28"""Formatter associated with Python pickled objects."""
30from __future__ import annotations
32__all__ = ("PickleFormatter",)
34import pickle
35from typing import Any
37from .file import FileFormatter
40class PickleFormatter(FileFormatter):
41 """Interface for reading and writing Python objects to and from pickle
42 files.
43 """
45 extension = ".pickle"
47 unsupportedParameters = None
48 """This formatter does not support any parameters"""
50 def _readFile(self, path: str, pytype: type[Any] | None = None) -> Any:
51 """Read a file from the path in pickle format.
53 Parameters
54 ----------
55 path : `str`
56 Path to use to open the file.
57 pytype : `class`, optional
58 Not used by this implementation.
60 Returns
61 -------
62 data : `object`
63 Either data as Python object read from the pickle file, or None
64 if the file could not be opened.
65 """
66 try:
67 with open(path, "rb") as fd:
68 data = self._fromBytes(fd.read(), pytype)
69 except FileNotFoundError:
70 data = None
72 return data
74 def _writeFile(self, inMemoryDataset: Any) -> None:
75 """Write the in memory dataset to file on disk.
77 Parameters
78 ----------
79 inMemoryDataset : `object`
80 Object to serialize.
82 Raises
83 ------
84 Exception
85 The file could not be written.
86 """
87 with open(self.fileDescriptor.location.path, "wb") as fd:
88 pickle.dump(inMemoryDataset, fd, protocol=-1)
90 def _fromBytes(self, serializedDataset: bytes, pytype: type[Any] | None = None) -> Any:
91 """Read the bytes object as a python object.
93 Parameters
94 ----------
95 serializedDataset : `bytes`
96 Bytes object to unserialize.
97 pytype : `class`, optional
98 Not used by this implementation.
100 Returns
101 -------
102 inMemoryDataset : `object`
103 The requested data as a object, or None if the string could
104 not be read.
105 """
106 try:
107 data = pickle.loads(serializedDataset)
108 except pickle.PicklingError:
109 data = None
111 return data
113 def _toBytes(self, inMemoryDataset: Any) -> bytes:
114 """Write the in memory dataset to a bytestring.
116 Parameters
117 ----------
118 inMemoryDataset : `object`
119 Object to serialize
121 Returns
122 -------
123 serializedDataset : `bytes`
124 Bytes object representing the pickled object.
126 Raises
127 ------
128 Exception
129 The object could not be pickled.
130 """
131 return pickle.dumps(inMemoryDataset, protocol=-1)