Git - Fast Version Control System

This post is for the sake of getting rid of one of our old habits. I remember my first days of programming. While I was writing a code for a  project, after each progress I made, I used to copy the entire project, give it a new name(project1, project2, project3, etc.) and save it. This was my way to keep track of my working code. If after changing the latest version of my code, the project gets worse, I used to delete it, extract the latest running version of the project and move on modifying it.

There are better ways of controlling the versions of our projects. Git is the one I am going to write about. You can find a detailed documentation about it from here. Download and install it from http://git-scm.com/download . After installing it, it will be available to run from your command prompt window or terminal.  I will write the commands I frequently use and explain the purpose of each.

As a Linux user, I will use Unix commands to explain it.

1. Lets first create a directory that will contain our project and name it FirstGitProject.

mkdir FirstGitProject


2. Now change the current working directory to FirstGitProject.


cd  FirstGitProject


3. Now we can initialize our git repository. ( Since this is the first time we use this command it will create  an empty repository. That is, the .git directory, its sub-directories and templates that will be used by git. )


git init


4. Lets introduce ourselves to git, so that it knows who will do the changes, and commit into the repository.



git config --global user.name "Mahir Atmis"
git config --global user.email "atmismahir@gmail.com"


5. Lets create two files in our project. There are many ways to create a file under unix environment, and I will use output redirecting.


echo "content of foo" > foo.txt
echo "content of bar" > bar.txt


6. Now we created two files, but git doesn't track them. We want it to track our files, so we type


git add .


This will tell git to track all the files under the current directory. In technical terms this will add these files to the index.


7. Everything is ready to record into repository. Lets commit the project and save it in repository

git commit -m "My first Commit"


Here, the commit option saves into the repository the files that have been added . That is, to commit a change first you have to add it using the command at 6th step.  The -m flag is optional. If you use this flag, the message in between the parenthesis will be your commit message. If you don't use that flag,  git will open a page for you to write your commit message. Commit messages are extremely important for a programmer to know what changes he/she has  done at each commit.

8. Now lets make some changes in foo.txt file.

echo "Updated the content of foo.txt" > foo.txt

We changed the content of foo.txt.

9. Lets see if git is aware of the change we just made.

git status

This command shows the status of the git repository. That is, checks if there are any untracked or modified files to commit. When we  run the above command, we will get the following output:

# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified:   foo.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

The output says foo.txt is modified, but this change is not reflected into the repository. It asks for adding this change using git add , or git commit -a commands.  

10. For easiness we will use the latter one. That is,

git commit -a -m "foo.txt has been modified"

Providing the -a flag to git commit makes git save all the files that has been changed into the repository. NOTE: It won't save newly created files, it will only save the changed files. To add newly created files use "git add"

Sed, Grep, Awk

Sed, Grep, and Awk are tools widely used by Linux experts to retrieve desired data from text files, the results of other Linux commands, or webpage contents, and etc. We are going to use them to retrieve useful information from a web content.

First, we will use the sed to convert the contents of an HTML page into an ASCII file. The internet movie database (www.imdb.com) contains a list of the top 250 movies of all time in rank order. We will write a pipeline of sed commands to convert the raw HTML into a simpler ASCII formatted "pipe"-delimited text file.(to see what the raw HTML looks like, go here and "View Source" in your web browser). The ASCII file we create will have the following format:

Rank|Rating|Title|Year|Votes

While there are many ways to solve the problem of removing unnecessary HTML content from the raw HTML file, below is an outline of the steps that I applied by my sed script .

First I downloaded the content of the top 250 films by using the Unix GET command and piped the result of the GET command to my list of sed commands. For easiness I saved all my sed commands in a file named HtmltoAscii.sed. I exposed the content of that file below, and explained what each sed command is used for. The following pipeline of commands downloads the content of the file located at  http://www.imdb.com/chart/top, and parses it using sed commands and turns it into ASCII format mentioned above and saves it in a file named part1.dat.

----------------------------------------------------------------------------
GET -n http://www.imdb.com/chart/top | sed -f HtmltoAscii.sed > part1.dat
----------------------------------------------------------------------------


HtmltoAscii.sed file content:
----------------------------------------------------------------------------
#delete all lines except the one containing "Top 250 movies"
/Top 250 movies/!d

#remove everything up to first "</b></font></td></tr>"
s/^.*<\/b><\/font><\/td><\/tr>//

#remove everyting after the last "</tr>"
s/\(.*\)<\/tr>.*$/\1 /

#replace all "</tr>" with "#"
s/<\/tr>/#/g

#replace all html tags with a space
s/<[^>]*>/ /g

#replace two or more spaces with "|"
s/  [ ]*/|/g

#remove the trailing dots of all the rank fields
s/\([1-9][0-9]*\)\.|/\1|/g

#remove the parentheses in all the year fields
s/(\([1-9][0-9][0-9][0-9]\))/\1/g

#remove any commas in the votes fields
s/\([1-9][0-9]*\),\([0-9]*\)/\1\2/g

#delete the first and last "|" delimiter
s/^|\(.*\)|$/\1/

#replace each occurrence of the string |#| with a newline character
s/|#|/\n/g

#replace each occurrence of &#x27 with the single quote "'"
s/&#x27/'/g

#replace all of the form &#xHH, where HH is hex number, with an "*"
s/&#x[0-9A-F][0-9A-F]/*/g
----------------------------------------------------------------------------

Now our ASCII file is ready and in a format we can obtain useful data from, so we can start querying its content. Lets answer the following questions by using grep and other basic Unix commands except awk and sed.

1. List the titles of all the 2011 movies in the top 100.
     grep '|2011|' part1.dat | cut -d "|" -f 3 
2. Print the number of movies that use the same word twice in the title.
      cut -d "|" -f 3 < part1.dat | grep -c '^\(.*\) .*\1 \| \(.*\) .* \2 \| \(.*\) .* \3$'
3. Print the rank of each movie that contains a non-alphabetic character in its title (excluding spaces).  
   cut -d "|" -f 1,3 < part1.dat | grep '[1-9][0-9]*|.*[^a-zA-Z ].*' | cut -d "|" -f 1
4. Print the number of movies with less than 50000 votes.
      cut -d "|" -f 5 < part1.dat | grep -c '^[1-9][0-9]\{0,3\}$\|^[1-4][0-9]\{4,4\}$'

And now lets write awk scripts to do the following tasks with the movie database.  Let's use a single awk script per question, and no other tools.

1- Print the total number of votes across all moves. 
      awk -F"|" '{ tot += $5 }END{ print tot }' part1.dat 
2- Print the year that had the greatest number of votes.
      awk -F"|" '$5 > greatest { date=$4; greatest=$5 }END{ print date }' part1.dat
3- Print the average number of votes for movies above an 8.5 rating and the average number of votes for 
movies below an 8.5 rating on a single line.
      awk -F"|" '{ if($2<8.5){nBelow++; BelowSum+=$5}else if($2>8.5){nAbove++; AboveSum+=$5}} 
      END{ printf("%i %i\n",AboveSum/nAbove,BelowSum/nBelow) }' part1.dat
4- Print the average number of words in each title.  A word is any string of non-whitespace characters.
     awk -F"|" '{ wordCount += split ($3,wordsArray," ")} END{printf("%.3f\n", 
   wordCount/NR)}' part1.dat 
5- Print the most commonly used word in titles besides “The” and "the".
     awk -F"|" '{ split ($3,wordsArr," "); for(i in wordsArr { 
     wordCountArr[wordsArr[i]]++}}
     END{ for(word in wordCountArr){ if(wordCountArr[word] > maxCount && word !=  
     "The" && word != "the"){maxCount = wordCountArr[word]; winner=word}};  
     printf("%s\n",winner)}'      part1.dat
6- Print the movies with the longest and shortest titles on two lines
     awk -F"|" 'BEGIN {minLength=10000} { titleLen=length($3); if(titleLen<=minLength)
    {minLength=titleLen;minTitle=$3}if(titleLen>=maxLength){maxLength=titleLen;maxTitle=$3}}
     END{ printf("%s\n",maxTitle);printf("%s\n",minTitle)}' part1.dat


 

Java Builder Pattern Example

This is an example to demonstrate how to use the Builder Pattern in Java. The main advantage of using a Builder Pattern in this example is to deal with the various number of optional parameters of Contact class.
The Contact class in this example keeps track of a person's Contacts information. The Contact class has fields that are not required to be set. The user of this class only sets the fields he need by using the Builder Pattern.

Contact Class:

package edu.nyu.pqs;

/** This is an immutable, underivable Contact class that implements Comparable
 * overrides toString and hashCode methods, uses Builder pattern to
 * deal with its large number of optional parameters.
 * All of its fields are final to provide the immutability.
 * The class is final to make it underivable 
 */
public final class Contact implements Comparable { 
 private final String name;
 private final String jobTitle;
 private final String company;
 private final String email;
 private final String workPhone;
 private final String mobilePhone;
 private final String address;
 private final String notes; 
 /** to avoid concatenation cost each time the toString method is invoked,
  * the String version of the class is stored as a field and initialized 
  * once the build method is invoked.
 */
 private final String stringFormofContact;
 /** to avoid calculation cost each time the hashCode method is invoked,
  * the hash code is stored as a field and initialized 
  * once the build method is invoked.
 */
 private final int hashcode;
 
 /** The member class of the Contact class which helps the Contact class
  * to easily deal with large number of optional parameters
  */
 public static class Builder{  
  private String name ="";
  private String jobTitle ="";
  private String company="";
  private String email ="";
  private String workPhone="";
  private String mobilePhone="";
  private String address="";
  private String notes="";
  
  /**
   * Sets the name field of the Builder class
   * @param val , String value to set the name 
   * field of the Builder and so the Contact class
   */
  public Builder name (String val){   
   name=val; return this;
  }
  
  /**
   * Sets the company field of the Builder class
   * @param val , String value to set the company 
   * field of the Builder and so the Contact class
   */
  public Builder company (String val){   
   company=val; return this;
  }
  
  /**
   * Sets the jobTitle field of the Builder class
   * @param val , String value to set the jobTitle 
   * field of the Builder and so the Contact class
   */
  public Builder jobTitle (String val){   
   jobTitle=val; return this;
  }
  
  /**
   * Sets the email field of the Builder class
   * @param val , String value to set the email 
   * field of the Builder and so the Contact class
   */
  public Builder email (String val){   
   email=val; return this;
  }
  
  /**
   * Sets the workPhone field of the Builder class
   * @param val , String value to set the workphone 
   * field of the Builder and so the Contact class
   */
  public Builder workPhone (String val){   
   workPhone=val; return this;
  }
  
  /**
   * Sets the mobilePhone field of the Builder class
   * @param val , String value to set the mobilePhone 
   * field of the Builder and so the Contact class
   */
  public Builder mobilePhone (String val){   
   mobilePhone=val; return this;
  }
  
  /**
   * Sets the address field of the Builder class
   * @param val , String value to set the address 
   * field of the Builder and so the Contact class  
   */
  public Builder address (String val){   
   address=val; return this;
  }
  
  /**
   * Sets the notes field of the Builder class
   * @param val , String value to set the notes 
   * field of the Builder and so the Contact class
   */
  public Builder notes (String val){   
   notes=val; return this;
  }
  
  /**
   * Builds the Contact class using methods called from Builder class 
   * to the builder class
   */
  public Contact build (){   
   return new Contact(this);
  }  
 }
 
 /** Initializes the fields of the Contact class 
  *  benefiting from the Builder class
  *  @param builder, the builder which is called to initiate 
  *  the fields of Contact class instance 
  */
 private Contact(Builder builder){
  name= builder.name ;
  jobTitle = builder.jobTitle;
  company = builder.company;
  email = builder.email;
  workPhone = builder.workPhone;
  mobilePhone = builder.mobilePhone;
  address = builder.address;
  notes = builder.notes;
  stringFormofContact = convertFieldsToString();
  hashcode = calculateHashCode() ;
 }
 
 /** suppresses default constructor for noninstantiability
  * throws AssertionError if it is being called 
  */
 private Contact(){
  throw new AssertionError();
 }
 
 /** Returns the name field of the class
  */
 public String getName() {
  return name;
 }

 /** Returns the jobTitle field of the class
  */
 public String getJobTitle() {
  return jobTitle;
 }

 /** Returns the company field of the class
  */
 public String getCompany() {
  return company;
 }

 /** Returns the email field of the class
  */
 public String getEmail() {
  return email;
 }

 /** Returns the workPhone field of the class
  */
 public String getWorkPhone() {
  return workPhone;
 }

 /** Returns the mobilePhone field of the class
  */
 public String getMobilePhone() {
  return mobilePhone;
 }

 /** Returns the address field of the class
  */
 public String getAddress() {
  return address;
 }

 /** Returns the notes field of the class
  */
 public String getNotes() {
  return notes;
 }
 
 /** If the two given objects are the same,
  * function returns true.
  * If the Object to be compared is not an instance of the Contact class,
  * function returns false.
  * Otherwise, function compares each of the fields in the class
  */
 @Override public boolean equals(Object o){  
  if (o == this) return true;
  if(!(o instanceof Contact)) return false;  
  Contact cntct = (Contact) o;  
  return cntct.name.equals(name) && 
    cntct.jobTitle.equals(jobTitle) && 
    cntct.email.equals(email) && 
    cntct.workPhone.equals(workPhone) &&
    cntct.mobilePhone.equals(mobilePhone) &&
    cntct.address.equals(address) &&
    cntct.notes.equals(notes);
 }
 
 /** Converts a given Contact class instance into String. 
  * Fields are separated by line breaks and are labeled as they are 
  * converted into String 
  * 
  * @return The String equivalent of a Contact class instance 
  */
 @Override public String toString(){
  return stringFormofContact;
 }
 
 /** This function is called only when the build() method is invoked. 
  *  Returns the String version of the each fields by adding them a label.
  */
 private String convertFieldsToString() { 
  return ("Name: "+name+"\nJob Title: "+jobTitle+ 
      "\nCompany: "+company+"\nEmail: "+email+
      "\nWork Phone: "+workPhone+
      "\nMobile Phone: "+mobilePhone+ 
      "\nAddress: "+address+"\nNotes: "+notes) ;
 }
 
 /** Returns the hash code value for a given class instance 
  * The code was already created just after other fields of
  * the Contact class are initialized by calling the calculateHashCode
  * method.
  */
 @Override public int hashCode(){
  return hashcode; 
 }
 
 /** Returns the hash code value for the current class
  * The code is generated by calculating the hash code values of each 
  * field and multiplying those values by some constant
  */
 private int calculateHashCode(){
  int result = 17;
  result = 31*result+name.hashCode();
  result = 31*result+jobTitle.hashCode();
  result = 31*result+company.hashCode();
  result = 31*result+email.hashCode();
  result = 31*result+workPhone.hashCode();
  result = 31*result+mobilePhone.hashCode();
  result = 31*result+address.hashCode();
  result = 31*result+notes.hashCode();
  return result ;
 }
 
 /**
  * Compares two Contact classes
  * 
  * @return  Positive integer if left hand side of the compared 
  * objects is bigger than the right hand side
  *    Negative integer if left hand side of the compared 
  * objects is smaller than the right hand side
  *    Zero if left hand side of the compared 
  * objects is equal to the right hand side
  */
 public int compareTo(Contact cntct){
  int comparisionReslt = name.compareTo(cntct.name);
   if(comparisionReslt != 0) return comparisionReslt ;
  comparisionReslt = jobTitle.compareTo(cntct.jobTitle);
   if(comparisionReslt != 0) return comparisionReslt ;
  comparisionReslt = company.compareTo(cntct.company);
   if(comparisionReslt != 0) return comparisionReslt ;
  comparisionReslt = email.compareTo(cntct.email);
   if(comparisionReslt != 0) return comparisionReslt ;
  comparisionReslt = workPhone.compareTo(cntct.workPhone);
   if(comparisionReslt != 0) return comparisionReslt ;
  comparisionReslt = mobilePhone.compareTo(cntct.mobilePhone);
   if(comparisionReslt != 0) return comparisionReslt ;
  comparisionReslt = address.compareTo(cntct.address);
   if(comparisionReslt != 0) return comparisionReslt ;
  comparisionReslt = notes.compareTo(cntct.notes);
   return comparisionReslt ;
 } 
}


How To Use it:
package edu.nyu.pqs;

public class TryContact {

 public static void main(String[] args) {
  // Create a Contact instance
  Contact c = new Contact.Builder().name("Mahir").notes("My School Friend").build();
  // Print it
  System.out.print(c);
 }
}

Python Line Profilers using Decorator Pattern

You can use any of the following decorators to profile your functions line by line.  The first one(Profiler1) is a decorator as a class and...