本文整理汇总了Python中predictionio.connection.AsyncRequest类的典型用法代码示例。如果您正苦于以下问题:Python AsyncRequest类的具体用法?Python AsyncRequest怎么用?Python AsyncRequest使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AsyncRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _aget_itemsim_topn
def _aget_itemsim_topn(self, engine, iid, n, params={}):
"""Private function to asynchronously get top n similar items of the
item
:param engine: name of the prediction engine. type str.
:param iid: item id. type str.
:param n: number of similar items. type int.
:param params: optional parameters. type dictionary
For example, { 'pio_itypes' : ("t1","t2") }
:returns:
AsyncRequest object. You should call the aresp() method using this
AsyncRequest object as argument to get the final result or status
of this asynchronous request.
"""
if "pio_itypes" in params:
params["pio_itypes"] = ",".join(params["pio_itypes"])
if "pio_latlng" in params:
params["pio_latlng"] = ",".join(map(str, params["pio_latlng"]))
if "pio_attributes" in params:
params["pio_attributes"] = ",".join(params["pio_attributes"])
path = "%s/engines/itemsim/%s/topn.json" % (self.apiversion, engine)
request = AsyncRequest("GET", path, pio_appkey=self.appkey,
pio_iid=iid, pio_n=n, **params)
request.set_rfunc(self._aget_itemsim_topn_resp)
self._connection.make_request(request)
return request
示例2: _auser_action_on_item
def _auser_action_on_item(self, action, uid, iid, params):
"""Private function to asynchronously create an user action on an item
:param action: action type. type str. ("like", "dislike", "conversion",
"rate", "view")
:param uid: user id. type str or int.
:param iid: item id. type str or int.
:param params: optional attributes. type dictionary.
For example, { 'pio_rate' : 4, 'pio_latlng' : [1.23,4.56] }
NOTE: For "rate" action, pio_rate attribute is required.
integer value of 1-5 (1 is least preferred and 5 is most
preferred)
:returns:
AsyncRequest object. You should call the aresp() method using this
AsyncRequest object as argument to get the final result or status
of this asynchronous request.
"""
if "pio_latlng" in params:
params["pio_latlng"] = ",".join(map(str, params["pio_latlng"]))
path = "%s/actions/u2i.json" % (self.apiversion)
request = AsyncRequest("POST", path, pio_appkey=self.appkey,
pio_action=action, pio_uid=uid, pio_iid=iid,
**params)
request.set_rfunc(self._auser_action_on_item_resp)
self._connection.make_request(request)
return request
示例3: _aget_user_itemrank_ranked
def _aget_user_itemrank_ranked(self, engine, uid, iids, params={}):
"""Private function to asynchronously get ranked item for user
:param engine: name of the prediction engine. type str.
:param uid: user id. type str.
:param iids: items to be ranked. type list of item ids.
For example, ["i0", "i1", "i2"]
:param params: optional parameters. type dictionary
For example. { 'pio_attributes' : "name" }
:returns:
AsyncRequest object. You should call the aresp() method using this
AsyncRequest object as argument to get the final result or status
of this asynchronous request.
"""
if "pio_attributes" in params:
params["pio_attributes"] = ",".join(params["pio_attributes"])
pio_iids = ",".join(iids)
path = "%s/engines/itemrank/%s/ranked.json" % \
(self.apiversion, engine)
request = AsyncRequest("GET", path, pio_appkey=self.appkey,
pio_uid=uid, pio_iids=pio_iids, **params)
request.set_rfunc(self._aget_user_itemrank_ranked_resp)
self._connection.make_request(request)
return request
示例4: acreate_item
def acreate_item(self, iid, itypes, params={}):
"""Asynchronously create item.
:param iid: item id. type str.
:param itypes: item types. Tuple of Str.
For example, if this item belongs to item types "t1", "t2", "t3", "t4",
then itypes=("t1", "t2", "t3", "t4").
NOTE: if this item belongs to only one itype, use tuple of one element, eg. itypes=("t1",)
:param params: optional attributes. type dictionary.
For example, { 'custom': 'value', 'pio_inactive' : True, 'pio_latlng': [4.5,67.8] }
:returns:
AsyncRequest object. You should call the aresp() method using this AsyncRequest
object as argument to get the final result or status of this asynchronous request.
"""
itypes_str = ",".join(itypes) # join items with ","
if "pio_latlng" in params:
params["pio_latlng"] = ",".join(map(str, params["pio_latlng"]))
if "pio_inactive" in params:
params["pio_inactive"] = str(params["pio_inactive"]).lower()
path = "%s/items.json" % self.apiversion
request = AsyncRequest("POST", path, pio_appkey=self.appkey, pio_iid=iid, pio_itypes=itypes_str, **params)
request.set_rfunc(self._acreate_item_resp)
self._connection.make_request(request)
return request
示例5: acreate_user
def acreate_user(self, uid, params={}):
"""Asynchronously create a user.
:param uid: user id. type str.
:param params: optional attributes. type dictionary.
For example,
{ 'custom': 'value', 'pio_inactive' : True,
'pio_latlng': [4.5,67.8] }
:returns:
AsyncRequest object. You should call the aresp() method using this
AsyncRequest object as argument to get the final result or status
of this asynchronous request.
"""
if "pio_latlng" in params:
params["pio_latlng"] = ",".join(map(str, params["pio_latlng"]))
if "pio_inactive" in params:
params["pio_inactive"] = str(params["pio_inactive"]).lower()
path = "%s/users.json" % self.apiversion
request = AsyncRequest(
"POST", path, pio_appkey=self.appkey, pio_uid=uid, **params)
request.set_rfunc(self._acreate_user_resp)
self._connection.make_request(request)
return request
示例6: _aget_user_itemrec_topn
def _aget_user_itemrec_topn(self, uid, n, engine, params={}):
"""Private function to asynchronously get recommendations for user
:param uid: user id. type str.
:param n: number of recommendation. type int.
:param engine: name of the prediction engine. type str.
:param params: optional parameters. type dictionary
For example, { 'pio_itypes' : ("t1","t2") }
:returns:
AsyncRequest object. You should call the aresp() method using this AsyncRequest
object as argument to get the final result or status of this asynchronous request.
"""
if "pio_itypes" in params:
params["pio_itypes"] = ",".join(params["pio_itypes"])
if "pio_latlng" in params:
params["pio_latlng"] = ",".join(map(str, params["pio_latlng"]))
if "pio_attributes" in params:
params["pio_attributes"] = ",".join(params["pio_attributes"])
enc_uid = urllib.quote(uid,"") # replace special char with %xx
path = "%s/engines/itemrec/%s/topn.json" % (self.apiversion, engine)
request = AsyncRequest("GET", path, pio_appkey=self.appkey, pio_uid=enc_uid, pio_n=n, **params)
request.set_rfunc(self._aget_user_itemrec_topn_resp)
self._connection.make_request(request)
return request
示例7: acreate_event
def acreate_event(self, event, entity_type, entity_id,
target_entity_type=None, target_entity_id=None, properties=None,
event_time=None):
"""Asynchronously create an event.
:param event: event name. type str.
:param entity_type: entity type. It is the namespace of the entityId and
analogous to the table name of a relational database. The entityId must be
unique within same entityType. type str.
:param entity_id: entity id. *entity_type-entity_id* becomes the unique
identifier of the entity. For example, you may have entityType named user,
and different entity IDs, say 1 and 2. In this case, user-1 and user-2
uniquely identifies entities. type str
:param target_entity_type: target entity type. type str.
:param target_entity_id: target entity id. type str.
:param properties: a custom dict associated with an event. type dict.
:param event_time: the time of the event. type datetime, must contain
timezone info.
:returns:
AsyncRequest object. You can call the get_response() method using this
object to get the final resuls or status of this asynchronous request.
"""
data = {
"event": event,
"entityType": entity_type,
"entityId": entity_id,
}
if target_entity_type is not None:
data["targetEntityType"] = target_entity_type
if target_entity_id is not None:
data["targetEntityId"] = target_entity_id
if properties is not None:
data["properties"] = properties
et = event_time_validation(event_time)
# EventServer uses milliseconds, but python datetime class uses micro. Hence
# need to skip the last three digits.
et_str = et.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + et.strftime("%z")
data["eventTime"] = et_str
qparam = {
"accessKey" : self.access_key
}
if self.channel is not None:
qparam["channel"] = self.channel
path = "/events.json?%s" % (urlencode(qparam), )
request = AsyncRequest("POST", path, **data)
request.set_rfunc(self._acreate_resp)
self._connection.make_request(request)
return request
示例8: get_status
def get_status(self):
"""Get the status of the PredictionIO API Server
:returns:
status message.
:raises:
ServerStatusError.
"""
path = "/"
request = AsyncRequest("GET", path)
request.set_rfunc(self._aget_resp)
self._connection.make_request(request)
result = request.get_response()
return result
示例9: asend_query
def asend_query(self, data):
"""Asynchronously send a request to the engine instance with data as the
query.
:param data: the query: It is coverted to an json object using json.dumps
method. type dict.
:returns:
AsyncRequest object. You can call the get_response() method using this
object to get the final resuls or status of this asynchronous request.
"""
path = "/queries.json"
request = AsyncRequest("POST", path, **data)
request.set_rfunc(self._aget_resp)
self._connection.make_request(request)
return request
示例10: adelete_event
def adelete_event(self, event_id):
"""Asynchronouly delete an event from Event Server.
:param event_id: event id returned by the EventServer when creating the
event.
:returns:
AsyncRequest object.
"""
enc_event_id = urllib.quote(event_id, "") # replace special char with %xx
path = "/events/%s.json" % enc_event_id
request = AsyncRequest("DELETE", path)
request.set_rfunc(self._adelete_resp)
self._connection.make_request(request)
return request
示例11: aget_item
def aget_item(self, iid):
"""Asynchronously get item
:param iid: item id. type str.
:returns:
AsyncRequest object. You should call the aresp() method using this AsyncRequest
object as argument to get the final result or status of this asynchronous request.
"""
enc_iid = urllib.quote(iid, "")
path = "%s/items/%s.json" % (self.apiversion, enc_iid)
request = AsyncRequest("GET", path, pio_appkey=self.appkey)
request.set_rfunc(self._aget_item_resp)
self._connection.make_request(request)
return request
示例12: adelete_user
def adelete_user(self, uid):
"""Asynchronously delete user.
:param uid: user id. type str.
:returns:
AsyncRequest object. You should call the aresp() method using this AsyncRequest
object as argument to get the final result or status of this asynchronous request.
"""
enc_uid = urllib.quote(uid,"") # replace special char with %xx
path = "%s/users/%s.json" % (self.apiversion, enc_uid)
request = AsyncRequest("DELETE", path, pio_appkey=self.appkey)
request.set_rfunc(self._adelete_user_resp)
self._connection.make_request(request)
return request
示例13: acreate_user
def acreate_user(self, uid, **params):
"""Asynchronously create a user.
:param uid: user id. type str or int.
:param params: keyword arguments for optional attributes.
For example, bday="1985-01-23", inactive="true".
:returns:
AsyncRequest object. You should call the aresp() method using this AsyncRequest
object as argument to get the final result or status of this asynchronous request.
"""
path = "%s/users.json" % self.apiversion
request = AsyncRequest("POST", path, appkey=self.appkey, uid=uid, **params)
request.set_rfunc(self._acreate_user_resp)
self._connection.make_request(request)
return request
示例14: _auser_action_item
def _auser_action_item(self, action, uid, iid, **params):
"""Asynchronously create an user action on an item
:param action: action type. type str. ("like", "dislike", "conversion", "rate", "view")
:param uid: user id. type str or int.
:param iid: item id. type str or int.
:param params: keyword arguments for optional attributes.
For example, latlng="123.4, 56.7"
For "rate" action, rate param is required. integer value of 1-5 (1 is least preferred and 5 is most preferred) (eg. rate=4)
:returns:
AsyncRequest object. You should call the aresp() method using this AsyncRequest
object as argument to get the final result or status of this asynchronous request.
"""
path = "%s/actions/u2i/%s.json" % (self.apiversion, action)
request = AsyncRequest("POST", path, appkey=self.appkey, uid=uid, iid=iid, **params)
request.set_rfunc(self._au2i_action_resp)
self._connection.make_request(request)
return request
示例15: adelete_event
def adelete_event(self, event_id):
"""Asynchronouly delete an event from Event Server.
:param event_id: event id returned by the EventServer when creating the
event.
:returns:
AsyncRequest object.
"""
qparam = {
"accessKey" : self.access_key
}
if self.channel is not None:
qparam["channel"] = self.channel
enc_event_id = quote(event_id, "") # replace special char with %xx
path = "/events/%s.json" % (enc_event_id, )
request = AsyncRequest("DELETE", path, **qparam)
request.set_rfunc(self._adelete_resp)
self._connection.make_request(request)
return request