Evaluating Promt in a Python interactive shell before displaying a tooltip

It would be very convenient if the Python interactive shell displayed the current time each time the invitation was updated . I’m thinking about setting up my invitation to something like: sys.ps1 = str (datetime.datetime.now (). Time (). Isoformat () [: 8])

Since the invitation is not evaluated every time it is displayed, it only displays the time the shell was created and will not update it throughout its life.

I use version 3.5.1 more often - in case it matters.

Is there a way to get the shell to evaluate the prompt just before every time the prompt is displayed ?

Thanks for your answers and time.

+4
source share
1 answer
import sys
import datetime

class Prompt():
    def __str__(self):
        return str(datetime.datetime.now().time().isoformat()[:8])

sys.ps1 = Prompt()

https://docs.python.org/2/library/sys.html

If a non-string object is assigned to either variable, its str()
is re-evaluated each time the interpreter prepares to read a
new interactive command; this can be used to implement a
dynamic prompt.
+1
source

Source: https://habr.com/ru/post/1661517/


All Articles