Skip to content

Commit fa118ee

Browse files
committed
deploy: f2c59a8
0 parents  commit fa118ee

561 files changed

Lines changed: 111662 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.buildinfo

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Sphinx build info version 1
2+
# This file records the configuration used when building these files. When it is not found, a full rebuild will be done.
3+
config: 7c1e11b48ca765e2de4fd016241471d9
4+
tags: 645f666f9bcd5a90fca523b33c5a78b7

.nojekyll

Whitespace-only changes.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""
2+
roman.py
3+
4+
A Roman numeral to arabic numeral (and back!) converter
5+
6+
complete with tests
7+
8+
tests are expected to be able to be run with the pytest system
9+
"""
10+
11+
12+
def to_roman(n):
13+
'''convert an integer to Roman numeral'''
14+
pass
15+
16+
17+
## Tests for roman numeral conversion
18+
19+
KNOWN_VALUES = ( (1, 'I'),
20+
(2, 'II'),
21+
(3, 'III'),
22+
(4, 'IV'),
23+
(5, 'V'),
24+
(6, 'VI'),
25+
(7, 'VII'),
26+
(8, 'VIII'),
27+
(9, 'IX'),
28+
(10, 'X'),
29+
(50, 'L'),
30+
(100, 'C'),
31+
(500, 'D'),
32+
(1000, 'M'),
33+
(31, 'XXXI'),
34+
(148, 'CXLVIII'),
35+
(294, 'CCXCIV'),
36+
(312, 'CCCXII'),
37+
(421, 'CDXXI'),
38+
(528, 'DXXVIII'),
39+
(621, 'DCXXI'),
40+
(782, 'DCCLXXXII'),
41+
(870, 'DCCCLXX'),
42+
(941, 'CMXLI'),
43+
(1043, 'MXLIII'),
44+
(1110, 'MCX'),
45+
(1226, 'MCCXXVI'),
46+
(1301, 'MCCCI'),
47+
(1485, 'MCDLXXXV'),
48+
(1509, 'MDIX'),
49+
(1607, 'MDCVII'),
50+
(1754, 'MDCCLIV'),
51+
(1832, 'MDCCCXXXII'),
52+
(1993, 'MCMXCIII'),
53+
(2074, 'MMLXXIV'),
54+
(2152, 'MMCLII'),
55+
(2212, 'MMCCXII'),
56+
(2343, 'MMCCCXLIII'),
57+
(2499, 'MMCDXCIX'),
58+
(2574, 'MMDLXXIV'),
59+
(2646, 'MMDCXLVI'),
60+
(2723, 'MMDCCXXIII'),
61+
(2892, 'MMDCCCXCII'),
62+
(2975, 'MMCMLXXV'),
63+
(3051, 'MMMLI'),
64+
(3185, 'MMMCLXXXV'),
65+
(3250, 'MMMCCL'),
66+
(3313, 'MMMCCCXIII'),
67+
(3408, 'MMMCDVIII'),
68+
(3501, 'MMMDI'),
69+
(3610, 'MMMDCX'),
70+
(3743, 'MMMDCCXLIII'),
71+
(3844, 'MMMDCCCXLIV'),
72+
(3888, 'MMMDCCCLXXXVIII'),
73+
(3940, 'MMMCMXL'),
74+
(3999, 'MMMCMXCIX'),
75+
)
76+
77+
78+
def test_to_roman_known_values():
79+
"""
80+
to_roman should give known result with known input
81+
"""
82+
for integer, numeral in KNOWN_VALUES:
83+
result = to_roman(integer)
84+
assert numeral == result
85+
86+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
A quaratic function evaluator
5+
6+
used to demonstrate callable classes
7+
"""
8+
9+
class Quadratic:
10+
"""
11+
Class to evaluate quadratic equations
12+
13+
Each instance wil have a certain set of coefficients
14+
"""
15+
16+
def __init__(self, A, B, C):
17+
self.A = A
18+
self.B = B
19+
self.C = C
20+
21+
def __call__(self, x):
22+
return self.A * x**2 + self.B * x + self.C
23+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
3+
'''
4+
Decorators to convert all arguments passed to a function or method to
5+
unicode or str, including default arguments
6+
7+
From: http://axialcorps.com/2014/03/20/unicode-str/
8+
9+
'''
10+
11+
12+
import sys
13+
import functools
14+
import inspect
15+
16+
def _convert_arg(arg, from_, conv, enc):
17+
'''Safely convert unicode to string or string to unicode'''
18+
return getattr(arg, conv)(encoding=enc) if isinstance(arg, from_) else arg
19+
20+
def _wrap_convert(from_type, fn, encoding=None):
21+
'''Decorate a function converting all str arguments to unicode or
22+
vice-versa'''
23+
conv = 'decode' if from_type is str else 'encode'
24+
encoding = encoding or sys.getdefaultencoding()
25+
26+
# override string defaults using partial
27+
aspec, dflts = inspect.getargspec(fn), {}
28+
if aspec.defaults:
29+
for k,v in zip(aspec.args[-len(aspec.defaults):],aspec.defaults):
30+
dflts[k] = _convert_arg(v, from_type, conv, encoding)
31+
fn = functools.partial(fn, **dflts)
32+
33+
@functools.wraps(fn.func if isinstance(fn, functools.partial) else fn)
34+
def converted(*args, **kwargs):
35+
args = [_convert_arg(a, from_type, conv, encoding) for a in args]
36+
for k,v in kwargs.iteritems():
37+
kwargs[k] = _convert_arg(v, from_type, conv, encoding)
38+
return fn(*args, **kwargs)
39+
40+
return converted
41+
42+
def unicodify(fn=None, encoding=None):
43+
'''Convert all str arguments to unicode'''
44+
if fn is None:
45+
return functools.partial(unicodify, encoding=encoding)
46+
return _wrap_convert(str, fn, encoding=encoding)
47+
48+
def stringify(fn=None, encoding=None):
49+
'''Convert all unicode arguments to str'''
50+
if fn is None:
51+
return functools.partial(stringify, encoding=encoding)
52+
return _wrap_convert(unicode, fn, encoding=encoding)
53+
54+
__all__ = ['unicodify', 'stringify']
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import unittest
2+
3+
import calculator_functions as calc
4+
5+
6+
def setUpModule():
7+
print("running setup module")
8+
9+
10+
class TestCalculatorFunctions(unittest.TestCase):
11+
12+
def setUp(self):
13+
print("running setup")
14+
self.x = 2
15+
self.y = 3
16+
17+
def tearDown(self):
18+
print("running teardown")
19+
20+
def test_add(self):
21+
print("running test_add")
22+
self.assertEqual(calc.add(self.x, self.y), 5)
23+
24+
def test_add2(self):
25+
print("running test_add2")
26+
self.assertEqual(calc.add(7, 8), 15)
27+
28+
29+
# class TestCalculatorFunctions2(unittest.TestCase):
30+
31+
# def setUp(self):
32+
# self.x = 2
33+
# self.y = 3
34+
35+
# def test_add(self):
36+
# self.assertEqual(calc.subtract(self.y, self.x), 1)
37+
38+
if __name__ == "__main__":
39+
unittest.main()
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
test code for the object_canvas
5+
6+
Note: Testing image generation is hard. So for now, this mostly just
7+
tests that the rendering function runs.
8+
And during development, you can look at the resulting files.
9+
10+
One could store "properly" rendered results for future tests to
11+
check against.
12+
"""
13+
14+
# import os
15+
import pathlib
16+
import object_canvas as oc
17+
18+
SAVE_ALL=True # save all the temp files?
19+
20+
21+
def render_to_file(canvas, filename="test_image.png", save=False):
22+
"""
23+
utility to render a canvas to a file
24+
25+
:param filename: name of file to render to it will be put in a test_images dir.
26+
27+
:param remove=True: whether to remove the file after rendering.
28+
"""
29+
path = pathlib.Path("test_images")
30+
path.mkdir(exist_ok=True)
31+
path /= filename
32+
canvas.render(str(path))
33+
assert path.is_file()
34+
if not (SAVE_ALL or save):
35+
path.unlink()
36+
37+
38+
def test_init():
39+
canvas = oc.ObjectCanvas()
40+
41+
assert canvas
42+
43+
def test_backgound():
44+
canvas = oc.ObjectCanvas(background='blue')
45+
render_to_file(canvas, "blue_background.png")
46+
47+
def test_polyline():
48+
"""
49+
can we draw a polyline?
50+
"""
51+
canvas = oc.ObjectCanvas()
52+
points = ((10, 10), # this should be a triangle
53+
(10, 400),
54+
(400, 10),
55+
(10, 10),
56+
)
57+
58+
pl = oc.PolyLine(points)
59+
canvas.add_object(pl)
60+
render_to_file(canvas, "polyline.png")
61+
62+
63+
def test_circle():
64+
canvas = oc.ObjectCanvas()
65+
center = (100, 100)
66+
diameter = 75
67+
for line_width in range(1, 5):
68+
c = oc.Circle(center,
69+
diameter,
70+
line_color="red",
71+
fill_color="blue",
72+
line_width=line_width,
73+
)
74+
canvas.add_object(c)
75+
center = (center[0] + 50, center[0] + 50)
76+
render_to_file(canvas, "circle.png")
77+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/python
2+
3+
"""
4+
An exercise in playing with Exceptions.
5+
Make lots of try/except blocks for fun and profit.
6+
7+
Make sure to catch specifically the error you find, rather than all errors.
8+
"""
9+
10+
from except_test import fun, more_fun, last_fun
11+
12+
13+
# Figure out what the exception is, catch it and while still
14+
# in that catch block, try again with the second item in the list
15+
first_try = ['spam', 'cheese', 'mr death']
16+
17+
joke = fun(first_try[0])
18+
19+
# Here is a try/except block. Add an else that prints not_joke
20+
try:
21+
not_joke = fun(first_try[2])
22+
except SyntaxError:
23+
print('Run Away!')
24+
25+
# What did that do? You can think of else in this context, as well as in
26+
# loops as meaning: "else if nothing went wrong"
27+
# (no breaks in loops, no exceptions in try blocks)
28+
29+
# Figure out what the exception is, catch it and in that same block
30+
#
31+
# try calling the more_fun function with the 2nd language in the list,
32+
# again assigning it to more_joke.
33+
#
34+
# If there are no exceptions, call the more_fun function with the last
35+
# language in the list
36+
37+
# Finally, while still in the try/except block and regardless of whether
38+
# there were any exceptions, call the function last_fun with no
39+
# parameters. (pun intended)
40+
41+
langs = ['java', 'c', 'python']
42+
43+
more_joke = more_fun(langs[0])
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Name: Nickname, languages
2+
Swift, Taylor: python, java, perl
3+
Swift, Samuel: Sam
4+
Brooks, Garth: fortran, java, matlab, bash
5+
Buble, Michael: python, powershell
6+
Stefani, Gwen: c#, javascript, python, typescript
7+
Gaye, Marvin: c++, java
8+
Jagger, Michael: Mick, shell, python
9+
Lennon, John:
10+
Nelson, Willy: python, java
11+
McCartney, Paul: nothing
12+
Dylan, Bob: java, python, ruby
13+
Springsteen, Bruce: c++, python
14+
Jackson, Michael: java, c#, python
15+
Berry, Charles: Chuck c++, matlab,
16+
Wonder, Steven: Stevie, c, perl, java, erlang, python
17+
Nicks, Stevie: java, perl, c#, c++, python
18+
Turner, Tina: bash, python
19+
Plant, Robert: Bob, bash, python, ansible
20+
Townshend, Peter: Pete, c#, powershell, python, sql
21+
Moon, Keith: python, r, visualbasic
22+
Mitchell, Joni: php, mysql, python
23+
Ramone, John: Johnny, rex, db
24+
King, Carol: r
25+
Waters, Muddy: perl, python
26+
Star, Richard: Ringo, shell, python, vb
27+
Smith, Patricia: Patti, python
28+
Morrison, Jim: fortran, perl, sql, python
29+
Marley, Robert: Bob, c, c++, lisp
30+
Simon, Paul: bash, python, sql
31+
Charles, Ray: Chuck, java, python

0 commit comments

Comments
 (0)