public static Dictionary <string, object> ToCDictionary(DesignScript.Builtin.Dictionary dictionary) { List <string> keys = dictionary.Keys.ToList(); List <object> values = new List <object>(); foreach (var value in dictionary.Values) { //TODO: add support for deeply nested dictionarys if (value.GetType() == typeof(DesignScript.Builtin.Dictionary)) { DesignScript.Builtin.Dictionary subDict = value as DesignScript.Builtin.Dictionary; List <string> subKeys = subDict.Keys.ToList(); List <object> subVals = subDict.Values.ToList(); var newDict = subKeys.Zip(subVals, (k, v) => new { k, v }).ToDictionary(x => x.k, x => x.v); values.Add(newDict); } else { values.Add(value); } } Dictionary <string, object> cDict = keys.Zip(values, (k, v) => new { k, v }) .ToDictionary(x => x.k, x => x.v); return(cDict); }
/// SpeckleCore does not currently support dictionaries, therefore avoiding the canonical ToSpeckle public static Dictionary <string, object> ToSpeckleX(this DesignScript.Builtin.Dictionary dict) { if (dict == null) { return(null); } var speckleDict = new Dictionary <string, object>(); foreach (var key in dict.Keys) { object value = dict.ValueAtKey(key); if (value is DesignScript.Builtin.Dictionary) { value = (value as DesignScript.Builtin.Dictionary).ToSpeckleX(); } //TODO: //else if (value is Geometry) //{ // value = Converter.Serialise(value); //} speckleDict.Add(key, value); } return(speckleDict); }
/// <summary> /// Returns the value of dictionary at a given key. /// </summary> /// <param name="dictionary">Dictionary exposing all available values</param> /// <param name="key">The key of the value to retrieve</param> /// <returns>The value retrieved from the dictionary at the given key</returns> public static object UnPackOutputByKey(DesignScript.Builtin.Dictionary dictionary, string key) { if (dictionary == null) { return(null); } return(dictionary.ValueAtKey(key)); }
private Base DictionaryToBase(DesignScript.Builtin.Dictionary dsDic) { var @base = new Base(); foreach (var key in dsDic.Keys) { @base[key] = RecurseTreeToSpeckle(dsDic.ValueAtKey(key)); } return(@base); }
/// <summary> /// /// </summary> /// <param name="dataframe"></param> /// <param name="columns"></param> /// <returns></returns> public static DataFrame DropOutliers(DataFrame dataframe, DesignScript.Builtin.Dictionary zValues, int standardDeviation) { string jsonStr = dataframe.InternalDfJson; Dictionary <string, object> zValsDict = DictionaryHelpers.ToCDictionary(zValues); // Build argument JSON objec dynamic arguments = new JObject(); arguments.jsonStr = jsonStr; arguments.z_values = JToken.FromObject(zValsDict); arguments.standard_deviation = JToken.FromObject(standardDeviation); string dataframeJson = DynamoPandas.Pandamo.PythonRestCall .webUriPostCaller(PythonConstants.webUri + UrlPrefix + "/drop_outliers/", arguments); DataFrame df = new DataFrame(dataframeJson); return(df); }
/// <summary> /// Creates a new instance of JsonObject from a Dynamo Dictionary's components. /// </summary> /// <param name="dictionary">DesignDcript.Builtin.Dictionary</param> /// <returns name="jsonObject">New JsonObject</returns> public static JsonObject ByDictionary(DesignScript.Builtin.Dictionary dictionary) { List <string> keys = dictionary.Keys.ToList(); List <object> values = new List <object>(); foreach (var value in dictionary.Values) { if (value.GetType() == typeof(DesignScript.Builtin.Dictionary)) { values.Add(JsonObject.ByDictionary(value as DesignScript.Builtin.Dictionary)); } else { values.Add(value); } } return(new JsonObject(keys, values, true, JsonOption.None)); }