diff --git a/source/examples/async/async_executor.py b/source/examples/async/async_executor.py index 1f18f13..1acd339 100644 --- a/source/examples/async/async_executor.py +++ b/source/examples/async/async_executor.py @@ -6,7 +6,6 @@ import asyncio import time -import datetime import random @@ -20,6 +19,7 @@ async def small_task(num): # pause for a random amount of time between 0 and 2 seconds await asyncio.sleep(random.random() * 2) + async def slow_task(): while True: # keep going forever print("running the slow task- blocking!") @@ -27,9 +27,7 @@ async def slow_task(): # result = slow_function(random.random() * 8 + 2) # uncomment to put it on a different thread: # result = slow_function(random.random() * 8 + 2) - result = await loop.run_in_executor(None, - slow_function, - random.random() * 8 + 2) + result = await loop.run_in_executor(None, slow_function, random.random() * 8 + 2) print("slow function done: result", result) # await asyncio.sleep(0.0) # to release the loop diff --git a/source/examples/async/client_test.py b/source/examples/async/client_test.py index 836d8a3..72defe8 100644 --- a/source/examples/async/client_test.py +++ b/source/examples/async/client_test.py @@ -9,6 +9,7 @@ import asyncio import aiohttp + async def get_events(): async with aiohttp.ClientSession() as session: print("created a session") diff --git a/source/examples/async/gather.py b/source/examples/async/gather.py index 56c5628..a47539c 100644 --- a/source/examples/async/gather.py +++ b/source/examples/async/gather.py @@ -20,6 +20,7 @@ async def factorial(name, number): f *= i print("Task %s: factorial(%s) = %s" % (name, number, f)) + async def run(): await asyncio.gather( factorial("A", 2), @@ -27,4 +28,5 @@ async def run(): factorial("C", 4), ) + asyncio.run(run()) diff --git a/source/examples/async/get_news.py b/source/examples/async/get_news.py index ee9ffd2..3d300ab 100644 --- a/source/examples/async/get_news.py +++ b/source/examples/async/get_news.py @@ -1,5 +1,3 @@ - - NEWS_API_KEY = 1fabc23bb9bc485ca59b3966cbd6ea26 url = https://newsapi.org/ diff --git a/source/examples/async/get_news_sync.py b/source/examples/async/get_news_sync.py index 82a7b13..596cc5d 100755 --- a/source/examples/async/get_news_sync.py +++ b/source/examples/async/get_news_sync.py @@ -15,6 +15,7 @@ import time import requests + WORD = "trump" NEWS_API_KEY = "84d0483394c44f288965d7b366e54a74" diff --git a/source/examples/async/just_coroutines.py b/source/examples/async/just_coroutines.py index c70ab5f..bf75812 100755 --- a/source/examples/async/just_coroutines.py +++ b/source/examples/async/just_coroutines.py @@ -9,10 +9,12 @@ Hopefully, this will help us better understand how they work. """ + async def corout(): print("running corout") return "something returned" + # Note that the returned value gets tacked on to the StopIteration async def corout2(): @@ -34,6 +36,7 @@ async def corout2(): """applying the coroutine decorator makes a generator a coroutine, and thus an awaitable object. """ + @coroutine def do_nothing(): """ @@ -51,6 +54,7 @@ def do_nothing(): # """But we can now we can make another coroutine that awaits on the first one: # """ + async def do_a_few_things(num=3): # a loop for multiple things for i in range(num): @@ -113,6 +117,7 @@ async def do_a_few_things(num=3): print("\n\n*********\n\n") + @coroutine def nothing(): """ @@ -122,6 +127,7 @@ def nothing(): yield "from nothing" return ("returned from nothing") + @coroutine def count(num): """ @@ -132,6 +138,7 @@ def count(num): for i in range(num): yield f"count: {i}" + async def do_a_few_things(num=3, name="no_name"): # a loop for multiple things for i in range(num): @@ -139,6 +146,7 @@ async def do_a_few_things(num=3, name="no_name"): from_await = await nothing() print("value returned from await:", from_await) + # create it: daft = do_a_few_things(5, "first one") @@ -187,6 +195,7 @@ def count(num): yield f"count: {i}" return "returned from count" + # and this one loops a bit more, calling count async def do_a_few_things(num=3, name="no_name"): # a loop for multiple things @@ -195,6 +204,7 @@ async def do_a_few_things(num=3, name="no_name"): from_await = await count(i + 2) print("value returned from await:", from_await) + # """ # We're going to create a little class to make a task loop # """ @@ -278,7 +288,7 @@ def sleep(secs=0): async def fib(n): """ - Classic fibbonacci number, but with a delay + Classic fibonacci number, but with a delay """ if n == 0: return 0 @@ -333,6 +343,7 @@ def run_all(self): results.append(si.args[0]) return results + # """ # Now let's try it out by putting a creating a few tasks and putting them on the loop. # """ diff --git a/source/examples/classes/simple_classes.py b/source/examples/classes/simple_classes.py index 4f99606..96cdc34 100644 --- a/source/examples/classes/simple_classes.py +++ b/source/examples/classes/simple_classes.py @@ -30,6 +30,7 @@ def __init__(self, x, y): self.x = x self.y = y + p2 = Point2(4, 5) print(p2.size) print(p2.color) @@ -49,8 +50,8 @@ def get_color(self): def get_size(self): return self.size -class Rect: +class Rect: def __init__(self, w, h): self.w = w self.h = h @@ -93,6 +94,7 @@ def grow(self, factor=2): """grows the area by factor...""" self.diameter = self.diameter * math.sqrt(2) + nc = NewCircle print(nc.color) diff --git a/source/examples/closures_currying/play_with_scope.py b/source/examples/closures_currying/play_with_scope.py index a71292b..0b45254 100644 --- a/source/examples/closures_currying/play_with_scope.py +++ b/source/examples/closures_currying/play_with_scope.py @@ -2,11 +2,13 @@ some example code to play with scope """ + def start_at(x): def increment_by(y): return x + y return increment_by + closure_1 = start_at(3) closure_2 = start_at(5) closure_1(2) @@ -15,6 +17,7 @@ def increment_by(y): def make_um_counter(): series = [] + def um_counter(new_word): series.append(new_word) # free variable count = 0 diff --git a/source/examples/context_managers/looking_glass.py b/source/examples/context_managers/looking_glass.py index d7a2a63..e5be40f 100644 --- a/source/examples/context_managers/looking_glass.py +++ b/source/examples/context_managers/looking_glass.py @@ -5,6 +5,7 @@ import sys import contextlib + class LookingGlass: def __enter__(self): @@ -22,11 +23,11 @@ def __exit__(self, exc_type, exc_value, traceback): return True - @contextlib.contextmanager def looking_glass(): msg = '' original_write = sys.stdout.write + def reverse_write(text): original_write(text[::-1]) diff --git a/source/examples/context_managers/raising_an_assert.py b/source/examples/context_managers/raising_an_assert.py index 0ea86ea..2d8989a 100644 --- a/source/examples/context_managers/raising_an_assert.py +++ b/source/examples/context_managers/raising_an_assert.py @@ -9,5 +9,5 @@ def test_raise_assertion(): raise AssertionError("this was done with a direct raise") -def test_trasditional_assert(): +def test_traditional_assert(): assert False, "this was done with a forced assert" diff --git a/source/examples/decorators/play_with_imports.py b/source/examples/decorators/play_with_imports.py index 0c84140..0891a2d 100644 --- a/source/examples/decorators/play_with_imports.py +++ b/source/examples/decorators/play_with_imports.py @@ -3,6 +3,7 @@ def my_decorator(func): print('in my_decorator for: ', func.__name__) + def inner(): print('in inner function') func() @@ -14,10 +15,12 @@ def inner(): def first_func(): print('running first_func') + @my_decorator def other_func(): print('running other_func') + print('imports and loading done') if __name__ == '__main__': diff --git a/source/examples/iterators_generators/my_for.py b/source/examples/iterators_generators/my_for.py index ae6faae..7f31294 100644 --- a/source/examples/iterators_generators/my_for.py +++ b/source/examples/iterators_generators/my_for.py @@ -7,7 +7,7 @@ """ -l = [1,2,3,4,5,] +x = [1, 2, 3, 4, 5,] def my_for(an_iterable, func): @@ -16,13 +16,13 @@ def my_for(an_iterable, func): func() will be called with each item in an_iterable - :param an_iterable: anything that satisfies the interation protocol + :param an_iterable: anything that satisfies the iteration protocol :param func: a callable -- it will be called, passing in each item in an_iterable. """ - # equiv of "for i in l:" + # equiv of "for i in x:" iterator = iter(an_iterable) while True: try: @@ -37,9 +37,9 @@ def my_for(an_iterable, func): def print_func(x): print(x) - l = [1,2,3,4,5,] - my_for(l, print_func) + x = [1, 2, 3, 4, 5,] + my_for(x, print_func) - t = ('a','b','c','d') + t = ('a', 'b', 'c', 'd') my_for(t, print_func) diff --git a/source/examples/iterators_generators/yield_example.py b/source/examples/iterators_generators/yield_example.py index adf9448..3bb6ab4 100644 --- a/source/examples/iterators_generators/yield_example.py +++ b/source/examples/iterators_generators/yield_example.py @@ -6,6 +6,7 @@ def counter(): print('counter: yield', i) yield i + def y_range(start, stop, step=1): print("at the start") i = start @@ -16,6 +17,7 @@ def y_range(start, stop, step=1): i += step print("at end of func") + def test(): yield 4 yield 45 diff --git a/source/examples/metaprogramming/singleton.py b/source/examples/metaprogramming/singleton.py index 890082b..0fc6c31 100644 --- a/source/examples/metaprogramming/singleton.py +++ b/source/examples/metaprogramming/singleton.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 """ -example of using __metaclass__ to impliment the singleton pattern +example of using __metaclass__ to implement the singleton pattern """ @@ -17,6 +17,7 @@ def __call__(cls, *args, **kwargs): class MyClass(metaclass=Singleton): pass + object1 = MyClass() object2 = MyClass() diff --git a/source/examples/modules/my_package/a_module.py b/source/examples/modules/my_package/a_module.py index 8f5f98e..3d9edb0 100644 --- a/source/examples/modules/my_package/a_module.py +++ b/source/examples/modules/my_package/a_module.py @@ -1,5 +1,6 @@ name3 = "Mary" name4 = "Jane" + def a_function(): print("a_function has been called") diff --git a/source/examples/multiple_inheritance/mro.py b/source/examples/multiple_inheritance/mro.py index 760d70a..d514baf 100644 --- a/source/examples/multiple_inheritance/mro.py +++ b/source/examples/multiple_inheritance/mro.py @@ -35,6 +35,7 @@ def my_method(self): print("called D") super(D, self).my_method() + print(D.mro()) d = D() print(d.my_method()) diff --git a/source/examples/multiple_inheritance/object_canvas.py b/source/examples/multiple_inheritance/object_canvas.py index 5d94827..2b7824b 100644 --- a/source/examples/multiple_inheritance/object_canvas.py +++ b/source/examples/multiple_inheritance/object_canvas.py @@ -7,9 +7,9 @@ wxPython desktop GUI library FloatCanvas is a system for handling zoomable and scalable graphics in -a object-persistant way. That is, graphic objects like circles and +a object-persistent way. That is, graphic objects like circles and rectangles and what not can be created ahead of time, and then the Canvas -can render them accoding to the current zoom level and pan position, etc. +can render them according to the current zoom level and pan position, etc. This lets the user think about their graphics object should look like, and not have to worry about exactly how to draw them -- or their pixel @@ -54,7 +54,7 @@ def __init__(self, self.background = background def add_object(self, draw_object, position="top"): - #fixme: maybe overload the inplace addition operator? + # fixme: maybe overload the inplace addition operator? """ Add a new object to the canvas. diff --git a/source/examples/multiple_inheritance/super_test.py b/source/examples/multiple_inheritance/super_test.py index 0a73e93..c5bed44 100644 --- a/source/examples/multiple_inheritance/super_test.py +++ b/source/examples/multiple_inheritance/super_test.py @@ -1,27 +1,28 @@ #!/usr/bin/env python3 """ -Some example code demonstrating some super() behaviour +Some example code demonstrating some super() behavior """ + # Define a multiple inheritance scheme: class A(): def __init__(self): print("in A __init__") print("self's class is:", self.__class__) - s = super().__init__() + super().__init__() class B(): def __init__(self): print("in B.__init__") - s = super().__init__() + super().__init__() class C(): def __init__(self): print("in C.__init__") - s = super().__init__() + super().__init__() class D(C, B, A): @@ -29,6 +30,7 @@ def __init__(self): print("self's class is:", self.__class__) super().__init__() + # print our D's method resoluton order # Is it what you expect? print("\nD's mro:") @@ -108,16 +110,19 @@ class A(): def this(self): print("in A.this") + class B(): def this(self): print("in B.this") + class C(A,B): def this(self): print("in C.this") A.this(self) B.this(self) + print("\nRunning without super()") print("Creating a C instance without super() -- and calling it's this method:") c = C() @@ -129,14 +134,17 @@ def this(self): print("\n using super() in C, but not everywhere...") + class A(): def this(self): print("in A.this") + class B(A): def this(self): print("in B.this") + class C(B): def this(self): print("in C.this") @@ -162,20 +170,25 @@ def this(self): print("using super everywhere:") + class Base(): def this(self): pass # just so there is a base that has the method + class A(Base): def this(self): print("in A.this") super().this() + class B(Base): def this(self): print("in B.this") super().this() -class C(A,B): + + +class C(A, B): def this(self): print("in C.this") super().this() @@ -192,27 +205,32 @@ def this(self): # But if you don't want both called -- better to just be Explicit, rather than use super(): + class Base(): def this(self): pass # just so there is a base that has the method + class A(Base): def this(self): print("in A.this") super().this() + class B(Base): def this(self): print("in B.this") super().this() -class C(A,B): + +class C(A, B): def this(self): print("in C.this") A.this(self) -print("\nIf you want total control of what methods get called --don't use super" - "and be explicit") + +print("\nIf you want total control of what methods get called --don't use " + "super and be explicit") c = C() c.this() @@ -228,22 +246,26 @@ def this(self): print("\nBut if we print the class of each instance when this() is called") + class Base(): def this(self): pass # just so there is a base that has the method + class A(Base): def this(self): print("in A.this") print("self's class in A's this method:", self.__class__) super().this() + class B(Base): def this(self): print("in B.this") super().this() -class C(A,B): + +class C(A, B): def this(self): print("in C.this") A.this(self) diff --git a/source/examples/multiple_inheritance/test_object_canvas.py b/source/examples/multiple_inheritance/test_object_canvas.py index 1b6967d..4e6b6cb 100644 --- a/source/examples/multiple_inheritance/test_object_canvas.py +++ b/source/examples/multiple_inheritance/test_object_canvas.py @@ -15,7 +15,8 @@ import pathlib import object_canvas as oc -SAVE_ALL=True # save all the temp files? + +SAVE_ALL = True # save all the temp files? def render_to_file(canvas, filename="test_image.png", save=False): @@ -40,10 +41,12 @@ def test_init(): assert canvas + def test_backgound(): canvas = oc.ObjectCanvas(background='blue') render_to_file(canvas, "blue_background.png") + def test_polyline(): """ can we draw a polyline? diff --git a/source/examples/nosql/address_book_model.py b/source/examples/nosql/address_book_model.py index 2b9a5cd..99449d2 100644 --- a/source/examples/nosql/address_book_model.py +++ b/source/examples/nosql/address_book_model.py @@ -3,7 +3,7 @@ """ sample data for NOSQL examples -This version has a not completely-trival data model +This version has a not completely-trivial data model """ @@ -239,7 +239,6 @@ def create_sample(): address=python_cert_address ) - address_book = AddressBook() address_book.add_person(chris) diff --git a/source/examples/nosql/address_book_mongo.py b/source/examples/nosql/address_book_mongo.py index a6073ab..377c85c 100644 --- a/source/examples/nosql/address_book_mongo.py +++ b/source/examples/nosql/address_book_mongo.py @@ -90,7 +90,6 @@ def __init__(self, city='', state='', zip_code='', -# **kwargs ): """ Initialize an address @@ -115,7 +114,7 @@ class Household(PersistObject): """ def __init__(self, - name = '', + name='', people=(), address=None, phone='', @@ -188,7 +187,7 @@ def __init__(self, client = MongoClient('localhost', 27017) db = client.address_book if fresh: - ## clean out old one + # clean out old one db.people.drop() db.businesses.drop() db.households.drop() @@ -340,7 +339,6 @@ def create_sample(): address=python_cert_address ) - address_book = AddressBook() address_book.add_person(chris) diff --git a/source/examples/nosql/address_book_zodb.py b/source/examples/nosql/address_book_zodb.py index f49e45e..cab80f6 100644 --- a/source/examples/nosql/address_book_zodb.py +++ b/source/examples/nosql/address_book_zodb.py @@ -10,6 +10,7 @@ import persistent # from ZODB from persistent.list import PersistentList + class Person(persistent.Persistent): """ class to represent an individual person @@ -210,25 +211,24 @@ def create_sample(): the_barkers = Household(name="The Barkers", people=(chris, donna, emma), - address = barker_address) - + address=barker_address) - joseph = Person(last_name = 'Sheedy', + joseph = Person(last_name='Sheedy', first_name='Joseph', cell_phone='(234) 555-8910', - email = 'js@some_thing.com', + email='js@some_thing.com', ) - cris = Person(last_name = 'Ewing', + cris = Person(last_name='Ewing', first_name='Cris', cell_phone='(345) 555-6789', - email = 'cris@a_fake_domain.com', + email='cris@a_fake_domain.com', ) - fulvio = Person(last_name = 'Casali', - first_name= 'Fulvio', + fulvio = Person(last_name='Casali', + first_name='Fulvio', cell_phone='(345) 555-1234', - email = 'fulvio@a_fake_domain.com', + email='fulvio@a_fake_domain.com', ) fred = Person(first_name="Fred", @@ -244,17 +244,16 @@ def create_sample(): zip_code='98105', ) - python_cert = Business(name = 'Python Certification Program', + python_cert = Business(name='Python Certification Program', people=(chris, joseph, cris, fulvio), - address = Address('UW Professional and Continuing Education', - line_2='4333 Brooklyn Ave. NE', - city='Seattle', - state='WA', - zip_code='98105', - ) + address=Address('UW Professional and Continuing Education', + line_2='4333 Brooklyn Ave. NE', + city='Seattle', + state='WA', + zip_code='98105', + ) ) - root.address_book = AddressBook() root.address_book.people.append(chris) diff --git a/source/examples/nosql/mongo_test.py b/source/examples/nosql/mongo_test.py index 114e59a..66037d6 100644 --- a/source/examples/nosql/mongo_test.py +++ b/source/examples/nosql/mongo_test.py @@ -15,11 +15,11 @@ collection = db.test_collection -chris = {'last_name':'Barker', - 'first_name':'Chris', - 'middle_name':'H', - 'cell_phone':'(123) 555-7890', - 'email':'PythonCHB@gmail.com', +chris = {'last_name': 'Barker', + 'first_name': 'Chris', + 'middle_name': 'H', + 'cell_phone': '(123) 555-7890', + 'email': 'PythonCHB@gmail.com', } collection.insert(chris) diff --git a/source/examples/nosql/neo4j/neo4japp.py b/source/examples/nosql/neo4j/neo4japp.py index 8385a1c..cdc4c00 100644 --- a/source/examples/nosql/neo4j/neo4japp.py +++ b/source/examples/nosql/neo4j/neo4japp.py @@ -59,10 +59,10 @@ def clear_all(driver): NOTE: The Docs say about this: - "This query isn’t for deleting large amounts of data, but is nice + "This query isn't for deleting large amounts of data, but is nice when playing around with small example data sets." - I suppose if you have a really big datbase, you should just throw + I suppose if you have a really big database, you should just throw it away and make a new one. """ logger.info("Running clear_all") @@ -98,6 +98,7 @@ def test1(driver): logger.info('Note - this needs some exception handling!') + def test2(driver): """ Add a few people, some with a little more info @@ -119,7 +120,7 @@ def test2(driver): # a record is one or more nodes, # in an ordered mapping, you can get it by name or index: print(rec[0]) - print(rec['n']) # the n from the query above + print(rec['n']) # the n from the query above node = rec[0] # each node as a unique id: @@ -163,10 +164,10 @@ def test3(driver): print("relationship:", rel) print("relationship's type:", rel.type) print("it connects nodes:", rel.start, "and", rel.end) - # for name, node in rec.items(): - # print("got node:", name) - # print(type(node)) - # print(node['first_name'], node['last_name']) + # for name, node in rec.items(): + # print("got node:", name) + # print(type(node)) + # print(node['first_name'], node['last_name']) print("can we find Bob's friend?") result = session.run("MATCH (bob {first_name:'Bob', last_name:'Jones'})" diff --git a/source/examples/nosql/test_address_book_model.py b/source/examples/nosql/test_address_book_model.py index d621354..4e07156 100755 --- a/source/examples/nosql/test_address_book_model.py +++ b/source/examples/nosql/test_address_book_model.py @@ -42,7 +42,7 @@ def test_zip_search(): def test_state_search(): locations = a_book.find_state('WA') - names = [l.name for l in locations] + names = [x.name for x in locations] assert "The Barkers" in names assert "Python Certification Program" in names diff --git a/source/examples/oo_intro/oo_intro.py b/source/examples/oo_intro/oo_intro.py index 66601e5..e360182 100644 --- a/source/examples/oo_intro/oo_intro.py +++ b/source/examples/oo_intro/oo_intro.py @@ -1,5 +1,3 @@ -import math -import operator from uuid import uuid4 @@ -75,7 +73,6 @@ def get_paged_rows(self, sort_field, page): report.add_row(Row("johny", "jakes", "WA")) report.add_row(Row("derek", "wright", "WA")) - def run_report(sort_field): print(f"... PAGED REPORT SORTED BY: '{sort_field}'...") page = 1 @@ -97,7 +94,6 @@ def run_report(sort_field): page += 1 - run_report("fname") print(f"\n\nRemoving student: {report.rows[1].fname} [{report.rows[1].row_id}]... \n\n") diff --git a/source/examples/packaging/capitalize.zip b/source/examples/packaging/capitalize.zip index 265db44..4b88325 100644 Binary files a/source/examples/packaging/capitalize.zip and b/source/examples/packaging/capitalize.zip differ diff --git a/source/examples/packaging/capitalize/cap_script.py b/source/examples/packaging/capitalize/cap_script.py index f794964..5121a7f 100755 --- a/source/examples/packaging/capitalize/cap_script.py +++ b/source/examples/packaging/capitalize/cap_script.py @@ -4,9 +4,6 @@ A really simple script just to demonstrate packaging """ -import sys -import os -import capital_mod import main diff --git a/source/examples/packaging/capitalize/capital_mod.py b/source/examples/packaging/capitalize/capital_mod.py index 47536f0..88a6ed8 100644 --- a/source/examples/packaging/capitalize/capital_mod.py +++ b/source/examples/packaging/capitalize/capital_mod.py @@ -17,7 +17,7 @@ def load_special_words(data_file_name, words=None): the # charactor is a comment -- everything after it will be ignored """ - words = set() if words is None else words + words = set() if words is None else words with open(data_file_name) as data_file: for line in data_file: word = line.split('#')[0].strip() @@ -25,15 +25,18 @@ def load_special_words(data_file_name, words=None): words.add(word.lower()) return words + def get_datafile_name(): """ return the default data file that comes with the package """ return Path(__file__).parent / "cap_data.txt" -## load up the special words on import + +# load up the special words on import special_words = load_special_words(get_datafile_name()) + def capitalize_line(instr, special_words=special_words): """ capitalizes the input string @@ -42,7 +45,7 @@ def capitalize_line(instr, special_words=special_words): :type instr: string :param special_words: set of words that should not be capitalized - defaults to the words in the encosed data file + defaults to the words in the enclosed data file :type special_words: set of str :returns: a capitalized version of instr diff --git a/source/examples/packaging/capitalize/main.py b/source/examples/packaging/capitalize/main.py index e677c74..fdc881d 100644 --- a/source/examples/packaging/capitalize/main.py +++ b/source/examples/packaging/capitalize/main.py @@ -4,7 +4,8 @@ here is a simple main() module -- to demonstrate setuptools entrypoints """ -import sys, os +import os +import sys import capital_mod help = """ @@ -15,6 +16,7 @@ capitalizes (title case) each line in a passed in file """ + def main(): """ startup function for running a capitalize as a script diff --git a/source/examples/packaging/capitalize/test_capital_mod.py b/source/examples/packaging/capitalize/test_capital_mod.py index 63d3343..4277a8c 100644 --- a/source/examples/packaging/capitalize/test_capital_mod.py +++ b/source/examples/packaging/capitalize/test_capital_mod.py @@ -13,6 +13,7 @@ import capital_mod + # fixture that creates and removes a file with special words in it @pytest.fixture(scope='module') def special_words_path(): @@ -75,7 +76,8 @@ def test_capitalize_line(): expected = "This is a Line to Capitalize" result = capital_mod.capitalize_line(line, special_words=special) - assert result == expected + assert result == expected + def test_capitalize(test_file_path): """ test an actual file """ diff --git a/source/examples/persistence/add_book_data.py b/source/examples/persistence/add_book_data.py index bdd05dc..6e254bc 100644 --- a/source/examples/persistence/add_book_data.py +++ b/source/examples/persistence/add_book_data.py @@ -7,42 +7,50 @@ - can be saved with pickle, JSON, xml... """ -AddressBook = [ {'first_name': "Chris", - 'last_name': "Barker", - 'address' : {'line_1':"835 NE 33rd St", - 'line_2' : "", - 'city' : "Seattle", - 'state': "WA", - 'zip': "96543"}, - 'email' : "PythonCHB@gmail.com", - 'home_phone' : "206-555-1234", - 'office_phone' : "123-456-7890", - 'cell_phone' : "234-567-8901", - }, - - {'first_name': "Fred", - 'last_name': "Jones", - 'address' : {'line_1':"123 SE 13th St", - 'line_2' : "Apt. 43", - 'city' : "Tacoma", - 'state': "WA", - 'zip': "93465"}, - 'email' : "FredJones@some_company.com", - 'home_phone' : "510-555-1234", - 'office_phone' : "564-466-7990", - 'cell_phone' : "403-561-8911", - }, - - {'first_name': "Nancy", - 'last_name': "Wilson", - 'address' : {'line_1':"8654 Walnut St", - 'line_2' : "Suite 567", - 'city' : "Pasadena", - 'state': "CA", - 'zip': "12345"}, - 'email' : "Wilson.Nancy@gmail.com", - 'home_phone' : "423-321-9876", - 'office_phone' : "123-765-9877", - 'cell_phone' : "432-567-8466", - }, - ] +AddressBook = [ + { + "first_name": "Chris", + "last_name": "Barker", + "address": { + "line_1": "835 NE 33rd St", + "line_2": "", + "city": "Seattle", + "state": "WA", + "zip": "96543", + }, + "email": "PythonCHB@gmail.com", + "home_phone": "206-555-1234", + "office_phone": "123-456-7890", + "cell_phone": "234-567-8901", + }, + { + "first_name": "Fred", + "last_name": "Jones", + "address": { + "line_1": "123 SE 13th St", + "line_2": "Apt. 43", + "city": "Tacoma", + "state": "WA", + "zip": "93465", + }, + "email": "FredJones@some_company.com", + "home_phone": "510-555-1234", + "office_phone": "564-466-7990", + "cell_phone": "403-561-8911", + }, + { + "first_name": "Nancy", + "last_name": "Wilson", + "address": { + "line_1": "8654 Walnut St", + "line_2": "Suite 567", + "city": "Pasadena", + "state": "CA", + "zip": "12345", + }, + "email": "Wilson.Nancy@gmail.com", + "home_phone": "423-321-9876", + "office_phone": "123-765-9877", + "cell_phone": "432-567-8466", + }, +] diff --git a/source/examples/persistence/add_book_data_flat.py b/source/examples/persistence/add_book_data_flat.py index 7520907..23a308a 100644 --- a/source/examples/persistence/add_book_data_flat.py +++ b/source/examples/persistence/add_book_data_flat.py @@ -5,42 +5,44 @@ this version is flat for saving in CSV, ini, etc. """ -AddressBook = [ {'first_name': "Chris", - 'last_name': "Barker", - 'address_line_1':"835 NE 33rd St", - 'address_line_2' : "", - 'address_city' : "Seattle", - 'address_state': "WA", - 'address_zip': "96543", - 'email' : "PythonCHB@gmail.com", - 'home_phone' : "206-555-1234", - 'office_phone' : "123-456-7890", - 'cell_phone' : "234-567-8901", - }, - - {'first_name': "Fred", - 'last_name': "Jones", - 'address_line_1':"123 SE 13th St", - 'address_line_2' : "Apt. 43", - 'address_city' : "Tacoma", - 'address_state': "WA", - 'address_zip': "93465", - 'email' : "FredJones@some_company.com", - 'home_phone' : "510-555-1234", - 'office_phone' : "564-466-7990", - 'cell_phone' : "403-561-8911", - }, - - {'first_name': "Nancy", - 'last_name': "Wilson", - 'address_line_1':"8654 Walnut St", - 'address_line_2' : "Suite 567", - 'address_city' : "Pasadena", - 'address_state': "CA", - 'address_zip': "12345", - 'email' : "Wilson.Nancy@gmail.com", - 'home_phone' : "423-321-9876", - 'office_phone' : "123-765-9877", - 'cell_phone' : "432-567-8466", - }, - ] +AddressBook = [ + { + "first_name": "Chris", + "last_name": "Barker", + "address_line_1": "835 NE 33rd St", + "address_line_2": "", + "address_city": "Seattle", + "address_state": "WA", + "address_zip": "96543", + "email": "PythonCHB@gmail.com", + "home_phone": "206-555-1234", + "office_phone": "123-456-7890", + "cell_phone": "234-567-8901", + }, + { + "first_name": "Fred", + "last_name": "Jones", + "address_line_1": "123 SE 13th St", + "address_line_2": "Apt. 43", + "address_city": "Tacoma", + "address_state": "WA", + "address_zip": "93465", + "email": "FredJones@some_company.com", + "home_phone": "510-555-1234", + "office_phone": "564-466-7990", + "cell_phone": "403-561-8911", + }, + { + "first_name": "Nancy", + "last_name": "Wilson", + "address_line_1": "8654 Walnut St", + "address_line_2": "Suite 567", + "address_city": "Pasadena", + "address_state": "CA", + "address_zip": "12345", + "email": "Wilson.Nancy@gmail.com", + "home_phone": "423-321-9876", + "office_phone": "123-765-9877", + "cell_phone": "432-567-8466", + }, +] diff --git a/source/examples/profiling/timer/timer_test.py b/source/examples/profiling/timer/timer_test.py index 193c89c..b929112 100644 --- a/source/examples/profiling/timer/timer_test.py +++ b/source/examples/profiling/timer/timer_test.py @@ -22,5 +22,6 @@ def expensive_function(): def less_expensive_function(): time.sleep(.02) + expensive_function() less_expensive_function() diff --git a/source/examples/properties/properties_example.py b/source/examples/properties/properties_example.py index 776d57f..f77abdb 100644 --- a/source/examples/properties/properties_example.py +++ b/source/examples/properties/properties_example.py @@ -11,18 +11,22 @@ class C: def __init__(self): self._x = None + @property def x(self): print("in getter") return self._x + @x.setter def x(self, value): print("in setter", value) self._x = value + @x.deleter def x(self): del self._x + if __name__ == "__main__": c = C() c.x = 5 diff --git a/source/examples/quadratic/quadratic.py b/source/examples/quadratic/quadratic.py index ade372c..84b49a7 100644 --- a/source/examples/quadratic/quadratic.py +++ b/source/examples/quadratic/quadratic.py @@ -1,11 +1,12 @@ #!/usr/bin/env python3 """ -A quaratic function evaluator +A quadratic function evaluator used to demonstrate callable classes """ + class Quadratic: """ Class to evaluate quadratic equations diff --git a/source/examples/quadratic/test_quadratic.py b/source/examples/quadratic/test_quadratic.py index fdf99d2..5ff1884 100644 --- a/source/examples/quadratic/test_quadratic.py +++ b/source/examples/quadratic/test_quadratic.py @@ -12,18 +12,18 @@ def test_init(): - q = Quadratic(1,2,3) + Quadratic(1, 2, 3) def test_evaluate(): - q = Quadratic(1,2,3) + q = Quadratic(1, 2, 3) assert q(3) == 9 + 6 + 3 assert q(0) == 3 def test_evaluate2(): - q = Quadratic(2,1,-3) + q = Quadratic(2, 1, -3) assert q(0) == -3 assert q(1) == 2 + 1 - 3 @@ -31,6 +31,5 @@ def test_evaluate2(): def test_bad_input(): - with pytest.raises(TypeError): - q = Quadratic(2,3) + Quadratic(2, 3) diff --git a/source/examples/test_driven_development/roman.py b/source/examples/test_driven_development/roman.py index 4898160..e84a168 100644 --- a/source/examples/test_driven_development/roman.py +++ b/source/examples/test_driven_development/roman.py @@ -8,63 +8,64 @@ tests are expected to be able to be run with the pytest system """ -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman1.py b/source/examples/test_driven_development/roman1.py index 95f36f8..dd4d880 100644 --- a/source/examples/test_driven_development/roman1.py +++ b/source/examples/test_driven_development/roman1.py @@ -10,69 +10,69 @@ def to_roman(n): - '''convert an integer to Roman numeral''' - pass + '''convert an integer to Roman numeral''' + pass -## Tests for roman numeral conversion - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman10.py b/source/examples/test_driven_development/roman10.py index 1474f89..cda00c8 100644 --- a/source/examples/test_driven_development/roman10.py +++ b/source/examples/test_driven_development/roman10.py @@ -10,19 +10,21 @@ import pytest -roman_numeral_map = (('M', 1000), - ('CM', 900), - ('D', 500), - ('CD', 400), - ('C', 100), - ('XC', 90), - ('L', 50), - ('XL', 40), - ('X', 10), - ('IX', 9), - ('V', 5), - ('IV', 4), - ('I', 1)) +roman_numeral_map = ( + ('M', 1000), + ('CM', 900), + ('D', 500), + ('CD', 400), + ('C', 100), + ('XC', 90), + ('L', 50), + ('XL', 40), + ('X', 10), + ('IX', 9), + ('V', 5), + ('IV', 4), + ('I', 1), +) def to_roman(n): @@ -53,67 +55,65 @@ def from_roman(s): return result -##################################### -# Tests for roman numeral conversion -##################################### - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman11.py b/source/examples/test_driven_development/roman11.py index 6aef523..4afff72 100644 --- a/source/examples/test_driven_development/roman11.py +++ b/source/examples/test_driven_development/roman11.py @@ -10,19 +10,21 @@ import pytest -roman_numeral_map = (('M', 1000), - ('CM', 900), - ('D', 500), - ('CD', 400), - ('C', 100), - ('XC', 90), - ('L', 50), - ('XL', 40), - ('X', 10), - ('IX', 9), - ('V', 5), - ('IV', 4), - ('I', 1)) +roman_numeral_map = ( + ('M', 1000), + ('CM', 900), + ('D', 500), + ('CD', 400), + ('C', 100), + ('XC', 90), + ('L', 50), + ('XL', 40), + ('X', 10), + ('IX', 9), + ('V', 5), + ('IV', 4), + ('I', 1), +) def to_roman(n): @@ -53,67 +55,65 @@ def from_roman(s): return result -##################################### -# Tests for roman numeral conversion -##################################### - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman12.py b/source/examples/test_driven_development/roman12.py index 740741d..1b21fbf 100644 --- a/source/examples/test_driven_development/roman12.py +++ b/source/examples/test_driven_development/roman12.py @@ -10,19 +10,21 @@ import pytest -roman_numeral_map = (('M', 1000), - ('CM', 900), - ('D', 500), - ('CD', 400), - ('C', 100), - ('XC', 90), - ('L', 50), - ('XL', 40), - ('X', 10), - ('IX', 9), - ('V', 5), - ('IV', 4), - ('I', 1)) +roman_numeral_map = ( + ('M', 1000), + ('CM', 900), + ('D', 500), + ('CD', 400), + ('C', 100), + ('XC', 90), + ('L', 50), + ('XL', 40), + ('X', 10), + ('IX', 9), + ('V', 5), + ('IV', 4), + ('I', 1), +) def to_roman(n): @@ -70,67 +72,65 @@ def from_roman(s): return result -##################################### -# Tests for roman numeral conversion -##################################### - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman13.py b/source/examples/test_driven_development/roman13.py index d930b92..e46e79b 100644 --- a/source/examples/test_driven_development/roman13.py +++ b/source/examples/test_driven_development/roman13.py @@ -10,19 +10,21 @@ import pytest -roman_numeral_map = (('M', 1000), - ('CM', 900), - ('D', 500), - ('CD', 400), - ('C', 100), - ('XC', 90), - ('L', 50), - ('XL', 40), - ('X', 10), - ('IX', 9), - ('V', 5), - ('IV', 4), - ('I', 1)) +roman_numeral_map = ( + ('M', 1000), + ('CM', 900), + ('D', 500), + ('CD', 400), + ('C', 100), + ('XC', 90), + ('L', 50), + ('XL', 40), + ('X', 10), + ('IX', 9), + ('V', 5), + ('IV', 4), + ('I', 1), +) def to_roman(n): @@ -54,18 +56,18 @@ def is_valid_roman_numeral(s): print("starting to parse") print("the thousands") - print(f"{s = }") + print(f"{s=}") # first look for the thousands -- up to three Ms for _ in range(3): if s[:1] == "M": s = s[1:] # then look for the hundreds: print("the hundreds") - print(f"{s = }") + print(f"{s=}") # there can be ony one of CM, CD, or D: - if s[:2] == "CM": # 900 + if s[:2] == "CM": # 900 s = s[2:] - elif s[:2] == "CD": # 400 + elif s[:2] == "CD": # 400 s = s[2:] elif s[:1] == "D": # 500 s = s[1:] @@ -75,7 +77,7 @@ def is_valid_roman_numeral(s): s = s[1:] # now the tens print("the tens") - print(f"{s = }") + print(f"{s=}") # There can be one of either XC, XL or L if s[:2] == "XC": # 90 s = s[2:] @@ -89,7 +91,7 @@ def is_valid_roman_numeral(s): s = s[1:] # and the ones print("the ones") - print(f"{s = }") + print(f"{s=}") # There can be one of IX, IV or V if s[:2] == "IX": # 9 s = s[2:] @@ -98,14 +100,14 @@ def is_valid_roman_numeral(s): elif s[:1] == "V": # 5 s = s[1:] print("looking for the Is") - print(f"{s = }") + print(f"{s=}") # There can be up to three Is for _ in range(3): if s[:1] == "I": # 1 s = s[1:] # if there is anything left, it's not a valid Roman numeral print("done") - print(f"{s = }") + print(f"{s=}") if s: return False else: @@ -126,67 +128,65 @@ def from_roman(s): return result -##################################### -# Tests for roman numeral conversion -##################################### - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman14.py b/source/examples/test_driven_development/roman14.py index 190af69..00d99fd 100644 --- a/source/examples/test_driven_development/roman14.py +++ b/source/examples/test_driven_development/roman14.py @@ -10,19 +10,21 @@ import pytest -roman_numeral_map = (('M', 1000), - ('CM', 900), - ('D', 500), - ('CD', 400), - ('C', 100), - ('XC', 90), - ('L', 50), - ('XL', 40), - ('X', 10), - ('IX', 9), - ('V', 5), - ('IV', 4), - ('I', 1)) +roman_numeral_map = ( + ('M', 1000), + ('CM', 900), + ('D', 500), + ('CD', 400), + ('C', 100), + ('XC', 90), + ('L', 50), + ('XL', 40), + ('X', 10), + ('IX', 9), + ('V', 5), + ('IV', 4), + ('I', 1), +) def to_roman(n): @@ -54,18 +56,18 @@ def is_valid_roman_numeral(s): print("starting to parse") print("the thousands") - print(f"{s = }") + print(f"{s=}") # first look for the thousands -- up to three Ms for _ in range(3): if s[:1] == "M": s = s[1:] # then look for the hundreds: print("the hundreds") - print(f"{s = }") + print(f"{s=}") # there can be only one of CM, CD, or D: - if s[:2] == "CM": # 900 + if s[:2] == "CM": # 900 s = s[2:] - elif s[:2] == "CD": # 400 + elif s[:2] == "CD": # 400 s = s[2:] else: if s[:1] == "D": # 500 @@ -76,7 +78,7 @@ def is_valid_roman_numeral(s): s = s[1:] # now the tens print("the tens") - print(f"{s = }") + print(f"{s=}") # There can be one of either XC, XL or L if s[:2] == "XC": # 90 s = s[2:] @@ -90,7 +92,7 @@ def is_valid_roman_numeral(s): s = s[1:] # and the ones print("the ones") - print(f"{s = }") + print(f"{s=}") # There can be one of IX, IV or V if s[:2] == "IX": # 9 s = s[2:] @@ -99,14 +101,14 @@ def is_valid_roman_numeral(s): elif s[:1] == "V": # 5 s = s[1:] print("looking for the Is") - print(f"{s = }") + print(f"{s=}") # There can be up to three Is for _ in range(3): if s[:1] == "I": # 1 s = s[1:] # if there is anything left, it's not a valid Roman numeral print("done") - print(f"{s = }") + print(f"{s=}") if s: return False else: @@ -127,67 +129,65 @@ def from_roman(s): return result -##################################### -# Tests for roman numeral conversion -##################################### - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman15.py b/source/examples/test_driven_development/roman15.py index abc23e5..89e5119 100644 --- a/source/examples/test_driven_development/roman15.py +++ b/source/examples/test_driven_development/roman15.py @@ -10,19 +10,21 @@ import pytest -roman_numeral_map = (('M', 1000), - ('CM', 900), - ('D', 500), - ('CD', 400), - ('C', 100), - ('XC', 90), - ('L', 50), - ('XL', 40), - ('X', 10), - ('IX', 9), - ('V', 5), - ('IV', 4), - ('I', 1)) +roman_numeral_map = ( + ('M', 1000), + ('CM', 900), + ('D', 500), + ('CD', 400), + ('C', 100), + ('XC', 90), + ('L', 50), + ('XL', 40), + ('X', 10), + ('IX', 9), + ('V', 5), + ('IV', 4), + ('I', 1), +) def to_roman(n): @@ -54,18 +56,18 @@ def is_valid_roman_numeral(s): print("starting to parse") print("the thousands") - print(f"{s = }") + print(f"{s=}") # first look for the thousands -- up to three Ms for _ in range(3): if s[:1] == "M": s = s[1:] # then look for the hundreds: print("the hundreds") - print(f"{s = }") + print(f"{s=}") # there can be only one of CM, CD, or D: - if s[:2] == "CM": # 900 + if s[:2] == "CM": # 900 s = s[2:] - elif s[:2] == "CD": # 400 + elif s[:2] == "CD": # 400 s = s[2:] else: if s[:1] == "D": # 500 @@ -76,7 +78,7 @@ def is_valid_roman_numeral(s): s = s[1:] # now the tens print("the tens") - print(f"{s = }") + print(f"{s=}") # There can be one of either XC, XL or L if s[:2] == "XC": # 90 s = s[2:] @@ -91,7 +93,7 @@ def is_valid_roman_numeral(s): s = s[1:] # and the ones print("the ones") - print(f"{s = }") + print(f"{s=}") # There can be one of IX, IV or V if s[:2] == "IX": # 9 s = s[2:] @@ -100,14 +102,14 @@ def is_valid_roman_numeral(s): elif s[:1] == "V": # 5 s = s[1:] print("looking for the Is") - print(f"{s = }") + print(f"{s=}") # There can be up to three Is for _ in range(3): if s[:1] == "I": # 1 s = s[1:] # if there is anything left, it's not a valid Roman numeral print("done") - print(f"{s = }") + print(f"{s=}") if s: return False else: @@ -128,67 +130,65 @@ def from_roman(s): return result -##################################### -# Tests for roman numeral conversion -##################################### - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman16.py b/source/examples/test_driven_development/roman16.py index a111f1f..5a4f397 100644 --- a/source/examples/test_driven_development/roman16.py +++ b/source/examples/test_driven_development/roman16.py @@ -10,19 +10,21 @@ import pytest -roman_numeral_map = (('M', 1000), - ('CM', 900), - ('D', 500), - ('CD', 400), - ('C', 100), - ('XC', 90), - ('L', 50), - ('XL', 40), - ('X', 10), - ('IX', 9), - ('V', 5), - ('IV', 4), - ('I', 1)) +roman_numeral_map = ( + ('M', 1000), + ('CM', 900), + ('D', 500), + ('CD', 400), + ('C', 100), + ('XC', 90), + ('L', 50), + ('XL', 40), + ('X', 10), + ('IX', 9), + ('V', 5), + ('IV', 4), + ('I', 1), +) def to_roman(n): @@ -54,14 +56,14 @@ def is_valid_roman_numeral(s): print("starting to parse") print("the thousands") - print(f"{s = }") + print(f"{s=}") # first look for the thousands -- up to three Ms for _ in range(3): if s[:1] == "M": s = s[1:] # then look for the hundreds: print("the hundreds") - print(f"{s = }") + print(f"{s=}") # there can be only one of CM, CD, or D: if s[:2] == "CM": # 900 s = s[2:] @@ -76,7 +78,7 @@ def is_valid_roman_numeral(s): s = s[1:] # now the tens print("the tens") - print(f"{s = }") + print(f"{s=}") # There can be one of either XC, XL or L if s[:2] == "XC": # 90 s = s[2:] @@ -91,7 +93,7 @@ def is_valid_roman_numeral(s): s = s[1:] # and the ones print("the ones") - print(f"{s = }") + print(f"{s=}") # There can be one of IX, IV or V if s[:2] == "IX": # 9 s = s[2:] @@ -101,14 +103,14 @@ def is_valid_roman_numeral(s): if s[:1] == "V": # 5 s = s[1:] print("looking for the Is") - print(f"{s = }") + print(f"{s=}") # There can be up to three Is for _ in range(3): if s[:1] == "I": # 1 s = s[1:] # if there is anything left, it's not a valid Roman numeral print("done") - print(f"{s = }") + print(f"{s=}") if s: return False else: @@ -129,67 +131,65 @@ def from_roman(s): return result -##################################### -# Tests for roman numeral conversion -##################################### - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman17.py b/source/examples/test_driven_development/roman17.py index 6ad7bf1..96eda6b 100644 --- a/source/examples/test_driven_development/roman17.py +++ b/source/examples/test_driven_development/roman17.py @@ -10,19 +10,21 @@ import pytest -roman_numeral_map = (('M', 1000), - ('CM', 900), - ('D', 500), - ('CD', 400), - ('C', 100), - ('XC', 90), - ('L', 50), - ('XL', 40), - ('X', 10), - ('IX', 9), - ('V', 5), - ('IV', 4), - ('I', 1)) +roman_numeral_map = ( + ('M', 1000), + ('CM', 900), + ('D', 500), + ('CD', 400), + ('C', 100), + ('XC', 90), + ('L', 50), + ('XL', 40), + ('X', 10), + ('IX', 9), + ('V', 5), + ('IV', 4), + ('I', 1), +) def to_roman(n): @@ -110,67 +112,65 @@ def from_roman(s): return result -##################################### -# Tests for roman numeral conversion -##################################### - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman2.py b/source/examples/test_driven_development/roman2.py index 8503720..9b5b218 100644 --- a/source/examples/test_driven_development/roman2.py +++ b/source/examples/test_driven_development/roman2.py @@ -8,19 +8,21 @@ tests are expected to be able to be run with the pytest system """ -roman_numeral_map = (('M', 1000), - ('CM', 900), - ('D', 500), - ('CD', 400), - ('C', 100), - ('XC', 90), - ('L', 50), - ('XL', 40), - ('X', 10), - ('IX', 9), - ('V', 5), - ('IV', 4), - ('I', 1)) +roman_numeral_map = ( + ('M', 1000), + ('CM', 900), + ('D', 500), + ('CD', 400), + ('C', 100), + ('XC', 90), + ('L', 50), + ('XL', 40), + ('X', 10), + ('IX', 9), + ('V', 5), + ('IV', 4), + ('I', 1), +) def to_roman(n): @@ -34,65 +36,65 @@ def to_roman(n): return result -## Tests for roman numeral conversion - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman3.py b/source/examples/test_driven_development/roman3.py index ba02c23..c144166 100644 --- a/source/examples/test_driven_development/roman3.py +++ b/source/examples/test_driven_development/roman3.py @@ -10,19 +10,21 @@ import pytest -roman_numeral_map = (('M', 1000), - ('CM', 900), - ('D', 500), - ('CD', 400), - ('C', 100), - ('XC', 90), - ('L', 50), - ('XL', 40), - ('X', 10), - ('IX', 9), - ('V', 5), - ('IV', 4), - ('I', 1)) +roman_numeral_map = ( + ('M', 1000), + ('CM', 900), + ('D', 500), + ('CD', 400), + ('C', 100), + ('XC', 90), + ('L', 50), + ('XL', 40), + ('X', 10), + ('IX', 9), + ('V', 5), + ('IV', 4), + ('I', 1), +) def to_roman(n): @@ -36,65 +38,65 @@ def to_roman(n): return result -## Tests for roman numeral conversion - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman4.py b/source/examples/test_driven_development/roman4.py index d994a8d..24cb269 100644 --- a/source/examples/test_driven_development/roman4.py +++ b/source/examples/test_driven_development/roman4.py @@ -10,19 +10,21 @@ import pytest -roman_numeral_map = (('M', 1000), - ('CM', 900), - ('D', 500), - ('CD', 400), - ('C', 100), - ('XC', 90), - ('L', 50), - ('XL', 40), - ('X', 10), - ('IX', 9), - ('V', 5), - ('IV', 4), - ('I', 1)) +roman_numeral_map = ( + ('M', 1000), + ('CM', 900), + ('D', 500), + ('CD', 400), + ('C', 100), + ('XC', 90), + ('L', 50), + ('XL', 40), + ('X', 10), + ('IX', 9), + ('V', 5), + ('IV', 4), + ('I', 1), +) def to_roman(n): @@ -38,65 +40,65 @@ def to_roman(n): return result -## Tests for roman numeral conversion - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman5.py b/source/examples/test_driven_development/roman5.py index 5737d04..be5e11d 100644 --- a/source/examples/test_driven_development/roman5.py +++ b/source/examples/test_driven_development/roman5.py @@ -10,19 +10,21 @@ import pytest -roman_numeral_map = (('M', 1000), - ('CM', 900), - ('D', 500), - ('CD', 400), - ('C', 100), - ('XC', 90), - ('L', 50), - ('XL', 40), - ('X', 10), - ('IX', 9), - ('V', 5), - ('IV', 4), - ('I', 1)) +roman_numeral_map = ( + ('M', 1000), + ('CM', 900), + ('D', 500), + ('CD', 400), + ('C', 100), + ('XC', 90), + ('L', 50), + ('XL', 40), + ('X', 10), + ('IX', 9), + ('V', 5), + ('IV', 4), + ('I', 1), +) def to_roman(n): @@ -38,65 +40,65 @@ def to_roman(n): return result -## Tests for roman numeral conversion - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman6.py b/source/examples/test_driven_development/roman6.py index e088ebf..b13a215 100644 --- a/source/examples/test_driven_development/roman6.py +++ b/source/examples/test_driven_development/roman6.py @@ -10,19 +10,21 @@ import pytest -roman_numeral_map = (('M', 1000), - ('CM', 900), - ('D', 500), - ('CD', 400), - ('C', 100), - ('XC', 90), - ('L', 50), - ('XL', 40), - ('X', 10), - ('IX', 9), - ('V', 5), - ('IV', 4), - ('I', 1)) +roman_numeral_map = ( + ('M', 1000), + ('CM', 900), + ('D', 500), + ('CD', 400), + ('C', 100), + ('XC', 90), + ('L', 50), + ('XL', 40), + ('X', 10), + ('IX', 9), + ('V', 5), + ('IV', 4), + ('I', 1), +) def to_roman(n): @@ -38,65 +40,65 @@ def to_roman(n): return result -## Tests for roman numeral conversion - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman7.py b/source/examples/test_driven_development/roman7.py index beb34ae..ec4f151 100644 --- a/source/examples/test_driven_development/roman7.py +++ b/source/examples/test_driven_development/roman7.py @@ -10,19 +10,21 @@ import pytest -roman_numeral_map = (('M', 1000), - ('CM', 900), - ('D', 500), - ('CD', 400), - ('C', 100), - ('XC', 90), - ('L', 50), - ('XL', 40), - ('X', 10), - ('IX', 9), - ('V', 5), - ('IV', 4), - ('I', 1)) +roman_numeral_map = ( + ('M', 1000), + ('CM', 900), + ('D', 500), + ('CD', 400), + ('C', 100), + ('XC', 90), + ('L', 50), + ('XL', 40), + ('X', 10), + ('IX', 9), + ('V', 5), + ('IV', 4), + ('I', 1), +) def to_roman(n): @@ -38,65 +40,65 @@ def to_roman(n): return result -## Tests for roman numeral conversion - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman8.py b/source/examples/test_driven_development/roman8.py index 7da3152..40fb997 100644 --- a/source/examples/test_driven_development/roman8.py +++ b/source/examples/test_driven_development/roman8.py @@ -10,19 +10,21 @@ import pytest -roman_numeral_map = (('M', 1000), - ('CM', 900), - ('D', 500), - ('CD', 400), - ('C', 100), - ('XC', 90), - ('L', 50), - ('XL', 40), - ('X', 10), - ('IX', 9), - ('V', 5), - ('IV', 4), - ('I', 1)) +roman_numeral_map = ( + ('M', 1000), + ('CM', 900), + ('D', 500), + ('CD', 400), + ('C', 100), + ('XC', 90), + ('L', 50), + ('XL', 40), + ('X', 10), + ('IX', 9), + ('V', 5), + ('IV', 4), + ('I', 1), +) def to_roman(n): @@ -41,65 +43,65 @@ def to_roman(n): return result -## Tests for roman numeral conversion - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/test_driven_development/roman9.py b/source/examples/test_driven_development/roman9.py index a3b7ef2..26ba35b 100644 --- a/source/examples/test_driven_development/roman9.py +++ b/source/examples/test_driven_development/roman9.py @@ -10,19 +10,21 @@ import pytest -roman_numeral_map = (('M', 1000), - ('CM', 900), - ('D', 500), - ('CD', 400), - ('C', 100), - ('XC', 90), - ('L', 50), - ('XL', 40), - ('X', 10), - ('IX', 9), - ('V', 5), - ('IV', 4), - ('I', 1)) +roman_numeral_map = ( + ('M', 1000), + ('CM', 900), + ('D', 500), + ('CD', 400), + ('C', 100), + ('XC', 90), + ('L', 50), + ('XL', 40), + ('X', 10), + ('IX', 9), + ('V', 5), + ('IV', 4), + ('I', 1), +) def to_roman(n): @@ -41,65 +43,65 @@ def to_roman(n): return result -## Tests for roman numeral conversion - -KNOWN_VALUES = ( (1, 'I'), - (2, 'II'), - (3, 'III'), - (4, 'IV'), - (5, 'V'), - (6, 'VI'), - (7, 'VII'), - (8, 'VIII'), - (9, 'IX'), - (10, 'X'), - (50, 'L'), - (100, 'C'), - (500, 'D'), - (1000, 'M'), - (31, 'XXXI'), - (148, 'CXLVIII'), - (294, 'CCXCIV'), - (312, 'CCCXII'), - (421, 'CDXXI'), - (528, 'DXXVIII'), - (621, 'DCXXI'), - (782, 'DCCLXXXII'), - (870, 'DCCCLXX'), - (941, 'CMXLI'), - (1043, 'MXLIII'), - (1110, 'MCX'), - (1226, 'MCCXXVI'), - (1301, 'MCCCI'), - (1485, 'MCDLXXXV'), - (1509, 'MDIX'), - (1607, 'MDCVII'), - (1754, 'MDCCLIV'), - (1832, 'MDCCCXXXII'), - (1993, 'MCMXCIII'), - (2074, 'MMLXXIV'), - (2152, 'MMCLII'), - (2212, 'MMCCXII'), - (2343, 'MMCCCXLIII'), - (2499, 'MMCDXCIX'), - (2574, 'MMDLXXIV'), - (2646, 'MMDCXLVI'), - (2723, 'MMDCCXXIII'), - (2892, 'MMDCCCXCII'), - (2975, 'MMCMLXXV'), - (3051, 'MMMLI'), - (3185, 'MMMCLXXXV'), - (3250, 'MMMCCL'), - (3313, 'MMMCCCXIII'), - (3408, 'MMMCDVIII'), - (3501, 'MMMDI'), - (3610, 'MMMDCX'), - (3743, 'MMMDCCXLIII'), - (3844, 'MMMDCCCXLIV'), - (3888, 'MMMDCCCLXXXVIII'), - (3940, 'MMMCMXL'), - (3999, 'MMMCMXCIX'), - ) +# Tests for roman numeral conversion +KNOWN_VALUES = ( + (1, 'I'), + (2, 'II'), + (3, 'III'), + (4, 'IV'), + (5, 'V'), + (6, 'VI'), + (7, 'VII'), + (8, 'VIII'), + (9, 'IX'), + (10, 'X'), + (50, 'L'), + (100, 'C'), + (500, 'D'), + (1000, 'M'), + (31, 'XXXI'), + (148, 'CXLVIII'), + (294, 'CCXCIV'), + (312, 'CCCXII'), + (421, 'CDXXI'), + (528, 'DXXVIII'), + (621, 'DCXXI'), + (782, 'DCCLXXXII'), + (870, 'DCCCLXX'), + (941, 'CMXLI'), + (1043, 'MXLIII'), + (1110, 'MCX'), + (1226, 'MCCXXVI'), + (1301, 'MCCCI'), + (1485, 'MCDLXXXV'), + (1509, 'MDIX'), + (1607, 'MDCVII'), + (1754, 'MDCCLIV'), + (1832, 'MDCCCXXXII'), + (1993, 'MCMXCIII'), + (2074, 'MMLXXIV'), + (2152, 'MMCLII'), + (2212, 'MMCCXII'), + (2343, 'MMCCCXLIII'), + (2499, 'MMCDXCIX'), + (2574, 'MMDLXXIV'), + (2646, 'MMDCXLVI'), + (2723, 'MMDCCXXIII'), + (2892, 'MMDCCCXCII'), + (2975, 'MMCMLXXV'), + (3051, 'MMMLI'), + (3185, 'MMMCLXXXV'), + (3250, 'MMMCCL'), + (3313, 'MMMCCCXIII'), + (3408, 'MMMCDVIII'), + (3501, 'MMMDI'), + (3610, 'MMMDCX'), + (3743, 'MMMDCCXLIII'), + (3844, 'MMMDCCCXLIV'), + (3888, 'MMMDCCCLXXXVIII'), + (3940, 'MMMCMXL'), + (3999, 'MMMCMXCIX'), +) def test_to_roman_known_values(): diff --git a/source/examples/testing/calculator/calculator.py b/source/examples/testing/calculator/calculator.py index 8bd0c75..97e5548 100755 --- a/source/examples/testing/calculator/calculator.py +++ b/source/examples/testing/calculator/calculator.py @@ -50,6 +50,7 @@ def main(): else: return "invalid input" + if __name__ == "__main__": try: print(main()) diff --git a/source/examples/testing/calculator/calculator_functions.py b/source/examples/testing/calculator/calculator_functions.py index 532d776..c740204 100644 --- a/source/examples/testing/calculator/calculator_functions.py +++ b/source/examples/testing/calculator/calculator_functions.py @@ -15,8 +15,10 @@ def add(x, y): def subtract(x, y): return int(x) - int(y) + def multiply(x, y): return int(x) * int(y) + def divide(x, y): return int(x) / int(y) diff --git a/source/examples/testing/calculator/test_calculator_pytest.py b/source/examples/testing/calculator/test_calculator_pytest.py index 0482978..bd86ed5 100644 --- a/source/examples/testing/calculator/test_calculator_pytest.py +++ b/source/examples/testing/calculator/test_calculator_pytest.py @@ -34,6 +34,8 @@ def test_multiply_ugly(): (-2, -2, 4), (3, 0, 0), ] + + @pytest.mark.parametrize(param_names, params) def test_multiply(arg1, arg2, result): assert calc.multiply(arg1, arg2) == result diff --git a/source/examples/testing/pytest_fixtures.py b/source/examples/testing/pytest_fixtures.py index 36d7434..0c9a3e9 100644 --- a/source/examples/testing/pytest_fixtures.py +++ b/source/examples/testing/pytest_fixtures.py @@ -35,6 +35,7 @@ def test_two(example_fixture): print("running test_two") assert example_fixture["that"] == 2 + # with teardown: @pytest.fixture(scope="module") def example_fixture2(): diff --git a/source/examples/testing/test_mock_input.py b/source/examples/testing/test_mock_input.py index a207b63..8ccec50 100644 --- a/source/examples/testing/test_mock_input.py +++ b/source/examples/testing/test_mock_input.py @@ -10,7 +10,6 @@ def get_color(): return "Hey! that's mine too!" else: raise ValueError("nothing to say about that color") - return color @mock.patch('builtins.input') @@ -31,4 +30,4 @@ def test_get_color_blue(mocked_input): def test_get_color_purple(mocked_input): mocked_input.return_value = "purple" with pytest.raises(ValueError): - result = get_color() + get_color() diff --git a/source/examples/threading-multiprocessing/lock/simple_locks.py b/source/examples/threading-multiprocessing/lock/simple_locks.py index a4456a9..9fb56ad 100644 --- a/source/examples/threading-multiprocessing/lock/simple_locks.py +++ b/source/examples/threading-multiprocessing/lock/simple_locks.py @@ -3,12 +3,14 @@ lock = threading.Lock() + def f(): lock.acquire() print("%s got lock" % threading.current_thread().name) time.sleep(1) lock.release() + threading.Thread(target=f).start() threading.Thread(target=f).start() threading.Thread(target=f).start() diff --git a/source/examples/threading-multiprocessing/lock/stdout_writer.py b/source/examples/threading-multiprocessing/lock/stdout_writer.py index 715ae3f..bbf1a90 100644 --- a/source/examples/threading-multiprocessing/lock/stdout_writer.py +++ b/source/examples/threading-multiprocessing/lock/stdout_writer.py @@ -9,6 +9,7 @@ def write(): time.sleep(random.random()) sys.stdout.write("..done\n") + for i in range(100): thread = threading.Thread(target=write) thread.daemon = True # allow ctrl-c to end diff --git a/source/examples/threading-multiprocessing/lock/stdout_writer_semaphore.py b/source/examples/threading-multiprocessing/lock/stdout_writer_semaphore.py index 2bc21cd..d26dd4d 100644 --- a/source/examples/threading-multiprocessing/lock/stdout_writer_semaphore.py +++ b/source/examples/threading-multiprocessing/lock/stdout_writer_semaphore.py @@ -5,11 +5,12 @@ lock = threading.Semaphore(2) + def write(): lock.acquire() - sys.stdout.write( "%s writing.." % threading.current_thread().name) + sys.stdout.write("%s writing.." % threading.current_thread().name) time.sleep(random.random()) - sys.stdout.write( "..done\n") + sys.stdout.write("..done\n") lock.release() diff --git a/source/examples/threading-multiprocessing/lock/stdout_writer_solution.py b/source/examples/threading-multiprocessing/lock/stdout_writer_solution.py index 01084a2..816e4cf 100644 --- a/source/examples/threading-multiprocessing/lock/stdout_writer_solution.py +++ b/source/examples/threading-multiprocessing/lock/stdout_writer_solution.py @@ -13,6 +13,7 @@ def write(): sys.stdout.write("..done\n") lock.release() + threads = [] for i in range(50): thread = threading.Thread(target=write) diff --git a/source/examples/threading-multiprocessing/lock_exercise.zip b/source/examples/threading-multiprocessing/lock_exercise.zip index d315044..bb50a51 100644 Binary files a/source/examples/threading-multiprocessing/lock_exercise.zip and b/source/examples/threading-multiprocessing/lock_exercise.zip differ diff --git a/source/examples/threading-multiprocessing/multiprocessing/channel.py b/source/examples/threading-multiprocessing/multiprocessing/channel.py index 0a0a343..2612cfa 100644 --- a/source/examples/threading-multiprocessing/multiprocessing/channel.py +++ b/source/examples/threading-multiprocessing/multiprocessing/channel.py @@ -2,6 +2,7 @@ # Does not work with python3... import pickle + class Channel(): def __init__(self, out_f, in_f): self.out_f = out_f diff --git a/source/examples/threading-multiprocessing/multiprocessing/parent.py b/source/examples/threading-multiprocessing/multiprocessing/parent.py index 21c0d6c..0247ba5 100644 --- a/source/examples/threading-multiprocessing/multiprocessing/parent.py +++ b/source/examples/threading-multiprocessing/multiprocessing/parent.py @@ -8,5 +8,5 @@ ch.send(b"Hello World") ch.send(42) -ch.send([1,2,3,4]) -ch.send({'host':'python.org', 'port':80}) +ch.send([1, 2, 3, 4]) +ch.send({'host': 'python.org', 'port': 80}) diff --git a/source/examples/threading-multiprocessing/server/client-asyncio.py b/source/examples/threading-multiprocessing/server/client-asyncio.py index 1240cde..ce2f4bf 100755 --- a/source/examples/threading-multiprocessing/server/client-asyncio.py +++ b/source/examples/threading-multiprocessing/server/client-asyncio.py @@ -2,40 +2,36 @@ # TODO: update this with async / await -import os -import sys -from urllib.request import urlopen import asyncio +from urllib.request import urlopen +from decorators import timer -sys.path.append(os.path.join(os.path.dirname(__file__), "..")) - -# from decorators.decorators import timer - -# @timer results = asyncio.Queue() - url = "http://localhost:37337" -@asyncio.coroutine -def producer(): +async def producer(): conn = urlopen(url) result = conn.read() return result -@asyncio.coroutine -def worker(): - result = yield from producer() +async def worker(): + result = await producer() results.put(result) -loop = asyncio.get_event_loop() +@timer +def threading_client(number_of_requests=10): + loop = asyncio.new_event_loop() + + for i in range(number_of_requests): + loop.run_until_complete(worker()) -number_of_requests = 100 + print("made %d requests" % number_of_requests) -for i in range(number_of_requests): - loop.run_until_complete(worker()) -print("made %d requests" % number_of_requests) +if __name__ == "__main__": + number_of_requests = 100 + threading_client(number_of_requests=number_of_requests) diff --git a/source/examples/threading-multiprocessing/server/client-mp.py b/source/examples/threading-multiprocessing/server/client-mp.py index 9fbf0fe..f293666 100755 --- a/source/examples/threading-multiprocessing/server/client-mp.py +++ b/source/examples/threading-multiprocessing/server/client-mp.py @@ -1,27 +1,23 @@ #!/usr/bin/env python -import os -import sys import urllib.request import multiprocessing +from decorators import timer -sys.path.append(os.path.join(os.path.dirname(__file__), "..")) -from decorators.decorators import timer +results = multiprocessing.Queue() +url = "http://localhost:37337" -@timer -def threading_client(number_of_requests=10): - - results = multiprocessing.Queue() - url = "http://localhost:37337" +def worker(*args): + conn = urllib.request.urlopen(url) + result = conn.read() + conn.close() + results.put(result) - def worker(*args): - conn = urllib.request.urlopen(url) - result = conn.read() - conn.close() - results.put(result) +@timer +def threading_client(number_of_requests=10): for i in range(number_of_requests): proc = multiprocessing.Process(target=worker, args=()) proc.start() @@ -34,6 +30,5 @@ def worker(*args): if __name__ == "__main__": - number_of_requests = 100 threading_client(number_of_requests=number_of_requests) diff --git a/source/examples/threading-multiprocessing/server/client-pooled-solution.py b/source/examples/threading-multiprocessing/server/client-pooled-solution.py index 3e4d046..fbd38a2 100755 --- a/source/examples/threading-multiprocessing/server/client-pooled-solution.py +++ b/source/examples/threading-multiprocessing/server/client-pooled-solution.py @@ -1,28 +1,27 @@ #!/usr/bin/env python from multiprocessing.pool import ThreadPool -import os -import sys -import urllib.request, urllib.error, urllib.parse +import urllib.request +import urllib.error +import urllib.parse import queue +from decorators import timer -sys.path.append(os.path.join(os.path.dirname(__file__), "..")) -from decorators.decorators import timer +results = queue.Queue() +url = "http://localhost:37337" -@timer -def threading_client(number_of_requests=10, thread_count=2): - results = queue.Queue() - url = "http://localhost:37337" +def worker(*args): + conn = urllib.request.urlopen(url) + result = conn.read() + conn.close() + results.put(result) + print(result) - def worker(*args): - conn = urllib.request.urlopen(url) - result = conn.read() - conn.close() - results.put(result) - print(result) +@timer +def threading_client(number_of_requests=10, thread_count=2): pool = ThreadPool(processes=thread_count) pool.map(worker, list(range(number_of_requests))) diff --git a/source/examples/threading-multiprocessing/server/client-pooled.py b/source/examples/threading-multiprocessing/server/client-pooled.py index 391738e..d02fc8b 100755 --- a/source/examples/threading-multiprocessing/server/client-pooled.py +++ b/source/examples/threading-multiprocessing/server/client-pooled.py @@ -1,27 +1,24 @@ #!/usr/bin/env python from multiprocessing.pool import ThreadPool -import os -import sys -import urllib.request, urllib.error, urllib.parse -import queue +import urllib.request +import urllib.error +import urllib.parse +from decorators import timer -sys.path.append(os.path.join(os.path.dirname(__file__), "..")) -from decorators.decorators import timer +url = "http://localhost:37337" -@timer -def threading_client(number_of_requests=10, thread_count=2): - - url = "http://localhost:37337" +def worker(*args): + conn = urllib.request.urlopen(url) + result = conn.read() + conn.close() + print(result) - def worker(*args): - conn = urllib.request.urlopen(url) - result = conn.read() - conn.close() - print(result) +@timer +def threading_client(number_of_requests=10, thread_count=2): pool = ThreadPool(processes=thread_count) pool.map(worker, list(range(number_of_requests))) diff --git a/source/examples/threading-multiprocessing/server/client-threading.py b/source/examples/threading-multiprocessing/server/client-threading.py index b652ced..e16245f 100755 --- a/source/examples/threading-multiprocessing/server/client-threading.py +++ b/source/examples/threading-multiprocessing/server/client-threading.py @@ -1,27 +1,24 @@ #!/usr/bin/env python -import os -import sys import urllib.request -import threading, queue +import threading +import queue +from decorators import timer -sys.path.append(os.path.join(os.path.dirname(__file__), "..")) -from decorators.decorators import timer +results = queue.Queue() +url = "http://localhost:37337" -@timer -def threading_client(number_of_requests=10): - - results = queue.Queue() - url = "http://localhost:37337" +def worker(*args): + conn = urllib.request.urlopen(url) + result = conn.read() + conn.close() + results.put(result) - def worker(*args): - conn = urllib.request.urlopen(url) - result = conn.read() - conn.close() - results.put(result) +@timer +def threading_client(number_of_requests=10): for i in range(number_of_requests): thread = threading.Thread(target=worker, args=()) thread.start() @@ -34,6 +31,5 @@ def worker(*args): if __name__ == "__main__": - number_of_requests = 100 threading_client(number_of_requests=number_of_requests) diff --git a/source/examples/threading-multiprocessing/server/decorators.py b/source/examples/threading-multiprocessing/server/decorators.py new file mode 100644 index 0000000..a46356f --- /dev/null +++ b/source/examples/threading-multiprocessing/server/decorators.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python + +import time + + +def timer(func): + def timer(*args, **kwargs): + t1 = time.time() + result = func(*args, **kwargs) + print("-- executed in %.4f seconds :" % (time.time() - t1)) + return result + return timer diff --git a/source/examples/threading-multiprocessing/simple-threading.py b/source/examples/threading-multiprocessing/simple-threading.py index f623fee..b269672 100644 --- a/source/examples/threading-multiprocessing/simple-threading.py +++ b/source/examples/threading-multiprocessing/simple-threading.py @@ -4,9 +4,6 @@ import random - - - def func(n): for i in range(n): print("hello from thread %s" % threading.current_thread().name) @@ -20,11 +17,6 @@ def func(n): threads.append(thread) - - - - - for thread in threads: print("joining thread:", thread.name) thread.join() diff --git a/source/examples/threading-multiprocessing/sqlite_threaded.py b/source/examples/threading-multiprocessing/sqlite_threaded.py index 2b70a71..023913f 100644 --- a/source/examples/threading-multiprocessing/sqlite_threaded.py +++ b/source/examples/threading-multiprocessing/sqlite_threaded.py @@ -1,9 +1,7 @@ import logging import os -import sys import sqlite3 import threading -import time import random import string @@ -14,10 +12,12 @@ DB_FILENAME = 'test.db' + def create_db_table(): with sqlite3.connect(DB_FILENAME) as conn: conn.execute("""CREATE TABLE BOOKS(author VARCHAR, title VARCHAR, id INTEGER PRIMARY KEY)""") + def reader(): """ Function to be threaded. Gets book name and author from the database, one at a time. @@ -28,9 +28,9 @@ def reader(): # keep track of each loop print('try to get a book', i) cursor = conn.cursor() - cursor.execute("""SELECT * FROM BOOKS WHERE id={}""".format(i)) + cursor.execute("SELECT * FROM BOOKS WHERE id = ?", (i,)) #cursor.execute("SELECT * FROM BOOKS ORDER BY id DESC LIMIT 1") - # have to iteratote through the cursor to get the result, but it + # have to iterate through the cursor to get the result, but it # is only printing one thing. # If there is not a new book there yet, will get the last book for row in cursor: @@ -42,6 +42,7 @@ def reader(): print('exit show_books') + def writer(): """ Function to be threaded. Over-the-top creating of a random words for author name and book name, @@ -69,10 +70,11 @@ def writer(): create_db_table() ready = threading.Event() - # If you start the writer thread first, it will hog the thread similarly - # In general, you will see that the database connection does not like to let go - # Can you figure out how to get the connection to cooperate more? - # Under what circumstances are database transactions optimized by threading? + # If you start the writer thread first, it will hog the thread similarly. + # In general, you will see that the database connection does not like to + # let go. Can you figure out how to get the connection to cooperate more? + # Under what circumstances are database transactions optimized by + # threading? threads = [ threading.Thread(name="Reader", target=reader, args=()), diff --git a/source/examples/threading-multiprocessing/test.db b/source/examples/threading-multiprocessing/test.db deleted file mode 100644 index 89c9683..0000000 Binary files a/source/examples/threading-multiprocessing/test.db and /dev/null differ diff --git a/source/examples/wikidef/mock_input_example.py b/source/examples/wikidef/mock_input_example.py index a207b63..8ccec50 100644 --- a/source/examples/wikidef/mock_input_example.py +++ b/source/examples/wikidef/mock_input_example.py @@ -10,7 +10,6 @@ def get_color(): return "Hey! that's mine too!" else: raise ValueError("nothing to say about that color") - return color @mock.patch('builtins.input') @@ -31,4 +30,4 @@ def test_get_color_blue(mocked_input): def test_get_color_purple(mocked_input): mocked_input.return_value = "purple" with pytest.raises(ValueError): - result = get_color() + get_color() diff --git a/source/examples/wikidef/test_wikidef_with_mock.py b/source/examples/wikidef/test_wikidef_with_mock.py index 87ab0ec..df0a332 100644 --- a/source/examples/wikidef/test_wikidef_with_mock.py +++ b/source/examples/wikidef/test_wikidef_with_mock.py @@ -36,19 +36,19 @@ def tearDown(self): @patch.object(Wikipedia, 'get_article') def test_article_success_decorator_mocked(self, mock_method): - article = Definitions.article("Robot") + Definitions.article("Robot") mock_method.assert_called_once_with("Robot") @patch.object(requests, 'get') def test_error_finding_article(self, mock_response): mock_response.return_value = MockResponse() with self.assertRaises(MissingArticleError): - article = Wikipedia.get_article("blah") + Wikipedia.get_article("blah") class MockResponse(): def json(self): - return {'error':{'info': 'nonsense'}} + return {'error': {'info': 'nonsense'}} # # patch with a context manager # def test_article_success_context_manager_mocked(self):