Monday 2 November 2020

PyBloom coding project part 8: Orchestrating the code

My hundredth post and part 8 of this series is a short one. Here I describe how I orchestrated the various Python scripts to create the final program. 

Orchestrating the code

Right at the top, I defined the structure of the code that I wanted to write, as:

  1. Check the current weather

  2. Set the blooms to the current temperature

  3. Log the weather reading

  4. Generate new graphs


Now that we’ve gone through the detail of the classes and functions, following the execution code becomes quite straightforward. I’ll leave it to you, dear reader, to put it together. See you in the next section, when we present this on a web page!


I’ve wrapped the orchestration in another function, called weather. This gives me the flexibility to call it as a module from my Flask app later, and also to run it directly from the Terminal as python pybloom.py.


Putting it together

def weather():

    # Check current weather

    observation = weather_observation()

    observation.new(HOME_LOCATION)

    # observation.set(datetime.now().strftime(DATETIME_STRING), 23,

        'dummy observation')

    print(observation)


    # Set lounge bloom to current temperature

    lounge_bloom = hue_lamp(hue_lamp_ids['lounge bloom'])

     lounge_bloom.set_colour(convert_temp_to_colour(observation.temperature))

    # Set den bloom to current temperature

    den_bloom = hue_lamp(hue_lamp_ids['den bloom'])

    den_bloom.set_colour(convert_temp_to_colour(observation.temperature))


    # Log weather observation

    observation.log()


    # generate new graphs

    generate_graphs(observation.timestamp)


    return 'Fetched weather'


weather()


Keeping secrets

Way back when I showed you how I organised the code, I mentioned that I kept sensitive secrets out of the main code. I had global constants for these secrets, which were imported from a separate credentials.py file, as structured below. I decided on a simple dictionary with self-evident but unique variable names. Note that both key and values are strings.


credentials.py

credentials = {

    'hue_ip': <IP address of your Hue Bridge>,

    'hue_username': <given by the Hue Bridge on first sync>,

    'owm_key': <generated by service on signing up>,

    'home_location': <where you live, in the format city, country code>

}



Since the hard work was done in previous sections, orchestrating the code turns out to be quite straightforward. In part 9, I'll you through how I created a web page to present these readings. Also visit https://github.com/Schmoiger/pybloom for the full story.

No comments:

Post a Comment

It's always great to hear what you think. Please leave a comment, and start a conversation!