61
Python Developer's Daily Routine Maxim Avanov (github.com/avanov)

Python Developer's Daily Routine

Embed Size (px)

Citation preview

Page 1: Python Developer's Daily Routine

Python Developer'sDaily Routine

Maxim Avanov (github.com/avanov)

Page 2: Python Developer's Daily Routine

Project structure

Page 3: Python Developer's Daily Routine

Project structure./chebit

|-- chebit

| -- __init__.py

-- setup.py

3

Page 4: Python Developer's Daily Routine

setup.pyfrom setuptools import setup, find_packages

setup(

name='Chebit',

version='0.0.1',

packages=find_packages(),

test_suite='tests',

tests_require=['pytest', 'coverage'],

install_requires=['Pyramid==1.5a2']

)

4

Page 5: Python Developer's Daily Routine

Bootstrapping$ python setup.py install

$ python setup.py develop

$ python setup.py test

$ python setup.py regtister sdist [bdist_egg] upload

5

Page 6: Python Developer's Daily Routine

Package

management

Page 7: Python Developer's Daily Routine

pip commands$ pip install Pyramid

$ pip install Pyramid --upgrade

$ pip install Pyramid==1.5a2 --upgrade

$ pip uninstall Pyramid

7

Page 8: Python Developer's Daily Routine

pip commands$ pip freeze

Babel==1.3

Chebit==0.0.1

CoffeeScript==1.0.8

Jinja2==2.7.1

Mako==0.9.0

...

8

Page 9: Python Developer's Daily Routine

pip-tools$ pip-review

requests==0.13.4 available (you have 0.13.2)

$ pip-review --auto

... <pip install output>

$ pip-review --interactive

requests==0.14.0 available (you have 0.13.2)

Upgrade now? [Y]es, [N]o, [A]ll, [Q]uit y

...

9

Page 10: Python Developer's Daily Routine

Cookiecutter• Generates initial project files from defined templates;

• File names are also templates;

• Transparently works with remote VCS.

10

Page 11: Python Developer's Daily Routine

Cookiecutter template./chebit-template

|-- cookiecutter.json

-- {{cookiecutter.project_name}}

|-- {{cookiecutter.project_name}}

| -- __init__.py

-- setup.py

11

Page 12: Python Developer's Daily Routine

cookiecutter.json{

"project_name": "myproject",

"project_version": "0.0.1"

}

12

Page 13: Python Developer's Daily Routine

Cookiecutter in action$ cookiecutter {project_template_path}

project_name (default is "myproject")? mynew

project_version (default is "0.0.1")? 0.1.1

$ tree ./mynew

./mynew

|-- mynew

| -- __init__.py

-- setup.py

13

Page 14: Python Developer's Daily Routine

We'd like to

isolate projectenvironments

Page 15: Python Developer's Daily Routine

virtualenv$ virtualenv ~/venv/{project_name}

$ source ~/venv/{project_name}/bin/activate

What if we need another Python version?$ virtualenv ~/venv/{project_name} -p python3

15

Page 16: Python Developer's Daily Routine

Still not happy

enough!

Page 17: Python Developer's Daily Routine

Introducing pyenv

Page 18: Python Developer's Daily Routine

pyenv - 5 steps to happiness$ git clone git://github.com/yyuu/pyenv.git ~/pyenv

$ echo 'export PYENV_ROOT="$HOME/pyenv"' >> ~/.bashrc

$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc

$ echo 'eval "$(pyenv init -)"' >> ~/.bashrc

$ exec $SHELL

18

Page 19: Python Developer's Daily Routine

pyenv - Rise and Shine!$ pyenv install -l

$ pyenv install 2.7.6

$ pyenv install 3.3.3

$ pyenv install pypy-2.2.1

$ pyenv rehash

19

Page 20: Python Developer's Daily Routine

The Happiness$ pyenv versions

$ pyenv version

$ pyenv local 2.7.6 3.3.3

$ pyenv global 3.3.3

$ pyenv shell pypy-2.2.1

$ pyenv whence 2to3

20

Page 21: Python Developer's Daily Routine

...

$ which python/home/ghostwriter/pyenv/shims/python

$ pythonPython 2.7.3 (87aa9de10f9c, Nov 24 2013, 18:48:13)[PyPy 2.2.1 with GCC 4.6.3] on linux2>>>>

21

Page 22: Python Developer's Daily Routine

virtualenv meets

pyenv

Page 23: Python Developer's Daily Routine

pyenv-virtualenv

$ git clone git://github.com/yyuu/pyenv-virtualenv.git ~/pyenv/plugins/pyenv-virtualenv

$ pyenv virtualenv pypy-2.2.1 ~/venv/{project_name}

23

Page 24: Python Developer's Daily Routine

pyenv-virtualenv in action$ pyenv virtualenvs

{project_name} (created from /home/ghostwriter/pyenv/versions/pypy-2.2.1)

$ source ~/pyenv/versions/{project_name}/bin/activate

24

Page 25: Python Developer's Daily Routine

Well done!

Page 26: Python Developer's Daily Routine

Now, what about

other components?

Page 27: Python Developer's Daily Routine

Virtualization

Page 28: Python Developer's Daily Routine

Vagrant• Written in Ruby

• Relies on VirtualBox

• Runs on Linux / OS X / Win

28

Page 29: Python Developer's Daily Routine

Juju• Written in Python / Go

• Relies on LXC

• Linux only

29

Page 30: Python Developer's Daily Routine

Docker• Written in Go

• Relies on LXC and AUFS

• Linux only

30

Page 31: Python Developer's Daily Routine

Vagrant's Success Story...1. Put a Vagrantfile into the project root

2. $ vagrant up

3. $ vagrant ssh

4. Profit!

31

Page 32: Python Developer's Daily Routine

Vagrantfile

Vagrant.configure("2") do |config|

config.vm.box = "project_name"

config.vm.box_url = "http://files.vagrantup.com/precise64.box"

config.vm.network :private_network, ip: "192.168.33.100"

end

32

Page 33: Python Developer's Daily Routine

...Vagrant's Success Story1. Put a Vagrantfile into the project root

2. $ vagrant up

3. $ vagrant ssh

4. Profit! ..???

5. Deployment

33

Page 34: Python Developer's Daily Routine

Deployment toolsChef (Ruby)

Puppet (Ruby)

Ansible (Python)

Salt (Python)

34

Page 35: Python Developer's Daily Routine

Ansible Success Story1. Specify an inventory file

2. Specify a playbook

3. $ ansible-playbook ./playbook.yml -i ./hosts

35

Page 36: Python Developer's Daily Routine

Ansible inventory[develop-vm]

192.168.33.100

36

Page 37: Python Developer's Daily Routine

Ansible playbook---

-

hosts: develop-vm

sudo: yes

tasks:

-

name: ensure apt cache is up to date

action: apt update_cache=yes

http://www.ansibleworks.com/docs/modules.html

37

Page 38: Python Developer's Daily Routine

Ansible meets Vagrant

$ ansible-playbook ./playbook.yml -i ./hosts -v -u vagrant -c paramiko --private-key=$HOME/.vagrant.d/insecure_private_key

or

$ vagrant provision

38

Page 39: Python Developer's Daily Routine

Ansible - Vagrant Integration# Vagrantfile

Vagrant.configure("2") do |config|

# ...

config.vm.provision :ansible do |ansible|

ansible.playbook = "./playbook.yml"

ansible.inventory_path = "./hosts"

ansible.verbose = "v"

end

end

39

Page 40: Python Developer's Daily Routine

Python & Web

Page 41: Python Developer's Daily Routine

Web Server Gateway InterfaceStandardized by PEP 333, PEP 3333

def application(environ, start_response):

start_response('200 OK', [('Content-Type', 'text/plain')])

yield 'Hello World\n'

41

Page 42: Python Developer's Daily Routine

WSGI Servers• CherryPy WSGI Server

• gunicorn

• tornado.wsgi

• uWSGI

• Waitress

http://nichol.as/benchmark-of-python-web-servers

42

Page 43: Python Developer's Daily Routine

Frameworks

Page 44: Python Developer's Daily Routine

Heavyweights• Django

• Twisted

• web2py

• Zope

44

Page 45: Python Developer's Daily Routine

Middleweights• CherryPy

• Pyramid

• Tornado

• TurboGears

45

Page 46: Python Developer's Daily Routine

Flyweights• Bottle

• Flask

• Webpy

46

Page 47: Python Developer's Daily Routine

Database Access

Page 48: Python Developer's Daily Routine

ORM• SQLAlchemy ORM

• Django ORM (Django only)

• Pony ORM

• Peewee

48

Page 49: Python Developer's Daily Routine

Something else• Native DB-API 2.0 (standardized with PEP 249)

• SQLAlchemy's raw queries

• mosql

49

Page 50: Python Developer's Daily Routine

SQL DDL Versioning & Data Migration• South (Django only)

• Alembic (SQLAlchemy)

• Pyrseas

50

Page 51: Python Developer's Daily Routine

Background task

processing

Page 52: Python Developer's Daily Routine

Celery• Out of the box solution for complex cases;

• Multiple brokers - RabbitMQ, Redis, RDBMS, SQS, MongoDB etc;

• Tasks scheduler;

• Results backends.

52

Page 53: Python Developer's Daily Routine

RQ (Redis Queue)• Designed to have a low barrier to enter;

• Uses Redis' Pub/Sub feature.

53

Page 54: Python Developer's Daily Routine

Process managers

Page 55: Python Developer's Daily Routine

Process managers• Supervisor

• Circus.io

55

Page 56: Python Developer's Daily Routine

Sentry

Page 57: Python Developer's Daily Routine
Page 58: Python Developer's Daily Routine
Page 59: Python Developer's Daily Routine
Page 60: Python Developer's Daily Routine

Testing tools• unittest

• py.test

• nose

• WebTest

• coverage

• tox

60

Page 61: Python Developer's Daily Routine

Thank you!