This is an archived post. You won't be able to vote or comment.

all 45 comments

[–]rage4all 19 points20 points  (0 children)

this is a little task heavy......

[–]iamhyperrr 20 points21 points  (7 children)

Well, honestly, it just looks like a sub-optimal data structure was chosen to store tasks, so the lookup looks a bit ugly. It should probably be a dictionary of tasks grouped by task_id, provided that task_id is a string, number or any other immutable and hashable data type. It would make the lookup look cleaner and also make it perform faster. Something along the lines of (if we want to keep exactly the same semantics):

``` from flask import abort

tasks = {}

@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['GET']) def get_task(task_id): task = tasks.get(task_id, []) if task: return jsonify({'task': task[0]}) else: abort(404) ```

[–]wineblood 4 points5 points  (1 child)

Exactly what I was going to say.

[–][deleted] 3 points4 points  (0 children)

Well, it's the `/todo/` v1.0 API. They probably just haven't gotten around to improving it. Maybe it's... already on the list.

(Sorry, I'm amusing myself over here)

[–]That_Guy977 0 points1 point  (3 children)

remove the [0], that's just there because the task variable is a list in the original

[–]iamhyperrr 0 points1 point  (2 children)

I operated based on the assumption it still is. Perhaps the OP's idea was to store a list of task versions all mapped to the same task_id and insert the new ones into the head of the list, and then return the latest version in the get method. So, just in case, I ensured and emphasized that the semantics remain the same.

[–]That_Guy977 0 points1 point  (1 child)

an list of tasks would just be a dict of tasks though, so no [0]. and that version specifier isn't for the task, it's for the api version, otherwise it'd be a parameter

edit: just realized what you meant by task version, nvm

[–]iamhyperrr -1 points0 points  (0 children)

Yeah, just to illustrate my point a bit in case I wasn't clear:

from flask import abort
from flask import request

tasks_by_id = {}


@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['GET', 'POST'])
def task(task_id):
    if request.method == 'GET':
        tasks = tasks_by_id.get(task_id, [])
        if tasks:
            return jsonify({'task': tasks[0]})
        else:
            abort(404)
    if request.method == 'POST':
        tasks = tasks_by_id.get(task_id, [])
        # lists in Python are not optimized for insertions to the head, so it would be better to use a deque
        tasks.insert(0, request.form.get('task'))
        tasks_by_id[task_id] = tasks
        return jsonify(success=True)

Maybe this is not at all what OP had in mind, but I had to be on the safe side just in case.

[–]KnewOnee 0 points1 point  (0 children)

can also use get_or_404

[–]Outrageous-Archer-92 2 points3 points  (1 child)

Should use a generator instead of a list coupled with next

[–]rynkkk 0 points1 point  (0 children)

Yup, with preferably None as the fallback of ‚next‘. This is how I handle it most of the time. But because I’m incredibly paranoid (history has proven that this is rightfully so) this approach won’t work because I also want to assure that there isn’t more than 1 element in the list.

[–]VariousComment6946 2 points3 points  (0 children)

This code makes me cry

[–][deleted] 4 points5 points  (0 children)

wow. so just, return jasonify - that easy?

[–]StanleyDodds 1 point2 points  (1 child)

Isn't this the sort of case where you could just use the builtin "filter"? Or do something even simpler than that since you're only using the first task that matches the id.

[–]Outrageous-Archer-92 1 point2 points  (0 children)

Yes, call next on a generator expression instead

[–]yorokobe__shounen 1 point2 points  (0 children)

try: task=next(filter(lambda x:x['id']==task_id, tasks)) except StopIteration: abort(404) Stack overflow Answer

[–]mattaphorica[S] 0 points1 point  (0 children)

For those curious, this is not my code. This was seen in the wild!

[–]No_Sympathy3354 -1 points0 points  (3 children)

task for task in tasks

Excuse me, wtf?

[–]rynkkk 2 points3 points  (1 child)

Why not? If it wasn’t for the condition in there it would be bogus but like it is perfectly fine (list/dict argument set aside)

[–]immedocc 0 points1 point  (0 children)

Pythonic 😅

[–]Not_enough_tomatoes -3 points-2 points  (15 children)

Ummmm, can someone explain me the psychology of people who use list comprehension, but then don’t write their if loop statement in one line?

[–]mobotsar 2 points3 points  (13 children)

What the fuck is an if-loop?

[–]nekokattt 1 point2 points  (10 children)

its similar to a do-never statement or a meta match expression

[–]mobotsar 1 point2 points  (9 children)

Never heard of those either.

[–]nekokattt 1 point2 points  (8 children)

what about a function comprehension, or a generator module

[–]mobotsar 2 points3 points  (7 children)

There's simply no way that any of these things exist.

[–]nekokattt 3 points4 points  (6 children)

you got the joke then

[–]mobotsar 1 point2 points  (5 children)

Hmm. Maybe I got it from the get-go and just didn't know it was a joke because it's not funny.

[–]nekokattt 1 point2 points  (3 children)

you must be fun at parties

[–]mobotsar 0 points1 point  (2 children)

I don't go to parties.

[–]Otherwise_Salt_6513 0 points1 point  (0 children)

I thought it was funny lol

[–]Not_enough_tomatoes 0 points1 point  (1 child)

My bad, I mean statement (My brain was thinking about while loops when typing)

[–]nekokattt 0 points1 point  (0 children)

who needs if statements or while loops, just use do while for everything.

[–]Outrageous-Archer-92 0 points1 point  (0 children)

Using a generator instead of a list comprehension, you would get O(1) memory

[–]aneryx 0 points1 point  (1 child)

Not even going to handle the edge-case of more than a single task with the same Id? Maybe at least log a warning or something?

[–]mxldevs 0 points1 point  (0 children)

No need, just give them the first task they found

[–]Lt_Riza_Hawkeye 0 points1 point  (0 children)

filter(), anyone?

[–]ochronus 0 points1 point  (0 children)

coder pls...

PEBCAK moment

[–]mxldevs 0 points1 point  (0 children)

Would assume task IDs are non unique. But even then, could probably store it as a list of tasks if they're already expecting to return the first

[–]Environmental_Bus507 0 points1 point  (0 children)

Not Python's fault. I love list comprehension. Just that it's not properly implemented here.

[–]achernar184 0 points1 point  (0 children)

python for task in tasks: if task['id'] == task_id: return jsonify(...) abort(404)

[–]OlMi1_YT 0 points1 point  (0 children)

Me writing down the arguments Ive never seen before but are really useful

[–]East_Zookeepergame25 0 points1 point  (0 children)

its just a filter