(Jul-21-2022, 10:50 PM)Skaperen Wrote: [ -> ]if i have a list of strings and 2-tuples of 2 ints, where the 2-tuples of 2 ints change the cursor position to display the next text
Then try to find a better data-structure.
data = ["test1", (10, 10), "test2", (10, 20)]
This could be deconstructed into two different objects and later with zip, you can combine them:
data = ["test1", (10, 10), "test2", (10, 20)]
texts = data[::2]
positions = data[1::2]
for text, pos in zip(texts, positions):
for _ in range(pos[1]):
print()
print(text.rjust(pos[0]))
As you can see, first I create the list, then deconstructing it, then zipping it.
A different data structure can solve this problem
data = [
{"text": "test1", "position": (10, 10)},
{"text": "test2", "position": (10, 20)},
]
for element in data:
x, y = element["position"]
for _ in range(y):
print()
print(element["text"].rjust(x))
And it's better for typing.
from typing import TypedDict
class LCD_Data(TypedDict):
text: str
position: tuple[int, int]
def test1(data_many: list[LCD_Data]):
...
def test2(data_one: LCD_Data):
...
data: list[LCD_Data] = [
{"text": "test1", "position": (10, 10)},
{"text": "test2", "position": (10, 20)},
]
# check with IDE or MyPy
test1(data) # ok
test1(data[0]) # fail
test2(data) # fail
test2(data[0]) # ok
Output:
[andre@andre-Fujitsu-i5 ~]$ mypy mt.py
mt.py:25: error: Argument 1 to "test1" has incompatible type "LCD_Data"; expected "List[LCD_Data]"
mt.py:27: error: Argument 1 to "test2" has incompatible type "List[LCD_Data]"; expected "LCD_Data"
Found 2 errors in 1 file (checked 1 source file)
Even without typehints, but with the right data-sructure, you get lesser errors.
Getting the right elements from a mixed list is a bit of guessing.