Skip to content

Commit

Permalink
Merge pull request #375 from qipe-nlab/ddh5writer-convenience-methods
Browse files Browse the repository at this point in the history
Add convenience methods
  • Loading branch information
marcosfrenkel committed Feb 20, 2023
2 parents df5c369 + 9c3aaba commit 928face
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions plottr/data/datadict_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import time
import datetime
import uuid
import json
import shutil
from enum import Enum
from typing import Any, Union, Optional, Dict, Type, Collection
from types import TracebackType
Expand All @@ -20,6 +22,7 @@
import numpy as np
import h5py

from qcodes.utils import NumpyJSONEncoder
from plottr import QtGui, Signal, Slot, QtWidgets, QtCore

from ..node import (
Expand Down Expand Up @@ -687,3 +690,30 @@ def add_data(self, **kwargs: Any) -> None:
with FileOpener(self.filepath, 'a', timeout=self.file_timeout) as f:
add_cur_time_attr(f, name='last_change')
add_cur_time_attr(f[self.groupname], name='last_change')


# convenience methods for saving things in the same directory as the ddh5 file

def add_tag(self, tags: Union[str, Collection[str]]) -> None:
assert self.filepath is not None
if isinstance(tags, str):
tags = [tags]
for tag in tags:
open(self.filepath.parent / f"{tag}.tag", "x").close()

def backup_file(self, paths: Union[str, Collection[str]]) -> None:
assert self.filepath is not None
if isinstance(paths, str):
paths = [paths]
for path in paths:
shutil.copy(path, self.filepath.parent)

def save_text(self, name: str, text: str) -> None:
assert self.filepath is not None
with open(self.filepath.parent / name, "x") as f:
f.write(text)

def save_dict(self, name: str, d: dict) -> None:
assert self.filepath is not None
with open(self.filepath.parent / name, "x") as f:
json.dump(d, f, indent=4, ensure_ascii=False, cls=NumpyJSONEncoder)

0 comments on commit 928face

Please sign in to comment.