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  if (!typeof(TInterface).IsInterface)
38  {
39  throw new ArgumentException(
40  $"{nameof(PythonWrapper)}.{nameof(ValidateImplementationOf)}(): {Messages.PythonWrapper.ExpectedInterfaceTypeParameter}");
41  }
42 
43  var missingMembers = new List<string>();
44  var members = typeof(TInterface).GetMembers(BindingFlags.Public | BindingFlags.Instance);
45  using (Py.GIL())
46  {
47  foreach (var member in members)
48  {
49  if ((member is not MethodInfo method || !method.IsSpecialName) &&
50  !model.HasAttr(member.Name) && !model.HasAttr(member.Name.ToSnakeCase()))
51  {
52  missingMembers.Add(member.Name);
53  }
54  }
55 
56  if (missingMembers.Any())
57  {
58  throw new NotImplementedException(
59  Messages.PythonWrapper.InterfaceNotFullyImplemented(typeof(TInterface).Name, model.GetPythonType().Name, missingMembers));
60  }
61  }
62 
63  return model;
64  }
65 
66  /// <summary>
67  /// Invokes the specified method on the provided <see cref="PyObject"/> instance with the specified arguments
68  /// </summary>
69  /// <param name="model">The <see cref="PyObject"/> instance</param>
70  /// <param name="methodName">The name of the method to invoke</param>
71  /// <param name="args">The arguments to call the method with</param>
72  /// <returns>The return value of the called method converted into the <typeparamref name="T"/> type</returns>
73  public static T InvokeMethod<T>(this PyObject model, string methodName, params object[] args)
74  {
75  using var _ = Py.GIL();
76  return InvokeMethodImpl(model, methodName, args).GetAndDispose<T>();
77  }
78 
79  /// <summary>
80  /// Invokes the specified method on the provided <see cref="PyObject"/> instance with the specified arguments
81  /// </summary>
82  /// <param name="model">The <see cref="PyObject"/> instance</param>
83  /// <param name="methodName">The name of the method to invoke</param>
84  /// <param name="args">The arguments to call the method with</param>
85  public static void InvokeMethod(this PyObject model, string methodName, params object[] args)
86  {
87  InvokeMethodImpl(model, methodName, args);
88  }
89 
90  /// <summary>
91  /// Invokes the given <see cref="PyObject"/> method with the specified arguments
92  /// </summary>
93  /// <param name="method">The method to invoke</param>
94  /// <param name="args">The arguments to call the method with</param>
95  /// <returns>The return value of the called method converted into the <typeparamref name="T"/> type</returns>
96  public static T Invoke<T>(this PyObject method, params object[] args)
97  {
98  using var _ = Py.GIL();
99  return InvokeMethodImpl(method, args).GetAndDispose<T>();
100  }
101 
102  /// <summary>
103  /// Invokes the given <see cref="PyObject"/> method with the specified arguments
104  /// </summary>
105  /// <param name="method">The method to invoke</param>
106  /// <param name="args">The arguments to call the method with</param>
107  public static PyObject Invoke(this PyObject method, params object[] args)
108  {
109  return InvokeMethodImpl(method, args);
110  }
111 
112  private static PyObject InvokeMethodImpl(PyObject model, string methodName, params object[] args)
113  {
114  using var _ = Py.GIL();
115  PyObject method = model.GetMethod(methodName);
116  return InvokeMethodImpl(method, args);
117  }
118 
119  private static PyObject InvokeMethodImpl(PyObject method, params object[] args)
120  {
121  using var _ = Py.GIL();
122  return method.Invoke(args.Select(arg => arg.ToPython()).ToArray());
123  }
124  }
125 }