given a cache key tuple, counts how many instances of actual tuples are found. used to alert large jumps in cache key complexity.
(tup)
| 498 | |
| 499 | |
| 500 | def count_cache_key_tuples(tup): |
| 501 | """given a cache key tuple, counts how many instances of actual |
| 502 | tuples are found. |
| 503 | |
| 504 | used to alert large jumps in cache key complexity. |
| 505 | |
| 506 | """ |
| 507 | stack = [tup] |
| 508 | |
| 509 | sentinel = object() |
| 510 | num_elements = 0 |
| 511 | |
| 512 | while stack: |
| 513 | elem = stack.pop(0) |
| 514 | if elem is sentinel: |
| 515 | num_elements += 1 |
| 516 | elif isinstance(elem, tuple): |
| 517 | if elem: |
| 518 | stack = list(elem) + [sentinel] + stack |
| 519 | return num_elements |
| 520 | |
| 521 | |
| 522 | @contextlib.contextmanager |