Coverage for python/lsst/daf/butler/formatters/pickle.py: 65%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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
30from typing import (
31 Any,
32 Optional,
33 Type,
34)
36from .file import FileFormatter
39class PickleFormatter(FileFormatter):
40 """Interface for reading and writing Python objects to and from pickle
41 files.
42 """
43 extension = ".pickle"
45 unsupportedParameters = None
46 """This formatter does not support any parameters"""
48 def _readFile(self, path: str, pytype: Optional[Type[Any]] = None) -> Any:
49 """Read a file from the path in pickle format.
51 Parameters
52 ----------
53 path : `str`
54 Path to use to open the file.
55 pytype : `class`, optional
56 Not used by this implementation.
58 Returns
59 -------
60 data : `object`
61 Either data as Python object read from the pickle file, or None
62 if the file could not be opened.
63 """
64 try:
65 with open(path, "rb") as fd:
66 data = self._fromBytes(fd.read(), pytype)
67 except FileNotFoundError:
68 data = None
70 return data
72 def _writeFile(self, inMemoryDataset: Any) -> None:
73 """Write the in memory dataset to file on disk.
75 Parameters
76 ----------
77 inMemoryDataset : `object`
78 Object to serialize.
80 Raises
81 ------
82 Exception
83 The file could not be written.
84 """
85 with open(self.fileDescriptor.location.path, "wb") as fd:
86 pickle.dump(inMemoryDataset, fd, protocol=-1)
88 def _fromBytes(self, serializedDataset: bytes, pytype: Optional[Type[Any]] = None) -> Any:
89 """Read the bytes object as a python object.
91 Parameters
92 ----------
93 serializedDataset : `bytes`
94 Bytes object to unserialize.
95 pytype : `class`, optional
96 Not used by this implementation.
98 Returns
99 -------
100 inMemoryDataset : `object`
101 The requested data as a object, or None if the string could
102 not be read.
103 """
104 try:
105 data = pickle.loads(serializedDataset)
106 except pickle.PicklingError:
107 data = None
109 return data
111 def _toBytes(self, inMemoryDataset: Any) -> bytes:
112 """Write the in memory dataset to a bytestring.
114 Parameters
115 ----------
116 inMemoryDataset : `object`
117 Object to serialize
119 Returns
120 -------
121 serializedDataset : `bytes`
122 Bytes object representing the pickled object.
124 Raises
125 ------
126 Exception
127 The object could not be pickled.
128 """
129 return pickle.dumps(inMemoryDataset, protocol=-1)