Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

# This file is part of daf_butler. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

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

# for details of code ownership. 

# 

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

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

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

# (at your option) any later version. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

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

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

 

from __future__ import annotations 

 

__all__ = ("DimensionRecord",) 

 

from typing import Dict, Any, Type, Mapping, TYPE_CHECKING 

 

from ..timespan import Timespan 

from .coordinate import DataCoordinate 

 

31 ↛ 32line 31 didn't jump to line 32, because the condition on line 31 was never trueif TYPE_CHECKING: # Imports needed only for type annotations; may be circular. 

from .elements import DimensionElement 

 

 

def _reconstructDimensionRecord(definition: DimensionElement, *args): 

"""Unpickle implementation for `DimensionRecord` subclasses. 

 

For internal use by `DimensionRecord`. 

""" 

return definition.RecordClass(*args) 

 

 

def _makeTimespanFromRecord(record: DimensionRecord): 

"""Extract a `Timespan` object from the appropriate endpoint attributes. 

 

For internal use by `DimensionRecord`. 

""" 

from ..timespan import TIMESPAN_FIELD_SPECS 

return Timespan( 

begin=getattr(record, TIMESPAN_FIELD_SPECS.begin.name), 

end=getattr(record, TIMESPAN_FIELD_SPECS.end.name), 

) 

 

 

def _subclassDimensionRecord(definition: DimensionElement) -> Type[DimensionRecord]: 

"""Create a dynamic subclass of `DimensionRecord` for the given 

`DimensionElement`. 

 

For internal use by `DimensionRecord`. 

""" 

from .schema import makeElementTableSpec 

d = { 

"definition": definition, 

"__slots__": tuple(makeElementTableSpec(definition).fields.names) 

} 

if definition.temporal: 

d["timespan"] = property(_makeTimespanFromRecord) 

return type(definition.name + ".RecordClass", (DimensionRecord,), d) 

 

 

class DimensionRecord: 

"""Base class for the Python representation of database records for 

a `DimensionElement`. 

 

Parameters 

---------- 

args 

Field values for this record, ordered to match ``__slots__``. 

 

Notes 

----- 

`DimensionRecord` subclasses are created dynamically for each 

`DimensionElement` in a `DimensionUniverse`, and are accessible via the 

`DimensionElement.RecordClass` attribute. The `DimensionRecord` base class 

itself is pure abstract, but does not use the `abc` module to indicate this 

because it does not have overridable methods. 

 

Record classes have attributes that correspond exactly to the fields in the 

related database table, a few additional methods inherited from the 

`DimensionRecord` base class, and two additional injected attributes: 

 

- ``definition`` is a class attribute that holds the `DimensionElement`; 

 

- ``timespan`` is a property that returns a `Timespan`, present only 

on record classes that correspond to temporal elements. 

 

The field attributes are defined via the ``__slots__`` mechanism, and the 

``__slots__`` tuple itself is considered the public interface for obtaining 

the list of fields. 

 

Instances are usually obtained from a `Registry`, but in the rare cases 

where they are constructed directly in Python (usually for insertion into 

a `Registry`), the `fromDict` method should generally be used. 

 

`DimensionRecord` instances are immutable. 

""" 

 

# Derived classes are required to define __slots__ as well, and it's those 

# derived-class slots that other methods on the base class expect to see 

# when they access self.__slots__. 

__slots__ = ("dataId",) 

 

def __init__(self, *args): 

for attrName, value in zip(self.__slots__, args): 

object.__setattr__(self, attrName, value) 

dataId = DataCoordinate( 

self.definition.graph, 

args[:len(self.definition.graph.required.names)] 

) 

object.__setattr__(self, "dataId", dataId) 

 

@classmethod 

def fromDict(cls, mapping: Mapping[str, Any]): 

"""Construct a `DimensionRecord` subclass instance from a mapping 

of field values. 

 

Parameters 

---------- 

mapping : `~collections.abc.Mapping` 

Field values, keyed by name. The keys must match those in 

``__slots__``, with the exception that a dimension name 

may be used in place of the primary key name - for example, 

"tract" may be used instead of "id" for the "id" primary key 

field of the "tract" dimension. 

 

Returns 

------- 

record : `DimensionRecord` 

An instance of this subclass of `DimensionRecord`. 

""" 

# If the name of the dimension is present in the given dict, use it 

# as the primary key value instead of expecting the field name. 

# For example, allow {"instrument": "HSC", ...} instead of 

# {"name": "HSC", ...} when building a record for instrument dimension. 

primaryKey = mapping.get(cls.definition.name) 

if primaryKey is not None: 

d = dict(mapping) 

d[cls.definition.primaryKey.name] = primaryKey 

else: 

d = mapping 

values = tuple(d.get(k) for k in cls.__slots__) 

return cls(*values) 

 

def __reduce__(self): 

args = tuple(getattr(self, name) for name in self.__slots__) 

return (_reconstructDimensionRecord, (self.definition,) + args) 

 

def toDict(self) -> Dict[str, Any]: 

"""Return a vanilla `dict` representation of this record. 

""" 

return {name: getattr(self, name) for name in self.__slots__} 

 

# Class attributes below are shadowed by instance attributes, and are 

# present just to hold the docstrings for those instance attributes. 

 

dataId: DataCoordinate 

"""A dict-like identifier for this record's primary keys 

(`DataCoordinate`). 

"""