Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sparse and circle have been my favorite exercises so far. Learned a TON #100

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions students/MichaelGregor/Session 7/html_render.py
Original file line number Diff line number Diff line change
@@ -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'
224 changes: 224 additions & 0 deletions students/MichaelGregor/Session 7/run_html_render.py
Original file line number Diff line number Diff line change
@@ -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")
27 changes: 27 additions & 0 deletions students/MichaelGregor/Session 7/sample_html.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>PythonClass = Revision 1087:</title>
</head>
<body>
<h2>PythonClass - Class 6 example</h2>
<p style="text-align: center; font-style: oblique;">
Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text
</p>
<hr />
<ul style="line-height:200%" id="TheList">
<li>
The first item in a list
</li>
<li style="color: red">
This is the second item
</li>
<li>
And this is a
<a href="http://google.com">link</a>
to google
</li>
</ul>
</body>
</html>
4 changes: 4 additions & 0 deletions students/MichaelGregor/Session 7/test_html_output1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html>
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
</html>
10 changes: 10 additions & 0 deletions students/MichaelGregor/Session 7/test_html_output2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<body>
<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
</p>
<p>
And here is another piece of text -- you should be able to add any number
</p>
</body>
</html>
15 changes: 15 additions & 0 deletions students/MichaelGregor/Session 7/test_html_output3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head>
<title>
PythonClass = Revision 1087:
</title>
</head>
<body>
<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
</p>
<p>
And here is another piece of text -- you should be able to add any number
</p>
</body>
</html>
12 changes: 12 additions & 0 deletions students/MichaelGregor/Session 7/test_html_output4.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<head>
<title>
PythonClass = Revision 1087:
</title>
</head>
<body>
<p style="text-align: center; font-style: oblique;">
Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text
</p>
</body>
</html>
Loading