Lean  $LEAN_TAG$
PythonUtil.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.Linq;
19 using Python.Runtime;
20 using System.Collections.Generic;
22 using System.Text.RegularExpressions;
24 using System.IO;
25 using System.Globalization;
26 
27 namespace QuantConnect.Util
28 {
29  /// <summary>
30  /// Collection of utils for python objects processing
31  /// </summary>
32  public class PythonUtil
33  {
34  private static Regex LineRegex = new Regex("line (\\d+)", RegexOptions.Compiled);
35  private static Regex StackTraceFileLineRegex = new Regex("\"(.+)\", line (\\d+), in (.+)", RegexOptions.Compiled | RegexOptions.Singleline);
36  private static readonly Lazy<dynamic> lazyInspect = new Lazy<dynamic>(() => Py.Import("inspect"));
37 
38  /// <summary>
39  /// The python exception stack trace line shift to use
40  /// </summary>
41  public static int ExceptionLineShift { get; set; } = 0;
42 
43  /// <summary>
44  /// Encapsulates a python method with a <see cref="System.Action{T1}"/>
45  /// </summary>
46  /// <typeparam name="T1">The input type</typeparam>
47  /// <param name="pyObject">The python method</param>
48  /// <returns>A <see cref="System.Action{T1}"/> that encapsulates the python method</returns>
49  public static Action<T1> ToAction<T1>(PyObject pyObject)
50  {
51  using (Py.GIL())
52  {
53  long count = 0;
54  if (!TryGetArgLength(pyObject, out count) || count != 1)
55  {
56  return null;
57  }
58  dynamic method = GetModule().GetAttr("to_action1");
59  return method(pyObject, typeof(T1)).AsManagedObject(typeof(Action<T1>));
60  }
61  }
62 
63  /// <summary>
64  /// Encapsulates a python method with a <see cref="System.Action{T1, T2}"/>
65  /// </summary>
66  /// <typeparam name="T1">The first input type</typeparam>
67  /// <typeparam name="T2">The second input type type</typeparam>
68  /// <param name="pyObject">The python method</param>
69  /// <returns>A <see cref="System.Action{T1, T2}"/> that encapsulates the python method</returns>
70  public static Action<T1, T2> ToAction<T1, T2>(PyObject pyObject)
71  {
72  using (Py.GIL())
73  {
74  long count = 0;
75  if (!TryGetArgLength(pyObject, out count) || count != 2)
76  {
77  return null;
78  }
79  dynamic method = GetModule().GetAttr("to_action2");
80  return method(pyObject, typeof(T1), typeof(T2)).AsManagedObject(typeof(Action<T1, T2>));
81  }
82  }
83 
84  /// <summary>
85  /// Encapsulates a python method with a <see cref="System.Func{T1, T2}"/>
86  /// </summary>
87  /// <typeparam name="T1">The data type</typeparam>
88  /// <typeparam name="T2">The output type</typeparam>
89  /// <param name="pyObject">The python method</param>
90  /// <returns>A <see cref="System.Func{T1, T2}"/> that encapsulates the python method</returns>
91  public static Func<T1, T2> ToFunc<T1, T2>(PyObject pyObject)
92  {
93  using (Py.GIL())
94  {
95  long count = 0;
96  if (!TryGetArgLength(pyObject, out count) || count != 1)
97  {
98  return null;
99  }
100  dynamic method = GetModule().GetAttr("to_func1");
101  return method(pyObject, typeof(T1), typeof(T2)).AsManagedObject(typeof(Func<T1, T2>));
102  }
103  }
104 
105  /// <summary>
106  /// Encapsulates a python method with a <see cref="System.Func{T1, T2, T3}"/>
107  /// </summary>
108  /// <typeparam name="T1">The first argument's type</typeparam>
109  /// <typeparam name="T2">The first argument's type</typeparam>
110  /// <typeparam name="T3">The output type</typeparam>
111  /// <param name="pyObject">The python method</param>
112  /// <returns>A <see cref="System.Func{T1, T2, T3}"/> that encapsulates the python method</returns>
113  public static Func<T1, T2, T3> ToFunc<T1, T2, T3>(PyObject pyObject)
114  {
115  using (Py.GIL())
116  {
117  long count = 0;
118  if (!TryGetArgLength(pyObject, out count) || count != 2)
119  {
120  return null;
121  }
122  dynamic method = GetModule().GetAttr("to_func2");
123  return method(pyObject, typeof(T1), typeof(T2), typeof(T3)).AsManagedObject(typeof(Func<T1, T2, T3>));
124  }
125  }
126 
127  /// <summary>
128  /// Encapsulates a python method in coarse fundamental universe selector.
129  /// </summary>
130  /// <param name="pyObject">The python method</param>
131  /// <returns>A <see cref="Func{T, TResult}"/> (parameter is <see cref="IEnumerable{CoarseFundamental}"/>, return value is <see cref="IEnumerable{Symbol}"/>) that encapsulates the python method</returns>
132  public static Func<IEnumerable<CoarseFundamental>, IEnumerable<Symbol>> ToCoarseFundamentalSelector(PyObject pyObject)
133  {
134  var selector = ToFunc<IEnumerable<CoarseFundamental>, Symbol[]>(pyObject);
135  if (selector == null)
136  {
137  using (Py.GIL())
138  {
139  throw new ArgumentException($"{pyObject.Repr()} is not a valid coarse fundamental universe selector method.");
140  }
141  }
142  return selector;
143  }
144 
145  /// <summary>
146  /// Encapsulates a python method in fine fundamental universe selector.
147  /// </summary>
148  /// <param name="pyObject">The python method</param>
149  /// <returns>A <see cref="Func{T, TResult}"/> (parameter is <see cref="IEnumerable{FineFundamental}"/>, return value is <see cref="IEnumerable{Symbol}"/>) that encapsulates the python method</returns>
150  public static Func<IEnumerable<FineFundamental>, IEnumerable<Symbol>> ToFineFundamentalSelector(PyObject pyObject)
151  {
152  var selector = ToFunc<IEnumerable<FineFundamental>, Symbol[]>(pyObject);
153  if (selector == null)
154  {
155  using (Py.GIL())
156  {
157  throw new ArgumentException($"{pyObject.Repr()} is not a valid fine fundamental universe selector method.");
158  }
159  }
160  return selector;
161  }
162 
163  /// <summary>
164  /// Parsers <see cref="PythonException"/> into a readable message
165  /// </summary>
166  /// <param name="pythonException">The exception to parse</param>
167  /// <returns>String with relevant part of the stacktrace</returns>
168  public static string PythonExceptionParser(PythonException pythonException)
169  {
170  return PythonExceptionMessageParser(pythonException.Message) + PythonExceptionStackParser(pythonException.StackTrace);
171  }
172 
173  /// <summary>
174  /// Parsers <see cref="PythonException.Message"/> into a readable message
175  /// </summary>
176  /// <param name="message">The python exception message</param>
177  /// <returns>String with relevant part of the stacktrace</returns>
178  public static string PythonExceptionMessageParser(string message)
179  {
180  var match = LineRegex.Match(message);
181  if (match.Success)
182  {
183  foreach (Match lineCapture in match.Captures)
184  {
185  var newLineNumber = int.Parse(lineCapture.Groups[1].Value) + ExceptionLineShift;
186  message = Regex.Replace(message, lineCapture.ToString(), $"line {newLineNumber}");
187  }
188  }
189  else if (message.Contains(" value cannot be converted to ", StringComparison.InvariantCulture))
190  {
191  message += ": This error is often encountered when assigning to a member defined in the base QCAlgorithm class. For example, self.universe conflicts with 'QCAlgorithm.Universe' but can be fixed by prefixing private variables with an underscore, self._universe.";
192  }
193 
194  return message;
195  }
196 
197  /// <summary>
198  /// Parsers <see cref="PythonException.StackTrace"/> into a readable message
199  /// </summary>
200  /// <param name="value">String with the stacktrace information</param>
201  /// <returns>String with relevant part of the stacktrace</returns>
202  public static string PythonExceptionStackParser(string value)
203  {
204  if (string.IsNullOrWhiteSpace(value))
205  {
206  return string.Empty;
207  }
208 
209  // The stack trace info before "at Python.Runtime." is the trace we want,
210  // which is for user Python code.
211  var endIndex = value.IndexOf("at Python.Runtime.", StringComparison.InvariantCulture);
212  var neededStackTrace = endIndex > 0 ? value.Substring(0, endIndex) : value;
213 
214  // The stack trace is separated in blocks by file
215  var blocks = neededStackTrace.Split(" File ", StringSplitOptions.RemoveEmptyEntries)
216  .Select(fileTrace =>
217  {
218  var trimedTrace = fileTrace.Trim();
219  if (string.IsNullOrWhiteSpace(trimedTrace))
220  {
221  return string.Empty;
222  }
223 
224  var match = StackTraceFileLineRegex.Match(trimedTrace);
225  if (!match.Success)
226  {
227  return string.Empty;
228  }
229 
230  var capture = match.Captures[0] as Match;
231 
232  var filePath = capture.Groups[1].Value;
233  var lastFileSeparatorIndex = Math.Max(filePath.LastIndexOf('/'), filePath.LastIndexOf('\\'));
234  if (lastFileSeparatorIndex < 0)
235  {
236  return string.Empty;
237  }
238 
239  var fileName = filePath.Substring(lastFileSeparatorIndex + 1);
240  var lineNumber = int.Parse(capture.Groups[2].Value, CultureInfo.InvariantCulture) + ExceptionLineShift;
241  var locationAndInfo = capture.Groups[3].Value.Trim();
242 
243  return $" at {locationAndInfo}{Environment.NewLine} in {fileName}: line {lineNumber}";
244  })
245  .Where(x => !string.IsNullOrWhiteSpace(x));
246 
247  var result = string.Join(Environment.NewLine, blocks);
248  result = Extensions.ClearLeanPaths(result);
249 
250  return string.IsNullOrWhiteSpace(result)
251  ? string.Empty
252  : $"{Environment.NewLine}{result}{Environment.NewLine}";
253  }
254 
255  /// <summary>
256  /// Try to get the length of arguments of a method
257  /// </summary>
258  /// <param name="pyObject">Object representing a method</param>
259  /// <param name="length">Lenght of arguments</param>
260  /// <returns>True if pyObject is a method</returns>
261  private static bool TryGetArgLength(PyObject pyObject, out long length)
262  {
263  using (Py.GIL())
264  {
265  var inspect = lazyInspect.Value;
266  if (inspect.isfunction(pyObject))
267  {
268  var args = inspect.getfullargspec(pyObject).args as PyObject;
269  var pyList = new PyList(args);
270  length = pyList.Length();
271  pyList.Dispose();
272  args.Dispose();
273  return true;
274  }
275 
276  if (inspect.ismethod(pyObject))
277  {
278  var args = inspect.getfullargspec(pyObject).args as PyObject;
279  var pyList = new PyList(args);
280  length = pyList.Length() - 1;
281  pyList.Dispose();
282  args.Dispose();
283  return true;
284  }
285  }
286  length = 0;
287  return false;
288  }
289 
290  /// <summary>
291  /// Creates a python module with utils methods
292  /// </summary>
293  /// <returns>PyObject with a python module</returns>
294  private static PyObject GetModule()
295  {
296  return PyModule.FromString("x",
297  "from clr import AddReference\n" +
298  "AddReference(\"System\")\n" +
299  "from System import Action, Func\n" +
300  "def to_action1(pyobject, t1):\n" +
301  " return Action[t1](pyobject)\n" +
302  "def to_action2(pyobject, t1, t2):\n" +
303  " return Action[t1, t2](pyobject)\n" +
304  "def to_func1(pyobject, t1, t2):\n" +
305  " return Func[t1, t2](pyobject)\n" +
306  "def to_func2(pyobject, t1, t2, t3):\n" +
307  " return Func[t1, t2, t3](pyobject)");
308  }
309 
310  /// <summary>
311  /// Convert Python input to a list of Symbols
312  /// </summary>
313  /// <param name="input">Object with the desired property</param>
314  /// <returns>List of Symbols</returns>
315  public static IEnumerable<Symbol> ConvertToSymbols(PyObject input)
316  {
317  List<Symbol> symbolsList;
318  Symbol symbol;
319 
320  using (Py.GIL())
321  {
322  // Handle the possible types of conversions
323  if (PyList.IsListType(input))
324  {
325  List<string> symbolsStringList;
326 
327  //Check if an entry in the list is a string type, if so then try and convert the whole list
328  if (PyString.IsStringType(input[0]) && input.TryConvert(out symbolsStringList))
329  {
330  symbolsList = new List<Symbol>();
331  foreach (var stringSymbol in symbolsStringList)
332  {
333  symbol = QuantConnect.Symbol.Create(stringSymbol, SecurityType.Equity, Market.USA);
334  symbolsList.Add(symbol);
335  }
336  }
337  //Try converting it to list of symbols, if it fails throw exception
338  else if (!input.TryConvert(out symbolsList))
339  {
340  throw new ArgumentException($"Cannot convert list {input.Repr()} to symbols");
341  }
342  }
343  else
344  {
345  //Check if its a single string, and try and convert it
346  string symbolString;
347  if (PyString.IsStringType(input) && input.TryConvert(out symbolString))
348  {
349  symbol = QuantConnect.Symbol.Create(symbolString, SecurityType.Equity, Market.USA);
350  symbolsList = new List<Symbol> { symbol };
351  }
352  else if (input.TryConvert(out symbol))
353  {
354  symbolsList = new List<Symbol> { symbol };
355  }
356  else
357  {
358  throw new ArgumentException($"Cannot convert object {input.Repr()} to symbol");
359  }
360  }
361  }
362  return symbolsList;
363  }
364  }
365 }