Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/saving/legacy/saved_model/order_preserving_set.py: 65%

40 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

1# Copyright 2021 The TensorFlow Authors. All Rights Reserved. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14# ============================================================================== 

15"""A set based on dict so that it preserves key insertion order. 

16 

17Python Dicts are order-preserving since 3.6 

18(https://mail.python.org/pipermail/python-dev/2017-December/151283.html), 

19but sets are not. This class implements a set on top of a dict so that we get 

20deterministic iteration order across runs. 

21""" 

22 

23import collections.abc 

24 

25 

26class OrderPreservingSet(collections.abc.MutableSet): 

27 """A set based on dict so that it preserves key insertion order.""" 

28 

29 def __init__(self, iterable=None): 

30 self._dict = {item: None for item in (iterable or [])} 

31 

32 # abstract from collections.MutableSet 

33 def __len__(self): 

34 return len(self._dict) 

35 

36 # abstract from collections.MutableSet 

37 def __contains__(self, value): 

38 return value in self._dict 

39 

40 # override from collections.MutableSet 

41 def __iter__(self): 

42 return iter(self._dict) 

43 

44 # abstract from collections.MutableSet 

45 def add(self, item): 

46 self._dict[item] = None 

47 

48 # abstract from collections.MutableSet 

49 def discard(self, value): 

50 del self._dict[value] 

51 

52 # override from collections.MutableSet 

53 def clear(self): 

54 self._dict = {} 

55 

56 # override from collections.Set 

57 def __eq__(self, other): 

58 if not isinstance(other, OrderPreservingSet): 

59 return NotImplemented 

60 return self._dict.keys() == other._dict.keys() 

61 

62 # override from collections.Set 

63 def __le__(self, other): 

64 if not isinstance(other, OrderPreservingSet): 

65 return NotImplemented 

66 return self._dict.keys() <= other._dict.keys() 

67 

68 # override from collections.Set 

69 def __ge__(self, other): 

70 if not isinstance(other, OrderPreservingSet): 

71 return NotImplemented 

72 return self._dict.keys() >= other._dict.keys() 

73 

74 # override from collections.Set 

75 def __and__(self, other): 

76 # collections.Set defaults to the ordering in other, we want to use self 

77 return self._from_iterable(value for value in self if value in other) 

78 

79 # override from collections.Set 

80 def __or__(self, other): 

81 # ensure that other is ordered before performing __or__ 

82 if not isinstance(other, OrderPreservingSet): 

83 raise TypeError( 

84 "cannot union an 'OrderPreservingSet' with an " 

85 "unordered iterable." 

86 ) 

87 result = self._from_iterable(value for value in self) 

88 for value in other: 

89 result._dict[value] = None 

90 return result 

91 

92 def union(self, other): 

93 return self | other 

94