You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: slides_sources/source/exercises/html_renderer.rst
+17-26Lines changed: 17 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -347,7 +347,7 @@ But this is not very Pythonic style -- it's OO heavy. Strings for text are so co
347
347
348
348
So much easier.
349
349
350
-
To accomplish this, you can update the ``append()`` method to put this wrapper around plain strings when somethign new is added.
350
+
To accomplish this, you can update the ``append()`` method to put this wrapper around plain strings when something new is added.
351
351
352
352
353
353
Checking if it's the right type
@@ -402,9 +402,10 @@ check if the passed-in object has a ``render()`` attribute:
402
402
self.content.append(TextWrapper(str(content))
403
403
404
404
405
-
Note that I added a ``str()`` call too -- so you can passin anything -- it it will get string-ified -- this will be ugly for many objects, but will work fine for numbers and other simple objects.
405
+
Note that I added a ``str()`` call too -- so you can passin anything -- it will get stringified -- this will be ugly for many objects, but will work fine for numbers and other simple objects.
406
+
407
+
This is my favorite. ``html_render_wrap.py``in Solutions demonstrates some core bits of this approach.
406
408
407
-
This is my favorite. ``html_render_wrap.py``in Solutions demonstrates with method.
408
409
409
410
Duck Typing on the Fly
410
411
----------------------
@@ -422,7 +423,7 @@ Again, you could type check -- but I prefer the duck typing approach, and EAFP:
422
423
423
424
If content is a simple string then it won't have a render method, and an ``AttributeError`` will be raised.
424
425
425
-
You can catch that, and simply write the content.
426
+
You can catch that, and simply write the content directly instead.
426
427
427
428
.. nextslide::
428
429
@@ -442,25 +443,25 @@ If the object doesn't have a ``render`` method, then an AttributeError will be r
442
443
443
444
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.
444
445
445
-
But if, just by bad luck, it has an bug that raises an ``AttributeError``-- then this could catch it, andtry to simply write it out instead. So you may get somethign like: ``<html_render.H object at 0x103604400>``in the middle of your html.
446
+
But if, just by bad luck, it has an bug that raises an ``AttributeError``-- then this could catch it, andtry to simply write it out instead. So you may get something like: ``<html_render.H object at 0x103604400>``in the middle of your html.
446
447
447
448
**The beauty of testing**
448
449
449
-
If you have a unit test that calls every render method in your code -- then it should catch that error, and it wil be clear where it is coming from.
450
+
If you have a unit test that calls every render method in your code -- then it should catch that error, andin the unit test it will be clear where it is coming from.
450
451
451
452
452
453
HTML Primer
453
454
============
454
455
455
456
.. rst-class:: medium
456
457
457
-
The very least you need to know about html to do this assigment.
458
+
The very least you need to know about html to do this assignment.
458
459
459
460
.. rst-class:: left
460
461
461
-
If you are familarwith 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.
462
+
If you are familiarwith 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.
462
463
463
-
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 helpto know a little bit about what you are doing.
464
+
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 helpunderstand a little bit about what you trying to do.
464
465
465
466
HTML
466
467
----
@@ -487,15 +488,16 @@ Elements
487
488
Modern HTMLis a particular dialect of XML (eXtensible Markup Language),
488
489
which is itself a special case of SGML (Standard Generalized Markup Language)
489
490
490
-
It inherits fromSGML 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::
491
+
It inherits fromSGML 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::
491
492
492
493
<some_tag> some content </some_tag>
493
494
494
-
thatis, the tag name is surrounded by <and>, which marks the beginning of
495
+
Thatis, the tag name is surrounded by <and>, which marks the beginning of
495
496
the element, and the end of the element is indicated by the same tag with a slash.
496
497
497
498
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.
498
499
500
+
499
501
Basic tags
500
502
----------
501
503
@@ -516,7 +518,7 @@ In addition to the tag name and the content, extra attributes can be attached to
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?
522
524
@@ -537,6 +539,9 @@ To make a link, you use an "anchor" tag: ``<a>``. It requires attributes to indi
537
539
538
540
The ``href`` attribute is the link (hyper reference).
539
541
542
+
lists
543
+
-----
544
+
540
545
To make a bulleted list, you use a <ul> tag (unordered list), and inside that, you put individual list items <li>:
541
546
542
547
.. code-block:: html
@@ -559,17 +564,3 @@ Section Headers are created with "h" tags: <h1> is the biggest (highest level),
0 commit comments