Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Cleaned code

  • Loading branch information...
commit 45e443213cf0bfdb58f68a900b0b9375e9815017 1 parent 725adb6
@tsileo authored
Showing with 95 additions and 87 deletions.
  1. +11 −0 README.rst
  2. +67 −67 cube.py
  3. +17 −20 setup.py
View
11 README.rst
@@ -85,6 +85,17 @@ Usage
# Request known event types
cube.types()
+Changelog
+=========
+
+0.2.0
+-----
+
+**September 30 2013**
+
+- Cleaned code (PEP8)
+- Compatible with requests 2.0
+
License (MIT)
=============
View
134 cube.py
@@ -1,69 +1,69 @@
-import requests
-import json
from datetime import datetime
-API_VERSION = "1.0"
-
-class Cube:
- def __init__(self, hostname="localhost", **kwargs):
- self.collector_url = "http://%s:%s/%s/" % (hostname, \
- kwargs.get("collector_port", 1080), API_VERSION)
- self.evaluator_url = "http://%s:%s/%s/" % (hostname, \
- kwargs.get("evaluator_port", 1081), API_VERSION)
-
- def put(self, event_type, event_data={}, **kwargs):
- """
- Create/update an event.
- """
- event = dict(type=event_type, data=event_data)
-
- event["time"] = kwargs.get("time", datetime.utcnow().isoformat())
-
- if kwargs.get("id"):
- event["id"] = kwargs.get("id")
-
- data = json.dumps([event])
-
- r = requests.post(self.collector_url + "event/put", data=data,
- headers={'content-type': 'application/json'})
- r.raise_for_status()
-
- return [event]
-
- def make_query(self, query_type, expression, **kwargs):
- data = dict(expression=expression,
- stop=kwargs.get("stop", datetime.utcnow().isoformat()))
-
- if kwargs.get("start"):
- data["start"] = kwargs.get("start")
-
- if kwargs.get("limit"):
- data["limit"] = kwargs.get("limit")
-
- if query_type == "metric" and kwargs.get("step"):
- data["step"] = kwargs.get("step")
-
- r = requests.get(self.evaluator_url + query_type, params=data)
- r.raise_for_status()
-
- return r.json()
-
- def event(self, expression, **kwargs):
- """
- Query with an event expression
- """
- return self.make_query("event", expression, **kwargs)
-
- def metric(self, expression, **kwargs):
- """
- Query with a metric expression
- """
- return self.make_query("metric", expression, **kwargs)
-
- def types(self):
- """
- List of the known event types
- """
- r = requests.get(self.evaluator_url + "types")
- r.raise_for_status()
- return r.json()
+try:
+ import ujson as json
+except ImportError:
+ import json
+
+import requests
+
+API_VERSION = '1.0'
+
+
+class Cube(object):
+ def __init__(self, hostname="localhost", **kwargs):
+ self.collector_url = 'http://{0}:{1}/{2}/'.format(hostname,
+ kwargs.get('collector_port', 1080),
+ API_VERSION)
+ self.evaluator_url = 'http://{0}:{1}/{2}/'.format(hostname,
+ kwargs.get('evaluator_port', 1081),
+ API_VERSION)
+
+ def put(self, event_type, event_data={}, **kwargs):
+ """
+ Create/update an event.
+ """
+ event = dict(type=event_type, data=event_data)
+
+ event["time"] = kwargs.get("time", datetime.utcnow().isoformat())
+
+ if kwargs.get("id"):
+ event["id"] = kwargs.get("id")
+
+ data = json.dumps([event])
+
+ r = requests.post(self.collector_url + 'event/put',
+ data=data,
+ headers={'content-type': 'application/json'})
+ r.raise_for_status()
+
+ return [event]
+
+ def make_query(self, query_type, expression, **kwargs):
+ data = dict(expression=expression,
+ stop=kwargs.get('stop', datetime.utcnow().isoformat()))
+ data.update(kwargs)
+ r = requests.get(self.evaluator_url + query_type, params=data)
+ r.raise_for_status()
+
+ return r.json()
+
+ def event(self, expression, **kwargs):
+ """
+ Query with an event expression
+ """
+ return self.make_query('event', expression, **kwargs)
+
+ def metric(self, expression, **kwargs):
+ """
+ Query with a metric expression
+ """
+ return self.make_query('metric', expression, **kwargs)
+
+ def types(self):
+ """
+ List of the known event types
+ """
+ r = requests.get(self.evaluator_url + 'types')
+ r.raise_for_status()
+ return r.json()
View
37 setup.py
@@ -1,31 +1,28 @@
import os
-import sys
from setuptools import setup
+
def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()
setup(
- name = "cube-client",
- version = "0.1",
- author = "Thomas Sileo",
- author_email = "thomas.sileo@gmail.com",
- description = "A Python client for Cube: Time Series Data Collection & Analysis",
- license = "MIT",
- keywords = "cube time series",
- url = "https://github.com/tsileo/cube-client",
+ name='cube-client',
+ version='0.2.0',
+ author='Thomas Sileo',
+ author_email='thomas.sileo@gmail.com',
+ description='A Python client for Cube: Time Series Data Collection & Analysis',
+ license='MIT',
+ keywords='cube time series',
+ url='https://github.com/tsileo/cube-client',
py_modules=['cube'],
- long_description= read('README.rst'),
- install_requires=[
- "requests"
- ],
+ long_description=read('README.rst'),
+ install_requires=['requests'],
classifiers=[
- "Development Status :: 4 - Beta",
- "Intended Audience :: Developers",
- "Topic :: Database",
- "License :: OSI Approved :: MIT License",
- "Programming Language :: Python",
+ 'Development Status :: 4 - Beta',
+ 'Intended Audience :: Developers',
+ 'Topic :: Database',
+ 'License :: OSI Approved :: MIT License',
+ 'Programming Language :: Python',
],
- scripts = ["cube.py"],
zip_safe=False,
-)
+)
Please sign in to comment.
Something went wrong with that request. Please try again.