Skip to content

Commit 467c445

Browse files
committed
Merge pull request #73 from Nolan330/master
Added example script and dedicated example resource config file
2 parents e4c9341 + 4d611e8 commit 467c445

File tree

8 files changed

+172
-3
lines changed

8 files changed

+172
-3
lines changed

examples/__init__.py

Whitespace-only changes.

examples/example_script.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# An example script showing the functionality of the TinCanPython Library
2+
3+
import uuid
4+
from resources import lrs_properties
5+
from tincan import (
6+
RemoteLRS,
7+
Statement,
8+
Agent,
9+
Verb,
10+
Activity,
11+
Context,
12+
LanguageMap,
13+
ActivityDefinition,
14+
StateDocument,
15+
)
16+
17+
# construct an LRS
18+
print "constructing the LRS..."
19+
lrs = RemoteLRS(
20+
version=lrs_properties.version,
21+
endpoint=lrs_properties.endpoint,
22+
username=lrs_properties.username,
23+
password=lrs_properties.password,
24+
)
25+
print "...done"
26+
27+
# construct the actor of the statement
28+
print "constructing the Actor..."
29+
actor = Agent(
30+
name='UserMan',
31+
mbox='mailto:[email protected]',
32+
)
33+
print "...done"
34+
35+
# construct the verb of the statement
36+
print "constructing the Verb..."
37+
verb = Verb(
38+
id='http://adlnet.gov/expapi/verbs/experienced',
39+
display=LanguageMap({'en-US': 'experienced'}),
40+
)
41+
print "...done"
42+
43+
# construct the object of the statement
44+
print "constructing the Object..."
45+
object = Activity(
46+
id='http://tincanapi.com/TinCanPython/Example/0',
47+
definition=ActivityDefinition(
48+
name=LanguageMap({'en-US': 'TinCanPython Library'}),
49+
description=LanguageMap({'en-US': 'Use of, or interaction with, the TinCanPython Library'}),
50+
),
51+
)
52+
print "...done"
53+
54+
# construct a context for the statement
55+
print "constructing the Context..."
56+
context = Context(
57+
registration=uuid.uuid4(),
58+
instructor=Agent(
59+
name='Lord TinCan',
60+
mbox='mailto:[email protected]',
61+
),
62+
#language='en-US',
63+
)
64+
print "...done"
65+
66+
# construct the actual statement
67+
print "constructing the Statement..."
68+
statement = Statement(
69+
actor=actor,
70+
verb=verb,
71+
object=object,
72+
context=context,
73+
)
74+
print "...done"
75+
76+
# save our statement to the remote_lrs and store the response in 'response'
77+
print "saving the Statement..."
78+
response = lrs.save_statement(statement)
79+
80+
if not response:
81+
raise ValueError("statement failed to save")
82+
print "...done"
83+
84+
# retrieve our statement from the remote_lrs using the id returned in the response
85+
print "Now, retrieving statement..."
86+
response = lrs.retrieve_statement(response.content.id)
87+
88+
if not response.success:
89+
raise ValueError("statement could not be retrieved")
90+
print "...done"
91+
92+
print "constructing new Statement from retrieved statement data..."
93+
ret_statement = response.content
94+
print "...done"
95+
96+
# now, using our old statement and our returned statement, we can send multiple statements
97+
# note: these statements are logically identical, but are 2 separate objects
98+
print "saving both Statements"
99+
response = lrs.save_statements([statement, ret_statement])
100+
101+
if not response:
102+
raise ValueError("statements failed to save")
103+
print "...done"
104+
105+
# we can query our statements using an object
106+
# constructing the query object with common fields
107+
# note: more information about queries can be found in the API documentation:
108+
# docs/build/html/tincan.html#module-tincan.remote_lrs
109+
query = {
110+
"agent": actor,
111+
"verb": verb,
112+
"activity": object,
113+
"related_activities": True,
114+
"related_agents": True,
115+
"limit": 2,
116+
}
117+
118+
print "querying statements..."
119+
response = lrs.query_statements(query)
120+
121+
if not response:
122+
raise ValueError("statements could not be queried")
123+
print "...done"
124+
125+
# now we will explore saving a document, e.g. a state document
126+
print "constructing a state document..."
127+
state_document = StateDocument(
128+
activity=object,
129+
agent=actor,
130+
id='stateDoc',
131+
content=bytearray('stateDocValue', encoding='utf-8'),
132+
)
133+
print "...done"
134+
135+
print "saving state document..."
136+
response = lrs.save_state(state_document)
137+
138+
if not response.success:
139+
raise ValueError("could not save state document")
140+
print "...done"

examples/resources/__init__.py

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
Contains user-specific information for testing.
3+
"""
4+
5+
6+
endpoint="<endpoint>"
7+
version ="<valid_version>" # 1.0.1 | 1.0.0 | 0.95 | 0.9
8+
username="<username>"
9+
password="<password>"

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[metadata]
2-
description-file = README.md
2+
description-file = README.md

test/languagemap_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,4 @@ def mapVerificationHelper(self, lmap):
132132

133133
if __name__ == '__main__':
134134
suite = unittest.TestLoader().loadTestsFromTestCase(LanguageMapTest)
135-
unittest.TextTestRunner(verbosity=2).run(suite)
135+
unittest.TextTestRunner(verbosity=2).run(suite)

test/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ def assertSerializeDeserialize(self, obj, version=None):
4747
orig_dict = obj.__dict__
4848
clone_dict = clone.__dict__
4949

50-
self.assertEqual(orig_dict, clone_dict)
50+
self.assertEqual(orig_dict, clone_dict)

tincan/remote_lrs.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,26 @@ def query_statements(self, query):
282282
:type query: dict
283283
:return: LRS Response object with the returned StatementsResult object as content
284284
:rtype: :class:`tincan.lrs_response.LRSResponse`
285+
286+
.. note::
287+
Optional query parameters are\n
288+
**statementId:** (*str*) ID of the Statement to fetch\n
289+
**voidedStatementId:** (*str*) ID of the voided Statement to fetch\n
290+
**agent:** (*Agent* |*Group*) Filter to return Statements for which the specified Agent or Group is the Actor\n
291+
**verb:** (*Verb id IRI*) Filter to return Statements matching the verb id\n
292+
**activity:** (*Activity id IRI*) Filter to return Statements for which the specified Activity is the Object\n
293+
**registration:** (*UUID*) Filter to return Statements matching the specified registration ID\n
294+
**related_activities:** (*bool*) Include Statements for which the Object, Context Activities or any Sub-Statement
295+
properties match the specified Activity\n
296+
**related_agents:** (*bool*) Include Statements for which the Actor, Object, Authority, Instructor, Team, or
297+
any Sub-Statement properties match the specified Agent\n
298+
**since:** (*datetime*) Filter to return Statements stored since the specified datetime\n
299+
**until:** (*datetime*) Filter to return Statements stored at or before the specified datetime\n
300+
**limit:** (*positive int*) Allow <limit> Statements to be returned. 0 indicates the maximum supported by the LRS\n
301+
**format:** (*str* {"ids"|"exact"|"canonical"}) Manipulates how the LRS handles importing and returning the statements\n
302+
**attachments:** (*bool*) If true, the LRS will use multipart responses and include all attachment data per Statement returned.
303+
Otherwise, application/json is used and no attachment information will be returned\n
304+
**ascending:** (*bool*) If true, the LRS will return results in ascending order of stored time (oldest first)\n
285305
"""
286306
params = {}
287307

0 commit comments

Comments
 (0)