MCPcopy Create free account
hub / github.com/bslatkin/effectivepython / NewBucket

Class NewBucket

example_code/item_059.py:112–145  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

110
111print("Example 7")
112class 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
148print("Example 10")

Callers 1

item_059.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected