~dkuhlman/python-training-materials/Materials

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.12: http://docutils.sourceforge.net/" />
<title>Document server</title>
<style type="text/css">

/* css */

body {
  font: 90% 'Lucida Grande', Verdana, Geneva, Lucida, Arial, Helvetica, sans-serif;
  background: #ffffff;
  color: black;
  margin: 2em;
  padding: 2em;
}

a[href] {
  color: #436976;
  background-color: transparent;
}

a.toc-backref {
  text-decoration: none;
}

h1 a[href] {
  text-decoration: none;
  color: #fcb100;
  background-color: transparent;
}

a.strong {
  font-weight: bold;
}

img {
  margin: 0;
  border: 0;
}

p {
  margin: 0.5em 0 1em 0;
  line-height: 1.5em;
}
p a {
  text-decoration: underline;
}
p a:visited {
  color: purple;
  background-color: transparent;
}
p a:active {
  color: red;
  background-color: transparent;
}
a:hover {
  text-decoration: none;
}
p img {
  border: 0;
  margin: 0;
}

h1, h2, h3, h4, h5, h6 {
  color: #003a6b;
  background-color: transparent;
  font: 100% 'Lucida Grande', Verdana, Geneva, Lucida, Arial, Helvetica, sans-serif;
  margin: 0;
  padding-top: 0.5em;
}

h1 {
  font-size: 160%;
  margin-bottom: 0.5em;
  border-bottom: 1px solid #fcb100;
}
h2 {
  font-size: 140%;
  margin-bottom: 0.5em;
  border-bottom: 1px solid #aaa;
}
h3 {
  font-size: 130%;
  margin-bottom: 0.5em;
  text-decoration: underline;
}
h4 {
  font-size: 110%;
  font-weight: bold;
}
h5 {
  font-size: 100%;
  font-weight: bold;
}
h6 {
  font-size: 80%;
  font-weight: bold;
}

ul a, ol a {
  text-decoration: underline;
}

dt {
  font-weight: bold;
}
dt a {
  text-decoration: none;
}

dd {
  line-height: 1.5em;
  margin-bottom: 1em;
}

legend {
  background: #ffffff;
  padding: 0.5em;
}

form {
  margin: 0;
}


dl.form {
  margin: 0;
  padding: 1em;
}

dl.form dt {
  width: 30%;
  float: left;
  margin: 0;
  padding: 0 0.5em 0.5em 0;
  text-align: right;
}

input {
  font: 100% 'Lucida Grande', Verdana, Geneva, Lucida, Arial, Helvetica, sans-serif;
  color: black;
  background-color: white;
  vertical-align: middle;
}

abbr, acronym, .explain {
  color: black;
  background-color: transparent;
}

q, blockquote {
}

code, pre {
  font-family: monospace;
  font-size: 1.2em;
  display: block;
  padding: 10px;
  border: 1px solid #838183;
  background-color: #eee;
  color: #000;
  overflow: auto;
  margin: 0.5em 1em;
}

div.admonition, div.attention, div.caution, div.danger, div.error,
div.hint, div.important, div.note, div.tip, div.warning {
  margin: 2em ;
  border: medium outset ;
  padding: 1em }

div.admonition p.admonition-title, div.hint p.admonition-title,
div.important p.admonition-title, div.note p.admonition-title,
div.tip p.admonition-title {
  font-weight: bold ;
  font-family: sans-serif }

div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title {
  color: red ;
  font-weight: bold ;
  font-family: sans-serif }

tt.docutils {
  background-color: #dddddd;
}

ul.auto-toc {
  list-style-type: none }

</style>
</head>
<body>
<div class="document" id="document-server">
<h1 class="title">Document server</h1>

<div class="section" id="preliminaries">
<h1>Preliminaries</h1>
<p>We will be building an application to retrieve and store documents.
We will implement an access library/module, several servers, and
several clients.</p>
<p>The document store is an SQLite relational database.  It is file
based.</p>
<p>The document store will contain the following information:</p>
<ol class="arabic simple">
<li>The <tt class="docutils literal">documents</tt> table:<ol class="arabic">
<li>id</li>
<li>tags</li>
<li>description</li>
<li>body -- The document (body) itself.</li>
</ol>
</li>
<li>The <tt class="docutils literal">config</tt> table:<ol class="arabic">
<li>latest_id</li>
</ol>
</li>
</ol>
<p>There is a sample database containing a few documents (records) in
<tt class="docutils literal">./Data/documents01.sqlite</tt>.</p>
<p>You can create a new, empty database by running
<tt class="docutils literal">create_doc_database.py</tt>.</p>
<p>Here are some SQL queries that you will find useful:</p>
<pre class="literal-block">
# Get and update config (latest_id).
sql = 'select latest_id from config'
sql = 'update config set latest_id={}'.format(new_id)
#
# Get id, tags, and description for all documents.
sql = 'select id, tags, description from documents'
#
# Get all fields for a specific document by ID.
sql = 'select * from documents where id = {}'.format(id)
#
# Insert a new document into the database.
sql = 'insert into documents values ({}, &quot;{}&quot;, &quot;{}&quot;, &quot;{}&quot;)'.format(
    new_id, tags, description, body)
</pre>
<p>More information about SQLite: <a class="reference external" href="http://www.sqlite.org/docs.html">http://www.sqlite.org/docs.html</a></p>
</div>
<div class="section" id="exercises">
<h1>Exercises</h1>
<p>Now, do each of the following:</p>
<ol class="arabic">
<li><p class="first">Implement a module/library to access and add records to a
document repository stored in a SQLite relational file/database.
Support the following API:</p>
<ul class="simple">
<li><tt class="docutils literal">list()</tt> -- Provide a list of the ids, tags, and descriptions
for all the documents/records in the database.</li>
<li><tt class="docutils literal">get(id)</tt> -- Retrieve a record by ID.</li>
<li><tt class="docutils literal">search(tag)</tt> -- Return the ids, tags, and descriptions for
all records whose tags field contains <tt class="docutils literal">tag</tt>.</li>
<li><tt class="docutils literal">add(tags, description, body)</tt> -- Add a new record to the
database.</li>
</ul>
<p>The file <tt class="docutils literal">create_doc_database.py</tt> can be used to create and
initialize a new empty SQLite document database file.</p>
</li>
<li><p class="first">Implement a REST-ful document server.  Build your server on
ZeroMQ.</p>
<p>Your document server should provide these capabilities:</p>
<ul class="simple">
<li>Return a list of all the documents in the database.  Return
these fields: ID, tags, description.</li>
<li>Search for documents and return a set of document IDs (and
possibly their descriptions).</li>
<li>Retrieve a document by ID.</li>
<li>Add a new document given tags, description, and the body
(contents) of a file to store in the database.</li>
</ul>
<p>You can find templates for the ZeroMQ parts in
<tt class="docutils literal">Templates/hwserver.py</tt> and <tt class="docutils literal">Templates/hwclient.py</tt></p>
</li>
<li><p class="first">Implement a ZeroMQ client for your document server.  The client
should be able to perform these functions:</p>
<ol class="arabic simple">
<li>Retrieve a list of all the documents in the database.</li>
<li>Search a set of documents, given a tag.</li>
<li>Retrieve a document, given its ID.</li>
<li>Add a new document, given the tags, description, and name of a
file to be stored.</li>
</ol>
</li>
<li><p class="first">Implement a command line shell as a client for the document
store.  Use the <tt class="docutils literal">cmd</tt> module from the standard Python library.
Your command line shell can either (1) access your library/module
directly or (2) use your ZeroMQ client.</p>
<p>In your command line shell, implement the same commands: list,
search, get, and add.</p>
<p>Optional task -- Use one of the Python command line parsers
(getopt or argparse) to parse the command line passed into this
program.  Implement support for options &quot;-v&quot;, &quot;--verbose&quot;, &quot;-h&quot;,
and &quot;--help&quot;.</p>
</li>
<li><p class="first">Implement a Web application server that provides access to the
documents in our document store.  Build your Web application with
Pyramid.</p>
<p>Your Web application server should support these operations
(URLs):</p>
<pre class="literal-block">
/help -- Show this help.
/list -- List all documents.
/search/{tag} -- Search for and show documents by tag.
/get/{id} -- Get and show document by ID.
</pre>
<p>You can create a starter Pyramid application with the following
commands:</p>
<pre class="literal-block">
$ pcreate --help
$ pcreate --list
$ pcreate --scaffold=starter my_doc_server
$ cd my_doc_server
$ python setup.py develop
</pre>
<p>Now start the server with the following:</p>
<pre class="literal-block">
$ pserve development.ini
</pre>
<p>Then visit <tt class="docutils literal"><span class="pre">http://localhost:6543</span></tt> in your Web browser to see
whether your application is good.</p>
</li>
</ol>
<!-- vim:ft=rst: -->
</div>
</div>
<div class="footer">
<hr class="footer" />
<a class="reference external" href="docserver.txt">View document source</a>.
Generated on: 2015-10-07 20:01 UTC.
Generated by <a class="reference external" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.

</div>
</body>
</html>