Streams

BK
Ky
Last updated 2 months ago
from datetime import *
from pytz import timezone
eastern = timezone("EST")

Importing the `sentenai` library

Sentenai's python library is safe to import *. When writing queries this can make it easier to work with. Let's start by initializing an API Client:

from sentenai import Sentenai, hql, V
sentenai = Sentenai(auth_key="<api-key>")

This sentenai client is not a "connection" to Sentenai, but provides methods which expose Sentenai's API.

Streams

Return a list of streams

sentenai.streams()

name

length

health

0

weather-boston-day

6553

True

1

weather-boston-hour

157398

True

This is useful for seeing what's available, but if we know the name of a stream we can reference it by using the .Stream() function. There is no need to create a stream as a stream is instantiated within Sentenai as soon as data is logged to that stream.

hweather = sentenai.Stream("weather-boston-hour")
print(weather)
# Note that the print function returns "prettified" syntax.
(stream "weather-boston-hour")

Testing stream existence

Streams have a __bool__ method so you can use a stream's existence in branches. A new stream is created when an event is put into a non-existent stream. Streams with zero events may still exist if all events have been deleted from them but the stream itself has not been deleted.

print(bool(hweather))
print(bool(sentenai.Stream('This-Stream-Does-Not-Exist)))
True
False

Stream Names and Paths

The name of a stream is accessible with the .name property.

print(hweather.name)
"weather-boston-hour"

Stream paths that contain a reserved word can be made as follows:

hweather["name"]["bar"].baz
name.bar.baz