diff --git a/example1.kv b/example1.kv index 8e850df7aa18ac0deca366f1525b0ca83b294210..2ea9ff20a05bb1d215e651cb30212201d9476132 100644 --- a/example1.kv +++ b/example1.kv @@ -16,7 +16,7 @@ BoxLayout: on_press: app.remove_field() Label: id: result - text: 'Smallest Positive: inf' + text: f'Smallest Positive: {app.smallest_positive}' font_size: sp(24) <Field>: diff --git a/example1.py b/example1.py index f7ee4b18e9afdd031b3c4468d3c9cdeeac82dfdf..53d1d42c91155f4ba613251a7fc1d9131e599faa 100644 --- a/example1.py +++ b/example1.py @@ -2,7 +2,7 @@ from math import inf from kivy.app import App from kivy.uix.boxlayout import BoxLayout -from kivy.properties import StringProperty +from kivy.properties import NumericProperty, StringProperty class Field(BoxLayout): @@ -10,6 +10,8 @@ class Field(BoxLayout): class Example1App(App): + smallest_positive = NumericProperty(inf) + def add_field(self): container = self.root.ids.fields container.add_widget(Field(label_text=f'Field #{len(container.children)}: ')) @@ -19,17 +21,23 @@ class Example1App(App): if len(container.children) > 0: container.remove_widget(container.children[0]) + @staticmethod + def _find_smallest_positive(values): + smallest_positive = inf + for value in values: + if 0 < value < smallest_positive: + smallest_positive = value + return smallest_positive + def update(self): container = self.root.ids.fields - smallest_positive = inf + values = [] for field in container.children: try: - value = float(field.ids.input.text) - if 0 < value < smallest_positive: - smallest_positive = value + values.append(float(field.ids.input.text)) except ValueError: pass - self.root.ids.result.text = f'Smallest Positive: {smallest_positive}' + self.smallest_positive = Example1App._find_smallest_positive(values) if __name__ == '__main__': diff --git a/example2.py b/example2.py index 4fbaa094a1aec59d0d47e863aa4cd76a8e8a1473..f2b854443a574f81d63347a8b0e70b54dc2da443 100644 --- a/example2.py +++ b/example2.py @@ -12,10 +12,6 @@ class Field(BoxLayout): class Example2App(App): smallest_positive = NumericProperty(inf) - def __init__(self, **kwargs): - super().__init__(**kwargs) - self.values = [] - def add_field(self): container = self.root.ids.fields container.add_widget(Field(label_text=f'Field #{len(container.children)}: ')) @@ -25,22 +21,23 @@ class Example2App(App): if len(container.children) > 0: container.remove_widget(container.children[0]) - def _find_smallest_positive(self): + @staticmethod + def _find_smallest_positive(values): result = inf - for value in self.values: + for value in values: if 0 < value < result: result = value return result def update(self): container = self.root.ids.fields - self.values = [] + values = [] for field in container.children: try: - self.values.append(float(field.ids.input.text)) + values.append(float(field.ids.input.text)) except ValueError: pass - self.smallest_positive = self._find_smallest_positive() + self.smallest_positive = self._find_smallest_positive(values) if __name__ == '__main__': diff --git a/example3.py b/example3.py index f6e6627880b74427e9d993375285ab6abe73741c..2673a12e98cf63d3fe2139fe2b2dfb1592d5b047 100644 --- a/example3.py +++ b/example3.py @@ -19,12 +19,15 @@ class Example3App(App): result = value return result + @staticmethod + def _get_values_from_database(session): + return [record.value for record in session.query(Value).all()] + def on_start(self): url = ValueDatabase.construct_mysql_url('localhost', 3306, 'values', 'root', 'cse1208') value_database = ValueDatabase(url) session = value_database.create_session() - values = [record.value for record in session.query(Value).all()] - self.smallest_positive = Example3App._find_smallest_positive(values) + self.smallest_positive = Example3App._find_smallest_positive(Example3App._get_values_from_database(session)) def main(): diff --git a/test_example1.py b/test_example1.py index f34301ccb9fd4ccd594fd773e685d152fc0499c4..76acfbc3bd8fd06dc91c8ed91bf4b729382f1235 100644 --- a/test_example1.py +++ b/test_example1.py @@ -5,4 +5,5 @@ from example1 import Example1App class TestExample1App(TestCase): def test_update(self): - self.fail() + actual = Example1App._find_smallest_positive([5.0, 2.0, 4.0]) + self.assertEqual(actual, 2.0) diff --git a/test_example2.py b/test_example2.py index d401b3565697a8b3be91fd0b93bd38cbf2ec8f70..1c9949024ad97d725e63097631d1f594f22757d9 100644 --- a/test_example2.py +++ b/test_example2.py @@ -5,7 +5,5 @@ from example2 import Example2App class TestExample2App(TestCase): def test_find_smallest_positive(self): - app = Example2App() - app.values = [5.0, 2.0, 4.0] - actual = app._find_smallest_positive() + actual = Example2App._find_smallest_positive([5.0, 2.0, 4.0]) self.assertEqual(actual, 2.0) diff --git a/test_example3.py b/test_example3.py index b8be0d345f45d86a0a4db4829ae796da6ff84d70..7f1ac0be026d99e4547272ffb311bd36e0614570 100644 --- a/test_example3.py +++ b/test_example3.py @@ -7,4 +7,13 @@ from example3 import Example3App class TestExample3App(TestCase): def test_database_access(self): - self.fail() + url = ValueDatabase.construct_in_memory_url() + database = ValueDatabase(url) + database.ensure_tables_exist() + session = database.create_session() + session.add(Value(value=5.0)) + session.add(Value(value=2.0)) + session.add(Value(value=4.0)) + session.commit() + actual = Example3App._get_values_from_database(session) + self.assertEqual(actual, [5.0, 2.0, 4.0])