Python modules

Alright, so we've set up the data side of our chart, pulling data from the Web and parsing this data into useable objects in Python. We might think that it's now easy to plug these values into a pygal chart and call it a day, and in a way, this is correct; however, we want to be smart with our code.

Remember our discussion on how to wrap large chunks of Python code into functions; well, for a larger project, we would want to go even higher with modules; so, our first thought is: what is a module? Have we even used modules in the course of this book?

Yes, we have been using time, pygal, or urllib2 any time we use an import statement in our code. That's a module, (sometimes called a library), and more interestingly, we can make our own modules, thereby allowing us to modularize our code.

Building the main method

In many programming languages, there exists a concept of a main function that is the very first function called in a program's execution. Let's create one here called main_chartbuild by simply creating the main_chartbuild.py file in our IDE.

Next, we want to remove the get_all_rss_dates() method call found during our initial testing in LoadRssFeed and then call our get_all_rss_dates() function's dates method using import in our main_chartbuild.py file. We will then reference our method but prefix it with our import name like this:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import LoadRssFeed

#Call our 'LoadRssFeed' function.
LoadRssFeed.get_all_rss_dates()

If we rerun the script, we should see the same result as with our June post count, which is 7:

Building the main method
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.220.22