[Python.NET] Using a .Net DLL
Brian Lloyd
brian at zope.com
Fri Mar 12 16:54:44 EST 2004
Hi Sean -
What I think you're trying to do here hadn't really occured to
me before ;)
Generally, you don't need to make your C# class "pythonic" in any
way (__xxx__ methods, etc.) The idea of defining things like
__and__ in C# is interesting. It may work for some "special"
methods - I've never really tried it.
It generally _won't_ work for __init__ though - that one is
special on a few different levels. You really don't need a
special Pythonic constructor - I suggest just removing that
and making it a regular constructor overload (which should
work fine from Python).
Brian Lloyd brian at zope.com
V.P. Engineering 540.361.1716
Zope Corporation http://www.zope.com
> -----Original Message-----
> From: pythondotnet-bounces at python.org
> [mailto:pythondotnet-bounces at python.org]On Behalf Of Sean McKay
> Sent: Friday, March 12, 2004 2:03 PM
> To: pythondotnet at python.org
> Subject: [Python.NET] Using a .Net DLL
>
>
> 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 youre looking for faster
> http://search.yahoo.com
>
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org
> http://mail.python.org/mailman/listinfo/pythondotnet
>
More information about the PythonDotNet
mailing list