Repository navigation
Expand file tree
/
Copy pathhtml_renderer.html
More file actions
800 lines (673 loc) · 71.1 KB
/
Copy pathhtml_renderer.html
File metadata and controls
800 lines (673 loc) · 71.1 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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Renderer Exercise — Programming in Python 7.0 documentation</title>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Tutorial for the Html Render Assignment" href="html_renderer_tutorial.html" />
<link rel="prev" title="Subclassing and Inheritance" href="../modules/SubclassingAndInheritance.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" style="background: #4b2e83" >
<a href="../index.html">
<img src="../_static/UWPCE_logo_full.png" class="logo" alt="Logo"/>
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">Topics in the Program</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../topics/01-setting_up/index.html">1. Setting up your Environment</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/02-basic_python/index.html">2. Basic Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/03-recursion_booleans/index.html">3. Booleans and Recursion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/04-sequences_iteration/index.html">4. Sequences and Iteration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/05-text_handling/index.html">5. Basic Text Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/06-exceptions/index.html">6. Exception Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/07-unit_testing/index.html">7. Unit Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/08-dicts_sets/index.html">8. Dictionaries and Sets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/09-files/index.html">9. File Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/10-modules_packages/index.html">10. Modules and Packages</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/11-argument_passing/index.html">11. Advanced Argument Passing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/12-comprehensions/index.html">12. Comprehensions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/13-intro_oo/index.html">13. Intro to Object Oriented Programing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/14-magic_methods/index.html">14. Properties and Magic Methods</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../topics/15-subclassing/index.html">15. Subclassing and Inheritance</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../modules/SubclassingAndInheritance.html">Subclassing and Inheritance</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">HTML Renderer Exercise</a></li>
<li class="toctree-l2"><a class="reference internal" href="html_renderer_tutorial.html">Tutorial for the Html Render Assignment</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../topics/16-multiple_inheritance/index.html">16. Multiple Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/17-functional_programming/index.html">17. Introduction to Functional Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/18-advanced_testing/index.html">18. Advanced Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/99-extras/index.html">19. Extra Topics</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Programming in Python</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content style-external-links">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a> »</li>
<li><a href="../topics/15-subclassing/index.html"><span class="section-number">15. </span>Subclassing and Inheritance</a> »</li>
<li>HTML Renderer Exercise</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/exercises/html_renderer.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<div class="rst-breadcrumbs-buttons" role="navigation" aria-label="breadcrumb navigation">
<a href="html_renderer_tutorial.html" class="btn btn-neutral float-right" title="Tutorial for the Html Render Assignment" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="../modules/SubclassingAndInheritance.html" class="btn btn-neutral float-left" title="Subclassing and Inheritance" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="html-renderer-exercise">
<span id="exercise-html-renderer"></span><h1>HTML Renderer Exercise<a class="headerlink" href="#html-renderer-exercise" title="Permalink to this headline">¶</a></h1>
<p>Ever need to write some HTML? Maybe from data?</p>
<p>And not want to write all those tags yourself?</p>
<p>OK, maybe not – but trust me, it’s a pain – let’s write some code to do it for us.</p>
<div class="section" id="html-renderer">
<h2>HTML Renderer<a class="headerlink" href="#html-renderer" title="Permalink to this headline">¶</a></h2>
<div class="section" id="goal">
<h3>Goal:<a class="headerlink" href="#goal" title="Permalink to this headline">¶</a></h3>
<p>The goal is to create a set of classes to render html pages – in a “pretty printed” way.</p>
<p>i.e. nicely indented and human readable.</p>
<p>We’ll try to get to all the features required to render this file:</p>
<p><a class="reference download internal" download="" href="../_downloads/6b2570b221e822929ed51f41075b604c/sample_html.html"><code class="xref download docutils literal notranslate"><span class="pre">sample_html.html</span></code></a></p>
<p>Take a look at it by opening it in your text editor. And also in a browser to see how it’s rendered.</p>
<p>If you don’t know html – just look at the example and copy that. And you can read the: <a class="reference internal" href="#html-primer"><span class="std std-ref">HTML Primer</span></a> at the end of this page for enough to do this exercise. And anyone working with computers these days would benefit from at least a passing familiarity with html.</p>
<p>The exercise is broken down into a number of steps – each requiring a few more OO concepts in Python.</p>
<p>The goal of the code is to render html. The goal of the <em>exercise</em> is to build up a simple object hierarchy with:</p>
<ul class="simple">
<li><p>classes</p></li>
<li><p>class attributes</p></li>
<li><p>instance attributes</p></li>
<li><p>methods</p></li>
<li><p>subclassing</p></li>
<li><p>overriding attributes and methods</p></li>
</ul>
</div>
<div class="section" id="general-instructions">
<h3>General Instructions:<a class="headerlink" href="#general-instructions" title="Permalink to this headline">¶</a></h3>
<p>You can start with the framework in:</p>
<p><a class="reference download internal" download="" href="../_downloads/7b9029310dd0d29fb92c3c68c1bfaf1d/html_render.py"><code class="xref download docutils literal notranslate"><span class="pre">html_render.py</span></code></a></p>
<p>For each step, add the required functionality. There is example code to run your code for each step in:</p>
<p><a class="reference download internal" download="" href="../_downloads/fc2042604ef431b3938842700934faf5/run_html_render.py"><code class="xref download docutils literal notranslate"><span class="pre">run_html_render.py</span></code></a></p>
<p>You should be able to run that code at each step, uncommenting each new step in <code class="docutils literal notranslate"><span class="pre">run_html_render.py</span></code> as you go.</p>
<p>It builds up an html tree, and then calls the <code class="docutils literal notranslate"><span class="pre">render()</span></code> method of your element to render the page.</p>
<p>It uses a <code class="docutils literal notranslate"><span class="pre">StringIO</span></code> object (like a file, but in memory) to render to memory, then dumps it to the console, and writes a file. Take a look at the render_page function at the top of the file to make sure you understand it.</p>
<p>The html generated at each step will be written to the files named:
<code class="docutils literal notranslate"><span class="pre">test_html_ouput?.html</span></code></p>
</div>
<div class="section" id="unit-tests">
<h3>Unit tests<a class="headerlink" href="#unit-tests" title="Permalink to this headline">¶</a></h3>
<p>Running the <code class="docutils literal notranslate"><span class="pre">run_html_render.py</span></code> script is a (simple) form of integration testing – it checks how the individual components are working together. But we also want to make sure each individual <em>unit</em> (class, method) of code works. So to do that, we’ll use:</p>
<p><strong>Test Driven Development</strong></p>
<p>In addition to checking if the output is what you expect with the running script – you should also write unit tests as you go.</p>
<p>Each new line of code should have a test that will run it – <em>before</em> you write that code.</p>
<p>That is:</p>
<blockquote>
<div><ol class="arabic simple">
<li><p>write a test that exercises the next step in your process</p></li>
<li><p>run the tests – the new test will fail</p></li>
<li><p>write your code…</p></li>
<li><p>run the tests. If it still fails, go back to step 3…</p></li>
</ol>
</div></blockquote>
<p>A start of a test file is provided here:</p>
<p><a class="reference download internal" download="" href="../_downloads/051b47da30d80fc908192323355db0cf/test_html_render.py"><code class="xref download docutils literal notranslate"><span class="pre">test_html_render.py</span></code></a></p>
<p>It has a few tests for the first few steps – uncomment them as you go along.</p>
<p>But it is NOT comprehensive – you will need to add more tests at every step!</p>
<p>You can run <code class="docutils literal notranslate"><span class="pre">pytest</span></code> on that test file first thing – it should pass two tests (that you can create an Element object – not that it works) and fail one – one that actually starts to test functionality.</p>
<p><strong>NOTE</strong> if you are lost, take a look at the tutorial here:
<a class="reference internal" href="html_renderer_tutorial.html#html-renderer-tutorial"><span class="std std-ref">Tutorial for the Html Render Assignment</span></a>. But do try to do it yourself first.</p>
</div>
<div class="section" id="step-0">
<h3>Step 0:<a class="headerlink" href="#step-0" title="Permalink to this headline">¶</a></h3>
<p>Before you can start writing code, you need to get setup.</p>
<ul class="simple">
<li><p>In your directory in the class repo called <code class="docutils literal notranslate"><span class="pre">lesson07</span></code></p></li>
<li><p>In that directory, you can start working on the code. Start by putting the files you just downloaded in that dir:</p>
<ul>
<li><p><code class="docutils literal notranslate"><span class="pre">html_render.py</span></code>, <code class="docutils literal notranslate"><span class="pre">run_html_render.py</span></code>,
<code class="docutils literal notranslate"><span class="pre">sample_html.html</span></code>, <code class="docutils literal notranslate"><span class="pre">test_html_render.py</span></code></p></li>
</ul>
</li>
<li><p>Add those files to your git repo:</p>
<ul>
<li><p><code class="docutils literal notranslate"><span class="pre">git</span> <span class="pre">add</span> <span class="pre">*.py</span> <span class="pre">sample_html.html</span></code></p></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="step-1">
<h3>Step 1:<a class="headerlink" href="#step-1" title="Permalink to this headline">¶</a></h3>
<p class="rubric">1a.</p>
<p>Create an <code class="docutils literal notranslate"><span class="pre">Element</span></code> class for rendering an html element (xml element).</p>
<p>There is a skeleton for one in <code class="docutils literal notranslate"><span class="pre">html_render.py</span></code> – it has just enough so that the first few tests in <code class="docutils literal notranslate"><span class="pre">test_html_render.py</span></code> can run – though that won’t pass!</p>
<p>Do run those tests first – then add the code to make them pass.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">Element</span></code> class should have a class attribute for the tag name (“html” first).</p>
<p>The initializer signature should look like:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">Element</span><span class="p">(</span><span class="n">content</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
</pre></div>
</div>
<p>Where <code class="docutils literal notranslate"><span class="pre">content</span></code> is expected to be a string – and defaults to nothing.</p>
<p class="rubric">1b.</p>
<p>The class should have an <code class="docutils literal notranslate"><span class="pre">append</span></code> method that can add another string to the content.</p>
<p>(The <code class="docutils literal notranslate"><span class="pre">html_render.py</span></code> file you downloaded above should have a skeleton for this class in it.)</p>
<p>So your class will need a way to store the content in a way that you can keep adding more to it.</p>
<p>An <code class="docutils literal notranslate"><span class="pre">Element</span></code> object has to collect a bunch of sub-elements, in order, and you need to be able to append new ones to it – sounds like a <code class="docutils literal notranslate"><span class="pre">list</span></code>, doesn’t it? So should it subclass from <code class="docutils literal notranslate"><span class="pre">list</span></code>?</p>
<p>Ask yourself – does this make sense? an “Element <em>is</em> a list” – no.</p>
<p>But “An Element <em>uses</em> a list” makes perfect sense.</p>
<p>If the <em>is</em> phrase makes sense, then subclassing would makes sense. If the <em>uses</em> phrase makes sense, <em>then</em> you would not want to subclass.</p>
<p>So no – you don’t want <code class="docutils literal notranslate"><span class="pre">Element</span></code> to subclass from list.</p>
<p class="rubric">1c.</p>
<p>It should have a <code class="docutils literal notranslate"><span class="pre">render(file_out)</span></code> method that renders the tag and the strings in the content.</p>
<p><code class="docutils literal notranslate"><span class="pre">file_out</span></code> could be any open, writable file-like object ( i.e. have a <code class="docutils literal notranslate"><span class="pre">write()</span></code> method ). This is what you get from the <code class="docutils literal notranslate"><span class="pre">open()</span></code> function – but there are other kinds of file-like objects. The html will be rendered to this file-like object.</p>
<p><strong>NOTE:</strong> html is not sensitive to newlines – but you don’t want all your html on one line. so put a newline in after each tag and each content string. Later on in the assignment, you’ll add indentation as well!</p>
<p>So this <code class="docutils literal notranslate"><span class="pre">render()</span></code> method takes a file-like object, and calls its <code class="docutils literal notranslate"><span class="pre">write()</span></code> method, writing the html for a tag.</p>
<p>Once this works, this code:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">page</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s2">"Some content"</span><span class="p">)</span>
<span class="n">page</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">"Some more contenet"</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"test.html"</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">outfile</span><span class="p">:</span>
<span class="n">page</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">outfile</span><span class="p">)</span>
</pre></div>
</div>
<p>Will result in a file with something like this in it:</p>
<div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">html</span><span class="p">></span>
Some content.
Some more content.
<span class="p"></</span><span class="nt">html</span><span class="p">></span>
</pre></div>
</div>
<p>That is, you should now be able to render an html tag with text in it as content.</p>
<p>See: step 1. in <code class="docutils literal notranslate"><span class="pre">run_html_render.py</span></code> and the test code.</p>
<p>If you are stuck – see the tutorial: <a class="reference internal" href="html_renderer_tutorial.html#render-tutorial-1"><span class="std std-ref">Step 1:</span></a></p>
</div>
<div class="section" id="step-2">
<h3>Step 2:<a class="headerlink" href="#step-2" title="Permalink to this headline">¶</a></h3>
<div class="section" id="part-a">
<h4>Part A:<a class="headerlink" href="#part-a" title="Permalink to this headline">¶</a></h4>
<p>Create a couple subclasses of <code class="docutils literal notranslate"><span class="pre">Element</span></code>, for each of <code class="docutils literal notranslate"><span class="pre"><html></span></code>, <code class="docutils literal notranslate"><span class="pre"><body></span></code>, and <code class="docutils literal notranslate"><span class="pre"><p></span></code> tags. All you should have to do is override the <code class="docutils literal notranslate"><span class="pre">tag</span></code> class attribute (you may need to add a <code class="docutils literal notranslate"><span class="pre">tag</span></code> class attribute to the <code class="docutils literal notranslate"><span class="pre">Element</span></code> class first, if you haven’t already).</p>
<p>Now you can render a few different types of element. For example:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">page</span> <span class="o">=</span> <span class="n">Body</span><span class="p">(</span><span class="s2">"Some content"</span><span class="p">)</span>
<span class="n">page</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">"Some more contenet"</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"test.html"</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">outfile</span><span class="p">:</span>
<span class="n">page</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">outfile</span><span class="p">)</span>
</pre></div>
</div>
<p>Will result in a file with something like this in it:</p>
<div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">body</span><span class="p">></span>
Some content.
Some more content.
<span class="p"></</span><span class="nt">body</span><span class="p">></span>
</pre></div>
</div>
<p>Note: So why are we subclassing here? Because: “a body element <em>is</em> an <code class="docutils literal notranslate"><span class="pre">Element</span></code>” makes perfect sense – that’s when you want to subclass. Another way to think about it is that you want to subclass to make a specialized version of something.</p>
<p>You may note that the <code class="docutils literal notranslate"><span class="pre">Element</span></code> class really doesn’t do anything by itself – it needs a tag (at least) to be a proper element. This is what’s called a “Base Class”. It contains functionality required by various subclasses, but may not do anything on its own. In this case, we gave it the tag ‘html’, so we could run and test the render method. But strictly speaking, as a base class, it could have no tag.</p>
<p>And of course these subclasses are pretty simple – only overriding one class attribute. If that’s all you need to do to specialize, there are other ways than subclassing to do it. But bear with us – other element subclasses will require more specialization.</p>
<p>If you are stuck – see the tutorial: <a class="reference internal" href="html_renderer_tutorial.html#render-tutorial-2-a"><span class="std std-ref">Step 2:</span></a></p>
</div>
<div class="section" id="part-b">
<h4>Part B:<a class="headerlink" href="#part-b" title="Permalink to this headline">¶</a></h4>
<p>Now it gets fun!</p>
<p>Now that you have multiple types of elements, it’s worth looking a bit at how html works. A given element can hold text, but it can <em>also</em> hold other elements. So we need to update our <code class="docutils literal notranslate"><span class="pre">Element</span></code> classes to support that.</p>
<p>Extend the <code class="docutils literal notranslate"><span class="pre">Element.render()</span></code> method so that it can render other elements inside the tag in addition to strings. A recursion-like approach should do it. i.e. it can call the <code class="docutils literal notranslate"><span class="pre">render()</span></code> method of the elements it contains.</p>
<p>You should be able to <code class="docutils literal notranslate"><span class="pre">append</span></code> an element to another element – not just text.</p>
<p>If this recursion-like idea doesn’t make sense to you, take a look at this blog post, which talks about recursive algorithms:</p>
<p><a class="reference external" href="https://realpython.com/python-thinking-recursively/">https://realpython.com/python-thinking-recursively/</a></p>
<p>Figure out a way to deal with the fact that the contained elements could be either simple strings or <code class="docutils literal notranslate"><span class="pre">Element</span></code> s with render methods (there are a few ways to handle that…). Think about “Duck Typing” and EAFP. See the section <a class="reference internal" href="#notes-on-handling-duck-typing"><span class="std std-ref">Notes on handling “Duck Typing”</span></a> and the end of the Exercise for more.</p>
<p>You should now be able to render a basic web page with an <code class="docutils literal notranslate"><span class="pre"><html></span></code> tag around the whole thing, a <code class="docutils literal notranslate"><span class="pre"><body></span></code> tag inside, and multiple <code class="docutils literal notranslate"><span class="pre"><p></span></code> tags inside that, with text inside that.</p>
<p>So code like:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">page</span> <span class="o">=</span> <span class="n">Html</span><span class="p">()</span>
<span class="n">body</span> <span class="o">=</span> <span class="n">Body</span><span class="p">()</span>
<span class="n">body</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">P</span><span class="p">(</span><span class="s2">"a very small paragraph"</span><span class="p">))</span>
<span class="n">body</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">P</span><span class="p">(</span><span class="s2">"another small paragraph"</span><span class="p">))</span>
<span class="n">page</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">body</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"test.html"</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">outfile</span><span class="p">:</span>
<span class="n">page</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">outfile</span><span class="p">)</span>
</pre></div>
</div>
<p>Should result in something like:</p>
<div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">html</span><span class="p">></span>
<span class="p"><</span><span class="nt">body</span><span class="p">></span>
<span class="p"><</span><span class="nt">p</span><span class="p">></span>
a very small paragraph
<span class="p"></</span><span class="nt">p</span><span class="p">></span>
<span class="p"><</span><span class="nt">p</span><span class="p">></span>
another small paragraph
<span class="p"></</span><span class="nt">p</span><span class="p">></span>
<span class="p"></</span><span class="nt">body</span><span class="p">></span>
<span class="p"></</span><span class="nt">html</span><span class="p">></span>
</pre></div>
</div>
<p>See: <a class="reference download internal" download="" href="../_downloads/14f7079015a09629095a1e335c5c216b/test_html_output2.html"><code class="xref download docutils literal notranslate"><span class="pre">test_html_output2.html</span></code></a></p>
<p>NOTE: when you run step 2 in <code class="docutils literal notranslate"><span class="pre">run_html_render.py</span></code>, you will want to comment out step 1 – that way you’ll only get one set of output.</p>
<p>If you are stuck – see the tutorial: <a class="reference internal" href="html_renderer_tutorial.html#render-tutorial-2-b"><span class="std std-ref">Part B:</span></a></p>
</div>
</div>
<div class="section" id="step-3">
<h3>Step 3:<a class="headerlink" href="#step-3" title="Permalink to this headline">¶</a></h3>
<p>Create a <code class="docutils literal notranslate"><span class="pre"><head></span></code> element – a simple subclass.</p>
<p>Create a <code class="docutils literal notranslate"><span class="pre">OneLineTag</span></code> subclass of <code class="docutils literal notranslate"><span class="pre">Element</span></code>:</p>
<ul>
<li><p>It should override the render method, to render everything on one line – for the simple tags, like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o"><</span><span class="n">title</span><span class="o">></span> <span class="n">PythonClass</span> <span class="o">-</span> <span class="n">Session</span> <span class="mi">6</span> <span class="n">example</span> <span class="o"></</span><span class="n">title</span><span class="o">></span>
</pre></div>
</div>
</li>
</ul>
<p>Create a <code class="docutils literal notranslate"><span class="pre">Title</span></code> subclass of <code class="docutils literal notranslate"><span class="pre">OneLineTag</span></code> class for the title.</p>
<p>You should now be able to render an html doc with a head element, with a
title element in that, and a body element with some <code class="docutils literal notranslate"><span class="pre"><P></span></code> elements and some text.</p>
<p>See <a class="reference download internal" download="" href="../_downloads/18981fb71a1d906bd8ce331570c49328/test_html_output3.html"><code class="xref download docutils literal notranslate"><span class="pre">test_html_output3.html</span></code></a></p>
</div>
<div class="section" id="step-4">
<h3>Step 4:<a class="headerlink" href="#step-4" title="Permalink to this headline">¶</a></h3>
<p>Extend the <code class="docutils literal notranslate"><span class="pre">Element</span></code> class to accept a set of attributes as keywords to the constructor, e.g. <code class="docutils literal notranslate"><span class="pre">run_html_render.py</span></code></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">Element</span><span class="p">(</span><span class="s2">"some text content"</span><span class="p">,</span> <span class="nb">id</span><span class="o">=</span><span class="s2">"TheList"</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="s2">"line-height:200%"</span><span class="p">)</span>
</pre></div>
</div>
<p>html elements can take essentially any attributes – so you can’t hard-code these particular ones (remember <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code>? )</p>
<p>The render method will need to be extended to render the attributes properly.</p>
<p>Note that you may now have <em>two</em> render methods – the one in the <code class="docutils literal notranslate"><span class="pre">Element</span></code> base class, and the one in the <code class="docutils literal notranslate"><span class="pre">OneLineTag</span></code> class. They both need to be be able to handle attributes. But <strong>DRY</strong> – so see if you can factor the code so the code that makes the opening tag, with the attributes is not repeated.</p>
<p>You can now render some <code class="docutils literal notranslate"><span class="pre"><p></span></code> tags (and others) with attributes.</p>
<p>See: <a class="reference download internal" download="" href="../_downloads/0ceb7320d480c2a40958bbe7130460f1/test_html_output4.html"><code class="xref download docutils literal notranslate"><span class="pre">test_html_output4.html</span></code></a></p>
<p>NOTE: if you do “proper” CSS+html, then you wouldn’t specify style directly in element attributes.</p>
<p>Rather you would set the “class” attribute:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o"><</span><span class="n">p</span> <span class="n">class</span><span class="o">=</span><span class="s2">"intro"</span><span class="o">></span>
<span class="n">This</span> <span class="ow">is</span> <span class="n">my</span> <span class="n">recipe</span> <span class="k">for</span> <span class="n">making</span> <span class="n">curry</span> <span class="n">purely</span> <span class="k">with</span> <span class="n">chocolate</span><span class="o">.</span>
<span class="o"></</span><span class="n">p</span><span class="o">></span>
</pre></div>
</div>
<p>However, if you try this as a keyword argument in Python:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="n">P</span><span class="p">(</span><span class="s2">"some content"</span><span class="p">,</span> <span class="n">class</span><span class="o">=</span><span class="s2">"intro"</span><span class="p">)</span>
<span class="go">File "<ipython-input-1-7d9a6b30cd26>", line 1</span>
<span class="go"> P("some content", class="intro")</span>
<span class="go"> ^</span>
<span class="go">SyntaxError: invalid syntax</span>
</pre></div>
</div>
<p>Huh?</p>
<p>“class” is a reserved work in Python – for making classes.
So it can’t be used as a keyword argument.</p>
<p>But it’s a fine key in a dict, so you can put it in a dict, and pass it in with <code class="docutils literal notranslate"><span class="pre">**</span></code>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">attrs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'class'</span><span class="p">:</span> <span class="s1">'intro'</span><span class="p">}</span>
<span class="n">P</span><span class="p">(</span><span class="s2">"some content"</span><span class="p">,</span> <span class="o">**</span><span class="n">attrs</span><span class="p">)</span>
</pre></div>
</div>
<p>You could also special-case this in your code – so your users could use “clas” with one s, and you could tranlate it in the generated html. Or even both!</p>
</div>
<div class="section" id="step-5">
<h3>Step 5:<a class="headerlink" href="#step-5" title="Permalink to this headline">¶</a></h3>
<p>Create a <code class="docutils literal notranslate"><span class="pre">SelfClosingTag</span></code> subclass of Element, to render tags like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o"><</span><span class="n">hr</span> <span class="o">/></span> <span class="ow">and</span> <span class="o"><</span><span class="n">br</span> <span class="o">/></span> <span class="p">(</span><span class="n">horizontal</span> <span class="n">rule</span> <span class="ow">and</span> <span class="n">line</span> <span class="k">break</span><span class="p">)</span><span class="o">.</span>
</pre></div>
</div>
<p>(See: <a class="reference external" href="https://www.w3schools.com/tags/tag_hr.asp">https://www.w3schools.com/tags/tag_hr.asp</a>)</p>
<p>For example you should be able to use this code:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Hr</span><span class="p">(</span><span class="n">width</span><span class="o">=</span><span class="mi">400</span><span class="p">)</span>
</pre></div>
</div>
<p>To get this result:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o"><</span><span class="n">hr</span> <span class="n">width</span><span class="o">=</span><span class="s2">"400"</span> <span class="o">/></span>
</pre></div>
</div>
<p>You will need to override the render method to render just the one tag and attributes, if any.</p>
<p>Note that self closing tags can’t have any content. Make sure that your SelfClosingTag element raises an exception if someone tries to put in any content – probably a <code class="docutils literal notranslate"><span class="pre">TypeError</span></code>.</p>
<p>Create a couple subclasses of <code class="docutils literal notranslate"><span class="pre">SelfClosingTag</span></code> for <code class="docutils literal notranslate"><span class="pre"><hr</span> <span class="pre">/></span></code> and <code class="docutils literal notranslate"><span class="pre"><br</span> <span class="pre">/></span></code></p>
<p>Note that you now have maybe three render methods – is there repeated code in them?</p>
<p>Can you refactor the common parts into a separate method that all the render methods can call? And do all your tests still pass (you do have tests for everything, don’t you?) after refactoring?</p>
<p>See: <a class="reference download internal" download="" href="../_downloads/c205fe025d5cbf59e762b22d0523d062/test_html_output5.html"><code class="xref download docutils literal notranslate"><span class="pre">test_html_output5.html</span></code></a></p>
</div>
<div class="section" id="step-6">
<h3>Step 6:<a class="headerlink" href="#step-6" title="Permalink to this headline">¶</a></h3>
<p>Create an <code class="docutils literal notranslate"><span class="pre">A</span></code> class for an anchor (link) element. Its constructor should look like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">A</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">link</span><span class="p">,</span> <span class="n">content</span><span class="p">)</span>
</pre></div>
</div>
<p>where <code class="docutils literal notranslate"><span class="pre">link</span></code> is the link, and <code class="docutils literal notranslate"><span class="pre">content</span></code> is what you see. It can be called like so:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">A</span><span class="p">(</span><span class="s2">"http://google.com"</span><span class="p">,</span> <span class="s2">"link to google"</span><span class="p">)</span>
</pre></div>
</div>
<p>and it should render like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o"><</span><span class="n">a</span> <span class="n">href</span><span class="o">=</span><span class="s2">"http://google.com"</span><span class="o">></span><span class="n">link</span> <span class="n">to</span> <span class="n">google</span><span class="o"></</span><span class="n">a</span><span class="o">></span>
</pre></div>
</div>
<p>You should be able to subclass from <code class="docutils literal notranslate"><span class="pre">Element</span></code>, and only override the <code class="docutils literal notranslate"><span class="pre">__init__</span></code> — calling the <code class="docutils literal notranslate"><span class="pre">Element</span></code> <code class="docutils literal notranslate"><span class="pre">__init__</span></code> from the <code class="docutils literal notranslate"><span class="pre">A</span></code> <code class="docutils literal notranslate"><span class="pre">__init__</span></code></p>
<p>You can now add a link to your web page.</p>
<p>See: <a class="reference download internal" download="" href="../_downloads/ef9be52b61ca4b6efa64c7b43ce8fe40/test_html_output6.html"><code class="xref download docutils literal notranslate"><span class="pre">test_html_output6.html</span></code></a></p>
</div>
<div class="section" id="step-7">
<h3>Step 7:<a class="headerlink" href="#step-7" title="Permalink to this headline">¶</a></h3>
<p>Create <code class="docutils literal notranslate"><span class="pre">Ul</span></code> class for an unordered list (really simple subclass of <code class="docutils literal notranslate"><span class="pre">Element</span></code>).</p>
<p>Create <code class="docutils literal notranslate"><span class="pre">Li</span></code> class for an element in a list (also really simple).</p>
<p>Add a list to your web page.</p>
<p>Create a <code class="docutils literal notranslate"><span class="pre">Header</span></code> class – this one should take an integer argument for the header level. i.e <h1>, <h2>, <h3>, called like</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">H</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="s2">"The text of the header"</span><span class="p">)</span>
</pre></div>
</div>
<p>for an <h2> header.</p>
<p>It can subclass from <code class="docutils literal notranslate"><span class="pre">OneLineTag</span></code> – overriding the <code class="docutils literal notranslate"><span class="pre">__init__</span></code>, then calling the superclass <code class="docutils literal notranslate"><span class="pre">__init__</span></code></p>
<p>See: <a class="reference download internal" download="" href="../_downloads/e6726755ec5450041c7567ea8ba037c6/test_html_output7.html"><code class="xref download docutils literal notranslate"><span class="pre">test_html_output7.html</span></code></a></p>
</div>
<div class="section" id="step-8">
<h3>Step 8:<a class="headerlink" href="#step-8" title="Permalink to this headline">¶</a></h3>
<p>Update the <code class="docutils literal notranslate"><span class="pre">Html</span></code> element class to render the “<!DOCTYPE html>” tag at the head of the page, before the html element.</p>
<p>You can do this by subclassing <code class="docutils literal notranslate"><span class="pre">Element</span></code>, overriding <code class="docutils literal notranslate"><span class="pre">render()</span></code>, but then calling the <code class="docutils literal notranslate"><span class="pre">Element</span></code> render from the new render.</p>
<p>Create a subclass of <code class="docutils literal notranslate"><span class="pre">SelfClosingTag</span></code> for <code class="docutils literal notranslate"><span class="pre"><meta</span> <span class="pre">charset="UTF-8"</span> <span class="pre">/></span></code> (like for <code class="docutils literal notranslate"><span class="pre"><hr</span> <span class="pre">/></span></code> and <code class="docutils literal notranslate"><span class="pre"><br</span> <span class="pre">/></span></code> and add the meta element to the beginning of the head element to give your document an encoding.</p>
<p>The doctype and encoding are HTML 5 and you can check this at:</p>
<p><a class="reference external" href="http://validator.w3.org/#validate_by_input">http://validator.w3.org/#validate_by_input</a></p>
<p>You now have a pretty full-featured html renderer – play with it, add some new tags, etc….</p>
<p>See <a class="reference download internal" download="" href="../_downloads/f6bb161d82f83a97c816d1c1907935a8/test_html_output8.html"><code class="xref download docutils literal notranslate"><span class="pre">test_html_output8.html</span></code></a></p>
</div>
<div class="section" id="step-9-adding-indentation">
<h3>Step 9: Adding Indentation<a class="headerlink" href="#step-9-adding-indentation" title="Permalink to this headline">¶</a></h3>
<p>Indentation is not strictly required for html – html ignores most whitespace.</p>
<p>But it can make it much easier to read for humans, and it’s a nice exercise to see how one might make it work in arbitrarily nested html.</p>
<p>There is also more than one way to indent html – so you have a bit of flexibility here.</p>
<p>You will need to enhance your code in a couple ways to add indentation.</p>
<div class="section" id="a-specify-the-indentation-level">
<h4>A. Specify the indentation level<a class="headerlink" href="#a-specify-the-indentation-level" title="Permalink to this headline">¶</a></h4>
<p>Add a class attribute to the <code class="docutils literal notranslate"><span class="pre">Element</span></code> base class that indicates how much indentation you want – you can either use a simple string: 2 or four spaces:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Element</span><span class="p">:</span>
<span class="n">indent</span> <span class="o">=</span> <span class="s2">" "</span>
</pre></div>
</div>
<p>Or you can use an integer to specify how many spaces you want to use:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Element</span><span class="p">:</span>
<span class="n">indent</span> <span class="o">=</span> <span class="mi">4</span>
</pre></div>
</div>
<p>Your render method(s) can access this attribute to know how much to indent a element. You want it as a class attribute in the base class, so that all the instances of all the subclasses will share the same value – to indent all the html consistently.</p>
<p>Then you need to pass this indentation down the tree as you render the page.</p>
</div>
<div class="section" id="b-pass-the-current-level-of-indentation-down-the-tree-of-elements">
<h4>B. Pass the “current level” of indentation down the tree of elements<a class="headerlink" href="#b-pass-the-current-level-of-indentation-down-the-tree-of-elements" title="Permalink to this headline">¶</a></h4>
<p>html elements can be nested arbitrarily deep:</p>
<div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="cp"><!DOCTYPE html></span>
<span class="p"><</span><span class="nt">html</span><span class="p">></span>
<span class="p"><</span><span class="nt">head</span><span class="p">></span>
<span class="p"><</span><span class="nt">title</span><span class="p">></span>PythonClass = Revision 1087:<span class="p"></</span><span class="nt">title</span><span class="p">></span>
<span class="p"></</span><span class="nt">head</span><span class="p">></span>
<span class="p"><</span><span class="nt">body</span><span class="p">></span>
<span class="p"><</span><span class="nt">p</span><span class="p">></span>
Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text
<span class="p"></</span><span class="nt">p</span><span class="p">></span>
<span class="p"><</span><span class="nt">ul</span><span class="p">></span>
<span class="p"><</span><span class="nt">li</span><span class="p">></span>
The first item in a list
<span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="p"><</span><span class="nt">li</span><span class="p">></span>
This is the second item
<span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
<span class="p"></</span><span class="nt">body</span><span class="p">></span>
<span class="p"></</span><span class="nt">html</span><span class="p">></span>
</pre></div>
</div>
<p>So how does a given element know where it is in the tree? And therefore how deep to indent itself?</p>
<p>One way: extend your <code class="docutils literal notranslate"><span class="pre">render</span></code> method(s) to take another parameter:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">render</span><span class="p">(</span><span class="n">out_file</span><span class="p">,</span> <span class="n">cur_ind</span><span class="o">=</span><span class="s2">""</span><span class="p">):</span>
<span class="o"><</span><span class="n">render</span> <span class="n">code</span> <span class="n">here</span><span class="o">></span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">cur_ind</span></code> is a string (or number) with the current level of indentation in it: the amount that the entire tag should be indented for pretty printing.</p>
<p>This is a little tricky: <code class="docutils literal notranslate"><span class="pre">cur_ind</span></code> will be the amount that this element should be indented already. It will be from zero (an empty string) to a lot of spaces, depending on how deep it is in the tree. You could use an integer for the number of spaces to indent – or keep it simple and just use a string with the correct number of spaces in it.</p>
<p>The amount of each level of indentation should be set by the class attribute: <code class="docutils literal notranslate"><span class="pre">indent</span></code></p>
<p>So:</p>
<ul class="simple">
<li><p>You probably want <code class="docutils literal notranslate"><span class="pre">cur_ind</span></code> to be an optional argument to render – so it will not indent if nothing is passed in.</p></li>
<li><p>But if it is passed in, you want your code to USE the <code class="docutils literal notranslate"><span class="pre">cur_ind</span></code> parameter – it is supposed to indicate how much this entire tag is already indented.</p></li>
<li><p>When a given element gets rendered, you don’t know where it is in a potentially deeply nested hierarchy – it could be at the top level or ten levels deep. passing <code class="docutils literal notranslate"><span class="pre">cur_ind</span></code> into the render method is how this is communicated.</p></li>
<li><p>So when you call <code class="docutils literal notranslate"><span class="pre">render</span></code> from <em>inside</em> a render method – you need to tell the nested elements how deep to render themselves – usually one more level of indentation deep. Probably something like:</p></li>
</ul>
<p><in <code class="docutils literal notranslate"><span class="pre">render()</span></code>></p>
<p><code class="docutils literal notranslate"><span class="pre">sub_element.render(out_file,</span> <span class="pre">cur_ind</span> <span class="pre">+</span> <span class="pre">self.indent)</span></code></p>
<ul class="simple">
<li><p>Remember to keep the amount of spaces per indentation defined as a class attribute of the base class (the <code class="docutils literal notranslate"><span class="pre">Element</span></code> class). That way, you could change it in one place, and it would change everywhere and remain consistent.</p></li>
<li><p>Be sure to test that the indentation of the result changes if you change the class attribute!</p></li>
</ul>
<p>You should have nice pretty indented html now!</p>
<p>See <a class="reference download internal" download="" href="../_downloads/a8258274537e67e034a65e18617ead19/test_html_output9.html"><code class="xref download docutils literal notranslate"><span class="pre">test_html_output9.html</span></code></a></p>
</div>
</div>
</div>
<div class="section" id="notes-on-handling-duck-typing">
<span id="id1"></span><h2>Notes on handling “Duck Typing”<a class="headerlink" href="#notes-on-handling-duck-typing" title="Permalink to this headline">¶</a></h2>
<p>In this exercise, we need to deal with the fact that XML (and thus HTML) allows <em>either</em> plain text <em>or</em> other tags to be the content of a tag. So our code needs to handle the fact that there are two possible types that we need to be able to render.</p>
<p>There are two primary ways to address this (and multiple ways to actually write the code for each of these).</p>
<ol class="arabic simple">
<li><p>Make sure that the content only has renderable objects in it.</p></li>
<li><p>Make sure the render() method can handle either type on the fly.</p></li>
</ol>
<p>The difference is where you handle the multiple types – in the render method itself, or ahead of time, when you append new content to the <code class="docutils literal notranslate"><span class="pre">Element</span></code>.</p>
<div class="section" id="the-ahead-of-time-option">
<h3>The Ahead of Time Option:<a class="headerlink" href="#the-ahead-of-time-option" title="Permalink to this headline">¶</a></h3>
<p>You can handle it ahead of time by creating a simple object that wraps a string and gives it a render method. As simple as:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">TextWrapper</span><span class="p">:</span>
<span class="sd">"""</span>
<span class="sd"> A simple wrapper that creates a class with a render method</span>
<span class="sd"> for simple text</span>
<span class="sd"> """</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">text</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">text</span> <span class="o">=</span> <span class="n">text</span>
<span class="k">def</span> <span class="nf">render</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">file_out</span><span class="p">):</span>
<span class="n">file_out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
</pre></div>
</div>
<p>You could require your users to use the wrapper, so instead of just appending a string, they would do:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">an_element</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">TextWrapper</span><span class="p">(</span><span class="s2">"the string they want to add"</span><span class="p">))</span>
</pre></div>
</div>
<p>But this is not very Pythonic style – it’s OO heavy. Strings for text are so common you want to be able to simply use them:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">an_element</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">"the string they want to add"</span><span class="p">)</span>
</pre></div>
</div>
<p>So much easier.</p>
<p>To accomplish this, you can update the <code class="docutils literal notranslate"><span class="pre">append()</span></code> method to put this wrapper around plain strings when something new is added.</p>
</div>
<div class="section" id="checking-if-it-s-the-right-type">
<h3>Checking if it’s the Right Type<a class="headerlink" href="#checking-if-it-s-the-right-type" title="Permalink to this headline">¶</a></h3>
<p>How do you decide if the wrapper is required?</p>
<p><strong>Checking it it’s an instance of Element:</strong></p>
<p>You could check and see if the object being appended is an Element:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">content</span><span class="p">,</span> <span class="n">Element</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">content</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">content</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">content</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">TextWrapper</span><span class="p">(</span><span class="n">content</span><span class="p">))</span>
</pre></div>
</div>
<p>This would work well, but closes the door to using any other type that may not be a strict subclass of Element, but can render itself. Not too bad in this case, but in general, frowned upon in Python.</p>
<p>Alternatively, you could check for the string type:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">content</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">content</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">TextWrapper</span><span class="p">(</span><span class="n">content</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">content</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">content</span><span class="p">)</span>
</pre></div>
</div>
<p>I think this is a little better – strings are a pretty core type in Python, so it’s not likely that anyone is going to need to use a “string-like” object.</p>
</div>
<div class="section" id="duck-typing">
<h3>Duck Typing<a class="headerlink" href="#duck-typing" title="Permalink to this headline">¶</a></h3>
<p>The Python model of duck typing is: If quacks like a duck, then treat it like a duck.</p>
<p>But in this case, we’re not actually rendering the object at this stage, so calling the method isn’t appropriate.</p>
<p><strong>Checking for an attribute</strong></p>
<p>Instead of calling the method, see if it’s there. You can do that with <code class="docutils literal notranslate"><span class="pre">hasattr()</span></code></p>
<p>Check if the passed-in object has a <code class="docutils literal notranslate"><span class="pre">render</span></code> attribute:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">content</span><span class="p">,</span> <span class="s1">'render'</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">content</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">content</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">content</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">TextWrapper</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">content</span><span class="p">))</span>
</pre></div>
</div>
<p>Note that I added a <code class="docutils literal notranslate"><span class="pre">str()</span></code> call too – so you can pass in anything – it will get stringified – this will be ugly for many objects, but will work fine for numbers and other simple objects.</p>
<p>This is my favorite.</p>
</div>
<div class="section" id="duck-typing-on-the-fly">
<h3>Duck Typing on the Fly<a class="headerlink" href="#duck-typing-on-the-fly" title="Permalink to this headline">¶</a></h3>
<p>The other option is to simply put both elements and text in the content list, and figure out what to do in the <code class="docutils literal notranslate"><span class="pre">render()</span></code> method.</p>
<p>Again, you could type check – but I prefer the duck typing approach, and EAFP:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
<span class="n">content</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">out_file</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
<span class="n">outfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">content</span><span class="p">)</span>
</pre></div>
</div>
<p>If content is a simple string then it won’t have a render method, and an <code class="docutils literal notranslate"><span class="pre">AttributeError</span></code> will be raised.</p>
<p>You can catch that, and simply write the content directly instead.</p>
<p>You may want to turn it into a string, first:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">outfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">content</span><span class="p">))</span>
</pre></div>
</div>
<p>Then you could write just about anything – numbers, etc.</p>
</div>
<div class="section" id="where-did-the-exception-come-from">
<h3>Where did the Exception come from?<a class="headerlink" href="#where-did-the-exception-come-from" title="Permalink to this headline">¶</a></h3>
<p><strong>Caution</strong></p>
<p>If the object doesn’t have a <code class="docutils literal notranslate"><span class="pre">render</span></code> method, then an AttributeError will be raised. But what if it does have a render method, but that method is broken?</p>
<p>Depending on what’s broken, it could raise any number of exceptions. Most will not get caught by the except clause, and will halt the program.</p>
<p>But if, just by bad luck, it has an bug that raises an <code class="docutils literal notranslate"><span class="pre">AttributeError</span></code> – then this could catch it, and try to simply write it out instead. So you may get something like: <code class="docutils literal notranslate"><span class="pre"><html_render.H</span> <span class="pre">object</span> <span class="pre">at</span> <span class="pre">0x103604400></span></code> in the middle of your html.</p>
<p><strong>The beauty of testing</strong></p>
<p>If you have a unit test that calls every render method in your code – then it should catch that error, and in the unit test it will be clear where it is coming from.</p>
</div>
</div>
<div class="section" id="html-primer">
<span id="id2"></span><h2>HTML Primer<a class="headerlink" href="#html-primer" title="Permalink to this headline">¶</a></h2>
<p>The very least you need to know about html to do this assignment.</p>
<p>If you are familiar with html, then this will all make sense to you. If you have never seen html before, this might be a bit intimidating, but you really don’t need to know much to do this assignment.</p>
<p>First of all, sample output from each step is provided. So all you really need to do is look at that, and make your code do the same thing. But it does help understand a little bit about what you trying to do.</p>
<div class="section" id="html">
<h3>HTML<a class="headerlink" href="#html" title="Permalink to this headline">¶</a></h3>
<p>HTML is “Hyper Text Markup Language”. Hypertext, because it can contain links
to other pages, and markup language means that text is “marked up” with
instructions about how to format the text, etc.</p>
<p>Here is a good basic intro:</p>
<p><a class="reference external" href="http://www.w3schools.com/html/html_basic.asp">http://www.w3schools.com/html/html_basic.asp</a></p>
<p>And there are countless others online.</p>
<p>As html is XML – the XML intro is a good source of the XML syntax, too:</p>
<p><a class="reference external" href="http://www.w3schools.com/xml/default.asp">http://www.w3schools.com/xml/default.asp</a></p>
<p>But here is a tiny summary of just what you need to know for this project.</p>
</div>
<div class="section" id="elements">
<h3>Elements<a class="headerlink" href="#elements" title="Permalink to this headline">¶</a></h3>
<p>Modern HTML is a particular dialect of XML (eXtensible Markup Language),
which is itself a special case of SGML (Standard Generalized Markup Language)</p>
<p>It inherits from SGML a basic structure: each piece of the document is an element. Each element is described by a “tag”. Each tag has a different meaning, but they all have the same structure:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o"><</span><span class="n">some_tag</span><span class="o">></span> <span class="n">some</span> <span class="n">content</span> <span class="o"></</span><span class="n">some_tag</span><span class="o">></span>
</pre></div>
</div>
<p>That is, the tag name is surrounded by < and >, which marks the beginning of
the element, and the end of the element is indicated by the same tag with a slash.</p>
<p>The real power is that these elements can be nested arbitrarily deep. In order to keep that all readable, we often want to indent the content inside the tags, so it’s clear what belongs with what. That is one of the tricky bits of this assignment.</p>
</div>
<div class="section" id="basic-tags">
<h3>Basic tags<a class="headerlink" href="#basic-tags" title="Permalink to this headline">¶</a></h3>
<div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">html</span><span class="p">></span> is the core tag indicating the entire document <span class="p"></</span><span class="nt">html</span><span class="p">></span>
<span class="p"><</span><span class="nt">p</span><span class="p">></span> is a single paragraph of text <span class="p"></</span><span class="nt">p</span><span class="p">></span>
<span class="p"><</span><span class="nt">body</span><span class="p">></span> is the tag that indicated the text of the document <span class="p"></</span><span class="nt">body</span><span class="p">></span>
<span class="p"><</span><span class="nt">head</span><span class="p">></span> defines the header of the document -- a place for metadata <span class="p"></</span><span class="nt">head</span><span class="p">></span>
</pre></div>
</div>
</div>
<div class="section" id="attributes">
<h3>Attributes:<a class="headerlink" href="#attributes" title="Permalink to this headline">¶</a></h3>
<p>In addition to the tag name and the content, extra attributes can be attached to a tag. These are added to the “opening tag”, with name=”something”, another_name=”something else” format:</p>
<div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">p</span> <span class="na">style</span><span class="o">=</span><span class="s">"text-align: center"</span> <span class="na">id</span><span class="o">=</span><span class="s">"intro"</span><span class="p">></span>
</pre></div>
</div>
<p>There can be all sorts of stuff stored in attributes – some required for specific tags, some extra, like font sizes and colors. Note that since tags can essentially have any attributes, your code will need to support that – doesn’t it kind of look like a dict? And keyword arguments?</p>
</div>
<div class="section" id="special-elements">
<h3>Special Elements<a class="headerlink" href="#special-elements" title="Permalink to this headline">¶</a></h3>
<p>The general structure is everything in between the opening and closing tag. But some elements don’t really have content – just attributes. So the slash goes at the end of the tag, after the attributes. We can call these self-closing tags:</p>
<div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">"UTF-8"</span> <span class="p">/></span>
</pre></div>
</div>
<p>To make a link, you use an “anchor” tag: <code class="docutils literal notranslate"><span class="pre"><a></span></code>. It requires attributes to indicate what the link is:</p>
<div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"http://google.com"</span><span class="p">></span> link <span class="p"></</span><span class="nt">a</span><span class="p">></span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">href</span></code> attribute is the link (hyper reference).</p>
</div>
<div class="section" id="lists">
<h3>lists<a class="headerlink" href="#lists" title="Permalink to this headline">¶</a></h3>
<p>To make a bulleted list, you use a <ul> tag (unordered list), and inside that, you put individual list items <li>:</p>
<div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">ul</span> <span class="na">style</span><span class="o">=</span><span class="s">"line-height:200%"</span> <span class="na">id</span><span class="o">=</span><span class="s">"TheList"</span><span class="p">></span>
<span class="p"><</span><span class="nt">li</span><span class="p">></span>
The first item in a list
<span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="p"><</span><span class="nt">li</span> <span class="na">style</span><span class="o">=</span><span class="s">"color: red"</span><span class="p">></span>
This is the second item
<span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
</pre></div>
</div>
<p>Note that the list itself <em>and</em> the list items can both take various attributes (all tags can…)</p>
<p>Section Headers are created with “h” tags: <h1> is the biggest (highest level), and there is <h2>, <h3>, etc. for sections, sub sections, subsub sections…</p>
<div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">h2</span><span class="p">></span> PythonClass -- Example <span class="p"></</span><span class="nt">h2</span><span class="p">></span>
</pre></div>
</div>
<p>I think that’s all you need to know!</p>
</div>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="html_renderer_tutorial.html" class="btn btn-neutral float-right" title="Tutorial for the Html Render Assignment" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="../modules/SubclassingAndInheritance.html" class="btn btn-neutral float-left" title="Subclassing and Inheritance" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2020, University of Washington, Natasha Aleksandrova, Christopher Barker, Brian Dorsey, Cris Ewing, Christy Heaton, Jon Jacky, Maria McKinley, Andy Miles, Rick Riehle, Joseph Schilz, Joseph Sheedy, Hosung Song. Creative Commons Attribution-ShareAlike 4.0 license
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>