LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-30-2010, 10:39 AM   #1
General
Member
 
Registered: Aug 2005
Distribution: Debian 7
Posts: 526

Rep: Reputation: 31
Python: find-and-replace while typing in raw_input


I'm writing a program in Python that presents a place for the user to input one-line of text (using raw_input).

I need, however, as the user types, for certain words to be replaced, even before the user hits enter.

I try to make an animation so you can see what I mean, here the word "pig" gets replaced with "sheep":

>I|
>I |
>I l|
>I li|
>I lik|
>I like|
>I like |
>I like t|
>I like to|
>I like to |
>I like to e|
>I like to ea|
>I like to eat|
>I like to eat |
>I like to eat p|
>I like to eat pi|
>I like to eat pig|
>I like to eat sheep|

Note, the user never typed "sheep", the program replaced "pig" with "sheep" as soon as they finished typing the word "pig". The user did not use "Enter".

Is this at all possible in Python?

Last edited by General; 04-30-2010 at 10:40 AM.
 
Old 05-01-2010, 06:44 AM   #2
hda7
Member
 
Registered: May 2009
Distribution: Debian wheezy
Posts: 252

Rep: Reputation: 31
It should be trivial to write a program like that in python, but most likely, if you run it on a terminal, the input will be buffered: your program will only get the input after the user hits enter. You could try looking at "man termios(3)" I think icanon is the option that needs to be turned off.
 
Old 05-02-2010, 02:28 AM   #3
General
Member
 
Registered: Aug 2005
Distribution: Debian 7
Posts: 526

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by hda7 View Post
It should be trivial to write a program like that in python, but most likely, if you run it on a terminal, the input will be buffered: your program will only get the input after the user hits enter. You could try looking at "man termios(3)" I think icanon is the option that needs to be turned off.
The man page is a bit over my head. Is this something that changes the behavior of the terminal for all applications or something that I can run from within python? I only need this behavior in my single application.

Would this be easier to impliment if I moved to something like ncurses? The entire rest of the application is just menus where I just type a number and hit enter to make a selection.
 
Old 05-02-2010, 07:30 AM   #4
hda7
Member
 
Registered: May 2009
Distribution: Debian wheezy
Posts: 252

Rep: Reputation: 31
OK, I found out that Python has two libraries for dealing with terminal control: tty and termios. termios is similar to the C library function. tty is used for a few commonly used settings. Try this:
Code:
import tty
tty.setraw(0)
 
Old 05-03-2010, 09:06 PM   #5
General
Member
 
Registered: Aug 2005
Distribution: Debian 7
Posts: 526

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by hda7 View Post
OK, I found out that Python has two libraries for dealing with terminal control: tty and termios. termios is similar to the C library function. tty is used for a few commonly used settings. Try this:
Code:
import tty
tty.setraw(0)
Okay, this works. Thanks much!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
is it possible to override the raw_input() command in python deathalele Programming 1 10-05-2008 04:26 PM
If no raw_input: (Python question) xadrith Programming 4 07-26-2008 07:21 AM
Suspending operations with raw_input (Python) xadrith Programming 1 07-24-2008 05:09 PM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM
Python: raw_input & puTTy on remote server Hairein Programming 1 01-01-2007 08:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration