Monthly Archives: January 2018

Learning Prolog (really basic starting point)

What do I need to get started?

I’ve decided to use SWI Prolog and I’m running this on a Linux box. Why ? Somehow it felt right to use Linux for this – I don’t have a better reason than that, but either way it also runs on Windows as far as the website says.

So after I’ve downloaded and installed the SWI Prolog package using

sudo apt install swi-prolog-nox

We can run the installed application by simply typing prolog at the command line.

Note: I will refer to both the language and the command line tool as Prolog for here on.

Using Prolog

The best way to work with Prolog is by creating .pl files and loading into the Prolog interpreter, although we can enter facts into the Prolog interpreter if we prefer.

Let’s begin with some basic commands

Before we begin, we terminate commands using the dot . thus help(halt).

Loading a Prolog file

[myprogam].

enclosing the filename of your Prolog file (excluding the .pl extension) and terminating with the . will load the file myprogram.pl.

The response from Prolog will be true. basically meaning Prolog has successfully loaded the file.

Alternatively simply execute the prolog command at the command line and supply the file there, for example

prolog myprogram.pl

Exiting Prolog

Before we look at executing queries against our Prolog program, let’s look at cleanly exiting the Prolog environment.

halt.

Listing a program

After loading a program we might want to list it within the Prolog environment, we can use

listing.

Executing queries

Before we can execute a query we need a Prolog program, so here’s a simple one (we’ll discuss how it works later).

gender(leonard,male).
gender(sheldon,male).
gender(howard,male).
gender(rajesh,male).
gender(penny,female).
gender(bernadette,female).
gender(amy,female).
gender(stuart,male).
gender(leslie,female).

save this as bigbang.pl. Next from the folder where you saved this file, run swipl, load the file using [bigbang]. and the response from swipl should be true.

Now query for all male cast members using

gender(X,male).

Basically we’re saying, query for those cast members of Big Bang Theory where the gender is male. We supply a capitalised variable name (in this case X, but it could be any valid variable name). Prolog returns

X = leonard

if we want to view further evaluations of the query then we type a semi-colon and Prolog will then list the next male cast member until eventually outputting the last cast member (in this case X = stuart) terminated with a dot .

Basics of our source code

When we write something like

gender(leonard,male).

we are defining a predicate gender which takes two arguments each of which must begin with lowercase text. An uppercase is used as a variable for querying. Hence

gender(X,male).

is a query whereby we’re asking Prolog to supply the results for the variable X.

The predicate show above is also known as a fact i.e. we’ve defined the fact that leanord is male.

A Prolog program (as shown earlier) can be said to consist of multiple predicates which have one or more arguments (or clauses).

As mentioned, variables begin with an uppercase/capital letter or and underscore.

Each predicate must be terminated with a .

More on syntax

Comments

Comments are declared in the usual C/C++ way using /* … */ or for a single line using %, i.e.

/* This is multi
   line a comment */

% This is a single line comment

if or provided that

Let’s see some code where this is used first

square(X,Y) :- Y = X * X.

@ Example usage
square(3,Y).
Y = 9.

so in this case the square(X,Y) can be thought of as being true if Y is X * X. In this example we cannot use square(X, 9) to find values that when squared equal 9. We can, however, use square(X,3*3) which returns X = 3.

Assignment

We can assign values to variables using the standard = operator, you can also use is for evaluating arithmetic operations, for example

square(X,Y) :- Y =X * X.

@ or we could write

square(X,Y) :- Y is X * X.

This new version of the square predicate will fail if we try to use square(X,3*3) as Y is not instantiated.

Equality and Inequality

X == Y

is true if both X and Y are identical whereas

X \== Y

is true if X and Y are not identical.

And and Or operators

The comma is used as an AND operator, for example

abs(X,Y) :- X < 0, Y is -X
abs(X,X) :- X >= 0

In the first case we’re saying if X < 0 AND Y is -X then abs is true and using an overload (to use OO language) of the predicate we state that X equates to X if X >= 0.

The semi-colon is used for OR scenarios. For example

oneOrTwo(X) :- X = 1 ; X = 2.

Okay, not a great example but basically this says, if X = 1 OR X = 2 then the result is true otherwise its false.

I think that’s enough to a just get some very basic programs up and running.

Unit testing your Kotlin code

Let’s start with our simple little Calculator class

package com.putridparrot

class Calculator {
    fun add(a : Int, b : Int): Int{
        return a + b
    }
}

Creating our unit test(s)

Add a Kotlin class file and copy the following code into it

package com.putridparrot

import kotlin.test.assertEquals
import org.junit.Test as test

class TestCalculator() {
    @test fun testAdd() {
        val c = Calculator()

        assertEquals(c.add(5, 2), 7)
    }
}

IntelliJ will help us add the dependencies, but if you need to add them manually, select File | Project Structure and in the Project Settings | Modules we need to add JUnit4.

Obviously we’re able to import Java compiled code into our project as Kotlin and Java share the JVM bytecode, so in this instance we’re using JUnit’s Test annotation (renaming to test for this example), we’re also using Kotlin, test assertions.

That’s all there is to it.

Kotlin language basics

Hello World, again

The Kotlin HelloWorld.kt looks like this

fun main(args: Array<String>) {
    println("Hello World")
}

Which, it’s pretty self-explanatory.

fun declares a function which takes an argument named args of type Array<String>.

Within Java we have the concept of packages and the same exist within Kotlin, the code above demonstrates that we do not require a package, but let’s add one anyway

package com.putridparrot

fun main(args: Array<String>) {
    println("Hello World")
}

For the JVM to correctly run the main method Kotlin automatically creates a wrapper around our code, we can write this manually like this

package com.putridparrot

object Main {
    @JvmStatic fun main(args: Array<String>) {
        println("Hello World")
    }
}

Now this looks a little bit more like what you’re probably used to seeing in Java or C#.

Kotlin doesn’t include a static method. If we require static methods, these should simply be created at the package level (like our original main function was). However in the above we use @JvmStatic to allow our code to be called by the JVM in the expected way.

Oh, by the way, notice we do not require semi-colons to terminate a command, these are optional and more likely seen in situations where you have multiple statements on a single line (not that this is necessarily good practise!).

Importing packages

We can import packages using the import keyword, like this

// import everything in the org.junit namespace
import org.junit.*
// import a specific function
import kotlin.test.assertEquals
// import the Test annotation and name it test
import org.junit.Test as test

Classes & Objects (basics)

We’ve seen that we can create functions at the package level and we can create an object. The object is more like a singleton or static class, but ofcourse we can also create non-static/instance classes.

Let’s look at creating a simple OO example using classes.

open class Animal {
    open val name : String
        get() = ""
}

class Dog : Animal() {
    override val name : String
        get() = "Dog"
}

There’s a few things to take in from this. First off, without the open keyword a class or method is final (or sealed) and hence cannot be overridden or inherited from. The class keyword declares our new type (pretty standard stuff). The Animal contains no constructor but we can assume it really contains a default constructor which takes no arguments. When we derive Dog from Animal, we need to in essence show the constructor that is to be called (i.e. the primary or secondary constructors – we’ll cover these another time).

Kotlin includes the concept of properties, so in this case Animal declares a property name or type String which has a getter but no setter. Again it’s open so that we can override this in the Dog class.

Finally, to create an instance of a class we do not need the likes of a new keyword. We simply write

val dog = Dog()

Back to basics

As I had wanted to cover how Hello World was able to actually run and I’ve somewhat jumped ahead by describing the basics of OO within Kotlin, totally bypassing the real basics of a programming languages such as variables, assignment etc. So let’s return to the basics and look at variables.

Variables (val & var)

As you saw in the last bit of code, we created a val named dog. Like F#, val is used to create an immutable value whilst var is the mutable equivalent. Hence

val a = 123
a = 456

will fail to compile due to a being immutable, thus changing val to var will allow us to mutate state.

Notice that we did not declare the type of the variable, this was inferred, but we can include hints like this

val a : Int = 123

although in this example, the hint is superfluous as it’s quite obvious what the expected type should be. Had the intention been to store a float (for example) then we’d just change to this

val a = 123.0F

Ofcourse we have all the expected/required operators for use on a var include increment ++ etc.

Functions

We’ve already seen that to declare a function we use the fun keyword, so let’s create my usual Calculator example class and start adding some functions

class Calculator {
    fun add(a : Int, b : Int): Int {
        return a + b
    }
}

It’s all fairly obvious, although we need to explicitly declare the types on inputs and outputs in a similar way to how we declare type hints. Like F#, Kotlin uses the Unit keyword for functions which do not return a value, although this can be omitted, hence these two functions are really the same

fun output1(a : Int) {
   println(a)
}

fun output2(a : Int) : Unit {
   println(a)
}

Kotlin also supports expressions, hence we could write the above like this

fun output3(a : Int) = println(a)

Comments

In terms of commenting code, simply think Java (C#, C etc.). A single like comment prefixes the comment with // and a comment block uses /* */.

Kotlin also include documenting comments using /** */ blocks with the appropriate syntax/labels.

Generics

As we’ve already seem within our Hello World main method, Kotlin supports generics (as you’d expect as it’s a JVM based language).

Here’s a quick and dirty sample, which is pretty much as you’d expect if you’ve used generics in C# or Java.

class MyList<T> {
    val list = ArrayList<T>()

    fun put(item : T) {
        list.add(item)
    }
}

Classes, Objects & Interfaces

Let’s return to the OO world of classes. Kotlin includes classes and interfaces. Unlike C#, interfaces can contain both non-implemented and implemented methods. i.e. for a C# developer these are more like abstract classes. For a Java dev (from my understanding) these are much like Java 8 interfaces (as you’d expect from a JVM bytecode language).

Let’s look at our Animal and Dog derived class now using an interface

interface Animal {
    val name : String
        get() = ""
}

class Dog : Animal {
    override val name : String
        get() = "Dog"
}

The main differences from the previous example of this derivation is that an interface is open by default, hence we can override without the base class requiring the open keyword. Also interfaces cannot have constructors, hence no need to explicitly show that in the Dog class.

We can multiply inherit from interfaces but not classes. Only one class can be derived from.

All classes derive ultimately from Any and Any can be passed as a variable in the was we might use Object within another language.

Classes can take primary and secondary constructors, here’s how we create a primary constructor

class HttpClient(url : String) {
    val _url = url
}

We can also declare a class without a primary constructor syntax, like this, using the constructor keyword

class HttpClient {
    var _url = ""
    var _method = ""

    constructor(url: String, method: String) {
        _url = url
        _method = method
    }
}

If our class already has a primary constructor, then any other constructors are classes as secondary constructors and will need to call the primary constructor using the this keyword


class HttpClient(url : String) {
    var _url = url
    var _method = ""
    
    constructor(url: String, method: String) : this(url) {
        _url = url
        _method = method
    }
}

We can nest classes, i.e. have inner classes, but Kotlin also has the concept of companion objects which act like friend classes within C++, in that they can access private members of the outer class – let’s take a look first at a simple nested class

class A {
    class B {
        fun doSomething() {
        }
    }
}

pretty standard stuff, now let’s see what a companion object looks like

class HttpClient {
    private var _url = ""

    companion object Factory {
        fun create() : HttpClient {
            val a = HttpClient()
            a._url = "abc"
            return a
        }
    }

    val url : String
          get() = _url
}

// we can call this as follows
val a = HttpClient.Factory.create()

So far we’ve used default visibility which is public, but we can use visibility modifiers such as private, internal, protected and explicitly set to public.

Lamba’s

Kotlin includes lots of the goodies from most modern languages, including lambda’s. Here’s an example of a method that takes a lamba

fun output(p : (String) -> Unit) {
   p("Hello World")
}

// and in use
output(::println)

Notice we declare the parameter of output (with name p) as taking a String and returning Unit. To pass a function to the lambda we prefix it with :: or we could pass as an anonymous function like this

output({s -> println(s)})

Extension methods

Let C#, Kotlin supports extension methods as a way to extend class functionality without inheriting or altering the class’ code. For example

fun Animal.output() {
    println(this.name)
}

We prefix the function name with the class we’re extending (followed by a dot). So the above is obviously extending an Animal class (or subclass) and it’s implicitly got a this reference to the caller, i.e. if we called it like this

val d = Dog()
d.output()

then the this within the extension method is a reference to the Dog d.

References

This is just an overview of the basics of Kotlin, to be get us started. Check out Kotlin Reference for more in depth discussion of the topics.

Looking into Kotlin

Kotlin is one of several languages built on top of the Java JVM. I recall, a long time back (when Microsoft had a Java offering) going to a conference on Java where everyone spoke about the Java language (it’s syntax etc.) being the key thing. However one speaker, from IBM, talked about the byte code being the most important part of Java. At the time he suggested how lots of different languages would become cross-platform using the Java byte code as their compiled unit. We ofcourse see this happening more and more now (and the same ofcourse can be said for .NET and the CIL).

In this post I’m looking at one of those languages (Kotlin) that uses the JVM and can compile to byte code.

Hello World

I know it’s tedious but we’re going to write the Hello World application in Kotlin to get everything up and running.

Whilst Kotlin can be compiled using Eclipse, I have IntelliJ on my machine so will be sticking with that for all development, hence instructions around using the development environment will be specific to IntelliJ.

Let’s create our first project (from IntelliJ)

  • File | New project, select Java then locate Kotlin/JVM in the additional libraries and frameworks tree
  • Tick the check box against Kotlin/JVM) and then click Next
  • Give your project a name, HelloWorld for example
  • Click the Finish button

This created a project and not much else of interest. So now…

  • Right mouse click on the src folder in the project view
  • Select New | Kotlin File/Class
  • Name it, for example HelloWorld
  • Click the OK button

Now we have a blank Kotlin file which defaulted to the .kt extension.

Simply type (or copy and paste) the following

fun main(args: Array<String>) {
    println("Hello World")
}

Now, either create a configuration to run the code or more easily, right mouse click on the main function and select Run ‘HelloWorldKt’ which will create the configuration. If you choose to create the configuration yourself, then Main class: should be HelloWorldKt.

Note: HelloWorldKt was created by IntelliJ to wrap around the main function in a class, so that the JVM gets the correct method signature etc. So basically take the name of your file with the main method in and append Kt to it (and remove the extension).

IntelliJ (in this instance) created a folder off of our project named

out\production\<your_project_name>

Within this is the .class that your Kotlin code was compiled to.

It’s Java bytecode so we can…

Ultimately Kotlin turns source code into Java byte code, so we can easily create a JAR (for example) and execute our code. Let’s create a JAR using IntelliJ

  • In IntelliJ select File | Project Structure
  • Select Artifacts
  • Click the + button and select JAR | From modules with dependencies

Finally we need to build the JAR, so in IntelliJ select Build | Build Artifacts | Build.

Now we can execute our Hello World application from the JAR build folder, i.e.
C:\Development\HelloKotlinWorld\out\artifacts\HelloKotlinWorld_jar, by running the following at the command line

java -jar HelloKotlinWorld.jar

References

Kotlin Programming Language

Python REST service using web.py

First off, run either of the following commands from PyCharm’s Terminal window (or the command line, ofcourse).

pip install web.py
# or
easy_install web.py

From PyCharm you can also go to File | Settings, then locate Project Interpreter, for your project. Here you can press the + button, search for web.py then press the Install Package button.

If we take the Getting Started code from the web.py website and place into a .py file

import web

urls = (
    '/(.*)', 'hello'
)
app = web.application(urls, globals())


class hello:
    def GET(self, name):
        if not name:
            name = 'World'
        return 'Hello, ' + name + '!'


if __name__ == "__main__":
    app.run()

Run this and you should see the output http://0.0.0.0:8080. Now, from your preferred web browser type http://localhost:8080. The default result will be “Hello hello!” if you now type http://localhost:8080/PutridParrot you’ll see “Hello PutridParrot!”.

So what’s happening…?

urls = (
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

The web.application creates a web applications with the supplied url’s and the classes that each url maps to. In this instance all url’s map to the hello class. Now the hello class is

class hello:
    def GET(self, name):
        if not name:
            name = 'World'
        return 'Hello, ' + name + '!'

So this class takes all url’s and defines a function for each HTTP method accepts, the case maps to that sent via the browser, hence is uppercase GET (if you make a mistake here you’ll see a Method Not Allowed error from your REST service).

Our GET method takes an argument, in the case of http://localhost:8080/PutridParrot this argument is Putridparrot. If no argument is supplied the code sets name to ‘World’. You get the idea.

Finally we need to run the service application, hence the code

if __name__ == "__main__":
    app.run()

Templating

So this is all well and good, but the output is plain text, what if we are serving web pages, it’d be a lot better to be able to write HTML and embed our Python code in it, like PHP, ASP etc.

web.py includes a templating capability, so if we create a folder for our templates (let’s call it templates) off of our service source. Then add an HTML file, let’s call it index.html, now remove the HTML from this and replace with

$def with(name)

$if name:
   Hello <em>$name</em>
$else:
   Hello <em>World</em>

We’ve got the first line as $def with(name) which basically states that our template is called with the variable name which we then use within the rest of the template. The $ obviously preceeds our Python statements, but other than that it’s probably pretty obvious what’s going on here and fairly standard looking for Python.

Now change our python source to look like this

import web

urls = (
    '/(.*)', 'index'
)

render = web.template.render('templates/')

class index:
    def GET(self, name):
        return render.index(name)


if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

We need to tell web.py where our templates reside, hence the line

render = web.template.render('templates/')

Now I’ve changed the class etc. to be our index class, this wasn’t necessary as render.index maps to our HTML file name, but I figured I’d try and make things more like a real world application.

Python REST service using Bottle

From the command line/terminal run

pip install bottle

Bottle supports a style of defining routes to functions using decorators, for example

from bottle import route, run

@route('/<name>')
def index(name):
    return f"Hello {name}"

run(host='localhost', port=8080)

HTTP Method(s)

By default a @route will respond to a GET HTTP method, but we can change to POST or be more explicit using the following

@route('/<name>', method='GET')
# or
@get('/<name>')
# or POST
@route('/<name>', method='POST')
# or
@post('/<name>')

We can combine the methods using the following

@route('/<name>', method=['GET', 'POST'])

and then within the actual function we can use the request class like this

@route('/<name>', method=['GET', 'POST'])
def index(name):
    if request.method == 'POST':
       return f"POST {name}"
    elif request.method == 'GET':
       return f"GET {name}"

    return "Unexpected"

We can also declare multiple routes to a function like this

@route('/', method=['GET', 'POST'])
@route('/<name>', method=['GET', 'POST'])
def index(name = None):
    if request.method == 'POST':
       return f"POST {name}"
    else request.method == 'GET':
       return f"GET {name}"

    return "Unexpected"

How about some JSON

Obviously JSON is the current “in vogue” format for transferring data. To return JSON instead of a simple string we can use jsonpickle, start off by installing the library using

pip install jsonpickle

We’ll add an example class for our result

class Person:
    def __init__(self):
        self.firstName = "Eddie"
        self.lastName = "Van Halen"

and our code (and additional imports) looks like this

import jsonpickle

@get('/<name>')
def index(name):
    person = Person()
    return jsonpickle.encode(person, unpicklable=False)

The unpicklable=False turns off the pickle additional information to our JSON.

Errors

We can also supply web pages, data etc. based upon HTTP errors, such as 404 errors. Assuming we changed out @get (above) to

@get(‘/name/<name>’)

now URL’s such as http://localhost:8080/PutridParrot will fail to find a matching route and if we supply an error handler for a 404 then we can return some other data, i.e.

@error(404)
def error404(error):
    return "Got a 404!"

References

Bottle: Python Web Framework

Mocks and Python

In my post Unit testing in Python I covered the basics of writing unit tests within Python.

Testing also sometimes involves writing stubs and/or using mock objects.

As Python is a dynamic language which supports duck typing, we can fairly easily create stub objects, but a mocking framework can make things even simpler. However a good mock framework also gives us the ability to check whether a method was called and assert that it was not called, or was called the correct number of times.

Using MagicMock

MagicMock is a class within the unittest.mock module used to patch methods. Patching being the process of swapping out methods with our mock methods.

Here’s a Calculator class with an add method

class Calculator:
    def add(self, a, b):
        return a + b

Let’s assume we want to mock/patch the add method and ensure it’s called a certain number of times

Note: this is a contrived example as we are explicitly calling the add method, thus know how many times it’s called, but it’s a simple enough example to see how things fit together.

from unittest.mock import MagicMock 

class CalculatorTests(TestCase):

    @classmethod
    def setUpClass(cls):
        cls.c = Calculator()

    def test_add(self):
        self.c.add = MagicMock(name='add')
        self.c.add.return_value = 5

        self.assertEqual(5, self.c.add(3, 2))
        self.c.add.assert_called_once_with(3, 2)

In the above we’re using assertEqual just to demonstrate the use of the return_value. The call to the add method within the assertEqual demonstrates how we still call our patched method in the same way and then finally we use assert_called_once_with(3, 2) to assert that the add method was call once and with the expected parameters.

References

unittest.mock — mock object library