Python PDF Generator
This application was initially meant to be written over the period of a few weeks, but took only a few hours. The original goal was just to make sure I'm hitting the word limit the professor wanted on discussion posts. I hit that goal pretty quickly, and this project morphed into something much larger. Nowadays it can generate PDFs on top of typical functions, and the code is a little prettier. Let's take a look at the code.
Everything else here on out is related to the graphical user interface or GUI (pronounced: Gooey). The first step to creating a graphical user interface is to define the main window. Then each individual item that is going to be added to the main window has to be "packed" to that window. Lastly, I created an array to store all of the text from the textbox. This step is probably not entirely necessary, but it makes the code easier to read and a little more straightforward.
The next function just lets me call a file dialog box to open files on demand later on when I implement the menu.
This function is basically more of the same, except it will let me save files on demand later on when I implement the menu.
The following function is the one that enables exporting to PDF. Some of the values that are hardcoded could be made changeable with variables. I just did not feel like doing that kind of work. This app is not really meant to compete with Microsoft Word.
The next series of statements simply let me create a menu for the window. The "filemenu" items are added to the menubar at the end, and with a cascading effect so that the items are listed vertically.
Now we are getting to the fun parts of the application. This next function pulls out all of the words from the textbox using a regular expression (regex) pattern that will tell it to get only letters or letters separated by hyphens. Letters separated by hyphens or that include apostrophes are counted as a single word because they are included in the pattern, and the addition sign at the end instructs the computer to keep going until it hits punctuation or spaces.
This next snippet of code merely creates a popup later on that will display the analysis performed on the text.
Between the merger of this application this function easily underwent the largest change. The first thing I do is set up separate arrays to keep track of each unique word and how many times that word appears (count array). Then I keep track of the index of each unique word and associate it with an index in the count array, which is incremented each time that word is seen. Later on I go through both of these arrays to create objects of the 'Word' class so that both the amount of times the word appears in the text, and the word itself are closely associated. By doing this I'm able to reduce the arrays down to a single array, which can be sorted using Python's built-in function. However, it is important that these values are sorted by the amount of times they appear in the text so I give the sorted function an unnamed function call telling it to sort in reverse (decreasing) order based on the count attribute.
The next three functions were covered above. They just provide popups on demand and add more stuff to the menu.
This final bit of code is the class objection definition that makes sorting the array possible. All I do is define two variables to keep track of the word and how many times it appears. Then I define a constructor that lets me assign an initial value to both of those variables. Usually classes have methods to alter the class members instead of allowing direct access, but Python doesn't really have a private scope like C++ does. So I left them out because I did not think they would add much at all.