Python tips and tricks

Hi all,

Being a DevOps means you are an Operator which originate from Developer ... it's my definition, so call, an Automation Build Administrator. So you need a bit background on development, coding.

I had some touches on Python with my previous job but a very simple task on Web Logic which has the python environment on wlst, which is called Jython. So I knew Python as an on-demand language to fix a bug. Working with my current task, I have to write some shared libraries for other teams then Python script is my only choice, so I have to dig more detail into Python coding now.

So today is simple, some tips and tricks when using Python language.

First thing is some basic conventions from Python. As I focus on Python3 so most of my comments are applied for Python3

  • Indentation should use Tab instead of space (in case you have IDE)
  • Use math operator, comma at new line so it's easier to read also delete a line
  • Import in separated lines, avoid wildcard import
  • "Dunder" name for module level
  • String quote: single quote for fixed data, interrogate  string with double quotes
  • Document string with triple quotes
  • Focus on the naming convention for the variable names
  • Use try exception with this guide
Next item is to package the module, read through this thread to have an idea about module vs class. Package is like its name, it's a group of modules or classes. To import your module, just import the module file, for the class import, from <module_file> import <class>

To write a unit test, you can use many provided framework, however, as I have a task on Sonarqube so I will use nose framework. To have unit test and code coverage, you will install nose and coverage with pip, then you are good to go. 

Below are the sample code that you can image how to implement them

mymodule.py

def encrypt(key, secret):
    ''' Implement encryption method
    Usage: key is the hash key, secret is the encrypt data'''
    return <sth>

def decrypt(key, secret):

    ''' Implement decryption method
    Usage: key is the hash key, secret is the decrypt data'''
    return <sth>
========================
myclass.py

import requests
import mymodule
class MyClass:
    __glob_const__ = 'global property'

    def __init__(self):
        self.cls_prop = 'initial data'

    def login(self, user, password):
        response = requests.get(url, auth=(user, password))
        if response.status_code == 200:
            token = mymodule.encypt(response.json()['apikey']
        else:
            raise Exception('Login failed!')
        return token
========================
myclass_test.py

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

from myclass import MyClass

class TestMyClass(object):
    @classmethod
    def setupAll(self): #setup fixture
        self.test_data1 = 'test data seed'
        self.test_data2 = 'test data seed'
        self.cls_inst = MyClass()

    def test_login_pass(self):
        actual = self.cls_inst.login(user, password)
        expected = self.test_data1
        assert_equal(actual, expected)



    @raises(Exception)
    def test_login_fail(self):
        self.cls_inst.login(user, password)

To run the unit test only you can call

nosetests myclass_test.py

for more detail which test cases are failed

nosetests -s --verbosity=2 myclass_test.py

To generate the coverage report

nosetests --with-coverage --cover-erase --cover-package=myclass --cover-html myclass_test.py

To create unit test report then add option --with-xunit

nosetests --with-xunit --with-coverage --cover-erase --cover-package=myclass --cover-html myclass_test.py

If you want to convert the coverage report into xml format

coverage xml

Some notes when using Ubuntu with Python
  • Want to check if python module is installed, you can use: pip3 freeze or python3 -c "import <module>"; echo $?
  • If your environment has multiple python versions (for ex: python3.5 and python3.6). Make sure to use the proper pip to install your module by using your proper python to install, for ex: python3.6 -m pip install nose
Hope you will find this helpful!

Comments