BCDA-APS / gemviz

Data visualization for tiled
https://bcda-aps.github.io/gemviz/
Other
4 stars 0 forks source link

refactor similar code to one method #126

Closed prjemian closed 1 year ago

prjemian commented 1 year ago
prjemian commented 1 year ago

@rodolakis: Good suggestion! Also good opportunity to try using functools.partial!

rodolakis commented 1 year ago

I found an alternative using lambda:

        self.actions_library = {
            "Scan ID": lambda run: utils.get_md(run, "start", "scan_id"),
            "Plan Name": lambda run: utils.get_md(run, "start", "plan_name"),
            "Positioners": lambda run: self.get_str_list(run, "start", "motors"),
            "Detectors": lambda run: self.get_str_list(run, "start", "detectors"),
            "#points": lambda run: utils.get_md(run, "start", "num_points"),
            "Date": self.get_run_start_time,
            "Status": lambda run: utils.get_md(run, "stop", "exit_status"),
            "Streams": lambda run: self.get_str_list(run, "summary", "stream_names"),
            # "uid": lambda run: utils.get_md(run, "start", "uid"),
            # "uid7": self.get_run_uid7,
        }

   # ...

    def get_str_list(self, run, doc, key):
        """Return the document's key values as a list."""
        items = utils.get_md(run, doc, key, [])
        return ", ".join(items)

That way there is no need to change the argument order in get_str_list (which makes it more consistent with get_md) and it remove the if statement in the data method:

def data(self, index, role=None):
    # display data
    if role == QtCore.Qt.DisplayRole:
        uid = self.uidList()[index.row()]
        run = self.catalog()[uid]

        label = self.columnLabels[index.column()]
        action = self.actions_library[label]

        return action(run)

what do you think?