Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomly change the prompt in the Python interpreter

It's kind of boring to always see the >>> prompt in Python. What would be the best way to go about randomly changing the prompt prefix?

I imagine an interaction like:

This is a tobbaconist!>> import sys
Sorry?>> import math
Sorry?>> print sys.ps1
Sorry?
What?>>
like image 732
mircealungu Avatar asked Nov 12 '15 10:11

mircealungu


People also ask

What is interpreter prompt in Python?

Python is an interpreter language. It means it executes the code line by line. Python provides a Python Shell, which is used to execute a single Python command and display the result.

What is interpreter prompt?

We can use the Python interpreter to run our programs as and when we write the code. First, we have to open a command line. Linux/BSD/Mac users can open up a shell emulator of their choice such as konsole or gnome-terminal.

How do you use interactive mode in Python?

The interactive Python mode lets you run your script instantly via the command line without using any code editor or IDE. To run a Python script interactively, open up your command line and type python. Then hit Enter. You can then go ahead and write any Python code within the interactive mode.

How do I get out of interactive mode in Python?

Go to File -> New Window (Ctrl+N) and a window where you can write your program will pop up.


4 Answers

According to the docs, if you assign a non-string object to sys.ps1 then it will evaluate the str function of it each time:

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.

Well now it's obvious, you should make it dynamic! Make an object with a __str__ method where you can place any logic you want:

class Prompt:
    def __str__(self):
        # Logic to randomly determine string
        return string

You can also make changes or insert things into this class as you go too. So for example, you could have a list of messages in Prompt that you append to, or change, and that will affect the console message.

like image 69
SuperBiasedMan Avatar answered Oct 01 '22 15:10

SuperBiasedMan


Try this:

>>> import sys
>>> import random
>>> class RandomPrompt(object):
...     prompts = 'hello >', 'hi >', 'hey >'
...     def __repr__ (self): return random.choice(self.prompts)
... 
>>> sys.ps1 = RandomPrompt()
hello >1
1
hi >2
2
like image 34
Klaus D. Avatar answered Oct 01 '22 16:10

Klaus D.


For changing the prompt, we use

>>>import sys
>>>sys.ps1 = '=>'
=>

Now the way to do it randomly would be something like this:

import random
import sys

random_prompts = ['->', '-->', '=>', 'Hello->']
sys.ps1 = random.choice(random_prompts)

To execute this when your python interpreter starts, you can follow this guide: https://docs.python.org/2/tutorial/appendix.html#the-interactive-startup-file

like image 36
Yash Mehrotra Avatar answered Oct 01 '22 16:10

Yash Mehrotra


Nice question. The >>> prompt is in sys.ps1, the ... in sys.ps2. The next question would be how to change this randomly. Just as a demonstration of changing it by hand:

>>> import sys
>>> sys.ps1 = '<<<'
<<<sys.ps1 = '<<< '
<<< sys.ps2 = '.?. '
<<< for i in line:
.?. 
like image 22
serv-inc Avatar answered Oct 01 '22 16:10

serv-inc