forked from csev/py4e
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomment_xml.php
More file actions
127 lines (114 loc) · 4.23 KB
/
comment_xml.php
File metadata and controls
127 lines (114 loc) · 4.23 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
<?php
require_once('data/data_util.php');
use \Tsugi\Core\LTIX;
use \Tsugi\Util\LTI;
$sanity = array(
'urllib' => 'You should use urllib to retrieve the data from the URL'
);
// A random code
$code = $USER->id+$LINK->id+$CONTEXT->id;
// Set the data URLs
$sample_url = dataUrl('comments_42.xml');
$actual_url = dataUrl('comments_'.$code.'.xml');
// Compute the sum data
$json = getJsonOrDie(dataUrl('comments_42.json'));
$sum_sample = sumCommentJson($json);
$json = getJsonOrDie(dataUrl('comments_'.$code.'.json'));
$sum = sumCommentJson($json);
$oldgrade = $RESULT->grade;
if ( isset($_POST['sum']) && isset($_POST['code']) ) {
$RESULT->setJsonKey('code', $_POST['code']);
if ( $_POST['sum'] != $sum ) {
$_SESSION['error'] = "Your sum did not match";
header('Location: '.addSession('index.php'));
return;
}
$val = validate($sanity, $_POST['code']);
if ( is_string($val) ) {
$_SESSION['error'] = $val;
header('Location: '.addSession('index.php'));
return;
}
LTIX::gradeSendDueDate(1.0, $oldgrade, $dueDate);
// Redirect to ourself
header('Location: '.addSession('index.php'));
return;
}
// echo($goodsha);
if ( $RESULT->grade > 0 ) {
echo('<p class="alert alert-info">Your current grade on this assignment is: '.($RESULT->grade*100.0).'%</p>'."\n");
}
if ( $dueDate->message ) {
echo('<p style="color:red;">'.$dueDate->message.'</p>'."\n");
}
?>
<p>
<b>Extracting Data from XML</b>
<p>
In this assignment you will write a Python program somewhat similar to
<a href="http://www.pythonlearn.com/code/geoxml.py" target="_blank">http://www.pythonlearn.com/code/geoxml.py</a>.
The program will prompt for a URL, read the XML data from that URL using
<b>urllib</b> and then parse and extract the comment counts from the XML data,
compute the sum of the numbers in the file.
</p>
<p>
We provide two files for this assignment. One is a sample file where we give you the sum for your
testing and the other is the actual data you need to process for the assignment.
<ul>
<li> Sample data: <a href="<?= deHttps($sample_url) ?>" target="_blank"><?= deHttps($sample_url) ?></a>
(Sum=<?= $sum_sample ?>) </li>
<li> Actual data: <a href="<?= deHttps($actual_url) ?>" target="_blank"><?= deHttps($actual_url) ?></a>
(Sum ends with <?= $sum%100 ?>)<br/> </li>
</ul>
You do not need to save these files to your folder since your
program will read the data directly from the URL.
<b>Note:</b> Each student will have a distinct data url for the assignment - so only use your
own data url for analysis.
</p>
<b>Data Format and Approach</b>
<p>
The data consists of a number of names and comment counts in XML as follows:
<pre>
<comment>
<name>Matthias</name>
<count>97</count>
</comment>
</pre>
You are to look through all the <comment> tags and find the <count> values
sum the numbers.
The closest sample code that shows how to parse XML is
<a href="http://www.pythonlearn.com/code/geoxml.py" target="_blank">geoxml.py</a>.
But since the nesting of the elements in our data is different than the data
we are parsing in that sample code you will have to make real changes to the code.
</p>
<p>
To make the code a little simpler, you can use an XPath selector string to
look through the entire tree of XML for any tag named 'count' with the following
line of code:
<pre>
counts = tree.findall('.//count')
</pre>
Take a look at the Python ElementTree documentation and look for the supported XPath
syntax for details. You could also work from the top of the XML down to the comments
node and then loop through the child nodes of the comments node.
</p>
<p><b>Sample Execution</b></p>
<p>
<pre>
$ python solution.py
Enter location: http://python-data.dr-chuck.net/comments_42.xml
Retrieving http://python-data.dr-chuck.net/comments_42.xml
Retrieved 4204 characters
Count: 50
Sum: 2...
</pre>
<?php httpsWarning($sample_url); ?>
<p><b>Turning in the Assignment</b></p>
<form method="post">
Enter the sum from the actual data and your Python code below:<br/>
Sum: <input type="text" size="20" name="sum">
(ends with <?= $sum%100 ?>)
<input type="submit" value="Submit Assignment"><br/>
Python code:<br/>
<textarea rows="20" style="width: 90%" name="code"></textarea><br/>
</form>