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

# 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/>. 

 

__all__ = ("DatabaseDict",) 

 

from collections.abc import MutableMapping 

 

from lsst.utils import doImport 

 

 

class DatabaseDict(MutableMapping): 

"""An abstract base class for dict-like objects with a specific key type 

and namedtuple values, backed by a database. 

 

DatabaseDict subclasses must implement the abstract ``__getitem__``, 

``__setitem__``, ``__delitem__`, ``__iter__``, and ``__len__`` abstract 

methods defined by `~collections.abc.MutableMapping`. 

 

They must also provide a constructor that takes the same arguments as that 

of `DatabaseDict` itself, *unless* they are constructed solely by 

`Registry.makeDatabaseDict` (in which case any constructor arguments are 

permitted). 

 

Parameters 

---------- 

config : `Config` 

Configuration used to identify and construct a subclass. 

types : `dict` 

A dictionary mapping `str` field names to type objects, containing 

all fields to be held in the database. 

key : `str` 

The name of the field to be used as the dictionary key. Must not be 

present in ``value._fields``. 

value : `type` 

The type used for the dictionary's values, typically a `namedtuple`. 

Must have a ``_fields`` class attribute that is a tuple of field names 

(i.e. as defined by `namedtuple`); these field names must also appear 

in the ``types`` arg, and a `_make` attribute to construct it from a 

sequence of values (again, as defined by `namedtuple`). 

""" 

 

@staticmethod 

def fromConfig(config, types, key, value, registry=None): 

"""Create a `DatabaseDict` subclass instance from `config`. 

 

If ``config`` contains a class ``cls`` key, this will be assumed to 

be the fully-qualified name of a DatabaseDict subclass to construct. 

If not, ``registry.makeDatabaseDict`` will be called instead, and 

``config`` must contain a ``table`` key with the name of the table 

to use. 

 

Parameters 

---------- 

config : `Config` 

Configuration used to identify and construct a subclass. 

types : `dict` 

A dictionary mapping `str` field names to type objects, containing 

all fields to be held in the database. 

key : `str` 

The name of the field to be used as the dictionary key. Must not 

be present in ``value._fields``. 

value : `type` 

The type used for the dictionary's values, typically a 

`namedtuple`. Must have a ``_fields`` class attribute that is a 

tuple of field names (i.e. as defined by `namedtuple`); these 

field names must also appear in the ``types`` arg, and a `_make` 

attribute to construct it from a sequence of values (again, as 

defined by `namedtuple`). 

registry : `Registry` 

A registry instance from which a `DatabaseDict` subclass can be 

obtained. Ignored if ``config["cls"]`` exists; may be None if 

it does. 

 

Returns 

------- 

dictionary : `DatabaseDict` (subclass) 

A new `DatabaseDict` subclass instance. 

""" 

if "cls" in config: 

cls = doImport(config["cls"]) 

return cls(config=config, types=types, key=key, value=value) 

else: 

table = config["table"] 

if registry is None: 

raise ValueError("Either config['cls'] or registry must be provided.") 

return registry.makeDatabaseDict(table, types=types, key=key, value=value) 

 

def __init__(self, config, types, key, value): 

# This constructor is currently defined just to clearly document the 

# interface subclasses should conform to. 

pass