-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathexample_script.py
More file actions
143 lines (121 loc) · 3.67 KB
/
example_script.py
File metadata and controls
143 lines (121 loc) · 3.67 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# An example script showing the functionality of the TinCanPython Library
import uuid
from test.resources import lrs_properties
from tincan import (
RemoteLRS,
Statement,
Agent,
Verb,
Activity,
Context,
LanguageMap,
ActivityDefinition,
StateDocument,
)
# construct an LRS
print("constructing the LRS...")
lrs = RemoteLRS(
version=lrs_properties.version,
endpoint=lrs_properties.endpoint,
timeout=lrs_properties.timeout,
username=lrs_properties.username,
password=lrs_properties.password,
)
print("...done")
# construct the actor of the statement
print("constructing the Actor...")
actor = Agent(
name='UserMan',
mbox='mailto:tincanpython@tincanapi.com',
)
print("...done")
# construct the verb of the statement
print("constructing the Verb...")
verb = Verb(
id='http://adlnet.gov/expapi/verbs/experienced',
display=LanguageMap({'en-US': 'experienced'}),
)
print("...done")
# construct the object of the statement
print("constructing the Object...")
object = Activity(
id='http://tincanapi.com/TinCanPython/Example/0',
definition=ActivityDefinition(
name=LanguageMap({'en-US': 'TinCanPython Library'}),
description=LanguageMap({'en-US': 'Use of, or interaction with, the TinCanPython Library'}),
),
)
print("...done")
# construct a context for the statement
print("constructing the Context...")
context = Context(
registration=uuid.uuid4(),
instructor=Agent(
name='Lord TinCan',
mbox='mailto:lordtincan@tincanapi.com',
),
# language='en-US',
)
print("...done")
# construct the actual statement
print("constructing the Statement...")
statement = Statement(
actor=actor,
verb=verb,
object=object,
context=context,
)
print("...done")
# save our statement to the remote_lrs and store the response in 'response'
print("saving the Statement...")
response = lrs.save_statement(statement)
if not response:
raise ValueError("statement failed to save")
print("...done")
# retrieve our statement from the remote_lrs using the id returned in the response
print("Now, retrieving statement...")
response = lrs.retrieve_statement(response.content.id)
if not response.success:
raise ValueError("statement could not be retrieved")
print("...done")
print("constructing new Statement from retrieved statement data...")
ret_statement = response.content
print("...done")
# now, using our old statement and our returned statement, we can send multiple statements
# note: these statements are logically identical, but are 2 separate objects
print("saving both Statements")
response = lrs.save_statements([statement, ret_statement])
if not response:
raise ValueError("statements failed to save")
print("...done")
# we can query our statements using an object
# constructing the query object with common fields
# note: more information about queries can be found in the API documentation:
# docs/build/html/tincan.html#module-tincan.remote_lrs
query = {
"agent": actor,
"verb": verb,
"activity": object,
"related_activities": True,
"related_agents": True,
"limit": 2,
}
print("querying statements...")
response = lrs.query_statements(query)
if not response:
raise ValueError("statements could not be queried")
print("...done")
# now we will explore saving a document, e.g. a state document
print("constructing a state document...")
state_document = StateDocument(
activity=object,
agent=actor,
id='stateDoc',
content=bytearray('stateDocValue', encoding='utf-8'),
)
print("...done")
print("saving state document...")
response = lrs.save_state(state_document)
if not response.success:
raise ValueError("could not save state document")
print("...done")