T. Meyer ROD Software Workshop 18-19 July 2002 Scripting and Interpreted Languages Tom Meyer Iowa State University

Slides:



Advertisements
Similar presentations
CS 450 Module R4. R4 Overview Due on March 11 th along with R3. R4 is a small yet critical part of the MPX system. In this module, you will add the functionality.
Advertisements

CS 450 Module R1. R1 Introduction In Module R1, you will implement a user interface (command handler). There are a couple of options: ▫Command Line: interface.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
ITEC 320 Lecture 24 OO in Ada (2). Review Questions? Inheritance in Ada.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices.
Advanced Programming in the UNIX Environment Hop Lee.
ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between kloc.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
Guide to Programming with Python Chapter Ten GUI Development: The Mad Lib Program.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
By – Tanvir Alam.  This tutorial offers several things.  You’ll see some neat features of the language.  You’ll learn the right things to google. 
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
SCRIPTING II/III References:
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
ITEC 320 C++ Examples.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 20 Graphical User Interface (GUI)
Python Programming Graphical User Interfaces Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Computing Science 1P Lecture 17: Friday 23 rd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Applications Development
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
How the Session Works Outline Practical on arrival Talk 1 Reflect on practical Clarify concepts Practical exercises at your own pace Talk 2: Further concepts.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements.
July Doxygen A Code Documentation System Doxygen generates documentation directly from the structure and comments in the code –Browsable HTML documentation.
System Programming Practical Session 7 C++ Memory Handling.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Guide to Programming with Python
FILE MANAGEMENT SYSTEM Group Members RAMOLY Nathan HEMMI Florent AZANGUE Roméo CHOUIKH Jaouher JRIDI Bayrem NINTIDEM Estelle Olivia Supervisor M. TIMSIT.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
12-Jun-16 Event loops. 2 Programming in prehistoric times Earliest programs were all “batch” processing There was no interaction with the user Input Output.
Python C API overview References:
Development Environment
Pointers and Dynamic Arrays
Chapter 6 CS 3370 – C++ Functions.
Event loops 16-Jun-18.
Intro to Python Programming – Part II
Protection of System Resources
Command Line Arguments
TCL command in C, a simple example Nguyen Truong – GCS VN Aug 5, 2004
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Event loops.
Event loops 17-Jan-19.
Event loops 17-Jan-19.
C++ Tutorial Rob Jagnow
Tutorial: The Programming Interface
Whatcha doin'? Aims: Begin to create GUI applications. Objectives:
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Strings and Pointer Arrays
Event loops 8-Apr-19.
Homework Continue with K&R Chapter 5 Skipping sections for now
Lab 4: Introduction to Scripting
Pointers and dynamic objects
Event loops.
Event loops.
Event loops 19-Aug-19.
SPL – PS1 Introduction to C++.
Presentation transcript:

T. Meyer ROD Software Workshop July 2002 Scripting and Interpreted Languages Tom Meyer Iowa State University

T. Meyer ROD Software Workshop July 2002 Where to Use  Use for middle layer(s), where easy and rapid reconfiguration is necessary.  I don’t think we need to look further than CINT or Python for an interpreted language.

T. Meyer ROD Software Workshop July 2002 A Test Case  On a Friday morning, I asked an undergraduate student to code a test case of a layer of Python code sandwiched between two layers of C++ code.  By that evening, he had a working example.  By the next day, he had a demo version interfaced to RodModule. His demo prompts the user for ROD parameters and a data string to be passed to the ECHO primitive.  The hardest part was understanding the poorly written documentation.  However, he is a VERY capable student with a lot of Python experience.  Nevertheless, this convinced me that this approach is very feasible.  I’m pretty sure CINT would be comparably easy to interface, but no test case has been done (volunteers?).

T. Meyer ROD Software Workshop July 2002 py_demo: Source Files  py_demo.cxx - The main routine  ParamGetter.h - C++ header file for ParamGetter class  ParamGetter.cxx – The implementation of ParamGetter class  py_demo.py – The Python source

T. Meyer ROD Software Workshop July 2002 py_demo.cxx (1) // py_demo.cxx // This demo calls a python script from C++ #include "py_demo/ParamGetter.h" // My code to deal with returned params #include #include "RodPrimitive.h" #include "primParams.h" #include "RodModule.h" #include "../VmeInterface/RCCVmeInterface.h" using namespace SctPixelRod; int main(int argc, char **argv) { RCCVmeInterface *vme = new RCCVmeInterface(); // Initialize my ParamGetter class. See the code in the py_demo directory. // What this does is execute the function GetParams from the python script // echo_params.py. We can then use p's methods to get the returned params. ParamGetter p("echo_params", argc, argv); // Get five long ints from the dictionary returned by GetParams, and // deal with them appropriately. UINT32 baseaddr = p.GetLongParam("BaseAddress"); UINT32 mapsize = p.GetLongParam("MapSize"); UINT32 slot = p.GetLongParam("SlotNum"); UINT32 numslaves = p.GetLongParam("NumSlaves"); UINT32 body = p.GetLongParam("ByteToEcho"); RodModule *rod = new RodModule(baseaddr, mapsize, *vme, slot, numslaves); // Create a RodPrimitive based on the parameters we got. RodPrimitive *rp = new RodPrimitive(4,1,ECHO,&body);

T. Meyer ROD Software Workshop July 2002 py_demo.cxx (2) // Create a RodPrimList with rp as its only element. RodPrimList *pl = new RodPrimList(1); pl->insert(pl->begin(), *rp); // Build PrimList message. pl->bufferBuild(); // Now send it. rod->sendPrimList(MASTER_INBUFF, *pl); // Wait for the PrimList to finish executing. while (rod->primHandler != PRIM_EXECUTING && rod->primHandler != PRIM_WAITING) {} // Deal with the output from the Rod. RodOutList *outlist = rod->getOutList(); cout << "outLength=" << outList[0] << ", outIndex=" << outList[1] << ", outNumPrims=" << outList[2] << endl << "primLength=" << outList[3] << ", primIndex=" << outList[4] << ", primId=" << outList[5] << endl; cout << "data: " << endl; for (int i=0; i<outList[3]-3; i++) cout << outList[6+i] << endl; // Clear the PrimList and free the memory we used. primList->clear(); delete vme, rod, pl, rp, outlist; return 0; }

T. Meyer ROD Software Workshop July 2002 py_demo.py (1) class App: # App constructor def __init__(self, master): from Tkinter import * # Create some string variables to hold the input parameters. self.base=StringVar(master) self.byte=StringVar(master) self.maps=StringVar(master) self.slot=StringVar(master) self.slvs=StringVar(master) # Create a tk frame that will hold each element of our gui. self.frame = Frame(master) self.frame.pack() # Now create a subframe of frame that holds the entry box for # the base address parameter. self.subframe1 = Frame(self.frame) self.ent1 = Entry(self.subframe1, textvar=self.base) self.ent1.pack(side=RIGHT) self.lab1 = Label(self.subframe1, text='Base address:') self.lab1.pack(side=LEFT) self.subframe1.pack() # And again for the map size parameter. self.subframe2 = Frame(self.frame) self.ent2 = Entry(self.subframe2, textvar=self.maps) self.ent2.pack(side=RIGHT) self.lab2 = Label(self.subframe2, text='Map size:') self.lab2.pack(side=LEFT) self.subframe2.pack()

T. Meyer ROD Software Workshop July 2002 py_demo.py (2) # And again for the slot number. self.subframe3 = Frame(self.frame) self.ent3 = Entry(self.subframe3, textvar=self.slot) self.ent3.pack(side=RIGHT) self.lab3 = Label(self.subframe3, text='Slot number:') self.lab3.pack(side=LEFT) self.subframe3.pack() # Still another for the number of slaves. self.subframe4 = Frame(self.frame) self.ent4 = Entry(self.subframe4, textvar=self.slvs) self.ent4.pack(side=RIGHT) self.lab4 = Label(self.subframe4, text='Number of slaves:') self.lab4.pack(side=LEFT) self.subframe4.pack() # Yet another for the byte to actually echo. self.subframe5 = Frame(self.frame) self.ent5 = Entry(self.subframe5, textvar=self.byte) self.ent5.pack(side=RIGHT) self.lab5 = Label(self.subframe5, text='Byte to echo:') self.lab5.pack(side=LEFT) self.subframe5.pack() # At the bottom, put in a button to click when you're done # inputting stuff. When clicked, it will call the command # self.done(). self.donebutton=Button(self.frame,text="Done", command=self.done) self.donebutton.pack()

T. Meyer ROD Software Workshop July 2002 py_demo.py (3) # When the Done button is clicked, this function is called and # the frame is closed. def done(self): self.frame.quit() # After the frame has been closed, this function can be called to # get the values input by the user. It returns a dictionary. def get_vals(self): return { 'BaseAddress' : int(self.base.get()), 'MapSize' : int(self.maps.get()), 'SlotNum' : int(self.slot.get()), 'NumSlaves' : int(self.slvs.get()), 'ByteToEcho' : int(self.byte.get()) } # The function name "GetParams" is hard-wired into the ParamGetter # C++ class. This is the function that will be called when the class # is initialized. def GetParams(): import Tkinter # Create a root window for Tk. root = Tkinter.Tk() # Create an App within the Tk root window. app = App(root) # Give it a name. root.title("Echo parameters") # Start the Tk event loop. root.mainloop() # The user terminates the event loop by clicking the Done button. # Now return the dictionary to C++ to deal with as it pleases. return app.get_vals()

T. Meyer ROD Software Workshop July 2002 ParamGetter.h #ifndef PARAMGETTER_H #define PARAMGETTER_H #include #include // Standard Python include class ParamGetter { private: PyObject *pName, *pModule, *pDict, *pFunc; PyObject *pArgs, *pValue; void Crash(string message); public: ParamGetter(string module_name, int argc, char **argv); ~ParamGetter(); long GetLongParam(string param_name); // Get a long int from the dictionary returned by a // python script. string GetStringParam(string param_name); // Get a std::string from the dictionary returned by a // python script. }; #endif

T. Meyer ROD Software Workshop July 2002 ParamGetter.cxx (1) #include "ParamGetter.h" // Functions to get parameters returned in a dictionary from a python script void ParamGetter::Crash(string message) { // Exit neatly so that core isn't dumped. You always have to do a DECREF // because of the way Python handles memory. cerr << message << endl; PyErr_Print(); Py_XDECREF(pValue); // Free all the memory Py_XDECREF(pArgs); // that we did not Py_XDECREF(pModule); // "borrow" from Py_XDECREF(pName); // Python Py_Finalize(); } ParamGetter::ParamGetter(string module_name, int argc, char **argv) { Py_Initialize(); // Start the interpreter PySys_SetArgv(argc,argv); // Set the arguments correctly }

T. Meyer ROD Software Workshop July 2002 ParamGetter.cxx (2) pName = PyString_FromString(module_name.c_str()); pModule = PyImport_Import(pName); //Import the python module if (!pModule) { Crash("Unable to load module."); return; } pDict = PyModule_GetDict(pModule); // Get a dictionary containing all the objects in the module pFunc = PyDict_GetItemString(pDict, "GetParams"); // Grab a pointer to the function GetParams pArgs = PyTuple_New(0); // We don't need to pass it any arguments if (!pFunc) { Crash("Unable to load function."); return; } if (!PyCallable_Check(pFunc)) { Crash("Unable to call function."); return; } pValue = PyObject_CallObject(pFunc, pArgs); // Call the function! if (!pValue) { Crash("Call failed."); return; }