From script to package
One common way to begin writing a package is to start with code you have already written as a script. At the time you first write this code, you may not realize how useful it might be in other places.
If you did the prerequisite course, in one exercise you wrote a script to count the number of times cats were mentioned in the book Alice in Wonderland.
In this exercise, you'll copy from that script to make a generalized function you can use on any text file for any words. This will be the first function in a new library.
Note: You can open/close the folder and files overview by clicking on the icon on the left side of the editor highlighted in red.
This exercise is part of the course
Developing Python Packages
Exercise instructions
- Copy the code from
myscript.py
into thetextanalysis.py
file intextanalysis/
folder. - Modify
textanalysis.py
to create the functioncount_words(filepath, words_list)
which opens the text filefilepath
, and returns the number of times the words inwords_list
appear.
Hands-on interactive exercise
Turn theory into action with one of our interactive exercises
