Python: Make it possible to construct a ListModel from an iterable

Makes for a more pythonic API :)

cc #4135
This commit is contained in:
Simon Hausmann 2024-03-07 08:55:37 +01:00
parent c83615cfb1
commit 176aa4bf72
2 changed files with 18 additions and 2 deletions

View file

@ -25,9 +25,12 @@ class Model(native.PyModelBase):
class ListModel(Model):
def __init__(self, lst=None):
def __init__(self, iterable=None):
super().__init__()
self.list = lst or []
if iterable is not None:
self.list = list(iterable)
else:
self.list = []
def row_count(self):
return len(self.list)

View file

@ -83,6 +83,19 @@ def test_python_model_sequence():
assert model[2] == 3
def test_python_model_iterable():
def test_generator(max):
i = 0
while i < max:
yield i
i += 1
model = models.ListModel(test_generator(5))
assert len(model) == 5
assert list(model) == [0, 1, 2, 3, 4]
def test_rust_model_sequence():
compiler = native.ComponentCompiler()