Skip to content

Commit cc3150f

Browse files
committed
2 parents e3ed395 + bf427d9 commit cc3150f

File tree

8 files changed

+568
-17
lines changed

8 files changed

+568
-17
lines changed

materials/source/lightning_talks.rst

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,24 @@ Zandra Eng: How I got here
104104

105105

106106

107-
John Navitsky
108-
-------------
107+
John Navitsky: Ansible and python
108+
---------------------------------
109109

110-
Matthew Sachio Maeda
111-
--------------------
110+
Simple, modular, written in Python...
111+
112+
Matthew Sachio Maeda: Celery
113+
----------------------------
114+
115+
Parallel task runner.
116+
117+
118+
Morgan Heinemann: Selenium
119+
--------------------------
120+
121+
Open source project for web automation.
122+
123+
Can be driven by Python (and other languages)
112124

113-
Morgan Heinemann
114-
----------------
115125

116126
James Takata
117127
------------

materials/source/notes/session08.rst

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Matthew Sachio Maeda
1818

1919
Morgan Heinemann
2020

21+
Let's take a look at the past lightning talks list -- make sure we haven't missed anyone.
22+
2123

2224
Issues that came up during the week.
2325
====================================
@@ -58,16 +60,37 @@ I know I started out that way -- 'cause there wasn't anything else to test. But
5860
5961
6062
Do you always need an __init__?
61-
------------------------------
63+
-------------------------------
6264

6365
No -- you don't :-)
6466

65-
The ONLY thing "special" about __init__ is that it is automatically called when and instance is created. Other than that, it's a regular method. So if you don't define one, then the superclass' __init__ will be called.
67+
The ONLY thing "special" about __init__ is that it is automatically called when an instance is created. Other than that, it's a regular method. So if you don't define one, then the superclass' __init__ will be called.
6668

6769
That's what inheritance is all about -- the subclass inherits ALL the superclasses methods -- including __init__.
6870

6971
So never write an __init__ that does nothing but call the superclass __init__
7072

73+
Subclasses and ``self``
74+
-----------------------
75+
76+
``self`` is the first parameter in all methods. But why??
77+
78+
``self`` is the "current" instance of the object. This means that you don't know at code writing time what type it is -- is it the current class? some subclass?
79+
80+
Let's experiment with that.
81+
82+
html_render
83+
-----------
84+
85+
Let's look at up to step 3....
86+
87+
And move along...
88+
89+
Lightning Talks
90+
---------------
91+
92+
Circle class....
93+
7194

7295

7396

students/Chris/session07/html_render.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,46 @@ class Element():
55
tag = 'html'
66
indent = ' '
77

8-
def __init__(self, content=None):
8+
def __init__(self, content=None, **kwargs):
99
if content is None:
1010
self.content = []
1111
else:
1212
self.content = [content]
13+
self.attributes = kwargs
1314

1415
def append(self, content):
1516
self.content.append(content)
1617

1718
def render(self, file_obj, cur_ind=""):
18-
open_tag = '{}<{}>'.format(cur_ind, self.tag)
19+
open_tag = self.get_open_tag()
1920
close_tag = '{}</{}>'.format(cur_ind, self.tag)
2021

22+
file_obj.write(cur_ind)
2123
file_obj.write(open_tag)
2224
file_obj.write("\n")
2325
for each in self.content:
24-
if isinstance(each, str):
26+
try:
27+
each.render(file_obj, cur_ind + self.indent)
28+
except AttributeError:
2529
file_obj.write(cur_ind + self.indent)
2630
file_obj.write(each)
27-
else:
28-
each.render(file_obj, cur_ind + self.indent)
2931
file_obj.write("\n")
3032
file_obj.write(close_tag)
3133

34+
def get_open_tag(self):
35+
open_tag = '<{}'.format(self.tag)
36+
for at, val in self.attributes.items():
37+
open_tag += ' {}="{}"'.format(at, val)
38+
open_tag += "> "
39+
return open_tag
40+
41+
3242
class OneLineTag(Element):
3343
def render(self, file_obj, cur_ind=""):
3444
# there is some repition here -- maybe factor that out?
35-
file_obj.write('{}<{}> '.format(cur_ind, self.tag))
45+
open_tag = self.get_open_tag()
46+
file_obj.write(cur_ind)
47+
file_obj.write(open_tag)
3648
for each in self.content:
3749
# OneLineTags should only have strings...
3850
file_obj.write(each)

students/Chris/session07/test_html_render.py

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,15 @@ def test_indent():
242242
Tests that the indentation gets passed through to the renderer
243243
"""
244244
html = HTML("some content")
245-
file_contents = render_element(html, cur_ind=" ")
245+
cur_ind = 6 * " "
246+
file_contents = render_element(html, cur_ind=cur_ind)
246247

247248
print(file_contents)
248249
lines = file_contents.split("\n")
249250

250-
assert lines[0].startswith(" <")
251-
assert lines[-1].startswith(" <")
251+
assert lines[0].startswith(cur_ind + "<")
252+
assert lines[1].startswith(cur_ind + Element.indent + "som")
253+
assert lines[-1].startswith(cur_ind + "<")
252254

253255

254256
def test_indent_contents():
@@ -349,5 +351,75 @@ def test_full_page_with_title():
349351
# assert False
350352

351353

354+
def test_single_attribute():
355+
# <p style="text-align: center; font-style: oblique;">
356+
# Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text
357+
# </p>
358+
p = Para("Here is a paragraph of text", style="text-align: center; font-style: oblique;")
359+
360+
results = render_element(p)
361+
362+
assert results.startswith('<p style="text-align: center; font-style: oblique;">')
363+
364+
print(results)
365+
366+
def test_multiple_attributes():
367+
# <p style="text-align: center; font-style: oblique;">
368+
# Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text
369+
# </p>
370+
p = Para("Here is a paragraph of text",
371+
id="fred",
372+
color="red",
373+
size="12px",
374+
)
375+
376+
results = render_element(p)
377+
print(results)
378+
379+
lines = results.split('\n')
380+
assert lines[0].startswith('<p ')
381+
assert lines[0].endswith('"> ')
382+
assert 'id="fred"' in lines[0]
383+
assert 'color="red"' in lines[0]
384+
assert 'size="12px"' in lines[0]
385+
386+
def test_multiple_attributes_title():
387+
t = Title("Here is a paragraph of text",
388+
id="fred",
389+
color="red",
390+
size="12px",
391+
)
392+
393+
results = render_element(t)
394+
print(results)
395+
396+
lines = results.split('\n')
397+
assert lines[0].startswith('<title ')
398+
assert lines[0].endswith('</title>')
399+
assert 'id="fred"' in lines[0]
400+
assert 'color="red"' in lines[0]
401+
assert 'size="12px"' in lines[0]
402+
403+
404+
# test class attribute
405+
def test_class_attribute():
406+
atts = {"id": "fred",
407+
"class": "special",
408+
"size": "12px",
409+
}
410+
p = Para("Here is a paragraph of text",
411+
**atts)
412+
413+
results = render_element(p)
414+
print(results)
415+
416+
lines = results.split('\n')
417+
assert lines[0].startswith('<p ')
418+
assert lines[0].strip().endswith('">')
419+
assert 'id="fred"' in lines[0]
420+
assert 'class="special"' in lines[0]
421+
assert 'size="12px"' in lines[0]
422+
423+
352424

353425

students/Chris/session08/circle.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
nifty Circle class
3+
"""
4+
5+
from math import pi
6+
7+
class Circle:
8+
def __init__(self, radius):
9+
self.radius = radius
10+
11+
@property
12+
def diameter(self):
13+
return 2 * self.radius
14+
@diameter.setter
15+
def diameter(self, val):
16+
self.radius = val / 2
17+
18+
@property
19+
def area(self):
20+
return pi * self.radius**2
21+
22+
def __repr__(self):
23+
return "Circle({})".format(self.radius)
24+
25+
def __str__(self):
26+
return "Circle with radius: {}".format(self.radius)
27+
28+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
tests for Circle class
3+
"""
4+
5+
from math import pi
6+
import pytest
7+
8+
from circle import Circle
9+
10+
11+
def test_init():
12+
Circle(5)
13+
14+
assert True
15+
16+
def test_radius():
17+
c = Circle(5)
18+
19+
assert c.radius == 5
20+
21+
def test_diameter():
22+
c = Circle(4)
23+
24+
assert c.diameter == 8
25+
26+
27+
def test_change_radius():
28+
c = Circle(4)
29+
30+
assert c.diameter == 8
31+
32+
c.radius = 5
33+
assert c.radius == 5
34+
assert c.diameter == 10
35+
36+
37+
def test_change_diameter():
38+
c = Circle(4)
39+
40+
assert c.diameter == 8
41+
42+
c.diameter = 10
43+
assert c.radius == 5
44+
assert c.diameter == 10
45+
46+
def test_area():
47+
c = Circle(10)
48+
49+
assert c.area == pi * 10**2
50+
51+
def test_set_area():
52+
53+
c = Circle(10)
54+
55+
with pytest.raises(AttributeError):
56+
c.area = 100
57+
58+
def test_delete_diameter():
59+
c = Circle(10)
60+
61+
with pytest.raises(AttributeError):
62+
del c.diameter
63+
64+

0 commit comments

Comments
 (0)