[4] | 1 | ------------------------------------------------------------------------ |
---|
| 2 | Rappture - Rapid APPlication infrastrucTURE |
---|
| 3 | ------------------------------------------------------------------------ |
---|
| 4 | The Rappture toolkit provides the basic building blocks for many |
---|
| 5 | scientific applications. These blocks can be composed using C++, or |
---|
| 6 | a high-level language, such as Python. Having a set of blocks for |
---|
| 7 | basic I/O, meshing, and numerical methods, a scientist can then |
---|
| 8 | focus on developing his core algorithm. |
---|
| 9 | ------------------------------------------------------------------------ |
---|
| 10 | AUTHORS: |
---|
| 11 | Michael J. McLennan, Purdue University |
---|
| 12 | ------------------------------------------------------------------------ |
---|
| 13 | Copyright (c) 2004 Purdue Research Foundation, West Lafayette, IN |
---|
| 14 | See the file "license.terms" for information on usage and |
---|
| 15 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
| 16 | ------------------------------------------------------------------------ |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | INSTALLATION |
---|
| 20 | ------------------------------------------------------------------------ |
---|
| 21 | This part of the Rappture toolkit works with Python. If you need to |
---|
| 22 | install Python on your system, visit the Download section of the site |
---|
| 23 | http://www.python.org. |
---|
| 24 | |
---|
| 25 | Assuming you already have Python installed, you can install Rappture |
---|
| 26 | as follows: |
---|
| 27 | |
---|
| 28 | % cd rappture/python |
---|
| 29 | % python setup.py install |
---|
| 30 | |
---|
| 31 | This will make Rappture a part of your normal Python installation, so |
---|
| 32 | you can use it in your own Python scripts, as shown below. |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | GETTING STARTED |
---|
| 36 | ------------------------------------------------------------------------ |
---|
| 37 | Here is a quick tutorial to get you started using Rappture in your |
---|
| 38 | Python scripts. |
---|
| 39 | |
---|
| 40 | Rappture applications load their data from a library object. You can |
---|
| 41 | load a library as follows: |
---|
| 42 | |
---|
| 43 | import Rappture |
---|
| 44 | lib = Rappture.library('example.xml') |
---|
| 45 | print lib.xml() |
---|
| 46 | |
---|
| 47 | The "import" statement loads the Rappture toolkit into your Python |
---|
| 48 | application. The next line creates a library object representing |
---|
| 49 | the data in the "example.xml" file. The last line prints the |
---|
| 50 | contents of the XML library to the screen. |
---|
| 51 | |
---|
| 52 | You can access values within the XML by using the get() method. |
---|
| 53 | For example, |
---|
| 54 | |
---|
| 55 | import Rappture |
---|
| 56 | lib = Rappture.library('example.xml') |
---|
| 57 | tval = lib.get('group(ambient).number(temp).current') |
---|
| 58 | |
---|
| 59 | The get() method follows a path through the XML. In this example, |
---|
| 60 | it looks for a <group id="ambient"> tag, and then finds the |
---|
| 61 | <number id="temp"> tag within it, then finds the <current> tag |
---|
| 62 | within it, and then returns the text between that and the closing |
---|
| 63 | </current> tag. |
---|
| 64 | |
---|
| 65 | You can also add new data to the XML by using the put() method. |
---|
| 66 | For example, we could change the temperature value that we queried |
---|
| 67 | above as follows: |
---|
| 68 | |
---|
| 69 | tval += 10 |
---|
| 70 | lib.put('group(ambient).number(temp).current',str(tval)) |
---|
| 71 | |
---|
| 72 | This changes the text within the <current>...</current> tag, |
---|
| 73 | replacing it with the new value. Note that the value must be a |
---|
| 74 | string, so we use str(tval), rather than tval itself, when inserting |
---|
| 75 | a value. |
---|
| 76 | |
---|
| 77 | You can also append to an existing value. For example, |
---|
| 78 | |
---|
| 79 | lib.put('group(ambient).number(temp).current', 'more text', append=1) |
---|
| 80 | |
---|
| 81 | This adds the string 'more text' after the value (tval) within the |
---|
| 82 | <current>...</current> tag. |
---|
| 83 | |
---|
| 84 | Suppose we wanted to add another number to the "ambient" group. |
---|
| 85 | We could do that by requesting "number#". The # sign indicates |
---|
| 86 | that you'd like to create a new number element (number1, number2, |
---|
| 87 | etc.). For example, we could add a number for a damping factor: |
---|
| 88 | |
---|
| 89 | lib.put('group(ambient).number#(damping).current','0.25') |
---|
| 90 | |
---|
| 91 | This creates a new element <number id="damping"> within |
---|
| 92 | <group id="ambient">, positioned just after the existing |
---|
| 93 | <number id="temp">. |
---|