44Dealing with qcodes dataset (the database) data in plottr.
55"""
66import os
7+ import sys
8+ from contextlib import closing
79from itertools import chain
810from operator import attrgetter
911from typing import Dict , List , Set , Union , TYPE_CHECKING , Any , Tuple , Optional , cast
1416
1517from qcodes .dataset .data_set import load_by_id
1618from qcodes .dataset .experiment_container import experiments
17- from qcodes .dataset .sqlite .database import initialise_or_create_database_at
19+ from qcodes .dataset .sqlite .database import conn_from_dbpath_or_conn , initialise_or_create_database_at
1820
1921from .datadict import DataDictBase , DataDict , combine_datadicts
2022from ..node .node import Node , updateOption
@@ -160,7 +162,7 @@ def get_ds_info(ds: 'DataSetProtocol', get_structure: bool = True) -> DataSetInf
160162 return data
161163
162164
163- def load_dataset_from (path : str , run_id : int ) -> 'DataSetProtocol' :
165+ def load_dataset_from (path : str , run_id : int , read_only : bool = True ) -> 'DataSetProtocol' :
164166 """
165167 Loads ``DataSet`` with the given ``run_id`` from a database file that
166168 is located in in the given ``path``.
@@ -169,6 +171,8 @@ def load_dataset_from(path: str, run_id: int) -> 'DataSetProtocol':
169171 qcodes config of the current python process is changed to ``path``.
170172 """
171173 initialise_or_create_database_at (path )
174+ if sys .version_info >= (3 , 11 ):
175+ return load_by_id (run_id = run_id , read_only = read_only )
172176 return load_by_id (run_id = run_id )
173177
174178
@@ -187,18 +191,24 @@ def get_runs_from_db(path: str, start: int = 0,
187191 in the return dict.
188192 """
189193 initialise_or_create_database_at (path )
194+ if sys .version_info >= (3 , 11 ):
195+ conn = conn_from_dbpath_or_conn (conn = None , path_to_db = path , read_only = True )
196+ else :
197+ conn = conn_from_dbpath_or_conn (conn = None , path_to_db = path )
198+ with closing (conn ) as conn_ :
199+ exps = experiments (conn = conn_ )
190200
191- datasets = sorted (
192- chain .from_iterable (exp .data_sets () for exp in experiments () ),
193- key = attrgetter ('run_id' )
194- )
201+ datasets = sorted (
202+ chain .from_iterable (exp .data_sets () for exp in exps ),
203+ key = attrgetter ('run_id' )
204+ )
195205
196- # There is no need for checking whether ``stop`` is ``None`` because if
197- # it is the following is simply equivalent to ``datasets[start:]``
198- datasets = datasets [start :stop ]
206+ # There is no need for checking whether ``stop`` is ``None`` because if
207+ # it is the following is simply equivalent to ``datasets[start:]``
208+ datasets = datasets [start :stop ]
199209
200- overview = {ds .run_id : get_ds_info (ds , get_structure = get_structure )
201- for ds in datasets }
210+ overview = {ds .run_id : get_ds_info (ds , get_structure = get_structure )
211+ for ds in datasets }
202212 return overview
203213
204214
@@ -286,7 +296,7 @@ def process(self, dataIn: Optional[DataDictBase] = None) -> Optional[Dict[str, A
286296 path , runId = cast (Tuple [str , int ], self ._pathAndId )
287297
288298 if self ._dataset is None :
289- self ._dataset = load_dataset_from (path , runId )
299+ self ._dataset = load_dataset_from (path , runId , read_only = True )
290300
291301 if self ._dataset .number_of_results > self .nLoadedRecords :
292302
0 commit comments