I am new to using numpy and one thing that I really don’t understand is indexing arrays. In the tentative tutorial there is this example: I have no idea why it does that last thing. Can anyone explain that to me? Thanks! Answer Your array consists of: One way of indexing it would be using a list of inte…
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)…
Creating Python daemon – ‘module’ object has no attribute ‘DaemonContext’
I’m trying to daemonize my app and am getting the error: It looks like other people are getting this error from the module not being installed. As a newcomer to Python it’s a bit confusing that there is a daemon and python-daemon package and also there’s two ways of installing python package…
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…
Insert static files literally into Jinja templates without parsing them
I’m trying to insert file into a page using Jinja 2.6 using the include tag. This worked fine until I started using characters in the file that are reminiscent of the Jinja syntax, at which point it realized it couldn’t parse them and bombed. Short of going though the file and escaping all charact…
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. …