Lean  $LEAN_TAG$
PythonWrapper.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using System.Reflection;
21 using Python.Runtime;
22 
23 namespace QuantConnect.Python
24 {
25  /// <summary>
26  /// Provides extension methods for managing python wrapper classes
27  /// </summary>
28  public static class PythonWrapper
29  {
30  /// <summary>
31  /// Validates that the specified <see cref="PyObject"/> completely implements the provided interface type
32  /// </summary>
33  /// <typeparam name="TInterface">The interface type</typeparam>
34  /// <param name="model">The model implementing the interface type</param>
35  public static PyObject ValidateImplementationOf<TInterface>(this PyObject model)
36  {
37  var notInterface = !typeof(TInterface).IsInterface;
38  var missingMembers = new List<string>();
39  var members = typeof(TInterface).GetMembers(BindingFlags.Public | BindingFlags.Instance);
40  using (Py.GIL())
41  {
42  foreach (var member in members)
43  {
44  var method = member as MethodInfo;
45  if ((method == null || !method.IsSpecialName) &&
46  !model.HasAttr(member.Name) && !model.HasAttr(member.Name.ToSnakeCase()))
47  {
48  if (notInterface)
49  {
50  if (method != null && !method.IsAbstract && (method.IsFinal || !method.IsVirtual || method.DeclaringType != typeof(TInterface)))
51  {
52  continue;
53  }
54  else if (member is ConstructorInfo)
55  {
56  continue;
57  }
58  else if (member.Name is "ToString")
59  {
60  continue;
61  }
62  }
63  missingMembers.Add(member.Name);
64  }
65  }
66 
67  if (missingMembers.Any())
68  {
69  throw new NotImplementedException(
70  Messages.PythonWrapper.InterfaceNotFullyImplemented(typeof(TInterface).Name, model.GetPythonType().Name, missingMembers));
71  }
72  }
73 
74  return model;
75  }
76 
77  /// <summary>
78  /// Invokes the specified method on the provided <see cref="PyObject"/> instance with the specified arguments
79  /// </summary>
80  /// <param name="model">The <see cref="PyObject"/> instance</param>
81  /// <param name="methodName">The name of the method to invoke</param>
82  /// <param name="args">The arguments to call the method with</param>
83  /// <returns>The return value of the called method converted into the <typeparamref name="T"/> type</returns>
84  public static T InvokeMethod<T>(this PyObject model, string methodName, params object[] args)
85  {
86  using var _ = Py.GIL();
87  return InvokeMethodImpl(model, methodName, args).GetAndDispose<T>();
88  }
89 
90  /// <summary>
91  /// Invokes the specified method on the provided <see cref="PyObject"/> instance with the specified arguments
92  /// </summary>
93  /// <param name="model">The <see cref="PyObject"/> instance</param>
94  /// <param name="methodName">The name of the method to invoke</param>
95  /// <param name="args">The arguments to call the method with</param>
96  public static void InvokeMethod(this PyObject model, string methodName, params object[] args)
97  {
98  InvokeMethodImpl(model, methodName, args);
99  }
100 
101  /// <summary>
102  /// Invokes the given <see cref="PyObject"/> method with the specified arguments
103  /// </summary>
104  /// <param name="method">The method to invoke</param>
105  /// <param name="args">The arguments to call the method with</param>
106  /// <returns>The return value of the called method converted into the <typeparamref name="T"/> type</returns>
107  public static T Invoke<T>(this PyObject method, params object[] args)
108  {
109  using var _ = Py.GIL();
110  return InvokeMethodImpl(method, args).GetAndDispose<T>();
111  }
112 
113  /// <summary>
114  /// Invokes the given <see cref="PyObject"/> method with the specified arguments
115  /// </summary>
116  /// <param name="method">The method to invoke</param>
117  /// <param name="args">The arguments to call the method with</param>
118  public static PyObject Invoke(this PyObject method, params object[] args)
119  {
120  return InvokeMethodImpl(method, args);
121  }
122 
123  private static PyObject InvokeMethodImpl(PyObject model, string methodName, params object[] args)
124  {
125  using var _ = Py.GIL();
126  PyObject method = model.GetMethod(methodName);
127  return InvokeMethodImpl(method, args);
128  }
129 
130  private static PyObject InvokeMethodImpl(PyObject method, params object[] args)
131  {
132  using var _ = Py.GIL();
133  return method.Invoke(args.Select(arg => arg.ToPython()).ToArray());
134  }
135  }
136 }