Skip to content

Instantly share code, notes, and snippets.

@xezpeleta
Created July 10, 2014 20:40
Show Gist options
  • Save xezpeleta/5b8aebb38baf22551269 to your computer and use it in GitHub Desktop.
Save xezpeleta/5b8aebb38baf22551269 to your computer and use it in GitHub Desktop.
Python library for sending data to Phant (data.sparkfun.com). https://bitbucket.org/boomlinde/pyphant/overview
import phant
import time
log = phant.factory(
'http://data.sparkfun.com/input/aGRVr0MQ4NHbwxWaZRLv?private_key=KE75BAJKR8HebZWVPr2p',
key=str,
value=int
)
i = 0
while True:
print log(key="tick", value=i)
i += 1
time.sleep(5)
import urllib
import urllib2
def factory(endpoint, timeout=10, *plain, **converters):
for p in plain:
converters[p] = lambda x: x
def committer(**values):
converted = {}
if set(converters.keys()) ^ set(values.keys()):
raise TypeError('Value mismatch (expected: %s)' % (
', '.join(converters.keys())
))
for k, v in values.items():
try:
converted[k] = converters[k](v)
except ValueError:
raise ValueError(
'Parameter "%s" can not be converted with "%s"' % (
k, converters[k].__name__
))
url = endpoint + '&' + urllib.urlencode(converted)
return bool(int(urllib2.urlopen(url, timeout=timeout).read(1)))
return committer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment