4 ms·
import flask import random app=flask.Flask() @app.route('/') def random_redir(): with open('urls.txt') as of: return flask.redirect(random.cho
by pinkcan 3y ago
import flask
import random
app=flask.Flask()
@app.route('/')
def random_redir():
with open('urls.txt') as of:
return flask.redirect(random.choice(of.readlines()))
- marginalia_nu 3y agoProlly don't want to read the file on every request though
- pinkcan 3y agocongratulations, you've optimised a snippet that never ran – it was typed into the post form from memory
- marginalia_nu 3y agoPremature optimization may sometimes be unadvisable, but in this case the optimized version uses half the memory, is two orders of magnitude faster, and doesn't let your users DOS you by triggering huge file reads on each request.
- pinkcan 3y ago> uses half the memory this is false > two orders of magnitude faster incorrect > doesn't let your users DOS you by triggering huge file reads on each request. I understood what you were trying to do on your first reply – "Prolly don't want to read the file on every request though" – no need to repeat yourself. plese remember: 1. it's pseudo code, the optimisation is superfluous 2. it never ran, the optimisation is useless
- marginalia_nu 3y agoOptimization is never useless. Even code that doesn't run is best written in a resource-effective manner.
- pinkcan 3y agounless the intent of the code is to demonstrate some implementation where the optimisations would be distracting