numworks / epsilon

Modern graphing calculator operating system.
https://www.numworks.com/resources/engineering/software/
1.75k stars 463 forks source link

Adding flush to Python #2239

Open S0d4Dev opened 2 weeks ago

S0d4Dev commented 2 weeks ago

I have worked in a simple mastermind game made in python, and made the following code to make the letters of the text appear with a delay between them (without making newlines).

import time
def message(message, delay) :
    for letter in message :
        print(letter, flush=True)
        time.sleep(delay)

Unfortunately, this doesn't seem to work on the calculator, and results in a TypeError: screenshot

Another way of doing this is using escape codes, such as \b for a backspace. This does work but gives a unwanted output : screenshot (1)

To implement this flush feature, a first option would be to add the flush type, but that may not work if that is a limitation from Micropython itself. Another way to put it would be to add the sys module (or a part of it, such as stdout, if it can't fit in the calculator). The last option (that I can think of) would be to make the the escape codes such as \b work, but again, I don't know if there are some limitations around.

mobluse commented 1 week ago

It might be possible to solve using kandinsky:

# Type text with delay between letters.
import kandinsky as k
import time
def message(message, delay):
  r=11; c=1
  for letter in message:
    k.draw_string(letter, 10*c, 18*r+2)
    time.sleep(delay)
    c+=1
message("Hello, world.", 0.2)

https://my.numworks.com/python/mobluse/slowtext

Another solution might be to always have flush=True as default and not be able to change it.

S0d4Dev commented 1 week ago

I guess using kandinsky to resolve this is an option we can use now. Unfortunately, we can't really go back to the first lines, and making it possible with kandinsky will loose both a lot of time to develop and space on the device. But again, unlike Classic Python, this version of Python for embedded devices does not have the flush capabilities by default (which you can try here), so I do not know how hard or at which cost (in terms of space) it will be implemented.

mobluse commented 1 week ago

At https://micropython.org/unicorn/ I tried:

import time
def message(message, delay) :
    for letter in message :
        print(end=letter)
        time.sleep(delay)
message("Hello, world.", 0.2)

And it works as expected because it has flush=True by default.