Updating a File from Multiple Threads in Python

Use locking to properly share resources across multiple threads

“You’re not obligated to win. You’re obligated to keep trying. To the best you can do everyday.” ― Jason Mraz

1. Introduction

Resource management in multi-threaded programs is a tricky situation. Say, for example, you have a counter variable which is read and modified by multiple threads. If you do not exercise proper care, it is possible that inconsistent values are read and/or propagated. This leads to subtle and hard-to-debug errors.

Continue reading “Updating a File from Multiple Threads in Python”

Python Threading Tutorial

A beginner’s guide to threading in python

“The truth is like salt. Men want to taste a little, but too much makes everyone sick.” ― Joe Abercrombie, The Heroes

1. Introduction

Let us learn about python threading. Threading refers to executing a task in a separate thread of control, freeing up the main thread for other activities. It is very useful for executing time-consuming tasks without holding up the rest of the program. Typical applications include servers which handle each request in a thread and programs with a user-interface which execute complex tasks in a background thread.

Continue reading “Python Threading Tutorial”

Proper Cleanup of Resources in Python using the With Statement

The python with statement is damn useful! Learn the intricacies of the with statement.

“Accept who you are. Unless you’re a serial killer.” ― Ellen DeGeneres, Seriously… I’m Kidding

1. Introduction

Resource acquisition and cleanup is a very crucial aspect of programs. Resources need to be properly cleaned and released as soon as their usage is complete. Failing which scare resources might not be available to other programs running on the same machine. This is especially true for long-running programs such as servers and daemons.

Continue reading “Proper Cleanup of Resources in Python using the With Statement”

Python Class Methods – Implementing Ordered Dictionary

Use python class special methods to add idiomatic behavior to your class.

“It is the time you have wasted for your rose that makes your rose so important.” ― Antoine de Saint-Exupéry, The Little Prince

1. Introduction

The python class system supports a number of special methods which allows class implementations to mimic built-in python objects objects. These special methods are invoked by the python run-time system in a way similar to the built-in classes. For example, the method __len__() can be defined by a user class which will be invoked when the client code attempts to find the length of the object using len(obj).

Continue reading “Python Class Methods – Implementing Ordered Dictionary”

Nested or Inner Classes in Python

Learn how to declare and use nested classes in python

“There is nothing either good or bad, but thinking makes it so.” ― William Shakespeare, Hamlet

1. Introduction

A nested or inner class is contained within another class. This could be for reasons of encapsulation, where the inner class is not useful by itself. Continue reading “Nested or Inner Classes in Python”

An Introduction to Python Classes

Here is a beginner’s introduction to classes in python

“Sometimes it’s worth lingering on the journey for a while before getting to the destination.” ― Richelle Mead, The Indigo Spell

1. Introduction

The concept of a class is the core of Object Oriented Programming. It defines a blue-print for constructing objects. It is a plan of how an object is laid out, including its data members, instance methods, class methods and inheritance hierarchy. Python supports object oriented programming and classes, although with certain differences from other languages like C++ and Java. In this article, we shall delve into python classes and learn how they work.

Continue reading “An Introduction to Python Classes”

Java 8 Streams Map Examples

Convert one value to another in a Streams Operation

“Always and never are two words you should always remember never to use. ” ― Wendell Johnson

1. Introduction

Do you know about Java 8 Streams?

Java 8 streams is a very powerful facility for working with collections. What formerly required multiple lines of code and loops can now be accomplished with a single line of code. A part of Java 8 streams is the map() facility which can map one value to another in a stream. In this article, let us examine some common use cases where map() is useful.

Continue reading “Java 8 Streams Map Examples”

Add Javascript Scripting to your Java Application

Extend your application’s usability with a scripting interface

“Success is not how high you have climbed, but how you make a positive difference to the world.” ― Roy T. Bennett, The Light in the Heart

1. Introduction

A non-trivial program of any type has a reasonably complex interface and probably supports multiple options with several ways of controlling input and output. Invoking such a program can be done via the command line, but here we run into limitations of what you can do with the command line. You can probably add options for all supported settings and multiple modes for the program to run.

Continue reading “Add Javascript Scripting to your Java Application”

How to use Selenium for Driving the Chrome Browser

Learn how to automate tasks on the web using Selenium

“Indifference and neglect often do much more damage than outright dislike.” ― J.K. Rowling, Harry Potter and the Order of the Phoenix

1. Introduction

Selenium is a handy tool for controlling a web browser (such as Google Chrome) through a program. It allows you to automate tasks on a website by using the browser much the same way a human user would. Such automation can be useful for a variety of tasks including: regression testing of web applications, data extraction and more. In this article, we will learn how to use Selenium to drive the Chrome Browser.

Continue reading “How to use Selenium for Driving the Chrome Browser”

How to Search XML Using XQuery from Java

Use XQuery for more powerful XML search than is possible with XPath

“I guess there are never enough books.” ― John Steinbeck, A John Steinbeck Encyclopedia

1. Introduction

XPath offers an easy way to search an XML document using java. It is much more convenient than having to crawl through the document tree using the DOM API. XQuery builds on XPath and provides an SQL-like language for querying XML documents. It is also capable of updating and modifying the XML document, something which XPath cannot do. In this article, we present a beginner’s introduction to XQuery and how to use it from java.

Continue reading “How to Search XML Using XQuery from Java”