[FRPythoneers] Shell commands in interactive mode

cary miller cmiller at math.cudenver.edu
Wed Jul 25 15:05:42 MDT 2001


On Wed, 25 Jul 2001, Eric Brunson wrote:

>
> I think the only way to get close to what you want to do would require
> subclassing the parser and completely rewriting it since only keywords
> can be used without parens, and only some of them.  It is, afterall, a
> python shell expecting you to type python.

Yes, what I am doing kind of violates all kinds of good practice I'm sure.
Typing in a variable name and getting this kind of behavior is very
abusive but it's fun (and useful).


>
> At the most, I might see enhancing the interactive mode to support
> shell escapes using a "!" or similar, but beyond that you'd just be
> rewriting bash.  It seems a bit odd that you'd need that much shell
> functionality within the interpretter, I would just open another
> window.

You are right I do not need it.  But I thought of it, did not know haw to
do it and kept thinking about it.  Is this what you mean by shell escape?

class Shell:
    def __repr__(self):
        eval('os.system(raw_input())')
        return ''

sh = Shell()



>
> * cary miller (cmiller at math.cudenver.edu) [010725 13:34]:
> > That will get you arbitrary shell commands but what I would like to have
> > is completely transparent access to the shell without qoute marks or any
> > python commands intervening.  It's probably a mad dream but fun to think
> > about.  Here is another idea.
> >
> > >>> sh cat foo | grep bar > baz.py
> > for example.
> > You precede the shell command with sh (an undeclared variable).
> > You have code that looks for NameError: sh
> > When such an error is found the rest of the line gets executed with the
> > command module or something.
> > I think it would work but it seems a bit fishy so I have not tried it.
> >
> > cam
> >
> > On Wed, 25 Jul 2001, Eric Brunson wrote:
> >
> > >
> > > Try reading about commands module and see if that does what you need
> > > it to do.
> > >
> > > http://python.org/doc/current/lib/module-commands.html
> > >
> > > * cary miller (cmiller at math.cudenver.edu) [010725 13:06]:
> > > > Hi
> > > >
> > > > In tcl interactive mode you have access to shell commands like pwd and
> > > > ls.  I wanted python to do that and came up with the following.  This
> > > > code goes in pythonstartup.  The nice thing about it is that you just
> > > > type
> > > > >>> ls
> > > > or
> > > > >>> pwd
> > > > and get exactly what you would see if you typed the same in the shell.
> > > >
> > > > Problems:
> > > >     1.  Blank line after the output.  The problem here is __repr__ is
> > > >     required to return a string.  Is there any way around this?
> > > >
> > > >     2.  Only works for one-word commands.  Cannot pass options to ls,
> > > >     and cannot do this
> > > >
> > > > >>> cat foo.py
> > > >
> > > >     Is there a way to do multiword shell commands??
> > > >
> > > > import os
> > > >
> > > > class ShellCommand:
> > > >     def __init__(self,cmd):
> > > >         self.cmd=cmd
> > > >     def __repr__(self):
> > > >         eval('os.system(self.cmd)')
> > > >         return ''
> > > >
> > > > pwd = ShellCommand('pwd')
> > > > ls = ShellCommand('ls')
> > > >
> > > >
> > > > Here is the best I aan come up with for cat.
> > > >
> > > >
> > > > def cat(file):
> > > >     for line in open(file).readlines():
> > > >         print line,
> > > >
> > > >
> > > >
> > > > --
> > > > Cary Miller
> > > > Math Deparment
> > > > University of Colorado - Denver
> > > > cmiller at math.cudenver.edu
> > > > http://www-math.cudenver.edu/~cmiller
> > > >
> > > > _______________________________________________
> > > > This message sent by the FRPythoneers mailing list.
> > > > Unsubscribe: echo unsubscribe | FRPythoneers-request at lists.community.tummy.com
> > > > URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers
> > >
> > >
> > >
> >
> > --
> > Cary Miller
> > Math Deparment
> > University of Colorado - Denver
> > cmiller at math.cudenver.edu
> > http://www-math.cudenver.edu/~cmiller
> >
> > _______________________________________________
> > This message sent by the FRPythoneers mailing list.
> > Unsubscribe: echo unsubscribe | FRPythoneers-request at lists.community.tummy.com
> > URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers
>
>
>

-- 
Cary Miller
Math Deparment
University of Colorado - Denver
cmiller at math.cudenver.edu
http://www-math.cudenver.edu/~cmiller




More information about the FRPythoneers mailing list