473,385 Members | 1,587 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.

Extending Python questions

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
Jul 18 '05 #1
3 1280
> 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)
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
"Diez B. Roggisch" <de*********@web.de> wrote in message news:<cm*************@news.t-online.com>...
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
Jul 18 '05 #3
>
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.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
0
by: Hugh Macdonald | last post by:
I've got a slight problem... and I'm stuck as to where to go with it... I'm running on Redhat7.2, using Python 2.2.2 I've got a compiled module that I wrote almost a year ago - it works fine,...
3
by: Marco Meoni | last post by:
Hi all! I've a problem with a C++ class that has to be included in a python application. One way to do it is Extending and Embedding the Python Interpreter Now i have 2 questions 1) Is there a...
5
by: vbgunz | last post by:
Hello everyone. I own two books. Learning Python and Python in a nutshell. When cross referencing the two books to try and clarify the ideas behind extending methods and delegates, this is where...
3
by: Redefined Horizons | last post by:
I've got a third-part application that exposes a C API. I'd like to wrap it in Python. Is there a specific forum that covers extending and embedding Python, or are those type of questions O.K. on...
3
by: Redefined Horizons | last post by:
I'm trying to understand the argument flags that are used in the method table of an extension module written in C. First let me ask this question about the method table. Is it an C array named...
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
3
by: dmoore | last post by:
Hi Folks: I have a question about the use of static members in Python/C extensions. Take the simple example from the "Extending and Embedding the Python Interpreter" docs: A simple module...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.