Friday, 29 April 2016

Python code to read and write array to file in json format

First install and import 'json' module

import json

Reading JSON object from file:

def read_json_from_file(json_file):
    Read array of objects from a file in json format.
    :param json_file: input json file name
    :return: array of objects from the json
    with open(json_file, 'r') as f:
        exp_string = json.load(f)
        json_array = json.loads(exp_string)
    return json_array

Writing JSON to the file:

def write_json_to_file(array, out_file_name):
    Dump an array to file in json format.
    :param array: Array to write to the file
    :param out_file_name: output file name
    with open(out_file_name, 'w') as out_f:
        s = json.dumps([o.__dict__ for o in array], sort_keys=True)
        json.dump(s, out_f)

Get recursive file listing in python

Below is the python code:

def get_recursive_filelist(rootdir):
    outfiles = []
    if os.path.isfile(rootdir):
    for (dir, _, files) in os.walk(rootdir):
        for f in files:
            path = os.path.join(dir, f)
            if os.path.exists(path):
    return outfiles

Easy way to check internet speed using command line speedtest tool

Here is the light weight command line speedtest tool to check the internet speed.

$ wget -O speedtest-cli
$ chmod +x speedtest-cli
$ ./speedtest-cli
Retrieving configuration...
Retrieving server list...
Testing from Comcast Cable (x.x.x.x)...
Selecting best server based on ping...
Hosted by FiberCloud, Inc (Seattle, WA) [12.03 km]: 44.028 ms
Testing download speed........................................
Download: 32.29 Mbit/s
Testing upload speed..................................................
Upload: 5.18 Mbit/s

Easy way to parse command line arguments in python using argparse

Here is the easiest way to parse command line arguments in python.

import argparse

args = argparse.ArgumentParser(description='Description of the script.')
args.add_argument('-i', '--input', help='Input file name', required=True)
args.add_argument('-o', '--output', help='Output file', required=True)
args = args.parse_args()

input = args.input
output = args.output

print "Input:" + input
print "Ouput:" + output

Rahuls-MacBook-Pro:exception rahuldiyewar$ python -h
usage: [-h] -i INPUT -o OUTPUT

Description of the script.

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT, --input INPUT
                        Input file name
  -o OUTPUT, --output OUTPUT
                        Output file

Rahuls-MacBook-Pro:exception rahuldiyewar$ python -i myinput -o myoutput 

Python code to search and create bug in JIRA using rest api

Here is the Python code to search/open issues in JIRA:
  1. Search for open issues
  2. Create new issue

from jira import JIRA

class JIRARestAPI:
    url = 'https://<company-name>'    
    username = '<username>'    
    password = '<password>'    
    jira = JIRA(url, basic_auth=(username, password))

    def search_issues(self, search_string):
        return JIRARestAPI.jira.search_issues(search_string)

    def print_bugs(self, bugs):
        for bug in bugs:

    def print_bug(self, bug):
        print ("%s, %s, \"%s\"" % (bug.key,, bug.fields.summary))

    def create_cyg_bug(self, summary, description, label):
        issue_dict = {
            'project': {'id': 10000},
            'summary': summary,
            'description': description,
            'issuetype': {'name': 'Bug'},
            'labels': [label]
        return JIRARestAPI.jira.create_issue(fields=issue_dict)