Fix Python – Should import statements always be at the top of a module?

PEP 8 states:

Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

However if the class/method/function that I am importing is only used in rare cases, surely it is more efficient to do the import when it is needed?
Isn’t this:
class SomeClass(object):

def not….

Fix Python – Convert Python dict into a dataframe

I have a Python dictionary like the following:
{u’2012-06-08′: 388,
u’2012-06-09′: 388,
u’2012-06-10′: 388,
u’2012-06-11′: 389,
u’2012-06-12′: 389,
u’2012-06-13′: 389,
u’2012-06-14′: 389,
u’2012-06-15′: 389,
u’2012-06-16′: 389,
u’2012-06-17′: 389,
u’2012-06-18′: 390,
u’2012-06-19′: 390,
u’2012-06-20′: 390,
u’2012-06-21′: 390,
u’2012-….

Fix Python – Iterating through a range of dates in Python

I have the following code to do this, but how can I do it better? Right now I think it’s better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension.
day_count = (end_date – start_date).days + 1
for single_date in [d for d in (start_date + timedelta(n) for n in range(day_count)) if d <= end_d....

Fix Python – Unresolved reference issue in PyCharm

I have a directory structure
├── simulate.py
├── src
│   ├── networkAlgorithm.py
│   ├── …

And I can access the network module with sys.path.insert().
import sys
import os.path
sys.path.insert(0, “./src”)
from networkAlgorithm import *

However, pycharm complains that it cannot access the module. How can I teach pycham to resolve the refere….

Fix Python – Error after upgrading pip: cannot import name ‘main’

Whenever I am trying to install any package using pip, I am getting this import error:
guru@guru-notebook:~$ pip3 install numpy
Traceback (most recent call last):
File “/usr/bin/pip3”, line 9, in
from pip import main
ImportError: cannot import name ‘main’

guru@guru-notebook:~$ cat `which pip3`
#!/usr/bin/python3
# GENERATED BY DEB….