Get the exact project count of a studio using a binary-search-like strategy
(self)
| 601 | ).json() |
| 602 | |
| 603 | def get_exact_project_count(self) -> int: |
| 604 | """ |
| 605 | Get the exact project count of a studio using a binary-search-like strategy |
| 606 | """ |
| 607 | if self.project_count is not None and self.project_count < 100: |
| 608 | return self.project_count |
| 609 | |
| 610 | # Get maximum possible project count before binary search |
| 611 | maximum = 100 |
| 612 | minimum = 0 |
| 613 | |
| 614 | while True: |
| 615 | if not self.projects(offset=maximum): |
| 616 | break |
| 617 | minimum = maximum |
| 618 | maximum *= 2 |
| 619 | |
| 620 | # Binary search |
| 621 | while True: |
| 622 | middle = (minimum + maximum) // 2 |
| 623 | projects = self.projects(limit=40, offset=middle) |
| 624 | |
| 625 | if not projects: |
| 626 | # too high - no projects found |
| 627 | maximum = middle |
| 628 | elif len(projects) < 40: |
| 629 | # we are 40 within true value, and can infer the rest |
| 630 | break |
| 631 | else: |
| 632 | # too low - full project list |
| 633 | minimum = middle |
| 634 | |
| 635 | return middle + len(projects) |
| 636 | |
| 637 | |
| 638 |