bluesky / suitcase-tiff

http://nsls-ii.github.io/suitcase
Other
2 stars 5 forks source link

Another structure to consider before we settle on one #4

Closed danielballan closed 5 years ago

danielballan commented 5 years ago

A thought I had on the way home that I just want to scribble down for discussion tomorrow:

The availability of event_model.DocumentRouter, which landed after we started work on suitcase-csv, invites another promising possibility for how to structure suitcases. We could move the business logic into a DocumentRouter subclass, trading the wall of if name == ... blocks for corresponding methods on DocumentRouter. We would still include a top-level function that consumes a generator (export / whatever we name it). Only the internal implementation would change: that function would become a thin wrapper around the callable class. Both would be public.

def export(gen, directory, **kwargs):
    serializer = Serializer(directory, **kwargs)
    for item in gen:
       serializer(*item)
    return serializer.artifacts

class Serializer:
    def __init__(self, directory, **kwargs):

        if isinstance(directory, (str, Path)):
            # Write to ordinary files by default.
            self._writer = suitcase.utils.MultiFileWriter(directory)
        else:
            self._writer = directory
        self.artifacts = self._writer.artifacts  # Expose as public.

    ...

# The two layers also make room for a compromise in the naming
# (high-level vs low-level) but that's not the main point.

Let's consider the what this would make easy and what it would make hard.

Pro:

Con:

Is this worth trying to see how it compares?

awalter-bnl commented 5 years ago

I like this approach mainly as it reduces the amount of 'duplicate' code we are seeing at the moment in the suitcase.* files. As you say it also gives both naming conventions, the higher level one and the lower level one which I also find appealing.

awalter-bnl commented 5 years ago

This has been resolved with PR#1