top of page

Object-Oriented Programming and Web User Interfacing Part I

  • katiebrown0729
  • Mar 5, 2018
  • 2 min read

The goal for this week is to write more organized code by working with classes. A class is a method of creating objects, providing their initial values, and then implementing a specific behavior. This essentially breaks down complex code into simpler pieces that are easier to work with. To practice, classes will be used to develop flask web applications to control a Raspberry Pi. Mentor John has provided a series of exercises to accomplish this objective.

Exercise 1

Write a class to encapsulate the functionality of your Raspberry Pi LED contraption. It should do the following:

  1. Initialize itself by setting up all the LEDs we need to control.

  2. Provide a led_on() method to turn on individual LEDs

  3. Provide a led_off() method to turn off individual LEDs

  4. Provide a race_up() method to make the LEDs race up

  5. Provide a race_down() method to make the LEDs race down

  6. Provide a dance_randomly() method to make the LEDs dance on and off randomly

  7. Your class should also include tests when the file is run by itself using the if name == "main" trick you've learned.

Part 1 Solution

My solution is located in my flask-pi-exercises repository on Github in the pi-led-contraption file. The code begins by creating a class. The naming convention is to use the CamelCase version of the file name. In this instance, mine was called PiLedContraption.

To initialize the Raspberry Pi, a simple initialization method was used.​​ Since my pair programming partner this week was Mentor John, I got to practice some test driven development (TDD) with this code. The idea of TDD is to develop the test as the program develops. This allows for the testing of small chunks of code to minimize errors. I also noticed I was able to thoughtfully consider the ladder of code, instead of blindly churning out line after line. To do this, before fleshing out the entire initialization method, a test function is started at the bottom of the code. It is common to start the test by using the if __name__ == "__main__": line. This enables the test to only run when the program is called directly, such as from the Command Prompt. Since we are no longer inside the class, the PiLedContraption() must now be called a variable. In this example, it is is labelled thingy. The way to test the initialization is to print the type of this variable, which we will see is a class. This indicates that the initialization has been successful, and we are free to start adding more lines to our initialization method. This process can be repeated for each of the part of this exercise. A skeleton of the TDD process on Exercise 1 Parts 1 - 3 has been created to visualize the process more clearly. It is also shown below.


 
 
 

Comments


Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page