mirror of
https://gitea.com/mcereda/oam.git
synced 2026-02-09 05:44:23 +00:00
3.2 KiB
3.2 KiB
Python
TL,DR
# String formatting with f-strings.
f"Hello, {name}. You are {age}."
F"{name.lower()} is funny."
# Make elements in a list unique.
# Keep the resulting list mutable.
unique_list = list(set(redundant_list))
Web servers
Flask
request.argsgets query argumentsrequest.formgets POST arguments
from flask import request, jsonify
@app.route('/get/questions/', methods=['GET', 'POST','DELETE', 'PATCH'])
def question():
if request.method == 'GET':
start = request.args.get('start', default=0, type=int)
limit_url = request.args.get('limit', default=20, type=int)
data = [doc for doc in questions]
return jsonify(
isError = False,
message = "Success",
statusCode = 200,
data= data
), 200
if request.method == 'POST':
question = request.form.get('question')
topics = request.form.get('topics')
return jsonify(
isError = True,
message = "Conflict",
statusCode = 409,
data = data
), 409
WSGI server
You can use waitress:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "<h1>Hello!</h1>"
if __name__ == "__main__":
from waitress import serve
serve(app, host="0.0.0.0", port=8080)
pip install flask waitress
python hello.py
Further readings
- PIP
- How To Update All Python Packages
- invl/pip-autoremove
- Data types
- F-strings
- How to filter list elements in Python
- Logging
- Flask at first run: do not use the development server in a production environment
- Flask example with POST
- Multi-value query parameters with Flask
- *args and **kwargs in Python
- An intro to threading in Python
- ThreadPoolExecutor in Python: the complete guide