3 # Copyright The SCons Foundation
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 # Enable memoization counting
29 SCons
.Memoize
.EnableMemoization()
32 def __init__(self
) -> None:
35 def _dict_key(self
, argument
):
38 @SCons.Memoize
.CountDictCall(_dict_key
)
39 def dict(self
, argument
):
43 memo_dict
= self
._memo
['dict']
46 self
._memo
['dict'] = memo_dict
49 return memo_dict
[memo_key
]
53 result
= self
.compute_dict(argument
)
55 memo_dict
[memo_key
] = result
59 @SCons.Memoize
.CountMethodCall
63 return self
._memo
['value']
67 result
= self
.compute_value()
69 self
._memo
['value'] = result
73 def get_memoizer_counter(self
, name
):
74 return SCons
.Memoize
.CounterList
.get(self
.__class
__.__name
__+'.'+name
, None)
77 def __init__(self
, result
) -> None:
80 def __call__(self
, *args
, **kw
):
81 self
.calls
= self
.calls
+ 1
85 class CountDictTestCase(unittest
.TestCase
):
87 def test___call__(self
) -> None:
88 """Calling a Memoized dict method
97 obj
.compute_dict
= fd1
102 obj
.compute_dict
= fd2
110 obj
.compute_dict
= fd1
118 assert fd1
.calls
== 1, fd1
.calls
119 assert fd2
.calls
== 1, fd2
.calls
121 c
= obj
.get_memoizer_counter('dict')
123 assert c
.hit
== 3, c
.hit
124 assert c
.miss
== 2, c
.miss
127 class CountValueTestCase(unittest
.TestCase
):
129 def test___call__(self
) -> None:
130 """Calling a Memoized value method
139 obj
.compute_value
= fv1
146 obj
.compute_value
= fv2
153 assert fv1
.calls
== 1, fv1
.calls
154 assert fv2
.calls
== 0, fv2
.calls
156 c
= obj
.get_memoizer_counter('value')
158 assert c
.hit
== 3, c
.hit
159 assert c
.miss
== 1, c
.miss
162 if __name__
== "__main__":
167 # indent-tabs-mode:nil
169 # vim: set expandtab tabstop=4 shiftwidth=4: