I promised in my previous articles about my web apps I would explain the code choices I used in developing them. That’s why I created the “My Code Choices” series where I go into detail about those decisions. In “My Code Choices: Episode One” I explain why I like reading files in Python when it comes to processing strings of text.
My Code Choices: Episode One | Why I Like Reading Files
When I taught myself Python using this Udemy course years and years ago (which I highly recommend) there was a long section about reading to and from files. I learned how (which isn’t a full list because the section is quite long):
- How to read from a text file
- Using “with” as the best practice
- Parsing data in a text file
- Writing data to a text file
This section was fun to me because I got to see my code in action, which was great for me because I like to see the results of my actions in real-time. Thus, I could write some code, write to a file, and see the results in the file by viewing it manually or using code to read it.
After getting the writing and read part down, I experimented with formatting. I wanted to know how I could get the data to look a certain way. Or I wanted to add newlines to separate each sentence. Some of this was covered in the class, while I discovered the answer to my question by reading the official Python documentation.
All of this came in handy when I built my web apps. Here’s how:
Reading Various Facts For Black History Fact Generator
When I coded Black History Fact Generator I debated on ways on how to include the facts, and then retrieve them so each one displays on the website.
I thought about using a list becuase I could iterate over it and pull out data. I think I experimented with using a list in the early design of the app, but I can’t remember. Unfortunately, I don’t have records of the early version of the app anymore.
I don’t think I even experimented with using tuples at all. It didn’t make sense to use them during my decision-making because of how I wanted to randomly retrieve each fact using the random.choice built-in function.
I finally decided on reading from a file containing all the facts. A text file (.txt) has a small file size so I didn’t have to worry about running out of disk space. Plus, I didn’t need any text formatting so that was another reason for choosing the text format. I would format the displayed text using CSS. The final reason I chose reading from a file was the ability to iterate over it like I could a list. Thus, this code came to be:
def main():
"""
Opens and reads the files/bhfg_facts.txt file, loops over the entire file to read each line, collects all the
lines as a list, and then randomly chooses a fact to display on index.html.
:return: Display a random Black History fact to the home.html page.
"""
with open("static/files/fact_list.txt", "r") as facts:
fact_list = facts.readlines()
return render_template('index.html', black_history_fact=random.choice(fact_list))
Note: I use Flask in my backend which is why I have the variable “black_history_fact” equals to the “fact_list.” The “black_history_fact” is in the index.html file, and I use it to display the random fact.
Reading Various Restaurant URLs For Where Should I Go Eat?
I also read files in my other web app, Where Should I Go Eat?, because it also displays a random restaurant when the user clicks a button:
with open("static/files/restaurant.txt", "r") as restaurant:
restaurant_list = restaurant.readlines()
There’s more going on in that web app, however, which I will discuss in a future article.
Comments