Skip to content

Commit f21eae0

Browse files
committed
Merge pull request UWPCE-PythonCert#85 from ARSimmons/master
HMWK from 6 & 7 also initial code for class project
2 parents f6786ff + 9635b26 commit f21eae0

File tree

18 files changed

+774
-0
lines changed

18 files changed

+774
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
__author__ = 'Ari'
2+
3+
# STEP 1:
4+
# -
5+
6+
# Step 3:
7+
# - create a head element
8+
9+
10+
# Step 4:
11+
# - create a class to accept a set of attributes
12+
# remember "**kwargs" --
13+
14+
# Step 5:
15+
# - create a SelfClosingTag
16+
17+
18+
19+
class Element(object):
20+
# 4 spaces = indent,
21+
# these are called class attributes
22+
indent = ' '
23+
24+
## chris used **attributes instead of **kwargs
25+
def __init__(self, content=None, **kwargs):
26+
# we have a dictionary of keys
27+
self.attributes = kwargs
28+
self.content = []
29+
if content is not None:
30+
self.content.append(content)
31+
32+
def append(self, content):
33+
self.content.append(content)
34+
35+
def render_tag(self, current_ind):
36+
# because there is some code duplication in OneLineTag and def render
37+
# we wrote this to deal with the tag formatting
38+
attrs = "".join([' {}="{}"'.format(k, v) for k, v in self.attributes.items()])
39+
tag_str = "{}<{}{}>".format(current_ind, self.tag,attrs)
40+
return tag_str
41+
42+
43+
def render(self, file_out, current_ind=""):
44+
"""
45+
render, just renders itself
46+
"""
47+
# from kwargs...need to format a dictionary of items, replaced with render_tag
48+
#attrs = "".join([' {}="{}"'.format(k, v) for k, v in self.kwargs.items()])
49+
50+
# file_out -- File name in which to output render
51+
52+
# a file_out has a write method
53+
# carriage return is necessary
54+
# so you could have file_out.write("<html>\n")...
55+
# but once you start using the class Html..
56+
# this isn't going to work so well (so '.format' to the rescue)
57+
58+
file_out.write(self.render_tag(current_ind))
59+
# adding the carriage return since we took it out of render_tag
60+
file_out.write('\n')
61+
# append content in a list as entered
62+
# print self.content
63+
for con in self.content:
64+
#print "trying to render",con
65+
try:
66+
file_out.write(current_ind+ self.indent + con+"\n")
67+
except TypeError:
68+
# in order to have indent = ""...we are modifying the
69+
# embedded element to add an indent
70+
# by
71+
con.render(file_out, current_ind+self.indent)
72+
file_out.write("{}</{}>\n".format(current_ind,self.tag))
73+
74+
75+
76+
class OneLineTag(Element):
77+
def render(self, file_out, current_ind=""):
78+
file_out.write(self.render_tag(current_ind))
79+
for con in self.content:
80+
file_out.write(con)
81+
file_out.write("</{}>\n".format(self.tag))
82+
83+
class Title(OneLineTag):
84+
tag = 'hr'
85+
86+
class SelfClosingTag(Element):
87+
def render(self, file_out, current_ind=""):
88+
# a little array slicing notation
89+
file_out.write(self.render_tag(current_ind)[:-1])
90+
file_out.write(" />\n")
91+
92+
class Hr(SelfClosingTag):
93+
tag = 'hr'
94+
95+
96+
class A(OneLineTag):
97+
tag ='a'
98+
# it is content, and not self.content because you
99+
# haven't assigned it
100+
def __init__(self, link, content, **kwargs):
101+
OneLineTag.__init__(self, content=None, **kwargs)
102+
self.attributes["href"] = link
103+
104+
class H(OneLineTag):
105+
def __init__(self, level, content=None, **kwargs):
106+
OneLineTag.__init__(self, content, **kwargs)
107+
self.tag = "h%i"%(level)
108+
109+
class U1(Element):
110+
tag = "u1"
111+
112+
class Li(Element):
113+
tag='li'
114+
115+
class Html(Element):
116+
tag = 'html'
117+
def render(self, file_out, current_ind=""):
118+
file_out.write("<!DOCTYPE html>\n")
119+
Element.render(self, file_out, current_ind=current_ind)
120+
121+
class Body(Element):
122+
tag = 'body'
123+
124+
class P(Element):
125+
tag = 'p'
126+
127+
class Head(Element):
128+
tag = 'head'
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
__author__ = 'Ari'
2+
3+
from cStringIO import StringIO
4+
5+
import html_render as hr
6+
7+
reload(hr) # for ipython
8+
9+
## writing file out
10+
11+
12+
def render_page(page, filename):
13+
"""
14+
render the tree of elements
15+
"""
16+
17+
f = StringIO()
18+
page.render(f, " ")
19+
20+
f.reset()
21+
22+
print f.read()
23+
24+
f.reset()
25+
open(filename, 'w').write(f.read())
26+
27+
28+
page = hr.Html()
29+
30+
head = hr.Head()
31+
32+
#head.append(hr.Meta(charset="UTF-8"))
33+
head.append(hr.Title("some stuff ex"))
34+
35+
page.append(head)
36+
37+
body = hr.Body()
38+
39+
body.append(hr.H(2, "Python Class - Hmwk 6 ex"))
40+
41+
body.append(hr.P("Here is a paragraph - keep reading!!",
42+
style="text-align: center, font-style: oblique;"))
43+
44+
body.append(hr.Hr())
45+
46+
list = hr.U1(id="TheList", style="line-height:200%")
47+
48+
list.append(hr.Li("the first item"))
49+
list.append(hr.Li("the second item", style="color: red"))
50+
51+
item = hr.Li()
52+
item.append("And this is a ")
53+
item.append(hr.A("http://google.com", "link"))
54+
item.append("to google")
55+
56+
list.append(item)
57+
58+
body.append(list)
59+
60+
page.append(body)
61+
62+
render_page(page, "test_html_outpt.html")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<hr>some stuff ex</hr>
5+
</head>
6+
<body>
7+
<h2>Python Class - Hmwk 6 ex</h2>
8+
<p style="text-align: center, font-style: oblique;">
9+
Here is a paragraph - keep reading!!
10+
</p>
11+
<hr />
12+
<u1 style="line-height:200%" id="TheList">
13+
<li>
14+
the first item
15+
</li>
16+
<li style="color: red">
17+
the second item
18+
</li>
19+
<li>
20+
And this is a
21+
<a href="http://google.com"></a>
22+
to google
23+
</li>
24+
</u1>
25+
</body>
26+
</html>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
Python Session 7
2+
3+
4+
## Mixin's
5+
6+
Mixins are a sort of class that is used to
7+
"mix in" extra properties and methods into a class.
8+
9+
## New-Style Classes
10+
11+
super(): use it to call a superclass method, rather than explicitly calling the unbound
12+
method on the superclass.
13+
14+
Way to call a method on class
15+
16+
ex:
17+
18+
instead of:
19+
20+
class A(B):
21+
def __init__ (self, *args, **kwargs)
22+
B.__init__(self, *args, **kwargs)
23+
24+
you can:
25+
26+
class A(B):
27+
def init
28+
super (A, self).__init(*args, **kwargs)
29+
30+
Properties:
31+
32+
33+
- Something that looks to the outside user as an attribute,
34+
but internally it is something
35+
36+
"_x" : private attribute, external users not suppose to be using it
37+
38+
class C(object)
39+
40+
- What is up with the "@" symbols?
41+
- Special syntax for wrapping a function with a function
42+
- Those are decorators, it's a syntax for wrapping functions up with
43+
- something special.
44+
45+
ex:
46+
47+
@property
48+
49+
def x(self):
50+
# means make a property called x with a getter
51+
52+
- NOTE: when you define a property you should define a getter
53+
- You DO NOT need to define a setter. If you don't you
54+
- get a read-only attribute
55+
56+
57+
EXAMPLE CODE FORE PROPERTY
58+
59+
"""
60+
Example code for properties
61+
62+
"""
63+
64+
class C(object):
65+
_x = None
66+
@property
67+
def x(self):
68+
return self._x # returns c
69+
@x.setter
70+
def x(self, value):
71+
self._x = value
72+
@x.deleter
73+
def x(self):
74+
del self._x
75+
76+
if __name__ == "__main__":
77+
c = C()
78+
c.x = 5
79+
print c.x
80+
81+
## Static Methods
82+
83+
A static method is a method that doesn't get self:
84+
85+
class StaticAdder(object):
86+
@staticmethod
87+
def add(a,b);
88+
return a + b
89+
90+
>> StaticAdder.add(3,6)
91+
92+
Note: when you run "type()" and get 'type'...it is basically saying it is a class
93+
94+
Note: For circle assignment use **2 = ^2
95+
96+
## Protocols
97+
98+
The set of special methods needed to emulate a particular type
99+
of Python object is called a protocol
100+
101+
Your classes can "become" like a pre-existing python module
102+
103+
104+

0 commit comments

Comments
 (0)