[Week 6] IO, Refactoring, Recursion
This week was something new for me, something I hadn’t really worked with in code before. Because of this, while is did take me a bit longer to grasp the exercises, I was able to learn a lot of new techniques and syntax.
To sum up what I learnt about working with files I compiled a short summary based on notes from tutorial and online that I used throughout my code (for future me who will forget):
Example from tutorial of a working code utilising these functions:
Another thing we went over was refactoring. A concept I was very well aware of after strict marking on code craftsmanship last year. It involves eliminating any repetition, inappropriate names, bloated functions, unnecessarily complicated code and non useful comments. An easy way to remove things like repetition is to create separate functions for repeated processes.
Next new topic to explore was APIs and the son of J — JSON. This I find a bit more difficult to grasp. JSON is a data interchange format which can take Python data and convert it to string representations. These were both applied in this weeks exercises but I still think I could use a bit more exposure to them before I am fully comfortable using them.
WEEK4 Exercises:
This week took me a bit longer to actually start the exercise due to lack of knowledge. The first exercise was relatively straight forward and just tested whether we understood reading files and file paths. One thing it required us to look into was the strip() function which was necessary to remove the blank line in the file we were reading from.
The next activity was about reading a json file and extracting information we required from it in the form of a dictionary. Wasn’t too bad, however did get a bit confusing working with so many nested lists/dictionaries.
The pyramid was very similar to previous activities in the course so the only new thing was requesting information from a URL in which we used the basic structure of: response = requests.get(URL).text — this was modified to change the URL ending so that I could get multiple length words.
wunderground was like the previous json activity but involved using an API key to get our json information. I did get a bit stuck on this only due to multiple values for the information we had to find — which of course I used the wrong one. Also, I didn’t update the tests so one value was always showing to be wrong.
The last activity I didn’t really know how to approach it since we hadn’t looked deeply at analysing data within a file. Because of this my friend stack overflow was my guide to success. When looking at how to count occurrences in a file there were multiple ways but the two I found most understandable was using a loop (similar to the one in the table above) and then .count(). the count function was clearly a winner for me due to it’s simplicity.