botbotbot 's blog

All about Python

Tips:

  • 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()
    
  • How to execute a file within the python interpreter?

>>> execfile('filename.py')
import X
reload( X )
$ pip install --download-cache="/pth/to/downloaded/files" package

Tools:

# 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>

Python Project

  1. What are good open source projects in Python to contribute to and start learning Python?
  2. OpenHatch find project to contribute
  3. Ask HN: Good Python codebases to read?

Idiomatic Python:

  1. Be Pythonic
  2. What is Pythonic?
  3. Python track: python idioms
  4. Code Like a Pythonista: Idiomatic Python
  5. PEP8
  6. The Hitchhiker’s Guide to Python!
  7. Index of Python Enhancement Proposals
  8. 30 Python Language Features and Tricks You May Not Know About
  9. Transforming code into Beautiful, Idiomatic Python by Raymond Hettinger
  10. Python Tips, Tricks, and Hacks
  11. Writing Idiomatic Python
  12. Python 3 Patterns, Recipes and Idioms
  13. Decorators: A Powerful Weapon in Your Python Arsenal
  14. Anti-Patterns in Python Programming
  15. Best python tricks and one-liners
  16. Python Tips and Traps
  17. Python: faster way Tricks for your code run faster
  18. The Little Book of Python Anti-Patterns

Tutorial Python

  1. Python Koans
  2. New Coder Python
  3. Problem Solving with Algorithms and Data Structures Python
  4. Everyday Python Project
  5. Python Regular Expressions
  6. Python Practice Book

Functional Programming in Python

  1. O’Reilly Functional Programming in Python
  2. FUNCTIONAL PROGRAMMING WITH PYTHON by BY ALEXEY KACHAYEV, 2012
  3. Functional Programming HOWTO

Design Patterns Python:

  1. Python Patterns
  2. Six GoF design patterns, Python style
  3. Generic adapter class in Python
  4. Bridge Patterns Python
  5. Python Best Practice Patterns by Vladimir Keleshev
  6. Python SOLID
  7. The Clean Architecture in Python
  8. Stop Writing Classes

Optimization Python

  1. 10 Ways to Make Python Go Faster
  2. Speeding Up Your Python Code
  3. PythonSpeed
  4. How to Make Python Faster Without Trying That Much

Python Debugger (pdb)

  1. Getting started with the Python Debugger pdb
  2. Python Debugging Techniques
  3. Debugging in Python
  4. Getting Started with the Python Debugger
  5. pdb – Interactive Debugger
  6. Tutorial: Debugging your Python Applications with pdb(Youtube)
  7. Introduction to PDB(Youtube)
  8. setting breakpoints with nosetests –pdb option

Logging

  1. Become a logging expert in 30 minutes(Youtube)

Framework

  1. Awesome Python

Python Tutorial

  1. Must-watch videos about Python
  2. pyvideo.org

Python Testing

ETC.

  1. Terminating a Python script
  2. Should I put #! (shebang) in Python scripts, and what form should it take?
  3. Simple file server to serve current directory
  4. How to reload python module imported using “from module import *”
  5. 10 awesome features of Python that you can’t use because you refuse to upgrade to Python 3
  6. From C++ to Python (1) - ไทย
  7. From C++ to Python (2) - ไทย