static void Main(string[] args) { Console.WriteLine("Efficient matrix multiplication with NumPy:"); // before starting the measurement, let us call numpy once to get the setup checks done. np.arange(1); var stopwatch = Stopwatch.StartNew(); var a1 = np.arange(60000).reshape(300, 200); var a2 = np.arange(80000).reshape(200, 400); var result = np.matmul(a1, a2); stopwatch.Stop(); Console.WriteLine($"execution time with NumPy: {stopwatch.Elapsed.TotalMilliseconds}ms\n"); Console.WriteLine("Result:\n" + result.repr); Console.WriteLine("executing on bg thread"); var a = np.arange(1000); var b = np.arange(1000); // https://github.com/pythonnet/pythonnet/issues/109 PythonEngine.BeginAllowThreads(); Task.Run(() => { using (Py.GIL()) { np.matmul(a, b); Console.WriteLine("matmul on bg thread is done"); } }).Wait(); Console.WriteLine("Press key"); Console.ReadKey(); }
/// <summary> /// Creates Util module /// </summary> /// <returns>PyObject with utils</returns> private PyObject ImportUtil() { var code = "from clr import AddReference\n" + "AddReference(\"System\")\n" + "AddReference(\"QuantConnect.Common\")\n" + "from QuantConnect.Python import PythonData\n" + "import decimal\n" + // OnPythonData call OnData after converting the Slice object "def OnPythonData(self, data):\n" + " self.OnData(PythonSlice(data))\n" + // PythonSlice class "class PythonSlice(dict):\n" + " def __init__(self, slice):\n" + " for data in slice:\n" + " self[data.Key] = Data(data.Value)\n" + " self[data.Key.Value] = Data(data.Value)\n" + // Python Data class: Converts custom data (PythonData) into a python object''' "class Data(object):\n" + " def __init__(self, data):\n" + " members = [attr for attr in dir(data) if not callable(attr) and not attr.startswith(\"__\")]\n" + " for member in members:\n" + " setattr(self, member, getattr(data, member))\n" + " if not isinstance(data, PythonData): return\n" + " for member in data.DynamicMembers:\n" + " val = data[member]\n" + " setattr(self, member, decimal.Decimal(val) if isinstance(val, float) else val)"; using (Py.GIL()) { return(PythonEngine.ModuleFromString("AlgorithmPythonUtil", code)); } }
/// <summary> /// Converts an enumerable of <see cref="Slice"/> in a pandas.DataFrame /// </summary> /// <param name="data">Enumerable of <see cref="Slice"/></param> /// <returns><see cref="PyObject"/> containing a pandas.DataFrame</returns> public PyObject GetDataFrame(IEnumerable <Slice> data) { var symbols = data.SelectMany(x => x.Keys).Distinct().OrderBy(x => x.Value); // If data contains derivatives and its underlying, // we get the underlying to exclude it from the dataframe Symbol underlying = null; var derivatives = symbols.Where(x => x.HasUnderlying); if (derivatives.Count() > 0) { underlying = derivatives.First().Underlying; } using (Py.GIL()) { var dataFrame = _pandas.DataFrame(); foreach (var symbol in symbols) { if (symbol == underlying) { continue; } var items = new PyObject[] { dataFrame, GetDataFrame(data.Get <QuoteBar>(symbol)), GetDataFrame(data.Get <TradeBar>(symbol)) }; dataFrame = _pandas.concat(new PyList(items)); } return(dataFrame); } }
public void OldFillModel_NewFillContextAndMarketFill_Py() { using (Py.GIL()) { var module = PythonEngine.ModuleFromString(Guid.NewGuid().ToString(), "from clr import AddReference\n" + "AddReference(\"QuantConnect.Common\")\n" + "from QuantConnect.Orders.Fills import FillModel, Fill\n" + "class CustomFillModel(FillModel):\n" + " def __init__(self):\n" + " self.FillWasCalled = False\n" + " self.MarketFillWasCalled = False\n" + " def Fill(self, parameters):\n" + " self.FillWasCalled = True\n" + " self.Parameters = parameters\n" + " return Fill(self.MarketFill(parameters.Security, parameters.Order))\n" + " def MarketFill(self, asset, order):\n" + " self.MarketFillWasCalled = True\n" + " return super().MarketFill(asset, order)"); var customFillModel = module.GetAttr("CustomFillModel").Invoke(); var wrapper = new FillModelPythonWrapper(customFillModel); var result = wrapper.Fill(new FillModelParameters( _security, new MarketOrder(_security.Symbol, 1, orderDateTime), new MockSubscriptionDataConfigProvider(_config) )); bool called; customFillModel.GetAttr("FillWasCalled").TryConvert(out called); Assert.True(called); customFillModel.GetAttr("MarketFillWasCalled").TryConvert(out called); Assert.True(called); Assert.IsNotNull(result); Assert.AreEqual(OrderStatus.Filled, result.OrderEvent.Status); } }
public void TestReadme() { dynamic np; try { np = Py.Import("numpy"); } catch (PythonException) { Assert.Inconclusive("Numpy or dependency not installed"); return; } Assert.AreEqual("1.0", np.cos(np.pi * 2).ToString()); dynamic sin = np.sin; StringAssert.StartsWith("-0.95892", sin(5).ToString()); double c = np.cos(5) + sin(5); Assert.AreEqual(-0.675262, c, 0.01); dynamic a = np.array(new List <float> { 1, 2, 3 }); Assert.AreEqual("float64", a.dtype.ToString()); dynamic b = np.array(new List <float> { 6, 5, 4 }, Py.kw("dtype", np.int32)); Assert.AreEqual("int32", b.dtype.ToString()); Assert.AreEqual("[ 6. 10. 12.]", (a * b).ToString()); }
internal static List <ndarray> LoadDataset(Gpt2Encoder encoder, List <string> fileNames) { if (encoder == null) { throw new ArgumentNullException(nameof(encoder)); } var tokenChunks = new List <ndarray>(); foreach (string file in fileNames) { Debug.WriteLine($"Reading {file}"); if (Path.GetExtension(file) == ".npz") { // pre-encoded dynamic npzObject = np.load(file); var npz = npzObject.__enter__(); foreach (var item in npz.files) { tokenChunks.Add(npz[item]); } npzObject.__exit__(); } else { string rawText = File.ReadAllText(file); if (String.IsNullOrWhiteSpace(rawText)) { continue; } dynamic numpy = Py.Import("numpy"); PyObject tokens = numpy.stack(encoder.Encode(rawText)); tokenChunks.Add(ndarray.Wrap(tokens)); } } return(tokenChunks); }
/** * This method initializes python and returns the pointer. * Also protection against running multiple python initializations. * * method: StartPython() * * return type: Task<IntPtr> * * @author Anthony Shaidaee * @since 3/1/2021 * */ static async Task <IntPtr> StartPython() { Installer.InstallPath = Path.GetFullPath("."); Installer.LogMessage += Console.WriteLine; await Installer.SetupPython(); // initial setup if (PythonEngine.IsInitialized == false) { Installer.TryInstallPip(); Installer.PipInstallModule("pillow"); Installer.PipInstallModule("scipy"); Installer.PipInstallModule("numpy"); Installer.PipInstallModule("scikit-network"); PythonEngine.Initialize(); PythonEngine.BeginAllowThreads(); } IntPtr gs = PythonEngine.AcquireLock(); try { dynamic os = PythonEngine.ImportModule("os"); //Console.WriteLine("### Current working directory:\n\t" + os.getcwd()); //Console.WriteLine("### PythonPath:\n\t" + PythonEngine.PythonPath); dynamic pillow = Py.Import("PIL"); //Console.WriteLine("Pillow version: " + pillow.__version__); dynamic scipy = Py.Import("scipy"); dynamic np = Py.Import("numpy"); dynamic sknetwork = Py.Import("sknetwork"); } catch (PythonException pe) { Console.WriteLine(pe); } return(gs); }
public void RebalanceFunctionSecurityChanges(Language language) { TestPortfolioConstructionModel constructionModel; if (language == Language.Python) { constructionModel = new TestPortfolioConstructionModel(); using (Py.GIL()) { var func = PythonEngine.ModuleFromString("RebalanceFunc", @" from datetime import timedelta def RebalanceFunc(time): return time + timedelta(days=1)").GetAttr("RebalanceFunc"); constructionModel.SetRebalancingFunc(func); } } else { constructionModel = new TestPortfolioConstructionModel(time => time.AddDays(1)); } Assert.IsFalse(constructionModel.IsRebalanceDueWrapper( new DateTime(2020, 1, 2, 1, 0, 0), new Insight[0])); var security = new Security(Symbols.SPY, SecurityExchangeHours.AlwaysOpen(DateTimeZone.Utc), new Cash(Currencies.USD, 1, 1), SymbolProperties.GetDefault(Currencies.USD), new IdentityCurrencyConverter(Currencies.USD), new RegisteredSecurityDataTypesProvider(), new SecurityCache()); constructionModel.OnSecuritiesChanged(null, SecurityChanges.Added(security)); Assert.IsTrue(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 1, 2), new Insight[0])); Assert.IsFalse(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 1, 2), new Insight[0])); }
// Returns whether the client is connected or not. // If the client is not connected but is alive (see IsAlive), we assume // it is reconnecting and the method will wait (blocking the main thread) // for a certain time until the client reconnects. internal static bool EnsureConnection() { if (!IsAlive) { return(false); } // The client is alive. Give it some time to reconnect if it // is not var iter = PythonRunner.WaitForConnection(Constants.clientName, Constants.clientReconnectionTimeout); bool moving = true; while (moving) { using (Py.GIL()) { moving = iter.MoveNext(); // Use the Python time module to sleep. This gives the // interpreter a chance to schedule its threads dynamic time = Py.Import("time"); time.sleep(Constants.interpreterSleepPeriod); } } if (PythonRunner.IsClientConnected(Constants.clientName)) { return(true); } // The client never reconnected UnityEngine.Debug.LogWarning("The Shotgun client process is not connected. Please reimport the Shotgun package"); // Client is not connected, update the PID PID = -1; return(false); }
/// <summary> /// Creates Util module /// </summary> /// <returns>PyObject with utils</returns> private PyObject ImportUtil() { var code = "from clr import AddReference\n" + "AddReference(\"System\")\n" + "AddReference(\"QuantConnect.Common\")\n" + "import decimal\n" + // OnPythonData call OnData after converting the Slice object "def OnPythonData(self, data):\n" + " self.OnData(PythonSlice(data))\n" + // PythonSlice class "class PythonSlice(dict):\n" + " def __init__(self, slice):\n" + " for data in slice:\n" + " self[data.Key] = Data(data.Value)\n" + " self[data.Key.Value] = Data(data.Value)\n" + // Python Data class: Converts custom data (PythonData) into a python object''' "class Data(object):\n" + " def __init__(self, data):\n" + " members = [attr for attr in dir(data) if not callable(attr) and not attr.startswith(\"__\")]\n" + " for member in members:\n" + " setattr(self, member, getattr(data, member))\n" + " if not hasattr(data, 'GetStorageDictionary'): return\n" + " for kvp in data.GetStorageDictionary():\n" + " name = kvp.Key.replace('-',' ').replace('.',' ').title().replace(' ', '')\n" + " value = decimal.Decimal(kvp.Value) if isinstance(kvp.Value, float) else kvp.Value\n" + " setattr(self, name, value)"; using (Py.GIL()) { return(PythonEngine.ModuleFromString("AlgorithmPythonUtil", code)); } }
public void enable_grad() { //>>> x = torch.tensor([1], requires_grad = True) //>>> with torch.no_grad(): //... with torch.enable_grad(): //... y = x * 2 //>>> y.requires_grad //True //>>> y.backward() //>>> x.grad //>>> @torch.enable_grad() //...def doubler(x): //... return x * 2 //>>> with torch.no_grad(): //... z = doubler(x) //>>> z.requires_grad //True var x = torch.tensor(new double[] { 1 }, requires_grad: true); Tensor y = null; Py.With(torch.no_grad(), _ => { Py.With(torch.enable_grad(), __ => { y = x * 2; Assert.AreEqual(true, y.requires_grad); y.backward(); var grad = x.grad; Assert.NotNull(grad); }); }); Py.With(torch.no_grad(), _ => { var z = x * 2; }); Assert.AreEqual(true, y.requires_grad); }
/* * Returns a list of payoff based on input participants list and their contributions * Firstly creates a 'readable' python list containing participants' contribution, then obtaining payoff list from python class object */ public List <double> Calculate_Payoff(double budgetToAllocate, dynamic schemeObj, List <Player> participantList, double data_qual_weight, double data_quant_weight) { List <double> tempList = new List <double>(); using (Py.GIL()) { foreach (Player p in participantList) { //parameters to create list int pid = p.Pid; double dataQuality = p.DataOwned.DataQuality; double dataQuantity = p.DataOwned.DataQuantity; double modelQuality = dataQuality * data_qual_weight + data_quant_weight * dataQuantity; double filterTOCHANGE = 0; double costContribution = p.Asset; this.UtilModule.inputList(pid, dataQuality, dataQuantity, modelQuality, filterTOCHANGE, costContribution); } //assigns budget for this round schemeObj.Env.budget = budgetToAllocate; Console.WriteLine("Federation allocated budget = $" + budgetToAllocate); dynamic obj = schemeObj.payout_calculation(this.UtilModule.listBuild); Console.WriteLine(this.UtilModule.listBuild); Console.WriteLine(obj); //reset listbuild in python this.UtilModule.reInit(); for (int i = 0; i < participantList.Count; i++) { tempList.Add(Math.Round((double)obj[i], 2)); Console.WriteLine("Player " + (i + 1) + " received $" + tempList[i]); } schemeObj.Env.t += 1; } return(tempList); }
public void Py_AccessPropertyThatDoesNotExists_ReturnsEmptyList_WhenTypeIsIncludedInRegisteredTypes() { var registeredTypes = new RegisteredSecurityDataTypesProvider(); registeredTypes.RegisterType(typeof(TradeBar)); dynamic securityData = new DynamicSecurityData(registeredTypes, _cache); using (Py.GIL()) { dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * def Test(dynamicData): data = dynamicData.GetAll(TradeBar) if data is None: raise Exception('Unexpected None value') if len(data) != 0: raise Exception('Unexpected length')").GetAttr("Test"); Assert.DoesNotThrow(() => test(securityData)); } }
public void Py_StoreData_Get_UsesTypeName() { var data = new DynamicSecurityData(_dataTypesProvider, _cache); _cache.StoreData(new List <TradeBar> { new TradeBar(DateTime.UtcNow, Symbols.SPY, 10m, 20m, 5m, 1, 10000) }, typeof(TradeBar)); using (Py.GIL()) { dynamic test = PyModule.FromString("testModule", @" from AlgorithmImports import * def Test(dynamicData): data = dynamicData.Get(TradeBar) if data.Close != 1: raise Exception('Unexpected value')").GetAttr("Test"); Assert.DoesNotThrow(() => test(data)); } }
public void TestList() { using (Py.GIL()) { var scope = Py.CreateScope(); scope.Exec( "a=[1, \"2\"]" ); dynamic a = scope.Get("a"); //now, I want to convert res into List<object> var converter = NewMyListConverter(); //create a instance of PyConverter var b = converter.ToClr(a); //b is a List of CLR objects var c = converter.ToPython(b); // object c0 = c[0]; object c1 = c[1]; var d = converter.ToClr(c); scope.Dispose(); } }
public void CallPlotTests() { using (Py.GIL()) { // self.Plot('NUMBER', 0.1) Assert.DoesNotThrow(() => _algorithm.call_plot_number_test()); // self.Plot('STD', self.std), where self.sma = self.SMA('SPY', 20) Assert.DoesNotThrow(() => _algorithm.call_plot_sma_test()); // self.Plot('SMA', self.sma), where self.std = self.STD('SPY', 20) Assert.DoesNotThrow(() => _algorithm.call_plot_std_test()); // self.Plot("ERROR", self.Name), where self.Name is IAlgorithm.Name: string Assert.Throws <PythonException>(() => _algorithm.call_plot_throw_test()); // self.Plot("ERROR", self.Portfolio), where self.Portfolio is IAlgorithm.Portfolio: instance of SecurityPortfolioManager Assert.Throws <ArgumentException>(() => _algorithm.call_plot_throw_managed_test()); // self.Plot("ERROR", self.a), where self.a is an instance of a python object Assert.Throws <ArgumentException>(() => _algorithm.call_plot_throw_pyobject_test()); } }
/// <summary> /// Try to get the length of arguments of a method /// </summary> /// <param name="pyObject">Object representing a method</param> /// <param name="length">Lenght of arguments</param> /// <returns>True if pyObject is a method</returns> private static bool TryGetArgLength(PyObject pyObject, out int length) { using (Py.GIL()) { dynamic inspect = Py.Import("inspect"); if (inspect.isfunction(pyObject)) { var args = inspect.getargspec(pyObject).args; length = new PyList(args).Length(); return(true); } if (inspect.ismethod(pyObject)) { var args = inspect.getargspec(pyObject).args; length = new PyList(args).Length() - 1; return(true); } } length = 0; return(false); }
/// <summary> /// Generate the leverage utilization plot using the python libraries. /// </summary> public override string Render() { var backtestSeries = Metrics.LeverageUtilization(_backtestPortfolios).FillMissing(Direction.Forward); var liveSeries = Metrics.LeverageUtilization(_livePortfolios).FillMissing(Direction.Forward); var base64 = ""; using (Py.GIL()) { var backtestList = new PyList(); var liveList = new PyList(); backtestList.Append(backtestSeries.Keys.ToList().ToPython()); backtestList.Append(backtestSeries.Values.ToList().ToPython()); liveList.Append(liveSeries.Keys.ToList().ToPython()); liveList.Append(liveSeries.Values.ToList().ToPython()); base64 = Charting.GetLeverage(backtestList, liveList); } return(base64); }
public void HandlesEmptyEnumerable() { var converter = new PandasConverter(); var rawBars = Enumerable.Empty <TradeBar>().ToArray(); // GetDataFrame with argument of type IEnumerable<TradeBar> dynamic dataFrame = converter.GetDataFrame(rawBars); using (Py.GIL()) { Assert.IsTrue(dataFrame.empty.AsManagedObject(typeof(bool))); } // GetDataFrame with argument of type IEnumerable<TradeBar> var history = GetHistory(Symbols.SPY, Resolution.Minute, rawBars); dataFrame = converter.GetDataFrame(history); using (Py.GIL()) { Assert.IsTrue(dataFrame.empty.AsManagedObject(typeof(bool))); } }
public void PythonSlice_get_default() { using (Py.GIL()) { dynamic test = PythonEngine.ModuleFromString("testModule", @" from AlgorithmImports import * def Test(slice, symbol, default_value): return slice.get(symbol, default_value)").GetAttr("Test"); var pythonSlice = GetPythonSlice(); var expected = new QuoteBar { Symbol = Symbols.EURUSD, Time = DateTime.Now, Value = 9 }; PyObject result = null; Assert.DoesNotThrow(() => result = test(GetPythonSlice(), Symbols.EURUSD, expected)); BaseData actual; Assert.IsTrue(result.TryConvert(out actual)); Assert.AreEqual(expected.Symbol, actual.Symbol); Assert.AreEqual(expected.Value, actual.Value); } }
public IEnumerator <T> GetEnumerator() { PyObject iterObject = null; using (Py.GIL()) iterObject = new PyObject(Runtime.PyObject_GetIter(pyObject.Handle)); while (true) { using (Py.GIL()) { var item = Runtime.PyIter_Next(iterObject.Handle); if (item == IntPtr.Zero) { Runtime.CheckExceptionOccurred(); iterObject.Dispose(); break; } yield return((T) new PyObject(item).AsManagedObject(typeof(T))); } } }
public void OutputTypePyConsolidator() { using (Py.GIL()) { var module = PyModule.FromString(Guid.NewGuid().ToString(), "from AlgorithmImports import *\n" + "class CustomConsolidator():\n" + " def __init__(self):\n" + " self.InputType = QuoteBar\n" + " self.OutputType = QuoteBar\n" + " self.Consolidated = None\n" + " self.WorkingData = None\n"); var customConsolidator = module.GetAttr("CustomConsolidator").Invoke(); var wrapper = new DataConsolidatorPythonWrapper(customConsolidator); var time = DateTime.Today; var period = TimeSpan.FromMinutes(1); var type = wrapper.OutputType; Assert.True(type == typeof(QuoteBar)); } }
/// <summary> /// Wrapper for <see cref = "IAlgorithm.OnMarginCall" /> in Python /// </summary> /// <param name="requests"></param> public void OnMarginCall(List <SubmitOrderRequest> requests) { try { using (Py.GIL()) { _algorithm.OnMarginCall(requests); } } catch (PythonException pythonException) { // Pythonnet generated error due to List conversion if (pythonException.Message.Equals("TypeError : No method matches given arguments")) { _baseAlgorithm.OnMarginCall(requests); } // User code generated error else { throw pythonException; } } }
public void SecurityQuantBookHistoryTests(int year, int month, int day, SecurityType securityType, string symbol) { using (Py.GIL()) { var startDate = new DateTime(year, month, day); var securityTestHistory = _module.SecurityHistoryTest(startDate, securityType, symbol); // Get the last 10 candles var periodHistory = securityTestHistory.test_period_overload(10); var count = (periodHistory.shape[0] as PyObject).AsManagedObject(typeof(int)); Assert.AreEqual(10, count); // Get the one day of data var timedeltaHistory = securityTestHistory.test_period_overload(TimeSpan.FromDays(1)); var firstIndex = (DateTime)(timedeltaHistory.index.values[0] as PyObject).AsManagedObject(typeof(DateTime)); Assert.GreaterOrEqual(startDate.AddDays(-1).Date, firstIndex.Date); // Get the one day of data, ending one day before start date var startEndHistory = securityTestHistory.test_daterange_overload(startDate.AddDays(-1)); firstIndex = (DateTime)(startEndHistory.index.values[0] as PyObject).AsManagedObject(typeof(DateTime)); Assert.GreaterOrEqual(startDate.AddDays(-2).Date, firstIndex.Date); } }
public override string ToString() { IntPtr gs = PythonEngine.AcquireLock(); try { using (Py.GIL()) { var scope = PyScopeManager.Global.Get(CPythonEvaluator.globalScopeName); var pyobj = scope.Get(PythonObjectID.ToString()); return(pyobj.ToString()); } } catch (Exception e) { CPythonEvaluator.DynamoLogger?.Log($"error getting string rep of pyobj {this.PythonObjectID} {e.Message}"); return(this.PythonObjectID.ToString()); } finally { PythonEngine.ReleaseLock(gs); } }
/// <summary> /// Converts a dictionary with a list of <see cref="IndicatorDataPoint"/> in a pandas.DataFrame /// </summary> /// <param name="data">Dictionary with a list of <see cref="IndicatorDataPoint"/></param> /// <returns><see cref="PyObject"/> containing a pandas.DataFrame</returns> public PyObject GetIndicatorDataFrame(IDictionary <string, List <IndicatorDataPoint> > data) { using (Py.GIL()) { var pyDict = new PyDict(); foreach (var kvp in data) { var index = new List <DateTime>(); var values = new List <double>(); foreach (var item in kvp.Value) { index.Add(item.EndTime); values.Add((double)item.Value); } pyDict.SetItem(kvp.Key.ToLowerInvariant(), _pandas.Series(data: values, index: index)); } return(_pandas.DataFrame(pyDict, columns: data.Keys.Select(x => x.ToLowerInvariant()).OrderBy(x => x))); } }
public void PythonSlice_setdefault_success() { using (Py.GIL()) { dynamic test = PythonEngine.ModuleFromString("testModule", @" from clr import AddReference AddReference(""QuantConnect.Common"") from QuantConnect import * def Test(slice, symbol): return slice.setdefault(symbol)").GetAttr("Test"); var pythonSlice = GetPythonSlice(); dynamic expected = pythonSlice[Symbols.SPY]; PyObject result = null; Assert.DoesNotThrow(() => result = test(GetPythonSlice(), Symbols.SPY)); BaseData actual; Assert.IsTrue(result.TryConvert(out actual)); Assert.AreEqual(expected.Symbol, actual.Symbol); Assert.AreEqual(expected.Value, actual.Value); } }
public Sudoku Solve(Sudoku s) { using (Py.GIL()) { using (PyScope scope = Py.CreateScope()) { // convert the sudoku object to a PyObject PyObject pySudoku = s.ToPython(); // create a Python variable "sudoku" scope.Set("sudoku", pySudoku); // the sudoku object may now be used in Pythonn string code = "fullName = person.FirstName + ' ' + person.LastName"; scope.Exec(code); s = scope.Get <Sudoku>("sudoku"); } return(s); // PythonEngine.Exec("doStuff()"); } }
public void PassObjectInPython() { var stream = new StringBuilder(); dynamic sys = Py.Import("sys"); sys.testattr1 = stream; // Pass the .NET object in Python side PythonEngine.RunSimpleString( "import sys\n" + "sys.testattr2 = sys.testattr1\n" ); // Compare in Python PythonEngine.RunSimpleString( "import sys\n" + "sys.testattr3 = sys.testattr1 is sys.testattr2\n" ); Assert.AreEqual(sys.testattr3.ToString(), "True"); // Compare in .NET Assert.IsTrue(sys.testattr1.Equals(sys.testattr2)); }
/// <summary> /// Python implementation of GetFundamental, get fundamental data for input symbols or tickers /// </summary> /// <param name="input">The symbols or tickers to retrieve fundamental data for</param> /// <param name="selector">Selects a value from the Fundamental data to filter the request output</param> /// <param name="start">The start date of selected data</param> /// <param name="end">The end date of selected data</param> /// <returns>pandas DataFrame</returns> public PyObject GetFundamental(PyObject input, string selector = null, DateTime?start = null, DateTime?end = null) { //Covert to symbols var symbols = PythonUtil.ConvertToSymbols(input); //Fetch the data var fundamentalData = GetAllFundamental(symbols, selector, start, end); using (Py.GIL()) { var data = new PyDict(); foreach (var day in fundamentalData.OrderBy(x => x.Key)) { var orderedValues = day.Value.OrderBy(x => x.Key.ID.ToString()).ToList(); var columns = orderedValues.Select(x => x.Key.ID.ToString()); var values = orderedValues.Select(x => x.Value); var row = _pandas.Series(values, columns); data.SetItem(day.Key.ToPython(), row); } return(_pandas.DataFrame.from_dict(data, orient: "index")); } }