473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

More like a shell command.

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
4 951
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
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
Maybe this module would work fine:

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

--
Angel
Aug 6 '08 #4
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Bowling | last post by:
I have a java (2.0) program with the following lines: String cmdArray1 = {"lp", "-d", "hp4m", "MyFile"}; System.out.println(Runtime.getRuntime().exec(cmdArray1)); It compliles properly, but...
6
by: Lauren Wilson | last post by:
Hi folks, In an A2K app, I have attempted to use the following command in some VBA code with IDENTICAL results with every single version of the following: Shell "outlook.exe", vbHide Shell...
1
by: VRWC | last post by:
Hello dear people, In an A2K app, I have attempted to use the following command in some VBA code with IDENTICAL results with every single version of the following: Shell "outlook.exe", vbHide...
10
by: Siemel Naran | last post by:
Hi. I'm writing a command shell that reads commands from standard input. At this point I have the command in a std::string. Now I want to execute this command in the shell. From the Borland...
2
by: micahstrasser | last post by:
I have been trying for days to send a command to the command prompt through the shell() function in vb.net. For some reason it is not working. Here is the code: Private Sub Button1_Click(ByVal...
2
by: ¹é¿ø¼® | last post by:
Hello, everybody. I want to call any shell command from php script using shell_exec(). The problem is that the next line of the php script would not be run until a few minutes after running the...
4
by: RLN | last post by:
Re: Access 2003/WinXP Pro-SP3 I have a shell command that does not run correctly. I have two programs located on a file server: Pgm1.mdb and Pgm2.mdb Pgm1 launches Pgm2 via a command button...
25
by: dennijr | last post by:
ok, shell always used to be easy for me, now its starting to get annoying cause i dont know wats wrong heres the simplist code possible: Private Sub IExplorer_Click() a = Shell("C:\Program...
3
by: Max Vit | last post by:
I have come across a strange issue whilst trying to use a shell command call from Access and have spent some time trying to figure this out but can't find the cause as yet. The issue is: I need...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.