diff --git a/students/MichaelGregor/Session 7/html_render.py b/students/MichaelGregor/Session 7/html_render.py new file mode 100644 index 0000000..e0f11f8 --- /dev/null +++ b/students/MichaelGregor/Session 7/html_render.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python + +""" +Python class example. +""" + + +# The start of it all: +# Fill it all in here. +class Element(object): + + tag = 'html' # shouldn't really be usable without properly subclassing + indent = ' ' + + def __init__(self, content=None, **attributes): + + self.content = [] + # adding attributes dictionary + self.attributes = attributes + + if content is not None: + self.content.append(content) + + def append(self, content): + self.content.append(content) + + # added render tag method to deal with any type of tag at indentation level + def render_tag(self, current_ind): + # tag and then content for each class + attrs = "".join([' {}="{}"'.format(key, val) for key, val in self.attributes.items()]) + # indentation + tag + content + tag_str = "{}<{}{}>".format(current_ind, self.tag, attrs) + return tag_str + + def render(self, file_out, current_ind=""): + # render method now calls the render tag method instead of just a string + file_out.write(self.render_tag(current_ind)) + file_out.write('\n') + for con in self.content: + try: + file_out.write(current_ind + self.indent + con+"\n") + except TypeError: + con.render(file_out, current_ind+self.indent) + # write out closing tag + file_out.write("{}{}>\n".format(current_ind, self.tag)) + + +class Body(Element): + tag = 'body' + + +class P(Element): + tag = 'p' + + +class Html(Element): + tag = 'html' + + +class Head(Element): + tag = 'head' + + +class Title(Element): + tag = 'title' diff --git a/students/MichaelGregor/Session 7/run_html_render.py b/students/MichaelGregor/Session 7/run_html_render.py new file mode 100644 index 0000000..8c76b48 --- /dev/null +++ b/students/MichaelGregor/Session 7/run_html_render.py @@ -0,0 +1,224 @@ +#!/usr/bin/env python + +""" +a simple script can run and test your html rendering classes. + +Uncomment the steps as you add to your rendering. + +""" + +from io import StringIO + +# importing the html_rendering code with a short name for easy typing. +import html_render as hr +# reloading in case you are running this in iPython +# -- we want to make sure the latest version is used +import importlib +importlib.reload(hr) + + +# writing the file out: +def render_page(page, filename): + """ + render the tree of elements + + This uses StringIO to render to memory, then dump to console and + write to file -- very handy! + """ + + f = StringIO() + page.render(f, " ") + + f.seek(0) + + print(f.read()) + + f.seek(0) + open(filename, 'w').write(f.read()) + + +# Step 1 +######### + +#page = hr.Element() + +#page.append("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text") + +#page.append("And here is another piece of text -- you should be able to add any number") + +#render_page(page, "test_html_output1.html") + +# ## Step 2 +# ########## + +# page = hr.Html() + +# body = hr.Body() + +# body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text")) + +# body.append(hr.P("And here is another piece of text -- you should be able to add any number")) + +# page.append(body) + +# render_page(page, "test_html_output2.html") + +# # Step 3 +# ########## + +# page = hr.Html() + +# head = hr.Head() +# head.append(hr.Title("PythonClass = Revision 1087:")) + +# page.append(head) + +# body = hr.Body() + +# body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text")) +# body.append(hr.P("And here is another piece of text -- you should be able to add any number")) + +# page.append(body) + +# render_page(page, "test_html_output3.html") + +# # Step 4 +# ########## + +# page = hr.Html() + +# head = hr.Head() +# head.append(hr.Title("PythonClass = Revision 1087:")) + +# page.append(head) + +# body = hr.Body() + +# body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text", +# style="text-align: center; font-style: oblique;")) + +# page.append(body) + +# render_page(page, "test_html_output4.html") + +# # Step 5 +# ######### + +page = hr.Html() + +head = hr.Head() +head.append(hr.Title("PythonClass = Revision 1087:")) + +page.append(head) + +body = hr.Body() + +body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text", + style="text-align: center; font-style: oblique;")) + +body.append(hr.Hr()) + +page.append(body) + +render_page(page, "test_html_output5.html") + +# # Step 6 +# ######### + +page = hr.Html() + +head = hr.Head() +head.append(hr.Title("PythonClass = Revision 1087:")) + +page.append(head) + +body = hr.Body() + +body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text", + style="text-align: center; font-style: oblique;")) + +body.append(hr.Hr()) + +body.append("And this is a ") +body.append( hr.A("http://google.com", "link") ) +body.append("to google") + +page.append(body) + +render_page(page, "test_html_output6.html") + +# # Step 7 +# ######### + +# page = hr.Html() + +# head = hr.Head() +# head.append(hr.Title("PythonClass = Revision 1087:")) + +# page.append(head) + +# body = hr.Body() + +# body.append( hr.H(2, "PythonClass - Class 6 example") ) + +# body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text", +# style="text-align: center; font-style: oblique;")) + +# body.append(hr.Hr()) + +# list = hr.Ul(id="TheList", style="line-height:200%") + +# list.append( hr.Li("The first item in a list") ) +# list.append( hr.Li("This is the second item", style="color: red") ) + +# item = hr.Li() +# item.append("And this is a ") +# item.append( hr.A("http://google.com", "link") ) +# item.append("to google") + +# list.append(item) + +# body.append(list) + +# page.append(body) + +# render_page(page, "test_html_output7.html") + +# # Step 8 +# ######## + +# page = hr.Html() + + +# head = hr.Head() +# head.append( hr.Meta(charset="UTF-8") ) +# head.append(hr.Title("PythonClass = Revision 1087:")) + +# page.append(head) + +# body = hr.Body() + +# body.append( hr.H(2, "PythonClass - Class 6 example") ) + +# body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text", +# style="text-align: center; font-style: oblique;")) + +# body.append(hr.Hr()) + +# list = hr.Ul(id="TheList", style="line-height:200%") + +# list.append( hr.Li("The first item in a list") ) +# list.append( hr.Li("This is the second item", style="color: red") ) + +# item = hr.Li() +# item.append("And this is a ") +# item.append( hr.A("http://google.com", "link") ) +# item.append("to google") + +# list.append(item) + +# body.append(list) + +# page.append(body) + +# render_page(page, "test_html_output8.html") diff --git a/students/MichaelGregor/Session 7/sample_html.html b/students/MichaelGregor/Session 7/sample_html.html new file mode 100644 index 0000000..f2687e9 --- /dev/null +++ b/students/MichaelGregor/Session 7/sample_html.html @@ -0,0 +1,27 @@ + + +
+ ++ Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text +
++ Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text +
++ And here is another piece of text -- you should be able to add any number +
+ + diff --git a/students/MichaelGregor/Session 7/test_html_output3.html b/students/MichaelGregor/Session 7/test_html_output3.html new file mode 100644 index 0000000..8f0e72b --- /dev/null +++ b/students/MichaelGregor/Session 7/test_html_output3.html @@ -0,0 +1,15 @@ + + ++ Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text +
++ And here is another piece of text -- you should be able to add any number +
+ + diff --git a/students/MichaelGregor/Session 7/test_html_output4.html b/students/MichaelGregor/Session 7/test_html_output4.html new file mode 100644 index 0000000..65a2364 --- /dev/null +++ b/students/MichaelGregor/Session 7/test_html_output4.html @@ -0,0 +1,12 @@ + + ++ Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text +
+ + diff --git a/students/MichaelGregor/Session 9/Circle.py b/students/MichaelGregor/Session 9/Circle.py new file mode 100644 index 0000000..753edd2 --- /dev/null +++ b/students/MichaelGregor/Session 9/Circle.py @@ -0,0 +1,107 @@ +import math + +class Circle(object): + + def __init__(self, radius): + self._radius = radius + self._diameter = radius * 2 + + def __repr__(self): + return 'Circle(' + repr(self._radius) + ')' + + def __str__(self): + return 'Circle(' + str(self._radius) + ')' + + def __add__(self, other): + r1, r2 = self.radius, other.radius + total = r1 + r2 + return Circle(total) + + def __eq__(self, other): + r1, r2 = self.radius, other.radius + if r1 == r2: + return True + else: + return False + + def __gt__(self, other): + r1, r2 = self.radius, other.radius + if r1 > r2: + return True + else: + return False + + def __lt__(self, other): + r1, r2 = self.radius, other.radius + if r1 < r2: + return True + else: + return False + + @property + def radius(self): + return self._radius + + @property + def diameter(self): + return self._diameter + + @diameter.setter + def diameter(self, value): + self._diameter = value + self._radius = value/2 + + @property + def area(self): + return 2 * math.pi * self._radius + + @classmethod + def from_diameter(cls, value): + return cls(value/2) + + +def main(): + + c = Circle(10) + + print(c.radius) + print(c.diameter) + + c.diameter = 50 + + print(c.diameter) + print(c.radius) + + c = Circle(2) + print(c.area) + + c = Circle.from_diameter(8) + + print(c.diameter) + print(c.radius) + + print(repr(c)) + print(str(c)) + + d = eval(repr(c)) + + print(d) + + c1 = Circle(4) + c2 = Circle(2) + + print(c1) + print(c2) + + c3 = c1+c2 + print(c3._radius) + + print(c1 == c2) + + print(c1>c2) + print(c1