JConsole over SSH with SOCKS Proxy

This is from here: Create the SSH socks proxy locally on some free port (e.g. 7777):

ssh -fN -D 7777 user@firewalled-host

Run JConsole by specifying the SOCKS proxy (e.g. localhost:7777) and the address for the JMX server (e.g. localhost:2147)

jconsole -J-DsocksProxyHost=localhost \
  -J-DsocksProxyPort=7777 \
  service:jmx:rmi:///jndi/rmi://localhost:2147/jmxrmi \
  -J-DsocksNonProxyHosts=

Code exploration in C with GNU GLOBAL

For a long time I’ve been getting by with using cscope at work. I’m not a great C programmer by any stretch, and I need to have multiple windows open and a note book by my side to get any sort of understanding of the code.

globals
GLOBAL’s HTML output

So I was happy to discover GNU GLOBAL recently. Getting it setup is a matter of running gtags on the root of the repository. Running htags also produces nice HTML files that make it much easier to browse through.

It has quite a few integrations as well. The snippet I added to my emacs init.el is as follows:

;; gtags/gnu globals
(setq load-path (cons "/usr/share/emacs/site-lisp/" load-path))
(autoload 'gtags-mode "gtags" "" t)
(setq c-mode-hook
 '(lambda ()
 (gtags-mode 1)
 ))
(setq gtags-suggested-key-mapping t)
(setq gtags-auto-update t)

Here is a table that compares similar tools.

Publishing my (Emacs/org-powered) notes online

In the past decade, I’ve amassed a single, long file called notes.txt file that has a large amount of information I’ve gathered in my day-to-day activities at work. I use GNU Emacs exclusively to edit and search it. It is now more than 21000 lines long, and when I need to find the workaround for some obscure bug I hit several months back (if not years), the answer is always a C-s away.

Still, in an attempt to reorganize my notes, I spent my free time in the past few weeks in slicing up the file into smaller categories. I decided to properly use org-mode to format them this time. As a bonus, org-publish is a lovely way to render html pages out of them.

My workflow has also improved: I use a bookmark to open the incoming.org file and just dump the content there. Later I re-file them to the appropriate section. I use deft (bound to F8) to quickly search through them.

Since they needed a permanent home, I’ve uploaded the notes to GitHub. I was happy to see that github was able to render the .org files natively. The notes are infinitely more malleable within emacs itself of course 🙂

Here are the main sections:

And here is the project in GitHub: https://github.com/IndelibleStamp/notes

Update: GitLab looks really nice so I created an account and migrated my repositories there, so here’s the link for my notes repo in GitLab: https://gitlab.com/indeliblestamp/notes/tree/master 

Java factorial program with recursion and BigIntegers

Another initiative at work expects me to have a certain amount of Java knowledge. Oh well. Here are some good resources I am currently going through:

Learneroo’s first chapter has a factorial method recursively. No biggie. I wrote a quick program, ran and tested it for small values. When I tried it for ‘100’, though:

Enter a number for which I will find the factorial:
100
0

Oops. The same program worked fine with Python, what was I doing wrong? A quick check showed that my program returned the result as an int. The highest Int you can get is 2^(31)-1. So, enter the BigInteger class. It is not a native data type and I had to tweak the program a bit to handle it. This is what it shows as the output now:

Enter a number for which I will find the factorial:
100
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

And here is the full program:

import java.math.BigInteger;
import java.util.Scanner;

public class Factorial {

    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        System.out
                .println(“Enter a number for which I will find the factorial:”);
        int num = read.nextInt();
        Factorial test = new Factorial();
        BigInteger result = test.findfact(num);
        System.out.println(result);
        read.close();
    }

    public BigInteger findfact(int n) {
        if (n == 1) {
            return BigInteger.ONE;
        } else {
            return BigInteger.valueOf(n).multiply(findfact(n – 1));
        }
    }
}

Fetch unique words in a web page (in 7 lines of Python)

I got a chance to enroll for some Python training at work and had a nice time with it. I’ve tried getting into other languages myself before, and this has been my freshest experience so far. The three days of training were enough to show me how insanely easy it is to get productive in Python.

A quick example we saw was to:

  • Get the contents of a web page, and
  • Find all the unique words in it.
This simple example introduces the usage of lists, sets, modules and loops. The code looks like this:

import urllib2
words=[]
for line in urllib2.urlopen(‘http://slashdot.org’):
    words.extend(line.split(‘ ‘))
print ‘number of words is’, len(words)
uniq=set(words)
print ‘number of unique words is’, len(uniq)
That’s it! The whole job takes only eight lines. Running it gives an output like this:
C:Python27mypy>19.py
number of words is 13297
number of unique words is 3672

The first line imports the urllib2 module. 
The next line initializes an empty list.
The third line is where most of the magic happens. A for loop iterates over a list returned by urllib2 when we ask it to open Slashdot’s home page. For each line, we split the content into individual words and append it to our list.
That gives us all the words in the page, which we print.
Python supports a set data structure that we copy this list to, which ends up removing recurring elements automatically (since sets support only unique elements).
And that’s it, we’re done. Of course there is always more to do beyond this (such as in not counting html tags), but this was probably my favourite example from the course that captured how easy it was to get seemingly complicated tasks done in Python.

Python

I’m trying to learn Python and these days and have found a ton of resources on the web and off it. First up is Learning Python, a fat book I bought a couple of weeks back. While a bit on the verbose side, it certainly seems to cover a lot of the essentials. I’ve decided to jump straight into Python 3, which this book has been updated to cover.

Although I stay away from video tutorials, I found a ton of them at pyvideo. There are several good ones on iPython there, which looks great.

Lastly there are the conferences themselves, and coincidentally, the one in India starts tomorrow! I’ve signed up for that as well and look forward to an interesting weekend.