Skip to content

Commit

Permalink
pulled Examples back from the fall class repo.
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonCHB committed Dec 16, 2016
1 parent a1d2763 commit 479f992
Show file tree
Hide file tree
Showing 24 changed files with 339 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Examples/Session01/schedule.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ week 6: Adam Hollis
week 6: Nachiket Galande
week 6: Paul A Casey
week 7: Charles E Robison
week 7: Paul S Briant
week 7: Paul Vosper
week 8: Paul S Briant
week 8: Brandon Chavis
week 8: Jay N Raina
week 8: Josh Hicks
Expand Down
2 changes: 1 addition & 1 deletion Examples/Session01/students.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ Vosper, Paul: C#, macscript, python
Weidner, Matthew T: German, python, html
Williams, Marcus D: python
Wong, Darryl: perl, php
Yang, Minghao
Yang, Minghao: None
Hagi, Abdishu: python, bash
7 changes: 4 additions & 3 deletions Examples/Session04/__main__example.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

print("What it is depends on how it is used")

print("right now, this module's __name is: {}".format(__name__))
print("right now, this module's __name__ is: {}".format(__name__))

# so if you want coce to run only when a module is a top level script,
# you use this clause:
if __name__ == "__main__":
print("I must be running as a top-level script")
#if __name__ == "__main__":

print("I must be running as a top-level script")
15 changes: 15 additions & 0 deletions Examples/Session06/cigar_party.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

"""
When squirrels get together for a party, they like to have cigars.
A squirrel party is successful when the number of cigars is between
40 and 60, inclusive. Unless it is the weekend, in which case there
is no upper bound on the number of cigars.
Return True if the party with the given values is successful,
or False otherwise.
"""


def cigar_party(num, weekend):
return num >= 40 and (num <= 60 or weekend)

31 changes: 31 additions & 0 deletions Examples/Session06/codingbat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python

"""
Examples from: http://codingbat.com
Put here so we can write unit tests for them ourselves
"""

# Python > Warmup-1 > sleep_in

# The parameter weekday is True if it is a weekday, and the parameter
# vacation is True if we are on vacation.
#
# We sleep in if it is not a weekday or we're on vacation.
# Return True if we sleep in.


def sleep_in(weekday, vacation):
return not weekday or vacation


# We have two monkeys, a and b, and the parameters a_smile and b_smile
# indicate if each is smiling.

# We are in trouble if they are both smiling or if neither of them is
# smiling.

# Return True if we are in trouble.

def monkey_trouble(a_smile, b_smile):
return a_smile is b_smile
25 changes: 25 additions & 0 deletions Examples/Session06/safe_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def safe_input():
try:
the_input = input("\ntype something >>> ")
except (KeyboardInterrupt, EOFError) as error:
print("the error: ", error)
error.extra_info = "extra info for testing"
# raise
return None
return the_input

def main():
safe_input()

def divide(x,y):
try:
return x/y
except ZeroDivisionError as err:
print("you put in a zero!!!")
print("the exeption:", err)
err.args = (("very bad palce for a zero",))
err.extra_stuff = "all kinds of things"
raise

# if __name__ == '__main__':
# main()
1 change: 1 addition & 0 deletions Examples/Session06/test_cigar_party.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ def test_10():

def test_11():
assert cigar_party(39, True) is False

43 changes: 43 additions & 0 deletions Examples/Session06/test_codingbat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python

"""
test file for codingbat module
This version can be run with nose or py.test
"""

from codingbat import sleep_in, monkey_trouble


# tests for sleep_in
def test_false_false():
assert sleep_in(False, False)


def test_true_false():
assert not (sleep_in(True, False))


def test_false_true():
assert sleep_in(False, True)


def test_true_true():
assert sleep_in(True, True)


# put tests for monkey_trouble here
# monkey_trouble(True, True) → True
# monkey_trouble(False, False) → True
# monkey_trouble(True, False) → False

def test_monkey_true_true():
assert monkey_trouble(True, True)

def test_monkey_false_false():
assert monkey_trouble(False, False)

def test_monkey_true_false():
assert monkey_trouble(True, False) is False

# more!
31 changes: 31 additions & 0 deletions Examples/Session06/test_pytest_parameter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python

"""
pytest example of a parameterized test
NOTE: there is a failure in here! can you fix it?
"""
import pytest


# a (really simple) function to test
def add(a, b):
"""
returns the sum of a and b
"""
return a + b

# now some test data:

test_data = [((2, 3), 5),
((-3, 2), -1),
((2, 0.5), 2.5),
(("this", "that"), "this that"),
(([1, 2, 3], [6, 7, 8]), [1, 2, 3, 6, 7, 8]),
]


@pytest.mark.parametrize(("input", "result"), test_data)
def test_add(input, result):
assert add(*input) == result
43 changes: 43 additions & 0 deletions Examples/Session06/test_random_pytest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python

"""
port of the random unit tests from the python docs to py.test
"""

import random
import pytest


seq = list(range(10))


def test_shuffle():
# make sure the shuffled sequence does not lose any elements
random.shuffle(seq)
# seq.sort() # commenting this out will make it fail, so we can see output
print("seq:", seq) # only see output if it fails
assert seq == list(range(10))


def test_shuffle_immutable():
"""should get a TypeError with an imutable type """
with pytest.raises(TypeError):
random.shuffle((1, 2, 3))


def test_choice():
"""make sure a random item selected is in the original sequence"""
element = random.choice(seq)
assert (element in seq)


def test_sample():
"""make sure all items returned by sample are there"""
for element in random.sample(seq, 5):
assert element in seq


def test_sample_too_large():
"""should get a ValueError if you try to sample too many"""
with pytest.raises(ValueError):
random.sample(seq, 20)
30 changes: 30 additions & 0 deletions Examples/Session06/test_random_unitest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import random
import unittest


class TestSequenceFunctions(unittest.TestCase):

def setUp(self):
self.seq = list(range(10))

def test_shuffle(self):
# make sure the shuffled sequence does not lose any elements
random.shuffle(self.seq)
self.seq.sort()
self.assertEqual(self.seq, list(range(10)))

# should raise an exception for an immutable sequence
self.assertRaises(TypeError, random.shuffle, (1, 2, 3))

def test_choice(self):
element = random.choice(self.seq)
self.assertTrue(element in self.seq)

def test_sample(self):
with self.assertRaises(ValueError):
random.sample(self.seq, 20)
for element in random.sample(self.seq, 5):
self.assertTrue(element in self.seq)

if __name__ == '__main__':
unittest.main()
27 changes: 19 additions & 8 deletions Examples/Session07/html_render/run_html_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,25 @@ def render_page(page, filename):

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")

render_page(page, "test_html_output1.html")

# The rest of the steps have been commented out.
# Uncomment them a you move along with the assignment.

# ## 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("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"))

Expand All @@ -75,7 +80,8 @@ def render_page(page, filename):

# 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("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)
Expand All @@ -94,7 +100,8 @@ def render_page(page, filename):

# 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("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)
Expand All @@ -113,7 +120,8 @@ def render_page(page, filename):

# 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("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())
Expand All @@ -134,7 +142,8 @@ def render_page(page, filename):

# 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("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())
Expand All @@ -161,7 +170,8 @@ def render_page(page, filename):

# 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",
# 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())
Expand Down Expand Up @@ -200,7 +210,8 @@ def render_page(page, filename):

# 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",
# 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())
Expand Down
12 changes: 12 additions & 0 deletions Examples/Session07/simple_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ def __init__(self, x, y):
def get_color(self):
return self.color

def get_size(self):
return self.size

class Rect:

def __init__(self, w, h):
self.w = w
self.h = h

def get_size(self):
return self.w * self.h


p3 = Point3(4, 5)
print(p3.size)
Expand Down
14 changes: 13 additions & 1 deletion Examples/Session08/circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@


class Circle:
pass
@staticmethod
def a_method(x):
return x**2

def regular_method(self):
print(self)

@classmethod
def class_method(cls):
print(cls)



Empty file.
Loading

0 comments on commit 479f992

Please sign in to comment.