I have a DotNet class with methods with the following signature:
public bool MyMethod(out int myVariable)
{
myVariable = 5;
return true
}
Does anybody know how I cancall them from Python via CLR?
I've tried MyMethod(a), where a has been an empty list, a list with one
element, an integer etc, but nomatter what i get an error saying TypeError:
no method matches given arguments
Can anybody help?
Jill
Hi,
I'm trying to come up with a port of a python project into C# (to speed it
up, and get experience with C#), and I'm having problems using the C# DLL
I've created. I've added it to the GAC... Here is the Python code, and
also the C# code.. Any help is appreciated!
Thanks,
Sean
>>> import CLR
>>> import CLR.nltk
>>> from CLR.nltk import Set
>>> dir(Set)
['__doc__', '__name__']
>>> from CLR.nltk.Set import Set
>>> dir(Set)
['Equals', 'Finalize', 'GetHashCode', 'GetType', 'MemberwiseClone',
'ReferenceEquals', 'ToString', '__and__', '__call__', '__class__',
'__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__',
'__eq__', '__getattribute__', '__getitem__', '__hash__', '__init__',
'__iter__', '__len__', '__module__', '__new__', '__or__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__str__',
'__sub__', 'contains', 'copy', 'count', 'dict', 'difference', 'elements',
'f_measure', 'intersection', 'precision', 'recall', 'union']
>>> a = Set([5,3,2,1])
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'MethodObject' object is not callable
C# code:
namespace nltk
{
namespace Set
{
public class Set
{
protected Dict dict ;
public Set(Dict idict)
{
dict = idict;
}
public Set(params object[] list)
{
__init__(list);
}
private void __init__(params object[] list)
{
dict = new Dict();
foreach (object name in list)
dict[name.ToString()] = 1;
}
public string[] elements()
{
return dict.GetKeys;
}
public Set union(Set other)
{
Dict newDict = new Dict();
foreach (string key in dict.Keys)
newDict[key] = 1;
foreach (string key in other.elements())
newDict[key] = 1;
return new Set(newDict);
}
public bool contains(object element)
{
return dict.Contains(element.ToString());
}
public bool contains(string element)
{
return dict.Contains(element);
}
public Set intersection(Set other)
{
Dict newDict = new Dict();
foreach (string key in dict.Keys)
if (other.contains(key))
newDict[key] = 1;
return new Set( newDict);
}
public Set difference(Set other)
{
Dict newDict = new Dict();
foreach (string key in dict.Keys)
if (!other.contains(key))
newDict[key] = 1;
return new Set(newDict);
}
public Set __and__(Set other)
{
return intersection(other);
}
public Set __or__(Set other)
{
return union(other);
}
public Set __sub__(Set other)
{
return difference(other);
}
public bool __contains__(string element)
{
return dict.Contains(element);
}
public Set copy()
{
Dict newDict = new Dict();
foreach (string key in dict.Keys)
newDict[key] = 1;
return new Set(newDict);
}
public float precision(Set other)
{
int guessed = other.elements().Length;
if (guessed==0) return 0;
int found = intersection(other).elements().Length;
return (float) found / guessed;
}
public float recall(Set other)
{
int toFind = elements().Length;
if (toFind == 0) return 0;
int found = intersection(other).elements().Length;
return (float) found / toFind;
}
public float f_measure(Set other, float alpha)
{
float p = precision(other);
float r = recall(other);
if (p==0 || r==0) return 0;
return 1/(alpha/p + (1-alpha)/r);
}
public float f_measure(Set other)
{
return f_measure(other, (float)0.5);
}
public string __repr__()
{
return string.Format("[{0}]",string.Join(",
",elements())).Replace("[","{").Replace("]","}");
}
public int __len__()
{
return dict.GetKeys.Length;
}
public int count()
{
return __len__();
}
public int __cmp__(Set other)
{
if (other.count() > count())
return -1;
else if (other.count() == count())
return 0;
else return 1;
}
public bool __eq__(Set other)
{
return (other == this);
}
public virtual int __hash__()
{
return dict.GetHashCode();
}
}
__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you�re looking for faster
http://search.yahoo.com
Thane,
I figured out the problem.. it turns out that I had an old version of the
DLL in the GAC (and at one point an old one copied to the Python.Net
directory).. when I updated the GAC (and deleted the one in the Python.Net
directory) I was able to successfully initialize the Set object.
Thanks for looking into this. I wouldn't have figured it out if you
hadn't shown it working on your box!
Sean
--- Thane <thane(a)magna-capital.com> wrote:
> Code is good. See output below. I suspect that Python.NET can't see
> something...
>
> I'll keep playing with it and try to duplicate your error.
>
> --Thane
>
> ----------- PYTHON OUTPUT -----------------
> Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on
> win32
> Type "copyright", "credits" or "license" for more information.
> >>> Running initialization script, please stand by...
> >>> import CLR.nltk
> >>> from CLR.nltk import Set
> >>> dir(Set)
> ['__doc__', '__name__']
> >>> from CLR.nltk.Set import Set
> >>> dir(Set)
> ['Equals', 'Finalize', 'GetHashCode', 'GetType', 'MemberwiseClone',
> 'ReferenceEquals', 'ToString', '__and__', '__call__', '__class__',
> '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__',
> '__eq__', '__getattribute__', '__getitem__', '__hash__', '__init__',
> '__iter__', '__len__', '__module__', '__new__', '__or__', '__reduce__',
> '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__str__',
> '__sub__', 'contains', 'copy', 'count', 'dict', 'difference',
> 'elements',
> 'f_measure', 'intersection', 'precision', 'recall', 'union']
> >>> a = Set(1)
> Traceback (most recent call last):
> File "<console>", line 1, in ?
> TypeError: no constructor matches given arguments
> >>> a = Set([1,2])
> >>> a
> <CLR.nltk.Set.Set object at 0x072A7BD0>
> >>> b = Set([5,3,2,1])
> >>> b
> <CLR.nltk.Set.Set object at 0x072A7D30>
> >>> b.contains(3)
> True
> >>>
>
__________________________________
Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online.
http://taxes.yahoo.com/filing.html
My apologies! I'll add the Dict code.. (btw, some closing brackets are
probably missing, as well as possibly some using statements, I was trying
to cut stuff out for brevity). Thanks for looking at this...
Sean
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
namespace nltk
{
namespace probability
{
public delegate ProbDist CreateProbDistDelegate(FreqDist fdist, params
object[] args);
#region Helper Classes
public class Dict: Hashtable
{
private ArrayList keys = new ArrayList();
public override object this[ object key ]
{
get
{
return( base[key] );
}
set
{
if (!keys.Contains(key))
keys.Add(key);
base[key] = value;
}
}
public string[] GetKeys
{
get
{
string[] results = new string[keys.Count];
keys.CopyTo(results);
return( results);
}
}
public override string ToString()
{
string result = "";
foreach (string key in Keys)
{
result += key + ":" + base[key].ToString() + " ";
}
return result;
}
public override void Add( object key, object value )
{
if (!keys.Contains(key))
keys.Add(key);
base.Add(key, value);
}
public override void Remove( object key )
{
keys.Remove(key);
base.Remove( key );
}
public int Length
{
get
{
return base.Count;
}
}
}
#endregion
--- Thane <thane(a)magna-capital.com> wrote:
> Sean, I can't get your C# code to compile. The complaint is about
> "Dict".
>
<snipped>
__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you�re looking for faster
http://search.yahoo.com
It will probably be a .DLL because it will be a .NET Assembly.
---------------------------------
From: pythondotnet-bounces(a)python.org [mailto:pythondotnet-bounces@python.org] On Behalf Of Jill-Connie Lorentsen
Sent: 11 March 2004 05:07
To: pythondotnet(a)python.org
Subject: [Python.NET] Showing modal ..NET dialogs from Python
Thanks for a quick response, Mike.
Just one thing, - what kind of file is "MyColleaguesFile", .py, .cpp, .h, .dll...? (my colleague hasn't finished his part of the project yet, and he doesn't know much about this either...)
Jill
---------------------------------
Yahoo! Messenger - Communicate instantly..."Ping" your friends today! Download Messenger Now
Thanks for a quick response, Mike.
Just one thing, - what kind of file is "MyColleaguesFile", .py, .cpp, h, .dll...? (my colleague hasn't finished his part of the project yet, and he doesn't know much about this either...)
Jill
I am new to this, so please excuse me if I'm not making myself 100% clear.
My colleague is writing a modal dialog using .NET. I want to show it from Python, but I can't figure out how.
I guess it'll be something like:
import CLR
import CLR.System.Windows.Forms as MyForm
from MyColleaguesFile import MyColleaguesForm
MyForm = MyColleaguesForm
MyForm.Show()
I've looked at the helloform, splitter and wordwin examples, the mailinglist archive etc, but I feel like I'm missing something. Is it even possible to do what I want to do?
Thanks!
Jill
Hi Jill.
Have a look at the documentation where it describes using 'Assembly.LoadWithPartialName'. I think you need to do something like this:
from CLR.System.Reflection import Assembly
Assembly.LoadWithPartialName("MyColleaguesFile")
from CLR.MyColleaguesFile import MyColleaguesForm
Then you can use the class natively. The above assumes that the assembly can be located in the existing path (e.g. current working directory). If not, add its location to sys.path.
Also assumed is that the namespace for the class is the same as the assembly file name. If not, use the correct one in the third line above, e.g. CLR.MyColleaguesNamespace.
Mike
---------------------------------
From: pythondotnet-bounces(a)python.org [mailto:pythondotnet-bounces@python.org] On Behalf Of Jill-Connie Lorentsen
Sent: 11 March 2004 04:16
To: pythondotnet(a)python.org
Subject: [Python.NET] Showing modal ..NET dialogs from Python
I am new to this, so please excuse me if I'm not making myself 100% clear.
My colleague is writing a modal dialog using ..NET. I want to show it from Python, but I can't figure out how.
I guess it'll be something like:
import CLR
import CLR.System.Windows.Forms as MyForm
from MyColleaguesFile import MyColleaguesForm
MyForm = MyColleaguesForm
MyForm.Show()
I've looked at the helloform, splitter and wordwin examples, the mailinglist archive etc, but I feel like I'm missing something. Is it even possible to do what I want to do?
Thanks!
Jill
---------------------------------
Yahoo! Messenger - Communicate instantly..."Ping" your friends today! Download Messenger Now
http://www.python.org
"python" is "cpython"
when people talk about jpython or python.net or any other python, they use "cpython" to explicitly refer to THE "original" python. i believe this is because the low level python (cpython) libs and python interpreter are written in C.
> -----Original Message-----
> From: pythondotnet-bounces(a)python.org
> [mailto:pythondotnet-bounces@python.org]On Behalf Of Leonard :paniq:
> Ritter
> Sent: Friday, February 27, 2004 1:05 PM
> To: PythonDotNet(a)python.org
> Subject: Re[2]: [Python.NET] Anyone or anything happening here?
>
>
>
> (whoops, sent to individual instead of mailing list first)
>
> i heard a lot of this mysterious "CPython", could anybody give me a
> hint on how to get it and check it out? google doesnt give me useful
> resuls.
>
> would be charming, if it is an x86 compiler for a typed python dialect
> :)
>
> -- Leonard Ritter
> -- paniq(a)gmx.net
> -- http://www.paniq.de
> -- http://www.the-mu.com
>
> Friday, February 27, 2004, 8:03:25 PM, you wrote:
>
>
> Mcic> Actually, Hawkeye (and the OP) , it's yet a bit more
> than that. I agree
> Mcic> that "it is not "Python.Net"" (it does not make Python
> run as a .NET
> Mcic> language), but it does allow you to embed Python in a
> .NET app. In fact, as
> Mcic> you're probably aware, the python.exe that you run when
> using Python for
> Mcic> .NET in the conventional way (see documented examples)
> is a simple example
> Mcic> of embedding Python in a (very small) .NET app, which
> in turn provides
> Mcic> access to a very rich interface for calling back in the
> other direction.
>
> Mcic> The viewpoint one has while using Python for .NET in
> the conventional way
> Mcic> (running the CLR-enabled python.exe, importing CLR, and
> going from there)
> Mcic> is that the CLR is acting as an appendage to Python.
> But in fact there is a
> Mcic> duality in this architecture that is subtle but
> amazingly flexible. You
> Mcic> might say that in this clever framework, Python and
> .NET are mutually
> Mcic> embedded, which offers some unique possibilities
> similar to, but even more
> Mcic> powerful than, Jython.
>
> Mcic> BTW, Jython is pretty cool too, but I prefer Python for
> .NET because it is
> Mcic> real CPython, so almost anything that you could do in
> Python before
> Mcic> integrating it with .NET, you can still do. In Jython,
> any python code has
> Mcic> to be available in source form so it can be compiled by
> Jython (at least
> Mcic> once) whereasany Python byte code that works with the
> currently supported
> Mcic> Python runtime (2.3 currently) will work with Python for .NET.
>
> Mcic> The somewhat downplayed embedding capability is the
> aspect of Python for
> Mcic> .NET that I am personally finding very useful, with the
> "calling back to
> Mcic> .NET" feature being a very nice bonus, allowing the
> embedding .NET app to
> Mcic> use Python to allow it to sort-of "script into itself".
>
>
> Mcic> P.S. -- I am not contradicting you (your answer to the
> OP is completely
> Mcic> correct) -- I'm just adding a viewpoint that I have
> found useful.
>
> Mcic> ==========================
> Mcic> Mark Barclay
> Mcic> Sr. Software Engineer, CRi Inc.
>
>
>
> Mcic> |---------+------------------------------->
> Mcic> | | "Hawkeye Parker" |
> Mcic> | | <hawkeye.parker@auto|
> Mcic> | | desk.com> |
> Mcic> | | Sent by: |
> Mcic> | | pythondotnet-bounces|
> Mcic> | | @python.org |
> Mcic> | | |
> Mcic> | | |
> Mcic> | | 02/27/2004 01:05 PM |
> Mcic> | | |
> Mcic> |---------+------------------------------->
> Mcic>
> Mcic>
> >-------------------------------------------------------------
> ------------------------------------------------------|
> Mcic> |
> Mcic> | To: <srussell(a)lotmate.com>,
> Mcic> <pythondotnet(a)python.org>
> Mcic> | cc:
> Mcic> | Subject: RE: [Python.NET] Anyone or anything
> Mcic> happening here? |
> Mcic>
> Mcic>
> >-------------------------------------------------------------
> ------------------------------------------------------|
>
>
>
>
> Mcic> http://www.zope.org/Members/Brian/PythonNet/index_html
>
> Mcic> AFAIK, this is the only fully working (beta)
> implementation of anything
> Python >> .Net. take a look at the link contents for details on this
> Mcic> implementation. basically, it allows CPython access to
> .Net, but not the
> Mcic> other way around. it is not "Python.Net", but CPython
> *for* .Net . . . .
> Mcic> IMHO, it's quite good, and others on the list seem to
> like what it does/how
> Mcic> it works.
>
>
>
>
>
>
> Mcic> _________________________________________________
> Mcic> Python.NET mailing list - PythonDotNet(a)python.org
> Mcic> http://mail.python.org/mailman/listinfo/pythondotnet
>
>
>
> _________________________________________________
> Python.NET mailing list - PythonDotNet(a)python.org
> http://mail.python.org/mailman/listinfo/pythondotnet
>
(whoops, sent to individual instead of mailing list first)
i heard a lot of this mysterious "CPython", could anybody give me a
hint on how to get it and check it out? google doesnt give me useful
resuls.
would be charming, if it is an x86 compiler for a typed python dialect
:)
-- Leonard Ritter
-- paniq(a)gmx.net
-- http://www.paniq.de
-- http://www.the-mu.com
Friday, February 27, 2004, 8:03:25 PM, you wrote:
Mcic> Actually, Hawkeye (and the OP) , it's yet a bit more than that. I agree
Mcic> that "it is not "Python.Net"" (it does not make Python run as a .NET
Mcic> language), but it does allow you to embed Python in a .NET app. In fact, as
Mcic> you're probably aware, the python.exe that you run when using Python for
Mcic> .NET in the conventional way (see documented examples) is a simple example
Mcic> of embedding Python in a (very small) .NET app, which in turn provides
Mcic> access to a very rich interface for calling back in the other direction.
Mcic> The viewpoint one has while using Python for .NET in the conventional way
Mcic> (running the CLR-enabled python.exe, importing CLR, and going from there)
Mcic> is that the CLR is acting as an appendage to Python. But in fact there is a
Mcic> duality in this architecture that is subtle but amazingly flexible. You
Mcic> might say that in this clever framework, Python and .NET are mutually
Mcic> embedded, which offers some unique possibilities similar to, but even more
Mcic> powerful than, Jython.
Mcic> BTW, Jython is pretty cool too, but I prefer Python for .NET because it is
Mcic> real CPython, so almost anything that you could do in Python before
Mcic> integrating it with .NET, you can still do. In Jython, any python code has
Mcic> to be available in source form so it can be compiled by Jython (at least
Mcic> once) whereasany Python byte code that works with the currently supported
Mcic> Python runtime (2.3 currently) will work with Python for .NET.
Mcic> The somewhat downplayed embedding capability is the aspect of Python for
Mcic> .NET that I am personally finding very useful, with the "calling back to
Mcic> .NET" feature being a very nice bonus, allowing the embedding .NET app to
Mcic> use Python to allow it to sort-of "script into itself".
Mcic> P.S. -- I am not contradicting you (your answer to the OP is completely
Mcic> correct) -- I'm just adding a viewpoint that I have found useful.
Mcic> ==========================
Mcic> Mark Barclay
Mcic> Sr. Software Engineer, CRi Inc.
Mcic> |---------+------------------------------->
Mcic> | | "Hawkeye Parker" |
Mcic> | | <hawkeye.parker@auto|
Mcic> | | desk.com> |
Mcic> | | Sent by: |
Mcic> | | pythondotnet-bounces|
Mcic> | | @python.org |
Mcic> | | |
Mcic> | | |
Mcic> | | 02/27/2004 01:05 PM |
Mcic> | | |
Mcic> |---------+------------------------------->
Mcic>
Mcic> >-------------------------------------------------------------------------------------------------------------------|
Mcic> |
Mcic> | To: <srussell(a)lotmate.com>,
Mcic> <pythondotnet(a)python.org>
Mcic> | cc:
Mcic> | Subject: RE: [Python.NET] Anyone or anything
Mcic> happening here? |
Mcic>
Mcic> >-------------------------------------------------------------------------------------------------------------------|
Mcic> http://www.zope.org/Members/Brian/PythonNet/index_html
Mcic> AFAIK, this is the only fully working (beta) implementation of anything
Python >> .Net. take a look at the link contents for details on this
Mcic> implementation. basically, it allows CPython access to .Net, but not the
Mcic> other way around. it is not "Python.Net", but CPython *for* .Net . . . .
Mcic> IMHO, it's quite good, and others on the list seem to like what it does/how
Mcic> it works.
Mcic> _________________________________________________
Mcic> Python.NET mailing list - PythonDotNet(a)python.org
Mcic> http://mail.python.org/mailman/listinfo/pythondotnet