craighissett
Posts: 10
Joined: Fri Oct 03, 2014 10:12 pm

Modular Coding Practice

Tue Nov 10, 2015 5:46 pm

Good afternoon!

I am currently working on a few different Python projects. One, a desktop assistant, will be a voice-controlled device containing a Raspberry Pi connected to a USB mic, speakers and a webcam, and an Arduino Pro Micro connected to pan/tilt servos and an ultrasonic sensor. The other is a Pi-based Media player.

Both projects would benefit from having a modular coding structure where modes/features/services can be simply placed into a 'Service Folder' and used by the main code when there.
For example; the Assistant would handle voice control in it's main script, however I would use a folder to allow the addition of configurations for different environments (Standalone, attached to work PC, attached to home PC, home media centre etc). these configurations would define key words and functions that are unique to that setup.

The media centre would be very similar to that of elParaguayo's PI info screen project viewtopic.php?f=41&t=121392 , where he uses 'plugins' to add features and different screens to his project.
I would like to use the same style to add various functions (Clock, a TV-like function etc) by dropping them into a folder.
The TV function would again feature a drag-and-drop folder to add channels (specific playlists) for the main script to cycle through when the user presses up and down.

I am after some advice regarding the best way to set about coding in a modular fashion. Any insight, advice or examples would be greatly appreciated!

Thanks very much

Craig
Last edited by craighissett on Fri Nov 13, 2015 12:16 pm, edited 2 times in total.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Modular Coding Practice

Tue Nov 10, 2015 7:15 pm

Hi Craig.

You probably want to link to my project rather than my profile!

In terms of how I made that project modular was by having the script loop over a pre-determined folder and then import any modules that it finds that match my criteria.

This is probably best explained by looking at the code.

I have a function called getPlugins which is the part of the file that finds my plugins.
  • Lines 6-8 set the criteria to be satisfied. Here I'm going to restrict my search to the specified folder and I also want any plugin to have a file with the right name and a configuration file.
  • Line 16 gets the contents of the plugin older.
  • Lines 22-25 ignore anything that isn't a folder or is a folder that doesn't have the right file in it ("continue" restarts the for loop).
  • If the folder has the plugin file then I get the details I need to be able to import it later. This uses the imp module.
  • Next I try to load the config file and, if the use has said they want the plugin to be enabled then I create a dictionary of the relevant info. The key value here is "info" as this is what the script will use to import the module later.
  • Then, when I create the main instance of the InfoScreen, the list of plugins is passed as a parameter.
  • The screen then loops over the available plugins.
  • There are some tests to check if any dependencies for the plugins are met (but probably best ignore this bit for now).
  • I then load the module,get a reference to the main class object and load this into Kivy.
So, in short, the imp module is the key thing that I used for handling plugins.

I'd suggest you take a look at the code and ask any questions when there's something you don't follow. Once you think you're comfortable with what it's doing, have a go at creating a simple version yourself.

I hope this is helpful, but let me know if it isn't!

I'm almost dreading posting this and then waiting for the inevitable "oh, there's a much easier way of doing that..."
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Modular Coding Practice

Wed Nov 11, 2015 12:47 am

elParaguayo wrote:I'm almost dreading posting this and then waiting for the inevitable "oh, there's a much easier way of doing that..."
Relax, I'm sure that won't happen.

craighissett
Posts: 10
Joined: Fri Oct 03, 2014 10:12 pm

Re: Modular Coding Practice

Thu Nov 12, 2015 3:05 pm

elParaguayo wrote:Hi Craig.
You probably want to link to my project rather than my profile!
Oh balls, sorry mate - I wrote this in a hurry and didn't check what link I had actually copied ha ha!
This is a great bit of insight buddy, thank you.
I know your code is always well-structured and well-written, but I get a bit overwhelmed when trying to decipher multi-script projects.
Knowing where exactly to follow this looping process is a great advantage!
I hope an easier way doesn't come to light - your way is very smooth! :)

:D

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Modular Coding Practice

Thu Nov 12, 2015 4:18 pm

Craig, sorry - but you've linked the old project. The code I've posted is from the "new and improved" version here: viewtopic.php?f=41&t=121392

Anyway, back to your original question, if you do still want to ask anything after reading my explanations above then just post it here and I'll do my best to help.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

craighissett
Posts: 10
Joined: Fri Oct 03, 2014 10:12 pm

Re: Modular Coding Practice

Fri Nov 13, 2015 12:19 pm

elParaguayo wrote:Craig, sorry - but you've linked the old project. The code I've posted is from the "new and improved" version here: viewtopic.php?f=41&t=121392

Anyway, back to your original question, if you do still want to ask anything after reading my explanations above then just post it here and I'll do my best to help.
Ah, third time's a charm! Ha ha!
Thanks mate - I'm going to make a start over the weekend at trying to piece together something using your advice.
You have explained your code beautifully - thanks again buddy!

Return to “Python”