I am currently working on a simple integration module in python, which will enable python users to call table-valued functions of the .net library finaquant protos.
Calling .NET objects from Python is quite easy if you install a Python module named pythonnet using the PIP package manager, that automatically downloads and installs Python modules.
After installing PIP on your computer, run following command in your terminal (command line window) in order to install Python for NET:
- Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
- pip install pythonnet
- GeSHi ©
You can now access .NET classes using the Python module named CLR:
- Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
- import clr
- import time
- from System.Windows.Forms import Form # import a .NET class
- clr.AddReference("System.Windows.Forms") # add an assemby reference
- f = Form() # create a Form object in Python
- f.Show()
- time.sleep(3) # wait 3 seconds before closing the form
- f.Close()
- GeSHi ©
You can also import classes from your custom .NET assembly (dll file) in Python provided that your assemby resides in the same directory with the calling Python script:
- Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
- import clr
- from MySimpleMath import SimpleMath as sm # import static class named SimpleMath
- print sm.AddNumbers(7, 9)
- GeSHi ©
Following Python code example shows how a NumPy array can be converted to a .NET array:
- Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
- import clr
- import sys
- import numpy as np
- import System
- def ConvertNumPyToNETarray(nparr):
- a = np.array([1])
- # check if nparr is a NumPy array
- if type(nparr) != type(a):
- print "Wrong input type: Input argument must be a NumPy array!"
- raise Exception('Wrong Input Type: Input argument must be a NumPy array!')
- # check dimension of nparr
- dims = len(nparr.shape)
- if dims > 2:
- raise Exception('Too Many Dimensions: The array to be converted cannot have more than 2 dimensions!')
- if dims == 1:
- arrtype = type(nparr[0]).__name__
- (x1,) = nparr.shape
- else:
- arrtype = type(nparr[0,0]).__name__
- (x1,x2) = nparr.shape
- # check type of nparr
- if arrtype.find('float') > -1:
- if dims == 1:
- arr = Array.CreateInstance(System.Double, x1)
- for ind in range(x1):
- arr[ind] = nparr[ind]
- else: # dims == 2
- arr = Array.CreateInstance(System.Double, x1, x2)
- for ind1 in range(x1):
- for ind2 in range(x2):
- arr[ind1, ind2] = nparr[ind1, ind2]
- elif arrtype.find('int') > -1:
- if dims == 1:
- arr = Array.CreateInstance(System.Int64, x1)
- for ind in range(x1):
- arr[ind] = nparr[ind]
- else: # dims == 2
- arr = Array.CreateInstance(System.Int64, x1, x2)
- for ind1 in range(x1):
- for ind2 in range(x2):
- arr[ind1, ind2] = nparr[ind1, ind2]
- elif arrtype.find('string') > -1:
- if dims == 1:
- arr = Array.CreateInstance(System.String, x1)
- for ind in range(x1):
- arr[ind] = nparr[ind].tostring()
- else: # dims == 2
- arr = Array.CreateInstance(System.String, x1, x2)
- for ind1 in range(x1):
- for ind2 in range(x2):
- arr[ind1, ind2] = nparr[ind1, ind2].tostring()
- else:
- raise Exception('Invalid Data Type: Data type of the input array must be float, int or string.')
- return arr
- #x = 5
- # float
- x = np.array([1.5, 2, 3, 4.3, 5.2], float)
- xnet = ConvertNumPyToNETarray(x)
- print "xnet[1] = " , xnet[1]
- print "xnet[4] = " , xnet[4]
- x = np.array([[1.5, 2, 4],[3,5,7]], float)
- xnet = ConvertNumPyToNETarray(x)
- print "xnet[1,0] = " , xnet[1,0]
- print "xnet[1,2] = " , xnet[1,2]
- # int
- x = np.array([1, 2, 3, 4, 5], int)
- xnet = ConvertNumPyToNETarray(x)
- print "xnet[1] = " , xnet[1]
- print "xnet[4] = " , xnet[4]
- x = np.array([[1, 3, 5],[9,11,15]], int)
- xnet = ConvertNumPyToNETarray(x)
- print "xnet[1,0] = " , xnet[1,0]
- print "xnet[1,2] = " , xnet[1,2]
- # string
- x = np.array(['ali', 'veli', 'hasan', 'kamil'], str)
- xnet = ConvertNumPyToNETarray(x)
- print "xnet[1] = " , xnet[1]
- print "xnet[3] = " , xnet[3]
- x = np.array([['ali', 'veli', 'hasan'],['semih','dursun', 'kamil']], str)
- xnet = ConvertNumPyToNETarray(x)
- print "xnet[1,0] = " , xnet[1,0]
- print "xnet[1,1] = " , xnet[1,1]
- # to prevent CMD from closing quickly
- raw_input()
- GeSHi ©
Tunc