Reading Variables in Python

In this section we will Python and the spyrk library to read variables from our Spark Cores.

Documentation

As promised earlier each section will start with a documentation section. These sections are no different.

We will be accessing data from our Spark Cores using the Spark Cloud API. The Spark Cloud is a REST API which is extremely easy to use.

We will be using the open source spyrk library to read data from our Spark Cores through the Spark Cloud. The source code for spyrk as well as installation instructions is available over at

The installation instructions assume that you are familiar using pip (a package manager for python). Ask for help from an organizer or checkout the official pip documentation on how to install it

Some Notes About Spyrk

Most of spyrk’s operations are blocking which means the thread you are executing in will wait until the HTTP request to retrieve the data before continuing. If you are developing time sensitive systems it my be a good idea to look into using multiple threads to speed up execution… However how to do that is out of the scope of this workshop.

Using Spyrk

Spyrk provides several abstractions around the Spark API. These abstractions make it very easy to interact with the Spark Cloud API in a very pythonic fashion.

Connecting To The Spark Cloud

Before we can access any data from the Spark Cloud we first need to create an instance of the SparkCloud class (found in the spyrk package). Below is a simple example of how to achieve this

from spyrk import SparkCloud

connection = SparkCloud(ACCESS_TOKEN)

List Devices

Now that we have our connection accessing our Spark Devices is as simple as accessing the devices attribute on our connection.

# connection initialized from above

for device_id in connection.devices:
    print(device_id)

Listing available devices isn’t extremely useful. Before we can actually access variables on our Spark(s) we need to get an instance of our device. We can do that by accessing the connection.devices attribute as if it were a dict and using the device_id we got in our last example as a key.

device = connection.devices[device_id]

List Variables

Now that we’ve gotten our device we can get a list of variables that we’ve exposed in step 04 (Exposing Variables). To do this we can simply access the attribute variables on our device like so

variables = device.variables
variable_names = variables.keys() # get the names of the variables

Get Variable’s Value

Now it’s time to read a variable that we exposed from our Spark in Python. Doing this is very simple. Our variables can be read by accessing the attribute of the name of our variable on our device object

# lets pretend we exposed a variable named foo on our spark
foo = device.foo

Or if we want to get all of the variables from our spark and place them into a dictionary we can do the following

# this is why I defined variable_keys earlier
my_spark_vars = {}
for key in variable_keys:
    my_spark_vars[key] = getattr(device, key)

Putting It All Together

Here is the code for everything we’ve done so far in this section

from spyrk import SparkCloud

connection = SparkCloud(ACCESS_TOKEN)

for device_id in connection.devices:
    device = connection.devices[device_id]

    variables = device.variables
    variable_names = variables.keys()

    my_spark_vars = {}
    for key in variable_keys:
        my_spark_vars[key] = getattr(device, key)

    # my_spark_vars how has the values for all of the variables we exposed
    # from our Spark

Lets Make Those Variables Interesting

Okay so now we can read variables from our Spark. That isn’t too interesting since those variables don’t mean much yet. Now lets hook up a sensor to our Spark Core so that we can get live readings from it!