need to extend this C code so that it initiates this python script (python C api)

2011-07-02 Thread aregee
hi i need help with extending this http://paste.pound-python.org/show/8918/
c code so that it can initiate this python script 
http://paste.pound-python.org/show/8917/
and export file path here..

btw I tried to run following python code :

#include python2.3/Python.h //changed this to python2.7/Python.h
didn't work the I changed it to Python.h
  // Error msg : fatal
error: Python.h : no file or directory compilation terminated
 //python-dev and
python2.7-dev is already installed.

void process_expression(char* filename,
int num,
char** exp)
{
FILE*   exp_file;

// Initialize a global variable for
// display of expression results
PyRun_SimpleString(x = 0);

// Open and execute the file of
// functions to be made available
// to user expressions
exp_file = fopen(filename, r);
PyRun_SimpleFile(exp_file, exp);

// Iterate through the expressions
// and execute them
while(num--) {
PyRun_SimpleString(*exp++);
PyRun_SimpleString(print x);
}
}

int main(int argc, char** argv)
{
Py_Initialize();

if(argc != 3) {
printf(Usage: %s FILENAME EXPRESSION+\n);
return 1;
}
process_expression(argv[1], argc - 1, argv + 2);
return 0;
}


thanks
aregee
Ar
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python script and C++

2006-11-28 Thread Marc 'BlackJack' Rintsch
In [EMAIL PROTECTED], Ravi Teja
wrote:

 I am new to python  and currently I am working on a traffic simulation
 which I plan to define the various agents using scripting. It's kind of like
 scripting for non-playable character in games. I am thinking of using python
 for this but I am concerned with running time. Is scripting a lot slower
 compared to direct implementation in C++? Does compiling the script help in
 any way?
 
 Python is perfectly suitable for this use. Python was in use in video
 games in this way when computers were a lot slower.

There's a difference between simulations and games.  The simulation will
not always be observed by a human being so it runs as fast as possible.
In games the speed of NPCs is usually limited to a level the player can
deal with.

But I think Python is fine for such a task.  The only way to find out if
it may be to slow is writing a prototype and measure.  Maybe it's a good
idea to design the API for the NPCs independent from the language so you
can also write them in C++ or another scripting language.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python script and C++

2006-11-28 Thread Jorgen Grahn
On Tue, 28 Nov 2006 10:12:23 +0100, Marc 'BlackJack' Rintsch [EMAIL 
PROTECTED] wrote:
 In [EMAIL PROTECTED], Ravi Teja
 wrote:

 I am new to python  and currently I am working on a traffic simulation
 which I plan to define the various agents using scripting. It's kind of like
 scripting for non-playable character in games. I am thinking of using python
 for this but I am concerned with running time. Is scripting a lot slower
 compared to direct implementation in C++? Does compiling the script help in
 any way?
 
 Python is perfectly suitable for this use. Python was in use in video
 games in this way when computers were a lot slower.

 There's a difference between simulations and games.  The simulation will
 not always be observed by a human being so it runs as fast as possible.
 In games the speed of NPCs is usually limited to a level the player can
 deal with.

Yeah. Simulation can mean running for a week on the best hardware available,
with the most optimized code you can come up with. And a week may be
acceptable, while two weeks are not.

 But I think Python is fine for such a task.

I am not so sure, but ...

 The only way to find out if
 it may be to slow is writing a prototype and measure.

... this is a good approach.

 Maybe it's a good
 idea to design the API for the NPCs independent from the language so you
 can also write them in C++ or another scripting language.

However, if that API requires thousands of calls per second during
simulation time, it doesn't help speed much, because calling C code from
Python is a pretty heavy thing in itself. The big win is when you spend a
lot of uninterrupted CPU time in C code.

One approach is to write the executive engine in C++ (once again: if needed)
and the compiler/configuration subsystem -- the thing that generates the
simulated world -- in Python. That's a perfect place for a flexible,
high-level language.

/Jorgen

-- 
  // Jorgen Grahn grahn@Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org  R'lyeh wgah'nagl fhtagn!
-- 
http://mail.python.org/mailman/listinfo/python-list


Python script and C++

2006-11-27 Thread Thuan Seah Tan
Hi all,

I am new to python  and currently I am working on a traffic simulation 
which I plan to define the various agents using scripting. It's kind of like 
scripting for non-playable character in games. I am thinking of using python 
for this but I am concerned with running time. Is scripting a lot slower 
compared to direct implementation in C++? Does compiling the script help in 
any way? Also, can anyone recommend me a book that covers python in general 
as well as C++ binding? Thanks.


Thuan Seah Tan 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python script and C++

2006-11-27 Thread nepBabu.cx
Thuan Seah Tan wrote:
 Hi all,
 
 I am new to python  and currently I am working on a traffic simulation 
 which I plan to define the various agents using scripting. It's kind of like 
 scripting for non-playable character in games. I am thinking of using python 
 for this but I am concerned with running time. Is scripting a lot slower 
 compared to direct implementation in C++? Does compiling the script help in 

Learning Python is a good book to start learning Python. I don't think
you'll be better off in C++. If speed is of utmost importance for the
whole implementation, then I suggest C++ but well coded Python runs
atleast/near to C++ implementation. Otherwise, you can atleast code the
speed-savvy part of the implementation in C++. Ofcourse, Python object
model is based on a virtual machine (called PVM) which accounts for
slower start-up due to native function call conversion but you'll find
that learning curve is only a tiny fraction compared to C++.  And you'll
surely love Python. =)

 any way? Also, can anyone recommend me a book that covers python in general 
 as well as C++ binding? Thanks.

Progamming in Python is another excellent book that might be of help.
If you are developing on windows machine then
http://aspn.activestate.com/ASPN/Python has some helpful recipes.

C++ bindings in Python are handled by extending python objects and
manipulating it from C++ codes using the Python headers.

Also, http://python.org/docs should be a good reference.
Also Google for vaults of parnassus and Fredrik lundh's guide on Python.

Goodluck!


-- 
thanks,
nepBabu.cx
  c c
 .-.,;()
.'`~C.-.c =W=

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python script and C++

2006-11-27 Thread George Sakkis
Thuan Seah Tan wrote:
 Hi all,

 I am new to python  and currently I am working on a traffic simulation
 which I plan to define the various agents using scripting. It's kind of like
 scripting for non-playable character in games. I am thinking of using python
 for this but I am concerned with running time. Is scripting a lot slower
 compared to direct implementation in C++? Does compiling the script help in
 any way? Also, can anyone recommend me a book that covers python in general
 as well as C++ binding? Thanks.

Even if pure python turns out to be slow for some critical parts of
your application, there are quite a few ways to deal with it: psyco,
pyrex, weave/blitz, ctypes, SWIG, Boost-python, SIP, CXX, SCXX,
hand-written C extensions and perhaps more. Visit
http://www.scipy.org/PerformancePython for an example of taking a
simple pure Python function and boosting it using several different
tools. Check out the final comparison table first; the pyrex version is
less than half a second slower than the C++.

George

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python script and C++

2006-11-27 Thread Ravi Teja
Thuan Seah Tan wrote:
 Hi all,

 I am new to python  and currently I am working on a traffic simulation
 which I plan to define the various agents using scripting. It's kind of like
 scripting for non-playable character in games. I am thinking of using python
 for this but I am concerned with running time. Is scripting a lot slower
 compared to direct implementation in C++? Does compiling the script help in
 any way?

Python is perfectly suitable for this use. Python was in use in video
games in this way when computers were a lot slower. I doubt that you
will need to bother compiling the script or see any observable
enhancement if you do.

One example I can remember is Kingdom Under Fire (2001).

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running Python script from C++ code(.NET)

2006-09-27 Thread Gerard Flanagan

volcano wrote:

 volcano wrote:
  Hello, folks!
  A trivial question - I have a working Python script that I have to
  invoke from C++ code. No fancy stuff - just run the whole script with
  its parameters. No callbacks, no signalling - nada, just
  stupid,primitive, straightforward call.
 
  And while there is a lot of help on embedding, I could not find out how
  to run script as a whole.SOS

 Thanks a lot to all of you who cared to answer! Eventually it was
 ::CreateProcess, and it works!


I used to find http://pinvoke.net a good resource when I was doing
Active Directory stuff.  Just for interest's sake, cutting and pasting
from the CreateProcess page, the following arcana will start Python (
again from C#, but here you can see the internals at least):

using System;
using System.Runtime.InteropServices;

class Class1
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct STARTUPINFO
{
Int32 cb;
string lpReserved;
string lpDesktop;
string lpTitle;
Int32 dwX;
Int32 dwY;
Int32 dwXSize;
Int32 dwYSize;
Int32 dwXCountChars;
Int32 dwYCountChars;
Int32 dwFillAttribute;
Int32 dwFlags;
Int16 wShowWindow;
Int16 cbReserved2;
IntPtr lpReserved2;
IntPtr hStdInput;
IntPtr hStdOutput;
IntPtr hStdError;
}

[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}

[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle;
}

[DllImport(kernel32.dll)]
public static extern bool CreateProcess(string lpApplicationName,
string lpCommandLine, ref SECURITY_ATTRIBUTES 
lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes, bool 
bInheritHandles,
uint dwCreationFlags, IntPtr lpEnvironment, string
lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);

[STAThread]
static void Main(string[] args)
{
const uint NORMAL_PRIORITY_CLASS = 0x0020;

bool retValue;
string Application = @c:\python\python24\python.exe;
string CommandLine = ;
PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();
STARTUPINFO sInfo = new STARTUPINFO();
SECURITY_ATTRIBUTES pSec = new SECURITY_ATTRIBUTES();
SECURITY_ATTRIBUTES tSec = new SECURITY_ATTRIBUTES();
pSec.nLength = Marshal.SizeOf(pSec);
tSec.nLength = Marshal.SizeOf(tSec);

retValue = CreateProcess(Application,CommandLine,
ref pSec,ref tSec,false,NORMAL_PRIORITY_CLASS,
IntPtr.Zero,null,ref sInfo,out pInfo);

}
}

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running Python script from C++ code(.NET)

2006-09-27 Thread Gerard Flanagan

volcano wrote:

 volcano wrote:
  Hello, folks!
  A trivial question - I have a working Python script that I have to
  invoke from C++ code. No fancy stuff - just run the whole script with
  its parameters. No callbacks, no signalling - nada, just
  stupid,primitive, straightforward call.
 
  And while there is a lot of help on embedding, I could not find out how
  to run script as a whole.SOS

 Thanks a lot to all of you who cared to answer! Eventually it was
 ::CreateProcess, and it works!


I used to find http://pinvoke.net a good resource when I was doing
Active Directory stuff.  Just for interest's sake, cutting and pasting
from the CreateProcess page, the following arcana will start Python (
again from C#, but here you can see the internals at least):

using System;
using System.Runtime.InteropServices;

class Class1
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct STARTUPINFO
{
Int32 cb;
string lpReserved;
string lpDesktop;
string lpTitle;
Int32 dwX;
Int32 dwY;
Int32 dwXSize;
Int32 dwYSize;
Int32 dwXCountChars;
Int32 dwYCountChars;
Int32 dwFillAttribute;
Int32 dwFlags;
Int16 wShowWindow;
Int16 cbReserved2;
IntPtr lpReserved2;
IntPtr hStdInput;
IntPtr hStdOutput;
IntPtr hStdError;
}

[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}

[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle;
}

[DllImport(kernel32.dll)]
public static extern bool CreateProcess(string lpApplicationName,
string lpCommandLine, ref SECURITY_ATTRIBUTES 
lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes, bool 
bInheritHandles,
uint dwCreationFlags, IntPtr lpEnvironment, string
lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);

[STAThread]
static void Main(string[] args)
{
const uint NORMAL_PRIORITY_CLASS = 0x0020;

bool retValue;
string Application = @c:\python\python24\python.exe;
string CommandLine = ;
PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();
STARTUPINFO sInfo = new STARTUPINFO();
SECURITY_ATTRIBUTES pSec = new SECURITY_ATTRIBUTES();
SECURITY_ATTRIBUTES tSec = new SECURITY_ATTRIBUTES();
pSec.nLength = Marshal.SizeOf(pSec);
tSec.nLength = Marshal.SizeOf(tSec);

retValue = CreateProcess(Application,CommandLine,
ref pSec,ref tSec,false,NORMAL_PRIORITY_CLASS,
IntPtr.Zero,null,ref sInfo,out pInfo);

}
}

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running Python script from C++ code(.NET)

2006-09-26 Thread volcano
volcano wrote:
 Hello, folks!
 A trivial question - I have a working Python script that I have to
 invoke from C++ code. No fancy stuff - just run the whole script with
 its parameters. No callbacks, no signalling - nada, just
 stupid,primitive, straightforward call.

 And while there is a lot of help on embedding, I could not find out how
 to run script as a whole.SOS

Thanks a lot to all of you who cared to answer! Eventually it was
::CreateProcess, and it works!

But here is another question for gurus: sometimes my script fails, and
I cannot figure out why. OK, I can - especially since I terminate it
with sys.exit(), but I want my app to know too.
GetLastError returns 0 - for the obvious reason that this is the
value Python interpreter returns with. But how can I get the script
return value?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running Python script from C++ code(.NET)

2006-09-26 Thread Cameron Laird
In article [EMAIL PROTECTED],
volcano [EMAIL PROTECTED] wrote:
.
.
.
But here is another question for gurus: sometimes my script fails, and
I cannot figure out why. OK, I can - especially since I terminate it
with sys.exit(), but I want my app to know too.
GetLastError returns 0 - for the obvious reason that this is the
value Python interpreter returns with. But how can I get the script
return value?


Your question confuses me.  Do you realize Python can exit with

   sys.exit(0)
or
   sys.exit(1)

for example, to distinguish differenet exit conditions?  When 
you do these, GetLastError() will NOT always yield 0.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running Python script from C++ code(.NET)

2006-09-24 Thread Mc Osten
volcano [EMAIL PROTECTED] wrote:

 A trivial question - I have a working Python script that I have to
 invoke from C++ code. No fancy stuff - just run the whole script with
 its parameters. No callbacks, no signalling - nada, just
 stupid,primitive, straightforward call.

 In a unix based environment I would use fork + exec*. IIRC in Windows
you should have a CreateProcess function call.

http://goffconcepts.com/techarticles/development/cpp/createprocess.html

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllpro
c/base/createprocess.asp

You could also try system, but it's generally considered insecure.

-- 
blog:  http://www.akropolix.net/rik0/blogs | Uccidete i filosofi,
site:  http://www.akropolix.net/rik0/  | tenetevi riso e
forum: http://www.akropolix.net/forum/ | bacchette per voi.
-- 
http://mail.python.org/mailman/listinfo/python-list


Running Python script from C++ code(.NET)

2006-09-23 Thread volcano
Hello, folks!
A trivial question - I have a working Python script that I have to
invoke from C++ code. No fancy stuff - just run the whole script with
its parameters. No callbacks, no signalling - nada, just
stupid,primitive, straightforward call.

And while there is a lot of help on embedding, I could not find out how
to run script as a whole.SOS

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running Python script from C++ code(.NET)

2006-09-23 Thread Gerard Flanagan

volcano wrote:
 Hello, folks!
 A trivial question - I have a working Python script that I have to
 invoke from C++ code. No fancy stuff - just run the whole script with
 its parameters. No callbacks, no signalling - nada, just
 stupid,primitive, straightforward call.

 And while there is a lot of help on embedding, I could not find out how
 to run script as a whole.SOS

In C#:

http://groups.google.com/group/comp.lang.python/browse_frm/thread/da2a675da29b0bd/197b6a89095ef930?lnk=stq=rnum=4#197b6a89095ef930

hth

Gerard

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running Python script from C++ code(.NET)

2006-09-23 Thread volcano

Gerard Flanagan wrote:
 volcano wrote:
  Hello, folks!
  A trivial question - I have a working Python script that I have to
  invoke from C++ code. No fancy stuff - just run the whole script with
  its parameters. No callbacks, no signalling - nada, just
  stupid,primitive, straightforward call.
 
  And while there is a lot of help on embedding, I could not find out how
  to run script as a whole.SOS

 In C#:

 http://groups.google.com/group/comp.lang.python/browse_frm/thread/da2a675da29b0bd/197b6a89095ef930?lnk=stq=rnum=4#197b6a89095ef930

 hth

 Gerard

Thanks for fast response, alas - it did not!
My problem is - application in C++ used to use external application,
which does not work well. So I sort of reproduced the functionality in
Python script, but now I am stuck, unable to run it properly.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running Python script from C++ code(.NET)

2006-09-23 Thread Gerard Flanagan

volcano wrote:
 Gerard Flanagan wrote:
  volcano wrote:
   Hello, folks!
   A trivial question - I have a working Python script that I have to
   invoke from C++ code. No fancy stuff - just run the whole script with
   its parameters. No callbacks, no signalling - nada, just
   stupid,primitive, straightforward call.
  
   And while there is a lot of help on embedding, I could not find out how
   to run script as a whole.SOS
 
  In C#:
 
  http://groups.google.com/group/comp.lang.python/browse_frm/thread/da2a675da29b0bd/197b6a89095ef930?lnk=stq=rnum=4#197b6a89095ef930
 
  hth
 
  Gerard

 Thanks for fast response, alas - it did not!
 My problem is - application in C++ used to use external application,
 which does not work well. So I sort of reproduced the functionality in
 Python script, but now I am stuck, unable to run it properly.

Maybe my understanding is wrong, but can't managed (.NET) C++ call into
any other managed assembly, in this case (I think) System.Diagnostics?

Gerard

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running Python script from C++ code(.NET)

2006-09-23 Thread volcano

Gerard Flanagan wrote:
 volcano wrote:
  Gerard Flanagan wrote:
   volcano wrote:
Hello, folks!
A trivial question - I have a working Python script that I have to
invoke from C++ code. No fancy stuff - just run the whole script with
its parameters. No callbacks, no signalling - nada, just
stupid,primitive, straightforward call.
   
And while there is a lot of help on embedding, I could not find out how
to run script as a whole.SOS
  
   In C#:
  
   http://groups.google.com/group/comp.lang.python/browse_frm/thread/da2a675da29b0bd/197b6a89095ef930?lnk=stq=rnum=4#197b6a89095ef930
  
   hth
  
   Gerard
 
  Thanks for fast response, alas - it did not!
  My problem is - application in C++ used to use external application,
  which does not work well. So I sort of reproduced the functionality in
  Python script, but now I am stuck, unable to run it properly.

 Maybe my understanding is wrong, but can't managed (.NET) C++ call into
 any other managed assembly, in this case (I think) System.Diagnostics?
 
 Gerard

My application is written in regular C++:(

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running Python script from C++ code(.NET)

2006-09-23 Thread Ravi Teja
 A trivial question - I have a working Python script that I have to
 invoke from C++ code. No fancy stuff - just run the whole script with
 its parameters. No callbacks, no signalling - nada, just
 stupid,primitive, straightforward call.

 And while there is a lot of help on embedding, I could not find out how
 to run script as a whole.SOS

System::Diagnostics::Process::Start

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running Python script from C++ code(.NET)

2006-09-23 Thread Ravi Teja
 A trivial question - I have a working Python script that I have to
 invoke from C++ code. No fancy stuff - just run the whole script with
 its parameters. No callbacks, no signalling - nada, just
 stupid,primitive, straightforward call.

 And while there is a lot of help on embedding, I could not find out how
 to run script as a whole.SOS

System::Diagnostics::Process::Start

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: starting some Python script from C#

2006-05-30 Thread Gerard Flanagan
  Gerard Flanagan [EMAIL PROTECTED] je napisao u poruci interesnoj
  grupi:[EMAIL PROTECTED]
   tatamata wrote:
   Hello.
  
   How can I run some Python script within C# program?
  
  
   -
  ProcessStartInfo startInfo;
  Process process;
  string directory;
  string pyArgs;
  string script;
  
  startInfo = new ProcessStartInfo(python);
  startInfo.WorkingDirectory = directory;
  startInfo.Arguments = script +   + pyArgs;
  startInfo.UseShellExecute = false;
  startInfo.CreateNoWindow = true;
  startInfo.RedirectStandardOutput = true;
  startInfo.RedirectStandardError = true;
  
  process = new Process();
  process.StartInfo = startInfo;
  process.Start();
  
  string s;
  while ((s = process.StandardOutput.ReadLine()) != null)
  {
  //do something with s
  }
   -
  
 
  tatamata wrote:
  Hello. I tried to implement ypour suggestion, but an error apears:
  Exception System.ComponentModel.Win32Exception was thrown in debugee:
  The specified executable is not a valid Win32 application.
 
  namespace CS_script
  {
   class MainClass
   {
public static void Main(string[] args)
{
 
  System.Diagnostics.ProcessStartInfo psi =new
  System.Diagnostics.ProcessStartInfo();
  psi.FileName=my_script.py;
  psi.WorkingDirectory=Environment.CurrentDirectory;
  psi.RedirectStandardOutput = true;
  psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  psi.UseShellExecute = false;
  psi.CreateNoWindow = true;
 
  System.Diagnostics.Process script;
  script = System.Diagnostics.Process.Start(psi);
 
  System.IO.StreamReader myOutput = script.StandardOutput;
  script.WaitForExit(2000);
  if (script.HasExited)
   {
   string output = myOutput.ReadToEnd();
   //this.processResults.Text = output;
  }
  MessageBox.Show(finished!);
}
   }
  }
 
  When running the program, I have the following error:
 
  Exception System.ComponentModel.Win32Exception was thrown in debugee:
  The specified executable is not a valid Win32 application.
 
  StartWithCreateProcess()
  Start()
  Start()
  Main() - c:\Documents and Settings\Zlatko\My Documents\SharpDevelop
  Projects\CS_script\CS_script\Main.cs:32,5  
 
 
 
  I have no means of running C# programs at present and can't claim much
  expertise in any case.  A guess is that a valid Win32 application
  means an '.exe' file, not a '.py' file.
 
  You are assuming that a Process object is as smart as the command
  interpreter ('cmd.exe') and will know to use 'python.exe' for a file
  with a 'py' extension?
 
  What happens if you use 'python.exe' (or 'cmd.exe') as your file and
  the script name as argument as in the code I posted?
 
  Gerard
 
 
  (PS. This group prefers that one doesn't top-post)
 

tatamata wrote:
 Hello. It seems that the following code works. And it seems that Process
 object can automatically run script by using python.exe, but only if
 standard output is not redirected...

  class MainClass
  {
   public static void Main(string[] args)
   {
MyProcess myProcess = new MyProcess();
 myProcess.ExecuteScript();
 MessageBox.Show(Continue?,Application,
 MessageBoxButtons.OKCancel);
   }
  }
  public class MyProcess
 {
 // These are the Win32 error code for file not found or access
 denied.
 const int ERROR_FILE_NOT_FOUND =2;
 const int ERROR_ACCESS_DENIED = 5;

 /// summary
 /// Executes a python script.
 /// /summary
 public void ExecuteScript()
 {
 Process myProcess = new Process();

 try
 {
 // Get the path that stores the python script.
 //string myDocumentsPath
 =Environment.GetFolderPath(Environment.SpecialFolder.Personal);
 //If the script is placed in the same folder as C#
 executable, set the path to current directory:
 string myDocumentsPath=Environment.CurrentDirectory;

 //Set the fully qualified script name
 myProcess.StartInfo.FileName = myDocumentsPath +
 \\my_script.py;

 //Execute the script:
 myProcess.Start();

// Wait for it to die...
myProcess.WaitForExit();

MessageBox.Show (Python script is successfully executed!);

 }
 catch (Win32Exception e)
 {
 if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
 {
 Console.WriteLine(e.Message + . Check the path

Re: starting some Python script from C#

2006-05-29 Thread tatamata
Hello. It seems that the following code works. And it seems that Process 
object can automatically run script by using python.exe, but only if 
standard output is not redirected...

/*
 * Created by SharpDevelop.
 * User: Zlatko
 * Date: 28.5.2006
 * Time: 9:38
 *
 * To change this template use Tools | Options | Coding | Edit Standard 
Headers.
 */
using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Forms;

namespace CS_script
{

 class MainClass
 {
  public static void Main(string[] args)
  {
   MyProcess myProcess = new MyProcess();
myProcess.ExecuteScript();
MessageBox.Show(Continue?,Application, 
MessageBoxButtons.OKCancel);
  }
 }
 public class MyProcess
{
// These are the Win32 error code for file not found or access 
denied.
const int ERROR_FILE_NOT_FOUND =2;
const int ERROR_ACCESS_DENIED = 5;

/// summary
/// Executes a python script.
/// /summary
public void ExecuteScript()
{
Process myProcess = new Process();

try
{
// Get the path that stores the python script.
//string myDocumentsPath 
=Environment.GetFolderPath(Environment.SpecialFolder.Personal);
//If the script is placed in the same folder as C# 
executable, set the path to current directory:
string myDocumentsPath=Environment.CurrentDirectory;

//Set the fully qualified script name
myProcess.StartInfo.FileName = myDocumentsPath + 
\\my_script.py;

//Execute the script:
myProcess.Start();

   //string output = myProcess.StandardOutput.ReadToEnd();
   //Console.WriteLine(output);

   //Console.WriteLine(myProcess.StandardOutput.ReadToEnd());

   //TextReader t = myProcess.StandardOutput;
   //MessageBox.Show(t.ReadToEnd());

   // Wait for it to die...
   myProcess.WaitForExit();

   MessageBox.Show (Python script is successfully executed!);

}
catch (Win32Exception e)
{
if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
Console.WriteLine(e.Message + . Check the path.);
}

else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
{
// Note that if your word processor might generate 
exceptions
// such as this, which are handled first.
Console.WriteLine(e.Message +
. You do not have permission to print this file.);
}
}
}
}
}

Greetings,

Zlatko

Gerard Flanagan [EMAIL PROTECTED] je napisao u poruci interesnoj 
grupi:[EMAIL PROTECTED]
 Gerard Flanagan [EMAIL PROTECTED] je napisao u poruci interesnoj
 grupi:[EMAIL PROTECTED]
  tatamata wrote:
  Hello.
 
  How can I run some Python script within C# program?
 
 
  -
 ProcessStartInfo startInfo;
 Process process;
 string directory;
 string pyArgs;
 string script;
 
 startInfo = new ProcessStartInfo(python);
 startInfo.WorkingDirectory = directory;
 startInfo.Arguments = script +   + pyArgs;
 startInfo.UseShellExecute = false;
 startInfo.CreateNoWindow = true;
 startInfo.RedirectStandardOutput = true;
 startInfo.RedirectStandardError = true;
 
 process = new Process();
 process.StartInfo = startInfo;
 process.Start();
 
 string s;
 while ((s = process.StandardOutput.ReadLine()) != null)
 {
 //do something with s
 }
  -
 

 tatamata wrote:
 Hello. I tried to implement ypour suggestion, but an error apears:
 Exception System.ComponentModel.Win32Exception was thrown in debugee:
 The specified executable is not a valid Win32 application.

 namespace CS_script
 {
  class MainClass
  {
   public static void Main(string[] args)
   {

 System.Diagnostics.ProcessStartInfo psi =new
 System.Diagnostics.ProcessStartInfo();
 psi.FileName=my_script.py;
 psi.WorkingDirectory=Environment.CurrentDirectory;
 psi.RedirectStandardOutput = true;
 psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
 psi.UseShellExecute = false;
 psi.CreateNoWindow = true;

 System.Diagnostics.Process script;
 script = System.Diagnostics.Process.Start(psi);

 System.IO.StreamReader myOutput = script.StandardOutput;
 script.WaitForExit(2000);
 if (script.HasExited)
  {
  string output

Re: starting some Python script from C#

2006-05-28 Thread tatamata
Hello. I tried to implement ypour suggestion, but an error apears:
Exception System.ComponentModel.Win32Exception was thrown in debugee:
The specified executable is not a valid Win32 application.

StartWithCreateProcess()
Start()
Start()
Main() - c:\Documents and Settings\Zlatko\My Documents\SharpDevelop 
Projects\CS_script\CS_script\Main.cs:32,5  

The C# code is the following:

/*
 * Created by SharpDevelop.
 * User: Zlatko
 * Date: 28.5.2006
 * Time: 9:38
 *
 * To change this template use Tools | Options | Coding | Edit Standard 
Headers.
 */
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;

namespace CS_script
{
 class MainClass
 {
  public static void Main(string[] args)
  {

System.Diagnostics.ProcessStartInfo psi =new 
System.Diagnostics.ProcessStartInfo();
psi.FileName=my_script.py;
psi.WorkingDirectory=Environment.CurrentDirectory;
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;

System.Diagnostics.Process script;
script = System.Diagnostics.Process.Start(psi);

System.IO.StreamReader myOutput = script.StandardOutput;
script.WaitForExit(2000);
if (script.HasExited)
 {
 string output = myOutput.ReadToEnd();
 //this.processResults.Text = output;
}
MessageBox.Show(finished!);
  }
 }
}

When running the program, I have the following error:

Exception System.ComponentModel.Win32Exception was thrown in debugee:
The specified executable is not a valid Win32 application.

StartWithCreateProcess()
Start()
Start()
Main() - c:\Documents and Settings\Zlatko\My Documents\SharpDevelop 
Projects\CS_script\CS_script\Main.cs:32,5  

Gerard Flanagan [EMAIL PROTECTED] je napisao u poruci interesnoj 
grupi:[EMAIL PROTECTED]
 tatamata wrote:
 Hello.

 How can I run some Python script within C# program?


 -
ProcessStartInfo startInfo;
Process process;
string directory;
string pyArgs;
string script;

startInfo = new ProcessStartInfo(python);
startInfo.WorkingDirectory = directory;
startInfo.Arguments = script +   + pyArgs;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;

process = new Process();
process.StartInfo = startInfo;
process.Start();

string s;
while ((s = process.StandardOutput.ReadLine()) != null)
{
//do something with s
}
 -

 HTH

 Gerard
 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: starting some Python script from C#

2006-05-28 Thread Gerard Flanagan
 Gerard Flanagan [EMAIL PROTECTED] je napisao u poruci interesnoj
 grupi:[EMAIL PROTECTED]
  tatamata wrote:
  Hello.
 
  How can I run some Python script within C# program?
 
 
  -
 ProcessStartInfo startInfo;
 Process process;
 string directory;
 string pyArgs;
 string script;
 
 startInfo = new ProcessStartInfo(python);
 startInfo.WorkingDirectory = directory;
 startInfo.Arguments = script +   + pyArgs;
 startInfo.UseShellExecute = false;
 startInfo.CreateNoWindow = true;
 startInfo.RedirectStandardOutput = true;
 startInfo.RedirectStandardError = true;
 
 process = new Process();
 process.StartInfo = startInfo;
 process.Start();
 
 string s;
 while ((s = process.StandardOutput.ReadLine()) != null)
 {
 //do something with s
 }
  -
 

tatamata wrote:
 Hello. I tried to implement ypour suggestion, but an error apears:
 Exception System.ComponentModel.Win32Exception was thrown in debugee:
 The specified executable is not a valid Win32 application.

 namespace CS_script
 {
  class MainClass
  {
   public static void Main(string[] args)
   {

 System.Diagnostics.ProcessStartInfo psi =new
 System.Diagnostics.ProcessStartInfo();
 psi.FileName=my_script.py;
 psi.WorkingDirectory=Environment.CurrentDirectory;
 psi.RedirectStandardOutput = true;
 psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
 psi.UseShellExecute = false;
 psi.CreateNoWindow = true;

 System.Diagnostics.Process script;
 script = System.Diagnostics.Process.Start(psi);

 System.IO.StreamReader myOutput = script.StandardOutput;
 script.WaitForExit(2000);
 if (script.HasExited)
  {
  string output = myOutput.ReadToEnd();
  //this.processResults.Text = output;
 }
 MessageBox.Show(finished!);
   }
  }
 }

 When running the program, I have the following error:

 Exception System.ComponentModel.Win32Exception was thrown in debugee:
 The specified executable is not a valid Win32 application.

 StartWithCreateProcess()
 Start()
 Start()
 Main() - c:\Documents and Settings\Zlatko\My Documents\SharpDevelop
 Projects\CS_script\CS_script\Main.cs:32,5  



I have no means of running C# programs at present and can't claim much
expertise in any case.  A guess is that a valid Win32 application
means an '.exe' file, not a '.py' file.

You are assuming that a Process object is as smart as the command
interpreter ('cmd.exe') and will know to use 'python.exe' for a file
with a 'py' extension?

What happens if you use 'python.exe' (or 'cmd.exe') as your file and
the script name as argument as in the code I posted?

Gerard


(PS. This group prefers that one doesn't top-post)

-- 
http://mail.python.org/mailman/listinfo/python-list


starting some Python script from C#

2006-05-27 Thread tatamata
Hello.

How can I run some Python script within C# program?

Thanks,

Zlatko 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: starting some Python script from C#

2006-05-27 Thread Gerard Flanagan
tatamata wrote:
 Hello.

 How can I run some Python script within C# program?


-
ProcessStartInfo startInfo;
Process process;
string directory;
string pyArgs;
string script;

startInfo = new ProcessStartInfo(python);
startInfo.WorkingDirectory = directory;
startInfo.Arguments = script +   + pyArgs;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;

process = new Process();
process.StartInfo = startInfo;
process.Start();

string s;
while ((s = process.StandardOutput.ReadLine()) != null)
{
//do something with s
}
-

HTH

Gerard

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: starting some Python script from C#

2006-05-27 Thread tatamata
thanks.

Gerard Flanagan [EMAIL PROTECTED] je napisao u poruci interesnoj 
grupi:[EMAIL PROTECTED]
 tatamata wrote:
 Hello.

 How can I run some Python script within C# program?


 -
ProcessStartInfo startInfo;
Process process;
string directory;
string pyArgs;
string script;

startInfo = new ProcessStartInfo(python);
startInfo.WorkingDirectory = directory;
startInfo.Arguments = script +   + pyArgs;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;

process = new Process();
process.StartInfo = startInfo;
process.Start();

string s;
while ((s = process.StandardOutput.ReadLine()) != null)
{
//do something with s
}
 -

 HTH

 Gerard
 


-- 
http://mail.python.org/mailman/listinfo/python-list