From b9d3d999170f385e2371fc3fef1823f2ddd4c83b Mon Sep 17 00:00:00 2001 From: Martin-Molinero Date: Mon, 2 May 2022 14:38:50 -0300 Subject: [PATCH] Bump pythonNet version 2.0.12 (#6310) * Updates after pythonNet rebase * Bump pythonNet version 2.0.12 * Fix exception types being thrown --- .../QuantConnect.Algorithm.CSharp.csproj | 2 +- ...nOptimizationPortfolioConstructionModel.py | 1 + ...qualWeightingPortfolioConstructionModel.py | 1 + ...eOptimizationPortfolioConstructionModel.py | 1 + .../QuantConnect.Algorithm.Framework.csproj | 2 +- Algorithm.Python/CustomModelsAlgorithm.py | 3 + .../CustomWarmUpPeriodIndicatorAlgorithm.py | 1 + .../QuantConnect.Algorithm.Python.csproj | 2 +- .../RegisterIndicatorRegressionAlgorithm.py | 1 + Algorithm/QCAlgorithm.Python.cs | 3 +- Algorithm/QuantConnect.Algorithm.csproj | 2 +- .../Python/Wrappers/AlgorithmPythonWrapper.cs | 7 +- .../QuantConnect.AlgorithmFactory.csproj | 2 +- .../InvalidTokenPythonExceptionInterpreter.cs | 3 +- .../KeyErrorPythonExceptionInterpreter.cs | 15 +- ...NoMethodMatchPythonExceptionInterpreter.cs | 3 +- ...portedOperandPythonExceptionInterpreter.cs | 5 +- Common/Extensions.cs | 7 +- Common/Python/MarginCallModelPythonWrapper.cs | 9 +- Common/Python/PandasData.cs | 2 +- Common/Python/PythonSlice.cs | 6 +- Common/QuantConnect.csproj | 2 +- Common/Util/PythonUtil.cs | 4 +- Engine/QuantConnect.Lean.Engine.csproj | 2 +- Indicators/QuantConnect.Indicators.csproj | 2 +- Report/QuantConnect.Report.csproj | 2 +- Report/ReportElements/ChartReportElement.cs | 6 +- Research/QuantBook.cs | 3 +- Research/QuantConnect.Research.csproj | 2 +- Tests/Algorithm/AlgorithmHistoryTests.cs | 2 +- .../AlgorithmRegisterIndicatorTests.cs | 4 +- Tests/Algorithm/AlgorithmSetBrokerageTests.cs | 2 +- .../FrameworkModelsPythonInheritanceTests.cs | 6 +- ...mizationPortfolioConstructionModelTests.cs | 2 +- ...olioConstructionModelPythonWrapperTests.cs | 6 +- .../PortfolioConstructionModelTests.cs | 20 +- .../Portfolio/ReturnsSymbolDataTests.cs | 3 +- .../Common/Data/CalendarConsolidatorsTests.cs | 2 +- .../Data/Custom/PythonCustomDataTests.cs | 8 +- Tests/Common/Data/SliceTests.cs | 86 ++--- ...lidTokenPythonExceptionInterpreterTests.cs | 6 +- .../Notifications/NotificationManagerTests.cs | 3 +- .../BackwardsCompatibilityFeeModelTests.cs | 4 +- .../BackwardsCompatibilityFillModelsTests.cs | 12 +- .../Securities/DynamicSecurityDataTests.cs | 22 +- Tests/Common/Util/ExtensionsTests.cs | 34 +- .../PythonIndicatorNoinheritanceTests.cs | 6 +- ...PythonIndicatorNoinheritanceTestsLegacy.cs | 6 +- Tests/Indicators/PythonIndicatorTests.cs | 10 +- Tests/Python/AlgorithmPythonWrapperTests.cs | 6 +- .../DataConsolidatorPythonWrapperTests.cs | 11 +- Tests/Python/MethodOverloadTests.cs | 8 +- Tests/Python/NamedArgumentsTests.cs | 4 +- ...asConverterTests.BackwardsCompatibility.cs | 14 +- Tests/Python/PandasConverterTests.cs | 303 +++++++++--------- Tests/Python/PortfolioCustomModelTests.cs | 3 +- Tests/Python/PythonOptionTests.cs | 6 +- Tests/Python/PythonPackagesTests.cs | 4 +- Tests/Python/PythonWrapperTests.cs | 6 +- Tests/Python/SecurityCustomModelTests.cs | 2 +- Tests/QuantConnect.Tests.csproj | 2 +- Tests/Research/QuantBookFundamentalTests.cs | 2 +- Tests/ResearchRegressionTests.cs | 11 +- ToolBox/QuantConnect.ToolBox.csproj | 4 +- 64 files changed, 384 insertions(+), 347 deletions(-) diff --git a/Algorithm.CSharp/QuantConnect.Algorithm.CSharp.csproj b/Algorithm.CSharp/QuantConnect.Algorithm.CSharp.csproj index 2d2a2842e7eb..ae2beb03dcea 100644 --- a/Algorithm.CSharp/QuantConnect.Algorithm.CSharp.csproj +++ b/Algorithm.CSharp/QuantConnect.Algorithm.CSharp.csproj @@ -35,7 +35,7 @@ portable - + diff --git a/Algorithm.Framework/Portfolio/BlackLittermanOptimizationPortfolioConstructionModel.py b/Algorithm.Framework/Portfolio/BlackLittermanOptimizationPortfolioConstructionModel.py index ea239f635989..2de96b38dd01 100644 --- a/Algorithm.Framework/Portfolio/BlackLittermanOptimizationPortfolioConstructionModel.py +++ b/Algorithm.Framework/Portfolio/BlackLittermanOptimizationPortfolioConstructionModel.py @@ -51,6 +51,7 @@ def __init__(self, risk_free_rate(float): The risk free rate delta(float): The risk aversion coeffficient of the market portfolio tau(float): The model parameter indicating the uncertainty of the CAPM prior""" + super().__init__() self.lookback = lookback self.period = period self.resolution = resolution diff --git a/Algorithm.Framework/Portfolio/EqualWeightingPortfolioConstructionModel.py b/Algorithm.Framework/Portfolio/EqualWeightingPortfolioConstructionModel.py index 034b6b8e6f3b..67b2d026f0b7 100644 --- a/Algorithm.Framework/Portfolio/EqualWeightingPortfolioConstructionModel.py +++ b/Algorithm.Framework/Portfolio/EqualWeightingPortfolioConstructionModel.py @@ -28,6 +28,7 @@ def __init__(self, rebalance = Resolution.Daily, portfolioBias = PortfolioBias.L The function returns null if unknown, in which case the function will be called again in the next loop. Returning current time will trigger rebalance. portfolioBias: Specifies the bias of the portfolio (Short, Long/Short, Long)''' + super().__init__() self.portfolioBias = portfolioBias # If the argument is an instance of Resolution or Timedelta diff --git a/Algorithm.Framework/Portfolio/MeanVarianceOptimizationPortfolioConstructionModel.py b/Algorithm.Framework/Portfolio/MeanVarianceOptimizationPortfolioConstructionModel.py index cf3a5bdccac0..5fd83859dcb6 100644 --- a/Algorithm.Framework/Portfolio/MeanVarianceOptimizationPortfolioConstructionModel.py +++ b/Algorithm.Framework/Portfolio/MeanVarianceOptimizationPortfolioConstructionModel.py @@ -39,6 +39,7 @@ def __init__(self, period(int): The time interval of history price to calculate the weight resolution: The resolution of the history price optimizer(class): Method used to compute the portfolio weights""" + super().__init__() self.lookback = lookback self.period = period self.resolution = resolution diff --git a/Algorithm.Framework/QuantConnect.Algorithm.Framework.csproj b/Algorithm.Framework/QuantConnect.Algorithm.Framework.csproj index a30f0bb21b09..0c5d384be230 100644 --- a/Algorithm.Framework/QuantConnect.Algorithm.Framework.csproj +++ b/Algorithm.Framework/QuantConnect.Algorithm.Framework.csproj @@ -30,7 +30,7 @@ LICENSE - + diff --git a/Algorithm.Python/CustomModelsAlgorithm.py b/Algorithm.Python/CustomModelsAlgorithm.py index b4d424678d2a..916115f9870e 100644 --- a/Algorithm.Python/CustomModelsAlgorithm.py +++ b/Algorithm.Python/CustomModelsAlgorithm.py @@ -58,6 +58,7 @@ def OnData(self, data): # If we want to use methods from other models, you need to inherit from one of them class CustomFillModel(ImmediateFillModel): def __init__(self, algorithm): + super().__init__() self.algorithm = algorithm self.absoluteRemainingByOrderId = {} self.random = Random(387510346) @@ -85,6 +86,7 @@ def MarketFill(self, asset, order): class CustomFeeModel(FeeModel): def __init__(self, algorithm): + super().__init__() self.algorithm = algorithm def GetOrderFee(self, parameters): @@ -107,6 +109,7 @@ def GetSlippageApproximation(self, asset, order): class CustomBuyingPowerModel(BuyingPowerModel): def __init__(self, algorithm): + super().__init__() self.algorithm = algorithm def HasSufficientBuyingPowerForOrder(self, parameters): diff --git a/Algorithm.Python/CustomWarmUpPeriodIndicatorAlgorithm.py b/Algorithm.Python/CustomWarmUpPeriodIndicatorAlgorithm.py index 055392317413..96708c41b746 100644 --- a/Algorithm.Python/CustomWarmUpPeriodIndicatorAlgorithm.py +++ b/Algorithm.Python/CustomWarmUpPeriodIndicatorAlgorithm.py @@ -97,6 +97,7 @@ def OnData(self, data): # Represents the traditional simple moving average indicator (SMA) without Warm Up Period parameter defined class CSMANotWarmUp(PythonIndicator): def __init__(self, name, period): + super().__init__() self.Name = name self.Value = 0 self.queue = deque(maxlen=period) diff --git a/Algorithm.Python/QuantConnect.Algorithm.Python.csproj b/Algorithm.Python/QuantConnect.Algorithm.Python.csproj index fd57e267ffbc..a352e074fcfb 100644 --- a/Algorithm.Python/QuantConnect.Algorithm.Python.csproj +++ b/Algorithm.Python/QuantConnect.Algorithm.Python.csproj @@ -37,7 +37,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Algorithm.Python/RegisterIndicatorRegressionAlgorithm.py b/Algorithm.Python/RegisterIndicatorRegressionAlgorithm.py index e94da2517810..ab16c4438962 100644 --- a/Algorithm.Python/RegisterIndicatorRegressionAlgorithm.py +++ b/Algorithm.Python/RegisterIndicatorRegressionAlgorithm.py @@ -107,6 +107,7 @@ def OnEndOfAlgorithm(self): class CustomIndicator(PythonIndicator): def __init__(self): + super().__init__() self.Name = "Jose" self.Value = 0 diff --git a/Algorithm/QCAlgorithm.Python.cs b/Algorithm/QCAlgorithm.Python.cs index b4b5446cafe3..e57f9dcf3f99 100644 --- a/Algorithm/QCAlgorithm.Python.cs +++ b/Algorithm/QCAlgorithm.Python.cs @@ -1121,7 +1121,8 @@ public string Download(string address, PyObject headers, string userName, string // In order to convert it into a C# Dictionary if (PyDict.IsDictType(headers)) { - foreach (PyObject pyKey in headers) + using var iterator = headers.GetIterator(); + foreach (PyObject pyKey in iterator) { var key = (string)pyKey.AsManagedObject(typeof(string)); var value = (string)headers.GetItem(pyKey).AsManagedObject(typeof(string)); diff --git a/Algorithm/QuantConnect.Algorithm.csproj b/Algorithm/QuantConnect.Algorithm.csproj index 28e67fc82680..3109fd8572d1 100644 --- a/Algorithm/QuantConnect.Algorithm.csproj +++ b/Algorithm/QuantConnect.Algorithm.csproj @@ -30,7 +30,7 @@ LICENSE - + all diff --git a/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs b/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs index 327b38750da0..207ecb380b66 100644 --- a/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs +++ b/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs @@ -634,7 +634,7 @@ public void OnEndOfDay() // Only throws if there is an error in its implementation body catch (PythonException exception) { - if (!exception.Message.StartsWith("TypeError : OnEndOfDay()")) + if (!exception.Message.StartsWith("OnEndOfDay()")) { _baseAlgorithm.SetRunTimeError(exception); } @@ -662,7 +662,7 @@ public void OnEndOfDay(Symbol symbol) // Only throws if there is an error in its implementation body catch (PythonException exception) { - if (!exception.Message.StartsWith("TypeError : OnEndOfDay()")) + if (!exception.Message.StartsWith("OnEndOfDay()")) { _baseAlgorithm.SetRunTimeError(exception); } @@ -690,7 +690,8 @@ public void OnMarginCall(List requests) requests.Clear(); - foreach (PyObject pyRequest in pyRequests) + using var iterator = pyRequests.GetIterator(); + foreach (PyObject pyRequest in iterator) { SubmitOrderRequest request; if (TryConvert(pyRequest, out request)) diff --git a/AlgorithmFactory/QuantConnect.AlgorithmFactory.csproj b/AlgorithmFactory/QuantConnect.AlgorithmFactory.csproj index 002f797afcfc..4864f914b17e 100644 --- a/AlgorithmFactory/QuantConnect.AlgorithmFactory.csproj +++ b/AlgorithmFactory/QuantConnect.AlgorithmFactory.csproj @@ -29,7 +29,7 @@ LICENSE - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Common/Exceptions/InvalidTokenPythonExceptionInterpreter.cs b/Common/Exceptions/InvalidTokenPythonExceptionInterpreter.cs index af9f5d4e72e8..aa37936c4056 100644 --- a/Common/Exceptions/InvalidTokenPythonExceptionInterpreter.cs +++ b/Common/Exceptions/InvalidTokenPythonExceptionInterpreter.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -36,7 +36,6 @@ public class InvalidTokenPythonExceptionInterpreter : PythonExceptionInterpreter public override bool CanInterpret(Exception exception) { return base.CanInterpret(exception) && - exception.Message.Contains("SyntaxError") && exception.Message.Contains("invalid token"); } diff --git a/Common/Exceptions/KeyErrorPythonExceptionInterpreter.cs b/Common/Exceptions/KeyErrorPythonExceptionInterpreter.cs index 5f6b07ac2883..f2f23c22b0be 100644 --- a/Common/Exceptions/KeyErrorPythonExceptionInterpreter.cs +++ b/Common/Exceptions/KeyErrorPythonExceptionInterpreter.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -37,8 +37,17 @@ public class KeyErrorPythonExceptionInterpreter : PythonExceptionInterpreter /// True if the exception can be interpreted, false otherwise public override bool CanInterpret(Exception exception) { - return base.CanInterpret(exception) && - exception.Message.Contains("KeyError"); + var pythonException = exception as PythonException; + if (pythonException == null) + { + return false; + } + + using (Py.GIL()) + { + return base.CanInterpret(exception) && + pythonException.Type.Name.Contains("KeyError", StringComparison.InvariantCultureIgnoreCase); + } } /// /// Interprets the specified exception into a new exception diff --git a/Common/Exceptions/NoMethodMatchPythonExceptionInterpreter.cs b/Common/Exceptions/NoMethodMatchPythonExceptionInterpreter.cs index a72946008208..1ef3d16ef968 100644 --- a/Common/Exceptions/NoMethodMatchPythonExceptionInterpreter.cs +++ b/Common/Exceptions/NoMethodMatchPythonExceptionInterpreter.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -37,7 +37,6 @@ public class NoMethodMatchPythonExceptionInterpreter : PythonExceptionInterprete public override bool CanInterpret(Exception exception) { return base.CanInterpret(exception) && - exception.Message.Contains("TypeError") && exception.Message.Contains("No method match"); } diff --git a/Common/Exceptions/UnsupportedOperandPythonExceptionInterpreter.cs b/Common/Exceptions/UnsupportedOperandPythonExceptionInterpreter.cs index 78d788ce083a..ce36f2bf9313 100644 --- a/Common/Exceptions/UnsupportedOperandPythonExceptionInterpreter.cs +++ b/Common/Exceptions/UnsupportedOperandPythonExceptionInterpreter.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -37,7 +37,6 @@ public class UnsupportedOperandPythonExceptionInterpreter : PythonExceptionInter public override bool CanInterpret(Exception exception) { return base.CanInterpret(exception) && - exception.Message.Contains("TypeError") && exception.Message.Contains("unsupported operand type"); } @@ -51,7 +50,7 @@ public override Exception Interpret(Exception exception, IExceptionInterpreter i { var pe = (PythonException)exception; - var types = pe.Message.Split(':')[2].Trim(); + var types = pe.Message.Split(':')[1].Trim(); var message = $"Trying to perform a summation, subtraction, multiplication or division between {types} objects throws a TypeError exception. To prevent the exception, ensure that both values share the same type."; message += PythonUtil.PythonExceptionStackParser(pe.StackTrace); diff --git a/Common/Extensions.cs b/Common/Extensions.cs index 04433555e0f6..67285a2525ff 100644 --- a/Common/Extensions.cs +++ b/Common/Extensions.cs @@ -588,7 +588,7 @@ public static int GetPythonArgCount(this PyObject method) using (Py.GIL()) { int argCount; - var pyArgCount = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var pyArgCount = PyModule.FromString(Guid.NewGuid().ToString(), "from inspect import signature\n" + "def GetArgCount(method):\n" + " return len(signature(method).parameters)\n" @@ -2651,7 +2651,7 @@ public static bool TryConvertToDelegate(this PyObject pyObject, out T result) var name = type.FullName.Substring(0, type.FullName.IndexOf('`')); code = $"import System; delegate = {name}[{code.Substring(1)}](pyObject)"; - PythonEngine.Exec(code, null, locals.Handle); + PythonEngine.Exec(code, null, locals); result = (T)locals.GetItem("delegate").AsManagedObject(typeof(T)); locals.Dispose(); return true; @@ -2816,7 +2816,8 @@ public static IEnumerable ConvertToSymbolEnumerable(this PyObject pyObje pyObject = new PyList(new[] {pyObject}); } - foreach (PyObject item in pyObject) + using var iterator = pyObject.GetIterator(); + foreach (PyObject item in iterator) { if (PyString.IsStringType(item)) { diff --git a/Common/Python/MarginCallModelPythonWrapper.cs b/Common/Python/MarginCallModelPythonWrapper.cs index 4ba0f9b0f4e0..f350c150d9db 100644 --- a/Common/Python/MarginCallModelPythonWrapper.cs +++ b/Common/Python/MarginCallModelPythonWrapper.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -62,7 +62,7 @@ public List ExecuteMarginCall(IEnumerable gener // Since ExecuteMarginCall may return a python list // Need to convert to C# list var tickets = new List(); - var iterator = marginCalls.GetIterator(); + using var iterator = marginCalls.GetIterator(); foreach (PyObject pyObject in iterator) { OrderTicket ticket; @@ -105,7 +105,8 @@ public List GetMarginCallOrders(out bool issueMarginCallWarn // Since GetMarginCallOrders may return a python list // Need to convert to C# list var requests = new List(); - foreach (PyObject pyObject in marginCallOrders) + using var iterator = marginCallOrders.GetIterator(); + foreach (PyObject pyObject in iterator) { SubmitOrderRequest request; if (pyObject.TryConvert(out request)) @@ -120,4 +121,4 @@ public List GetMarginCallOrders(out bool issueMarginCallWarn } } } -} \ No newline at end of file +} diff --git a/Common/Python/PandasData.cs b/Common/Python/PandasData.cs index 0944d0f5a01e..ca8a8000c4b0 100644 --- a/Common/Python/PandasData.cs +++ b/Common/Python/PandasData.cs @@ -60,7 +60,7 @@ public PandasData(object data) using (Py.GIL()) { // Use our PandasMapper class that modifies pandas indexing to support tickers, symbols and SIDs - _pandas = PythonEngine.ImportModule("PandasMapper"); + _pandas = Py.Import("PandasMapper"); } } diff --git a/Common/Python/PythonSlice.cs b/Common/Python/PythonSlice.cs index 71dbe113322b..b32f258238a6 100644 --- a/Common/Python/PythonSlice.cs +++ b/Common/Python/PythonSlice.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -32,7 +32,7 @@ public class PythonSlice : Slice static PythonSlice() { // Python Data class: Converts custom data (PythonData) into a python object''' - _converter = PythonEngine.ModuleFromString("converter", + _converter = PyModule.FromString("converter", "class Data(object):\n" + " def __init__(self, data):\n" + " self.data = data\n" + @@ -161,4 +161,4 @@ public override bool TryGetValue(Symbol symbol, out dynamic data) return _slice.TryGetValue(symbol, out data); } } -} \ No newline at end of file +} diff --git a/Common/QuantConnect.csproj b/Common/QuantConnect.csproj index 65901a1c1806..3fe01393fbb5 100644 --- a/Common/QuantConnect.csproj +++ b/Common/QuantConnect.csproj @@ -35,7 +35,7 @@ - + diff --git a/Common/Util/PythonUtil.cs b/Common/Util/PythonUtil.cs index cfb36c79bf77..889fedebe6a3 100644 --- a/Common/Util/PythonUtil.cs +++ b/Common/Util/PythonUtil.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -256,7 +256,7 @@ private static bool TryGetArgLength(PyObject pyObject, out long length) /// PyObject with a python module private static PyObject GetModule() { - return PythonEngine.ModuleFromString("x", + return PyModule.FromString("x", "from clr import AddReference\n" + "AddReference(\"System\")\n" + "from System import Action, Func\n" + diff --git a/Engine/QuantConnect.Lean.Engine.csproj b/Engine/QuantConnect.Lean.Engine.csproj index f6ef65e2c445..bd0198ebabdd 100644 --- a/Engine/QuantConnect.Lean.Engine.csproj +++ b/Engine/QuantConnect.Lean.Engine.csproj @@ -43,7 +43,7 @@ - + diff --git a/Indicators/QuantConnect.Indicators.csproj b/Indicators/QuantConnect.Indicators.csproj index e3a1189776c3..f47d129682cd 100644 --- a/Indicators/QuantConnect.Indicators.csproj +++ b/Indicators/QuantConnect.Indicators.csproj @@ -32,7 +32,7 @@ - + all diff --git a/Report/QuantConnect.Report.csproj b/Report/QuantConnect.Report.csproj index adec6d4f5d67..8c3043bbd744 100644 --- a/Report/QuantConnect.Report.csproj +++ b/Report/QuantConnect.Report.csproj @@ -42,7 +42,7 @@ LICENSE - + diff --git a/Report/ReportElements/ChartReportElement.cs b/Report/ReportElements/ChartReportElement.cs index b256e5d7b7fa..51c84a35dedb 100644 --- a/Report/ReportElements/ChartReportElement.cs +++ b/Report/ReportElements/ChartReportElement.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -32,11 +32,11 @@ protected ChartReportElement() using (Py.GIL()) { - dynamic module = PythonEngine.ImportModule("ReportCharts"); + dynamic module = Py.Import("ReportCharts"); var classObj = module.ReportCharts; Charting = classObj.Invoke(); } } } -} \ No newline at end of file +} diff --git a/Research/QuantBook.cs b/Research/QuantBook.cs index 691215286b9f..c849e2f6b789 100644 --- a/Research/QuantBook.cs +++ b/Research/QuantBook.cs @@ -56,9 +56,10 @@ static QuantBook() //Determine if we are in a Python Notebook try { + PythonEngine.Initialize(); using (Py.GIL()) { - var isPython = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var isPython = PyModule.FromString(Guid.NewGuid().ToString(), "try:\n" + " import IPython\n" + " def IsPythonNotebook():\n" + diff --git a/Research/QuantConnect.Research.csproj b/Research/QuantConnect.Research.csproj index 06b9da2e904c..5ccfdd9dd260 100644 --- a/Research/QuantConnect.Research.csproj +++ b/Research/QuantConnect.Research.csproj @@ -32,7 +32,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Tests/Algorithm/AlgorithmHistoryTests.cs b/Tests/Algorithm/AlgorithmHistoryTests.cs index ad3f157980ca..017d02dad01a 100644 --- a/Tests/Algorithm/AlgorithmHistoryTests.cs +++ b/Tests/Algorithm/AlgorithmHistoryTests.cs @@ -280,7 +280,7 @@ public void GetLastKnownPricesCustomData(Language language) { using (Py.GIL()) { - var customDataType = PythonEngine.ModuleFromString("testModule", + var customDataType = PyModule.FromString("testModule", @" from AlgorithmImports import * from QuantConnect.Tests import * diff --git a/Tests/Algorithm/AlgorithmRegisterIndicatorTests.cs b/Tests/Algorithm/AlgorithmRegisterIndicatorTests.cs index 663bd8136765..6520e5ad401a 100644 --- a/Tests/Algorithm/AlgorithmRegisterIndicatorTests.cs +++ b/Tests/Algorithm/AlgorithmRegisterIndicatorTests.cs @@ -163,7 +163,7 @@ def Updat(self, input): using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), code); + var module = PyModule.FromString(Guid.NewGuid().ToString(), code); var goodIndicator = module.GetAttr("GoodCustomIndicator").Invoke(); Assert.DoesNotThrow(() => _algorithm.RegisterIndicator(_spy, goodIndicator, Resolution.Minute)); @@ -204,7 +204,7 @@ from QuantConnect.Lean.Engine.DataFeeds import * using (Py.GIL()) { - Assert.DoesNotThrow(() => PythonEngine.ModuleFromString("RegistersIndicatorProperlyPythonScript", code)); + Assert.DoesNotThrow(() => PyModule.FromString("RegistersIndicatorProperlyPythonScript", code)); } } } diff --git a/Tests/Algorithm/AlgorithmSetBrokerageTests.cs b/Tests/Algorithm/AlgorithmSetBrokerageTests.cs index 1a91bd640b34..ff4ba3094a97 100644 --- a/Tests/Algorithm/AlgorithmSetBrokerageTests.cs +++ b/Tests/Algorithm/AlgorithmSetBrokerageTests.cs @@ -80,7 +80,7 @@ public void PythonCallSetBrokerageModel() { using (Py.GIL()) { - var model = PythonEngine.ModuleFromString("testModule", + var model = PyModule.FromString("testModule", @" from AlgorithmImports import * diff --git a/Tests/Algorithm/Framework/FrameworkModelsPythonInheritanceTests.cs b/Tests/Algorithm/Framework/FrameworkModelsPythonInheritanceTests.cs index ff8240f8f434..3c28b11884ef 100644 --- a/Tests/Algorithm/Framework/FrameworkModelsPythonInheritanceTests.cs +++ b/Tests/Algorithm/Framework/FrameworkModelsPythonInheritanceTests.cs @@ -43,8 +43,7 @@ def __init__(self): using (Py.GIL()) { - dynamic pyModel = PythonEngine - .ModuleFromString(Guid.NewGuid().ToString(), code) + dynamic pyModel = PyModule.FromString(Guid.NewGuid().ToString(), code) .GetAttr("MockUniverseSelectionModel"); var model = new UniverseSelectionModelPythonWrapper(pyModel()); @@ -77,8 +76,7 @@ def SelectCoarse(self, algorithm, coarse): using (Py.GIL()) { - dynamic pyModel = PythonEngine - .ModuleFromString(Guid.NewGuid().ToString(), code) + dynamic pyModel = PyModule.FromString(Guid.NewGuid().ToString(), code) .GetAttr("MockUniverseSelectionModel"); var model = new UniverseSelectionModelPythonWrapper(pyModel()); diff --git a/Tests/Algorithm/Framework/Portfolio/BlackLittermanOptimizationPortfolioConstructionModelTests.cs b/Tests/Algorithm/Framework/Portfolio/BlackLittermanOptimizationPortfolioConstructionModelTests.cs index 04f1660cc0d2..f6448e8bf4c3 100644 --- a/Tests/Algorithm/Framework/Portfolio/BlackLittermanOptimizationPortfolioConstructionModelTests.cs +++ b/Tests/Algorithm/Framework/Portfolio/BlackLittermanOptimizationPortfolioConstructionModelTests.cs @@ -311,7 +311,7 @@ private void SetPortfolioConstruction(Language language, PortfolioBias portfolio using (Py.GIL()) { var name = nameof(BLOPCM); - var instance = PythonEngine.ModuleFromString(name, GetPythonBLOPCM()).GetAttr(name).Invoke(((int)portfolioBias).ToPython()); + var instance = PyModule.FromString(name, GetPythonBLOPCM()).GetAttr(name).Invoke(((int)portfolioBias).ToPython()); var model = new PortfolioConstructionModelPythonWrapper(instance); _algorithm.SetPortfolioConstruction(model); } diff --git a/Tests/Algorithm/Framework/Portfolio/PortfolioConstructionModelPythonWrapperTests.cs b/Tests/Algorithm/Framework/Portfolio/PortfolioConstructionModelPythonWrapperTests.cs index fd8324229559..6b0d3bcc2f77 100644 --- a/Tests/Algorithm/Framework/Portfolio/PortfolioConstructionModelPythonWrapperTests.cs +++ b/Tests/Algorithm/Framework/Portfolio/PortfolioConstructionModelPythonWrapperTests.cs @@ -36,13 +36,14 @@ public void PythonCompleteImplementation() using (Py.GIL()) { - dynamic model = PythonEngine.ModuleFromString( + dynamic model = PyModule.FromString( "TestPCM", @" from AlgorithmImports import * class PyPCM(EqualWeightingPortfolioConstructionModel): def __init__(self): + super().__init__() self.CreateTargets_WasCalled = False self.OnSecuritiesChanged_WasCalled = False self.ShouldCreateTargetForInsight_WasCalled = False @@ -102,7 +103,7 @@ public void PythonDoesNotImplementDetermineTargetPercent() using (Py.GIL()) { - dynamic model = PythonEngine.ModuleFromString( + dynamic model = PyModule.FromString( "TestPCM", @" @@ -113,6 +114,7 @@ from QuantConnect.Algorithm.Framework.Portfolio import * class PyPCM(EqualWeightingPortfolioConstructionModel): def __init__(self): + super().__init__() self.CreateTargets_WasCalled = False self.OnSecuritiesChanged_WasCalled = False self.ShouldCreateTargetForInsight_WasCalled = False diff --git a/Tests/Algorithm/Framework/Portfolio/PortfolioConstructionModelTests.cs b/Tests/Algorithm/Framework/Portfolio/PortfolioConstructionModelTests.cs index 2ae7cc71e18e..ecc5a47d6cc8 100644 --- a/Tests/Algorithm/Framework/Portfolio/PortfolioConstructionModelTests.cs +++ b/Tests/Algorithm/Framework/Portfolio/PortfolioConstructionModelTests.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -40,7 +40,7 @@ public void RebalanceFunctionPeriodDue(Language language) constructionModel = new TestPortfolioConstructionModel(); using (Py.GIL()) { - var func = PythonEngine.ModuleFromString("RebalanceFunc", + var func = PyModule.FromString("RebalanceFunc", @" from datetime import timedelta @@ -74,7 +74,7 @@ public void RebalanceFunctionSecurityChanges(Language language) constructionModel = new TestPortfolioConstructionModel(); using (Py.GIL()) { - var func = PythonEngine.ModuleFromString("RebalanceFunc", + var func = PyModule.FromString("RebalanceFunc", @" from datetime import timedelta @@ -114,7 +114,7 @@ public void RebalanceFunctionNewInsights(Language language) constructionModel = new TestPortfolioConstructionModel(); using (Py.GIL()) { - var func = PythonEngine.ModuleFromString("RebalanceFunc", + var func = PyModule.FromString("RebalanceFunc", @" from datetime import timedelta @@ -148,7 +148,7 @@ public void RebalanceFunctionInsightExpiration(Language language) constructionModel = new TestPortfolioConstructionModel(); using (Py.GIL()) { - var func = PythonEngine.ModuleFromString("RebalanceFunc", + var func = PyModule.FromString("RebalanceFunc", @" from datetime import timedelta @@ -179,7 +179,7 @@ public void NoRebalanceFunction(Language language) constructionModel = new TestPortfolioConstructionModel(); using (Py.GIL()) { - var func = PythonEngine.ModuleFromString( + var func = PyModule.FromString( "RebalanceFunc", @" from datetime import timedelta @@ -223,7 +223,7 @@ public void RebalanceFunctionNull(Language language) constructionModel = new TestPortfolioConstructionModel(); using (Py.GIL()) { - var func = PythonEngine.ModuleFromString( + var func = PyModule.FromString( "RebalanceFunc", @" from datetime import timedelta @@ -290,7 +290,7 @@ public void RebalanceFunctionDateRules(Language language) constructionModel = new TestPortfolioConstructionModel(); using (Py.GIL()) { - dynamic func = PythonEngine.ModuleFromString("RebalanceFunc", + dynamic func = PyModule.FromString("RebalanceFunc", @" import datetime @@ -327,7 +327,7 @@ public void RebalanceFunctionTimeSpan(Language language, int version) { if (version == 1) { - dynamic func = PythonEngine.ModuleFromString("RebalanceFunc", + dynamic func = PyModule.FromString("RebalanceFunc", @" from System import * @@ -337,7 +337,7 @@ def RebalanceFunc(timeSpan): } else { - dynamic func = PythonEngine.ModuleFromString("RebalanceFunc", + dynamic func = PyModule.FromString("RebalanceFunc", @" from datetime import timedelta diff --git a/Tests/Algorithm/Framework/Portfolio/ReturnsSymbolDataTests.cs b/Tests/Algorithm/Framework/Portfolio/ReturnsSymbolDataTests.cs index 87005066870d..51f03493ab88 100644 --- a/Tests/Algorithm/Framework/Portfolio/ReturnsSymbolDataTests.cs +++ b/Tests/Algorithm/Framework/Portfolio/ReturnsSymbolDataTests.cs @@ -200,8 +200,7 @@ def GetDeterminantFromHistory(history): using (Py.GIL()) { - dynamic GetDeterminantFromHistory = PythonEngine - .ModuleFromString("GetDeterminantFromHistory", code) + dynamic GetDeterminantFromHistory = PyModule.FromString("GetDeterminantFromHistory", code) .GetAttr("GetDeterminantFromHistory"); dynamic df = new PandasConverter().GetDataFrame(history); diff --git a/Tests/Common/Data/CalendarConsolidatorsTests.cs b/Tests/Common/Data/CalendarConsolidatorsTests.cs index dc4322597f8b..e15553116eab 100644 --- a/Tests/Common/Data/CalendarConsolidatorsTests.cs +++ b/Tests/Common/Data/CalendarConsolidatorsTests.cs @@ -40,7 +40,7 @@ public void SetUp() using (Py.GIL()) { - var module = PythonEngine.ModuleFromString( + var module = PyModule.FromString( "PythonCalendar", @" from AlgorithmImports import * diff --git a/Tests/Common/Data/Custom/PythonCustomDataTests.cs b/Tests/Common/Data/Custom/PythonCustomDataTests.cs index d8ab842b6a3d..dcfd8e07a663 100644 --- a/Tests/Common/Data/Custom/PythonCustomDataTests.cs +++ b/Tests/Common/Data/Custom/PythonCustomDataTests.cs @@ -28,7 +28,7 @@ public void IsSparseDataDefaultValue() dynamic instance; using (Py.GIL()) { - PyObject test = PythonEngine.ModuleFromString("testModule", + PyObject test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -49,7 +49,7 @@ public void OverridesIsSparseData(string value, bool booleanValue) dynamic instance; using (Py.GIL()) { - PyObject test = PythonEngine.ModuleFromString("testModule", + PyObject test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -68,7 +68,7 @@ public void OverridesDefaultResolution() dynamic instance; using (Py.GIL()) { - PyObject test = PythonEngine.ModuleFromString("testModule", + PyObject test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -87,7 +87,7 @@ public void OverridesSupportedResolutions() dynamic instance; using (Py.GIL()) { - PyObject test = PythonEngine.ModuleFromString("testModule", + PyObject test = PyModule.FromString("testModule", @" from AlgorithmImports import * diff --git a/Tests/Common/Data/SliceTests.cs b/Tests/Common/Data/SliceTests.cs index 7576f39deb4a..3e7171970e03 100644 --- a/Tests/Common/Data/SliceTests.cs +++ b/Tests/Common/Data/SliceTests.cs @@ -40,12 +40,12 @@ public void AccessesByDataType() var tradeBar = new TradeBar { Symbol = Symbols.SPY, Time = now }; var unlinkedData = new UnlinkedData { Symbol = Symbols.SPY, Time = now }; var quoteBar = new QuoteBar { Symbol = Symbols.SPY, Time = now }; - var tick = new Tick(now, Symbols.SPY, 1.1m, 2.1m) {TickType = TickType.Trade}; + var tick = new Tick(now, Symbols.SPY, 1.1m, 2.1m) { TickType = TickType.Trade }; var openInterest = new OpenInterest(now, Symbols.SPY, 1); var split = new Split(Symbols.SPY, now, 1, 1, SplitType.SplitOccurred); var delisting = new Delisting(Symbols.SPY, now, 1, DelistingType.Delisted); - var slice = new Slice(now, new BaseData[] {quoteBar, tradeBar, unlinkedData, tick, split, delisting, openInterest }, now); + var slice = new Slice(now, new BaseData[] { quoteBar, tradeBar, unlinkedData, tick, split, delisting, openInterest }, now); Assert.AreEqual(slice.Get(typeof(TradeBar))[Symbols.SPY], tradeBar); Assert.AreEqual(slice.Get(typeof(UnlinkedData))[Symbols.SPY], unlinkedData); @@ -345,7 +345,7 @@ public void PythonGetCustomData() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -368,7 +368,7 @@ public void PythonGetPythonCustomData() { using (Py.GIL()) { - dynamic testModule = PythonEngine.ModuleFromString("testModule", + dynamic testModule = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -420,7 +420,7 @@ public void PythonEnumerationWorks() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -442,7 +442,7 @@ public void PythonGetBySymbolCustomData() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * from QuantConnect.Tests import * @@ -465,7 +465,7 @@ public void PythonGetAndSymbolCustomData() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * from QuantConnect.Tests import * @@ -488,7 +488,7 @@ public void PythonGetTradeBar() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -513,7 +513,7 @@ public void PythonGetBySymbolOpenInterest() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * from QuantConnect.Tests import * @@ -540,7 +540,7 @@ public void PythonGetBySymbolTradeBar() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * from QuantConnect.Tests import * @@ -565,7 +565,7 @@ public void PythonGetAndSymbolTradeBar() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * from QuantConnect.Tests import * @@ -590,7 +590,7 @@ public void PythonGetCustomData_Iterate_IndexedLinkedData() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * from QuantConnect.Data.Custom.IconicTypes import * @@ -618,7 +618,7 @@ public void PythonGetCustomData_Iterate_IndexedLinkedData_Empty() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * from QuantConnect.Data.Custom.IconicTypes import * @@ -646,7 +646,7 @@ public void PythonGetCustomData_Iterate() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -693,7 +693,7 @@ public void AccessesTradeBarAndQuoteBarForSameSymbol() var tradeBars = new TradeBars { { Symbols.BTCUSD, tradeBar } }; var quoteBars = new QuoteBars { { Symbols.BTCUSD, quoteBar } }; - var slice = new Slice(DateTime.Now, new List(){ tradeBar, quoteBar }, tradeBars, quoteBars, null, null, null, null, null, null, null, DateTime.Now); + var slice = new Slice(DateTime.Now, new List() { tradeBar, quoteBar }, tradeBars, quoteBars, null, null, null, null, null, null, null, DateTime.Now); var tradeBarData = slice.Get(); Assert.AreEqual(1, tradeBarData.Count); @@ -721,14 +721,14 @@ public void PythonSlice_clear() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * def Test(slice): slice.clear()").GetAttr("Test"); - Assert.Throws(() => test(GetPythonSlice()), "Slice is read-only: cannot clear the collection"); + Assert.Throws(() => test(GetPythonSlice()), "Slice is read-only: cannot clear the collection"); } } @@ -737,14 +737,14 @@ public void PythonSlice_popitem() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * def Test(slice): slice.popitem()").GetAttr("Test"); - Assert.Throws(() => test(GetPythonSlice()), "Slice is read-only: cannot pop an item from the collection"); + Assert.Throws(() => test(GetPythonSlice()), "Slice is read-only: cannot pop an item from the collection"); } } @@ -753,14 +753,14 @@ public void PythonSlice_pop() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * def Test(slice, symbol): slice.pop(symbol)").GetAttr("Test"); - Assert.Throws(() => test(GetPythonSlice(), Symbols.SPY), $"Slice is read-only: cannot pop the value for {Symbols.SPY} from the collection"); + Assert.Throws(() => test(GetPythonSlice(), Symbols.SPY), $"Slice is read-only: cannot pop the value for {Symbols.SPY} from the collection"); } } @@ -769,14 +769,14 @@ public void PythonSlice_pop_default() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * def Test(slice, symbol, default_value): slice.pop(symbol, default_value)").GetAttr("Test"); - Assert.Throws(() => test(GetPythonSlice(), Symbols.SPY, null), $"Slice is read-only: cannot pop the value for {Symbols.SPY} from the collection"); + Assert.Throws(() => test(GetPythonSlice(), Symbols.SPY, null), $"Slice is read-only: cannot pop the value for {Symbols.SPY} from the collection"); } } @@ -785,7 +785,7 @@ public void PythonSlice_update_fails() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -793,7 +793,7 @@ def Test(slice, symbol): item = { symbol: 1 } slice.update(item)").GetAttr("Test"); - Assert.Throws(() => test(GetPythonSlice(), Symbols.SPY), "Slice is read-only: cannot update the collection"); + Assert.Throws(() => test(GetPythonSlice(), Symbols.SPY), "Slice is read-only: cannot update the collection"); } } @@ -802,7 +802,7 @@ public void PythonSlice_update_success() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -822,7 +822,7 @@ public void PythonSlice_contains() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * AddReference(""QuantConnect.Tests"") @@ -846,7 +846,7 @@ public void PythonSlice_performance() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * AddReference(""QuantConnect.Tests"") @@ -975,7 +975,7 @@ public void PythonSlice_len() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * AddReference(""QuantConnect.Tests"") @@ -999,7 +999,7 @@ public void PythonSlice_copy() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -1018,7 +1018,7 @@ public void PythonSlice_items() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -1037,7 +1037,7 @@ public void PythonSlice_keys() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -1059,7 +1059,7 @@ public void PythonSlice_values() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -1081,7 +1081,7 @@ public void PythonSlice_fromkeys() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -1100,7 +1100,7 @@ public void PythonSlice_fromkeys_default() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -1119,7 +1119,7 @@ public void PythonSlice_get_success() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -1129,7 +1129,7 @@ def Test(slice, symbol): var pythonSlice = GetPythonSlice(); dynamic expected = pythonSlice[Symbols.SPY]; PyObject result = null; - Assert.DoesNotThrow(() => result = test(GetPythonSlice(), Symbols.SPY )); + Assert.DoesNotThrow(() => result = test(GetPythonSlice(), Symbols.SPY)); BaseData actual; Assert.IsTrue(result.TryConvert(out actual)); Assert.AreEqual(expected.Symbol, actual.Symbol); @@ -1142,7 +1142,7 @@ public void PythonSlice_get_default() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -1165,7 +1165,7 @@ public void PythonSlice_get_NoneIfKeyNotFound() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -1181,7 +1181,7 @@ public void PythonSlice_setdefault_success() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -1204,7 +1204,7 @@ public void PythonSlice_setdefault_default_success() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -1230,7 +1230,7 @@ public void PythonSlice_setdefault_keynotfound() { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -1238,7 +1238,7 @@ def Test(slice, symbol): return slice.setdefault(symbol)").GetAttr("Test"); var symbol = Symbols.EURUSD; - Assert.Throws(() => test(GetPythonSlice(), symbol), + Assert.Throws(() => test(GetPythonSlice(), symbol), $"Slice is read-only: cannot set default value to for {symbol}"); } } diff --git a/Tests/Common/Exceptions/InvalidTokenPythonExceptionInterpreterTests.cs b/Tests/Common/Exceptions/InvalidTokenPythonExceptionInterpreterTests.cs index 544d20ecf820..22984c380552 100644 --- a/Tests/Common/Exceptions/InvalidTokenPythonExceptionInterpreterTests.cs +++ b/Tests/Common/Exceptions/InvalidTokenPythonExceptionInterpreterTests.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -35,7 +35,7 @@ public void Setup() try { // importing a module with syntax error 'x = 01' will throw - PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), "x = 01"); + PyModule.FromString(Guid.NewGuid().ToString(), "x = 01"); } catch (PythonException pythonException) { @@ -82,4 +82,4 @@ public void VerifyMessageContainsStackTraceInformation() private Exception CreateExceptionFromType(Type type) => type == typeof(PythonException) ? _pythonException : (Exception)Activator.CreateInstance(type); } -} \ No newline at end of file +} diff --git a/Tests/Common/Notifications/NotificationManagerTests.cs b/Tests/Common/Notifications/NotificationManagerTests.cs index 1e6025b0a2f7..90cc2e171bb8 100644 --- a/Tests/Common/Notifications/NotificationManagerTests.cs +++ b/Tests/Common/Notifications/NotificationManagerTests.cs @@ -21,7 +21,6 @@ namespace QuantConnect.Tests.Common.Notifications { - [Parallelizable(ParallelScope.Fixtures)] [TestFixture(true)] [TestFixture(false)] public class NotificationManagerTests @@ -124,7 +123,7 @@ public void PythonOverloads(string notificationType) { dynamic function; bool result; - var test = PythonEngine.ModuleFromString("testModule", + var test = PyModule.FromString("testModule", @" from AlgorithmImports import * diff --git a/Tests/Common/Orders/Fees/BackwardsCompatibilityFeeModelTests.cs b/Tests/Common/Orders/Fees/BackwardsCompatibilityFeeModelTests.cs index c518cbb7ba6b..19bfb9f2e3b1 100644 --- a/Tests/Common/Orders/Fees/BackwardsCompatibilityFeeModelTests.cs +++ b/Tests/Common/Orders/Fees/BackwardsCompatibilityFeeModelTests.cs @@ -48,7 +48,7 @@ public void OldFeeModelModel_GetOrderFee_Py() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "from AlgorithmImports import *\n" + "class CustomFeeModel:\n" + " def __init__(self):\n" + @@ -79,7 +79,7 @@ public void NewFeeModelModel_GetOrderFee_Py() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "from AlgorithmImports import *\n" + "class CustomFeeModel(FeeModel):\n" + " def __init__(self):\n" + diff --git a/Tests/Common/Orders/Fills/BackwardsCompatibilityFillModelsTests.cs b/Tests/Common/Orders/Fills/BackwardsCompatibilityFillModelsTests.cs index b344d301541e..a79d3a2a19a4 100644 --- a/Tests/Common/Orders/Fills/BackwardsCompatibilityFillModelsTests.cs +++ b/Tests/Common/Orders/Fills/BackwardsCompatibilityFillModelsTests.cs @@ -288,7 +288,7 @@ public void OldImmediateFillModelModel_MarketFill_Py() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "from AlgorithmImports import *\n" + "class CustomFillModel(ImmediateFillModel):\n" + " def __init__(self):\n" + @@ -320,7 +320,7 @@ public void OldBaseFillModel_MarketFill_Py() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "from AlgorithmImports import *\n" + "class CustomFillModel(FillModel):\n" + " def __init__(self):\n" + @@ -352,7 +352,7 @@ public void NewFillContext_Py() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "from AlgorithmImports import *\n" + "class CustomFillModel(FillModel):\n" + " def __init__(self):\n" + @@ -384,7 +384,7 @@ public void OldFillModel_NewFillContextAndMarketFill_Py() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "from AlgorithmImports import *\n" + "class CustomFillModel(FillModel):\n" + " def __init__(self):\n" + @@ -423,7 +423,7 @@ public void OldFillModel_NewFillContextAndGetPrices_Py() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "from AlgorithmImports import *\n" + "class CustomFillModel(FillModel):\n" + " def __init__(self):\n" + @@ -462,7 +462,7 @@ public void OldImmediateFillModel_MarketFill_Py() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "from AlgorithmImports import *\n" + "class CustomFillModel(ImmediateFillModel):\n" + " def __init__(self):\n" + diff --git a/Tests/Common/Securities/DynamicSecurityDataTests.cs b/Tests/Common/Securities/DynamicSecurityDataTests.cs index c6c27882cfe1..e01a72a47d00 100644 --- a/Tests/Common/Securities/DynamicSecurityDataTests.cs +++ b/Tests/Common/Securities/DynamicSecurityDataTests.cs @@ -153,7 +153,7 @@ public void Py_StoreData_GetProperty() using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -179,7 +179,7 @@ public void Py_StoreData_HasProperty() using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -203,7 +203,7 @@ public void Py_StoreData_Get_UsesTypeName() using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -227,7 +227,7 @@ public void Py_StoreData_GetAll_UsesTypeName() using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -254,7 +254,7 @@ public void Py_Get_UsesTypeName_AsKey_And_ReturnsLastItem() using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -279,7 +279,7 @@ public void Py_GetAll_TradeBar() using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -304,7 +304,7 @@ public void Py_Get_TradeBar() using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -329,7 +329,7 @@ public void Py_Get_TradeBarArray() using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -350,14 +350,14 @@ public void Py_GetTypeThatDoesNotExists_ThrowsKeyNotFoundException_WhenNotInclud using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * def Test(dynamicData): data = dynamicData.Get(TradeBar)").GetAttr("Test"); - Assert.Throws(() => test(securityData)); + Assert.Throws(() => test(securityData)); } } @@ -370,7 +370,7 @@ public void Py_AccessPropertyThatDoesNotExists_ReturnsEmptyList_WhenTypeIsInclud using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * diff --git a/Tests/Common/Util/ExtensionsTests.cs b/Tests/Common/Util/ExtensionsTests.cs index 676f87f0e342..ac3ad1cf6453 100644 --- a/Tests/Common/Util/ExtensionsTests.cs +++ b/Tests/Common/Util/ExtensionsTests.cs @@ -892,7 +892,7 @@ public void PyObjectTryConvertPythonClass() using (Py.GIL()) { // Try to convert a python class which inherits from a C# object - value = PythonEngine.ModuleFromString("testModule", + value = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -941,8 +941,8 @@ public void PyObjectTryConvertFailPython() using (Py.GIL()) { // Try to convert a python object as a IndicatorBase - var locals = new PyDict(); - PythonEngine.Exec("class A:\n pass", null, locals.Handle); + using var locals = new PyDict(); + PythonEngine.Exec("class A:\n pass", null, locals); var value = locals.GetItem("A").Invoke(); IndicatorBase indicatorBaseTradeBar; @@ -959,7 +959,7 @@ public void PyObjectTryConvertFailPythonClass() using (Py.GIL()) { // Try to convert a python class which inherits from a C# object - value = PythonEngine.ModuleFromString("testModule", + value = PyModule.FromString("testModule", @" from AlgorithmImports import * @@ -982,8 +982,8 @@ public void PyObjectTryConvertToFunc(string code) using (Py.GIL()) { - var locals = new PyDict(); - PythonEngine.Exec(code, null, locals.Handle); + using var locals = new PyDict(); + PythonEngine.Exec(code, null, locals); var pyObject = locals.GetItem("coarseSelector"); pyObject.TryConvertToDelegate(out coarseSelector); } @@ -1009,8 +1009,8 @@ public void PyObjectTryConvertToAction1() using (Py.GIL()) { - var locals = new PyDict(); - PythonEngine.Exec("def raise_number(a): raise ValueError(a)", null, locals.Handle); + using var locals = new PyDict(); + PythonEngine.Exec("def raise_number(a): raise ValueError(a)", null, locals); var pyObject = locals.GetItem("raise_number"); pyObject.TryConvertToDelegate(out action); } @@ -1022,7 +1022,7 @@ public void PyObjectTryConvertToAction1() } catch (PythonException e) { - Assert.AreEqual($"ValueError : {2}", e.Message); + Assert.AreEqual($"{2}", e.Message); } } @@ -1033,8 +1033,8 @@ public void PyObjectTryConvertToAction2() using (Py.GIL()) { - var locals = new PyDict(); - PythonEngine.Exec("def raise_number(a, b): raise ValueError(a * b)", null, locals.Handle); + using var locals = new PyDict(); + PythonEngine.Exec("def raise_number(a, b): raise ValueError(a * b)", null, locals); var pyObject = locals.GetItem("raise_number"); pyObject.TryConvertToDelegate(out action); } @@ -1046,7 +1046,7 @@ public void PyObjectTryConvertToAction2() } catch (PythonException e) { - Assert.AreEqual("ValueError : 6.0", e.Message); + Assert.AreEqual("6.0", e.Message); } } @@ -1057,8 +1057,8 @@ public void PyObjectTryConvertToNonDelegateFail() using (Py.GIL()) { - var locals = new PyDict(); - PythonEngine.Exec("def raise_number(a, b): raise ValueError(a * b)", null, locals.Handle); + using var locals = new PyDict(); + PythonEngine.Exec("def raise_number(a, b): raise ValueError(a * b)", null, locals); var pyObject = locals.GetItem("raise_number"); Assert.Throws(() => pyObject.TryConvertToDelegate(out action)); } @@ -1132,7 +1132,7 @@ public void PyObjectDictionaryConvertToDictionary_Success() { using (Py.GIL()) { - var actualDictionary = PythonEngine.ModuleFromString( + var actualDictionary = PyModule.FromString( "PyObjectDictionaryConvertToDictionary_Success", @" from datetime import datetime as dt @@ -1165,7 +1165,7 @@ public void PyObjectDictionaryConvertToDictionary_FailNotDictionary() { using (Py.GIL()) { - var pyObject = PythonEngine.ModuleFromString( + var pyObject = PyModule.FromString( "PyObjectDictionaryConvertToDictionary_FailNotDictionary", "actualDictionary = list()" ).GetAttr("actualDictionary"); @@ -1179,7 +1179,7 @@ public void PyObjectDictionaryConvertToDictionary_FailWrongItemType() { using (Py.GIL()) { - var pyObject = PythonEngine.ModuleFromString( + var pyObject = PyModule.FromString( "PyObjectDictionaryConvertToDictionary_FailWrongItemType", @" actualDictionary = dict() diff --git a/Tests/Indicators/PythonIndicatorNoinheritanceTests.cs b/Tests/Indicators/PythonIndicatorNoinheritanceTests.cs index 0a4867bdc50b..b8e7d6bcce10 100644 --- a/Tests/Indicators/PythonIndicatorNoinheritanceTests.cs +++ b/Tests/Indicators/PythonIndicatorNoinheritanceTests.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -33,7 +33,7 @@ protected override IndicatorBase CreateIndicator() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString( + var module = PyModule.FromString( Guid.NewGuid().ToString(), @" from collections import deque @@ -110,4 +110,4 @@ protected override void RunTestIndicator(IndicatorBase indicator) } } } -} \ No newline at end of file +} diff --git a/Tests/Indicators/PythonIndicatorNoinheritanceTestsLegacy.cs b/Tests/Indicators/PythonIndicatorNoinheritanceTestsLegacy.cs index cf71285cd4a3..3700827c5b68 100644 --- a/Tests/Indicators/PythonIndicatorNoinheritanceTestsLegacy.cs +++ b/Tests/Indicators/PythonIndicatorNoinheritanceTestsLegacy.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -33,7 +33,7 @@ protected override IndicatorBase CreateIndicator() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString( + var module = PyModule.FromString( Guid.NewGuid().ToString(), @" from collections import deque @@ -109,4 +109,4 @@ protected override void RunTestIndicator(IndicatorBase indicator) } } } -} \ No newline at end of file +} diff --git a/Tests/Indicators/PythonIndicatorTests.cs b/Tests/Indicators/PythonIndicatorTests.cs index 55f827095252..434951b7c4ee 100644 --- a/Tests/Indicators/PythonIndicatorTests.cs +++ b/Tests/Indicators/PythonIndicatorTests.cs @@ -35,7 +35,7 @@ protected override IndicatorBase CreateIndicator() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString( + var module = PyModule.FromString( Guid.NewGuid().ToString(), @" from AlgorithmImports import * @@ -172,7 +172,7 @@ public void RegisterPythonCustomIndicatorProperly() using (Py.GIL()) { - var module = PythonEngine.ModuleFromString( + var module = PyModule.FromString( Guid.NewGuid().ToString(), @" from AlgorithmImports import * @@ -216,7 +216,7 @@ public void AllPythonRegisterIndicatorCases() //Setup Python Indicator and Consolidator using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "from AlgorithmImports import *\n" + "consolidator = QuoteBarConsolidator(timedelta(days = 5)) \n" + "timeDelta = timedelta(days=2)\n" + @@ -257,7 +257,7 @@ public void WarmsUpProperlyPythonIndicator() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString( + var module = PyModule.FromString( Guid.NewGuid().ToString(), @" from AlgorithmImports import * @@ -300,7 +300,7 @@ public void SetDefaultWarmUpPeriodProperly() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString( + var module = PyModule.FromString( Guid.NewGuid().ToString(), @" from AlgorithmImports import * diff --git a/Tests/Python/AlgorithmPythonWrapperTests.cs b/Tests/Python/AlgorithmPythonWrapperTests.cs index 322bb8a3fa5d..a4b99d6db795 100644 --- a/Tests/Python/AlgorithmPythonWrapperTests.cs +++ b/Tests/Python/AlgorithmPythonWrapperTests.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -155,9 +155,9 @@ private AlgorithmPythonWrapper GetAlgorithm(string code) using (Py.GIL()) { - PythonEngine.ModuleFromString("Test_AlgorithmPythonWrapper", code); + PyModule.FromString("Test_AlgorithmPythonWrapper", code); return new AlgorithmPythonWrapper("Test_AlgorithmPythonWrapper"); } } } -} \ No newline at end of file +} diff --git a/Tests/Python/DataConsolidatorPythonWrapperTests.cs b/Tests/Python/DataConsolidatorPythonWrapperTests.cs index 3f474e7769da..b6f92284fdd8 100644 --- a/Tests/Python/DataConsolidatorPythonWrapperTests.cs +++ b/Tests/Python/DataConsolidatorPythonWrapperTests.cs @@ -30,7 +30,7 @@ public void UpdatePyConsolidator() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "from AlgorithmImports import *\n" + "class CustomConsolidator():\n" + " def __init__(self):\n" + @@ -72,7 +72,7 @@ public void ScanPyConsolidator() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "from AlgorithmImports import *\n" + "class CustomConsolidator():\n" + " def __init__(self):\n" + @@ -103,7 +103,7 @@ public void InputTypePyConsolidator() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "from AlgorithmImports import *\n" + "class CustomConsolidator():\n" + " def __init__(self):\n" + @@ -128,7 +128,7 @@ public void OutputTypePyConsolidator() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "from AlgorithmImports import *\n" + "class CustomConsolidator():\n" + " def __init__(self):\n" + @@ -189,7 +189,7 @@ public void AttachAndTriggerEvent() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "from AlgorithmImports import *\n" + "class ImplementingClass():\n" + " def __init__(self):\n" + @@ -200,6 +200,7 @@ public void AttachAndTriggerEvent() " self.EventCalled = True\n" + "class CustomConsolidator(QuoteBarConsolidator):\n" + " def __init__(self,span):\n" + + " super().__init__(span)\n" + " self.Span = span"); var implementingClass = module.GetAttr("ImplementingClass").Invoke(); diff --git a/Tests/Python/MethodOverloadTests.cs b/Tests/Python/MethodOverloadTests.cs index 115e101cda4c..a1fb4b1e34be 100644 --- a/Tests/Python/MethodOverloadTests.cs +++ b/Tests/Python/MethodOverloadTests.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -64,11 +64,11 @@ public void CallPlotTests() Assert.Throws(() => _algorithm.call_plot_throw_test()); // self.Plot("ERROR", self.Portfolio), where self.Portfolio is IAlgorithm.Portfolio: instance of SecurityPortfolioManager - Assert.Throws(() => _algorithm.call_plot_throw_managed_test()); + Assert.Throws(() => _algorithm.call_plot_throw_managed_test()); // self.Plot("ERROR", self.a), where self.a is an instance of a python object - Assert.Throws(() => _algorithm.call_plot_throw_pyobject_test()); + Assert.Throws(() => _algorithm.call_plot_throw_pyobject_test()); } } } -} \ No newline at end of file +} diff --git a/Tests/Python/NamedArgumentsTests.cs b/Tests/Python/NamedArgumentsTests.cs index 286924a982db..e09ec88c18b0 100644 --- a/Tests/Python/NamedArgumentsTests.cs +++ b/Tests/Python/NamedArgumentsTests.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -34,7 +34,7 @@ public void AddEquityTest() using (Py.GIL()) { // Test function that will used named args in Python -> C# - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "def test(algorithm):\n" + " aapl = algorithm.AddEquity(ticker='AAPL')\n" + " return aapl\n" diff --git a/Tests/Python/PandasConverterTests.BackwardsCompatibility.cs b/Tests/Python/PandasConverterTests.BackwardsCompatibility.cs index 067d23396ead..ded11f2899ba 100644 --- a/Tests/Python/PandasConverterTests.BackwardsCompatibility.cs +++ b/Tests/Python/PandasConverterTests.BackwardsCompatibility.cs @@ -35,7 +35,7 @@ public void BackwardsCompatibilityDataFrameDataFrameNonExceptionFunctions(string using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(df, symbol): df = df.lastprice.unstack(level=0){method}").GetAttr("Test"); @@ -51,7 +51,7 @@ public void BackwardsCompatibilityDataFrameParameterlessFunctions(string method, using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(df, symbol): df = df.lastprice.unstack(level=0){method} @@ -78,7 +78,7 @@ public void BackwardsCompatibilityDataFrameOtherParameterFunctions(string method using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(df, other, symbol): df = df{method} @@ -100,7 +100,7 @@ public void BackwardsCompatibilitySeriesNonExceptionFunctions(string method, str using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(df, symbol): series = df.lastprice @@ -117,7 +117,7 @@ public void BackwardsCompatibilitySeriesParameterlessFunctions(string method, st using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(df, symbol): series = df.lastprice @@ -145,7 +145,7 @@ public void BackwardsCompatibilitySeriesOtherParameterFunctions(string method, s using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(df, other, symbol): series, other = other.lastprice, df.lastprice @@ -304,7 +304,7 @@ private static Dictionary GetParameterlessFunctions() using (Py.GIL()) { - var module = PythonEngine.ModuleFromString("Test", + var module = PyModule.FromString("Test", @"import pandas from inspect import getmembers, isfunction, signature diff --git a/Tests/Python/PandasConverterTests.cs b/Tests/Python/PandasConverterTests.cs index 872d283a829a..c6d41ad1c221 100644 --- a/Tests/Python/PandasConverterTests.cs +++ b/Tests/Python/PandasConverterTests.cs @@ -41,13 +41,15 @@ public partial class PandasConverterTests private PandasConverter _converter = new PandasConverter(); private bool _newerPandas; - private static bool IsNewerPandas(){ + private static bool IsNewerPandas() + { bool newPandas; - using(Py.GIL()){ + using (Py.GIL()) + { var pandas = Py.Import("pandas"); - var local = new PyDict(); + using var local = new PyDict(); local["pd"] = pandas; - newPandas = PythonEngine.Eval("int(pd.__version__.split('.')[0]) >= 1", locals:local.Handle).As(); + newPandas = PythonEngine.Eval("int(pd.__version__.split('.')[0]) >= 1", locals: local).As(); } return newPandas; } @@ -69,7 +71,7 @@ public void TearDown() public void HandlesEnumerableDataType() { var converter = new PandasConverter(); - var data = new [] + var data = new[] { new EnumerableData { @@ -240,7 +242,8 @@ public void HandlesNullableValues() var converter = new PandasConverter(); var rawBars = Enumerable .Range(0, 10) - .Select(i => new NullableValueData { + .Select(i => new NullableValueData + { Symbol = Symbols.AAPL, EndTime = new DateTime(2020, 1, 1), NullableInt = null, @@ -298,7 +301,7 @@ public void HandlesOddTickers() using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame): data = dataFrame.loc['LOW'] @@ -336,7 +339,7 @@ public void BreakingOddTickers() using (Py.GIL()) { - var Test = PythonEngine.ModuleFromString("testModule", + var Test = PyModule.FromString("testModule", $@" def Test1(dataFrame): # Should not throw, access all LOW ticker data @@ -373,7 +376,7 @@ public void BackwardsCompatibilityDataFrame_Subset(bool cache) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.droplevel('symbol') @@ -392,7 +395,7 @@ public void BackwardsCompatibilityDataFrame_add_prefix(string index, bool cache using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.add_prefix('price_')['price_lastprice'].unstack(level=0) @@ -413,7 +416,7 @@ public void BackwardsCompatibilityDataFrame_add_suffix(string index, bool cache using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.add_suffix('_tick')['lastprice_tick'].unstack(level=0) @@ -434,7 +437,7 @@ public void BackwardsCompatibilityDataFrame_align(string index, bool cache = fal using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, other, symbol): data = dataFrame.lastprice.unstack(level=0) @@ -457,7 +460,7 @@ public void BackwardsCompatibilityDataFrame_apply(string index, bool cache = fal using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import numpy as np def Test(dataFrame, symbol): @@ -479,7 +482,7 @@ public void BackwardsCompatibilityDataFrame_applymap(string index, bool cache = using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.unstack(level=0).applymap(lambda x: x*2) @@ -500,7 +503,7 @@ public void BackwardsCompatibilityDataFrame_asfreq(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.unstack(level=0).asfreq(freq='30S') @@ -521,7 +524,7 @@ public void BackwardsCompatibilityDataFrame_asof(string index, bool cache = fals using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import pandas as pd def Test(dataFrame, symbol): @@ -544,7 +547,7 @@ public void BackwardsCompatibilityDataFrame_assign(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.assign(tmp=lambda x: x.lastprice * 1.1)['tmp'].unstack(level=0) @@ -565,7 +568,7 @@ public void BackwardsCompatibilityDataFrame_astype(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.unstack(level=0).astype('float16') @@ -593,7 +596,7 @@ public void BackwardsCompatibilityDataFrame_at(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.at[({index},), 'lastprice']").GetAttr("Test"); @@ -611,7 +614,7 @@ public void BackwardsCompatibilityDataFrame_at_time(string index, bool cache = f using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.unstack(level=0).at_time('04:00') @@ -632,7 +635,7 @@ public void BackwardsCompatibilityDataFrame_axes(string index, bool cache = fals using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): axes = dataFrame.axes[0] @@ -652,7 +655,7 @@ public void BackwardsCompatibilityDataFrame_between_time(string index, bool cach using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.unstack(level=0).between_time('02:00', '06:00') @@ -673,7 +676,7 @@ public void BackwardsCompatibilityDataFrame_columns(string index, bool cache = f using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): columns = dataFrame.lastprice.unstack(level=0).columns @@ -693,7 +696,7 @@ public void BackwardsCompatibilityDataFrame_combine(string index, bool cache = f using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import numpy as np def Test(dataFrame, other, symbol): @@ -717,7 +720,7 @@ public void BackwardsCompatibilityDataFrame_concat(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import pandas as pd def Test(dataFrame, dataFrame2, symbol): @@ -739,7 +742,7 @@ public void BackwardsCompatibilityDataFrame_corrwith(string index, bool cache = using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import numpy as np def Test(dataFrame, other, symbol): @@ -762,7 +765,7 @@ public void BackwardsCompatibilityDataFrame_drop(string index, bool cache = fals using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.drop(columns=['exchange']).lastprice.unstack(level=0) @@ -783,7 +786,7 @@ public void BackwardsCompatibilityDataFrame_droplevel(string index, bool cache = using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.droplevel('time').lastprice @@ -804,7 +807,7 @@ public void BackwardsCompatibilityDataFrame_dtypes(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import numpy as np def Test(dataFrame, symbol): @@ -826,7 +829,7 @@ public void BackwardsCompatibilityDataFrame_eval(string index, bool cache = fals using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.eval('tmp=lastprice * 1.1')['tmp'].unstack(level=0) @@ -847,7 +850,7 @@ public void BackwardsCompatibilityDataFrame_equals(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, other, symbol): if not dataFrame.equals(other): @@ -866,7 +869,7 @@ public void BackwardsCompatibilityDataFrame_ewm(string index, bool cache = false using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.unstack(level=0) @@ -888,7 +891,7 @@ public void BackwardsCompatibilityDataFrame_expanding(string index, bool cache = using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.unstack(level=0) @@ -910,7 +913,7 @@ public void BackwardsCompatibilityDataFrame_explode(string index, bool cache = f using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.explode('lastprice').lastprice.unstack(level=0) @@ -931,7 +934,7 @@ public void BackwardsCompatibilityDataFrame_filter(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.filter(items=['lastprice']).lastprice.unstack(level=0) @@ -949,7 +952,8 @@ def Test(dataFrame, symbol): public void BackwardsCompatibilityDataFrame_ftypes(string index, bool cache = false) { // ftypes deprecated in newer pandas; use dtypes instead - if(_newerPandas){ + if (_newerPandas) + { return; } @@ -957,7 +961,7 @@ public void BackwardsCompatibilityDataFrame_ftypes(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.unstack(level=0).ftypes @@ -978,7 +982,7 @@ public void BackwardsCompatibilityDataFrame_get_OnProperty(string index, bool ca using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.get({index}) @@ -998,7 +1002,7 @@ public void BackwardsCompatibilityDataFrame_getitem(string index, bool cache = f using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame['lastprice'][{index}]").GetAttr("Test"); @@ -1013,7 +1017,8 @@ def Test(dataFrame, symbol): public void BackwardsCompatibilityDataFrame_get_value_index(string index, bool cache = false) { // get_value deprecated in new Pandas; Syntax also deprecated; Use .at[] or .iat[] instead - if(_newerPandas){ + if (_newerPandas) + { return; } @@ -1021,7 +1026,7 @@ public void BackwardsCompatibilityDataFrame_get_value_index(string index, bool c using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.get_value(({index},), 'lastprice') @@ -1038,15 +1043,16 @@ def Test(dataFrame, symbol): public void BackwardsCompatibilityDataFrame_get_value_column(string index, bool cache = false) { // get_value deprecated in new Pandas; use at[] or iat[] instead - if(_newerPandas){ + if (_newerPandas) + { return; } - + if (cache) SymbolCache.Set("SPY", Symbols.SPY); using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.unstack(level=0) @@ -1068,7 +1074,7 @@ public void BackwardsCompatibilityDataFrame_groupby(string index, bool cache = f using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import pandas as pd def Test(df, other, symbol): @@ -1091,7 +1097,7 @@ public void BackwardsCompatibilityDataFrame_iloc(string index, bool cache = fals using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame['lastprice'].unstack(level=0).iloc[-1][{index}] @@ -1111,7 +1117,7 @@ public void BackwardsCompatibilityDataFrame_insert(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.unstack(level=0) @@ -1132,7 +1138,7 @@ public void BackwardsCompatibilityDataFrame_index_levels_contains(string index, using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): if {index} not in dataFrame.index.levels[0]: @@ -1154,7 +1160,7 @@ public void BackwardsCompatibilityDataFrame_items(string method, string index, b using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): for index, data in dataFrame.{method}(): @@ -1176,7 +1182,7 @@ public void BackwardsCompatibilityDataFrame_iterrows(string index, bool cache = using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): for index, data in dataFrame.lastprice.unstack(level=0).iterrows(): @@ -1195,7 +1201,8 @@ def Test(dataFrame, symbol): public void BackwardsCompatibilityDataFrame_ix(string index, bool cache = false) { // ix deprecated in newer pandas; use loc and iloc instead - if(_newerPandas){ + if (_newerPandas) + { return; } @@ -1203,7 +1210,7 @@ public void BackwardsCompatibilityDataFrame_ix(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame['lastprice'].unstack(level=0).ix[-1][{index}] @@ -1223,7 +1230,7 @@ public void BackwardsCompatibilityDataFrame_join(string index, bool cache = fals using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, dataFrame2, symbol): newDataFrame = dataFrame.join(dataFrame2, lsuffix='_') @@ -1244,7 +1251,7 @@ public void BackwardsCompatibilityDataFrame_loc(string index, bool cache = false using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.loc[{index}]").GetAttr("Test"); @@ -1266,13 +1273,13 @@ public void BackwardsCompatibilityDataFrame_loc_list(string index, bool cache = using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbols): data = dataFrame.lastprice.unstack(0) data = data.loc[:, {index}]").GetAttr("Test"); - var symbols = new List {Symbols.SPY, Symbols.AAPL}; + var symbols = new List { Symbols.SPY, Symbols.AAPL }; Assert.DoesNotThrow(() => test(GetTestDataFrame(symbols), symbols)); } } @@ -1286,7 +1293,7 @@ public void BackwardsCompatibilityDataFrame_loc_after_xs(string index, bool cach using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): time = dataFrame.index.get_level_values('time')[0] @@ -1306,7 +1313,7 @@ public void BackwardsCompatibilityDataFrame_loc_OnProperty(string index, bool ca using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.loc[{index}]").GetAttr("Test"); @@ -1324,7 +1331,7 @@ public void BackwardsCompatibilityDataFrame_loc_SubDataFrame(string index, bool using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.loc[{index}].loc['2013-10-07 04:00:00']").GetAttr("Test"); @@ -1342,7 +1349,7 @@ public void BackwardsCompatibilityDataFrame_mask(string index, bool cache = fals using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.unstack(0) @@ -1364,7 +1371,7 @@ public void BackwardsCompatibilityDataFrame_pivot_table(string index, bool cache using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import pandas as pd def Test(dataFrame, symbol): @@ -1388,7 +1395,7 @@ public void BackwardsCompatibilityDataFrame_merge(string index, bool cache = fal using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, dataFrame2, symbol): newDataFrame = dataFrame.merge(dataFrame2, on='symbol', how='outer') @@ -1412,7 +1419,7 @@ public void BackwardsCompatibilityDataFrame_nlarg_smallest(string method, string using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.{method}(3, 'lastprice') @@ -1433,7 +1440,7 @@ public void BackwardsCompatibilityDataFrame_pipe(string index, bool cache = fals using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import pandas as pd def Test(dataFrame, other, symbol): @@ -1459,7 +1466,7 @@ public void BackwardsCompatibilityDataFrame_pop(string index, bool cache = false using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.pop('lastprice') @@ -1480,7 +1487,7 @@ public void BackwardsCompatibilityDataFrame_query(string index, bool cache = fal using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.query('lastprice > 100').lastprice.unstack(0) @@ -1501,7 +1508,7 @@ public void BackwardsCompatibilityDataFrame_query_index(string index, bool cache using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): time = '2011-02-01' @@ -1523,7 +1530,7 @@ public void BackwardsCompatibilityDataFrame_reindex_like(string index, bool cach using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, other, symbol): data = other.reindex_like(dataFrame) @@ -1547,7 +1554,7 @@ public void BackwardsCompatibilityDataFrame_rename(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.rename({rename})['price'].unstack(0) @@ -1568,7 +1575,7 @@ public void BackwardsCompatibilityDataFrame_reorder_levels(string index, bool ca using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.reorder_levels(['time', 'symbol']).lastprice.unstack(1) @@ -1589,7 +1596,7 @@ public void BackwardsCompatibilityDataFrame_resample(string index, bool cache = using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.unstack(0) @@ -1611,7 +1618,7 @@ public void BackwardsCompatibilityDataFrame_reset_index(string index, bool cache using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.unstack(0).reset_index('time') @@ -1632,7 +1639,7 @@ public void BackwardsCompatibilityDataFrame_rolling(string index, bool cache = f using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.rolling(2).sum() @@ -1654,7 +1661,7 @@ public void BackwardsCompatibilityDataFrame_select_dtypes(string index, bool cac using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.select_dtypes(exclude=['int']).lastprice.unstack(0) @@ -1674,13 +1681,14 @@ public void BackwardsCompatibilityDataFrame_set_value(string index, bool cache = if (cache) SymbolCache.Set("SPY", Symbols.SPY); // set_value deprecated in newer pandas; use .at[] or .iat instead - if(_newerPandas) { + if (_newerPandas) + { return; } using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): idx = dataFrame.first_valid_index() @@ -1702,7 +1710,7 @@ public void BackwardsCompatibilityDataFrame_slice_shift(string index, bool cache using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.slice_shift().lastprice.unstack(0) @@ -1723,7 +1731,7 @@ public void BackwardsCompatibilityDataFrame_sort_values(string index, bool cache using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.sort_values(by=['lastprice']).lastprice.unstack(0) @@ -1744,7 +1752,7 @@ public void BackwardsCompatibilityDataFrame_swapaxes(string index, bool cache = using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.unstack(0).swapaxes('index', 'columns') @@ -1765,7 +1773,7 @@ public void BackwardsCompatibilityDataFrame_swaplevel(string index, bool cache = using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.swaplevel().lastprice.unstack(1) @@ -1786,7 +1794,7 @@ public void BackwardsCompatibilityDataFrame_take(string index, bool cache = fals using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.take([0, 3]).lastprice.unstack(0) @@ -1807,7 +1815,7 @@ public void BackwardsCompatibilityDataFrame_transform(string index, bool cache = using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import numpy as np def Test(dataFrame, symbol): @@ -1829,7 +1837,7 @@ public void BackwardsCompatibilityDataFrame_T(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.T.iloc[0] @@ -1850,7 +1858,7 @@ public void BackwardsCompatibilityDataFrame_transpose(string index, bool cache = using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.transpose().iloc[0] @@ -1871,7 +1879,7 @@ public void BackwardsCompatibilityDataFrame_tshift(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" from datetime import timedelta as d def Test(dataFrame, symbol): @@ -1891,7 +1899,7 @@ public void BackwardsCompatibilityDataFrame_tz_localize_convert(string index, bo using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.tz_localize('UTC', level=1) @@ -1913,7 +1921,7 @@ public void BackwardsCompatibilityDataFrame_unstack_lastprice(string index, bool using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.unstack(level=0).lastprice[{index}]").GetAttr("Test"); @@ -1931,7 +1939,7 @@ public void BackwardsCompatibilityDataFrame_unstack_loc_loc(string index, bool c using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): df2 = dataFrame.unstack(level=0) @@ -1951,7 +1959,7 @@ public void BackwardsCompatibilityDataFrame_unstack_get(string index, bool cache using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): df2 = dataFrame.lastprice.unstack(level=0) @@ -1974,7 +1982,7 @@ public void BackwardsCompatibilityDataFrame_update(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import pandas as pd def Test(dataFrame, symbol): @@ -1997,7 +2005,7 @@ public void BackwardsCompatibilityDataFrame_where(string index, bool cache = fal using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): dataFrame = dataFrame.lastprice.unstack(0) @@ -2019,7 +2027,7 @@ public void BackwardsCompatibilityDataFrame_xs(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.xs({index})").GetAttr("Test"); @@ -2036,7 +2044,7 @@ public void BackwardsCompatibilitySeries__str__(string symbol, bool cache = fals if (cache) SymbolCache.Set("SPY", Symbols.SPY); using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import pandas as pd from datetime import datetime as dt @@ -2061,7 +2069,7 @@ public void BackwardsCompatibilitySeries__repr__(string symbol, bool cache = fal if (cache) SymbolCache.Set("SPY", Symbols.SPY); using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import pandas as pd from datetime import datetime as dt @@ -2087,7 +2095,7 @@ public void BackwardsCompatibilitySeries_align(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, other, symbol): data = dataFrame.lastprice @@ -2110,7 +2118,7 @@ public void BackwardsCompatibilitySeries_apply(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import numpy as np def Test(dataFrame, symbol): @@ -2132,7 +2140,7 @@ public void BackwardsCompatibilitySeries_asfreq(string index, bool cache = false using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice.droplevel(0) @@ -2151,7 +2159,7 @@ public void BackwardsCompatibilitySeries_asof(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import pandas as pd def Test(dataFrame, symbol): @@ -2172,7 +2180,7 @@ public void BackwardsCompatibilitySeries_astype(string index, bool cache = false using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.astype('float16') @@ -2193,7 +2201,7 @@ public void BackwardsCompatibilitySeries_at_time(string index, bool cache = fals using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice.droplevel(0) @@ -2212,7 +2220,7 @@ public void BackwardsCompatibilitySeries_between(string index, bool cache = fals using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2231,7 +2239,7 @@ public void BackwardsCompatibilitySeries_between_time(string index, bool cache = using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice.droplevel(0) @@ -2250,7 +2258,7 @@ public void BackwardsCompatibilitySeries_combine(string index, bool cache = fals using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import numpy as np def Test(dataFrame, other, symbol): @@ -2274,7 +2282,7 @@ public void BackwardsCompatibilitySeries_drop(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2297,7 +2305,7 @@ public void BackwardsCompatibilitySeries_droplevel(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2319,7 +2327,7 @@ public void BackwardsCompatibilitySeries_equals(string index, bool cache = false using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, other, symbol): series = dataFrame.lastprice @@ -2340,7 +2348,7 @@ public void BackwardsCompatibilitySeries_ewm(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2362,7 +2370,7 @@ public void BackwardsCompatibilitySeries_expanding(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2384,7 +2392,7 @@ public void BackwardsCompatibilitySeries_filter(string index, bool cache = false using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2406,7 +2414,7 @@ public void BackwardsCompatibilitySeries_first(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice.droplevel(0) @@ -2425,7 +2433,7 @@ public void BackwardsCompatibilitySeries_get(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2443,7 +2451,8 @@ def Test(dataFrame, symbol): public void BackwardsCompatibilitySeries_get_value(string index, bool cache = false) { // get_value deprecated in new Pandas; Use .at[] or .iat[] instead - if (_newerPandas){ + if (_newerPandas) + { return; } @@ -2451,7 +2460,7 @@ public void BackwardsCompatibilitySeries_get_value(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2473,7 +2482,7 @@ public void BackwardsCompatibilitySeries_groupby(string index, bool cache = fals using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import pandas as pd def Test(df, other, symbol): @@ -2496,7 +2505,7 @@ public void BackwardsCompatibilitySeries_last(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice.droplevel(0) @@ -2517,7 +2526,7 @@ public void BackwardsCompatibilitySeries_map(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2539,7 +2548,7 @@ public void BackwardsCompatibilitySeries_mask(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2561,7 +2570,7 @@ public void BackwardsCompatibilitySeries_pipe(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import pandas as pd def Test(dataFrame, other, symbol): @@ -2587,7 +2596,7 @@ public void BackwardsCompatibilitySeries_pop(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): data = dataFrame.lastprice.pop({index}) @@ -2607,7 +2616,7 @@ public void BackwardsCompatibilitySeries_reindex_like(string index, bool cache = using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, other, symbol): series = dataFrame.lastprice @@ -2630,7 +2639,7 @@ public void BackwardsCompatibilitySeries_rename(string index, bool cache = false using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2652,7 +2661,7 @@ public void BackwardsCompatibilitySeries_repeat(string index, bool cache = false using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2674,7 +2683,7 @@ public void BackwardsCompatibilitySeries_reorder_levels(string index, bool cache using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2696,7 +2705,7 @@ public void BackwardsCompatibilitySeries_resample(string index, bool cache = fal using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2715,7 +2724,7 @@ public void BackwardsCompatibilitySeries_reset_index(string index, bool cache = using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2737,7 +2746,7 @@ public void BackwardsCompatibilitySeries_rolling(string index, bool cache = fals using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2766,7 +2775,7 @@ public void BackwardsCompatibilitySeries_set_value(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2789,7 +2798,7 @@ public void BackwardsCompatibilitySeries_slice_shift(string index, bool cache = using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2811,7 +2820,7 @@ public void BackwardsCompatibilitySeries_swapaxes(string index, bool cache = fal using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2833,7 +2842,7 @@ public void BackwardsCompatibilitySeries_swaplevel(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2855,7 +2864,7 @@ public void BackwardsCompatibilitySeries_take(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2877,7 +2886,7 @@ public void BackwardsCompatibilitySeries_transform(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import numpy as np def Test(dataFrame, symbol): @@ -2901,7 +2910,7 @@ public void BackwardsCompatibilitySeries_T(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2923,7 +2932,7 @@ public void BackwardsCompatibilitySeries_transpose(string index, bool cache = fa using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2945,7 +2954,7 @@ public void BackwardsCompatibilitySeries_tshift(string index, bool cache = false using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" from datetime import timedelta as d def Test(dataFrame, symbol): @@ -2965,7 +2974,7 @@ public void BackwardsCompatibilitySeries_tz_localize_convert(string index, bool using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -2988,7 +2997,7 @@ public void BackwardsCompatibilitySeries_update(string index, bool cache = false using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" import pandas as pd def Test(dataFrame, symbol): @@ -3012,7 +3021,7 @@ public void BackwardsCompatibilitySeries_where(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -3034,7 +3043,7 @@ public void BackwardsCompatibilitySeries_xs(string index, bool cache = false) using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", $@" def Test(dataFrame, symbol): series = dataFrame.lastprice @@ -3050,7 +3059,7 @@ public void NotBackwardsCompatibilityDataFrame_index_levels_contains_ticker_notI { using (Py.GIL()) { - dynamic test = PythonEngine.ModuleFromString("testModule", + dynamic test = PyModule.FromString("testModule", @" def Test(dataFrame, symbol): if 'SPY' not in dataFrame.index.levels[0]: @@ -3183,10 +3192,10 @@ public void HandlesQuoteTicks() private static Resolution[] ResolutionCases = { Resolution.Tick, Resolution.Minute, Resolution.Second }; - private static Symbol[] SymbolCases = {Symbols.Fut_SPY_Feb19_2016, Symbols.Fut_SPY_Mar19_2016, Symbols.SPY_C_192_Feb19_2016, Symbols.SPY_P_192_Feb19_2016}; + private static Symbol[] SymbolCases = { Symbols.Fut_SPY_Feb19_2016, Symbols.Fut_SPY_Mar19_2016, Symbols.SPY_C_192_Feb19_2016, Symbols.SPY_P_192_Feb19_2016 }; [Test] - public void HandlesOpenInterestTicks([ValueSource(nameof(ResolutionCases))]Resolution resolution, [ValueSource(nameof(SymbolCases))] Symbol symbol) + public void HandlesOpenInterestTicks([ValueSource(nameof(ResolutionCases))] Resolution resolution, [ValueSource(nameof(SymbolCases))] Symbol symbol) { // Arrange var converter = new PandasConverter(); @@ -3541,7 +3550,7 @@ private Security GetSecurity(SubscriptionDataConfig subscriptionDataConfig) private dynamic GetTestDataFrame(Symbol symbol, int count = 1) { - return GetTestDataFrame(new[] {symbol}, count); + return GetTestDataFrame(new[] { symbol }, count); } private dynamic GetTestDataFrame(IEnumerable symbols, int count = 1) @@ -3579,7 +3588,7 @@ public SubTradeBar() { } public SubTradeBar(TradeBar tradeBar) : base(tradeBar) { } public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode) => - new SubTradeBar((TradeBar) base.Reader(config, line, date, isLiveMode)); + new SubTradeBar((TradeBar)base.Reader(config, line, date, isLiveMode)); } internal class SubSubTradeBar : SubTradeBar @@ -3591,7 +3600,7 @@ public SubSubTradeBar() { } public SubSubTradeBar(TradeBar tradeBar) : base(tradeBar) { } public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode) => - new SubSubTradeBar((TradeBar) base.Reader(config, line, date, isLiveMode)); + new SubSubTradeBar((TradeBar)base.Reader(config, line, date, isLiveMode)); } internal class NullableValueData : BaseData @@ -3603,7 +3612,7 @@ internal class NullableValueData : BaseData public double? NullableColumn { get; set; } } - internal class CustomData: DynamicData + internal class CustomData : DynamicData { private bool _isInitialized; private readonly List _propertyNames = new List(); @@ -3637,7 +3646,7 @@ public override BaseData Reader(SubscriptionDataConfig config, string line, Date var value = csv[i].ToDecimal(); data.SetProperty(_propertyNames[i], value); } - + if (!_propertyNames.Contains("Value")) { data.Value = 1; diff --git a/Tests/Python/PortfolioCustomModelTests.cs b/Tests/Python/PortfolioCustomModelTests.cs index 6b9421ad6456..116976190bf2 100644 --- a/Tests/Python/PortfolioCustomModelTests.cs +++ b/Tests/Python/PortfolioCustomModelTests.cs @@ -84,7 +84,7 @@ private PyObject CreateCustomMarginCallModel(string code, SecurityPortfolioManag { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString("CustomMarginCallModel", code); + var module = PyModule.FromString("CustomMarginCallModel", code); dynamic CustomMarginCallModel = module.GetAttr("CustomMarginCallModel"); return CustomMarginCallModel(portfolio, null); } @@ -127,6 +127,7 @@ from AlgorithmImports import * class CustomMarginCallModel(DefaultMarginCallModel): def __init__(self, portfolio, defaultOrderProperties): + super().__init__(portfolio, defaultOrderProperties) self.porfolio = portfolio self.defaultOrderProperties = defaultOrderProperties diff --git a/Tests/Python/PythonOptionTests.cs b/Tests/Python/PythonOptionTests.cs index 9807edd2f545..b6bac18395df 100644 --- a/Tests/Python/PythonOptionTests.cs +++ b/Tests/Python/PythonOptionTests.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -35,7 +35,7 @@ public void PythonFilterFunctionReturnsList() using (Py.GIL()) { //Filter function that returns a list of symbols - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "def filter(universe):\n" + " universe = universe.WeeklysOnly().Expiration(0, 10)\n" + " return [symbol for symbol in universe\n"+ @@ -60,7 +60,7 @@ public void PythonFilterFunctionReturnsUniverse() using (Py.GIL()) { //Filter function that returns a OptionFilterUniverse - var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), + var module = PyModule.FromString(Guid.NewGuid().ToString(), "def filter(universe):\n" + " universe = universe.WeeklysOnly().Expiration(0, 5)\n" + " return universe" diff --git a/Tests/Python/PythonPackagesTests.cs b/Tests/Python/PythonPackagesTests.cs index 7f54c0b3f88f..68b91da8e3b4 100644 --- a/Tests/Python/PythonPackagesTests.cs +++ b/Tests/Python/PythonPackagesTests.cs @@ -1,4 +1,4 @@ -/* +/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * @@ -898,7 +898,7 @@ private static void AssetCode(string code) { using (Py.GIL()) { - dynamic module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), code); + dynamic module = PyModule.FromString(Guid.NewGuid().ToString(), code); Assert.DoesNotThrow(() => module.RunTest()); } } diff --git a/Tests/Python/PythonWrapperTests.cs b/Tests/Python/PythonWrapperTests.cs index f440f41d5042..55288aff37fb 100644 --- a/Tests/Python/PythonWrapperTests.cs +++ b/Tests/Python/PythonWrapperTests.cs @@ -30,7 +30,7 @@ public void ThrowsOnMissingMember() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(nameof(ValidateImplementationOf), MissingMethod1); + var module = PyModule.FromString(nameof(ValidateImplementationOf), MissingMethod1); var model = module.GetAttr("ModelMissingMethod1"); Assert.That(() => model.ValidateImplementationOf(), Throws .Exception.InstanceOf().With.Message.Contains("Method1")); @@ -42,7 +42,7 @@ public void DoesNotThrowWhenInterfaceFullyImplemented() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(nameof(ValidateImplementationOf), FullyImplemented); + var module = PyModule.FromString(nameof(ValidateImplementationOf), FullyImplemented); var model = module.GetAttr("FullyImplementedModel"); Assert.That(() => model.ValidateImplementationOf(), Throws.Nothing); } @@ -53,7 +53,7 @@ public void DoesNotThrowWhenDerivedFromCSharpModel() { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString(nameof(ValidateImplementationOf), DerivedFromCsharp); + var module = PyModule.FromString(nameof(ValidateImplementationOf), DerivedFromCsharp); var model = module.GetAttr("DerivedFromCSharpModel"); Assert.That(() => model.ValidateImplementationOf(), Throws.Nothing); } diff --git a/Tests/Python/SecurityCustomModelTests.cs b/Tests/Python/SecurityCustomModelTests.cs index f29a92209f29..a3cbc8d5ecaa 100644 --- a/Tests/Python/SecurityCustomModelTests.cs +++ b/Tests/Python/SecurityCustomModelTests.cs @@ -77,7 +77,7 @@ private PyObject CreateCustomBuyingPowerModel(string code) { using (Py.GIL()) { - var module = PythonEngine.ModuleFromString("CustomBuyingPowerModel", code); + var module = PyModule.FromString("CustomBuyingPowerModel", code); return module.GetAttr("CustomBuyingPowerModel").Invoke(); } } diff --git a/Tests/QuantConnect.Tests.csproj b/Tests/QuantConnect.Tests.csproj index 07af6462964d..1edc8f09c6e6 100644 --- a/Tests/QuantConnect.Tests.csproj +++ b/Tests/QuantConnect.Tests.csproj @@ -34,7 +34,7 @@ - + diff --git a/Tests/Research/QuantBookFundamentalTests.cs b/Tests/Research/QuantBookFundamentalTests.cs index 43b5d9b0af39..c53aa11fb9a3 100644 --- a/Tests/Research/QuantBookFundamentalTests.cs +++ b/Tests/Research/QuantBookFundamentalTests.cs @@ -144,7 +144,7 @@ public void PyReturnNullTest(dynamic input) { var testModule = _module.FundamentalHistoryTest(); var data = testModule.getFundamentals(input[0], input[1], input[2], input[3]); - Assert.IsEmpty(data); + Assert.AreEqual(true, (bool)data.empty); } } diff --git a/Tests/ResearchRegressionTests.cs b/Tests/ResearchRegressionTests.cs index 2cea4e66f90e..c4bdb2fcd84f 100644 --- a/Tests/ResearchRegressionTests.cs +++ b/Tests/ResearchRegressionTests.cs @@ -17,6 +17,7 @@ using NUnit.Framework; using QuantConnect.Configuration; using QuantConnect.Interfaces; +using QuantConnect.Logging; using QuantConnect.Util; using System; using System.Collections.Generic; @@ -213,6 +214,8 @@ private static string RunResearchNotebookAndGetOutput(string notebookPath, strin { var args = $"-m papermill \"{notebookPath}\" \"{notebookoutputPath}\" --log-output --cwd {workingDirectoryForNotebook}"; + Log.Trace($"ResearchRegressionTests.RunResearchNotebookAndGetOutput(): running '{_pythonLocation}' args {args}"); + // Use ProcessStartInfo class var startInfo = new ProcessStartInfo(_pythonLocation, args) { @@ -228,10 +231,16 @@ private static string RunResearchNotebookAndGetOutput(string notebookPath, strin { StartInfo = startInfo, }; + process.Start(); process.BeginErrorReadLine(); process.BeginOutputReadLine(); - process.WaitForExit(); + + if (!process.WaitForExit(1000 * 30)) + { + process.Kill(); + Assert.Fail("Timeout waiting for process to exit"); + } return File.ReadAllText(notebookoutputPath); } diff --git a/ToolBox/QuantConnect.ToolBox.csproj b/ToolBox/QuantConnect.ToolBox.csproj index d274e5bf219d..7af6acbe5748 100644 --- a/ToolBox/QuantConnect.ToolBox.csproj +++ b/ToolBox/QuantConnect.ToolBox.csproj @@ -1,4 +1,4 @@ - + Debug AnyCPU @@ -40,7 +40,7 @@ - +