Skip to content
Snippets Groups Projects
Commit eda69ca6 authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Matched code to the latest version of the testability examples.

parent a170be5f
No related branches found
No related tags found
No related merge requests found
*~
*.pyc
*.pyo
__pycache__
.idea
from math import inf
from random import randrange, choice
SUITE_TEMPLATE = '''
from math import inf
from unittest import TestCase
......@@ -37,7 +38,7 @@ def generate_padding(smallest_positive):
def generate_random_test(number):
smallest_positive = choice((randrange(VALUE_LIMIT), inf))
smallest_positive = choice((randrange(1, VALUE_LIMIT), inf))
inputs = [smallest_positive] # TODO
return TEST_TEMPLATE.format(number=number, inputs=inputs, expected=smallest_positive)
......
......@@ -2,42 +2,59 @@ from math import inf
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty, StringProperty
from kivy.properties import NumericProperty, StringProperty, ListProperty
class Field(BoxLayout):
label_text = StringProperty()
index = NumericProperty()
entry = StringProperty()
class Fields(BoxLayout):
entries = ListProperty([]) # elements are strings (some may not be valid numbers)
def rebuild(self):
while len(self.children) > len(self.entries):
self.remove_widget(self.children[0])
while len(self.children) < len(self.entries):
self.add_widget(Field(index=len(self.children)))
def on_entries(self, _, __):
self.rebuild()
for field, entry in zip(reversed(self.children), self.entries):
field.entry = entry
class SmallestPositiveApp(App):
smallest_positive = NumericProperty(inf)
entries = ListProperty([]) # elements are strings (some may not be valid numbers)
result = NumericProperty(inf)
def add_field(self):
container = self.root.ids.fields
container.add_widget(Field(label_text=f'Field #{len(container.children)}: '))
self.entries.append('')
def remove_field(self):
container = self.root.ids.fields
if len(container.children) > 0:
container.remove_widget(container.children[0])
try:
self.entries.pop()
except IndexError:
pass
@staticmethod
def _find_smallest_positive(values):
def _find_smallest_positive(entries):
smallest_positive = inf
for value in values:
for entry in entries:
try:
value = float(entry)
if 0 < value < smallest_positive:
smallest_positive = value
return smallest_positive
def update(self):
container = self.root.ids.fields
values = []
for field in container.children:
try:
values.append(float(field.ids.input.text))
except ValueError:
pass
self.smallest_positive = SmallestPositiveApp._find_smallest_positive(values)
return smallest_positive
def on_entries(self, _, __):
self.result = SmallestPositiveApp._find_smallest_positive(self.entries)
def set_entry(self, index, entry):
self.entries[index] = entry
if __name__ == '__main__':
......
BoxLayout:
orientation: 'vertical'
BoxLayout:
id: fields
orientation: 'vertical'
Fields:
entries: app.entries
size_hint: (1.0, 6.0)
BoxLayout:
orientation: 'horizontal'
......@@ -15,18 +14,19 @@ BoxLayout:
font_size: sp(24)
on_press: app.remove_field()
Label:
id: result
text: f'Smallest Positive: {app.smallest_positive}'
text: f'Smallest Positive: {app.result}'
font_size: sp(24)
<Fields>:
orientation: 'vertical'
<Field>:
orientation: 'horizontal'
Label:
text: root.label_text
text: f'Field #{root.index}: '
font_size: sp(24)
TextInput:
id: input
multiline: False
write_tab: False
font_size: sp(24)
on_text: app.update()
on_text: app.set_entry(root.index, self.text)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment