Posts tagged with 'hacking'.
You've stumbled across my blog: the home of my most private, innermost thoughts and reflections. And random cat videos.
$ blog engine ii: the basics24/05/2016

After another considerable hiatus (maybe I'm just not cut out for blogging), I thought I'd try and resuscitate this series of posts reflecting on my self-written blog engine. This post will cover the underlying tech, with some reasoning as to why it was chosen.

Language: JS/Node
Database: NONE
Styling: LESS
Templating: Handlebars
Blog posts: Markdown

First things first: I opted for Javascript and Node for the simple reason that this project was intended to be a bit of an experimentation into the possibilities of Node.js, especially in a desktop environment.

I originally intended to set up a MongoDB database to handle the storage of all my blog data, but after (not much) deliberation, I decided against this. My main driver for this project was simplicity, and I couldn't justify the need for a database here; the only real data I'm throwing around is a list of blog posts (with accompanying metadata) and some configuration data, so a database here seemed like overkill. The only 'problem' with this solution was that I wouldn't be able to send dynamic data to users, so all pages would have to be static. Although this adds some complexity to the engine itself, removing the middle-man meant that the end-user experience is much snappier.

Next up, structure. I chose Handlebars for the templating of my site, as it's a library I've used satisfactorily many times now, and had no reason for greener pastures. I created a very basic structure hierarchy for my site which consists of:

  • A top-level html page: contains the global elements such as the nav bar, footer, and all of the file includes.
  • Handlebars templates for individual pages: these would be loaded into the top-level page, and contain page-specific content.
  • Markdown files for the actual page content: this applies to the truly static pages (such as the homepage), and things like blog posts. Using markdown allows me to easily apply styles to the site text without the need for any complex CSS.

For the styling of my site, I originally used plain old CSS, but later added support for LESS, as it allowed me more flexibility and more importantly cut out a lot of clutter in my CSS files.

$ blog engine i: an introduction10/09/2015

I posted some time ago (1st November 2014 to be exact) about my intentions to write a Node.js blogging engine to replace the ramshackle Wordpress site I was using. I am pleased to report that it is finally (almost) done, and that it is this. After a month of stolen spare half hours, I've finally managed to get to the stage where I have something useful enough as to be usable. There's still quite a bit to do, but I'm pretty happy with where it's at so far, especially as it's my first real Node outing.

If you're interested, the code for the engine is hosted on GitHub (the website src is in another, undisclosed location).

In the next few weeks, I'll be writing about various aspects of this (hence the pt I in the name) with the hope that someone else may find this useful. For now though, I'd just like to present it in all its minimalistic glory.

Please have a look around, and keep your negative opinions to yourself

$ setting up an adapt development environment in mac os x13/01/2015

If you opt not to use the Adapt authoring tool, setting up your computer to work on the Adapt framework can be a less than trivial process; you'll need to have a number of tools installed and working before you can even view the vanilla course, and this will require you to get your hands dirty in the terminal. I've put together this guide to hopefully make this process a bit simpler, and get your shiny new courses up and running before you know it.

This guide assumes you are completely new to web development, and therefore will need to install everything from scratch. If this is not the case, some of the steps may be inapplicable.

Install Xcode using the App Store

Grab Xcode from the App Store, and fire it up when installed, agreeing to all terms and conditions. If this isn't done, building the node source may fail later.

You'll also need the command line tools to be able to pull down the Adapt repositories using git. To get these, type git in a terminal window, and follow the instructions.

Install node and node package manager

This is by far the trickiest part of the installation process, largely due to the issues surrounding file permissions you can easily run into. After several less than straightforward installs, the following instructions are my recommended method.

A note on node/npm installation paths
By default, these are stored in /usr/local/, which requires root access to write to. There are three routes around this:

  1. Run every npm install as root (definitely not a good idea, as it could result in something like: sudo npm install trojan-disguised-as-module).
  2. Change the owner of the contents of /usr/local/ to your user account (if you're using a shared computer, this is not advisable).
  3. Change the home of global modules to somewhere you do have permission. This is the option I use below.

Create (and change to) a directory in your home folder to store your globally accessible node modules, and add this to your system path variable:

mkdir ~/local/ && cd ~/local/ 'export PATH=$HOME/local/bin:$PATH' >> ~/.bash_profile

Download and extract the latest node release:

mkdir ~/node-latest-install && cd ~/node-latest-install curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1

Install node to your local folder:

./configure --prefix=~/local make install

When this has finished, close the terminal, reopen and try the node and npm commands to confirm everything installed as expected (after running the node shell, you'll need to exit pressing ctrl+c twice).

Install the required command line interfaces

Finally, you'll need to install both the adapt-cli and the grunt-cli globally. To do this:

npm install grunt-cli -g npm install adapt-cli -g

Test out your installation

Now let's try downloading and building the vanilla adapt build:

Clone the adapt_framework from github. (This assumes you have ssh set up, if not check out github for a nice guide):

git clone adapt_framework git@github.com:adaptlearning/adapt_framework.git && cd adapt_framework`

Install the relevant components:

adapt install

Install the required npm modules (if everything has gone to plan, you won't get any permissions errors here):

npm install

Build, and run the course using grunt:

grunt build grunt server

Provided the above steps went according to plan, you should now have the example Adapt course open in your default browser, ready for development!

$ just when you thought it was safe to go back into the water01/11/2014

After a considerable hiatus, I thought it about time I started to give this blog a bit of love again.

Over the next few weeks/months, I’m going to be diving head-first into learning Node.js, and thought that I’d attempt to build my own simple blogging engine as a nice training exercise. I’ve been meaning to replace the Wordpress site I currently use for a while (which is a bit bloated for my needs, and is always a massive headache to re-theme any time I decide to update my website), so what better way than to build my own?!

Wish me luck.

$ python challenge: insertion sort21/06/2012

Insertion sort is a fairly simple and 'clean' (implementation-wise) sorting algorithm. It works in much the same way that we might sort an unordered list (a shuffled deck of cards, for example): each item in the list is taken in turn, and compared with the item that comes before it in the list. If the preceding item is larger, the items are swapped. The element (in it's new position) is then compared with the element before it, swapping the two if it's larger. This process is continued until the item is positioned in such a way that the preceding element is smaller in value, or until the element is the first item in the queue. This process is repeated for all elements in the list.

The benefit to this algorithm is that each element is placed in its final resting position. It's also very quick when sorting small lists (some good quicksort implementations actually use insertion sort to sort lists below a certain length).

You can read more about insertion sort on Wikipedia.

# sort the list def insertionsort(list): for index in range(1, len(list)): item = list[index] holeIndex = index-1 while holeIndex >= 0 and list[holeIndex] > item: list[holeIndex+1] = list[holeIndex] holeIndex -= 1 list[holeIndex+1] = item
$ python challenge: bubblesort12/06/2012

Bubblesort was the lucky algorithm chosen for the Python Challenge: Part III. Pretty much useless in terms of it's practicality (due to it being so slow - it's average-case complexity is O(n2)), bubblesort is mainly used for educational purposes to illustrate list sorting.

It's pretty simple in how it works: the algorithm iterates through the list to be sorted and compares pairs of adjacent elements. The larger of the pair is always moved to the right, which results in the list being sorted in ascending order.

# swaps the two elements in the passed list def swap(list, index1, index2): index1Val = list[index1] index2Val = list[index2] list[index1] = index2Val list[index2] = index1Val # sort the list def bubblesort(list): while True: swapped = False for index in range(len(list)): if index == 0: continue if list[index-1] > list[index]: swap(list, index-1, index) swapped = True if not swapped: break
$ python challenge: in-place quicksort01/06/2012

Continuing with the Python challenge, the next step was to implement the in-place version of quicksort. Slightly more complex than the simple quicksort, this version uses less space by sorting the list in place. It keeps the same time complexity (O(n log n) average case)

# swaps the two elements in the passed list def swap(list, index1, index2): index1Val = list[index1] index2Val = list[index2] list[index1] = index2Val list[index2] = index1Val # partitions and sorts the list at the specified points def partition(list, left, right, pivotIndex): pivotVal = list[pivotIndex] swap(list, pivotIndex, right) storeIndex = left for index in range(left, right): if list[index] < pivotVal: swap(list, index, storeIndex) storeIndex = storeIndex + 1 swap(list, storeIndex, right) return storeIndex # sort the list def quicksort(list, left, right): if left < right: # get a pivot index pivotIndex = Random.getRandomInt(len(listToSort)) while pivotIndex < left or pivotIndex > right: pivotIndex = Random.getRandomInt(len(listToSort)) pivotNewIndex = partition(list, left, right, pivotIndex) # Recursively sort elements on each side quicksort(list, left, pivotNewIndex - 1) quicksort(list, pivotNewIndex + 1, right)
$ python challenge: simple quicksort30/05/2012

I've been meaning to do a bit of hacking in Python for a while now, but haven't really had the free time what with the mountain of uni work. Now I'm all done and nearly a graduate, I thought it'd be a good time to give Python a go.

In the attempt of killing two birds with one stone, I thought it'd be quite an interesting challenge to work through my big fat book of algorithms and implement them in Python, because hey, you can never implement too many algorithms right?

I thought I'd start of gently, so the first one I tackled was the simple version Quicksort:

def quicksort(toSort): if len(toSort) <= 1: return toSort pivotIndex = len(toSort)/2 pivot = toSort[pivotIndex] toSort.pop(pivotIndex) # partition the list lower = [] higher = [] for i in range(len(toSort)): item = toSort[i] if item <= pivot: lower.append(item) else: higher.append(item) return (quicksort(lower) + [pivot] + quicksort(higher)) sorted = quicksort(list)

I'm pretty impressed with Python so far, albeit only having played around for a short time. I like the fact that curly brackets are out; the whitespace-sensitivity makes for much cleaner and easier-to-read code than I'm used to. I also think that the syntax in general encourages much leaner code; I remember my implementation of the same algorithm in Java was many more lines.

Next stop: in-place quicksort.

$ cogito17/05/2012

After 6 months of hard work, I've finally finished my final year university project, which I'm pleased to say turned out pretty well.

The main aim of my project was to experiment with academic AI techniques in a game context. It's pretty surprising that games aren't more adventurous in terms of their AI, which still takes a 'smoke and mirrors' approach; providing an illusion of intelligence rather than actually creating intelligent systems. Many of the techniques and algorithms used today date back 30 years or more. With my project, I wanted to introduce some more modern techniques (in particular machine learning).

The main inspiration for my game was the old school platform-puzzler game Lemmings. My intention was to take the Lemmings concept, which requires the player to safely guide a number of Lemmings across a treacherous 2D level, and remove the human element. I wanted to allow the characters to explore for themselves and learn how best to cross the level, without any human interaction.

The main learning method used in the game is reinforcement learning, which uses a 'carrot and stick' method to reward the characters for 'good' behaviour, and punishing them 'bad' behaviour.

I developed my game for iOS (specifically the iPhone), and it turned out pretty well. The Lemmings actually work by using a number of 'episodes' or lives to actually learn the level. Movement is completely randomised during these episodes. When they finish the learning stage, they have one chance to try and reach the exit safely using their gained knowledge. You can see a couple of videos of my project in action:

The full source code is available on Github, and should be appearing on the App Store very soon.

$ optimise10/03/2012
Avoiding premature optimization doesn't only apply to writing code, it's a good way to approach life.