| 110 | |
| 111 | print("Example 7") |
| 112 | class NewBucket: |
| 113 | def __init__(self, period): |
| 114 | self.period_delta = timedelta(seconds=period) |
| 115 | self.reset_time = datetime.now() |
| 116 | self.max_quota = 0 |
| 117 | self.quota_consumed = 0 |
| 118 | |
| 119 | def __repr__(self): |
| 120 | return ( |
| 121 | f"NewBucket(max_quota={self.max_quota}, " |
| 122 | f"quota_consumed={self.quota_consumed})" |
| 123 | ) |
| 124 | |
| 125 | |
| 126 | print("Example 8") |
| 127 | @property |
| 128 | def quota(self): |
| 129 | return self.max_quota - self.quota_consumed |
| 130 | |
| 131 | |
| 132 | print("Example 9") |
| 133 | @quota.setter |
| 134 | def quota(self, amount): |
| 135 | delta = self.max_quota - amount |
| 136 | if amount == 0: |
| 137 | # Quota being reset for a new period |
| 138 | self.quota_consumed = 0 |
| 139 | self.max_quota = 0 |
| 140 | elif delta < 0: |
| 141 | # Quota being filled during the period |
| 142 | self.max_quota = amount + self.quota_consumed |
| 143 | else: |
| 144 | # Quota being consumed during the period |
| 145 | self.quota_consumed = delta |
| 146 | |
| 147 | |
| 148 | print("Example 10") |