forked from conductrics/conductrics-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconductrics.py
More file actions
51 lines (41 loc) · 1.4 KB
/
conductrics.py
File metadata and controls
51 lines (41 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import httplib
import json
from urlparse import urlparse
baseUrl = "http://api.conductrics.com"
apiKey = None
ownerCode = None
def _request(session, *parts, **args):
u = '/'.join(str(x) for x in (baseUrl, ownerCode) + parts)
if len(args):
u += "?" + "&".join("%s=%s"%item for item in args.items())
u = urlparse(u)
h = httplib.HTTPConnection(u.netloc, timeout=2)
h.request("GET", u.path + "?" + u.query, headers = {
"x-mpath-apikey": apiKey,
"x-mpath-session": session
})
return json.loads(h.getresponse().read())
class Agent:
def __init__(self, name):
self.name = name
def decide(self, session, *choices, **opts):
try:
decision = _request(session, self.name, "decision", len(choices), **opts)
return choices[int(decision['decision'])]
except:
return choices[0]
def reward(self, session, goalCode="goal-1", value=1.0):
return _request(session, self.name, "goal", goalCode, reward=value)
def expire(self, session):
return _request(session, self.name, "expire")
if __name__ == "__main__":
import uuid
apiKey = "api-nQyALpnyPsZHQrVbvtOhZpYz"
ownerCode = "owner_sxvgyHUlj"
a = Agent("python-test-features")
sessionId = str(uuid.uuid4())
print( a.decide(sessionId, "a", "b", features="ugly") )
print( a.reward(sessionId, value=.8) )
sessionId = str(uuid.uuid4())
print( a.decide(sessionId, "a", "b", features="handsome") )
print( a.reward(sessionId, value=1.2) )