From 03c630c2949a975af3d934903773423c4ae5c75b Mon Sep 17 00:00:00 2001 From: Michael Gregor Date: Sun, 21 Feb 2016 19:52:19 -0800 Subject: [PATCH 1/5] First html render commit. --- .../MichaelGregor/Session 7/html_render.py | 30 +++ .../Session 7/run_html_render.py | 224 ++++++++++++++++++ .../MichaelGregor/Session 7/sample_html.html | 27 +++ .../Session 7/test_html_output1.html | 2 + 4 files changed, 283 insertions(+) create mode 100644 students/MichaelGregor/Session 7/html_render.py create mode 100644 students/MichaelGregor/Session 7/run_html_render.py create mode 100644 students/MichaelGregor/Session 7/sample_html.html create mode 100644 students/MichaelGregor/Session 7/test_html_output1.html diff --git a/students/MichaelGregor/Session 7/html_render.py b/students/MichaelGregor/Session 7/html_render.py new file mode 100644 index 0000000..de0a6a9 --- /dev/null +++ b/students/MichaelGregor/Session 7/html_render.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +""" +Python class example. +""" + + +# The start of it all: +# Fill it all in here. +class Element(object): + + def __init__(self, content=None): + self.content = '\n' + + def append(self, new_content): + self.content += new_content + + def render(self, file_out, ind=""): + file_out.write(self.content) + +class Html(Element): + + def __init__(self, content=None): + self.content = '\t\n' + + +class Body(Element): + + def __init__(self, content=None): + self.content = '\t\t\n' 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..663e5ff --- /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 @@ + + + + + PythonClass = Revision 1087: + + +

PythonClass - Class 6 example

+

+ Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text +

+
+ + + \ No newline at end of file diff --git a/students/MichaelGregor/Session 7/test_html_output1.html b/students/MichaelGregor/Session 7/test_html_output1.html new file mode 100644 index 0000000..f3fca65 --- /dev/null +++ b/students/MichaelGregor/Session 7/test_html_output1.html @@ -0,0 +1,2 @@ + +Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some textAnd here is another piece of text -- you should be able to add any number \ No newline at end of file From bfa92406d6d0c52b219ee360ded731a72596e8e6 Mon Sep 17 00:00:00 2001 From: Michael Gregor Date: Wed, 2 Mar 2016 21:33:59 -0800 Subject: [PATCH 2/5] renderer --- .../MichaelGregor/Session 7/html_render.py | 57 +++++++++++++++---- .../Session 7/run_html_render.py | 54 +++++++++--------- .../Session 7/test_html_output1.html | 6 +- .../Session 7/test_html_output2.html | 10 ++++ .../Session 7/test_html_output3.html | 15 +++++ .../Session 7/test_html_output4.html | 12 ++++ 6 files changed, 114 insertions(+), 40 deletions(-) create mode 100644 students/MichaelGregor/Session 7/test_html_output2.html create mode 100644 students/MichaelGregor/Session 7/test_html_output3.html create mode 100644 students/MichaelGregor/Session 7/test_html_output4.html diff --git a/students/MichaelGregor/Session 7/html_render.py b/students/MichaelGregor/Session 7/html_render.py index de0a6a9..e0f11f8 100644 --- a/students/MichaelGregor/Session 7/html_render.py +++ b/students/MichaelGregor/Session 7/html_render.py @@ -9,22 +9,57 @@ # Fill it all in here. class Element(object): - def __init__(self, content=None): - self.content = '\n' + tag = 'html' # shouldn't really be usable without properly subclassing + indent = ' ' - def append(self, new_content): - self.content += new_content + def __init__(self, content=None, **attributes): - def render(self, file_out, ind=""): - file_out.write(self.content) + self.content = [] + # adding attributes dictionary + self.attributes = attributes -class Html(Element): + 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 __init__(self, content=None): - self.content = '\t\n' + 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' + - def __init__(self, content=None): - self.content = '\t\t\n' +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 index 663e5ff..8c76b48 100644 --- a/students/MichaelGregor/Session 7/run_html_render.py +++ b/students/MichaelGregor/Session 7/run_html_render.py @@ -40,13 +40,13 @@ def render_page(page, filename): # Step 1 ######### -page = hr.Element() +#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("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") +#page.append("And here is another piece of text -- you should be able to add any number") -render_page(page, "test_html_output1.html") +#render_page(page, "test_html_output1.html") # ## Step 2 # ########## @@ -104,48 +104,48 @@ def render_page(page, filename): # # Step 5 # ######### -# page = hr.Html() +page = hr.Html() -# head = hr.Head() -# head.append(hr.Title("PythonClass = Revision 1087:")) +head = hr.Head() +head.append(hr.Title("PythonClass = Revision 1087:")) -# page.append(head) +page.append(head) -# body = hr.Body() +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.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(hr.Hr()) -# page.append(body) +page.append(body) -# render_page(page, "test_html_output5.html") +render_page(page, "test_html_output5.html") # # Step 6 # ######### -# page = hr.Html() +page = hr.Html() -# head = hr.Head() -# head.append(hr.Title("PythonClass = Revision 1087:")) +head = hr.Head() +head.append(hr.Title("PythonClass = Revision 1087:")) -# page.append(head) +page.append(head) -# body = hr.Body() +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.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(hr.Hr()) -# body.append("And this is a ") -# body.append( hr.A("http://google.com", "link") ) -# body.append("to google") +body.append("And this is a ") +body.append( hr.A("http://google.com", "link") ) +body.append("to google") -# page.append(body) +page.append(body) -# render_page(page, "test_html_output6.html") +render_page(page, "test_html_output6.html") # # Step 7 # ######### diff --git a/students/MichaelGregor/Session 7/test_html_output1.html b/students/MichaelGregor/Session 7/test_html_output1.html index f3fca65..ffd85af 100644 --- a/students/MichaelGregor/Session 7/test_html_output1.html +++ b/students/MichaelGregor/Session 7/test_html_output1.html @@ -1,2 +1,4 @@ - -Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some textAnd here is another piece of text -- you should be able to add any number \ No newline at end of file + + 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_output2.html b/students/MichaelGregor/Session 7/test_html_output2.html new file mode 100644 index 0000000..c304baa --- /dev/null +++ b/students/MichaelGregor/Session 7/test_html_output2.html @@ -0,0 +1,10 @@ + + +

+ 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 @@ + + + + PythonClass = Revision 1087: + + + +

+ 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 @@ + + + + PythonClass = Revision 1087: + + + +

+ Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text +

+ + From e582e010df728ae2a89704cab6825c4d9bbd1bed Mon Sep 17 00:00:00 2001 From: Michael Gregor Date: Wed, 9 Mar 2016 18:27:28 -0800 Subject: [PATCH 3/5] session 9 --- .../MichaelGregor/Session 9/SparseArray.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 students/MichaelGregor/Session 9/SparseArray.py diff --git a/students/MichaelGregor/Session 9/SparseArray.py b/students/MichaelGregor/Session 9/SparseArray.py new file mode 100644 index 0000000..257215d --- /dev/null +++ b/students/MichaelGregor/Session 9/SparseArray.py @@ -0,0 +1,31 @@ + + +class SparseArray(object): + + def __init__(self, array=[]): + self._array = array + + @property + def array(self): + return self.array + + @array.setter + def array(self, item): + self.array.append(item) + + @array.deleter + def array(self, index): + self.array.pop[index] + + +def main(): + + sa = SparseArray([1, 2, 3, 4]) + + + + +if __name__ == "__main__": + main() + + From d784408f9595310e4a139c3bfd6b3139b1afc2cf Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 9 Mar 2016 21:01:15 -0800 Subject: [PATCH 4/5] Sparse Array mostly completed. Very cool assignment actually. learned a lot --- .../MichaelGregor/Session 9/SparseArray.py | 62 ++++++++++++++----- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/students/MichaelGregor/Session 9/SparseArray.py b/students/MichaelGregor/Session 9/SparseArray.py index 257215d..0ce31d7 100644 --- a/students/MichaelGregor/Session 9/SparseArray.py +++ b/students/MichaelGregor/Session 9/SparseArray.py @@ -2,27 +2,55 @@ class SparseArray(object): - def __init__(self, array=[]): - self._array = array - - @property - def array(self): - return self.array - - @array.setter - def array(self, item): - self.array.append(item) - - @array.deleter - def array(self, index): - self.array.pop[index] + def __init__(self, array): + if array == None: + return self._array + else: + self._array = array + + def __getitem__(self, key): + try: + return self._array[key] + except IndexError: + return 0 + + def __setitem__(self, key, value): + try: + if value is not 0: + self._array[key] = value + except IndexError: + num_diff = (key - len(self._array)) + for index in range(num_diff): + self._array.append(0) + self._array.append(value) + + def __delitem__(self, key): + del self._array[key] + + def __len__(self): + return len(self._array) + + def append(self, item): + self._array.append(item) def main(): - sa = SparseArray([1, 2, 3, 4]) - - + sa = SparseArray([1,2,0,0,0,0,3,0,0,4]) + print(len(sa)) + sa.append(5) + print("index 10 is {}".format(sa[10])) + sa[2] = 9 + print("index 2 is {}".format(sa[2])) + del sa[2] + print("index 2 is {}".format(sa[2])) + sa[15] = 20 + print("index 15 is {}".format(sa[15])) + print("index 12 is {}".format(sa[12])) + sa[15] = 0 + print("index 15 is {}".format(sa[15])) + value = sa[25] + print(value) if __name__ == "__main__": From 3de88c2befc551ff7f58603f3d33a48cb3e8f36a Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 9 Mar 2016 22:05:23 -0800 Subject: [PATCH 5/5] All of the circle lab cept for very last section of part 8. EXTREMELY GREAT EXERCISE!! LEARNED A TON! --- students/MichaelGregor/Session 9/Circle.py | 107 +++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 students/MichaelGregor/Session 9/Circle.py 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