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
* Write a the same function with the parameters as:
60
+
61
+
``*args`` and ``**kwags``
62
+
63
+
* Have it print the colors (use strings for the colors)
64
+
65
+
* Call it with the same various combinations of arguments used above.
66
+
67
+
* Also have it print `args` and `kwargs` directly, so you can be sure you understand what's going on.
68
+
69
+
* Note that in general, you can't know what will get passed into ``**kwargs`` So maybe adapt your function to be able to do something reasonable with any keywords.
Copy file name to clipboardExpand all lines: slides_sources/source/exercises/html_renderer.rst
+13-9Lines changed: 13 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,7 @@ You should be able to run that code at each step, uncommenting each new step in
39
39
40
40
It builds up an html tree, and then calls the ``render()`` method of your element to render the page.
41
41
42
-
It uses a ``cStringIO`` 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 code at the end to make sure you understand it.
42
+
It uses a ``StringIO`` 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 code at the end to make sure you understand it.
43
43
44
44
The html generated at each step will be in the files: ``test_html_ouput?.html``
45
45
@@ -176,7 +176,7 @@ Create a couple subclasses of ``SelfClosingTag`` for and <hr /> and <br />
176
176
177
177
Note that you now have a couple render methods -- is there repeated code in them?
178
178
179
-
Can you refactor the common parts into a separate method that all the render methods can call?
179
+
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?
180
180
181
181
See ``test_html_output5.html``
182
182
@@ -191,7 +191,7 @@ where ``link`` is the link, and ``content`` is what you see. It can be called li
191
191
192
192
A("http://google.com", "link to google")
193
193
194
-
You should be able to subclass from ``Element``, and only override the ``__init__`` --- Calling the ``Element`` ``__init__`` from the ``A __init__``
194
+
You should be able to subclass from ``Element``, and only override the ``__init__`` --- calling the ``Element`` ``__init__`` from the ``A __init__``
195
195
196
196
You can now add a link to your web page.
197
197
@@ -266,7 +266,8 @@ You can handle it ahead of time by creating a simple object that wraps a string
266
266
self.text = text
267
267
268
268
defrender(self, file_out, current_ind=""):
269
-
file_out.write(current_ind +self.text)
269
+
file_out.write(current_ind)
270
+
file_out.write(self.text)
270
271
271
272
.. nextslide::
272
273
@@ -321,22 +322,25 @@ I think this is a little better -- strings are a pretty core type in python, it'
321
322
Duck Typing
322
323
-----------
323
324
324
-
The Python model of duck typing is if quacks like a duck, then treat it like a duck.
325
+
The Python model of duck typing is: If quacks like a duck, then treat it like a duck.
325
326
326
327
But in this case, we're not actually rendering the object at this stage, so calling the method isn't appropriate.
327
328
328
329
**Checking for an attribute**
329
330
330
-
Instead of calling the method, see if it's there:
331
+
Instead of calling the method, see if it's there. You can do that with ``hasattr()``
331
332
332
-
You can check if the passed-in object has a ``render()`` attribute:
333
+
check if the passed-in object has a ``render()`` attribute:
333
334
334
335
.. code-block:: python
335
336
336
337
ifhasattr(content, 'render'):
337
338
self.content.append(content)
338
339
else:
339
-
self.content.append(TextWrapper(content))
340
+
self.content.append(TextWrapper(str(content))
341
+
342
+
343
+
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.
340
344
341
345
This is my favorite. ``html_render_wrap.py``in Solutions demonstrates with method.
342
346
@@ -376,7 +380,7 @@ If the object doesn't have a ``render`` method, then an AttributeError will be r
376
380
377
381
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.
378
382
379
-
But if, just by bad luck, it has an bug that raises an ``AttributeError`` -- then this could with catch it, and try to simply write it out instead. So you may get somethign like: ``<html_render.H object at 0x103604400>`` in the middle of your html.
383
+
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.
0 commit comments