Skip to content

Commit 802a423

Browse files
committed
cleaned up html render a bit
1 parent addcae8 commit 802a423

File tree

2 files changed

+84
-9
lines changed

2 files changed

+84
-9
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
.. _exercise_args_kwargs_lab:
2+
3+
*******************
4+
args and kwargs Lab
5+
*******************
6+
7+
Learning about ``args`` and ``kwargs``
8+
======================================
9+
10+
Goal:
11+
-----
12+
13+
Develop an understanding of using advanced argument passing and parameter definitons.
14+
15+
If this is all confusing -- you may want to review this:
16+
17+
http://stupidpythonideas.blogspot.com/2013/08/arguments-and-parameters.html
18+
19+
Procedure
20+
---------
21+
22+
**Keyword arguments:**
23+
24+
* Write a function that has four optional parameters (with defaults):
25+
26+
- `fore_color`
27+
- `back_color`
28+
- `link_color`
29+
- `visited_color`
30+
31+
* Have it print the colors (use strings for the colors)
32+
33+
* Call it with a couple different parameters set
34+
35+
- using just positional arguments:
36+
37+
- ``func('red', 'blue', 'yellow', 'chartreuse')``
38+
39+
- using just keyword arguments:
40+
41+
- ``func(link_color='red', back_color='blue')``
42+
43+
- using a combination of positional and keyword
44+
45+
- ````func('purple', link_color='red', back_color='blue')``
46+
47+
- using ``*some_tuple`` and/or ``**some_dict``
48+
49+
- ``regular = ('red', 'blue')``
50+
51+
- ``links = {'link_color': 'chartreuse'}``
52+
53+
- ``func(*regular, *links)``
54+
55+
.. nextslide::
56+
57+
**Generic parameters:**
58+
59+
* 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.
70+
71+

slides_sources/source/exercises/html_renderer.rst

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ You should be able to run that code at each step, uncommenting each new step in
3939

4040
It builds up an html tree, and then calls the ``render()`` method of your element to render the page.
4141

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.
4343

4444
The html generated at each step will be in the files: ``test_html_ouput?.html``
4545

@@ -176,7 +176,7 @@ Create a couple subclasses of ``SelfClosingTag`` for and <hr /> and <br />
176176

177177
Note that you now have a couple render methods -- is there repeated code in them?
178178

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?
180180

181181
See ``test_html_output5.html``
182182

@@ -191,7 +191,7 @@ where ``link`` is the link, and ``content`` is what you see. It can be called li
191191

192192
A("http://google.com", "link to google")
193193

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__``
195195

196196
You can now add a link to your web page.
197197

@@ -266,7 +266,8 @@ You can handle it ahead of time by creating a simple object that wraps a string
266266
self.text = text
267267
268268
def render(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)
270271
271272
.. nextslide::
272273

@@ -321,22 +322,25 @@ I think this is a little better -- strings are a pretty core type in python, it'
321322
Duck Typing
322323
-----------
323324

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.
325326

326327
But in this case, we're not actually rendering the object at this stage, so calling the method isn't appropriate.
327328

328329
**Checking for an attribute**
329330

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()``
331332

332-
You can check if the passed-in object has a ``render()`` attribute:
333+
check if the passed-in object has a ``render()`` attribute:
333334

334335
.. code-block:: python
335336
336337
if hasattr(content, 'render'):
337338
self.content.append(content)
338339
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 pass in anything -- it it will get string-ified -- this will be ugly for many objects, but will work fine for numbers and other simple objects.
340344
341345
This is my favorite. ``html_render_wrap.py`` in Solutions demonstrates with method.
342346
@@ -376,7 +380,7 @@ If the object doesn't have a ``render`` method, then an AttributeError will be r
376380
377381
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.
378382
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, 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.
380384
381385
**The beauty of testing**
382386

0 commit comments

Comments
 (0)