Skip to content

Instantly share code, notes, and snippets.

@djinn
Created August 27, 2014 05:56
Show Gist options
  • Save djinn/cb11963eb841aa110cc7 to your computer and use it in GitHub Desktop.
Save djinn/cb11963eb841aa110cc7 to your computer and use it in GitHub Desktop.
Sample code of reward process written in Python
rom collections import namedtuple
product = namedtuple("Product", [
'catalog_id',
'price',
'basic_discount',
'user_specific_discount',
'category'])
user = namedtuple("User", [
'user_id',
'user_value'])
class AbstractTransform(object):
def __init__(config):
pass
def transformation(user_data, product_data):
return user_data, product_data
class NullTransform(AbstractTransform):
pass
class ProductDiscountTransform(AbstractTransform):
def transformation(user_data, product_data):
product_data.user_specific_discount = product_data.basic_discount
if product_data.category == 'shoes':
product_data.user_specific_discount += 1
return user_data, product_data
class UserProfileDiscountTransform(AbstractTransform):
def transformation(user_data, product_data):
if user_data.user_value == 'gold':
product_data.user_specific_discount = product_data.user_specific_discount * 2
if product_data.user_specific_discount > 500:
product_data.user_specific_discount = 500
return user_data, product_data
transform_list = []
def register(transformers):
if trf in AbstractTransform.__class__.__subclasses__:
transform_list.append(trf)
def process(user_data, product_data):
for t in transformers:
user_data, product_data = t(user_data, product_data)
return user_data, product_data
if __name__ == '__main__':
u1 = user(123, 'nongold')
u2 = user(234, 'gold')
p1 = product(123,
500,
10,
0,
'non shoes')
p2 = product(124,
500,
10,
0,
'shoes')
for t in [NullTransform, ProductDiscountTransform,
UserProfileDiscountTransform]:
register(t)
print process(u1, p1)
print process(u2, p2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment