Flattened list of list
>>> l = [[1,5,6], [4,5,9,7] ,[] ,[] ,[9]]
[[1, 5, 6], [4, 5, 9, 7], [], [], [9]]
>>> sum(l, [])
[1, 5, 6, 4, 5, 9, 7, 9]
>>> [i for x in l for i in x if type(x) is list]
[1, 5, 6, 4, 5, 9, 7, 9]
Else with for loop
>> for i, v in enumerate(range(10)):
>> print i,
>> else:
>> print 10
1 2 3 4 5 6 7 8 9 10
Zen of Python :: run this command in python console
import this
Virtualenv with system package
virtualenv --system-site-packages <env_name>
Virtualenv with python3
virtualenv --python=$(which python3) <env_name>
Virtualenv can’t move. You must edit environment path in
<your_env>/bin/activate
<your_env>/bin/activate.csh
<your_env>/bin/activate.finsh
Install py package into python2
pip install <package>
Install py package into python3
pip3 install <package>
Pip upgrade
pip install <package> --upgrade
or you can use pip-tool to review all pip-package to upgrade
pip install pip-tools
pip-review
pip-dump
Install Project as develop via pip
# same as 'python setup.py develope'
pip install --use-mirrors --editable .
Print without newline or spaces
import sys
for i in xrange(0,10):
sys.stdout.write(".")
sys.stdout.flush()
>>> execfile('filename.py')
import X
reload( X )
$ pip install --download-cache="/pth/to/downloaded/files" package
function line {
PYTHON_ARG="$1" python - <<END
import os
line_len = int(os.environ['PYTHON_ARG'])
print '-' * line_len
END
}
Calling a python function from bash script
python -c'import themodule; themodule.thefunction("boo!")'
How to print to stderr in Python?
print >> sys.stderr, 'word'
Virtualenv Python Power Tools: virtualenv
pip install virtualenv
Or
brew install pyenv-virtualenv
VirtualenvWrapper - Python Power Tools: virtualenvwrapper
pip install virtualenvwrapper
add this into your .bashrc/.zshrc
export WORKON_HOME=<env_path>
source /usr/local/bin/virtualenvwrapper.sh
brew install autoenv
echo 'source /usr/local/opt/autoenv/activate.sh' >> ~/.zshrc
# install
$ pip install --upgrade autopep8
# usage
$ autopep8 --in-place --aggressive --aggressive <filename>
# install
$ pip install pylint
$ apt-get install graphviz
# usage
pyreverse -o png <code>