Urgent.News

One page, thousands of outlets. See who else covered it.

Editions

Tech

Parsing numbers from JSON in Python

Consider this hypothetical endpoint in a Python-based web-server: # withdraw.py import json from flask import Flask , request app = Flask ( __name__ ) STATE = { " balance " : 1000 } @app.post ( " /withdraw " ) def withdraw (): """ Withdraw a requested amount from the user ' s balance """ try : data = json . loads ( request . data ) except ValueError : return " Malformed JSON " , 400 if not…

In a Python-based web server, the withdraw.py script is used to withdraw a requested amount from a user's balance. The script uses the json module to parse incoming JSON data and the Flask framework for handling HTTP requests. The STATE dictionary is employed to maintain the user's balance for simplification purposes.

The script starts by importing necessary modules and initializing the Flask app and the STATE dictionary with an initial balance of 1000. A POST request is defined for the /withdraw endpoint, which attempts to withdraw the requested amount from the user's balance. The script checks for three conditions: validity of the JSON input, amount type, and whether the amount is within acceptable limits.

Testing the JSON input validation reveals that the script accepts various numeric values and even the non-standard JSON constant NaN. However, passing NaN leads to unexpected consequences - the balance is replaced with float('nan'), and subsequent withdrawals continue without issue. This issue persists across different databases like SQLite and PostgreSQL, causing further complications as SQLite converts NaN to NULL, while PostgreSQL can store NaN as a numeric value, corrupting the balance differently.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Your CI is not flaky. It fails every 7 days.

Quick take Before you label a failing test flaky, write down the dates it failed. Flaky has no rhythm. Yours might. "Flaky" is the most expensive word in CI. It closes the investigation.

  • CI pipeline may fail every seven days, not flaky
  • Examine failure gaps to identify timer-triggered issue
  • GitHub Actions cache evicts entries after a week

More from Monday 17 August →