Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Isolated development in python

749 views

Published on

How to develop in python safely using isolated environments, create packages and unit testing with code coverage.

Published in: Technology
  • Be the first to comment

Isolated development in python

  1. 1. Isolated Development in Python
  2. 2. 1. pip installer
  3. 3. pip installer $ pip install <package>
  4. 4. pip installer $ pip install <package> Download package from pypi.python.org
  5. 5. pip installer $ pip install <package> Download package from pypi.python.org $ pip install <directory> $ pip install <tar.gz>
  6. 6. pip installer $ pip install <gitrepo>
  7. 7. pip installer $ pip install <gitrepo> GREAT!!
  8. 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. 9. pip installer $ pip freeze
  10. 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. 11. 2. Virtualenv: a jail for python
  12. 12. Virtualenv: the python jail $ virtualenv --python=/usr/bin/python2.7 mynewenvironment
  13. 13. Virtualenv: the python jail $ virtualenv --python=/usr/bin/python2.7 mynewenvironment OR EVEN BETTER
  14. 14. Virtualenv: the python jail $ virtualenv --python=/usr/bin/python2.7 mynewenvironment OR EVEN BETTER $ mkvirtualenv --python=/usr/bin/python2.7 mynewenvironment
  15. 15. Virtualenv: the python jail $ virtualenv --python=/usr/bin/python2.7 mynewenvironment OR EVEN BETTER $ mkvirtualenv --python=/usr/bin/python2.7 mynewenvironmentmkvirtualenvwrapper
  16. 16. Virtualenvwrapper$ mkvirtualenv test
  17. 17. Virtualenvwrapper$ mkvirtualenv testNew python executable in test/bin/pythonInstallingdistribute.............................................................................................................................................................................................done.Installing pip...............done.virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/predeactivatevirtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postdeactivatevirtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/preactivatevirtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postactivatevirtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/get_env_details(test) $
  18. 18. Virtualenvwrapper$ mkvirtualenv testNew python executable in test/bin/pythonInstallingdistribute.............................................................................................................................................................................................done.Installing pip...............done.virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/predeactivatevirtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postdeactivatevirtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/preactivatevirtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postactivatevirtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/get_env_details(test) $ which python/home/ajdiaz/env/test/bin/python
  19. 19. Virtualenvwrapper$ mkvirtualenv testNew python executable in test/bin/pythonInstallingdistribute.............................................................................................................................................................................................done.Installing pip...............done.virtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/predeactivatevirtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postdeactivatevirtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/preactivatevirtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/postactivatevirtualenvwrapper.user_scripts creating /home/ajdiaz/env/test/bin/get_env_details(test) $ which python/home/ajdiaz/env/test/bin/python yep, these are hooks!
  20. 20. Unit testing
  21. 21. Unit testing: noseclass 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. 22. Unit testing: nosefrom a import Afrom nose.tools import assert_equalfrom nose.tools import assert_not_equalfrom nose.tools import assert_raisesfrom nose.tools import raisesclass 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. 23. Unit testing: nosedef 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. 24. Unit testing: nose$ nosetests -v tests/
  25. 25. Unit testing: nose$ nosetests -v tests/testa.TestA.test_init ... oktesta.TestA.test_raise_exc ... oktesta.TestA.test_raise_exc_with_decorator ... oktesta.TestA.test_return_true ... ok---------------------------------------------------------Ran 4 tests in 0.002sOK
  26. 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
  27. 27. Packaging Python Eggs
  28. 28. Python eggs: basic setup.py from setuptools import setupsetup( name = "example", version = "1.0", description = "An example package", author=Andres J. Diaz)
  29. 29. Python eggs: basic setup.pyfrom setuptools import setup, find_packagessetup( name = "example", version = "1.0", description = "An example package", author=Andres J. Diaz, packages=find_packages())
  30. 30. Python eggs: complex setup.pyimport refrom setuptools import setup, find_packagesfrom os import pathdef 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(rs*-es+, line): requirements.append(re.sub(rs*-es+.*#egg=(.*)$, r1, line)) elif re.match(rs*-fs+, line): pass else: requirements.append(line) return requirements .... continue ....
  31. 31. Python eggs: complex setup.pydef parse_dependency_links(file_name): dependency_links = [] for line in open(file_name, r).read().split(n): if re.match(rs*-[ef]s+, line): dependency_links.append(re.sub(rs*-[ef]s+, , line)) return dependency_linksdef get_file_contents(filename): fd = file(path.join(path.dirname(__file__), filename), "r") content = fd.read() fd.close() return content .... continue ....
  32. 32. Python eggs: complex setup.pysetup( 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, ],)
  33. 33. Python eggs: complex setup.pysetup( 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, ],)
  34. 34. Applauses & questions Not necessarily in that order.

×