[Python.NET] python for .net

Brian Lloyd brian.d.lloyd at gmail.com
Mon Jan 23 05:31:19 CET 2012


Hi Scot - generally CLR namespaces == python modules, so after loading the
assembly I think you
need to import NSPC. I don't have any VB experience, so not sure offhand
what a VB Module
corresponds to / compiles to in managed code (guessing it corresponds to a
compilation unit at
first glance, but its been a long while since I've worked on this stuff).

Short story, whatever Imports statements you would use to use your code in
an external VB file
would need to have corresponding import statements in Python code...

Hope this helps!

 - Brian

On Fri, Jan 20, 2012 at 4:37 PM, Scot Kelly <skelly at passportcapital.com>wrote:

> **
>
> ** **
>
> Re: Python for .NET.****
>
> ** **
>
> All,****
>
> I have created a Clr example which instantiates and manipulates objects of
> type System.String.****
>
> When I try to have it access the HelloWorld-esque object that I have
> written in VB.net python throws an error on my import statement.****
>
> In the docs on “Python for .NET” there’s no discussion on what form the
> .NET code must be in to be reachable, in terms of namespaces, modules and
> classes.****
>
> Here’s my code. I’ve tried encapsulating my class in Namespace and Module
> wrappers no avail.  Perhaps the lack of a PublicKeyToken is an ssue.****
>
> Any help you can provide would be greatly appreciated.****
>
> ** **
>
> ** **
>
> Feedback...****
>
> I thought the process was fairly simple.  ****
>
> 0. The inclusion of python.exe in the .zip merits annotating as optional.*
> ***
>
> 1. The readme should probably discuss the need to
> sys.path.append('c:\Python25\Lib\site-packages\Clr').****
>
> 2. I thought the filename, Python.Runtime.dll, was too generic.  Is it
> within your abilities to name it more specifically, such as
> PythonForNet.Runtime.dll****
>
> 3. There’s no reference section for the methods of clr. (ex: what is the
> parameter to and usage of ListAssemblies?)****
>
> ** **
>
> Regards,****
>
> **Scot Kelly**.****
>
> ** **
>
> ps: a little revolution now and then is a good thing****
>
> ** **
>
> ** **
>
> ** **
>
> python code:****
>
> import sys****
>
> ** **
>
> ** **
>
> sys.path.append('c:\Python25\Lib\site-packages\Clr') #Contains two files:
> clr.pyd and Python.Runtime.dll.****
>
> import clr****
>
> ** **
>
> ** **
>
> ** **
>
> def CallMicrosoftStringClass():****
>
>     #clr.AddReference("System")****
>
>     #from System import String as DotNetString****
>
>     #print clr.ListAssemblies()****
>
> ** **
>
>     print 'Welcome to CallMicrosoftStringClass.'****
>
>     a = DotNetString('SomeTyping')****
>
>     print 'a is a instance of ' + a.__class__.__name__****
>
>     print 'a', a****
>
>     b = DotNetString(a.PadRight(15, 'x'))****
>
>     print 'b', b****
>
>     c = DotNetString(b.Insert(2,'abc'))****
>
>     print 'c', c    ****
>
>     d = c.ToUpper() #note: not calling construtor of DotNetString here.***
> *
>
>     print 'd', d****
>
>     print 'd is a instance of ' + d.__class__.__name__****
>
>     print 'Leaving CallMicrosoftStringClass.'****
>
> ** **
>
> ** **
>
> ** **
>
> def CallObjectTwo():****
>
>     #Import System ****
>
>     clr.AddReference("CallbackExample")****
>
>     print '1111.a'****
>
>     print clr.FindAssembly("CallbackExample")****
>
>     print '1111.b'****
>
>     #print dir(clr)****
>
>     for e in clr.ListAssemblies(0):****
>
>         print e****
>
>     print '1111.c'****
>
>     for e in clr.ListAssemblies(1):****
>
>         print e****
>
>     print '1111.d'****
>
>     #from Namspacexyz import ObjectTwo as ObjDos****
>
>     #import CallbackExample****
>
>     import ObjectTwo ****
>
>     print '2222'****
>
>     ****
>
>     print 'Welcome to CallObjectTwo.'****
>
>     #a = ObjectDos('SomeTyping')****
>
>     #print 'a is a instance of ' + a.__class__.__name__****
>
>     print 'Leaving CallObjectTwo.'****
>
>   ****
>
> ** **
>
> ** **
>
> print '  '****
>
> print '  '****
>
> print '  '****
>
> print '  '****
>
> print '  '****
>
> print '  '****
>
> print '  '****
>
> #for element in sys.path:****
>
> #  print element****
>
> print 'Welcome to Scots_ClrExample      '*3****
>
> CallObjectTwo()****
>
> print 'Finished with Scots_ClrExample      '*3****
>
> ** **
>
> ** **
>
> ** **
>
> ** **
>
> VB.net code:****
>
> Imports System****
>
> 'Imports System.Collections.ArrayList****
>
> ** **
>
> 'Imports System.Collections****
>
> 'Imports System.Threading****
>
> 'Imports System.Runtime.InteropServices****
>
> 'Imports System.Data.Sql****
>
> 'Imports System.Data.SqlTypes****
>
> 'Imports Microsoft.SqlServer.Server****
>
> ''Imports System.Win32Native****
>
> ** **
>
> 'How to compile:****
>
> ' cd "c:\Program Files\Microsoft Sql Server"****
>
> ' path=%path%;%SystemRoot%\Microsoft.NET\Framework\v2.0.50727****
>
> ' vbc /target:library  "CallbackExample.vb" | more****
>
> ** **
>
> ** **
>
> Public Namespace NSPC****
>
> ** **
>
> 'Public Module CallbackExample****
>
> ** **
>
>   'Demo callback functionality****
>
>   Public Class ObjectTwo****
>
>      Public Shared Function HelloWorld() as string****
>
>           return "Hello World from ObjectTwo !!"****
>
>      End Function****
>
>   ****
>
>       Public Shared Function TestCallBack(obj As Object) As string****
>
>           Dim sResult as string****
>
>           sResult = "Hello"****
>
>           obj.CallbackFunc237(sResult) ****
>
>           sResult = "Goodbye"****
>
>           return sResult 'new SqlChars(CSTR(sResult)) ****
>
>       End Function****
>
>   End Class****
>
> ** **
>
> 'End Module****
>
> End Namespace****
>
> ** **
>
> ** **
>
>     *                                *
> PASSPORT CAPITAL
> 30 Hotaling Place, Suite 300
> San Francisco, CA 94111
> www.passportcapital.com
>
> *Scot Kelly
> *Tel 415-525-8910
> Fax 415-321-4620
> skelly at passportcapital.com
>
>
>
>
> CONFIDENTIALITY: The information transmitted is intended only for the
> addressee and may contain confidential or privileged material. Any review,
> retransmission, dissemination or other use of, or taking of any action in
> reliance upon, this information by anyone other than the intended recipient
> is prohibited. If you received this message in error, please contact the
> sender and delete the material from all computers.
>
> E-mail transmissions cannot be guaranteed to be secure or error-free and
> we accept no liability for any errors or omissions. We screen for viruses
> on a routine basis, however, we make no representation or warranty as to
> the absence of viruses in this e-mail or any attachments.  Please note that
> for regulatory and compliance reasons we may monitor and read e-mails sent
> to and from our servers.
>
> Passport Capital, LLC and its affiliated investment vehicles are actively
> engaged in investment management and evaluation activities in global
> markets and do not wish to be exposed to any nonpublic information
> regarding any company(s) or security(s) that could be deemed material and
> restrict their ability to trade securities without their express prior
> consent.
>
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org
> http://mail.python.org/mailman/listinfo/pythondotnet
>



-- 
Brian Lloyd
brian.d.lloyd at gmail.com
(c) 540.845.2975
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20120122/da380744/attachment-0001.html>


More information about the PythonDotNet mailing list