Coverage for python/lsst/daf/butler/_deferredDatasetHandle.py: 52%

30 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-07 02:46 -0700

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

15# This program is free software: you can redistribute it and/or modify 

16# it under the terms of the GNU General Public License as published by 

17# the Free Software Foundation, either version 3 of the License, or 

18# (at your option) any later version. 

19# 

20# This program is distributed in the hope that it will be useful, 

21# but WITHOUT ANY WARRANTY; without even the implied warranty of 

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <http://www.gnu.org/licenses/>. 

27 

28"""Module containing classes used with deferring dataset loading. 

29""" 

30from __future__ import annotations 

31 

32__all__ = ("DeferredDatasetHandle",) 

33 

34import dataclasses 

35from typing import TYPE_CHECKING, Any 

36 

37if TYPE_CHECKING: 

38 from ._dataset_ref import DatasetRef 

39 from ._limited_butler import LimitedButler 

40 from ._storage_class import StorageClass 

41 from .dimensions import DataCoordinate 

42 

43 

44@dataclasses.dataclass(frozen=True) 

45class DeferredDatasetHandle: 

46 """Proxy class that provides deferred loading of datasets from a butler.""" 

47 

48 def get( 

49 self, 

50 *, 

51 component: str | None = None, 

52 parameters: dict | None = None, 

53 storageClass: str | StorageClass | None = None, 

54 ) -> Any: 

55 """Retrieve the dataset pointed to by this handle. 

56 

57 This handle may be used multiple times, possibly with different 

58 parameters. 

59 

60 Parameters 

61 ---------- 

62 component : `str` or None 

63 If the deferred object is a component dataset type, this parameter 

64 may specify the name of the component to use in the get operation. 

65 parameters : `dict` or None 

66 The parameters argument will be passed to the butler get method. 

67 It defaults to None. If the value is not None, this dict will 

68 be merged with the parameters dict used to construct the 

69 `DeferredDatasetHandle` class. 

70 storageClass : `StorageClass` or `str`, optional 

71 The storage class to be used to override the Python type 

72 returned by this method. By default the returned type matches 

73 the dataset type definition for this dataset or the storage 

74 class specified when this object was created. Specifying a 

75 read `StorageClass` can force a different type to be returned. 

76 This type must be compatible with the original type. 

77 

78 Returns 

79 ------- 

80 return : `Object` 

81 The dataset pointed to by this handle. 

82 """ 

83 if self.parameters is not None: 

84 mergedParameters = self.parameters.copy() 

85 if parameters is not None: 

86 mergedParameters.update(parameters) 

87 elif parameters is not None: 

88 mergedParameters = parameters 

89 else: 

90 mergedParameters = {} 

91 if storageClass is None: 

92 storageClass = self.storageClass 

93 

94 ref = self.ref.makeComponentRef(component) if component is not None else self.ref 

95 return self.butler.get(ref, parameters=mergedParameters, storageClass=storageClass) 

96 

97 @property 

98 def dataId(self) -> DataCoordinate: 

99 """The full data ID associated with the dataset 

100 (`DataCoordinate`). 

101 

102 Guaranteed to contain records. 

103 """ 

104 return self.ref.dataId 

105 

106 butler: LimitedButler 

107 """The butler that will be used to fetch the dataset (`LimitedButler`). 

108 """ 

109 

110 ref: DatasetRef 

111 """Reference to the dataset (`DatasetRef`). 

112 """ 

113 

114 parameters: dict | None 

115 """Optional parameters that may be used to specify a subset of the dataset 

116 to be loaded (`dict` or `None`). 

117 """ 

118 

119 storageClass: str | StorageClass | None = None 

120 """Optional storage class override that can be applied on ``get()``."""