~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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
Bonus exercises
=================

Described below are a variety of additional Python coding exercises.

Contents:

- Process config files.
- Process files whose names match a pattern.
- Ordered processing respecting dependencies.
- Process files in a directory and its sub-directories.
- Search for string pattern; print locations of matches.
- Send an email message from a Python application.
- Print Zip archive listing.
- Implement simple threads.
- Implement a simple server.
- Implement a REST-ful HTTP server.
- Create and use lightweight pseudothreads using gevent..
- Implement an XML-RPC server.
- Implement an XML-RPC client.
- Process an XML document with elementtree.
- Implement a function that can be created with an additional
  argument: a closure.
- Create an example of the use of the doctest module.
- Scan a log file and extract data items using a regular expression.


Details:

--------------------

Synopsis:
    Process config files (files that obey the .ini format):
    1. Print a report of several config files.
    2. Enable the user to enter section/name/value triples, then write
       a new config file.
    Sample config files are here -- config1.ini, config2.ini
Usage:
    python configfile_report_update.py [options] <file1> <file2> ...
Options:
    -o, --ouput     Output file name.  Default: tmp.ini
Hints:
    - ConfigParser module in the Python standard library
    - raw_input built-in function
Solution:
    Solutions/configfile_report_update.py

--------------------

Synopsis:
    1. Process all the files whose names match a path/pattern.  For
       each file, do the following -- Count the characters, the
       words, and the lines in the file.  Print these counts.
       ``glob``.
    2. Process all the files whose names match a path/pattern.  For
       each file, do the following -- Search each line for a
       pattern.  Print each line that matches.
Usage:
    python file_count_search.py count <file_name_pattern>
    python file_count_search.py search <text_pattern> <file_name_pattern>
Hints:
    - glob module in the Python standard library
    - re (regular expression) module in the Python standard library
Solution:
    Solutions/file_count_search.py

--------------------

Synopsis:
    Given a list of 2-tuples, process each item in the list in an
    order such that each item in the list is processed after the
    tuple whose first item is equal to the current tuple's second
    item, unless the tuple's second item is None, in which case it
    can always be processed.  In other words:

        1. Process (xx, bb) only after processing (bb, yy).
        2. Process (xx, None) in any order.

    Here is a sample list of tuples::

        Data1 = [
            ('aaa', 'mmm'),
            ('bbb', 'lll'),
            ('mmm', None),
            ('ccc', 'kkk'),
            ('lll', None),
            ('ddd', 'jjj'),
            ('iii', None),
            ('eee', 'aaa'),
            ('jjj', None),
            # comment out the following line to test for endless loop error.
            ('kkk', None),
        ]
Usage:
    python ordered_processed_list.py
Solution:
    Solutions/ordered_processed_list.py

--------------------

Synopsis:
    Recursively print out the names of files and directories and
    nested files and directories starting at a given path.
Usage:
    python report_files_directories.py <path>
Hints:
    - glob module in the Python standard library
    - os.path.isdir function
Solution:
    Solutions/report_files_directories.py

--------------------

Synopsis:
    Search a target string for a pattern.
    Print all the positions of the occurance of the pattern string
        within the target string.
    Implement the above in two different ways:
        1. Use a while: statement.
        2. Implement a generator function (a function that uses the
           yield statement).
Usage:
    python search_str.py
Solution:
    Solutions/search_str.py

--------------------

Synopsis:
    - Implement a class that can be initialized with a to-address, and
      can send messages to that address.
    - Implement a "factory" function that creates an instance of the above
      class.
    - Implement a helper function that uses the above class to send messages.
    - Repeat:
        - Prompt user for subject and body.
        - Send email given fixed from and to addresses.
Usage:
    python send_mail.py
Hints:
    - smtplib module in the Python standard library
    - raw_input built-in function
Solution:
    Solutions/send_mail.py

--------------------

Synopsis:
    Print formatted listing of Zip file.
Usage:
    python zip-ls.py [options] <zip_file_name>
Options:
    -h, --help      Display this help message.
    -s, --sort      Sort files by x, where x is one of:
                        file_name     or fn (default)
                        date_time     or dt
                        file_size     or fs
                        compress_size or cs
    -c, --columns   Columns to display.  Values:
                        s   Short; file names only.
                        m   Medium (default)
                        l   Long
                        v   Verbose
    -r, --reverse   Reverse sort
    -t, --totals    Print column totals.
    --no-totals     Do not print column totals.  (override .zip-lsrc)
Examples:
    python zip-ls.py stuff.zip
    python zip-ls.py -s file_name stuff.zip
    python zip-ls.py --sort=date_time --reverse stuff.zip
    python zip-ls.py -s compress_size -c m stuff.zip

zip-ls reads default options from ${HOME}/.zip-lsrc.  Example
of .zip-lsrc:

    [general]
    sort: file_name
    columns: v
    reverse: 0
    totals: 1
Hints:
    - zipfile module in the Python standard library
    - ConfigParser module in the Python standard library
Solution:
    Solutions/zip_ls.py

--------------------

Synopsis:
    Create 10 threads.
    Each thread does the following:
        1. Increment a global count and prints it.
        2. Sleeps (waits) for 3 seconds.
        3. Prints the global count and decrements it.
Usage:
    python simple_threads.py
Hints:
    - Thread and Lock from the threading module in the Python standard library
    - time.sleep() from the time module in the Python standard library
Solution:
    Solutions/simple_threads.py

--------------------

Synopsis:
    Use 3 (or more) worker threads to process a list of "jobs".
    Place the jobs in a queue (an instance of Queue.Queue).
    Enable the worker threads to process the jobs.
    Here is a list of jobs -- Multiply the first item of each
    tuple by the second:
        JobsInputlist = [
            (5, 5),
            (10, 4),
            (78, 5),
            (87, 2),
            (65, 4),
            (10, 10),
            (65, 2),
            (88, 95),
            (44, 55),
            (33, 3),
        ]
Usage:
    python threads_and_queue.py
Hints:
    - threading module in the Python standard library
    - Queue module in the Python standard library
    - time.sleep() from the time module in the Python standard library
Solution:
    Solutions/threads_and_queue.py

--------------------

Synopsis:
    Implement a server that uses a low level Python socket to echo a response.
    Use the "socket" module in the Python standard library.
Usage:
    python socket_echo_server.py
Resources:
    The Python socket module -- https://docs.python.org/2/library/socket.html
Solution:
    Solutions/socket_echo_server.py

--------------------

Synopsis:
    Implement a client that uses a low level Python socket to echo a response.
    Use the "socket" module in the Python standard library.
Usage:
    python socket_echo_client.py
Solution:
    Solutions/socket_echo_client.py

--------------------

Synopsis:
    A simple server that responds to a Web request with "pong".
Usage:
    python gevent1.py
    Then direct your Web browser to: http://localhost:8070/
Hints:
    - modules gevent and gevent.socket from the gevent prackage
Solution:
    Solutions/gevent1.py

--------------------

Synopsis:
    A REST-ful HTTP server app that response to the following commands:
        - "/" or "/help" --> display command help
        - "/ping"        --> display "pong"
        - "/chocolate"   --> display "vanilla"
        - "/sugar"       --> display "spice"
    Implement your "Web app" with gevent.
Usage:
    python gevent2.py
    Then direct your Web browser to: http://localhost:8088/
Resources:
    Comparison of asynchronous servers:
        http://nichol.as/asynchronous-servers-in-python
    Gevent homepage:
        http://gevent.org/
Solution:
    Solutions/gevent2.py

--------------------

Synopsis:
    Create and use lightweight pseudothreads.
    Run several greenlets (instances of gevent Greenlets) in parallel.
    Implement a subclass of gevent.Greenlet.  When an instance of
        this subclass runs, it should (1) sleep for several seconds, and
        then (2) return a value.
    Create several instances of that class and run them.  Print the
        value returned by each greenlet.
Options:
    -h, --help              Print this help message.
    -c, --command <cmd>     Command to run.  Possibles values are
                              "empty" and "parsexml".
    -t, --threads <n>       Number of threads to start.
Arguments:
    For command "empty" -- n -- number of seconds to delay.
    For command "parsexml" -- filename.xml -- XML document to parse.
Usage:
    python greenlet1.py
    python greenlet1.py -c empty -t 10 2
    python greenlet1.py -c parsexml -t 100 people_big.xml
Hints:
    - class Greenlet from the gevent package
Solution:
    Solutions/greenlet1.py

--------------------

Synopsis:
    Implement an XML-RPC server that responds to (serves) several
    mathematical functions, for example pow, sqrt, etc.
Usage:
    python xmlrpcserver1.py
Hints:
    - module SimpleXMLRPCServer in the Python standard library.
Solution:
    Solutions/xmlrpcserver1.py

--------------------

Synopsis:
    Implement an XML-RPC client that "calls" the functions provided by
        your XML-RPC server.
Usage:
    python xmlrpcclient1.py
Hints:
    - module SimpleXMLRPCServer in the Python standard library.
    - module xmlrpclib in the Python standard library.
Solution:
    Solutions/xmlrpcclient1.py

--------------------

Synopsis:
    Process an XML document with elementtree.
    Show the document tree.
    Modify the document tree and then show it again.
Usage:
    python elementtree_walk.py [options] infilename
Options:
    -h, --help      Display this help message.
Example:
    python elementtree_walk.py myxmldoc.xml
Hints:
    - xml.etree.ElementTree module in the Python standard library
    - Or, if you have installed it, use Lxml ()
Solution:
    Solutions/elementtree_walk.py
    Solutions/lxml_walk.py

--------------------

Synopsis:
    You need to implement a function that takes 1 argument, but you
        need a way to give that function an additional parameter.
    Create a closure -- Implement a function that takes 1 argument,
        but has an additional argument "packed" into it.
Usage:
    python closure1.py infilename
Options:
    -h, --help      Display this help message.
Example:
    python elementtree_walk.py myxmldoc.xml
Hints:
    - Write a nested function definition.
Solution:
    Solutions/closure1.py

--------------------

Synopsis:
    Test a function in a module with doctest.

    Define a function that takes two arguments: a string and an
    integer.  The function should convert the string to upper case
    and multiply it by the integer. It should then return the new
    string.

    Now, create a harness in the module containing the above
    function definition so that the function is tested with
    ``doctest``.
Usage:
    python doc_test.py
Example:
    python doc_test.py
Hints:
    - module doctest in the Python standard library.
Solution:
    Solutions/doc_test.py

--------------------

Synopsis:
    Scan a log file and extract data items using a regular expression.
    For example, from the following line:
        0. 2014.07.22 9:30 PM value1{data10} filler value2{data20} stuff
    extract:
        "data10" and "data20"
Usage:
    python scan_log01.py in_data_file
Examples:
    python scan_log01.py log_data01.txt
Solution:
    Solutions/scan_log01.py
Sample data:
    Solutions/log_data01.txt