Re: [IronPython] Calling functions in IronPython that don't really exist

2008-12-04 Thread Jeff Slutter
I have found another way that better suits what I'm trying to do (and
control).

In a nutshell, I just add the function to the builtins. Here is the code:

// some member function, etc.
delegate int DoDelegate();
private int DoFunc()
{
  return 10;
}

//... in the IronPython initialization, after the scripting engine has
been created ...

ScriptScope builtins = Python.GetBuiltinModule(m_engine);
builtins.SetVariable("SomeTestFunction", new DoDelegate(DoFunc));




now, in the Python scripts I can call:

SomeTestFunction();


Simple problem and solution, but I just wanted a way to dynamically add
new functions to Python with names that I control.

___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


Re: [IronPython] Calling functions in IronPython that don't really exist

2008-12-04 Thread Dino Viehland
You can just import static functions instead.  Something like:

public static class ScriptHelpers {
public static object AddNumbers(params object[] args) {
return 42;
}
}

Add the reference and then:

from ScriptHelpers import *

If you really need to construct an instance for some reason you could do it 
inside of AddNumbers and do the .GetReturn there as well.

> -Original Message-
> From: [EMAIL PROTECTED] [mailto:users-
> [EMAIL PROTECTED] On Behalf Of Jeff Slutter
> Sent: Thursday, December 04, 2008 1:59 PM
> To: Discussion of IronPython
> Subject: Re: [IronPython] Calling functions in IronPython that don't
> really exist
>
> Actually this doesn't exactly work. If I want the command to be able to
> return a value, I can't go with the constructor route:
>
> res = AddNumbers( 3, 5 );
>
> you want res equal to "8", but really it is an instance of my
> AddNumbers
> class. I don't want the script writers to be able to save this class
> instance, and it would be 'ugly' if they had to call ".GetReturn()" to
> get the return value of the command.
>
> And I don't want the call to "Do" to be explicit.
>
> I sound really picky...
>
>
> Dino Viehland wrote:
> > Do you mean you'd call it like "AddNumbers().Do(3, 4)"?
> >
> > This is really easy.  Make AddNumbers public, then do:
> >
> > import clr
> > clr.AddReference('MyAssembly')
> > import AddNumbers
> > AddNumbers().Do(3, 4)
> >
> > If you really want to do AddNumbers(3, 4) then you'd just write it
> as:
> >
> > public class AddNumbers
> > {
> > public AddNumbers(params object[] args) {
> >  Do(args);
> > }
> > public string Do( params object[] args )
> > {
> > ..check args..
> > ..add the two arguments..
> > ..return the result as a string..
> > }
> > }
> >
> > And do the same:
> >
> > import clr
> > clr.AddReference('MyAssembly')
> > import AddNumbers
> > AddNumbers(3, 4)
> >
> ___
> Users mailing list
> Users@lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


Re: [IronPython] Calling functions in IronPython that don't really exist

2008-12-04 Thread Jeff Slutter
Actually this doesn't exactly work. If I want the command to be able to
return a value, I can't go with the constructor route:

res = AddNumbers( 3, 5 );

you want res equal to "8", but really it is an instance of my AddNumbers
class. I don't want the script writers to be able to save this class
instance, and it would be 'ugly' if they had to call ".GetReturn()" to
get the return value of the command.

And I don't want the call to "Do" to be explicit.

I sound really picky...


Dino Viehland wrote:
> Do you mean you'd call it like "AddNumbers().Do(3, 4)"?
>
> This is really easy.  Make AddNumbers public, then do:
>
> import clr
> clr.AddReference('MyAssembly')
> import AddNumbers
> AddNumbers().Do(3, 4)
>
> If you really want to do AddNumbers(3, 4) then you'd just write it as:
>
> public class AddNumbers
> {
> public AddNumbers(params object[] args) {
>  Do(args);
> }
> public string Do( params object[] args )
> {
> ..check args..
> ..add the two arguments..
> ..return the result as a string..
> }
> }
>
> And do the same:
>
> import clr
> clr.AddReference('MyAssembly')
> import AddNumbers
> AddNumbers(3, 4)
>
___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


Re: [IronPython] Calling functions in IronPython that don't really exist

2008-12-04 Thread Jeff Slutter
Ok, now I feel a little stupid. Thanks :)

I think it would be wise to drop the "params object[] args" and go with
just actually specifying the parameters to the constructor so I can take
advantage of type checking, etc. from Python.

In the constructor I'll have to save the instance of the class created
to my internal History list (for the undo, redo).

Thanks again!

Dino Viehland wrote:
> Do you mean you'd call it like "AddNumbers().Do(3, 4)"?
>
> This is really easy.  Make AddNumbers public, then do:
>
> import clr
> clr.AddReference('MyAssembly')
> import AddNumbers
> AddNumbers().Do(3, 4)
>
> If you really want to do AddNumbers(3, 4) then you'd just write it as:
>
> public class AddNumbers
> {
> public AddNumbers(params object[] args) {
>  Do(args);
> }
> public string Do( params object[] args )
> {
> ..check args..
> ..add the two arguments..
> ..return the result as a string..
> }
> }
>
> And do the same:
>
> import clr
> clr.AddReference('MyAssembly')
> import AddNumbers
> AddNumbers(3, 4)
>
>   
>
___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


Re: [IronPython] Calling functions in IronPython that don't really exist

2008-12-04 Thread Dino Viehland
Do you mean you'd call it like "AddNumbers().Do(3, 4)"?

This is really easy.  Make AddNumbers public, then do:

import clr
clr.AddReference('MyAssembly')
import AddNumbers
AddNumbers().Do(3, 4)

If you really want to do AddNumbers(3, 4) then you'd just write it as:

public class AddNumbers
{
public AddNumbers(params object[] args) {
 Do(args);
}
public string Do( params object[] args )
{
..check args..
..add the two arguments..
..return the result as a string..
}
}

And do the same:

import clr
clr.AddReference('MyAssembly')
import AddNumbers
AddNumbers(3, 4)

> -Original Message-
> From: [EMAIL PROTECTED] [mailto:users-
> [EMAIL PROTECTED] On Behalf Of Jeff Slutter
> Sent: Thursday, December 04, 2008 1:14 PM
> To: Discussion of IronPython
> Subject: [IronPython] Calling functions in IronPython that don't really
> exist
>
> I'm writing an application that I want to provide runtime scripting via
> IronPython for. This application loads "plug-ins" from assemblies to
> add
> functionality.
>
> Each plug-in is a class with, for the sake of simplicity, a "Do"
> function.
>
> What I'm trying to figure out is that if I have a class like:
>
> class AddNumbers
> {
> public string Do( params object[] args )
> {
> ..check args..
> ..add the two arguments..
> ..return the result as a string..
> }
> }
>
>
> That I could call it from IronPython like:
>
> AddNumbers( 3, 4 )
>
> When that happens a new instance of the AddNumbers class is created,
> the
> arguments are then passed to the Do member function, the result is
> returned back to Python.
>
> This may seem like a weird way for scripting. But, doing it this way I
> can do some other important things, such as keeping a history of the
> commands. If, along with Do, the commands have Undo and Redo functions,
> then I can keep the instances of the commands stored and should the
> user
> need to undo their command, I can internally run the Undo function.
>
> Any ideas on how I should approach this? I don't mind having to do a
> good chunk of backend work so that to add new command plugins, I just
> have to write a class with a Do function and possibly provide some
> Attributes if necessary. If I have to make some concessions that is
> fine, but I need to stick with the concept of creating an instance of a
> class and executing a function to perform the operation.
>
> I assume I'm either going to have to generate some sort of thunk
> function (I have no idea where to start) or provide some sort of
> 'command runner' that really gets called.
>
> Any help or guidance is mighty appreciated,
> Jeff
> ___
> Users mailing list
> Users@lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


Re: [IronPython] Calling functions in IronPython that don't really exist

2008-12-04 Thread James Matthews
You can just create the function name and do nothing with it. In Cpython i
would use meta classes but idk if they are in IronPython

On Thu, Dec 4, 2008 at 11:13 PM, Jeff Slutter <[EMAIL PROTECTED]>wrote:

> I'm writing an application that I want to provide runtime scripting via
> IronPython for. This application loads "plug-ins" from assemblies to add
> functionality.
>
> Each plug-in is a class with, for the sake of simplicity, a "Do" function.
>
> What I'm trying to figure out is that if I have a class like:
>
> class AddNumbers
> {
>public string Do( params object[] args )
>{
>..check args..
>..add the two arguments..
>..return the result as a string..
>}
> }
>
>
> That I could call it from IronPython like:
>
> AddNumbers( 3, 4 )
>
> When that happens a new instance of the AddNumbers class is created, the
> arguments are then passed to the Do member function, the result is
> returned back to Python.
>
> This may seem like a weird way for scripting. But, doing it this way I
> can do some other important things, such as keeping a history of the
> commands. If, along with Do, the commands have Undo and Redo functions,
> then I can keep the instances of the commands stored and should the user
> need to undo their command, I can internally run the Undo function.
>
> Any ideas on how I should approach this? I don't mind having to do a
> good chunk of backend work so that to add new command plugins, I just
> have to write a class with a Do function and possibly provide some
> Attributes if necessary. If I have to make some concessions that is
> fine, but I need to stick with the concept of creating an instance of a
> class and executing a function to perform the operation.
>
> I assume I'm either going to have to generate some sort of thunk
> function (I have no idea where to start) or provide some sort of
> 'command runner' that really gets called.
>
> Any help or guidance is mighty appreciated,
> Jeff
> ___
> Users mailing list
> Users@lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>



-- 
http://www.astorandblack.com/

http://www.thewatcherys.com/
___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


[IronPython] Calling functions in IronPython that don't really exist

2008-12-04 Thread Jeff Slutter
I'm writing an application that I want to provide runtime scripting via
IronPython for. This application loads "plug-ins" from assemblies to add
functionality.

Each plug-in is a class with, for the sake of simplicity, a "Do" function.

What I'm trying to figure out is that if I have a class like:

class AddNumbers
{
public string Do( params object[] args )
{
..check args..
..add the two arguments..
..return the result as a string..
}
}


That I could call it from IronPython like:

AddNumbers( 3, 4 )

When that happens a new instance of the AddNumbers class is created, the
arguments are then passed to the Do member function, the result is
returned back to Python.

This may seem like a weird way for scripting. But, doing it this way I
can do some other important things, such as keeping a history of the
commands. If, along with Do, the commands have Undo and Redo functions,
then I can keep the instances of the commands stored and should the user
need to undo their command, I can internally run the Undo function.

Any ideas on how I should approach this? I don't mind having to do a
good chunk of backend work so that to add new command plugins, I just
have to write a class with a Do function and possibly provide some
Attributes if necessary. If I have to make some concessions that is
fine, but I need to stick with the concept of creating an instance of a
class and executing a function to perform the operation.

I assume I'm either going to have to generate some sort of thunk
function (I have no idea where to start) or provide some sort of
'command runner' that really gets called.

Any help or guidance is mighty appreciated,
Jeff
___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com