Jon Perry's Blog

Python – Throwaway Variables

I’ve recently been using Python in my day job, and it’s a pretty neat language – Industrial Light and Magic, Google, Facebook, etc. all use it. It is one of the most versatile languages out there, along with Java.

A particular trick that I discovered recently, is throwaway variables. They are very useful for extracting values. They are denoted using an underscore and comma _, to tell the interpreter to ignore the next set of alphanumeric symbols.

Here’s an example:

names = “Peter, Daniel, Sarah, Tom, Danielle, Amy, Sebastien”
_, _, a, _, b, c, _ = names.split(“,”)
print “{}, {}, {}”.format(a, b, c)

Pretty neat!

Exception Handling in Java

Recently I have been learning how to use the exception handling types in Java’s JDK more appropriately.

Here are some golden rules to follow, when throwing an exception type. Much of how you use checked and unchecked exception types, depends on where you want to throw an exception in the application’s stack.

IllegalArgumentException

  • Used and useful when parsing an object or string that fails.
  • A good example could be validating whether a URL or email matches a given format.

NullPointerException

  • A difficult exception to throw compared to IllegalArgumentException.
  • It’s recommended you stay away from using them.
  • You do not want to mix bad NPEs with NPEs thrown intentionally.
  • It is recommended you throw IllegalArgumentException instead.
  • Just return null when encountering a value that has not been set.

RuntimeException

  • The foundational (base) exception class in Java.
  • It is commonly used to catch unchecked exceptions.
  • It is particularly useful to throw, when processing something at a higher level, for instance, parsing with an external properties file.
  • It is common to extend a custom exception class using the RuntimeException class, since it is considered best practice to do so, unless extending a super class with more unique exception handling capabilities.

Git Commands – Help Sheet

Essential Git commands for effective software development.

Undo a commit

git reset HEAD —hard

Pulls latest changes from the repo and rebases changes from the master branch

git pull origin remote-branch —rebase master

Title

git reset –soft HEAD

Resets stage locally (undo’s any uncommitted changes)

git reset -—hard HEAD

Continues iterating through merged changes made from the rebased repo

git rebase —continue

Aborts merging rebased changes

git rebase —abort

Force pushes changes made to the local repo (usually made when a rebase has occurred when updating a branch)

git push origin branch —force

Fetches all remote branch names not in local

git fetch

Checks out a remote branch into local tracking that repo for changes

git checkout —track remote-branch

Create a new branch and switch to it

git checkout -b branch

Current local branch

git branch -l

List current, all local remote branches being tracked

git branch -a

Display URL to remote repository

git remote -v

Add New Repository URL

git remote add origin url

Change Existing Repository URL

git remote set-url origin repository-url

Delete a local branch

git branch -D branch

Delete a remote branch

git push origin –delete remote-branch

Stages a file for commit

git add path-to-file

Commits all staged files with commit message

git commit -am “message”

Stash Changes

git stash

List Stash Changes

git stash list

Apply Stash

git stash apply stash@{stashIndex}

Note: 0 is the most recent stash

Delete Stash by Index

git stash drop stash@{stashIndex}

Delete All Stashes

git stash clear

This will delete ALL your stashed changes

Spring Web Flow – using Eclipse IDE (Tutorial)

I’ve been looking online lately for tutorials on Spring Web Flow, and I didn’t find any that were succinct, memorable, or very appealing. For every tutorial that you find online, that framework may be out of date, and another may be in date; but be horrendously long to read… This article helps challenge these attributes.

For this tutorial I am using:

IDE: Eclipse (Java EE)
Build tool: Maven
Framework: Spring v4 << the important bit ... and Java, obviously.

Step 1:

Create a new project in Eclipse. You’ll need to open ‘other’ option under the New menu heading. A new window will open – select Maven -> Maven Project.

Step 2:

Setup your Spring dependencies.

When you create a new Maven project (under File -> New -> Other…), you will need to use your default workspace location, using the ‘maven-archetype-webapp’ artifact ID. Give your Maven project a name, followed by the parent package that you want to create for this project.

Open the pom.xml file under the root of your application’s directory.

With your pom.xml configuration file open, you’ll need to specify to Maven what Java libraries your application will need to build, deploy and run.

To create a Spring Web Flow application, we’ll need three components belonging to the Spring framework, those being: Spring Core, Spring MVC and Spring Web Flow. We will also need Spring Boot, Spring Web, Spring Context and Spring Autoconfigure.

The framework itself isn’t one dependency, more-over is an assortment of components, (I presume) for the purpose of modularity and performance.




org.springframework.boot
spring-boot
1.2.3.RELEASE



org.springframework
spring-web
4.1.6.RELEASE



org.springframework
spring-core
4.1.6.RELEASE



org.springframework
spring-webmvc
4.1.6.RELEASE



org.springframework
spring-webflow
1.0.6



org.springframework
spring-context
4.1.6.RELEASE



org.springframework.boot
spring-boot-autoconfigure
1.2.3.RELEASE

Figure 1. Maven dependencies – feel free to copy n’ paste!

Step 2: Write code like MVC… not ABC…

We’ve got the important stuff out of the way. We can now focus on writing our Java code!

To begin with, we’re going to write the root of our application.

Start by creating a new Java class and name it something like Application. This will allow us to compile and execute the application on our local Tomcat server.

package com.learning.spring.webflow;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String [] args) {
SpringApplication.run(Application.class, args);
}

}

This very small and simple class simply instructs the Spring boot component to execute our code against the JVM.

Next, we need to create a class which will act as the Controller (eventually) in our Spring Web Flow application.

// Spring MVC component
import org.springframework.stereotype.Controller;

// Spring Web component
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HolidayBookingFormController {

@RequestMapping("/book-holiday-date")
public String holidayChooseDates() {

}

@RequestMapping("/book-holiday-guests")
public String holidayNumberOfGuests() {

}

@RequestMapping("/book-holiday-flights")
public String holidayChooseFlights() {

}

@RequestMapping("/book-holiday-extras")
public String holidayChooseExtras() {

}

@RequestMapping("/book-holiday-insurance")
public String holidayConfirmBooking() {

}

}

For each of the methods contained within this controller, each HTTP request maps (corresponds) with the JSP that will handle the presentation of our application.

Step 3: Create the JSP’s!

New Hobby…

So… we are 25 days into the New Year. It’s been ok, and despite the cold, things are going generally, ‘ok’.

For a long time, I’ve been exploring and developing experience in different disciplines encompassing Computer Science (mainly to get a job in software engineering), but I haven’t had time to explore a topic where I can unleash my creativity, never get bored technically, and generally enjoy a hobby with a lot of pleasure attached to it. That topic, is Computer Graphics!

I can’t wait to see how things develop. I’m going to make loads of mistakes, and probably adopt some bad practices (by mistake) at some stage, but I can’t wait to show the world what I do. I’ve been exposed to art, design and technical disciplines through my life and I really relish doing something a lot of Computer Science professionals never sadly explore in their spare time alongside a full time job.

Happy New Year!

It’s been quite a while since I last posted something on this blog. In that time I have finished my MSc and started a new job in the real world. In the spirit of the New Year and Resolutions, I have recently created my own custom theme for this blog (ooh er!).

The hope behind the change in the design is to put myself into a more focused, professional mindset, so I can use this little space on the inter-web to share my technical creations in my spare time, now that I am working in the commercial world of technology 🙂

Watch this space…

Dissertation Progress – Day 67

Tasks complete today:

  1. Coded intial monitoring system component – will implement Fuzzy Logic system tomorrow.
  2. Read paper ‘The Computer for the 21st Century’.

Dissertation Progress – Day 66

Tasks complete today:

  1. Printed Project Report (draft)
  2. Printed System Evaluation questionnaire.
  3. Uncommitted changes in Web GitHub repository. Was drifting towards measuring productivity.
  4. Amended System Evaluation Questionnaire – it has been structured in categories.
  5. Topped-up printing credits – there should be sufficient credit from-here-on-out.

Dissertation Progress – Day 65

Tasks complete today:

  1. Submitted Ethical Review Application.
  2. Produced rough System Evaluation document – including questions.
  3. Soldered second temperature sensor.

Dissertation Progress – Day 64

Tasks complete today:

  1. Finished Ethics Review – including supporting document.