Skip to content

Tag: python

How do I get the user agent with Flask?

I’m trying to get access to the user agent with Flask, but I either can’t find the documentation on it, or it doesn’t tell me. Answer You can also use the request.user_agent object which contains the following attributes which are created based on the useragent string: platform (windows, lin…

How do I get the day of week given a date?

I want to find out the following: given a date (datetime object), what is the corresponding day of the week? For instance, Sunday is the first day, Monday: second day.. and so on And then if the input is something like today’s date. Example The output is maybe 6 (since it’s Friday) Answer Use week…

Fast way of counting non-zero bits in positive integer

I need a fast way to count the number of bits in an integer in python. My current solution is but I am wondering if there is any faster way of doing this? PS: (i am representing a big 2D binary array as a single list of numbers and doing bitwise operations, and that brings the time down from hours

Pip install Matplotlib error with virtualenv

I am trying to install matplotlib in a new virtualenv. When I do: or I get this error: Anyone have an idea what is going on? Any help much appreciated. Answer Building Matplotlib requires libpng (and freetype, as well) which isn’t a python library, so pip doesn’t handle installing it (or freetype)…

How do I parallelize a simple Python loop?

This is probably a trivial question, but how do I parallelize the following loop in python? I know how to start single threads in Python but I don’t know how to “collect” the results. Multiple processes would be fine too – whatever is easiest for this case. I’m using currently Li…

Celery Received unregistered task of type (run example)

I’m trying to run example from Celery documentation. I run: celeryd –loglevel=INFO tasks.py: run_task.py: In same folder celeryconfig.py: When I run “run_task.py”: on python console errors on celeryd server Please explain what’s the problem. Answer You can see the current list of…

TypeError: ‘int’ object is not callable

Given the following integers and calculation This results in: How can I round the output to an integer? Answer Somewhere else in your code you have something that looks like this: Then when you write that is interpreted as meaning a function call on the object bound to round, which is an int. And that fails. …