Having finished the class ModelFeed, we need to write a function that initiates an object of this class. This will enable us to retrieve data by feed name. As the links will form the body of the HTML document, let's call this function formBody. The code looks like this:
def formBody(feedname): feed = ModelFeed() feedurl = feed.feeddata(feedname) body = feed.getlinks(feedurl) return body
If you have read my tutorials on functions and classes, this is straightforward. After declaring that 'formBody' requires the variable feedname, we initiate an object feed of class ModelFeed.
We then pass feedname as an argument to the method feeddata of the new object. This gives us feedurl, the address of the feed.
The address is then passed to the method getlinks. This method returns the headlines and links in HTML format and may thus be assigned to body and returned.
You may ask why we do not simply put all of this into main(). After all, we are only using it one time. The more that one can abstract a problem in software development, the more modular the design is. Modular design makes it easier to troubleshoot the program, to maintain the program, and to extend the program. If you ever want to do something else with the returned data from getlinks, you do need to gut main() and write the function. What's more, because it is Python, you can call all the classes and functions defined here from other Python programs. If you are unsure how to do that, see my tutorials on importing modules.