Skip to content

Commit e950598

Browse files
committed
Prefer optional parameters to a dictionary containing the parameters
1 parent 637096a commit e950598

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+193
-245
lines changed

README.rst

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Basic example
6060
6161
# Get recommendations for user 'user-25'
6262
print('Recommend for a user')
63-
recommended = client.send(UserBasedRecommendation('user-25', 5, {'rotationRate': 0}))
63+
recommended = client.send(UserBasedRecommendation('user-25', 5))
6464
print("Recommended items: %s" % recommended)
6565
except APIException as e:
6666
print(e)
@@ -121,27 +121,26 @@ Using property values
121121
for item_id in items:
122122
#Use cascadeCreate to create unexisting users
123123
purchasing_users = [user_id for user_id in users if random.random() < PROBABILITY_PURCHASED]
124-
requests += [AddPurchase(user_id, item_id, {'cascadeCreate': True}) for user_id in purchasing_users]
124+
requests += [AddPurchase(user_id, item_id, cascade_create=True) for user_id in purchasing_users]
125125
126126
# Send purchases to the recommender system
127127
client.send(Batch(requests))
128128
129129
# Get 5 recommendations for user-42, who is currently viewing computer-6
130-
recommended = client.send(ItemBasedRecommendation('computer-6', 5, {'targetUserId': 'user-42'}) )
130+
recommended = client.send(ItemBasedRecommendation('computer-6', 5, target_user_id='user-42'))
131131
print("Recommended items: %s" % recommended)
132132
133133
# Get 5 recommendations for user-42, but recommend only computers that
134134
# have at least 3 cores
135135
recommended = client.send(
136-
ItemBasedRecommendation('computer-6', 5, {'targetUserId': 'user-42', 'filter': "'num-cores'>=3"})
136+
ItemBasedRecommendation('computer-6', 5, target_user_id='user-42', filter="'num-cores'>=3")
137137
)
138138
print("Recommended items with at least 3 processor cores: %s" % recommended)
139139
140140
# Get 5 recommendations for user-42, but recommend only items that
141141
# are more expensive then currently viewed item (up-sell)
142142
recommended = client.send(
143-
ItemBasedRecommendation('computer-6', 5,
144-
{'targetUserId': 'user-42', 'filter': "'price' > context_item[\"price\"]"})
143+
ItemBasedRecommendation('computer-6', 5, target_user_id='user-42', filter="'price' > context_item[\"price\"]")
145144
)
146145
print("Recommended up-sell items: %s" % recommended)
147146
@@ -159,8 +158,7 @@ Example:
159158
160159
try:
161160
recommended = client.send(
162-
ItemBasedRecommendation('computer-6', 5,
163-
{'targetUserId': 'user-42', 'filter': "'price' > context_item[\"price\"]"})
161+
ItemBasedRecommendation('computer-6', 5,target_user_id='user-42', filter="'price' > context_item[\"price\"]")
164162
)
165163
except ResponseException as e:
166164
#Handle errorneous request => use fallback

recombee_api_client/api_requests/add_bookmark.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,24 @@ class AddBookmark(Request):
66
77
"""
88

9-
def __init__(self,user_id, item_id, optional = dict()):
9+
def __init__(self,user_id, item_id, timestamp=None, cascade_create=None):
1010
"""
1111
Required parameters:
1212
@param user_id: User who bookmarked the item
1313
1414
@param item_id: Bookmarked item
1515
1616
17-
Optional parameters (given as dictionary C{optional}):
17+
Optional parameters:
1818
@param timestamp: UTC timestamp of the bookmark as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
1919
20-
@param cascadeCreate: Sets whether the given user/item should be created if not present in the database.
20+
@param cascade_create: Sets whether the given user/item should be created if not present in the database.
2121
2222
"""
2323
self.user_id = user_id
2424
self.item_id = item_id
25-
self.timestamp = optional.get('timestamp')
26-
self.cascade_create = optional.get('cascadeCreate')
27-
for par in optional:
28-
if not par in {"timestamp","cascadeCreate"}:
29-
raise ValueError("Unknown parameter %s was given to the request" % par)
25+
self.timestamp = timestamp
26+
self.cascade_create = cascade_create
3027
self.timeout = 1000
3128
self.ensure_https = False
3229
self.method = 'post'

recombee_api_client/api_requests/add_cart_addition.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,24 @@ class AddCartAddition(Request):
66
77
"""
88

9-
def __init__(self,user_id, item_id, optional = dict()):
9+
def __init__(self,user_id, item_id, timestamp=None, cascade_create=None):
1010
"""
1111
Required parameters:
1212
@param user_id: User who added the item to the cart
1313
1414
@param item_id: Item added to the cart
1515
1616
17-
Optional parameters (given as dictionary C{optional}):
17+
Optional parameters:
1818
@param timestamp: UTC timestamp of the cart addition as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
1919
20-
@param cascadeCreate: Sets whether the given user/item should be created if not present in the database.
20+
@param cascade_create: Sets whether the given user/item should be created if not present in the database.
2121
2222
"""
2323
self.user_id = user_id
2424
self.item_id = item_id
25-
self.timestamp = optional.get('timestamp')
26-
self.cascade_create = optional.get('cascadeCreate')
27-
for par in optional:
28-
if not par in {"timestamp","cascadeCreate"}:
29-
raise ValueError("Unknown parameter %s was given to the request" % par)
25+
self.timestamp = timestamp
26+
self.cascade_create = cascade_create
3027
self.timeout = 1000
3128
self.ensure_https = False
3229
self.method = 'post'

recombee_api_client/api_requests/add_detail_view.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,27 @@ class AddDetailView(Request):
66
77
"""
88

9-
def __init__(self,user_id, item_id, optional = dict()):
9+
def __init__(self,user_id, item_id, timestamp=None, duration=None, cascade_create=None):
1010
"""
1111
Required parameters:
1212
@param user_id: User who viewed the item
1313
1414
@param item_id: Viewed item
1515
1616
17-
Optional parameters (given as dictionary C{optional}):
17+
Optional parameters:
1818
@param timestamp: UTC timestamp of the view as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
1919
2020
@param duration: Duration of the view
2121
22-
@param cascadeCreate: Sets whether the given user/item should be created if not present in the database.
22+
@param cascade_create: Sets whether the given user/item should be created if not present in the database.
2323
2424
"""
2525
self.user_id = user_id
2626
self.item_id = item_id
27-
self.timestamp = optional.get('timestamp')
28-
self.duration = optional.get('duration')
29-
self.cascade_create = optional.get('cascadeCreate')
30-
for par in optional:
31-
if not par in {"timestamp","duration","cascadeCreate"}:
32-
raise ValueError("Unknown parameter %s was given to the request" % par)
27+
self.timestamp = timestamp
28+
self.duration = duration
29+
self.cascade_create = cascade_create
3330
self.timeout = 1000
3431
self.ensure_https = False
3532
self.method = 'post'

recombee_api_client/api_requests/add_purchase.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,24 @@ class AddPurchase(Request):
66
77
"""
88

9-
def __init__(self,user_id, item_id, optional = dict()):
9+
def __init__(self,user_id, item_id, timestamp=None, cascade_create=None):
1010
"""
1111
Required parameters:
1212
@param user_id: User who purchased the item
1313
1414
@param item_id: Purchased item
1515
1616
17-
Optional parameters (given as dictionary C{optional}):
17+
Optional parameters:
1818
@param timestamp: UTC timestamp of the purchase as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
1919
20-
@param cascadeCreate: Sets whether the given user/item should be created if not present in the database.
20+
@param cascade_create: Sets whether the given user/item should be created if not present in the database.
2121
2222
"""
2323
self.user_id = user_id
2424
self.item_id = item_id
25-
self.timestamp = optional.get('timestamp')
26-
self.cascade_create = optional.get('cascadeCreate')
27-
for par in optional:
28-
if not par in {"timestamp","cascadeCreate"}:
29-
raise ValueError("Unknown parameter %s was given to the request" % par)
25+
self.timestamp = timestamp
26+
self.cascade_create = cascade_create
3027
self.timeout = 1000
3128
self.ensure_https = False
3229
self.method = 'post'

recombee_api_client/api_requests/add_rating.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class AddRating(Request):
66
77
"""
88

9-
def __init__(self,user_id, item_id, rating, optional = dict()):
9+
def __init__(self,user_id, item_id, rating, timestamp=None, cascade_create=None):
1010
"""
1111
Required parameters:
1212
@param user_id: User who submitted the rating
@@ -16,20 +16,17 @@ def __init__(self,user_id, item_id, rating, optional = dict()):
1616
@param rating: Rating rescaled to interval [-1.0,1.0], where -1.0 means the worst rating possible, 0.0 means neutral, and 1.0 means absolutely positive rating. For example, in the case of 5-star evaluations, rating = (numStars-3)/2 formula may be used for the conversion.
1717
1818
19-
Optional parameters (given as dictionary C{optional}):
19+
Optional parameters:
2020
@param timestamp: UTC timestamp of the rating as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
2121
22-
@param cascadeCreate: Sets whether the given user/item should be created if not present in the database.
22+
@param cascade_create: Sets whether the given user/item should be created if not present in the database.
2323
2424
"""
2525
self.user_id = user_id
2626
self.item_id = item_id
2727
self.rating = rating
28-
self.timestamp = optional.get('timestamp')
29-
self.cascade_create = optional.get('cascadeCreate')
30-
for par in optional:
31-
if not par in {"timestamp","cascadeCreate"}:
32-
raise ValueError("Unknown parameter %s was given to the request" % par)
28+
self.timestamp = timestamp
29+
self.cascade_create = cascade_create
3330
self.timeout = 1000
3431
self.ensure_https = False
3532
self.method = 'post'

recombee_api_client/api_requests/batch.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ class Batch(Request):
66
"""
77
Batch request for submitting an arbitrary sequence of requests
88
9-
In many cases, it may be desirable to execute multiple requests at once. By example, when synchronizing the catalog of items in periodical manner, you would have to execute a sequence of thousands of separate POST requests, which is very ineffective and may take a very long time to complete. Most notably, network latencies can make execution of such a sequence very slow and even if executed in multiple parallel threads, there will still be unreasonable overhead caused by the HTTP(s). To avoid the problems mentioned, batch processing may be used, encapsulating a sequence of requests into a single HTTP request.
10-
Batch processing allows you to submit arbitrary sequence of requests in form of JSON array. Any type of request from the above documentation may be used in the batch, and the batch may combine different types of requests arbitrarily as well.
11-
Note that:
12-
- executing the requests in a batch is equivalent as if they were executed one-by-one individually; there are, however, many optimizations to make batch execution as fast as possible,
13-
- the status code of the batch request itself is 200 even if the individual requests result in error - you have to inspect the code values in the resulting array,
14-
- if the status code of the whole batch is not 200, then there is an error in the batch request itself; in such a case, the error message returned should help you to resolve the problem,
15-
- currently, batch size is limited to **10,000** requests; if you wish to execute even larger number of requests, please split the batch into multiple parts.
9+
In many cases, it may be desirable to execute multiple requests at once. By example, when synchronizing the catalog of items in periodical manner, you would have to execute a sequence of thousands of separate POST requests, which is very ineffective and may take a very long time to complete. Most notably, network latencies can make execution of such a sequence very slow and even if executed in multiple parallel threads, there will still be unreasonable overhead caused by the HTTP(s). To avoid the problems mentioned, batch processing may be used, encapsulating a sequence of requests into a single HTTP request.
10+
Batch processing allows you to submit arbitrary sequence of requests in form of JSON array. Any type of request from the above documentation may be used in the batch, and the batch may combine different types of requests arbitrarily as well.
11+
Note that:
12+
- executing the requests in a batch is equivalent as if they were executed one-by-one individually; there are, however, many optimizations to make batch execution as fast as possible,
13+
- the status code of the batch request itself is 200 even if the individual requests result in error - you have to inspect the code values in the resulting array,
14+
- if the status code of the whole batch is not 200, then there is an error in the batch request itself; in such a case, the error message returned should help you to resolve the problem,
15+
- currently, batch size is limited to **10,000** requests; if you wish to execute even larger number of requests, please split the batch into multiple parts.
1616
"""
1717

1818
def __init__(self,requests):

recombee_api_client/api_requests/delete_bookmark.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,21 @@ class DeleteBookmark(Request):
66
77
"""
88

9-
def __init__(self,user_id, item_id, optional = dict()):
9+
def __init__(self,user_id, item_id, timestamp=None):
1010
"""
1111
Required parameters:
1212
@param user_id: ID of the user who made the bookmark.
1313
1414
@param item_id: ID of the item of which was bookmarked.
1515
1616
17-
Optional parameters (given as dictionary C{optional}):
17+
Optional parameters:
1818
@param timestamp: Unix timestamp of the bookmark. If the `timestamp` is omitted, then all the bookmarks with given `userId` and `itemId` are deleted.
1919
2020
"""
2121
self.user_id = user_id
2222
self.item_id = item_id
23-
self.timestamp = optional.get('timestamp')
24-
for par in optional:
25-
if not par in {"timestamp"}:
26-
raise ValueError("Unknown parameter %s was given to the request" % par)
23+
self.timestamp = timestamp
2724
self.timeout = 1000
2825
self.ensure_https = False
2926
self.method = 'delete'

recombee_api_client/api_requests/delete_cart_addition.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,21 @@ class DeleteCartAddition(Request):
66
77
"""
88

9-
def __init__(self,user_id, item_id, optional = dict()):
9+
def __init__(self,user_id, item_id, timestamp=None):
1010
"""
1111
Required parameters:
1212
@param user_id: ID of the user who made the cart addition.
1313
1414
@param item_id: ID of the item of which was added to cart.
1515
1616
17-
Optional parameters (given as dictionary C{optional}):
17+
Optional parameters:
1818
@param timestamp: Unix timestamp of the cart addition. If the `timestamp` is omitted, then all the cart additions with given `userId` and `itemId` are deleted.
1919
2020
"""
2121
self.user_id = user_id
2222
self.item_id = item_id
23-
self.timestamp = optional.get('timestamp')
24-
for par in optional:
25-
if not par in {"timestamp"}:
26-
raise ValueError("Unknown parameter %s was given to the request" % par)
23+
self.timestamp = timestamp
2724
self.timeout = 1000
2825
self.ensure_https = False
2926
self.method = 'delete'

recombee_api_client/api_requests/delete_detail_view.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,21 @@ class DeleteDetailView(Request):
66
77
"""
88

9-
def __init__(self,user_id, item_id, optional = dict()):
9+
def __init__(self,user_id, item_id, timestamp=None):
1010
"""
1111
Required parameters:
1212
@param user_id: ID of the user who made the detail view.
1313
1414
@param item_id: ID of the item of which the details were viewed.
1515
1616
17-
Optional parameters (given as dictionary C{optional}):
17+
Optional parameters:
1818
@param timestamp: Unix timestamp of the detail view. If the `timestamp` is omitted, then all the detail views with given `userId` and `itemId` are deleted.
1919
2020
"""
2121
self.user_id = user_id
2222
self.item_id = item_id
23-
self.timestamp = optional.get('timestamp')
24-
for par in optional:
25-
if not par in {"timestamp"}:
26-
raise ValueError("Unknown parameter %s was given to the request" % par)
23+
self.timestamp = timestamp
2724
self.timeout = 1000
2825
self.ensure_https = False
2926
self.method = 'delete'

0 commit comments

Comments
 (0)