Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
readline trick needed
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  20 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Steven D'Aprano  
View profile  
 More options Oct 13 2012, 9:30 am
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Date: 13 Oct 2012 13:30:14 GMT
Local: Sat, Oct 13 2012 9:30 am
Subject: readline trick needed
I'm working with the readline module, and I'm trying to set a key
combination to process the current command line by calling a known
function, *and* enter the command line.

Something along the lines of:

* execute function spam() in some context where it can access
  the current command line as a string
* enter the command line

Function spam() may or may not modify the command line.

Here is what I have got so far: I can discard the current line and call a
function:

readline.parse_and_bind(r'"\C-p": "%cspam()\n"' % 0x15)  # ^U

binds ctrl-P to the key combinations `ctrl-U spam() Enter`, which clears
the command line before entering spam().

If I leave out the ctrl-U, I'll get a SyntaxError or other exception,
e.g. command line `x = 123` gets transformed into `x = 123spam()`.

This is not suitable:

readline.parse_and_bind(r'"\C-p": "; spam()\n"')

because it changes the command line. It's okay for spam() itself to
modify the command line, but the key binding should not.

I tried to do this:

readline.parse_and_bind(r'"\C-p": "\nspam()\n"')

but it gives me a segmentation fault, which is a little less helpful than
I had expected.

This Stackoverflow question suggests that what I want is not possible in
vanilla Python:

http://stackoverflow.com/questions/11680356

but I'm a stubborn guy and I have not given up yet. Any suggestions?

(P.S. I'm aware of IPython, I want to get this working in the standard
CPython interpreter.)

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Etienne Robillard  
View profile  
 More options Oct 13 2012, 9:43 am
Newsgroups: comp.lang.python
From: Etienne Robillard <animelo...@gmail.com>
Date: Sat, 13 Oct 2012 09:44:04 -0400
Local: Sat, Oct 13 2012 9:44 am
Subject: Re: readline trick needed
On 13 Oct 2012 13:30:14 GMT

Why dont you grow yourself some usable neurons instead ? Don't you realize now stackoverflow.com is starting
to hurt your capacity to cogitate on your own or have you not realized this yet?

Cheers,

Etienne

--
Etienne Robillard
Green Tea Hackers Club
Fine Software Carpentry For The Rest Of Us!
http://gthc.org/
e...@gthcfoundation.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Oct 13 2012, 9:47 am
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Sun, 14 Oct 2012 00:47:52 +1100
Local: Sat, Oct 13 2012 9:47 am
Subject: Re: readline trick needed
On Sun, Oct 14, 2012 at 12:44 AM, Etienne Robillard

<animelo...@gmail.com> wrote:
> Why dont you grow yourself some usable neurons instead ? Don't you realize now stackoverflow.com is starting
> to hurt your capacity to cogitate on your own or have you not realized this yet?

Excuse me?

I'm not overly familiar with readline, so perhaps there is a really
obvious way to do what Steven's trying to do, but this post does not
appear to be the result of a lack of thinking.

If it really IS that obvious to you, post a link to appropriate
documentation without the rudeness... that way it'll be useful to
everyone, not just cathartic to you.

ChrisA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Etienne Robillard  
View profile  
 More options Oct 13 2012, 9:57 am
Newsgroups: comp.lang.python
From: Etienne Robillard <animelo...@gmail.com>
Date: Sat, 13 Oct 2012 09:58:11 -0400
Local: Sat, Oct 13 2012 9:58 am
Subject: Re: readline trick needed
On Sun, 14 Oct 2012 00:47:52 +1100

Chris Angelico <ros...@gmail.com> wrote:
> Excuse me?

> I'm not overly familiar with readline, so perhaps there is a really
> obvious way to do what Steven's trying to do, but this post does not
> appear to be the result of a lack of thinking.

> If it really IS that obvious to you, post a link to appropriate
> documentation without the rudeness... that way it'll be useful to
> everyone, not just cathartic to you.

> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list

whatever. i don't feel much like replying to idiots today so your apologies
and useless if not irrelevant.

--
Etienne Robillard
Green Tea Hackers Club
Fine Software Carpentry For The Rest Of Us!
http://gthc.org/
e...@gthcfoundation.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roel Schroeven  
View profile  
 More options Oct 13 2012, 5:15 pm
Newsgroups: comp.lang.python
From: Roel Schroeven <r...@roelschroeven.net>
Date: Sat, 13 Oct 2012 23:14:44 +0200
Local: Sat, Oct 13 2012 5:14 pm
Subject: Re: readline trick needed
Etienne Robillard schreef:

Then simply don't. Much better then replying in such a rude way.

I leave the question of who is being an idiot here as an exercise to the
reader.

--
"Too often we hold fast to the cliches of our forebears. We subject all
facts to a prefabricated set of interpretations. Too often we enjoy the
comfort of opinion without the discomfort of thought."
         -- John F Kennedy

r...@roelschroeven.net


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Oct 13 2012, 5:45 pm
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Sun, 14 Oct 2012 08:44:59 +1100
Local: Sat, Oct 13 2012 5:44 pm
Subject: Re: readline trick needed
On Sun, Oct 14, 2012 at 8:31 AM, Joshua Landau

<joshua.landau...@gmail.com> wrote:
> With two irritants (including 88888), is it not advisable that python-list
> gets an admin to block these accounts? Even if it does nothing more than
> slow them, that's something.

That's what killfiles are for. You have two options:

http://en.wikipedia.org/wiki/Kill_file
http://bofh.ch/bofh/bsmh2.html

The first option is not perfect, as you'll still see replies that
quote such people's posts. The second has a few issues with local law
enforcement, but other than that, is a very effective means of
avoiding seeing their posts.

ChrisA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Lawrence  
View profile  
 More options Oct 13 2012, 6:12 pm
Newsgroups: comp.lang.python
From: Mark Lawrence <breamore...@yahoo.co.uk>
Date: Sat, 13 Oct 2012 23:13:24 +0100
Local: Sat, Oct 13 2012 6:13 pm
Subject: Re: readline trick needed
On 13/10/2012 22:31, Joshua Landau wrote:

> With two irritants (including 88888), is it not advisable that python-list
> gets an admin to block these accounts? Even if it does nothing more than
> slow them, that's something.

Most irritants are mere amateurs compared to Ilias Lazaridis.  I wonder
if he's *STILL* researching?

--
Cheers.

Mark Lawrence.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Oct 13 2012, 6:41 pm
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Sun, 14 Oct 2012 09:41:13 +1100
Local: Sat, Oct 13 2012 6:41 pm
Subject: Re: readline trick needed
On Sun, Oct 14, 2012 at 9:25 AM, Joshua Landau

Agreed, defending newbies is important. But not everyone gets
frustrated at trolls. Those who don't, don't bother to killfile them,
and can respond in defense of the newbies. Also, if two or three
non-trolls respond to the original post, the angry troll is only one
of several, which helps dilute the problem a bit. Still not a
solution, but it helps.

ChrisA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Lawrence  
View profile  
 More options Oct 13 2012, 6:52 pm
Newsgroups: comp.lang.python
From: Mark Lawrence <breamore...@yahoo.co.uk>
Date: Sat, 13 Oct 2012 23:52:57 +0100
Local: Sat, Oct 13 2012 6:52 pm
Subject: Re: readline trick needed
On 13/10/2012 23:26, Joshua Landau wrote:

Oh no he's far higher up the food chain than that.  Here's a starter
https://groups.google.com/forum/?fromgroups=#!search/lazaridis$20bann...

--
Cheers.

Mark Lawrence.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Lawrence  
View profile  
 More options Oct 13 2012, 8:31 pm
Newsgroups: comp.lang.python
From: Mark Lawrence <breamore...@yahoo.co.uk>
Date: Sun, 14 Oct 2012 01:32:01 +0100
Local: Sat, Oct 13 2012 8:32 pm
Subject: Re: readline trick needed
On 13/10/2012 23:52, Mark Lawrence wrote:

Yes a real pro.  Trolling will never become an Olympic sport as the
guy's so good nobody can compete with him.  (If he was American they'd
still have a World Series though :)  Throw his name plus Eclipse,
Netbeans and banned into your search engine of choice and enjoy, but
beware that to really endulge yourself please stock up on vast
quantities of caffeine and sandwiches first cos you'll need them.  For
example this http://web.archiveorange.com/archive/v/7IfPlywrYZb0gAgMsPa1
points to this http://www.tfeb.org/lisp/mad-people.html

--
Cheers.

Mark Lawrence.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Oct 16 2012, 4:29 am
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Tue, 16 Oct 2012 10:30:01 +0200
Local: Tues, Oct 16 2012 4:30 am
Subject: [on topic] Re: readline trick needed

Steven D'Aprano wrote:
> I'm working with the readline module, and I'm trying to set a key
> combination to process the current command line by calling a known
> function, *and* enter the command line.

> Something along the lines of:

> * execute function spam() in some context where it can access
>   the current command line as a string
> * enter the command line

> Function spam() may or may not modify the command line.
> (P.S. I'm aware of IPython, I want to get this working in the standard
> CPython interpreter.)

If IPython does what you want why don't you have a look at the source?

Anyway, here's what I came up with (no warranties as it was all trial-and-
error):

$ cat readline_callback.py
import ctypes
import ctypes.util
from ctypes import c_int, c_char_p
import readline
import sys

rlname = ctypes.util.find_library("readline")

# int rl_add_defun (const char *name, rl_command_func_t *function, int key)
# typedef int rl_command_func_t (int, int);
# int rl_done
# char * rl_line_buffer

rl = ctypes.CDLL(rlname)

RL_COMMAND_FUNC = ctypes.CFUNCTYPE(c_int, c_int, c_int)

intercepted_line = None
def lovely_spam(a, b):
    global intercepted_line
    print
    intercepted_line = c_char_p.in_dll(rl, "rl_line_buffer").value
    rl.rl_insert_text("*" + intercepted_line)
    c_int.in_dll(rl, "rl_done").value = 1
    return 0

def control(c):
    return ord(c.upper())-64

rl.rl_add_defun("lovely-spam", RL_COMMAND_FUNC(lovely_spam), control("P"))

#rl.rl_add_defun("lovely-spam", RL_COMMAND_FUNC(lovely_spam), -1)
#readline.parse_and_bind("Control-P: lovely-spam")

$ python -i readline_callback.py

>>> alpha = 42
>>> alpha

1764

The relevant documentation:
http://cnswww.cns.cwru.edu/php/chet/readline/readline.html
http://docs.python.org/library/ctypes.html


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ned Deily  
View profile  
 More options Oct 16 2012, 4:50 am
Newsgroups: comp.lang.python
From: Ned Deily <n...@acm.org>
Date: Tue, 16 Oct 2012 01:50:17 -0700
Local: Tues, Oct 16 2012 4:50 am
Subject: Re: [on topic] Re: readline trick needed
In article <k5j5sk$ms...@ger.gmane.org>, Peter Otten <__pete...@web.de>
wrote:
> Steven D'Aprano wrote:
> > I'm working with the readline module, and I'm trying to set a key
> > combination to process the current command line by calling a known
> > function, *and* enter the command line.
[...]
> Anyway, here's what I came up with (no warranties as it was all trial-and-
> error):

[...]

Keep in mind that the Python readline module may be linked to either the
GPL-licensed GNU readline or the BSD-licensed editline (libedit) library
(the default on newer OS X systems and probably on *BSD systems) and
they have different command strings.  Note the warning here:

http://docs.python.org/py3k/library/readline.html

Or it may not be linked with either.

--
 Ned Deily,
 n...@acm.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Oct 16 2012, 6:20 am
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Tue, 16 Oct 2012 12:20:33 +0200
Local: Tues, Oct 16 2012 6:20 am
Subject: Re: [on topic] Re: readline trick needed

I think you are a Mac user. If so, can the snippet I posted be generalised
to work with libedit?

> Or it may not be linked with either.

OK, change "no warranties" to "absolutely no warranties" ;)

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Oct 16 2012, 7:27 am
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Date: 16 Oct 2012 11:27:37 GMT
Local: Tues, Oct 16 2012 7:27 am
Subject: Re: [on topic] Re: readline trick needed

Well, I was hoping for a pure Python solution, rather than having to
troll through who knows how many thousands of lines of code in a language
I can barely read.

> Anyway, here's what I came up with (no warranties as it was all
> trial-and- error):

> $ cat readline_callback.py
> import ctypes

Ah, ctypes.

Well, I guess I have some interesting study ahead of me, to make head or
tail of your solution. Thank you!

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Kern  
View profile  
 More options Oct 16 2012, 8:20 am
Newsgroups: comp.lang.python
From: Robert Kern <robert.k...@gmail.com>
Date: Tue, 16 Oct 2012 13:20:24 +0100
Local: Tues, Oct 16 2012 8:20 am
Subject: Re: [on topic] Re: readline trick needed
On 10/16/12 12:27 PM, Steven D'Aprano wrote:

Are you confusing IPython, the pure Python REPL for CPython, for IronPython, the
C# implementation of Python?

   https://github.com/ipython/ipython

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Oct 16 2012, 8:52 am
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Date: 16 Oct 2012 12:52:05 GMT
Local: Tues, Oct 16 2012 8:52 am
Subject: Re: [on topic] Re: readline trick needed

On Tue, 16 Oct 2012 13:20:24 +0100, Robert Kern wrote:
> On 10/16/12 12:27 PM, Steven D'Aprano wrote:
>> Well, I was hoping for a pure Python solution, rather than having to
>> troll through who knows how many thousands of lines of code in a
>> language I can barely read.

> Are you confusing IPython, the pure Python REPL for CPython,

IPython is pure Python?

Well, you learn something new everyday. Or at least I do.

I guess that means that I can look forward to trolling through 250 or so
Python modules instead of C code :)

Thanks to everyone who replied.

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dwight Hutto  
View profile  
 More options Oct 16 2012, 10:15 am
Newsgroups: comp.lang.python
From: Dwight Hutto <dwightdhu...@gmail.com>
Date: Tue, 16 Oct 2012 10:14:54 -0400
Local: Tues, Oct 16 2012 10:14 am
Subject: Re: [on topic] Re: readline trick needed
On Tue, Oct 16, 2012 at 7:27 AM, Steven D'Aprano

<steve+comp.lang.pyt...@pearwood.info> wrote:
> On Tue, 16 Oct 2012 10:30:01 +0200, Peter Otten wrote:

>> Steven D'Aprano wrote:

>>> I'm working with the readline module, and I'm trying to set a key
>>> combination to process the current command line by calling a known
>>> function, *and* enter the command line.

>>> Something along the lines of:

>>> * execute function spam() in some context where it can access
>>>   the current command line as a string
>>> * enter the command line

I'm working on the dictionary now,but I came up with this, which uses
a list as the key, and accepts the params to perform the function:

import subprocess as sub
key_list = ['print_something','espeak']

def print_something(params):
        print "%s" % (params)

def espeak(params):
        sub.call(['espeak','%s' % (params)])

key = raw_input("Please enter key: ")

for line in key_list:
        if str(line) == key:
                params = raw_input("Enter Params: ")
                eval("%s('%s')" % (key,params))

I keep getting the function performed in the dict. I'll figure it out
eventually, I know I've done it before, and it might be a lambda
solution...not sure.

But the above could be refined more, it just uses a list, and key/params.

--
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to ", was Re: [on topic] Re: readline trick needed" by Peter Otten
Peter Otten  
View profile  
 More options Oct 16 2012, 10:36 am
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Tue, 16 Oct 2012 16:36:56 +0200
Local: Tues, Oct 16 2012 10:36 am
Subject: [off topic], was Re: [on topic] Re: readline trick needed

Dwight Hutto wrote:

I knew I'd eventually regret putting "on topic" into the subject...

Well done, Dwight.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dwight Hutto  
View profile  
 More options Oct 16 2012, 11:14 am
Newsgroups: comp.lang.python
From: Dwight Hutto <dwightdhu...@gmail.com>
Date: Tue, 16 Oct 2012 11:14:11 -0400
Local: Tues, Oct 16 2012 11:14 am
Subject: Re: [off topic], was Re: [on topic] Re: readline trick needed

On Tue, Oct 16, 2012 at 10:36 AM, Peter Otten <__pete...@web.de> wrote:
> Dwight Hutto wrote:

> I knew I'd eventually regret putting "on topic" into the subject...

I didn't write that. If you're referring to OT, it means Off Topic,
and a response would be appreciated. And you can call me David, I go
by my middle name.

--
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "readline trick needed" by Ned Deily
Ned Deily  
View profile  
 More options Oct 16 2012, 4:45 pm
Newsgroups: comp.lang.python
From: Ned Deily <n...@acm.org>
Date: Tue, 16 Oct 2012 13:44:45 -0700
Local: Tues, Oct 16 2012 4:44 pm
Subject: Re: [on topic] Re: readline trick needed
In article <k5jcbs$eu...@ger.gmane.org>, Peter Otten <__pete...@web.de>
wrote:

> Ned Deily wrote:
> > Keep in mind that the Python readline module may be linked to either the
> > GPL-licensed GNU readline or the BSD-licensed editline (libedit) library
> > (the default on newer OS X systems and probably on *BSD systems) and
> > they have different command strings.  Note the warning here:

> > http://docs.python.org/py3k/library/readline.html

> I think you are a Mac user. If so, can the snippet I posted be generalised
> to work with libedit?

Isn't it hairy enough??

Here's how to make tab complete work with both; it gives an idea of the
libedit commands:

     import rlcompleter
     if 'libedit' in readline.__doc__:
         readline.parse_and_bind("bind ^I rl_complete")
     else:  # GNU readline format
         readline.parse_and_bind("tab: complete")

The documentation of the command syntax is in the .editrc man pages, for
example:

https://developer.apple.com/library/mac/#documentation/Darwin/Referen...
anPages/man5/editrc.5.html

> > Or it may not be linked with either.
> OK, change "no warranties" to "absolutely no warranties" ;)

Good idea.

--
 Ned Deily,
 n...@acm.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »