Skip to main content

This is a new website theme. Help me improve it and give your feedback (opens in a new tab).

Starting with Python

Published:

Tags:

Python University
This blog post is more than two years old. It is preserved here in the hope that it is useful to someone, but please be aware that links may be broken and that opinions expressed here may not reflect my current views. If this is a technical article, it may no longer reflect current best practice.

For absolutely ages I’ve been wanting to start to learn Python, but I always find it hard to think of a system to try and implement that will test me and force me to learn things I don’t already know. Luckily, where I’m learning Java as part of my University degree, there’s a whole load of problems to solve and all I have to do is translate them into Python.

If you’d like to start using Python as well, you can download Python from the Python download page.

The first problem I looked at solving, was the CS1016 assessment. It involved analysing temperature information from sensors.

Here is the code I produced to solve the problem:

#Using sets for all collections
from sets import Set

"""
The analyser class, containing all the samples
and the functions that operate on them.
"""
class analyser:
    """
    Add a sample to the analyser
    """
    def addSample(self,name,temprature):
        if(temprature >= 0):
            self.samples.add(sample(name,temprature))

    """
    Print out all samples in the analyser
    """
    def printSamples(self):
        for s in self.samples:
            print str(s)

    """
    Print the highest temprature in the analyser
    """
    def printHighestTemprature(self):
        high = -1 #set to a value that is lower than any sensor can be
        for s in self.samples:
            if s.temprature > high:
                high = s.temprature
        print str(high)

    """
    Update the set of faulty sensors
    """
    def updateFaulty(self):
        for s in self.samples:
            if s.temprature > 310 or s.temprature < 250:
                self.faulty.add(s)

    """
    Print out all the faulty sensors
    """
    def printFaulty(self):
        self.updateFaulty()
        for s in self.faulty:
            print str(s)

    """
    Update the set of overheated buildings
    """
    def updateOverheated(self):
        self.updateFaulty()
        for s in self.samples:
            if s not in self.faulty:
                if s.temprature >= 290:
                    self.overheated.add(s.name)

    """
    Print out all the overheated buildings
    """
    def printOverheated(self):
        self.updateOverheated()
        for b in self.overheated:
            print b

    """
    Constructor that also populates the set with sample
    samples.
    """
    def __init__(self):
        self.samples = Set()
        self.faulty = Set()
        self.overheated = Set()
        self.addSample("Fraser Noble",288)
        self.addSample("Meston",290)
        self.addSample("MacRobert",295)
        self.addSample("Fraser Noble",287)
        self.addSample("Meston",200)
        self.addSample("MacRobert",296)

"""
Sample class storing the name of the building and the
temprature.
"""
class sample:
    """
    Constructor to set the name and temprature
    """
    def __init__(self,name,temprature):
        self.name = name
        self.temprature = temprature

    """
    Similar to the toString() method in Java. This can't be
    called directly, but it is used where I've put str(s)
    in places.
    """
    def __str__(self):
        return "[" + self.name + "] " + str(self.temprature)

To use these classes, simply run the code above, create a new analyser instance, and the run the methods.

>>> a = analyser()
>>> a.printOverheated()
MacRobert
Meston
>>> a.printFaulty()
[Meston] 200
>>> a.printSamples()
[Meston] 200
[MacRobert] 296
[MacRobert] 295
[Meston] 290
[Fraser Noble] 288
[Fraser Noble] 287