LPTHW 15, 16, 17 and a cooking cliffhanger

Today’s exercises have to do with files: reading, writing, and manipulating them.

Exercise 15: Reading files

ex15pyzExercise 15 was pretty approachable. Essentially, I wrote a little script that opens a text file and prints it. I wasn’t content to use Zed’s meaningless sentences, so I grabbed some Mary Wollstonecraft Shelley to class the joint up a bit.

In the study drills, I alternately deleted lines where I used raw_input and *only* used raw_input to see what happens in either case. I also started Python and tried use open as directed, but I wasn’t able to get it work, even with my amazing friend Kyle’s help. I’m probably missing something really obvious. He gave me some great tips that I plan to try tomorrow.

Something I think Zed does really well is remind us how helpful it can be to write comments. Comments help others read your code, but as a learner, it’s also helpful for me to write them so that I know what’s going on… even it sometimes it gets kinda long, as in the case of exercise 15:

ex15py

Still, worth it.

Exercise 16: Reading and writing files

ex16py

Exercise 16 was pretty straightforward as well. Zed introduces a list of commands to remember:

commandstoremember

Exercise 17: More files

ex17py

Exercise 17’s study drill questions have to do with making the code cleaner, shorter, and easier to read. When Zed said that he could reduce the entire script to one line of code, I balked… and for the first time, I got frustrated. Indignant, even! Though I could definitely see ways to trim it down, I didn’t see the utility in reducing it to one line. I shared this with a new friend at the PyAtl meetup tonight and he concurred. Just because you can write something in a long, complicated manner, it doesn’t necessarily mean you’re doing yourself (or your reader) any favors.

Likewise, when I asked my friend Kyle about it, he helpfully advised that when writing code, consider the balancing act between lines of code and doing too many things on one line. Too many lines of code roughly equates to maintenance cost, he explained; but doing too many things on one line makes that line very difficult to understand. While it might be possible to write most of the script on one line, it might not be considered good style… or very friendly to future readers.

Ultimately, Kyle said, “it’s an aesthetic thing with no right or wrong answer. Just takes time and experience to develop your own.” I plan to keep his wise words in mind moving forward.

That’s it for tonight – gasp! No cooking lesson. I’ll make up for it with an extra-good one next go ’round…which may or may not involve something beer-battered and fried. (Let’s just go ahead and agree that it will. YES!)

One thought on “LPTHW 15, 16, 17 and a cooking cliffhanger

  1. Pingback: LPTHW 22: A balanced life | Coding with Knives

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s