Extending Python questions

E

Eli Daniel

Hi,
I am relative new to Python. Please tell me if the following is
possible.
I have a command line shell written in C. This shell executes some
commands which I would like to import to the python shell.
Here is an example command from my shell:

openconnection www.yahoo.com 12 2000

So I've created a dll (myshell_d.dll) according to the manual
("Extending Python with C or C++") and it works. The problem is the
syntax I have to use now is

myshell.openconnection('www.yahoo.com',12,2000)

It's not only the problem of comfort - it's a problem of changing
*some* scripts. And there's more than that - my shell functions
already know how to handle it's arguments (which's number may vary),
it doesn't need python to do that. All it needs is a pointer to a
string representing the arguments (in our case "www.yahoo.com 12
2000"). I've tried using PyArg_UnpackTuple but without success.

So I have two questions:
1. Is it possible to move the calls to a upper level (i.e. call
"openconnection" and not "myshell.openconnection") ?
2. Is is possible to remove the brackets and commas? (i.e.
"www.yahoo.com 12 2000" instead of ('www.yahoo.com',12,2000)?

My final goal is to use the original commands if possible.
thanks in advance,
Eli
 
D

Diez B. Roggisch

myshell.openconnection('www.yahoo.com',12,2000)
It's not only the problem of comfort - it's a problem of changing
*some* scripts. And there's more than that - my shell functions
already know how to handle it's arguments (which's number may vary),
it doesn't need python to do that. All it needs is a pointer to a
string representing the arguments (in our case "www.yahoo.com 12
2000"). I've tried using PyArg_UnpackTuple but without success.

Then don't specify the arguments separately, but instead make it one string
argument, and pass it to your command:

myshell.openconnection('www.yahoo.com 12 2000')

So I have two questions:
1. Is it possible to move the calls to a upper level (i.e. call
"openconnection" and not "myshell.openconnection") ?

from myshell import *
2. Is is possible to remove the brackets and commas? (i.e.
"www.yahoo.com 12 2000" instead of ('www.yahoo.com',12,2000)?

No. Thats too much of tinkering with pythons parser - you'd render all other
python sources useless, which I suspect outnumber your scripts by a degree
or two... :)
final goal is to use the original commands if possible.
thanks in advance,

What do you use python for in the first place, if you want to keep
everything as it is?

What you can do is to read your scripts _using_ python instead of _passing_
them to python - then you can control the syntax of your scripting
language. The main loop could look like this:

import myshell
for line in sys.stdin.readlines():
command, rest = line.split()[0], " ".join(line.split()[1:])
getattr(myshell, command)(rest)
 
E

Eli Daniel

Diez B. Roggisch said:
myshell.openconnection('www.yahoo.com',12,2000)

It's not only the problem of comfort - it's a problem of changing
*some* scripts. And there's more than that - my shell functions
already know how to handle it's arguments (which's number may vary),
it doesn't need python to do that. All it needs is a pointer to a
string representing the arguments (in our case "www.yahoo.com 12
2000"). I've tried using PyArg_UnpackTuple but without success.

Then don't specify the arguments separately, but instead make it one string
argument, and pass it to your command:

myshell.openconnection('www.yahoo.com 12 2000')

So I have two questions:
1. Is it possible to move the calls to a upper level (i.e. call
"openconnection" and not "myshell.openconnection") ?

from myshell import *
2. Is is possible to remove the brackets and commas? (i.e.
"www.yahoo.com 12 2000" instead of ('www.yahoo.com',12,2000)?

No. Thats too much of tinkering with pythons parser - you'd render all other
python sources useless, which I suspect outnumber your scripts by a degree
or two... :)
final goal is to use the original commands if possible.
thanks in advance,

What do you use python for in the first place, if you want to keep
everything as it is?

What you can do is to read your scripts _using_ python instead of _passing_
them to python - then you can control the syntax of your scripting
language. The main loop could look like this:

import myshell
for line in sys.stdin.readlines():
command, rest = line.split()[0], " ".join(line.split()[1:])
getattr(myshell, command)(rest)


Thanks for the reply. I'll try those.
The reason I'm trying it this way is that a user can add python code
inside the scripts.

Eli
 
D

Diez B. Roggisch

Thanks for the reply. I'll try those.
The reason I'm trying it this way is that a user can add python code
inside the scripts.

Sorry to say that, but then you'll have to either write a real parser that
understands both syntaxes - and I bet thats more effort as converting your
existing scripts.

You can't have your cake and eat it - if you want python, you should use its
syntax.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top