Something about Python

Something about Python


Programming on Mac OS

First, you need to install XCode and Homebrew

# install xcode
xcode-select --install

# install homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Install python, python3 via homebrew

brew install python python3

Install virtual env to isolate python workspace with os

pip install virtualenv

Create virtual environment

cd my_python_project_home
virtualenv venv

Start using virtual environment

source .venv/bin/activate

Stop virtual environment

deactivate

Packaging Python project

If you want to share your python project to others, you need to create a module dependency list (says requirement.txt) to tell others what modules you use.

Here is a module can auto generate requirement.txt by scanning project called https://github.com/damnever/pigar

You can simply install:

pip install pigar

and run command under project folder

pigar -i .venv/

-i .venv/ parameter means ignore virtualenv folder when scanning.

Unit Test

create a tests folder under project home, and write test code with name testXXX. here is an example:

# testHelloworld.py

import unittest
from hello import helloworld


class HelloWorldTestCase(unittest.TestCase):

    def setUp(self):
        self.hello = helloworld.HelloWorld()

    def test_sayhello(self):
        self.assertEquals('Hello nobody', self.hello.sayHello())

    def test_sayHelloWithArgs(self):
        self.assertEquals('Hello something', self.hello.sayHello('something'))

Use nose to simply test command:

pip install nose

Under project home, run:

nosetests

and the result should like:

(.venv) chenbiweide-MacBook-Air:helloworld WilliamChen$ nosetests
..
----------------------------------------------------------------------
Ran 2 tests in 0.010s

OK

results for ""

    No results matching ""