given a cache key tuple, counts how many instances of actual tuples are found. used to alert large jumps in cache key complexity.
(tup)
| 539 | |
| 540 | |
| 541 | def count_cache_key_tuples(tup): |
| 542 | """given a cache key tuple, counts how many instances of actual |
| 543 | tuples are found. |
| 544 | |
| 545 | used to alert large jumps in cache key complexity. |
| 546 | |
| 547 | """ |
| 548 | stack = [tup] |
| 549 | |
| 550 | sentinel = object() |
| 551 | num_elements = 0 |
| 552 | |
| 553 | while stack: |
| 554 | elem = stack.pop(0) |
| 555 | if elem is sentinel: |
| 556 | num_elements += 1 |
| 557 | elif isinstance(elem, tuple): |
| 558 | if elem: |
| 559 | stack = list(elem) + [sentinel] + stack |
| 560 | return num_elements |
| 561 | |
| 562 | |
| 563 | @contextlib.contextmanager |