Module 91896: Use advanced programming techniques to develop a computer program
Requirements: creating methods, functions, or procedures that use parameters and/or return values. Using additional non-core libraries. Checking input data for validity. Correctly handling expected, boundary and invalid cases
In the chapter on functions we looked at simplifying the layout of a programme by removing the complex calculation routines from the main programme and putting them into subroutines, or functions as Python prefers to call them.
This method definitely simplifies the main programme and makes it easier to read and interpret. Similarly each function, when separated from its peers, is also much easier to understand.
The drawback of the creating of functions is that the programme, although more understandable, is now much longer than it was. Recall that Listing 3 of the chapter Functions, although the longest programme we have looked at so far, does not include the validation functions, which would have made it longer still.
In order to avoid very long programmes we can break them up into modules and store each module as a separate file.
Our first module is shown in Listing 1. It is almost identical to Listing 4 in the chapter on Functions. The only difference is that it is stored in its own file: C9_Validating_Module.py
Up to now we have allowed spaces in our file names, however when we are dealing with modules we need to remove spaces and either replace them with underscores or else use CamelScript. As you can see from line 1 of Listing 1 we are using underscored in our example. Later we will explain why we need to remove spaces from the filenames.
Listing 2 below contains the functions for calculating the payroll. Again they are lifted directly out of Listing 3 of the chapter Functions. The only difference is that they are stored in their own file – C9_Payroll_Module.py
Listing 3 below is again based on the main body of the programme from Listing 3 of the chapter Functions. This, however, is where the greatest changes have occurred. The first of those changes are in lines 4 and 5. Here we use the keyword import to import the two files C9_Validating_Module and C9_Payroll_Module. Notice that when using the import keyword, we don’t need the file extension. Also import does not allow spaces in filenames, hence the reason why we removed the spaces earlier.
Next we look at the input section.
Line 8 gets the validated data for the hours for us. To validate the hours in our Functions chapter we used the function validateFloat(). We were able to call it directly there since the function and the main body of the programme were in the same file. In our case the main body and the function are in different files and so when we call the function we must precede it with the name of the file. Hence line 8 calls the function as:
floatHours=C9_Validating_Module.validateFloat(……)
The same applies to lines 9 and 10.
In the processing section, all of the functions used for calculating the various parts of the payroll are stored in the file C9_Payroll_Module. For this reason all of the names of the functions called in lines 13 – 16 have got C9_Payroll_Module in front of them. In all cases the name of the file is separated from the name of the function by a full stop sign.
Copy the code from Listing 1, Listing 2 and Listing 3 into three separate files. Ensure that you name the files according the comment on the first line of each listing.
Once all listings are copied into the files, try running the file containing code from Listing 3.
Modules in Python are a way of breaking up large programs into smaller, manageable chunks. This modular approach
not only enhances the readability and maintainability of code but also facilitates reuse.
By separating functionality into modules, developers can create libraries of functions that can be shared and reused
across multiple programs. For instance, a module containing validation functions, as demonstrated in the uploaded file,
can be used in any program requiring similar input validation.
Without modules, similar code might need to be replicated across different parts of a program or across
multiple programs. By storing common functions like validateFloat or validateInt in a module, redundant code is
eliminated.
Modify the Assignment Part 5 so that the functions in it are copied into a separate module. Next modify the main part of the programme so that it imports the module containing the functions. Also ensure that the function calls within the main programme are preceded by the name of the file containing them.
Apart from its restructuring, the programme should run exactly as before.