Why does using from __future__ import print_function breaks Python2-style print?

First of all, from __future__ import print_function needs to be the first line of code in your script (aside from some exceptions mentioned below). Second of all, as other answers have said, you have to use print as a function now. That’s the whole point of from __future__ import print_function; to bring the print function from Python 3 into Python 2.6+. __future__ statements need … Read more

Timeout on a function call

You may use the signal package if you are running on UNIX: 10 seconds after the call signal.alarm(10), the handler is called. This raises an exception that you can intercept from the regular Python code. This module doesn’t play well with threads (but then, who does?) Note that since we raise an exception when timeout happens, it may end … Read more

Python dictionary : removing u’ chars

Some databases such as Sqlite3 let you define converter and adapter functions so you can retrieve text as str rather than unicode. Unfortunately, MongoDB doesn’t provide this option for any of the commonly needed types such as str, decimal or datetime: http://api.mongodb.org/python/current/tutorial.html#a-note-on-unicode-strings http://api.mongodb.org/python/current/faq.html#how-can-i-store-decimal-decimal-instances http://api.mongodb.org/python/current/faq.html#how-can-i-save-a-datetime-date-instance Having eliminated Mongo options, that leaves writing Python code to do the conversion after the data is retrieved. … Read more

clang error: unknown argument: ‘-mno-fused-madd’ (python package installation failure)

You can tell clang to not raise this as an error by setting the following environment variables prior compilation: Then pip install psycopg2should work. I had the same when trying to pip install lxml. Edit: if you are installing as superuser (which will likely be the case if you are trying to append to /Library/Python/2.7/site-packages, the native Apple … Read more

How can I remove Nan from list Python/NumPy

The question has changed, so to has the answer: Strings can’t be tested using math.isnan as this expects a float argument. In your countries list, you have floats and strings. In your case the following should suffice: Old answer In your countries list, the literal ‘nan’ is a string not the Python float nan which is equivalent to: In your case the following should suffice: