Fix Python – What is a good practice to check if an environmental variable exists or not?

I want to check my environment for the existence of a variable, say “FOO”, in Python. For this purpose, I am using the os standard library. After reading the library’s documentation, I have figured out 2 ways to achieve my goal:
Method 1:
if “FOO” in os.environ:
pass

Method 2:
if os.getenv(“FOO”) is not None:
pass

I would like to know ….

Fix Python – Python UTC datetime object’s ISO format doesn’t include Z (Zulu or Zero offset)

Why python 2.7 doesn’t include Z character (Zulu or zero offset) at the end of UTC datetime object’s isoformat string unlike JavaScript?
>>> datetime.datetime.utcnow().isoformat()
‘2013-10-29T09:14:03.895210’

Whereas in javascript
>>> console.log(new Date().toISOString());
2013-10-29T09:38:41.341Z

….

Fix Python – sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’)

I am using sklearn and having a problem with the affinity propagation. I have built an input matrix and I keep getting the following error.
ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’).

I have run
np.isnan(mat.any()) #and gets False
np.isfinite(mat.all()) #and gets True

I tried using
mat[np.isfinite(mat) ==….

Fix Python – How to display pandas DataFrame of floats using a format string for columns?

I would like to display a pandas dataframe with a given format using print() and the IPython display(). For example:
df = pd.DataFrame([123.4567, 234.5678, 345.6789, 456.7890],
index=[‘foo’,’bar’,’baz’,’quux’],
columns=[‘cost’])
print df

cost
foo 123.4567
bar 234.5678
baz 345.6789
quux 456.7890
….

Fix Python – Using both Python 2.x and Python 3.x in IPython Notebook

I use IPython notebooks and would like to be able to select to create a 2.x or 3.x python notebook in IPython.
I initially had Anaconda. With Anaconda a global environment variable had to be changed to select what version of python you want and then IPython could be started. This is not what I was looking for so I uninstalled Anaconda and now ha….

Fix Python – What is the difference between json.load() and json.loads() functions

In Python, what is the difference between json.load() and json.loads()?
I guess that the load() function must be used with a file object (I need thus to use a context manager) while the loads() function take the path to the file as a string. It is a bit confusing.
Does the letter “s” in json.loads() stand for string?
Thanks a lot for your answers!….