2

I am reading values from my tempodb database using the following code:

import datetime
from tempodb import Client

client = Client("key", "secret")

start = datetime.datetime(2014, 3, 28)
end = datetime.datetime(2014, 3, 29)
data = client.read_key("custom", start, end, interval="1hour", function="mean")
print data

The code works fine, and it gets data points. However, it gets them in the format 'DataSet', which is TempoDB's format for data points. But they don't show how to convert from this format to actual coordinates or numbers. Here is the data that the code is returning:

{'series': <tempodb.base.Series object at 0x2500df0>, 
'summary': <tempodb.base.Summary object at 0x250acb0>, 
'end': datetime.datetime(2014, 3, 29, 0, 0, tzinfo=tzutc()), 
'data': [<tempodb.base.DataPoint object at 0x250a2b0>, 
<tempodb.base.DataPoint object at 0x250a270>, 
<tempodb.base.DataPoint object at 0x250aa50>, 
<tempodb.base.DataPoint object at 0x250a9f0>, 
<tempodb.base.DataPoint object at 0x250aad0>, 
<tempodb.base.DataPoint object at 0x250ab30>, 
<tempodb.base.DataPoint object at 0x250ab90>, 
<tempodb.base.DataPoint object at 0x250abf0>, 
tempodb.base.DataPoint object at 0x250ac50>], 
'start': datetime.datetime(2014, 3, 28, 0, 0, tzinfo=tzutc())}

Does anyone know how to convert form this format (DataSet) to actual coordinates?

EDIT:

Here is my current code:

import datetime
from tempodb import Client

client = Client("key", "secret")

start = datetime.datetime(2014, 3, 28)
end = datetime.datetime(2014, 3, 29)
data = client.read_key("custom", start, end, interval="1hour", function="mean")
print data      
for point in data['data']:
        print(point.value)

This outputs the following, with a TypeError:

{'series': <tempodb.base.Series object at 0x15dae30>, 'summary': <tempodb.base.Summary object at 0x15e4d30>, 'end': datetime.datetime(2014, 3, 29, 0, 0, tzinfo=tzutc()), 'data': [<tempodb.base.DataPoint object at 0x15e4910>, <tempodb.base.DataPoint object at 0x15e42d0>, <tempodb.base.DataPoint object at 0x15e4ab0>, <tempodb.base.DataPoint object at 0x15e4ad0>, <tempodb.base.DataPoint object at 0x15e4b70>, <tempodb.base.DataPoint object at 0x15e4bd0>, <tempodb.base.DataPoint object at 0x15e4c10>, <tempodb.base.DataPoint object at 0x15e4c70>, <tempodb.base.DataPoint object at 0x15e4cd0>], 'start': datetime.datetime(2014, 3, 28, 0, 0, tzinfo=tzutc())}
Traceback (most recent call last):
  File "readtempo.py", line 10, in <module>
    for point in data['data']:
TypeError: 'DataSet' object has no attribute '__getitem__'

2 Answers2

0

According to the tempodb python client source code, you can use ts attribute:

for point in data.data:
    print(point.ts.isoformat())

Also check to_json() method.

Hope that helps.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • When I try running this code, it gives me an error: `Traceback (most recent call last): File "readtempo.py", line 10, in for point in data['data']: TypeError: 'DataSet' object has no attribute '__getitem__'` Am I doing something wrong here? Or am I supposed to substitute the `['data']` for something else... –  Mar 31 '14 at 03:33
  • @applepi are you sure you are iterating over `data['data']`? – alecxe Mar 31 '14 at 03:34
  • Yes, here is that line in my code: `for point in data['data']: print(point.value)` –  Mar 31 '14 at 03:34
  • @applepi could you please print out the contents of `data['data']` and show it? thanks – alecxe Mar 31 '14 at 03:35
  • @applepi according to what you've provided it supposed to be a list of `DataPoint`s.. – alecxe Mar 31 '14 at 03:36
  • @applepi I think it's my mistake. Try iterating over `data.data`. – alecxe Mar 31 '14 at 03:37
  • @applepi according to the `tempodb` source code, `read_key` returns a `DataSet` which has a `data` attribute that contains a list of `DataPoint`s. `DataPoint` has `value` attribute. Should work in theory..I just don't have an ability to check.. – alecxe Mar 31 '14 at 03:43
  • Thanks! This worked! But it only gives numbers. How do I get the timestamp for each one as well? –  Mar 31 '14 at 03:43
  • @applepi great, use `ts.isoformat()` - I've updated the question. – alecxe Mar 31 '14 at 03:44
  • Great! Just one clarification: 2014-03-28T00:00:00+00:00 means midnight on the 28th of march 2014 right? Just as 2014-03-28T08:00:00+00:00 means 8:00 AM on that same date? And what timezone is this? :) –  Mar 31 '14 at 03:48
  • @applepi it's just an [ISO 8601 format](https://docs.python.org/2/library/datetime.html#datetime.date.isoformat), `point.ts` has a datetime object - you can do whatever you want with it :) – alecxe Mar 31 '14 at 12:29
  • what timezone is it? the local one? or GMT? –  Apr 01 '14 at 02:55
0

As @alecxe mentioned, make sure you're iterating with the format

for point in data.data:

TempoDB's DataSet class consists of two objects: ts (timestamp) and value (point value). To access the timestamp and the value:

print point.ts.strftime("%b %e, %Y")
print point.value

I utilized TempoDB for a data challenge analyzing 750,000 rows of bike share usage data in Chicago recently. Project results are here and GitHub source that includes Python interface to TempoDB is here.