public object getFunction(string name) { object p = EngineScope.GetVariable(name); if ((p != null)) { if (Engine.Operations.IsCallable(p)) { return(p); } } return(null); }
// Pass the script text to the interpreter and display results private void Intepret(string text_to_interpret) { object result = null; try { Undo.RegisterSceneUndo("script"); var scriptSrc = _ScriptEngine.CreateScriptSourceFromString(text_to_interpret); _historyText += "\n"; _historyText += text_to_interpret; _historyText += "\n"; result = scriptSrc.Execute(_ScriptScope); } // Log exceptions to the console too catch (System.Exception e) { Debug.LogException(e); _historyText += "\n"; _historyText += "# " + e.Message + "\n"; } finally { // grab the __print_buffer stringIO and get its contents var print_buffer = _ScriptScope.GetVariable("__print_buffer"); var gv = _ScriptEngine.Operations.GetMember(print_buffer, "getvalue"); var st = _ScriptEngine.Operations.Invoke(gv); var src = _ScriptEngine.CreateScriptSourceFromString("__print_buffer = sys.stdout = StringIO()"); src.Execute(_ScriptScope); if (st.ToString().Length > 0) { _historyText += ""; foreach (string l in st.ToString().Split('\n')) { _historyText += " " + l + "\n"; } _historyText += "\n"; } // and print the last value for single-statement evals if (result != null) { _historyText += "# " + result.ToString() + "\n"; } int lines = _historyText.Split('\n').Length; _historyScroll.y += (lines * 19); Repaint(); } }
private static void InitPythonFunctions() { bool needToAddSearchPath = (engine == null); engine = engine ?? ScriptUtil.CreatePythonEngine(); if (needToAddSearchPath) { var scriptsFolderPath = BatchRvt.GetBatchRvtScriptsFolderPath(); ScriptUtil.AddSearchPaths(engine, new[] { scriptsFolderPath }); } pathUtilModuleScope = pathUtilModuleScope ?? IronPythonHosting.Python.ImportModule(engine, "path_util"); PYTHON_FUNCTION_ExpandedFullNetworkPath = PYTHON_FUNCTION_ExpandedFullNetworkPath ?? pathUtilModuleScope.GetVariable("ExpandedFullNetworkPath"); revitFileListModuleScope = revitFileListModuleScope ?? IronPythonHosting.Python.ImportModule(engine, "revit_file_list"); PYTHON_FUNCTION_RevitFileInfo = PYTHON_FUNCTION_RevitFileInfo ?? revitFileListModuleScope.GetVariable("RevitFileInfo"); revitFileVersionModuleScope = revitFileVersionModuleScope ?? IronPythonHosting.Python.ImportModule(engine, "revit_file_version"); PYTHON_FUNCTION_GetRevitVersionNumberTextFromRevitVersionText = PYTHON_FUNCTION_GetRevitVersionNumberTextFromRevitVersionText ?? revitFileVersionModuleScope.GetVariable("GetRevitVersionNumberTextFromRevitVersionText"); }
public IronPython.Runtime.List GenerateListsOptionalCategory(int numberOfLists, int lengthOfEachList, bool isCategoryPool, System.Random rng, bool isTwoParter, bool isEvenNumberSession, string participantCode) { //////////////////////Load the python wordpool code Microsoft.Scripting.Hosting.ScriptScope scope = BuildPythonScope(); //////////////////////Load the word pool IronPython.Runtime.List all_words; if (isCategoryPool) { all_words = ReadWordsFromPoolTxt("ram_categorized_en", isCategoryPool); } else { all_words = ReadWordsFromPoolTxt("ram_wordpool_en", isCategoryPool); } //////////////////////For two part experiments, reliably shuffle according to participant name and construct halves, then shuffle again /// Otherwise, just shuffle if (isTwoParter) { System.Random reliable_random = new System.Random(participantCode.GetHashCode()); if (!isCategoryPool) { all_words = Shuffled(reliable_random, all_words); } else { all_words = CategoryShuffle(reliable_random, all_words, 12); } if (isEvenNumberSession) { all_words = (IronPython.Runtime.List)all_words.__getslice__(0, all_words.Count / 2); } else { all_words = (IronPython.Runtime.List)all_words.__getslice__(all_words.Count / 2, all_words.Count); } } if (isCategoryPool) { all_words = CategoryShuffle(rng, all_words, lengthOfEachList); } else { all_words = Shuffled(rng, all_words); } ////////////////////////////////////////////Call list creation functions from python //////////////////////Concatenate into lists with numbers var assign_list_numbers_from_word_list = scope.GetVariable("assign_list_numbers_from_word_list"); var words_with_listnos = assign_list_numbers_from_word_list(all_words, numberOfLists); //////////////////////Build type lists and assign tpyes IronPython.Runtime.List stim_nostim_list = new IronPython.Runtime.List(); for (int i = 0; i < STIM_LIST_COUNT; i++) { stim_nostim_list.Add("STIM"); } for (int i = 0; i < NONSTIM_LIST_COUNT; i++) { stim_nostim_list.Add("NON-STIM"); } stim_nostim_list = Shuffled(rng, stim_nostim_list); var assign_list_types_from_type_list = scope.GetVariable("assign_list_types_from_type_list"); var words_with_types = assign_list_types_from_type_list(words_with_listnos, BASELINE_LIST_COUNT, stim_nostim_list, num_ps: PS_LIST_COUNT); //////////////////////Build stim channel lists and assign stim channels IronPython.Runtime.List stim_channels_list = new IronPython.Runtime.List(); for (int i = 0; i < A_STIM_COUNT; i++) { stim_channels_list.Add(new IronPython.Runtime.PythonTuple(new int[] { 0 })); } for (int i = 0; i < B_STIM_COUNT; i++) { stim_channels_list.Add(new IronPython.Runtime.PythonTuple(new int[] { 1 })); } for (int i = 0; i < AB_STIM_COUNT; i++) { stim_channels_list.Add(new IronPython.Runtime.PythonTuple(new int[] { 0, 1 })); } stim_channels_list = Shuffled(rng, stim_channels_list); var assign_multistim_from_stim_channels_list = scope.GetVariable("assign_multistim_from_stim_channels_list"); var words_with_stim_channels = assign_multistim_from_stim_channels_list(words_with_types, stim_channels_list); ////////////////////Build amplitude index list and assign amplitude indeces IronPython.Runtime.List amplitude_index_list = new IronPython.Runtime.List(); int lists_per_amplitude_index = 0; if (AMPLITUDE_COUNT != 0) { lists_per_amplitude_index = STIM_LIST_COUNT / AMPLITUDE_COUNT; } for (int amplitude_index = 0; amplitude_index < AMPLITUDE_COUNT; amplitude_index++) { for (int i = 0; i < lists_per_amplitude_index; i++) { amplitude_index_list.Add(amplitude_index); } } amplitude_index_list = Shuffled(rng, amplitude_index_list); var assign_amplitudes_from_amplitude_index_list = scope.GetVariable("assign_amplitudes_from_amplitude_index_list"); var words_with_amplitude_indices = assign_amplitudes_from_amplitude_index_list(words_with_stim_channels, amplitude_index_list); return(words_with_amplitude_indices); }