mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-17 22:08:39 +00:00
Python: Make it possible to construct a ListModel from an iterable
Makes for a more pythonic API :) cc #4135
This commit is contained in:
parent
c83615cfb1
commit
176aa4bf72
2 changed files with 18 additions and 2 deletions
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue