By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,761 Members | 958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,761 IT Pros & Developers. It's quick & easy.

More like a shell command.

P: n/a
Is there anyway I can extend python to accept a command
which looks more like shell syntax than a function call.

I want to be able to do this:

if blah :
MyCommand Arg1 Arg2

as opposed to this:

if blah :
MyCommand(Arg1,Arg2)

or this:

if blah :
x("MyCommand Arg1 Arg2")

Of source, I would like to do this by writing a module (or through
some other run-time hook) as opposed to editing the Python source
code.

Thanks in advance
(unless you are just a Python nut who is flaming to tell me that I
should not want this. :-) )

Bill
Aug 6 '08 #1
Share this Question
Share on Google+
4 Replies

P: n/a
On Aug 6, 9:38*am, Bill <galaxyblu...@gmail.comwrote:
Is there anyway I can extend python to accept a command
which looks more like shell syntax than a function call.

I want to be able to do this:

* * if blah :
* * * * MyCommand *Arg1 *Arg2

as opposed to this:

* * if blah :
* * * * MyCommand(Arg1,Arg2)

or this:

* * if blah :
* * * * x("MyCommand *Arg1 *Arg2")

Of source, I would like to do this by writing a module (or through
some other run-time hook) as opposed to editing the Python source
code.

Thanks in advance
(unless you are just a Python nut who is flaming to tell me that I
should not want this. *:-) *)

Bill
I'm not aware of any way to do this without modifying the original
source in some fundamental way. You may be able to use some
metaprogramming techniques, like decorators, to achieve this, although
I'm not thinking of anything clever at the moment.

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org
Aug 6 '08 #2

P: n/a
Hello,
Is there anyway I can extend python to accept a command
which looks more like shell syntax than a function call.

I want to be able to do this:

* * if blah :
* * * * MyCommand *Arg1 *Arg2

as opposed to this:

* * if blah :
* * * * MyCommand(Arg1,Arg2)

or this:

* * if blah :
* * * * x("MyCommand *Arg1 *Arg2")

Of source, I would like to do this by writing a module (or through
some other run-time hook) as opposed to editing the Python source
code.
You might want to have a look at ipython, they do something like that
but for the command interpreter and not the compiler.

The other option will be to write a compiler from your syntax to valid
Python syntax. However this my not be simple since you need to know
the context. e.g. is f("a b c") calling f in the string "a b c" or
f(a, b, c)?

HTH,
--
Miki <mi*********@gmail.com>
http://pythonwise.blogspot.com
Aug 6 '08 #3

P: n/a
Maybe this module would work fine:

http://docs.python.org/lib/module-cmd.html

--
Angel
Aug 6 '08 #4

P: n/a
On Aug 6, 9:38*am, Bill <galaxyblu...@gmail.comwrote:
Is there anyway I can extend python to accept a command
which looks more like shell syntax than a function call.

I want to be able to do this:

* * if blah :
* * * * MyCommand *Arg1 *Arg2

as opposed to this:

* * if blah :
* * * * MyCommand(Arg1,Arg2)

or this:

* * if blah :
* * * * x("MyCommand *Arg1 *Arg2")

Of source, I would like to do this by writing a module (or through
some other run-time hook) as opposed to editing the Python source
code.

Thanks in advance
(unless you are just a Python nut who is flaming to tell me that I
should not want this. *:-) *)

Bill
Bill,

You'll need to decide on a grammar you want to use beforehand. What
do you do with:
>>if f 0 and g
?

Does it translate to:

if f( 0 ) and g( )

or

if f( 0 and g )

? If every line starts with its one and only function, and the
parameters exhaust the rest of the line, and you don't allow nested
expressions, you can run a preprocessor, then call Python. (Those
three conditions are limiting.) Here's the steps for each line.

1. strip leading tabs
2. compile( line, '<none>', 'exec' ) to check for SyntaxError
3. if found:
3a. replace first space with '('
3b. replace remaining space with ','
3c. add trailing ')'
3d. replace leading tabs

Then just run:

python preprocessor.py -filename-
python -filename-.pppy

Have a look at code.py and codeop.py as well and the
InteractiveInterpreter class.
Aug 6 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.