Coverage for python/lsst/daf/butler/nonempty_mapping.py: 46%

32 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-03 02:48 -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 

28from __future__ import annotations 

29 

30__all__ = ("NonemptyMapping",) 

31 

32from collections.abc import Callable, Iterator, Mapping 

33from typing import Any, TypeVar, overload 

34 

35_K = TypeVar("_K") 

36_T = TypeVar("_T") 

37_V = TypeVar("_V", covariant=True) 

38 

39 

40class NonemptyMapping(Mapping[_K, _V]): 

41 """A `Mapping` that implicitly adds values (like 

42 `~collections.defaultdict`) but treats any that evaluate to `False` as not 

43 present. 

44 

45 Parameters 

46 ---------- 

47 default_factory : `~collections.abc.Callable` 

48 A callable that takes no arguments and returns a new instance of the 

49 value type. 

50 

51 Notes 

52 ----- 

53 Unlike `~collections.defaultdict`, this class implements only 

54 `collections.abc.Mapping`, not `~collections.abc.MutableMapping`, and hence 

55 it can be modified only by invoking ``__getitem__`` with a key that does 

56 not exist. It is expected that the value type will be a mutable container 

57 like `set` or `dict`, and that an empty nested container should be 

58 considered equivalent to the absence of a key. 

59 """ 

60 

61 def __init__(self, default_factory: Callable[[], _V]) -> None: 

62 self._mapping: dict[_K, _V] = {} 

63 self._next: _V = default_factory() 

64 self._default_factory = default_factory 

65 

66 def __len__(self) -> int: 

67 return sum(bool(v) for v in self._mapping.values()) 

68 

69 def __iter__(self) -> Iterator[_K]: 

70 for key, values in self._mapping.items(): 

71 if values: 

72 yield key 

73 

74 def __getitem__(self, key: _K) -> _V: 

75 # We use setdefault with an existing empty inner container (_next), 

76 # since we expect that to usually return an existing object and we 

77 # don't want the overhead of making a new inner container each time. 

78 # When we do insert _next, we replace it. 

79 if (value := self._mapping.setdefault(key, self._next)) is self._next: 

80 self._next = self._default_factory() 

81 return value 

82 

83 # We don't let Mapping implement `__contains__` or `get` for us, because we 

84 # don't want the side-effect of adding an empty inner container from 

85 # calling `__getitem__`. 

86 

87 def __contains__(self, key: Any) -> bool: 

88 return bool(self._mapping.get(key)) 

89 

90 @overload 

91 def get(self, key: _K) -> _V | None: ... 91 ↛ exitline 91 didn't return from function 'get', because

92 

93 @overload 

94 def get(self, key: _K, default: _T) -> _V | _T: ... 94 ↛ exitline 94 didn't return from function 'get', because

95 

96 def get(self, key: _K, default: Any = None) -> Any: 

97 if value := self._mapping.get(key): 

98 return value 

99 return default