/// <summary> /// Gets the symbols/string from a PyObject /// </summary> /// <param name="pyObject">PyObject containing symbols</param> /// <returns>List of symbols</returns> private List <Symbol> GetSymbolsFromPyObject(PyObject pyObject) { using (Py.GIL()) { if (PyString.IsStringType(pyObject)) { Security security; if (Securities.TryGetValue(pyObject.ToString(), out security)) { return(new List <Symbol> { security.Symbol }); } return(null); } var symbols = new List <Symbol>(); foreach (var item in pyObject) { Security security; if (Securities.TryGetValue(item.ToString(), out security)) { symbols.Add(security.Symbol); } } return(symbols.Count == 0 ? null : symbols); } }
public new object this[string key] { get { if (ContainsKey(key)) { return(base[key]); } if (_lang.PyLang == null) { return(null); } object ret = null; using (Py.GIL()) { var pyKeyStr = new PyString(key); var pyObj = (PyObject)_lang.PyLang.meta.__getitem__(pyKeyStr); if (!PyString.IsStringType(pyObj)) { throw new NotImplementedException(); } var pyValStr = new PyString(pyObj); ret = pyValStr.ToString(); Add(key, ret); } return(ret); } }
public static object ToCLI(this PyObject o) { if (PySequence.IsSequenceType(o)) { var list = new List <object>(); foreach (PyObject subo in o) { list.Add(subo.ToCLI()); } return(list); } if (PyString.IsStringType(o)) { return(o.As <string>()); } if (PyInt.IsIntType(o)) { return(o.As <long>()); } if (PyLong.IsLongType(o)) { return(o.As <long>()); } if (PyFloat.IsFloatType(o)) { return(o.As <double>()); } return(o); }
/// <summary> /// Convert Python input to a list of Symbols /// </summary> /// <param name="input">Object with the desired property</param> /// <returns>List of Symbols</returns> public static IEnumerable <Symbol> ConvertToSymbols(PyObject input) { List <Symbol> symbolsList; Symbol symbol; using (Py.GIL()) { // Handle the possible types of conversions if (PyList.IsListType(input)) { List <string> symbolsStringList; //Check if an entry in the list is a string type, if so then try and convert the whole list if (PyString.IsStringType(input[0]) && input.TryConvert(out symbolsStringList)) { symbolsList = new List <Symbol>(); foreach (var stringSymbol in symbolsStringList) { symbol = QuantConnect.Symbol.Create(stringSymbol, SecurityType.Equity, Market.USA); symbolsList.Add(symbol); } } //Try converting it to list of symbols, if it fails throw exception else if (!input.TryConvert(out symbolsList)) { throw new ArgumentException($"Cannot convert list {input.Repr()} to symbols"); } } else { //Check if its a single string, and try and convert it string symbolString; if (PyString.IsStringType(input) && input.TryConvert(out symbolString)) { symbol = QuantConnect.Symbol.Create(symbolString, SecurityType.Equity, Market.USA); symbolsList = new List <Symbol> { symbol }; } else if (input.TryConvert(out symbol)) { symbolsList = new List <Symbol> { symbol }; } else { throw new ArgumentException($"Cannot convert object {input.Repr()} to symbol"); } } } return(symbolsList); }
public void IsStringFalse() { var t = new PyInt(5); Assert.False(PyString.IsStringType(t)); }
public void IsStringTrue() { var t = new PyString("foo"); Assert.True(PyString.IsStringType(t)); }
public static bool IsString(this PyObject obj) { return(PyString.IsStringType(obj)); }