Search This Blog

Tuesday, December 31, 2013

Writing CRON jobs on a Mac

Mac OSX is essentially a UNIX distribution, so we have all the cool under-the-bonnet tricks and tools of any operating system with a UNIX backbone. One of the utilities I’ve always found useful in a UNIX OS is the CRON tab:

“Cron is a time-based job scheduler in Unix-like computer operating systems. Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.”

To set up a new Cron job, open your Terminal and type:
sudo crontab -e

You’ll be prompted for your password, and upon correct submission, will open a blank VI window.

The basic format of a Cron is:
0 0 * * * sh /directory/script.sh
    1.    The first 0 denotes minutes past the hour (0 is on the hour)
    2.    The second 0 denotes the hour of the day (0 is 12:00am)
    3.    The first * denotes day of the week (* means every day, 1 is Monday etc)
    4.    The second * is the week of the month (* means every week, 1 is the first week of the month)
    5.    The final * is the month of the year (* means every month, 1 is the first month of the year)

We specify the type of script we wish to execute; in the example above, we’re executing a basic shell script, and the final parameter details the absolute location of the script you wish to execute at regular intervals.

I often execute daily PHP scripts on my server, an example of a PHP script (backup.php) running every day at 6pm is:
0 18 * * * php ~/site/cleanup.php

Note the script type is now php, as we’re executing a php script, not a shell script.
Finally, let’s write a script that backs up my ssh keys from ~/.ssh to my Dropbox directory, once a week, on a Monday at 9:30am.
Using Terminal.app, open a new file for editing anywhere on your system (I use vi).
sudo vi ~/src/backup.sh

Copy and paste the script below, write to backup.sh and exit vi.
#!/bin/bash
sudo rsync -av --progress --delete --log-file=/Users/*yourmachine*/logs/$(date +%Y%m%d)_rsync.log /Users/*yourmachine*/.ssh /Users/*yourmachine*/Dropbox

The script uses rsync to backup the directory at “/Users/*yourmachine*/.ssh” to your Dropbox directory at “/Users/*yourmachine*/Dropbox”. It’ll also log the backup in a logs directory in your home directory. Now for the Cron.

30 9 1 * * sh ~/src/backup.sh

Hopefully this will get you started with using Cron on your OSX system.

Sunday, December 29, 2013

Some Generic Functions

#################### Start of the Program

#!/usr/bin/python

import csv
import xlwt
import xlrd
import sys
import urllib2
import json

1) Convert csv file from excel file
excel_file -- Excel file name which you want to convert
sheet_name -- Name of the sheet mention in Excel file which you want to convert
csv_file -- Csv filename for which you are converting from excel file.

This is generic funtion to convert data from excel file to csv file which you can use for wider purpose and no matters about amount of data.

def csv_from_excel(excel_file,sheet_name,csv_file):
    wb = xlrd.open_workbook(excel_file)
    sh = wb.sheet_by_name(sheet_name)
    your_csv_file = open(csv_file, 'wb')
    wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
    for rownum in xrange(sh.nrows):
        wr.writerow(sh.row_values(rownum))
    your_csv_file.close()

2) Code to change excel font in excel file 
clr_index -- This is generic color indexex to change color font

def excel_font(clr_index):
    font0 = xlwt.Font()
    font0.name = 'Times New Roman'
    font0.colour_index = clr_index
    font0.height=240
    font0.bold = True
    style0 = xlwt.XFStyle()
    style0.font = font0
    return style0

3) Code for bold font
def bold_font():
    style = xlwt.easyxf('font: bold 1, height 240')
    return style

4) Code for font height
def font_height():
    style = xlwt.easyxf('font: height 240')
    return style

5) Code to get column parameters from excel file
col_index -- Column Index from which you want to get parameters given in excel file. 

def get_column_params(excel_file,sheet_index,col_index):
    workbook = xlrd.open_workbook(excel_file)
    sheet = workbook.sheet_by_index(sheet_index)
    data = sheet.col_values(col_index,start_rowx=1)
    return data

6) Convert values from csv file to dictionary (data structure in python)
def csv_to_dict_params(csv_file,excel_file,len_of_sheets):
    d_parameters={}
    data=get_column_params(excel_file,len_of_sheets,1)
    column_len=len(data)
    with open(csv_file, 'rb') as f:
        rows = list(csv.reader(f))
        while column_len !=0:
            d_parameters[rows[column_len][1]]=rows[column_len][2]
            column_len=column_len - 1
    return d_parameters

7) Convert values from coming json to dictionary (as mentioned earlier,  (key,value) pair)
request_url -- url which returning json

def json_to_dict(request_url):
    req=urllib2.Request(request_url)
    response=urllib2.urlopen(req)
    full_resp=response.read()
    json_dict={}
    json_list=json.loads(full_resp)
    if type(json_list) is list and len(json_list)>0:
        temp_dict=json_list[0]
        for k,v in temp_dict.items():
            if type(v) is not dict:
                json_dict[k]=v
            else:
                for d_k,d_v in v.items():
                    json_dict[d_k]=d_v
    elif type(json_list) is dict and len(json_list)>0:
        for k,v in json_list.items():
            if type(v) is not list:
                json_dict[k]=v
            elif len(v)>0:
                json_dict[k]=""
                temp_dict=v[0]
                if type(temp_dict) is dict:
                    for k,v in temp_dict.items():
                        json_dict[k]=v
    return json_dict

########################## End of the Program

Examples Reading Excel (.xls) Documents Using Python's xlrd

If running Ubuntu, you can install Python's xlrd module by running the command:
I suppose you have already installed python in your system.
If xlrd module is not installed in your system.
you can easily install by command :
sudo easy_install xlrd

Open an Excel workbook:
import xlrd
workbook = xlrd.open_workbook('my_workbook.xls')

Grab list of worksheets in a workbook:
import xlrd
workbook = xlrd.open_workbook('my_workbook.xls')
print workbook.sheet_names()

Grab a specific worksheet from a workbook:
import xlrd
workbook = xlrd.open_workbook('my_workbook.xls')
worksheet = workbook.sheet_by_name('Sheet1')

Iterate over each worksheet in a workbook:
import xlrd
workbook = xlrd.open_workbook('my_workbook.xls')
worksheets = workbook.sheet_names()
for worksheet_name in worksheets:
    worksheet = workbook.sheet_by_name(worksheet_name)
    print worksheet

Iterate over each row of a worksheet:
import xlrd
workbook = xlrd.open_workbook('my_workbook.xls')
worksheet = workbook.sheet_by_name('Sheet1')
num_rows = worksheet.nrows - 1
curr_row = -1
while curr_row < num_rows:
    curr_row += 1
    row = worksheet.row(curr_row)
    print row

Grab the cell contents of each row of a worksheet:
import xlrd
workbook = xlrd.open_workbook('my_workbook.xls')
worksheet = workbook.sheet_by_name('Sheet1')
num_rows = worksheet.nrows - 1
num_cells = worksheet.ncols - 1
curr_row = -1
while curr_row < num_rows:
    curr_row += 1
    row = worksheet.row(curr_row)
    print 'Row:', curr_row
    curr_cell = -1
    while curr_cell < num_cells:
        curr_cell += 1
        # Cell Types: 0=Empty, 1=Text, 2=Number, 3=Date, 4=Boolean, 5=Error, 6=Blank
        cell_type = worksheet.cell_type(curr_row, curr_cell)
        cell_value = worksheet.cell_value(curr_row, curr_cell)
        print '    ', cell_type, ':', cell_value

Numbers guess game in python

Steps to do :
First enter your name and guess number between 1 to 20.
You have three chances to guess the number.
If your guess is right, you win the game, else you lose.

You can play many times. Numbers are randomly generated.

################### Start of the Program

#!/usr/bin/python

import random

guesses_made = 0

name = raw_input('Hello! What is your name?\n')

number = random.randint(1, 20)
print 'Well, {0}, I am thinking of a number between 1 and 20.'.format(name)

while guesses_made < 3:

    guess = int(raw_input('Take a guess: '))

    guesses_made += 1

    if guess < number:
        print 'Your guess is too low.'

    if guess > number:
        print 'Your guess is too high.'

    if guess == number:
        break

if guess == number:
    print 'Good job, {0}! You guessed my number in {1} guesses!'.format(name, guesses_made)
else:
    print 'Nope. The number I was thinking of was {0}'.format(number)

#################### End of the Program

Saturday, December 28, 2013

MVC Architecture (Model Viewer Controller)

Model Viewer Controller (MVC) is a software architecture,currently considered an architectural pattern used in software engineering. The pattern isolates "domain logic" (the application logic for the user) from input and presentation (UI), permitting independent development, testing and maintenance of each.For instance, you may refer to "controller" as "input", "model" as "processor" or "processing" and "view" as "output".

So in other words, controller receives input passes it to the model for processing, or to the view for output. SO MVC benefits include :

1. Isolation of Business Logic from the user interface.

2. Ease of keeping code DRY.

3. Making it clear where different types of ode belong for easier maintenance.

Lets explain each in detail mentioning each functionalities.

1) MODEL 
A model represents the information (data) of the application and the rules to manipulate that data.Models are primarily used for managing the rules of interaction with a corresponding database table. In most cases, one table in your database will correspond to one model in your application. The bulk of your application’s business logic will be concentrated in the models.

2) VIEW
The view renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes. A viewport typically has a one to one correspondence with a display surface and knows how to render to it.

3) CONTROLLER
The controller receives input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and viewport to perform actions based on that input.

So the steps involved in the working of MVC is as follows :
  •  The browser makes a request, such as http://learnwithnitin.blogspot.in/2013/12/powerful-vi-editor.html
  • Webserver receives the request. It uses routes to find out which controller to use. The webserver then uses the dispatcher to create a new controller, call the action and passes the parameters.
  • Controller do the work of parsing user-requests, data submissions, cookies, sessions and the browser stuff. Controller asks the model to get data from database and will eventually display it to user.
  • Models are classes. They talk to the database, store and validate data, perform the business logic. It retrieves data from the database.
  • Views are what the user sees : HTML, CSS, XML, Javascript, JSON.Views are merelu puppets reading what the controller gives them. They don't know what exactly happening in the background.
  • The controller returns the response body (HTML, XML etc) and metadata (caching headers, redirects) to the server. The server combines the raw data into proper HTTP response and sends it to the user.

Friday, December 27, 2013

Powerful VI Editor

The vi editor (short for visual editor) is a screen editor which is available on almost all Unix systems. Once you have learned vi, you will find that it is a fast and powerful editor. vi has no menus but instead uses combinations of keystrokes in order to accomplish commands.

Here are some powerful commands which helps to use vi editor in daily basis:

Note : Using "+" here to indicate that we have to press both the buttons to perform action.

1. Cursor Navigation

j --  next line

k -- previous line

h -- left

l -- right

shift + g -- bottom of document

g + g -- top of document

2. Fast Search

/ -- Search Term

n -- next

N -- previous

3. Command Mode 

: -- enter command mode

:w -- write

:q -- quit  

:e -- open

Esc -- escape command mode

4. From normal to Edit mode

i -- Insert at current position

I -- insert at start of line

A -- append to end of line

o -- Insert line below

O -- Insert line above

5. Clipboard

d+w -- delete word

d+d -- delete line

p -- put after cursor

P -- put before cursor

y+w -- copy word

y+y -- copy line

Wednesday, December 25, 2013

What happens when you type google.com

Step 1: Request a record

You begin by asking your computer to resolve a hostname, such as visiting 'http://www.google.com' in a web browser. The first place your computer looks is its local DNS cache, which stores DNS information that the computer has recently retrieved.

Step 2: Ask the Recursive DNS servers
If the records are not stored locally, your computer queries (or contacts) your ISP's recursive DNS servers. These machines perform the legwork of DNS queries on behalf of their customers. The recursive DNS servers have their own caches, which they check before continuing with the query.

Step 3: Ask the Root DNS servers
If the recursive DNS servers do not have the record cached, they contact the root nameservers. These thirteen nameservers contain pointers for all of the Top-Level Domains (TLDs), such as '.com', '.net' and '.org'. If you are looking for 'www.google.com.', the root nameservers look at the TLD for the domain - 'www.google.com'- and direct the query to the TLD DNS nameservers responsible for all '.com' pointers.

Step 4: Ask the TLD DNS servers
The TLD DNS servers do not store the DNS records for individual domains; instead, they keep track of the authoritative nameservers for all the domains within their TLD. The TLD DNS servers look at the next part of the query from right to left - 'www.google.com' - then direct the query to the authoritative nameservers for 'google.com'.

Step 5: Ask the Authoritative DNS servers
Authoritative nameservers contain all of the DNS records for a given domain, such as host records (which store IP addresses), MX records (which identify nameservers for a domain), and so on. Since you are looking for the IP address of 'www.google.com', the recursive server queries the authoritative nameservers and asks for the host record for 'www.google.com'.

Step 6: Retrieving the record
The recursive DNS server receives the host record for 'www.google.com' from the authoritative nameservers, and stores the record in its local cache. If anyone else requests the host record for 'www.google.com', the recursive servers will already have the answer, and will not need to go through the lookup process again until the record expires from cache.

Step 7: The Answer!
Finally, the recursive server gives the host record back to your computer. Your computer stores the record in its cache, reads the IP address from the record, then passes this information to the web browser. Your browser then opens a connection to the IP address '72.14.207.99' on port 80 (for HTTP), and our webserver passes the web page to your browser, which displays Google.

My Profile

My photo
can be reached at 09916017317