Hacker News new | comments | show | ask | jobs | submit login
Django Redis Pipeline trick (pnegahdar.com)
22 points by Goopplesoft 1604 days ago | hide | past | web | 11 comments | favorite



"if request.pipeline.command_stack != []"

No

That's the worse way to checking it's empty, even if pipeline was a python list

And this pipeline is interesting, but in this case, it's useful for setting values, not reading them.


Hey! I (author) agree this isn't a great way to check equality in python, however after testing this is the way I'm sure it will work 100% of the time, so I went with it. If not [] would be more pythonic though.


Checking the code: https://github.com/andymccurdy/redis-py/blob/master/redis/cl...

It implements __len__ so there's your answer


I would argue that it is more readable to do:

if request.pipeline.command_stack: request.pipeline.execute()

rather than

if len(request.pipeline) > 0: request.pipeline.execute()

thoughts?


The 1st example is better

(Yes, if len returns 0 it's evaluated as false, just tested this)

But for the 2nd I would prefer

if not len(request.pipeline):


if len(request.pipeline):

you want to execute only if it has a length.

cool though, I am using pipelines for something at work and read this post and realized I was executing even if there was nothing to execute!


Oh, of course =)

I don't know why I put the 'not' there


Thanks, ill update the code in a bit.


Great, sorry if I was too harsh in the original post


if not request.pipeline.command_stack you mean? Or did I mis-read something


Yup, I said that since command_stack is a list. You can also just check the pipeline since __len__ returns the command_stack length. Will update code when I get to a computer.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: