Search

Coder's notes

For your infomation

Category

Python

Python related posts

Using Python unittest and coverage (a quick and dirty tutorial)

Follow this tutorial here to create a unittest

https://docs.python.org/3/library/unittest.html#basic-example

Assuming that you have a folder like

mypackage\
    uberlib.py
    models.py
test\
    test_uberlib.py
    test_models.py
main.py

You can run both tests by using

python3 -m unittest discover -s test

or a specific test

python3 -m unittest test.test_uberlib

To use coverage, first you need to install it (using pip)

pip install coverage

And then type

coverage run --source mypackage --branch -m unittest discover -s test

Note: --source mypackage means only the source code in mypackage folder will be covered, --branch is for measuring branch coverage. Read more here.

To see coverage report, use:

coverage report

If you fancy HTML based report, use this and a report will be generated into a folder named htmlcov in your project folder. Open index.html inside the folder to see the report

coverage html

That’s it. It’s pretty cool eh?

Setting up Python development environment without superuser permissions

  1. Compile Python from source
    1. Download Python source code from python.org. E.g.:
      wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz
      Unpack Python and compile it to our local folder
      tar -xvf Python-3.4.3.tgz
      cd Python-3.4.3
      ./configure --prefix=/home/user/bin/python --enable-unicode=utf8
      make && make install
  2. Install virtualenv locally
    1. Now you should have this folder ~/bin/python. Execute this to make sure Python have been installed properly
      ~/bin/python/bin/python --version
    2. Install virtualenv using local pip version (If you use Python 2.x, change pip to pip2
      ~/bin/python/bin/pip3 install virtualenv
    3. Create a virtualenv (e.g. ~/myp3)
      ~/bin/python/bin/virtualenv -p ~/bin/python/bin/python myp3
    4. Activate the virtualenv (make sure you have a space between the dot and the tilde.
      . ~/myp3/bin/activate
    5. That’s it. Now you should be able to build CPython packages. For example, try to install mecab-python3 which requires to have access to python.h
      pip3 install mecab-python3
  3. Lorem ipsum!

Using Emacs for Python development

  1. Install Emacs version 24+
  2. Install elpy by following the guideline here https://github.com/jorgenschaefer/elpy
  3. Use only white space => Add this to ~/.emacs
    (add-hook 'python-mode-hook
    (lambda ()
    (setq-default indent-tabs-mode nil)
    (setq-default tab-width 4)
    (setq-default python-indent 4)))
  4. Convert tab to space by pressing (Meta = Alt key or ESC key) Meta+x, type untabify then press Enter.

Install Python3 and NLTK on Windows 8 using pip

  • Download Python 3 from here
    https://www.python.org/downloads/
  • Open command prompt and run
    c:\Python34\Scripts\pip3 install nltk
  • Then start Python 3.4 by running
    c:\Python34\python
    Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (In
    tel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> nltk.download()
  • Now you are ready to work with NLTK.

Create Excel File with Python

Install Xlsx

$ git clone https://github.com/jmcnamara/XlsxWriter.git
$ cd XlsxWriter
$ sudo python setup.py install

Code:

import xlsxwriter

workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()

worksheet.write('A1', 'Hello world')

workbook.close()

Python Development Tools

Learning Python

Running Python within the browser

Create a free website or blog at WordPress.com.

Up ↑