跳转到主要内容

Timetric客户端库(http://timetric.com/)

项目描述

Timetric(http://timetric.com)的客户端。

需求(易于安装,列在setup.py中的预需求中)

  • httplib2

  • python-dateutil

  • simplejson

可选,如果您想使用OAuth身份验证

  • oauth

此需求未列出为预需求,因此默认不会安装。

身份验证设置

您需要向TimetricClient提供一个配置字典中的身份验证信息。Timetric支持两种类型的身份验证:OAuth和API令牌。您可以使用配置字典中的'authtype'键选择一种方法。此键的值可以是'oauth''apitoken';如果没有给出,则默认为'oauth'

如果使用API令牌,则必须有以下键:

  • ‘apitoken_key’

  • ‘apitoken_secret’

如果使用OAuth,还必须有以下键:

  • ‘consumer_key’

  • ‘consumer_secret’

如果您已经完成了必要的密钥交换,则此还可以包含以下内容:

  • ‘access_key’

  • ‘access_token’

OAuth设置

您第一次使用OAuth协议时,需要与Timetric进行一些密钥交换来设置您的令牌。

>>> import timetric

# 您需要一个包含至少您的Timetric密钥和密钥的配置字典 # (请参阅设置页面的“应用程序”面板)。在实际应用程序中,您 # 可能想使此配置持久化(shelve模块是一个轻量级的起点)因为库 # 会将OAuth身份验证信息存储回此配置。 >>> conf = {‘authtype’:’oauth’, … ‘consumer_key: ‘XXX’, … ‘consumer_secret’: ‘XXX’} >>> client = timetric.TimetricClient(conf)

第一次运行时,您需要通过将用户发送到授权请求页面来授权您的密钥给Timetric。>> token = client.get_request_token() >>> import webbrowser >>> webbrowser.open(client.get_authorize_url(token))

一旦用户在该页面上完成授权,您就可以继续了。>>> access_token = client.get_access_token(token)

现在您可以重新授权以使用此令牌:>>> conf.update({'oauth_key':access_token.key, … 'oauth_secret':access_token.secret}) >>> client = timetric.TimetricClient(conf)

API令牌设置

>>> import timetric

从某处获取了您的配置字典后:>>> conf = {'authtype':'apitoken', … 'apitoken_key': ‘XXX’, .. ‘apitoken-secret’: ‘XXX’}

您初始化Timetric客户端:>>> client = timetric.TimetricClient(conf)

使用方法

# There's no API method to get a list of a particular
# user's series, so you need to somehow "know" the series ID.
>>> series = client.series('p-DpewL0TO-iBE4nMBCTsQ')

# Get the latest value
>>> series.latest()
(1236707269.0, 2.0)

# Iterate over the whole dataset
>>> for (timestamp, value) in series:
...     print timestamp, value
1236463646.39 3.0
1236486562.94 5.0
1236493503.37 6.0

# Update given a single value
>>> series.update(14)

# Update given an iterable of (time, value) pairs
>>> import time
>>> data = [(time.time() - 100, 11), (time.time() + 100, 15)]
>>> series.update(data)

# Update given a file of CSV data
>>> series.update(open('/tmp/data.csv'))

# Clear all the data out of the series
>>> series.delete()

项目详情


下载文件

下载您平台对应的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码发行版

timetric-1.3.tar.gz (7.8 kB 查看哈希值)

上传时间 源代码

支持者