When entering a command in IPython console using multi-line input, like creating an instance of an object with several parameters, I might enter the parameters on separate lines like:
In [1]: post = Post("post title",
...: "Author name",
...: "This is the content and it is the last parameter");
However, while entering the post content I realize that I mistyped something in the post title on line 1 of my multi-line input. Is there a way to go back and correct a previous line?
Alternatively, is there a way to simply abandon the input altogether and stop entering the command without python attempting to interpret my partially entered command?
Related
The code:
input("Type your input here:)
displays as:
Type your input here:
I want to automatically populate the input window with text that can be cleared by pressing backspace so the display looks like:
Type your input here: DEFAULT
and after pressing backspace 3 times the user would see:
Type your input here: DEFA
Other posts have indicated that this isn't something you can do in, say, bash, but is there a way to do this in Python?
Simple answer: No. It's just not something you can do in a console app. What's commonly done is to display the default you'll get if you press return:
Type your input here [DEFAULT]:
I am trying to create a Twitter bot that posts a random line from a text file. I have gone as far as generating the random lines, which print one at a time, and giving the bot access to my Twitter app, but I can't for the life of me figure out how to use a printed line as a status.
I am using Tweepy. My understanding is that I need to use api.update_status(status=X), but I don't know what X needs to be for the status to match the most recently printed line.
This is the relevant section of what I have so far:
from random import choice
x = 1
while True:
file = open('quotes.txt')
content = file.read()
lines = content.splitlines()
print(choice(lines))
api.update_status(status=(choice(lines)))
time.sleep(3600)
The bot is accessing Twitter no problem. It is currently posting another random quote generated by (choice(lines)), but I'd like it to match what prints immediately before.
I may not fully understand your question, but from the very top, where it says, "How to use the most recently printed line as an input", I think I can answer that. Whenever you use the print() command, store the argument into a string variable that overwrites its last value. Then it saves the last printed value.
Instead of directly printing a choice:
print(choice(lines))
create a new variable and use it in your print() and your api.update_status():
selected_quote = choice(lines)
print(selected_quote)
api.update_status(status=selected_quote)
I am not very experienced with python, just trying to make a modification to the code of a program I purchased.
The code runs in the command terminal and its function is to gather data. It gives you the option of gathering the data in either csv or json. It prompts you once with asking which format and a second time for confirmation.
Please enter output format (csv/json)
Do you want to extract data in {} format? (y/n)
I am trying to change this to just do csv by default, and not be prompted for either choosing or confirming
I believe the relevant code from the program is the following ( this isn't all consecutive ). How do I alter the first part to just be 'csv' and do I need to do anything beyond deleting the entire def getConfirmMessage block to wipe that from the code?
if spiderToExecute in range(1, 7):
while True:
outputFormat = click.prompt('Please enter output format (csv/json)', default='csv')
outputFormat = ''.join(outputFormat).lower()
if outputFormat in ['json', 'csv']:
break
settings.set('FEED_FORMAT', outputFormat)
settings.set('FEED_URI', './data/{}.{}'.format(spiderConf['log_output_name_format'], outputFormat))
settings.set('LOG_FILE', './log/{}.log'.format(spiderConf['log_output_name_format']))
def getConfirmMessage(spiderToExecute, outputFormat):
confirmMessages = {
1: 'Do you want to extract data from in {} format ?'.format(outputFormat),
2: 'Do you want to extract data from in {} format ?'.format(outputFormat),
}
return confirmMessages[spiderToExecute]
edit: more code
if not click.confirm(getConfirmMessage(spiderToExecute, outputFormat), default=True):
click.echo()
click.secho(' Please relaunch the command to select new options! ', bg='red')
click.echo()
raise click.Abort()
Complete steps (maybe):
Remove the while loop and replace with outputFormat = "csv".
You should be able to completely remove the code block you posted (if not click.confirm...). If that doesn't work, you'll need to post the code for click.confirm.
I've been working on a general utility script for a while now that basically just accepts user input to preform some task like opening a program. In this program, I define a name "command" as a raw_input and then use if statements to check the list for a command (small example below).
Constantly using if statements is making the program run slowly and so I'm wondering if there is a better way such as maybe a table of commands? I'm pretty new to programming so not sure how to accomplish this.
import os
command = raw_input('What would you like to open:')
if 'skype' in command:
os.chdir('C:\Program Files (x86)\Skype\Phone')
os.startfile('Skype.exe')
You can keep the commands in a dictionary with a tuple, and do something like this to store the commands.
command = {}
command['skype'] = 'C:\Program Files (x86)\Skype\Phone', 'Skype.exe'
command['explorer'] = 'C:\Windows\', 'Explorer.exe'
You could then do the following to execute the correct command based on the user input.
if raw_input.lower().strip() in command: # Check to see if input is defined in the dictionary.
os.chdir(command[raw_input][0]) # Gets Tuple item 0 (e.g. C:\Program Files.....)
os.startfile(command[myIraw_inputput][1]) # Gets Tuple item 1 (e.g. Skype.exe)
You can find more information on Dictionaries and Tuples here.
In case you need to allow multiple commands, you can separate them by a space and split the commands into an array.
for input in raw_input.split():
if input.lower().strip() in command: # Check to see if input is defined in the dictionary.
os.chdir(command[input][0]) # Gets Tuple item 0 (e.g. C:\Program Files.....)
os.startfile(command[input][4]) # Gets Tuple item 1 (e.g. Skype.exe)
This would allow you to issue commands like skype explorer, but keep in mind that there are no room for typos, so they need to be an exact match, separated with nothing but white-spaces. As an example you could write explorer, but not explorer!.
I want to write a program which will print a string every second on the other hand it will allow user to write text, I have the code snippet below. My problem is everytime a new line is printed, input line is also disturbed. Is there a way to seperate the output lines from the input line?
import time
from thread import start_new_thread
def heron():
while 1:
time.sleep(1)
print "some text"
start_new_thread(heron,())
c = raw_input("Enter text>")
I doubt you can do this without curses. There might be another way, but I don't think it would be very pretty. There's a basic how-to here.