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() {
         @Override
         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) {
                fileNames.add(path.toString());
            }
        } 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) {
                fileNames.add(path.toString());
            }
        } 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);
        is.close();

        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.setTransform(at);

          graphics.setPaint(Color.white);
          graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
          slide[i].draw(graphics);
          FileOutputStream out = new FileOutputStream("slide-" + (i + 1) + ".png");
          javax.imageio.ImageIO.write(img, "png", out);
          out.close();
        }
    }
}

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()
.flatMap(Product::getOrders)
.mapToInt(Order::getPrice)
.sum();
}

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

public int getTotalPrice() {
return products.parallelStream()
.flatMap(Product::getOrders)
.mapToInt(Order::getPrice)
.sum();
}