What does if __name__ == “__main__”: do?

What does this do? [python]if __name__ == "__main__": print("Hello world!") [/python]   40   Short It’s boilerplate code that protects users from accidentally invoking the script when they didn’t intend to. Here are some common problems when the guard is omitted from a script: If you import the guardless script in another script (e.g. import … Read more

What does the “yield” keyword do?

Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. without enough detail may be edited or deleted. What is the use of the yield keyword in Python? What does it do? For example, I’m trying to understand this code1: def _get_child_candidates(self, distance, … Read more

Django – Rebuild a query string without one of the variables

I have a Django view that processes a GET request. I want to rebuild the query string to include all variables except for one. I was initially using list comprehension: >>> from django.http import QueryDict >>> q = QueryDict(‘a=2&b=4&c=test’) // <— make believe this is request.GET >>> z = QueryDict(”).copy() >>> z.update(dict([x for x in … Read more

How to use: while not in

I’m trying to check if a list has no member as boolean operator AND, OR, NOT. I use: while (‘AND’ and ‘OR’ and ‘NOT’) not in list: print ‘No boolean operator’ However, when my input is: a1 c2 OR c3 AND, it prints ‘No boolean operator’, which means this list is considered no boolean operator … Read more

HTML5 video element non-seekable when using Django development server

I’ve got a Django app serving a webpage with an HTML5 element. There’s a wierd “feature”, turning the video element to be non-seekable: video.seekable returns a timeRanges object with length=0, whereas it should be length=1. This means I can’t edit the video. JavaScript can’t do anything either. The thing is, when I upload the problematic … Read more