Skip to content

Commit fcfc2b7

Browse files
committed
added some notes about indentation to html render exercise.
1 parent b1e9612 commit fcfc2b7

File tree

2 files changed

+74
-9
lines changed

2 files changed

+74
-9
lines changed

slides_sources/source/exercises/html_renderer.rst

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ It should have a ``render(file_out, ind = "")`` method that renders the tag and
9393

9494
The amount of each level of indentation should be set by the class attribute: ``indent``
9595

96-
NOTE: don't worry too much about indentation at this stage -- the primary goal is to get proper, compliant html. i.e. the opening and closing tags rendered correctly. Worry about cleaning up the indentation once you've got that working.
96+
NOTE: don't worry too much about indentation at this stage -- the primary goal is to get proper, compliant html. i.e. the opening and closing tags rendered correctly. Worry about cleaning up the indentation once you've got that working. See "Note on indentation" below for more explaination.
9797

9898
.. nextslide::
9999

@@ -235,6 +235,32 @@ new tags, etc....
235235

236236
See ``test_html_output8.html``
237237

238+
Note on indentation
239+
===================
240+
241+
Indentation is not stricly required for html -- html ignores most whitespace.
242+
243+
But it can make it much easier to read for humans, and it's a nice excercise to see how one might make it nice.
244+
245+
There is also more than one way to indent html -- so you have a bit of flexibility here.
246+
247+
So:
248+
249+
* You probably ``ind`` to be an optional argument to render -- so it will not indent if nothing is passed in. And that lets you write the code without indentation first if you like.
250+
251+
* But ultimately, you want your code to USE the ind parameter -- it is supposed to indicate how much this entire tag is already indented.
252+
253+
* When this one 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 ``ind`` into the render method is how this is communicated.
254+
255+
* You have (at least) two options for how to indicate level of indentation:
256+
257+
- It could be a integer indicating number of levels of indentation
258+
- It could, more simply, be a bunch of spaces.
259+
260+
* You want to have the amount of spaces per indentation defined as a class attribute of the base class (the ``Element`` class). That way, you could change it in one place, and it would change everywhere an remain consistent.
261+
262+
263+
238264
Notes on handling "duck typing"
239265
===============================
240266

slides_sources/source/session07.rst

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
Object Oriented Programming
55
***************************
66

7+
Lightning Talks today
8+
---------------------
9+
10+
.. rst-class:: medium
11+
12+
|
13+
| Charles E Robison
14+
|
15+
| Paul Vosper
16+
|
17+
718
================
819
Review/Questions
920
================
@@ -17,19 +28,47 @@ Review of Previous Class
1728

1829
Testing
1930

31+
Any questions?
2032

33+
Should I go over my solution(s)?
2134

22-
Lightning Talks today
23-
---------------------
2435

25-
.. rst-class:: medium
36+
Notes from homework:
37+
--------------------
2638

27-
|
28-
| Charles E Robison
29-
|
30-
| Paul Vosper
31-
|
39+
**chaining or...**
40+
41+
Consider this:
42+
43+
``elif selection == '3' or 'exit':``
44+
45+
Careful here: you want to check if selection is '3' or 'exit', but that is no quite what this means:
46+
47+
You want:
48+
49+
``(selection == '3') or (selection == 'exit')``
50+
51+
== has higher precedence than or, so you don't need the parentheses.
52+
53+
``selection == '3' or selection == 'exit'``
54+
55+
That feels like more typing, but that's what you have to do.
56+
57+
.. nextslide::
58+
59+
So what does the first version mean?
60+
61+
It would return true for '3', but would never fail. Due to operator precedence, it is:
62+
63+
``(selection == '3') or 'exit'``
64+
65+
so first it's checking if selection == '3', which will return True or False.
66+
67+
Then it does the or: ``True or 'exit'`` or ``False or 'exit'``
68+
69+
``or`` returns the first "truthy" value it finds, to it will return either True or 'exit', regardless of the value of selection. 'exit' is truthy, so this if clause will always run.
3270

71+
(let's try this out in iPython)
3372

3473

3574
===========================

0 commit comments

Comments
 (0)