Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-14544: Add pickle support to Butler #43

Merged
merged 2 commits into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions python/lsst/daf/butler/butler.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ def __init__(self, config=None, collection=None, run=None):
if self.run is None:
self.run = self.registry.makeRun(runCollection)

def __reduce__(self):
"""Support pickling.
"""
return (Butler, (self.config, ))

def put(self, obj, datasetType, dataId, producer=None):
"""Store and register a dataset.

Expand Down
12 changes: 9 additions & 3 deletions python/lsst/daf/butler/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ def __call__(cls): # noqa N805
return cls._instances[cls]


# Helper Node in a TopologicalSet
# Unfortunately can't be a class member because that breaks pickling
TopologicalSetNode = namedtuple('TopologicalSetNode', ['element', 'sourceElements'])


class TopologicalSet:
"""A collection that behaves like a builtin `set`, but where
elements can be interconnected (like a graph).
Expand All @@ -230,10 +235,8 @@ class TopologicalSet:
elements : `iterable`
Any iterable with elements to insert.
"""
Node = namedtuple('Node', ['element', 'sourceElements'])

def __init__(self, elements):
self._nodes = {e: TopologicalSet.Node(e, set()) for e in elements}
self._nodes = {e: TopologicalSetNode(e, set()) for e in elements}
self._ordering = None

def __contains__(self, element):
Expand All @@ -242,6 +245,9 @@ def __contains__(self, element):
def __len__(self):
return len(self._nodes)

def __eq__(self, other):
return self._nodes == other._nodes

def connect(self, sourceElement, targetElement):
"""Connect two elements in the set.

Expand Down
9 changes: 9 additions & 0 deletions tests/test_butler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import os
import unittest
from tempfile import TemporaryDirectory
import pickle

import lsst.utils.tests

Expand Down Expand Up @@ -159,6 +160,14 @@ def testMakeRepo(self):
self.assertIn("datastore.formatters", full)
self.assertNotIn("datastore.formatters", limited)

def testPickle(self):
"""Test pickle support.
"""
butler = Butler(self.configFile)
butlerOut = pickle.loads(pickle.dumps(butler))
self.assertIsInstance(butlerOut, Butler)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to test that they share some configuration?

self.assertEqual(butlerOut.config, butler.config)


class MemoryTester(lsst.utils.tests.MemoryTestCase):
pass
Expand Down
12 changes: 12 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import unittest
import inspect
import pickle
from itertools import permutations
from random import shuffle

Expand Down Expand Up @@ -181,6 +182,17 @@ def testTopologicalOrdering(self):
topologicalSet2.connect(i, i+1)
self.assertEqual(list(topologicalSet2), elements)

def testPickle(self):
"""Should be possible to pickle.
"""
elements = ['a', 'd', 'f']
topologicalSet = TopologicalSet(elements)
# Adding connections should work
topologicalSet.connect('a', 'd')
topologicalSet.connect('a', 'f')
out = pickle.loads(pickle.dumps(topologicalSet))
self.assertEqual(out, topologicalSet)


class TestButlerUtils(lsst.utils.tests.TestCase):
"""Tests of the simple utilities."""
Expand Down