Friday, February 21, 2014

Synchronized decorator for Python

The following decorator provides synchronization for Python codes.
Reference


def synchronized(lock):
    """ Synchronization decorator. """

    def wrap(f):
        def newFunction(*args, **kw):
            lock.acquire()
            try:
                return f(*args, **kw)
            finally:
                lock.release()
        return newFunction
    return wrap

if __name__ == '__main__':
    from threading import Thread, Lock
    import time

    myLock = Lock()

    class MyThread(Thread):
        def __init__(self, n):
            Thread.__init__(self)
            self.n = n

        @synchronized(myLock)
        def run(self):
            """ Print out some stuff.

            The method sleeps for a second each iteration.  If another thread
            were running, it would execute then.
            But since the only active threads are all synchronized on the same
            lock, no other thread will run.
            """

            for i in range(5):
                print 'Thread %d: Start %d...' % (self.n, i),
                time.sleep(1)
                print '...stop [%d].' % self.n

    threads = [MyThread(i) for i in range(10)]
    for t in threads:
        t.start()

    for t in threads:
        t.join()

Filter forwarded messages in GMail

If you want to filter a message that has been forwarded to your GMail account by one of your other email services, then you can create a filter with Has Words field set to

deliveredto:your_email_service_mail_accountname_that_forwarded_message. 

You can set filters for example to archive such forwarded messages without showing them in your Inbox. You might want to do this for example, if you want to keep a record of all your emails in your GMail account but do not want to display duplicates when you are using an email client that display emails received in multiple accounts.

Thursday, December 19, 2013

numpy dot vs inner

dot and inner returns the same result for 1D arrays.
However, for 2D arrays (matrices), dot gives the matrix product, whereas inner gives the sum-product over the last axis.

References
inner vs. dot
inner document
dot document

what is sum product?
A = [[x1, x2], [x3, x4]]
B = [[y1, y2], [y3, y4]]

sum product of A and B is given by numpy.inner(A,B) as follows
A's row i is element-wise multiplied by B's row j and the values are added to get the (i,j) element

[[x1*y1+x2*y2, x1*y3+x2*y4], [x3*y1+x4*y2, x3*y3+x4*y4]]

Saturday, April 20, 2013

Apple airplay no sound issue

Sometimes when you mirror your display on airplay you cannot hear. Do the following to solve this issue.

sudo killall coreaudiod

Thursday, February 28, 2013

tmux

  • Commands:
    • starting a new named session
      • $ tmux new -s session_name
  • you can use a .tmux.conf file in your home directory to tmux as follows
    • setw -g mode-mouse on
    • # scrollback buffer size increase
    • set -g history-limit 500000
  • note that with mode-mouse on you cannot copy paste text (you can scroll). You need to do the following Ctrl+B: (note the colon) and then enter the following command
    set -g mode-mouse off
  • when you have finished copy/pasting text you can turn mode-mouse to on so that you can scroll using the mouse wheel

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 
 


Continuously monitor GPU usage

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