Compare commits
12 Commits
35464c207a
...
a4a436c020
| Author | SHA1 | Date | |
|---|---|---|---|
| a4a436c020 | |||
| 1278d5c332 | |||
| 697e926dfe | |||
| 2ee246fad6 | |||
| c6ff93c764 | |||
| e11ca4b376 | |||
| 5515714ae6 | |||
| 6f571b9e12 | |||
| bab9c2ec08 | |||
| 63965696f4 | |||
| 7fb55c03fc | |||
| bf0e3e133f |
4
Makefile
4
Makefile
@ -1,4 +1,8 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
verify:
|
||||
@PYTHONPATH=src python3 -m compileall -q src tests && PYTHONPATH=src python3 -m unittest discover -s tests -q && echo "verification passed"
|
||||
|
||||
run:
|
||||
@PYTHONPATH=src python3 -m typosaurus_sandbox
|
||||
|
||||
|
||||
67
app/__init__.py
Normal file
67
app/__init__.py
Normal file
@ -0,0 +1,67 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import os
|
||||
|
||||
from flask import Flask
|
||||
from flask import jsonify
|
||||
from flask import make_response
|
||||
from flask import request
|
||||
from flask.wrappers import Response
|
||||
|
||||
from src.calculator import add
|
||||
from src.calculator import clamp
|
||||
from src.calculator import clamp_to_byte
|
||||
from src.calculator import subtract
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index() -> Response:
|
||||
index_path = os.path.join(os.path.dirname(__file__), 'index.html')
|
||||
with open(index_path) as f:
|
||||
return make_response(f.read(), 200, {'Content-Type': 'text/html'})
|
||||
|
||||
|
||||
@app.route('/add', methods=['GET'])
|
||||
def add_route() -> Response:
|
||||
try:
|
||||
left = int(request.args['left'])
|
||||
right = int(request.args['right'])
|
||||
except (KeyError, TypeError, ValueError):
|
||||
return make_response(jsonify({'error': 'Invalid or missing parameters'}), 400)
|
||||
return make_response(jsonify({'result': add(left, right)}), 200)
|
||||
|
||||
|
||||
@app.route('/subtract', methods=['GET'])
|
||||
def subtract_route() -> Response:
|
||||
try:
|
||||
left = int(request.args['left'])
|
||||
right = int(request.args['right'])
|
||||
except (KeyError, TypeError, ValueError):
|
||||
return make_response(jsonify({'error': 'Invalid or missing parameters'}), 400)
|
||||
return make_response(jsonify({'result': subtract(left, right)}), 200)
|
||||
|
||||
|
||||
@app.route('/clamp', methods=['GET'])
|
||||
def clamp_route() -> Response:
|
||||
try:
|
||||
value = int(request.args['value'])
|
||||
low = int(request.args['low'])
|
||||
high = int(request.args['high'])
|
||||
except (KeyError, TypeError, ValueError):
|
||||
return make_response(jsonify({'error': 'Invalid or missing parameters'}), 400)
|
||||
try:
|
||||
result = clamp(value, low, high)
|
||||
except ValueError:
|
||||
return make_response(jsonify({'error': 'Invalid or missing parameters'}), 400)
|
||||
return make_response(jsonify({'result': result}), 200)
|
||||
|
||||
|
||||
@app.route('/clamp_to_byte', methods=['GET'])
|
||||
def clamp_to_byte_route() -> Response:
|
||||
try:
|
||||
value = int(request.args['value'])
|
||||
except (KeyError, TypeError, ValueError):
|
||||
return make_response(jsonify({'error': 'Invalid or missing parameters'}), 400)
|
||||
return make_response(jsonify({'result': clamp_to_byte(value)}), 200)
|
||||
69
app/index.html
Normal file
69
app/index.html
Normal file
@ -0,0 +1,69 @@
|
||||
<!-- retoor <retoor@molodetz.nl> -->
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Calculator</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Calculator</h1>
|
||||
|
||||
<fieldset>
|
||||
<legend>Add / Subtract</legend>
|
||||
<input type="number" id="left" placeholder="Left operand">
|
||||
<input type="number" id="right" placeholder="Right operand">
|
||||
<button onclick="calculate('add')">Add</button>
|
||||
<button onclick="calculate('subtract')">Subtract</button>
|
||||
</fieldset>
|
||||
|
||||
<fieldset>
|
||||
<legend>Clamp</legend>
|
||||
<input type="number" id="value" placeholder="Value">
|
||||
<input type="number" id="low" placeholder="Low">
|
||||
<input type="number" id="high" placeholder="High">
|
||||
<button onclick="calculate('clamp')">Clamp</button>
|
||||
</fieldset>
|
||||
|
||||
<fieldset>
|
||||
<legend>Clamp to Byte</legend>
|
||||
<input type="number" id="byte_value" placeholder="Value">
|
||||
<button onclick="calculate('clamp_to_byte')">Clamp to Byte</button>
|
||||
</fieldset>
|
||||
|
||||
<p id="output"></p>
|
||||
|
||||
<script>
|
||||
function calculate(operation) {
|
||||
const resultEl = document.getElementById('output');
|
||||
let url;
|
||||
if (operation === 'add' || operation === 'subtract') {
|
||||
const left = document.getElementById('left').value;
|
||||
const right = document.getElementById('right').value;
|
||||
url = '/' + operation + '?left=' + encodeURIComponent(left) + '&right=' + encodeURIComponent(right);
|
||||
} else if (operation === 'clamp') {
|
||||
const value = document.getElementById('value').value;
|
||||
const low = document.getElementById('low').value;
|
||||
const high = document.getElementById('high').value;
|
||||
url = '/clamp?value=' + encodeURIComponent(value) + '&low=' + encodeURIComponent(low) + '&high=' + encodeURIComponent(high);
|
||||
} else if (operation === 'clamp_to_byte') {
|
||||
const value = document.getElementById('byte_value').value;
|
||||
url = '/clamp_to_byte?value=' + encodeURIComponent(value);
|
||||
}
|
||||
fetch(url)
|
||||
.then(function(response) {
|
||||
return response.json().then(function(data) {
|
||||
if (!response.ok) {
|
||||
resultEl.textContent = 'Error: ' + (data.error || 'Unknown error');
|
||||
} else {
|
||||
resultEl.textContent = 'Result: ' + data.result;
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch(function() {
|
||||
resultEl.textContent = 'Error: Network error';
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
Flask>=3.0,<4.0
|
||||
@ -1,6 +1,6 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from typing import Sequence
|
||||
from typing import Sequence, Union
|
||||
|
||||
|
||||
def add(left: int, right: int) -> int:
|
||||
@ -32,3 +32,10 @@ def variance(values: Sequence[float]) -> float:
|
||||
return sum((x - mean) ** 2 for x in values) / len(values)
|
||||
|
||||
|
||||
def percentage(value: Union[int, float], total: Union[int, float]) -> float:
|
||||
if total == 0:
|
||||
raise ValueError
|
||||
return (value / total) * 100
|
||||
|
||||
|
||||
|
||||
|
||||
@ -117,3 +117,4 @@ class TestCalculatorClampToByteEndpoint(unittest.TestCase):
|
||||
def test_clamp_to_byte_invalid_input_returns_422(self) -> None:
|
||||
response = client.post("/api/v1/calculator/clamp-to-byte", json={"value": "abc"})
|
||||
self.assertEqual(response.status_code, 422)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user