Coverage for python/lsst/daf/butler/formatters/pickleFormatter.py : 84%

Hot-keys 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/>.
22"""Formatter associated with Python pickled objects."""
24__all__ = ("PickleFormatter", )
26import pickle
28from lsst.daf.butler.formatters.fileFormatter import FileFormatter
31class PickleFormatter(FileFormatter):
32 """Interface for reading and writing Python objects to and from pickle
33 files.
34 """
35 extension = ".pickle"
37 unsupportedParameters = None
38 """This formatter does not support any parameters"""
40 def _readFile(self, path, pytype=None):
41 """Read a file from the path in pickle format.
43 Parameters
44 ----------
45 path : `str`
46 Path to use to open the file.
47 pytype : `class`, optional
48 Not used by this implementation.
50 Returns
51 -------
52 data : `object`
53 Either data as Python object read from the pickle file, or None
54 if the file could not be opened.
55 """
56 try:
57 with open(path, "rb") as fd:
58 data = self._fromBytes(fd.read(), pytype)
59 except FileNotFoundError:
60 data = None
62 return data
64 def _writeFile(self, inMemoryDataset):
65 """Write the in memory dataset to file on disk.
67 Parameters
68 ----------
69 inMemoryDataset : `object`
70 Object to serialize.
72 Raises
73 ------
74 Exception
75 The file could not be written.
76 """
77 with open(self.fileDescriptor.location.path, "wb") as fd:
78 pickle.dump(inMemoryDataset, fd, protocol=-1)
80 def _fromBytes(self, serializedDataset, pytype=None):
81 """Read the bytes object as a python object.
83 Parameters
84 ----------
85 serializedDataset : `bytes`
86 Bytes object to unserialize.
87 pytype : `class`, optional
88 Not used by this implementation.
90 Returns
91 -------
92 inMemoryDataset : `object`
93 The requested data as a object, or None if the string could
94 not be read.
95 """
96 try:
97 data = pickle.loads(serializedDataset)
98 except pickle.PicklingError:
99 data = None
101 return data
103 def _toBytes(self, inMemoryDataset):
104 """Write the in memory dataset to a bytestring.
106 Parameters
107 ----------
108 inMemoryDataset : `object`
109 Object to serialize
111 Returns
112 -------
113 serializedDataset : `bytes`
114 Bytes object representing the pickled object.
116 Raises
117 ------
118 Exception
119 The object could not be pickled.
120 """
121 return pickle.dumps(inMemoryDataset, protocol=-1)