GPS Event [6] – Refactoring

Time to clean up a bit. As new services are added this code is becoming bulky. I was writing it bottom-up, code as you go — no wonder. Time to stop coding and reflect on it, before it bloats out of control.

The first task is to identify code that changes with the addition of a service.

Item Description Comment
1 Collecting GPS data over the serial port Does not change.
2 Parsing collected GPS data Does not change.
3 Template formats A new template is created each time a service is added.
4 Search and replace on a template Does not change — it depends on the GPS data set.
5 Service invocation, data transfer Different for each service; code grows with each new service.
6 Main program Does not change. Read serial port, parse, fetch a template, s/r through the template, send to service, wait, loop.

 

It should be noted that — at present — multiple templates are loaded in memory, but only one is needed.  This will have to be reworked to load code and a template for only one service at a time.

So here is the task list:

  • Extract GPS data collection and parsing into a separate module or class.
  • Each template gets its own module with generic template() function to return the string.
  • Put each service in its own module with generic send()  function.
  • Load required template and service modules dynamically into the main module.
  • Try to follow Python naming conventions for function names, classes etc.
  • Use consistent naming convention for module files.
  • And first, before anything else, write a test set (unit test) to deal with bugs introduced while refactoring.

Time goes by.

Ok, done; these are new (modified) modules, after refactoring.

Services:

Templates:

Authorization data:

Other:

 

Lots of files, but keep in mind that only one set (service  + template + authorization) is loaded at a time. All files reside in the same directory. I do not list test units here, essentially each code module has a matching test_.py. You may notice that some services are not implemented yet, the modules are simply place-holders. This is one of the benefits of refactoring, it does not just clean up the code — it includes redesign to allow for seamless future changes.

With this structure in place I should be ready for the next fun episode — twitter the GPS.

To be continued.

Leave a comment