
5 Python Mistakes That Cost Me Real Money (Don't Repeat Them)
Mistake #1: Not handling rate limits The cost: $340 in API overage charges I was scraping an API that charged $0.001 per request. My script had a bug: the retry logic created an infinite loop. I went to lunch. Came back to 340,000 requests and a surprise bill. # What I wrote (WRONG) def fetch_data ( url ): while True : resp = requests . get ( url ) if resp . status_code == 200 : return resp . json () # No break condition. No max retries. No sleep. # What I should have written from tenacity import retry , stop_after_attempt , wait_exponential @retry ( stop = stop_after_attempt ( 3 ), wait = wait_exponential ( multiplier = 1 , max = 10 )) def fetch_data ( url ): resp = requests . get ( url ) resp . raise_for_status () return resp . json () Lesson: Every network call needs a maximum retry count and exponential backoff. Always. Mistake #2: Using float for money The cost: $127 in rounding errors over 6 months >>> 0.1 + 0.2 0.30000000000000004 >>> price = 19.99 >>> quantity = 3 >>> total = p
Continue reading on Dev.to Python
Opens in a new tab




