Skip to content

Commit

Permalink
PredictionClient python sdk.
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Yip committed Sep 12, 2014
1 parent 4c66ff5 commit fefb06f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 25 deletions.
2 changes: 1 addition & 1 deletion sdk/python-sdk/itemrec_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import time

def import_testdata(app_id, api_url):
client = predictionio.Client(app_id, 1, api_url)
client = predictionio.DataClient(app_id, 1, api_url)
predictionio.connection.enable_log("test.log")
client.set_user("u0")
client.set_user("u1")
Expand Down
42 changes: 42 additions & 0 deletions sdk/python-sdk/itemrec_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Import simple query for getting itemrank
"""
import predictionio
import argparse
import time


def send_queries(apiurl):
client = predictionio.PredictionClient(threads=1, apiurl=apiurl)

# Sync Query
query = {
"uid": "u2",
"items": ["i0", "i1", "i2", "i3"],
}
response = client.send_query(query)
print(response)

# Async Query
query = {
"uid": "u9527",
"items": ["i0", "i1", "i2", "i3"],
}
response = client.asend_query(query)
print(response.get_response())

client.close()


def main():
parser = argparse.ArgumentParser(description="some description here..")
parser.add_argument('--apiurl', default="http:https://localhost:8000")

args = parser.parse_args()
print args

send_queries(apiurl=args.apiurl)

if __name__ == '__main__':
main()

78 changes: 54 additions & 24 deletions sdk/python-sdk/predictionio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ class NotCreatedError(PredictionIOAPIError):
class NotFoundError(PredictionIOAPIError):
pass

class Client(object):
def __init__(self, appid, threads=1, apiurl="http:https://localhost:8000",
class BaseClient(object):
def __init__(self, threads=1, apiurl="http:https://localhost:8000",
apiversion="", qsize=0, timeout=5):
"""Constructor of Client object.
"""
self.appid = appid
self.threads = threads
self.apiurl = apiurl
self.apiversion = apiversion
Expand All @@ -51,7 +50,7 @@ def __init__(self, appid, threads=1, apiurl="http:https://localhost:8000",
self._connection = Connection(host=self.host, threads=self.threads,
qsize=self.qsize, https=self.https,
timeout=self.timeout)

def close(self):
"""Close this client and the connection.
Expand All @@ -60,7 +59,7 @@ def close(self):
It will wait for all pending requests to finish.
"""
self._connection.close()

def pending_requests(self):
"""Return the number of pending requests.
Expand All @@ -85,6 +84,40 @@ def get_status(self):
result = request.get_response()
return result

def _acreate_resp(self, response):
if response.error is not None:
raise NotCreatedError("Exception happened: %s for request %s" %
(response.error, response.request))
elif response.status != httplib.CREATED:
raise NotCreatedError("request: %s status: %s body: %s" %
(response.request, response.status,
response.body))

return response

def _aget_resp(self, response):
if response.error is not None:
raise NotFoundError("Exception happened: %s for request %s" %
(response.error, response.request))
elif response.status != httplib.OK:
raise NotFoundError("request: %s status: %s body: %s" %
(response.request, response.status,
response.body))

data = json.loads(response.body) # convert json string to dict
return data


class DataClient(BaseClient):
def __init__(self, appid, threads=1, apiurl="http:https://localhost:8000",
apiversion="", qsize=0, timeout=5):
"""Constructor of Client object.
"""
super(DataClient, self).__init__(threads, apiurl, apiversion, qsize, timeout)
self.appid = appid


def acreate_event(self, data):
path = "/events"
request = AsyncRequest("POST", path, **data)
Expand Down Expand Up @@ -172,25 +205,22 @@ def record_user_action_on_item(self, action, uid, iid, properties={}):
return self.arecord_user_action_on_item(
action, uid, iid, properties).get_response()

def _acreate_resp(self, response):
if response.error is not None:
raise NotCreatedError("Exception happened: %s for request %s" %
(response.error, response.request))
elif response.status != httplib.CREATED:
raise NotCreatedError("request: %s status: %s body: %s" %
(response.request, response.status,
response.body))

return response
class PredictionClient(BaseClient):
def __init__(self, threads=1, apiurl="http:https://localhost:8000",
apiversion="", qsize=0, timeout=5):
"""Constructor of Client object.
def _aget_resp(self, response):
if response.error is not None:
raise NotFoundError("Exception happened: %s for request %s" %
(response.error, response.request))
elif response.status != httplib.OK:
raise NotFoundError("request: %s status: %s body: %s" %
(response.request, response.status,
response.body))
"""
super(PredictionClient, self).__init__(
threads, apiurl, apiversion, qsize, timeout)

data = json.loads(response.body) # convert json string to dict
return data
def asend_query(self, data):
path = "/"
request = AsyncRequest("POST", path, **data)
request.set_rfunc(self._aget_resp)
self._connection.make_request(request)
return request

def send_query(self, data):
return self.asend_query(data).get_response()

0 comments on commit fefb06f

Please sign in to comment.