Saturday, November 17, 2012

Excel Tricks

Keep the header row displayed when scrolling down a large sheet

Assume that you have a large table that you need to scroll to the bottom and at some point the header row disappears. The solution is to click just below the header row and from Window menu select Freeze Pane. Now the header row will be visible throughout the scrolling.


Sunday, September 30, 2012

git (using gitosis)

gitosis is a great way to have a private git server. To install gitosis and perform the basic configurations refer to this guide. gitosis can use ssh access to your remote repositories. Unlike svn (and many other version control systems), git keeps an entire snapshot for your commits instead of a series of diffs. This is great because you can see the entire history of your project locally and develop locally. This comes handy when you do not have access to the internet to push your commits to the remote repository. Moreover, even in the event that the remote repository dies, you can still recover your code using your local database. For this and many other reasons (such as the distributed mirroring and the free github), git is the most popular choice for version control.


Installing gitosis in your remote server

Once you have installed gitosis in your remote server, you must clone gitosis-admin repository to your local machine. You can perform numerous administrative tasks locally via this repository such as adding new projects.

The first time you use git to push something you must specify your name and e-mail address. This can be done as follows.

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

To add a new repository do as explained here.

  1. cd gitosis-admin
  2. git pull   This will update your gitosis-admin repository
  3. If you do not have your gitosis-admin repository, then you must first clone it by
    git clone gitosis@server:gitosis-admin.git
  4. Now edit the gitosis.conf file and add your newproject details
    [group helloproject]
    members = mylogin@myhost
    writable = helloproject
    Note that the last one is "writable" and not "writeable"
  5. Now commit the changes you made to gitosis-admin repository by,
    git commit -am "added the project helloproject"
    next push the commit to the local db by git push
  6. Now move to the directory that contains files for your new project "helloproject". Then do git init
  7. This will initialize the new repository. You can add individual files to track using
    git add filename
    You cannot add an empty directory to git repository. When you add a file from a directory that directory will be automatically added to the repository. However, if you want to add an empty directory to the repository you can either place a dummy README file in that directory or write a .gitignore file in the directory that you want to add. The .gitignore directory must contain the following two lines.
    # Add everything in this direcory
    *
    # Except for this file.
    !.gitignore
  8. Once you have added the files for the newly created helloproject you must commit them.
    git commit -am "Initial commit for helloproject"
  9. Now you need to add this new project to your remote repository by
    git remote add origin ssh://gitosis@myserver/helloproject.git
  10. You can now push the changes to your remote repository
    git push --all
  11. In order to be able to pull back to the same directory (without cloning the pushed project from the remote server) you must edit .git/config file as follows.
    [branch "master"]
    remote = origin
    merge = refs/heads/master
  12. That is it.
Git development cycle (command used frequently)
  1. You would either add an existing project to git as explained above or will clone a project from the remote repository. To clone do as follows.
    git clone gitosis@myserver:helloproject.git
  2. Now you will modify some tracked files and/or add new files to be tracked using
    git add filename
  3. Note that there is this concept of staging in git which means you must first "stage" files to be committed and then perform the actual committing. This means you will have to add again any files that you have modified in order for them to get committed. This additional staging step can be skipped by using the -a option during commit.
    git commit -am "message"
  4. Commit actually commit things to your local repository. It does not push your changes to your remote server. To push your changes to the server do as follows.
    git push
  5. This is the basic (frequent) development cycle. Note that if someone else have pushed before you (i.e. origin has been modified by some other person other than you), then git push will fail. You must first git pull their changes and then check them with your modifications (git fetch will not merge but git pull will try to merge) and then git push again.
Git useful tricks
  1. Use git status to see what are the changes that are going to be committed in the next commit.
  2. You can use tags to easily refer (and checkout) important milestones of your project.
    To list the current tags do
    git tag -l
  3. To create a new tag do as follows
    git tag -a v1.0 -m "first version"
  4. To push your tags to the remote server do,
    git push origin v1.0
  5. Now when someone clones this repository that person will also receive the tag v1.0. After cloning a project you can checkout a specific tag as follows.
    git checkout v1.0
  6. If we want to delete the tag see here.
Git hub
  • cloning a repository from github
    • git clone git://github.com/Bollegala/svdmi.git
  • You might not be able to push to the original git directory if you have not set the git url as follows
    • git remote set-url origin git@github.com:Bollegala/svdmi.git 
 


Saturday, September 29, 2012

fish shell vs. bash

fish shell is a great tool. But it can be confusing in the initial stages especially to adapt from bash to fish shell. Some important differences are highlighted below.


  1. escaping wildcards such as *
    use double quotes
    rm "*.pyc"
  2. command expansion using backticks
    the bash command
    rm `find -type d -name .svn` would be
    rm (find -type d -name .svn) in fish shell

Wednesday, August 29, 2012

Multiprocessing in Python

There are numerous ways to multiprocess in python. Pools give a very easy way.

Note: If you do not catch keyboard interrups as shown in the code then you will have to wait until all workers have finished even if you send Ctrl+C to the parent process. There are other methods such as map (blocks until each result is received and orders sequentially), apply and apply_async where you can provide a callback function that actually determines what must be done with the result. If the task items are not iterable then you must use apply or apply_async and handle the input in the callback.


from multiprocessing import Pool
from functools import partial

import time
import sys

def doWork(i, x):
    """
    Does the actual work.
    """
    print i
    time.sleep(2)
    return (x * i)
    pass


def multi():
    """
    Does the actual multiprocessing.
    """
    # Create the pool.
    pool = Pool()

    # We must convert the doWork to a function of one argument.
    # This argument will be filled in by an iterator.
    partialWorker = partial(doWork, x=5)

    tasks = range(100)

    p = pool.map_async(partialWorker, tasks)

    try:
        results = p.get(0xFFFF)
    except KeyboardInterrupt:
        print "Received Ctrl-c"
        sys.exit(1)

    pool.close()
    pool.join()          
    pass


if __name__ == "__main__":
    multi()
    

Saturday, August 18, 2012

Using lambda functions for sorting in Python

Give a list L = [56, 78, 98, 34] the following one linear will sort the elements in the descending order of their values.
L.sort(lambda x, y: 1 if y > x else -1)

Sunday, August 12, 2012

Reconnect an ejected USB HDD in a Mac

Sometimes you realize that a file that you need is in an external USB HDD that you just ejected. Of course the external USB is still connected to your USB port. So what would you do to make the USB HDD reappear in your connected drives? Of course, you can physically disconnect the external USB HDD and re-plugin it. But that sounds stupid because the device is already connected to your machine. Just open a terminal and first find out what is the number of your disk by,

$ diskutil list

This will list up all your disk. Let us assume the external USB HDD that you want to reconnect is disk1 (by the way your inbuilt HDD will be disk0), do the following,

$ diskutil mountDisk disk1
Thats it! No more physical unplugging and replugging... 

Sunday, August 5, 2012

Checking the site-packages

If you want to know where the python site-packages directory is located in your machine do the following.


from distutils.sysconfig import get_python_lib
print(get_python_lib())


Saturday, July 28, 2012

Adding and a flag when process has completed

Here are the details

with bash shell do
ping -c 5 www.google.com && tput bel
with fish shell do
ping -c 5 www.google.com ; tput bel 

Saturday, June 2, 2012

Ordering among citations in a multiple citation in Bibtex

It is possible to arrange multiple references within the same brackets by doing \cite{paper1,paper2} instead of \cite{paper1}\cite{paper2}. The former will produce for example [1,2] and the latter will produce [1][2]. The former approach is considered better.

However, sometimes the ordering among the citations within a multiple citation might not come right. For example, \cite{paper1,paper2} might produce [2,1] instead of [1,2]. Here again, it is considered better to order citations in a multiple citation according to the order they appear in the references list. To make sure they are ordered as such add \usepackage{cite} in the preamble of your latex source file.

Saturday, May 26, 2012

Mounting and Unmounting ISO files in Ubuntu

Create a directory first.

$mkdir /home/myusr/example

Mount the iso by,

$sudo mount -o loop filename.iso /home/myusr/example
This will mount the ISO file to the created directory.
To unmount do,
$sudo umount /home/myusr/example 


Continuously monitor GPU usage

 For nvidia GPUs do the follwing: nvidia-smi -l 1