Ryan Harrison My blog, portfolio and technology related ramblings

Python - Iterate over a list with indexes

TL;DR

for (index, value) in enumerate(values):  
	print "The value at index {0} = {1}".format(index, value)

In Python the typical way to iterate over a loop is to use the conventional foreach loop, as lists expose iterators:

values = ["Foo", "Bar", "Baz", "Qux"]

for value in values:  
	print value

Output:  
Foo  
Bar  
Baz  
Qux  

In a lot of cases this is all that’s needed, however in other circumstances we may need access to the index that each element is at inside the list - most of the time in order to perform some kind of check on the next or previous values in the list. The conventional for loop above of course simply gives us the values inside the list, not the indexes. We could set up a counter variable which increments each time around the loop, yet this seems clumsy. We are increasing the scope of a variable that should ideally be kept local to the loop.

Another popular solution is to use the range function, which returns an iterator of values from zero to the number we specify as a parameter. By creating a list of numbers from zero to the size of the list, we can then use each element as an index in order to extract each value:

 
for i in range(len(values)):  
	print "The value at index {0} = {1}".format(i, values[i])

Output:  
The value at index 0 = Foo  
The value at index 1 = Bar  
The value at index 2 = Baz  
The value at index 3 = Qux  

Although this gets us our desired output (in this simple example anyway), it again seems quite a clunky way to perform such as simple task in Python. We have to refer to each value as values[i], or otherwise set up a new local variable. Luckily however, as ever, this is a much neater way of doing this in Python.

One of the built-in functions that Python provides is the enumerate function. This handy function gives us an iterator that returns a tuple containing a count (which by default starts from zero) and the values obtained from iterating over the sequence. By simply running through the tuples returned from the iterator provided by this function, we get access to both the indexes and the corresponding values:

 
for (index, value) in enumerate(values):  
	print "The value at index {0} = {1}".format(index, value)

Output:  
The value at index 0 = Foo  
The value at index 1 = Bar  
The value at index 2 = Baz  
The value at index 3 = Qux  

As you can see this is much neater and more efficient than the other solutions. There is no need for extra variables - everything is provided to us in the loop declaration.

Read More

Maven - Adding Dependencies

Prerequisites -

It is assumed that before starting this tutorial, you have read and understand the first part of this series which includes an overview of Maven and creating a simple Maven project.

What are dependencies?

In this context, dependencies are any modules of code that your application requires in order to function properly. In this case, this mainly consists of large public libraries that are often needed for Java programs. Such examples include logging frameworks such as Log4J, unit testing frameworks such as JUnit, or a whole multitude of other frameworks and libraries such as Spring for dependency injection, Hibernate for object relational mapping and Struts for MVC web applications. In this example we will be using Guava which is a commonly used collections library from Google.

The dependency capabilities of Maven don’t just end with managing dependencies for a single a project however. Maven really starts to excel when you start getting into dealing with multi-module projects and applications that consist of maybe tens or even hundreds of modules - each of which may depend on a selection of the others.

Why do we need Maven for this?

At this point you may be asking why we even need Maven to handle this at all. You may be used to adding libraries to your project by visiting their website and downloading the latest release. You then copy the relevant .jar files into your project and modify the classpath so that the compiler can see and use them. This method however does present quite a few potential problems that Maven tries to fix.

Perhaps the biggest potential problem is handling the dependencies of those libraries themselves (the dependencies of the dependencies if you will). You’ve probably been there at some point. You download a library and find out that it itself needs a few other libraries or frameworks in order to operate. You then have to try and locate those files just to find that each of those has a whole set of other dependencies. After a while you end up with quite a headache on your hands. This is perhaps not readily apparent on smaller libraries, but when you start dealing with the big frameworks (think Spring and Hibernate etc), this can start to become a a real problem - and a big waste of time.

Another common problem with libraries is making sure you are using the correct version. Maybe one library needs a specific version of another in order to function properly. You then start having to delve deeper into the relevant websites in order to try and track down the right version. You then have to do it all over again if that library depends on others. Another example is when migrating to a new versions. Particularly with new JDK releases, you end up at some point updating to new supporting versions of the libraries that you’re using. You then end up going through the same problem as described above.

There are a whole load of other big problems you can run into with larger applications, but these examples alone are hopefully reason enough to start doing something about it - using Maven.

Maven tries to solve these problems by managing a repository that contains a whole load of libraries and frameworks that you could use in your application (there are also local repositories often for use with multi-module projects). When we want to start using a certain library, we can simply tell Maven what it is and what version we want to use. As long as the dependency is in the repository somewhere, Maven will run off, collect all the relevant files for that specific version and add them to your project for you - no manual work needed at all. In addition Maven also stores all the dependencies that library needs to operate. When you tell Maven about the new library, it will not only download the files for that library, but it will also collect each and every dependency - all automatically. When you want to update or change versions, simply tell Maven about it and everything will be sorted for you.

Collecting dependency information -

In order to add a new dependency to our project with Maven, we first need to collect the relevant information about that dependency in order for Maven to be able to correctly identify it in the repository. You can either memorise this information, or preferably use one of the many website that allow you to search for a library in the Maven repository. In this case we will be using the popular site http://mvnrepository.com/

In this example we will be adding the Guava collections library to our project. In order to get the information simply search for 'guava' in the search-bar. A list of search results will appear containing all the relevant artifacts in the repository. In this case (and in most cases), the top result is what we are looking for. When you click on the link, you will be presented with a page allowing you to select which version of the library to use. In this case we will be using the most recent release - 17.0 as of writing. By clicking on the version you will be presented with information about that particular version - including any of its dependencies and what other artifacts depend on it. What we are interested in however is the small snippet of XML near the top that defines the dependency to Maven. Snippets for various other dependency management systems are also provided by the site.

Adding the dependency to the project -

Here is the XML snippet we obtained from the repository explorer:

  
<dependency>  
    <groupId>com.google.guava</groupId>  
    <artifactId>guava</artifactId>  
    <version>17.0</version>  
</dependency>  

As you can see it’s really quite simple. It merely defines the groupId and artifactId of that particular dependency - much like we did when we created our project in the first tutorial. The groupId can be thought of as a kind of container which can potentially hold many projects. The artifactId defines the specific artifact in the container that we want to access.

In order to tell Maven about this new dependency, we simply add it to the existing pom.xml for your project. The dependency tag goes inside a container dependencies tag - meaning that we can have as many dependencies as we like in our project. The edited pom.xml looks like this:

 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.ryanharrison.app</groupId>  
    <artifactId>MavenTutorial</artifactId>  
    <version>1.0-SNAPSHOT</version><packaging>jar</packaging> 

    <name>MavenTutorial</name>  
    <url>http://maven.apache.org</url><properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> 

    <dependencies>  
        <dependency>  
            <groupId>com.google.guava</groupId>  
            <artifactId>guava</artifactId>  
            <version>17.0</version>  
        </dependency>  
        <dependency>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
            <version>3.8.1</version>  
            <scope>test</scope>  
        </dependency>  
    </dependencies>
</project>

As you may have seen, there is already a dependency inside the pom.xml file - JUnit. This has been added automatically by the archetype we chose when the project was created.

And that’s it! Maven will now know about that dependency when we compile our project, whereby the library will be downloaded and added to the classpath automatically. But before we compile, we first need some code to use the library in order to make sure everything has worked properly.

In the main App.java file, that currently has the Hello World example from the last tutorial, add the following code in order to use the Guava library:

package net.ryanharrison.app;

import com.google.common.base.Joiner;

public class App {  
    public static void main(String[] args) {  
        String[] words = {"Hello", "World", "using", "the", "Guava", "library"};  
        String joined = Joiner.on(" ").join(words);  
        System.out.println(joined);  
    }  
}

In this example we make use of the Joiner class to join together an array of words with a space delimiter.

Read More

Maven - Installation and Getting Started

What is Maven?

From their page:

“a tool that can now be used for building and managing any Java-based project. We hope that we have created something that will make the day-to-day work of Java developers easier and generally help with the comprehension of any Java-based project.”

Essentially Maven makes it much easier for developers to manage their Java projects. Where it really shines is with the build process (particularly when handling dependencies, but we’ll get onto that in a later tutorial) -

“Maven allows a project to build using its project object model (POM) and a set of plugins that are shared by all projects using Maven, providing a uniform build system. Once you familiarize yourself with how one Maven project builds you automatically know how all Maven projects build saving you immense amounts of time when trying to navigate many projects.”

Sounds pretty cool huh? In addition, it turns out that Maven is really easy to install and get started with using on all your Java projects.

Installation -

First step is to download the current Maven build from their website http://maven.apache.org/download.html. Pick whichever archive format you prefer and download and extract the most recent release to somewhere on your machine.

Windows -

Now if you are on Windows, all we have to do is set up some environment variables and we are good to go.

Go to your System properties page -> Advanced system settings -> Advanced -> Environment Variables

Add the following variables:

  • JAVA_HOME -> Point to your Java installation directory e.g C:\Program Files\Java\jdk1.7.0_51

  • MAVEN_HOME -> Point to your extracted Maven directory e.g C:\Users\Name\Java\apache-maven-3.2.1

  • PATH -> Locate the existing PATH variable and Edit it. At the end add the this: ;%MAVEN_HOME%\bin; (make sure all the entries are separated by one semi-colon).

That’s all you need to do. You should now be ready to get started with using Maven.

Unix -

Run the command sudo apt-get install maven and you should be good to go straight away! Skip to the verify your installation stage to make sure you are good to go.

Verifying your installation -

Once you have followed the installation steps depending on on your operating system, open up a command line/terminal window and enter the following command to make sure everything is working correctly:

mvn -version

You should see some output that looks something like this:

> C:\>mvn -version  
> Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T17:37:5  
> 2+00:00)  
> Maven home: C:\Users\Name\Java\apache-maven-3.2.1\bin\..  
> Java version: 1.7.0_51, vendor: Oracle Corporation  
> Java home: C:\Program Files\Java\jdk1.7.0_51\jre  
> Default locale: en_GB, platform encoding: Cp1252  
> OS name: "windows 8", version: "6.2", arch: "amd64", family: "windows"

If not then you may need to try the above steps again or search online for solutions.

If on the other hand you did get output that looked like the above, then that’s it. You are ready to use Maven in your projects!

Creating a Project

To create a project with Maven, you make use of its archetype mechanism. This is basically a large collection of pre-made project templates that you can base your project from. There are a ton of different types of archetypes available so you there should already be one that matches your requirements. In this tutorial we will be using a quickstart archetype which is one of the simpler models of a basic Java application.

To construct this project first open up a terminal or command prompt window and navigate to the location on your machine where you want your project to be stored. In this example I am using a ‘Projects’ folder sitting on the root C: drive directory.

Read More

Use the Tomcat 8 server in Eclipse

The current version of Eclipse (Kepler as of writing) doesn’t natively support version 8 of the Tomcat server. Although support will be added eventually, this is not likely to be until the release of Eclipse Luna in late June 2014. In the meantime, to add support you can download and install the latest version of the WTP (Web Tools Platform) into your current Eclipse environment.

  1. Go to the WTP Downloads Page and select the link to the latest release (easiest to look at the most recent build date). As of writing the latest version is 3.6.0.
  2. Under the ‘Traditional Zip Files’ section, download the .zip file for Web App Developers.
  3. Extract the archive somewhere and copy all of the files in the ‘features’ and ‘plugins’ directories’ into the corresponding directories in your Eclipse folder (overwriting the existing files).
  4. That’s it. Open up Eclipse and you should see the option for a Tomcat 8 server.

Tomcat 8 Server in Eclipse

Read More

Git - Change most recent commit message

When using any version control system it’s inevitable that you’re going to make some kind of mistake when writing out your commit messages. Luckily Git makes it extremely simple to change the message of your most recent commit. Simply use the amend command:

git commit --amend -m "new message"

For example given this test repository with three previous commits:

Before ammended message

To change the message of the most recent commit - in this case “Third commit”, you can use git commit --amend -m "This is a modified message"

This gives the updated log messages:

After ammended message

Read More