MCPcopy Create free account
hub / github.com/dateutil/dateutil / PicklableMixin

Class PicklableMixin

tests/_common.py:130–166  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

128
129
130class PicklableMixin:
131 def _get_nobj_bytes(self, obj, dump_kwargs, load_kwargs):
132 """
133 Pickle and unpickle an object using ``pickle.dumps`` / ``pickle.loads``
134 """
135 pkl = pickle.dumps(obj, **dump_kwargs)
136 return pickle.loads(pkl, **load_kwargs)
137
138 def _get_nobj_file(self, obj, dump_kwargs, load_kwargs):
139 """
140 Pickle and unpickle an object using ``pickle.dump`` / ``pickle.load`` on
141 a temporary file.
142 """
143 with tempfile.TemporaryFile('w+b') as pkl:
144 pickle.dump(obj, pkl, **dump_kwargs)
145 pkl.seek(0) # Reset the file to the beginning to read it
146 nobj = pickle.load(pkl, **load_kwargs)
147
148 return nobj
149
150 def assertPicklable(self, obj, singleton=False, asfile=False,
151 dump_kwargs=None, load_kwargs=None):
152 """
153 Assert that an object can be pickled and unpickled. This assertion
154 assumes that the desired behavior is that the unpickled object is the same
155 as the original, when singleton=True.
156 """
157 get_nobj = self._get_nobj_file if asfile else self._get_nobj_bytes
158 dump_kwargs = dump_kwargs or {}
159 load_kwargs = load_kwargs or {}
160
161 nobj = get_nobj(obj, dump_kwargs, load_kwargs)
162 if singleton:
163 self.assertIs(obj, nobj)
164 else:
165 self.assertIsNot(obj, nobj)
166 self.assertEqual(obj, nobj)
167
168
169class TZContextBase:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected