SlideShare a Scribd company logo
Isolated Development
            in Python
1. pip installer
pip installer


 $ pip install <package>
pip installer


 $ pip install <package>


                Download package from pypi.python.org
pip installer


 $ pip install <package>


                Download package from pypi.python.org




 $ pip install <directory>
 $ pip install <tar.gz>
pip installer


 $ pip install <gitrepo>
pip installer


 $ pip install <gitrepo>




                           GREAT!!
pip installer


 $ pip install <gitrepo>




                           GREAT!!
 $ pip install git+git://github.com/ajdiaz/mole
 $ pip install git+ssh://github.com/ajdiaz/mole
 $ pip install git+git://github.com/ajdiaz/mole@840d25
 $ pip install git+git://github.com/ajdiaz/mole@devel-branch
 $ pip install git+git://....@devel-branch#egg=Mole
pip installer


 $ pip freeze
pip installer


 $ pip freeze
 Fabric==1.5.2
 GitPython==0.3.2.RC1
 Jinja2==2.6
 Pygments==1.6
 Sphinx==1.2b1          Create requirements.txt
 argparse==1.2.1
 async==0.6.1
 boto==2.7.0
 cuisine==0.5.1
 distribute==0.6.24
 docutils==0.10
 gitdb==0.5.4
 mico==0
 paramiko==1.9.0
 pycrypto==2.6
 smmap==0.8.2
 wsgiref==0.1.2
2. Virtualenv: a jail for python
Virtualenv: the python jail


 $ virtualenv --python=/usr/bin/python2.7 mynewenvironment
Virtualenv: the python jail


 $ virtualenv --python=/usr/bin/python2.7 mynewenvironment




                      OR EVEN BETTER
Virtualenv: the python jail


 $ virtualenv --python=/usr/bin/python2.7 mynewenvironment




                      OR EVEN BETTER




 $ mkvirtualenv --python=/usr/bin/python2.7 mynewenvironment
Virtualenv: the python jail


  $ virtualenv --python=/usr/bin/python2.7 mynewenvironment




                       OR EVEN BETTER




  $ mkvirtualenv --python=/usr/bin/python2.7 mynewenvironment




mkvirtualenvwrapper
Virtualenvwrapper


$ mkvirtualenv test
Virtualenvwrapper


$ mkvirtualenv test
New python executable in test/bin/python
Installing
distribute......................................................................................................................
.......................................................................done.
Installing pip...............done.
virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/preactivate
virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postactivate
virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/get_env_details
(test) $
Virtualenvwrapper


$ mkvirtualenv test
New python executable in test/bin/python
Installing
distribute......................................................................................................................
.......................................................................done.
Installing pip...............done.
virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/preactivate
virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postactivate
virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/get_env_details
(test) $ which python
/home/ajdiaz/env/test/bin/python
Virtualenvwrapper


$ mkvirtualenv test
New python executable in test/bin/python
Installing
distribute......................................................................................................................
.......................................................................done.
Installing pip...............done.
virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/preactivate
virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postactivate
virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/get_env_details
(test) $ which python
/home/ajdiaz/env/test/bin/python
                                                                   yep, these are hooks!
Unit testing
Unit testing: nose


class A(object):
  def __init__(self):
    self.value = "Some Value"

  def return_true(self):
    return True                 save it in a.py
  def raise_exc(self, val):
    raise KeyError(val)
Unit testing: nose

from a import A
from nose.tools import assert_equal
from nose.tools import assert_not_equal
from nose.tools import assert_raises
from nose.tools import raises

class TestA(object):
  @classmethod
  def setup_class(klass):
    """This method is run once for each class before any tests are run"""

  @classmethod
  def teardown_class(klass):
    """This method is run once for each class _after_ all tests are run"""

  def setUp(self):
    """This method is run once before _each_ test method is executed"""

  def teardown(self):
    """This method is run once after _each_ test method is executed"""



            .... continue ....
Unit testing: nose

def test_init(self):
    a = A()
    assert_equal(a.value, "Some Value")
    assert_not_equal(a.value, "Incorrect Value")

 def test_return_true(self):
   a = A()
   assert_equal(a.return_true(), True)
   assert_not_equal(a.return_true(), False)

 def test_raise_exc(self):
   a = A()
   assert_raises(KeyError, a.raise_exc, "A value")

 @raises(KeyError)
 def test_raise_exc_with_decorator(self):
   a = A()
   a.raise_exc("A message")




                 save it in tests/testa.py
Unit testing: nose


$ nosetests -v tests/
Unit testing: nose


$ nosetests -v tests/


testa.TestA.test_init ... ok
testa.TestA.test_raise_exc ... ok
testa.TestA.test_raise_exc_with_decorator ... ok
testa.TestA.test_return_true ... ok

---------------------------------------------------------
Ran 4 tests in 0.002s

OK
Unit testing: Bonus: code coverage


$ pip install coverage

$ nosetests --with-coverage
....
Name Stmts Miss Cover Missing
-------------------------------------
a      8    0 100%
-------------------------------------
Ran 4 tests in 0.006s OK
Packaging Python Eggs
Python eggs: basic setup.py


    from setuptools import setup

setup(
  name = "example",
  version = "1.0",
  description = "An example package",
     author='Andres J. Diaz'
)
Python eggs: basic setup.py


from setuptools import setup, find_packages

setup(
  name = "example",
  version = "1.0",
  description = "An example package",
  author='Andres J. Diaz',
  packages=find_packages()
)
Python eggs: complex setup.py


import re

from setuptools import setup, find_packages
from os import path

def parse_requirements(file_name):
  requirements = []
  for line in open(file_name, 'r').read().split('n'):
     if re.match(r'(s*#)|(s*$)', line):
       continue
     if re.match(r's*-es+', line):
       requirements.append(re.sub(r's*-es+.*#egg=(.*)$', r'1', line))
     elif re.match(r's*-fs+', line):
       pass
     else:
       requirements.append(line)
  return requirements

        .... continue ....
Python eggs: complex setup.py


def parse_dependency_links(file_name):
  dependency_links = []
  for line in open(file_name, 'r').read().split('n'):
     if re.match(r's*-[ef]s+', line):
       dependency_links.append(re.sub(r's*-[ef]s+', '', line))
  return dependency_links


def get_file_contents(filename):
  fd = file(path.join(path.dirname(__file__), filename), "r")
  content = fd.read()
  fd.close()
  return content




        .... continue ....
Python eggs: complex setup.py


setup(
  name = "mico",
  version = "0.1",
  description = "A monkey driven cloud management",
  long_description=get_file_contents("README.rst"),
  author='Andres J. Diaz',
  author_email='ajdiaz@connectical.com',
  url='http://ajdiaz.github.com/mico',
  packages=find_packages(),
  install_requires = parse_requirements('requirements.txt'),
  dependency_links = parse_dependency_links('requirements.txt'),
  entry_points={
     'console_scripts': [
         'mico = mico.scripts.cmdline:main',
     ]
  },
  classifiers=[
       'Development Status :: 4 - Beta',
       'Intended Audience :: Developers',
       'License :: OSI Approved :: GNU General Public License (GPL)',
       'Operating System :: OS Independent',
       'Programming Language :: Python',
  ],
)
Python eggs: complex setup.py


setup(
  name = "mico",
  version = "0.1",
  description = "A monkey driven cloud management",
  long_description=get_file_contents("README.rst"),
  author='Andres J. Diaz',
  author_email='ajdiaz@connectical.com',
  url='http://ajdiaz.github.com/mico',
  packages=find_packages(),
  install_requires = parse_requirements('requirements.txt'),
  dependency_links = parse_dependency_links('requirements.txt'),
  entry_points={
     'console_scripts': [
         'mico = mico.scripts.cmdline:main',
     ]
  },
  classifiers=[
       'Development Status :: 4 - Beta',
       'Intended Audience :: Developers',
       'License :: OSI Approved :: GNU General Public License (GPL)',
       'Operating System :: OS Independent',
       'Programming Language :: Python',
  ],
)
Applauses & questions
     Not necessarily in that order.

More Related Content

What's hot (20)

PDF
Workshop quality assurance for php projects - ZendCon 2013
Michelangelo van Dam
 
PDF
Py.test
soasme
 
PDF
Puppet: What _not_ to do
Puppet
 
PDF
Let's read code: python-requests library
Susan Tan
 
PPTX
Five
Łukasz Langa
 
PDF
UA testing with Selenium and PHPUnit - PFCongres 2013
Michelangelo van Dam
 
PDF
Let's read code: the python-requests library
Susan Tan
 
KEY
dotCloud and go
Flavio Poletti
 
KEY
Django’s nasal passage
Erik Rose
 
KEY
Python在豆瓣的应用
Qiangning Hong
 
PDF
Java设置环境变量
Zianed Hou
 
PDF
Voxxed Days Vilnius 2015 - Having fun with Javassist
Anton Arhipov
 
PDF
Hacking Mac OSX Cocoa API from Perl
typester
 
PDF
Pytest: escreva menos, teste mais
Erick Wilder
 
PDF
#SPUG - Legacy applications
Piotr Pasich
 
PDF
Puppet Continuous Integration with PE and GitLab
Alessandro Franceschi
 
PDF
Google I/O 2021 Recap
furusin
 
PDF
Memory Manglement in Raku
Workhorse Computing
 
PDF
Non stop random2b
phanhung20
 
PDF
EuroPython 2015 - Decorators demystified
Pablo Enfedaque
 
Workshop quality assurance for php projects - ZendCon 2013
Michelangelo van Dam
 
Py.test
soasme
 
Puppet: What _not_ to do
Puppet
 
Let's read code: python-requests library
Susan Tan
 
UA testing with Selenium and PHPUnit - PFCongres 2013
Michelangelo van Dam
 
Let's read code: the python-requests library
Susan Tan
 
dotCloud and go
Flavio Poletti
 
Django’s nasal passage
Erik Rose
 
Python在豆瓣的应用
Qiangning Hong
 
Java设置环境变量
Zianed Hou
 
Voxxed Days Vilnius 2015 - Having fun with Javassist
Anton Arhipov
 
Hacking Mac OSX Cocoa API from Perl
typester
 
Pytest: escreva menos, teste mais
Erick Wilder
 
#SPUG - Legacy applications
Piotr Pasich
 
Puppet Continuous Integration with PE and GitLab
Alessandro Franceschi
 
Google I/O 2021 Recap
furusin
 
Memory Manglement in Raku
Workhorse Computing
 
Non stop random2b
phanhung20
 
EuroPython 2015 - Decorators demystified
Pablo Enfedaque
 

Viewers also liked (20)

PDF
Mico: A monkey in the cloud
Andrés J. Díaz
 
PDF
Python speleology
Andrés J. Díaz
 
PDF
We Buy Cheese in a Cheese Shop
Tzu-ping Chung
 
PPTX
Python, Development Environment for Windows
Kwangyoun Jung
 
PDF
Python Recipes for django girls seoul
Joeun Park
 
KEY
Overview of Testing Talks at Pycon
Jacqueline Kazil
 
ODP
Authentication & Authorization in ASPdotNet MVC
Mindfire Solutions
 
ODP
Rabbitmq & Postgresql
Lucio Grenzi
 
PDF
PyClab.__init__(self)
Tzu-ping Chung
 
PDF
NoSql Day - Chiusura
WEBdeBS
 
PDF
2 × 3 = 6
Tzu-ping Chung
 
PDF
PythonBrasil[8] closing
Tatiana Al-Chueyr
 
PDF
Django - The Web framework for perfectionists with deadlines
Markus Zapke-Gründemann
 
PPT
Load testing
Mindfire Solutions
 
PDF
The Django Book, Chapter 16: django.contrib
Tzu-ping Chung
 
PDF
Django - The Web framework for perfectionists with deadlines
Markus Zapke-Gründemann
 
PPT
Django-Queryset
Mindfire Solutions
 
PDF
Bottle - Python Web Microframework
Markus Zapke-Gründemann
 
PDF
2007 - 应用系统脆弱性概论
Na Lee
 
PPT
Digesting jQuery
Mindfire Solutions
 
Mico: A monkey in the cloud
Andrés J. Díaz
 
Python speleology
Andrés J. Díaz
 
We Buy Cheese in a Cheese Shop
Tzu-ping Chung
 
Python, Development Environment for Windows
Kwangyoun Jung
 
Python Recipes for django girls seoul
Joeun Park
 
Overview of Testing Talks at Pycon
Jacqueline Kazil
 
Authentication & Authorization in ASPdotNet MVC
Mindfire Solutions
 
Rabbitmq & Postgresql
Lucio Grenzi
 
PyClab.__init__(self)
Tzu-ping Chung
 
NoSql Day - Chiusura
WEBdeBS
 
2 × 3 = 6
Tzu-ping Chung
 
PythonBrasil[8] closing
Tatiana Al-Chueyr
 
Django - The Web framework for perfectionists with deadlines
Markus Zapke-Gründemann
 
Load testing
Mindfire Solutions
 
The Django Book, Chapter 16: django.contrib
Tzu-ping Chung
 
Django - The Web framework for perfectionists with deadlines
Markus Zapke-Gründemann
 
Django-Queryset
Mindfire Solutions
 
Bottle - Python Web Microframework
Markus Zapke-Gründemann
 
2007 - 应用系统脆弱性概论
Na Lee
 
Digesting jQuery
Mindfire Solutions
 
Ad

Similar to Isolated development in python (20)

PDF
From Dev to DevOps - Codemotion ES 2012
Carlos Sanchez
 
PDF
Arbeiten mit distribute, pip und virtualenv
Markus Zapke-Gründemann
 
PDF
MT_01_unittest_python.pdf
Hans Jones
 
KEY
From Dev to DevOps - ApacheCON NA 2011
Carlos Sanchez
 
PDF
Virtualenv
WEBdeBS
 
KEY
From Dev to DevOps - FOSDEM 2012
Carlos Sanchez
 
PDF
Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013
Carlos Sanchez
 
PPTX
Django deployment best practices
Erik LaBianca
 
KEY
Puppet for Java developers - JavaZone NO 2012
Carlos Sanchez
 
PDF
Effective testing with pytest
Hector Canto
 
PDF
Test-Driven Puppet Development - PuppetConf 2014
Puppet
 
TXT
Hello click click boom
symbian_mgl
 
PDF
From Dev to DevOps
Agile Spain
 
PDF
Тестирование и Django
MoscowDjango
 
PDF
Custom deployments with sbt-native-packager
GaryCoady
 
PDF
Arbeiten mit distribute, pip und virtualenv
Markus Zapke-Gründemann
 
PPT
Python virtualenv & pip in 90 minutes
Larry Cai
 
PDF
Making the most of your Test Suite
ericholscher
 
PDF
Christian Strappazzon - Presentazione Python Milano - Codemotion Milano 2017
Codemotion
 
PDF
Burn down the silos! Helping dev and ops gel on high availability websites
Lindsay Holmwood
 
From Dev to DevOps - Codemotion ES 2012
Carlos Sanchez
 
Arbeiten mit distribute, pip und virtualenv
Markus Zapke-Gründemann
 
MT_01_unittest_python.pdf
Hans Jones
 
From Dev to DevOps - ApacheCON NA 2011
Carlos Sanchez
 
Virtualenv
WEBdeBS
 
From Dev to DevOps - FOSDEM 2012
Carlos Sanchez
 
Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013
Carlos Sanchez
 
Django deployment best practices
Erik LaBianca
 
Puppet for Java developers - JavaZone NO 2012
Carlos Sanchez
 
Effective testing with pytest
Hector Canto
 
Test-Driven Puppet Development - PuppetConf 2014
Puppet
 
Hello click click boom
symbian_mgl
 
From Dev to DevOps
Agile Spain
 
Тестирование и Django
MoscowDjango
 
Custom deployments with sbt-native-packager
GaryCoady
 
Arbeiten mit distribute, pip und virtualenv
Markus Zapke-Gründemann
 
Python virtualenv & pip in 90 minutes
Larry Cai
 
Making the most of your Test Suite
ericholscher
 
Christian Strappazzon - Presentazione Python Milano - Codemotion Milano 2017
Codemotion
 
Burn down the silos! Helping dev and ops gel on high availability websites
Lindsay Holmwood
 
Ad

Recently uploaded (20)

PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
Designing Production-Ready AI Agents
Kunal Rai
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Designing Production-Ready AI Agents
Kunal Rai
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 

Isolated development in python

  • 3. pip installer $ pip install <package>
  • 4. pip installer $ pip install <package> Download package from pypi.python.org
  • 5. pip installer $ pip install <package> Download package from pypi.python.org $ pip install <directory> $ pip install <tar.gz>
  • 6. pip installer $ pip install <gitrepo>
  • 7. pip installer $ pip install <gitrepo> GREAT!!
  • 8. pip installer $ pip install <gitrepo> GREAT!! $ pip install git+git://github.com/ajdiaz/mole $ pip install git+ssh://github.com/ajdiaz/mole $ pip install git+git://github.com/ajdiaz/mole@840d25 $ pip install git+git://github.com/ajdiaz/mole@devel-branch $ pip install git+git://....@devel-branch#egg=Mole
  • 9. pip installer $ pip freeze
  • 10. pip installer $ pip freeze Fabric==1.5.2 GitPython==0.3.2.RC1 Jinja2==2.6 Pygments==1.6 Sphinx==1.2b1 Create requirements.txt argparse==1.2.1 async==0.6.1 boto==2.7.0 cuisine==0.5.1 distribute==0.6.24 docutils==0.10 gitdb==0.5.4 mico==0 paramiko==1.9.0 pycrypto==2.6 smmap==0.8.2 wsgiref==0.1.2
  • 11. 2. Virtualenv: a jail for python
  • 12. Virtualenv: the python jail $ virtualenv --python=/usr/bin/python2.7 mynewenvironment
  • 13. Virtualenv: the python jail $ virtualenv --python=/usr/bin/python2.7 mynewenvironment OR EVEN BETTER
  • 14. Virtualenv: the python jail $ virtualenv --python=/usr/bin/python2.7 mynewenvironment OR EVEN BETTER $ mkvirtualenv --python=/usr/bin/python2.7 mynewenvironment
  • 15. Virtualenv: the python jail $ virtualenv --python=/usr/bin/python2.7 mynewenvironment OR EVEN BETTER $ mkvirtualenv --python=/usr/bin/python2.7 mynewenvironment mkvirtualenvwrapper
  • 17. Virtualenvwrapper $ mkvirtualenv test New python executable in test/bin/python Installing distribute...................................................................................................................... .......................................................................done. Installing pip...............done. virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/predeactivate virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postdeactivate virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/preactivate virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postactivate virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/get_env_details (test) $
  • 18. Virtualenvwrapper $ mkvirtualenv test New python executable in test/bin/python Installing distribute...................................................................................................................... .......................................................................done. Installing pip...............done. virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/predeactivate virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postdeactivate virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/preactivate virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postactivate virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/get_env_details (test) $ which python /home/ajdiaz/env/test/bin/python
  • 19. Virtualenvwrapper $ mkvirtualenv test New python executable in test/bin/python Installing distribute...................................................................................................................... .......................................................................done. Installing pip...............done. virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/predeactivate virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postdeactivate virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/preactivate virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postactivate virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/get_env_details (test) $ which python /home/ajdiaz/env/test/bin/python yep, these are hooks!
  • 21. Unit testing: nose class A(object): def __init__(self): self.value = "Some Value" def return_true(self): return True save it in a.py def raise_exc(self, val): raise KeyError(val)
  • 22. Unit testing: nose from a import A from nose.tools import assert_equal from nose.tools import assert_not_equal from nose.tools import assert_raises from nose.tools import raises class TestA(object): @classmethod def setup_class(klass): """This method is run once for each class before any tests are run""" @classmethod def teardown_class(klass): """This method is run once for each class _after_ all tests are run""" def setUp(self): """This method is run once before _each_ test method is executed""" def teardown(self): """This method is run once after _each_ test method is executed""" .... continue ....
  • 23. Unit testing: nose def test_init(self): a = A() assert_equal(a.value, "Some Value") assert_not_equal(a.value, "Incorrect Value") def test_return_true(self): a = A() assert_equal(a.return_true(), True) assert_not_equal(a.return_true(), False) def test_raise_exc(self): a = A() assert_raises(KeyError, a.raise_exc, "A value") @raises(KeyError) def test_raise_exc_with_decorator(self): a = A() a.raise_exc("A message") save it in tests/testa.py
  • 24. Unit testing: nose $ nosetests -v tests/
  • 25. Unit testing: nose $ nosetests -v tests/ testa.TestA.test_init ... ok testa.TestA.test_raise_exc ... ok testa.TestA.test_raise_exc_with_decorator ... ok testa.TestA.test_return_true ... ok --------------------------------------------------------- Ran 4 tests in 0.002s OK
  • 26. Unit testing: Bonus: code coverage $ pip install coverage $ nosetests --with-coverage .... Name Stmts Miss Cover Missing ------------------------------------- a 8 0 100% ------------------------------------- Ran 4 tests in 0.006s OK
  • 28. Python eggs: basic setup.py from setuptools import setup setup( name = "example", version = "1.0", description = "An example package", author='Andres J. Diaz' )
  • 29. Python eggs: basic setup.py from setuptools import setup, find_packages setup( name = "example", version = "1.0", description = "An example package", author='Andres J. Diaz', packages=find_packages() )
  • 30. Python eggs: complex setup.py import re from setuptools import setup, find_packages from os import path def parse_requirements(file_name): requirements = [] for line in open(file_name, 'r').read().split('n'): if re.match(r'(s*#)|(s*$)', line): continue if re.match(r's*-es+', line): requirements.append(re.sub(r's*-es+.*#egg=(.*)$', r'1', line)) elif re.match(r's*-fs+', line): pass else: requirements.append(line) return requirements .... continue ....
  • 31. Python eggs: complex setup.py def parse_dependency_links(file_name): dependency_links = [] for line in open(file_name, 'r').read().split('n'): if re.match(r's*-[ef]s+', line): dependency_links.append(re.sub(r's*-[ef]s+', '', line)) return dependency_links def get_file_contents(filename): fd = file(path.join(path.dirname(__file__), filename), "r") content = fd.read() fd.close() return content .... continue ....
  • 32. Python eggs: complex setup.py setup( name = "mico", version = "0.1", description = "A monkey driven cloud management", long_description=get_file_contents("README.rst"), author='Andres J. Diaz', author_email='[email protected]', url='http://ajdiaz.github.com/mico', packages=find_packages(), install_requires = parse_requirements('requirements.txt'), dependency_links = parse_dependency_links('requirements.txt'), entry_points={ 'console_scripts': [ 'mico = mico.scripts.cmdline:main', ] }, classifiers=[ 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'License :: OSI Approved :: GNU General Public License (GPL)', 'Operating System :: OS Independent', 'Programming Language :: Python', ], )
  • 33. Python eggs: complex setup.py setup( name = "mico", version = "0.1", description = "A monkey driven cloud management", long_description=get_file_contents("README.rst"), author='Andres J. Diaz', author_email='[email protected]', url='http://ajdiaz.github.com/mico', packages=find_packages(), install_requires = parse_requirements('requirements.txt'), dependency_links = parse_dependency_links('requirements.txt'), entry_points={ 'console_scripts': [ 'mico = mico.scripts.cmdline:main', ] }, classifiers=[ 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'License :: OSI Approved :: GNU General Public License (GPL)', 'Operating System :: OS Independent', 'Programming Language :: Python', ], )
  • 34. Applauses & questions Not necessarily in that order.