Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding flush to Python #2239

Open
S0d4Dev opened this issue Oct 1, 2024 · 3 comments
Open

Adding flush to Python #2239

S0d4Dev opened this issue Oct 1, 2024 · 3 comments
Assignees

Comments

@S0d4Dev
Copy link

S0d4Dev commented Oct 1, 2024

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
Copy link

mobluse commented Oct 9, 2024

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
Copy link
Author

S0d4Dev commented Oct 10, 2024

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
Copy link

mobluse commented Oct 10, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants