Pulling Search Results From NASAs Images API With Testing - Stream Notes for October 31 2020 LiveCoding

October - 2020

youtube: https://www.youtube.com/watch?v=S1fyhO76BW0

Notes

Spent the stream working on my process to pull videos from the NASA Images API. I could have done a quick and dirty script, but my current practice is to do everything with Test Driven Development. This was a perfect thing to practice on. It's small and straightforward. At the start, I wasn't sure how to test it. That's a code smell. After a bunch of kicking around, I got it setup nicely.

This is the general pattern:

#!/usr/bin/env python3

from random import randint

class LoopRunner():
    def __init__(self):
        self.counter = 1
    
    def run_until_you_see_2(self):
        random_num = randint(1, 10)
        print(f'Run: {self.counter} Got: {random_num}')
        if random_num != 2:
            self.counter = self.counter + 1
    
    def run_loop(self):
        killer = 0
        while killer != self.counter:
            self.run_until_you_see_2()
            killer += 1
            
if __name__ == '__main__':
    tr = LoopRunner()
    tr.run_loop()

The while loop checks against an instance variable to see if it should keep going. It calls an external method if it should. That method updates the instance variable. In most examples, you see a True/False flag being set. That would take another step in this case. I like it referencing the counter directly.

Links From The Stream