Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the structure more similar to other languages #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Check the [Python documentation](https://docs.python.org/3/library/venv.html) fo
You can start the application like this:

cd src
python -m prices
python -m main

Note there is no webpage on the default url - try this url as an example to check it's running: http://127.0.0.1:3005/prices?type=1jour

Expand Down
6 changes: 6 additions & 0 deletions python/src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from prices import create_app


if __name__ == "__main__":
app = create_app()
Copy link

@nicoespeon nicoespeon Mar 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martinsson unfortunately, that would not work because it returns a tuple:

tuple

If we go with this approach, I'd suggest extracting the app part like this:

Suggested change
app = create_app()
app, connection = create_app()

after

Thanks for the suggestion, it's interesting. I don't have a solid opinion to support this decision, but having experimented with this, I can say "it works". I would go for the most idiomatic approach in the language, but I don't have a lot of XP with Flask myself (it's been years) so I can't help here 😅

app.run(port=3005)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also make it non-threaded, otherwise it doesn't work when we use the sqlite database:
thread

Suggested change
app.run(port=3005)
app.run(port=3005, threaded=False)

That's because SQLite objects can't be used in different threads, but Flask is multi-threaded by default.

126 changes: 62 additions & 64 deletions python/src/prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,75 @@
from datetime import datetime
from db import create_lift_pass_db_connection

app = Flask("lift-pass-pricing")

connection_options = {
"host": 'localhost',
"user": 'root',
"database": 'lift_pass',
"password": 'mysql'}
def create_app():
connection_options = {
"host": 'localhost',
"user": 'root',
"database": 'lift_pass',
"password": 'mysql'}

connection = None
connection = create_lift_pass_db_connection(connection_options)

@app.route("/prices", methods=['GET', 'PUT'])
def prices():
res = {}
global connection
if connection is None:
connection = create_lift_pass_db_connection(connection_options)
if request.method == 'PUT':
lift_pass_cost = request.args["cost"]
lift_pass_type = request.args["type"]
cursor = connection.cursor()
cursor.execute('INSERT INTO `base_price` (type, cost) VALUES (?, ?) ' +
'ON DUPLICATE KEY UPDATE cost = ?', (lift_pass_type, lift_pass_cost, lift_pass_cost))
return {}
elif request.method == 'GET':
cursor = connection.cursor()
cursor.execute(f'SELECT cost FROM base_price '
+ 'WHERE type = ? ', (request.args['type'],))
row = cursor.fetchone()
result = {"cost": row[0]}
if 'age' in request.args and request.args.get('age', type=int) < 6:
res["cost"] = 0
else:
if "type" in request.args and request.args["type"] != "night":
cursor = connection.cursor()
cursor.execute('SELECT * FROM holidays')
is_holiday = False
reduction = 0
for row in cursor.fetchall():
holiday = row[0]
if "date" in request.args:
d = datetime.fromisoformat(request.args["date"])
if d.year == holiday.year and d.month == holiday.month and holiday.day == d.day:
is_holiday = True
if not is_holiday and "date" in request.args and datetime.fromisoformat(request.args["date"]).weekday() == 0:
reduction = 35
app = Flask("lift-pass-pricing")

# TODO: apply reduction for others
if 'age' in request.args and request.args.get('age', type=int) < 15:
res['cost'] = math.ceil(result["cost"]*.7)
else:
if 'age' not in request.args:
cost = result['cost'] * (1 - reduction/100)
res['cost'] = math.ceil(cost)
@app.route("/prices", methods=['GET', 'PUT'])
def prices():
res = {}
if request.method == 'PUT':
lift_pass_cost = request.args["cost"]
lift_pass_type = request.args["type"]
cursor = connection.cursor()
cursor.execute('INSERT INTO `base_price` (type, cost) VALUES (?, ?) ' +
'ON DUPLICATE KEY UPDATE cost = ?', (lift_pass_type, lift_pass_cost, lift_pass_cost))
return {}
elif request.method == 'GET':
cursor = connection.cursor()
cursor.execute(f'SELECT cost FROM base_price '
+ 'WHERE type = ? ', (request.args['type'],))
row = cursor.fetchone()
result = {"cost": row[0]}
if 'age' in request.args and request.args.get('age', type=int) < 6:
res["cost"] = 0
else:
if "type" in request.args and request.args["type"] != "night":
cursor = connection.cursor()
cursor.execute('SELECT * FROM holidays')
is_holiday = False
reduction = 0
for row in cursor.fetchall():
holiday = row[0]
if "date" in request.args:
d = datetime.fromisoformat(request.args["date"])
if d.year == holiday.year and d.month == holiday.month and holiday.day == d.day:
is_holiday = True
if not is_holiday and "date" in request.args and datetime.fromisoformat(
request.args["date"]).weekday() == 0:
reduction = 35

# TODO: apply reduction for others
if 'age' in request.args and request.args.get('age', type=int) < 15:
res['cost'] = math.ceil(result["cost"] * .7)
else:
if 'age' in request.args and request.args.get('age', type=int) > 64:
cost = result['cost'] * .75 * (1 - reduction / 100)
res['cost'] = math.ceil(cost)
elif 'age' in request.args:
if 'age' not in request.args:
cost = result['cost'] * (1 - reduction / 100)
res['cost'] = math.ceil(cost)
else:
if 'age' in request.args and request.args.get('age', type=int) >= 6:
if request.args.get('age', type=int) > 64:
res['cost'] = math.ceil(result['cost'] * .4)
else:
res.update(result)
else:
if 'age' in request.args and request.args.get('age', type=int) > 64:
cost = result['cost'] * .75 * (1 - reduction / 100)
res['cost'] = math.ceil(cost)
elif 'age' in request.args:
cost = result['cost'] * (1 - reduction / 100)
res['cost'] = math.ceil(cost)
else:
res['cost'] = 0

return res
if 'age' in request.args and request.args.get('age', type=int) >= 6:
if request.args.get('age', type=int) > 64:
res['cost'] = math.ceil(result['cost'] * .4)
else:
res.update(result)
else:
res['cost'] = 0

return res

if __name__ == "__main__":
app.run(port=3005)
return app, connection
4 changes: 3 additions & 1 deletion python/test/test_prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
from datetime import datetime
import time

from prices import app
from prices import create_app

TEST_PORT = 3006


def server(port):
app, connection = create_app()
app.run(port=port)



def wait_for_server_to_start(server_url):
started = False
while not started:
Expand Down