Wednesday, March 25, 2015

source code of reading json configuration file from command line in python

In many application , we want to read a file as a configuration file from command line . here I have written a source code  to read json configuration file in python


source code:
#!/usr/bin/env python

from optparse import OptionParser
import re
import json
import collections

def utf8(data):
    """Encodes the given text as utf-8.
    Returns a string of bytes.
    """
    if isinstance(data, str):
       
        return data
    elif isinstance(data, unicode):
        
        return data.encode("utf-8")   

    return _modify_children(data, utf8)

def _modify_children(data, operation):
    if isinstance(data, collections.Mapping):
  
        return dict(map(operation, data.iteritems()))
    elif isinstance(data, collections.Iterable):
        
        return type(data)(map(operation, data))
    else:
        
        return data

     


def main():
    parser = OptionParser(usage="usage: %prog [options] filename",
                          version="%prog 1.0")
 
    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.error("wrong number of arguments")


    with open(args[0]) as f:
      data = re.compile("^\s*//.*$", re.MULTILINE).sub("", f.read())
    

    print "**** DATA  *********"
    print data

    


    valid_data = json.loads(data) 

   
   
    # print valid_data 

    # print valid_data["jobs"][0]
    # print valid_data["jobs"][1]

    new_data = utf8(valid_data)




    print "********* unicode encoded data *********" 

    print new_data





if __name__ == '__main__':
    
    main()
here I have used python library optparse to parse the command line argument in our python code. This library helps to read the configuration file mentioned in command line. the json .loads is used to decode the json data to unicode then is again encoded to the python string. To encode the json file , the utf8 function is created. this utf8 function Encodes the given text as utf-8 and returns a string of bytes.

python source code to Convert dictionary / list of strings from Unicode to python string

while using a unicode, we can come to the situation that we have data in unicode and are supposed to convert in ASCII. Here I have written a simple python code to convert dictionary from unicode to ASCII.

in using unicode, if we have simple unicode data as a= u"hello". Then we can easily use a.encode('utf-8') and we can get the desired result in python string. However if we have data in dictionary or tuple or list then in such case we cannot use encode as before .

Source Code:


#!/usr/bin/env python

import json

def convert(input):
    if isinstance(input, dict):
        return {convert(key): convert(value) for key, value in input.iteritems()}
    elif isinstance(input, list):
        return [convert(element) for element in input]
    elif isinstance(input, unicode):
        return input.encode('utf-8')
    else:
        return input



data={
    "books": [
        {
            "id": "1",
            "name": "Mathematics",
            "type": "Engineering",
            
            "run_at": {"day": "*/1", "second": 5},
            "start_date": "2011-12-21 17:00:23",


         
            "interval_type": "day",
            "timezone": "Asia/Kathmandu"
        },
        {
            "id": "124",
            "name": "Data Mining",
            "type": "Engineering",
            "run_at": {"second": 0},

           
            "interval_type": "day",
            "timezone": "Asia/Kathmandu"
        }
    ]
}

test = json.dumps(data)
valid_data = json.loads(test)
final_data = convert(valid_data)
print final_data




data1= [ { 'a':'A', 'b':(2, 4), 'c':3.0 } ]
test1 = json.dumps(data1)

valid_data1 = json.loads(test1)

final_data1 = convert(valid_data1)

print final_data1


here I have a dictionary and tuple data type and first I encoded to unicode by using json.load . Later I have writted a convert function which takes a unicode dictionary data then it encodes in to the python string.

Monday, March 23, 2015

Installation of CMake and using cmake for simple C program for creating of makefile

Installation of makefile and  using cmake for simple C program for  creating of makefile

Makefile is a file which tells how to compile and link a program. The instruction of compiling a source file , including library file and linking the file to produce the final binary file is written in Makefile. In Linux based system , If we are using an IDE then makefile is created by IDE. However if we are not using IDE and simply compiling and linking through command line, in such case we need to create MakeFile. CMake is a tool used to easy generation of cross platform Makefile.


here I have presented the use of Cmake to create a MakeFile in simple ways. The following procedure are tested in Linux/Mac OS.

1) Installation of CMake:

first install CMake. It is available in all the platforms. In mac, linux Cmake-gui is also available with GUI feature available. Be sure that command line feature is also installed along with GUI in linux and Mac OS.

Just type cmake to ensure that cmake command line is available in linux and mac.


2) creation of simple C project file  named cmake.c

 first let us create a simple C file as
#include <stdio.h>



int main()


printf("this  is the test file");


2) create CMakeLists.txt along with source file

After creation of source file , next we need a CMakeLists.txt file. This file is used by the Cmake to create a Makefile.

cmake_minimum_required (VERSION 2.6)

set(TARGET learn)

set(SOURCE cmake.c)



add_executable(${TARGET} ${SOURCE})

message("Success")




here we set learn as TARGET and cmake.c as SOURCE  and we want our makefile to create a executable file named as learn .

3) Generation of Makefile

first create a new folder and name it "build" so that all the file generated by Cmake are placed here. This new folder is not a compulsary one but is created to manage the files.

This makefile can be generated in two ways . Either by cmake-gui or command line

a) using GUI

 first open the gui of cmake . then there is input fields as "where is the source code"
 and where ti build the binaries. Just browse to the source code folder in the first input field and browse to the recently created build folder in the later input box.

 then finally press the Configure and Generate button of the GUI.

 then after this makefile is created in the build directory. make sure to see it.




 b) using command line

 first move the the build directory.
 then  input the command
   cmake ..

   this will create the makefile in the build folder.



4) running of MakeFile recently created

   then move to the folder build as MakeFile created is placed here.

   run the command

    make

    this will create the binary file learn as we have named our binary to be learn in earlier CMakeListes.txt at the start.

    5) so run the executable as

      finally move to the folder inside build where learn executables is created and run the command

      ./learn

      wow we get the output.