Tuesday, September 16, 2014

Python: Nested Scopes

Python 2.2 introduced the concept of Nested Scopes(Details) that affects the namespace search order. Prior to Python 2.2, when you reference a variable within a nested functionPython will search for that variable in the current function's namespace, then in the module's namespace. 

Python 2.2 will search for the variable in the current function's namespace, then in the parent function's namespace, then in the module's namespace. 

Python 2.1 can work either way; by default, it works like Python 2.0, but you can add the following line of code at the top of your module to make your module work like Python 2.2:

from __future__ import nested_scopes

Monday, August 11, 2014

Using Labmda Expression in File Filtering

Java 8 makes it so easy to write the anonymous functions which can be understood using the following file filtering example:

Before Java 8:
    public static List fileList(String directory) {
        List fileNames = new ArrayList<>();
        DirectoryStream.Filter filter = 
              new DirectoryStream.Filter() {
         public boolean accept(Path file) throws IOException {
           return (Files.size(file) > 8192L);
        try (DirectoryStream directoryStream = 
          Files.newDirectoryStream(Paths.get(directory), filter)) {
            for (Path path : directoryStream) {
        } catch (IOException ex) {}
        return fileNames;
With Java 8:
    public static List fileListWithLambda(String directory) {
        List fileNames = new ArrayList<>();
        DirectoryStream.Filter filter = 
          (Path file) -> (Files.size(file) > 8192L);
        try (DirectoryStream directoryStream = 
          Files.newDirectoryStream(Paths.get(directory), filter)) {
            for (Path path : directoryStream) {
        } catch (IOException ex) {}
        return fileNames;

Converting PPT to Png

public class PptToPng {

    public static void main(String[] args) throws Exception {
        FileInputStream is = new FileInputStream("example.pptx");
        XMLSlideShow ppt = new XMLSlideShow(is);

        double zoom = 2; // magnify it by 2
        AffineTransform at = new AffineTransform();
        at.setToScale(zoom, zoom);

        Dimension pgsize = ppt.getPageSize();

        XSLFSlide[] slide = ppt.getSlides();
        for (int i = 0; i < 2; i++) {
          BufferedImage img = new BufferedImage((int) Math.ceil(pgsize.width * zoom), 
                    (int) Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_INT_RGB);
          Graphics2D graphics = img.createGraphics();

          graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
          FileOutputStream out = new FileOutputStream("slide-" + (i + 1) + ".png");
          javax.imageio.ImageIO.write(img, "png", out);

Thursday, June 26, 2014

How to Catch i18n Errors Early?

Internationalization (often abbreviated i18n) is the process of making an application localizable for users in different locales, while localization (often abbreviated L10n) is the process of actually localizing an application for a particular locale. In today’s globally connected world, it is rare that an application does not need to support users in many different locales.

Pseudolocalization is the automated generation of fake translations of a program's localizable messages. Using the program in a pseudolocale generated in this manner facilitates finding bugs and weaknesses in the program's internationalization. 

Google has been using this technique internally for some time and has used this tool for improving GMail, Google+ and many more. Google has now released an open-source Java library to provide this functionality at http://code.google.com/p/pseudolocalization-tool/.

Make your customers happy by better supporting i18n using Pseudolocalization tool.

Wednesday, June 18, 2014

Java 8 and Data Parallelism

By Definition, Parallelism reduces the runtime of a specific task by breaking it down into smaller components and performing them in parallel. Since we are already in Big Data age, Data Parallelism is going to play a big role. To achieve something meaningful from Big Data, we need to analyse a lot of data coming from different data sources. We need different algorithms that can process data faster and can consume the power of all computation units. Data Parallelism is one of those techniques which can analyse a lot of data quickly.

Let’s first define the term ‘Data Parallelism’. Data Parallelism means splitting up the data and assigning a single processing unit to each chunk of data. This works really well if you want to perform the similar operation on a lot of data.

What does Java provide to realize ‘Data Parallelism’?

Java 8 has come up with a lot of new libraries and “streams” library is one of them. Making an operation execute in parallel using the streams library is a matter of changing a single method call. You can execute streams in serial or in parallel. When a stream executes in parallel, the Java runtime partitions the stream into multiple substreams. Aggregate operations iterate over and process these substreams in parallel and then combine the results.

Streaming can be realized in two ways:
·        If you already have a Stream object, then you can call its parallel() method in order to make it parallel.
·        If you’re creating a Stream from a Collection, you can call the parallelStream method in order to create a parallel stream.

Following example calculates the total orders of a sequence of products. It transforms each product into its component orders, then gets into the price of each product, and then sums them:

public int getTotalPrice() {
return products.stream()

We can perform this operation in parallel by using parallelStream method:

public int getTotalPrice() {
return products.parallelStream()

Monday, June 16, 2014

How to Experiment with JavaScript Code inside your Browser?

Are you a JavaScript developer? Have you faced issues like how to readily test your JavaScript code? 

There are couple of things that can be done to solve this issue:

1. Use some online tool like Codepen
2. Use some editors

Using the first option requires to have always an internet connection and second option is slow. What if you get the facility offline right in your browser?

Scratchpad is such a tool available in Firefox browser. 

  • It provides an environment for experimenting with JavaScript code. 
  • You can write and test code ideas that interact with the contents of the web page, before switching to your usual development tools to finalize and clean up the end result.
To open the Scratchpad window, press Shift+F4, or go to the Web Developer menu (which is a submenu in the Tools menu on Mac OSX and Linux), then select Scratchpad. This will open up a Scratchpad editor window, which includes a comment that provides some brief information about how to use Scratchpad. From there you can immediately start writing some JavaScript code to try.

Tuesday, June 10, 2014

Creating an Embedded Video Streaming Server in Android

Different video streaming server provides different capabilities for streaming videos. What to do if you want to stream something inside your Android application.

Here is how you can create a simple streaming server:

NanoHttpd is an open-source, small-footprint web server that is suitable for embedding in Java applications. The code consists of single Java application file which can be used as a 
streaming server. Following is how to create using Android code:

public class VideoStreamingServer extends NanoHTTPD {

    public VideoStreamingServer() {
         // by default listening on port 8080

    public Response serve(String URI, Method method,
        Map header, Map parameters,
        Map files) {

    FileInputStream fis = null;
    try {
        fis = new FileInputStream(EXTERNAL_STORAGE_FILE_LOCATION);
    } catch (FileNotFoundException e) {
    return new NanoHTTPD.Response(Status.OK, "video/mp4", fis);

What happens when we try to use String.trim in IE 8?


var tempString = " my string ";

//in modern browsers
console.log(tempString ); //outputs " my string "
console.log(tempString .trim()); //outputs "my string"

//in IE 8
console.log(tempString.trim()); //error: Object doesn't support property or method\

We can use a better trim function which works in every browser:

Taken from JQuery.js

// Make sure we trim BOM and NBSP (here's looking at you, Safari 5.0 and IE)
var trimPattern = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g
var myTrim = function( text ) {
return text == null ? "" : ( text + "" ).replace( trimPattern, "" );

This should work in every browser including IE8 as well.

How to create local modules in JavaScript

The module pattern can be used to make your function private and save it from global effects. JavaScript does not give any error even if you override the functions.

For example, if you have a global function like

function global() {
     console.log('I am global function.');

and now i create a new module function and override global function like this:

function newModule() {
     global = function() {
          console.log('I am new global.');

If you run the above functions by calling:


The output will be:

I am global function.
I am new global.

You can use module pattern to stop global overriding.

var module = {};

(function(exports) {
    exports.local = function() {
        console.log('I am local not global.');

Now you can call local function using:


which will print:

I am local not global.

Tuesday, April 22, 2014

Virtualization Impact on Big Data

To make a big impact using Big Data, a company CIO needs to use cheap storage options which can scale in and scale out to satisfy the variety needs of big data.

 provides exactly the same option. Virtualization can further be divided into following categories:
  • Hardware Virtualization
  • Application Virtualization
  • Network Virtualization
  • Process and Memory Virtualization 
Each of these virtualization can have significant impact on Big Data platofrms. Some of the Virtualization benefits can be summed as:
  • Virtualization helps to ensure that big data platform can scale as needed to handle the large volumes and varied types of data included in data analysis.
  • An enterprise might not have the finances to procure the array of inexpensive machines for its first pilot. Virtualization enables companies to tackle larger problems that have not yet been scoped without a huge upfront investment.
  • Big data computing algorithms like Map reduce works well in a virtualized environment with respect to storage and computing. Amazon EMR encapsulates the MapReduce engine in a virtual container so that you can split your tasks across a host of virtual machine (VM) instances. Hence it provides better performance.
  • Virtualizing the network helps in improving the capability to manage the large distributed data required for big data analysis.

Sunday, April 20, 2014

Key aspects of a Big Data System

A big data solution must address the five Vs of big data:

  1. Velocity
  2. Variety
  3. Volume
  4. Veracity and
  5. Value

Velocity of the data is used to define the speed with which different types of data enter the enterprise and are then analyzed.

Variety addresses the unstructured nature of the data in contrast to structured data in weblogs, radio frequency ID (RFID), meter data, stock-ticker data, tweets, images, and video files on the Internet.

For a data solution to be considered as big data, the volume has to be at least in the range of 30–50 terabytes (TBs). However, a small amount of data could have multiple sources of different types, both structured and unstructured, that would also be classified as a big data problem.

Veracity refers to the trustworthiness of data. With many sources of data in structured and unstructured form, the quality and accuracy of data is uncontrollable. There can be various errors/complexities such as typos, newly formed abbreviations by digital natives, colloquial speech etc. Big data systems should allows us to work with such type of data.

Value is the final V which we need to consider the most. Until we are able to create a value out of such big data, its of no use. Accurate analysis and algorithms are needed based on special needs of the business. One big data volume can provide different values to different organizations based on the analytics applied on it. 

Technology Stack for a Commercial PhoneGap Mobile Application

I am into PhoneGap based mobile application development since last one year. PhoneGap based mobile application is nothing more than using HTML5 based technology stack. Most common technologies used in a PhoneGap based application are: HTML5, CSS3 and JavaScript. 

But now a days, lot of JavaScript frameworks have come which makes an application developer's life easy. Most of the time, I rely on following stack while developing a mobile hybrid application:

  • PhoneGap
  • HTML5
  • Backbone.js
  • Require.js
  • Handlebars
  • Underscore.js
  • jQuery Mobile
  • Custom Android/iOS Plugins
I would love to hear what technologies stack other developers are using for creating hybrid applications?

Working with Broken Images on Your Site

Your website is only as good as your content. Images play an important part of overall content strategy of your site. And if your web site is dependent on third party data feeds, it may create ugly content for your site because of broken images. 

Instead of shown as broken, you can replace the broken images with a "missing image" graphic that exists on your site to give better visual feedback. This can be done using a small JavaScript snipplet as follows:

     $(this).attr('src', 'known_pic.jpg');