15.12.2022 Views

Python Eficaz

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

bucket.quota += amount

Toda vez que o consumidor de quota quiser fazer alguma coisa, o algoritmo deve

primeiro garantir que pode debitar do espaço restante na quota a quantidade

necessária de armazenamento.

def deduct(bucket, amount):

now = datetime.now()

if now - bucket.reset_time > bucket.period_delta:

return False

if bucket.quota - amount < 0:

return False

bucket.quota -= amount

return True

Para usar esta classe, primeiro enchemos o balde até a boca.

bucket = Bucket(60)

fill(bucket, 100)

print(bucket)

>>>

Bucket(quota=100)

Depois, debitamos a quota necessária.

if deduct(bucket, 99):

print('Had 99 quota')

else:

print('Not enough for 99 quota')

print(bucket)

>>>

Had 99 quota

Bucket(quota=1)

Até que chega o momento em que não conseguimos mais fazer isso porque

estamos tentando debitar mais quota do que o disponível. Neste caso, o nível de

quota no balde permanece inalterado.

if deduct(bucket, 3):

www.full-ebook.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!