Table of Contents
Run time variable module. These variables are commonly associated with the MineSight® Grail widgets (GWidgets), and allow you to,
- Associate a variable that updates as at the same time a user updates a widget.
- Work within the MineSight®-Compass context.
The various type of basic RTV classes defined in this module all inherit from the base RTV class. The basic hierarchy is as follows,
- RTV
- FloatRTV
- IntegerRTV
- StringRTV
Base class for all RTV classes. As the base class, all setting, getting and validation is done here.
If you wish to construct your own customized RTV object, you will need to override the _fromstringimpl() and _tostringimpot().
You can listen to the variable changing by using the const.sigON_CHANGE message (see grail.const).
Note
This object was built up over a series of iterations. Looking at in hindsight, it seems the usererrormessage() method resembles the human appendix.
- Methods to override:
- _fromstringimpl
- Takes a string value and calls set with a converted value. Should raise a RTVValueError if the conversion fails.
- _tostringimpl
- Takes the result of get and converts it to a string.
Builds the RTV object.
Since this is the base class, the value and validity do not have reasonable default values. However the name, must have a value.
- Arguments:
- value
- Value for the RTV object.
- validity : Validity class
- What is "valid" for this object.
- name : string
- Name for this object, must be unique.
- fromstring(string_)
Converts the string into the internal value.
Conversion routine is defined by the _fromstringimpl() method.
- Arguments:
- string_ : string
- A string value that can be converted to the internal value.
- Raises:
- RTVError
- If the string_ is not a string.
- RTVValueError
- If the conversion can not work or the value is invalid.
Note
That in multi-run setup mode (for MineSight®-Compass), everything is acceptable.
- get()
- Returns the internal value.
- isvalid(value)
Determines if a value is valid or not.
This is based on the validity object.
- name()
- Returns the name of the variable.
- set(value)
- Set the internal value to the input value. This method will raise a RTVValueError if the value does not satisfy isvalid.
- setvalidity(validityobj)
- Indicate what validity object you would like to use internally.
- tostring()
- Converts the internal value to a string.
- usererrormessage()
- Returns the error message for invalid data (or None if no validity).
Represents a Float run-time-variable. The internal value is of type float. This class inherits from RTV.
Represents an Integer run-time-variable. The internal value is of type integer. This class inherits from RTV.
Represents a String run-time-variable. The internal value is of type string. This class inherits from RTV.
You can listen to the variable changing by using the const.sigON_CHANGE message (see grail.const).
this will execute the function on_var_change, with the new variable.
So what are these OnDemandRTV objects?
Well, in the early days we need a way to dynamically build RTV objects on the fly, as well as, use RTV objects that already exist. So for example, lets say on one execution of a procedure the user only needed 2 variables in a list, but on another execution they needed 8, and then again, they went back to 3. Basically, we never knew how many RTV's the user would need, so we can never pre-generate the number.
So our first step would be to make "enough" of them that we do not have any problems. To do that we would pre-generate a list like this,
NUM_LABELS = 20
itemlist = [rtv.StringRTV(name="item(%d)" % (i)) for i in range(NUM_LABELS)]
# later we'd access it like...
itemlist[10].set("one more wafer")
Alas, what if during the scripts execution we wanted to access the 50th item in that list. Well, we'd either have to make sure NUM_LABELS was set to 50 prior to creating that list, or re-create that list and hope to re-use existing variables, or take a deep breath and start using the OnDemandRTV objects.
That was a bit of the background, now on to how to use this beast.
So, the basic premise is that we will define how to generate a name for a RTV when one is created (and or an existing one is retrieved), and we will also define how to go about creating these variables when we need one on the fly. So revisiting our example using the item list. This time we won't pre-define a maximum value, and instead we will use the OnDemandRTV construct,
itemlist = rtv.OnDemandRTV(rtvclass=rtv.StringRTV,
configname="itemvar(%d)")
# ... later ...
# creates a StringRTV called "item(50)" and
# sets its value to "one more wafer"
itemlist(50).set("one more wafer")
So you can see you never have to pre-generate a variable. The OnDemandRTV basically takes any request and searches for the variable with an existing name itemvar(50) or creates a new variable with the name itemvar(50).
The configname keyword indicates how the name is generated when you make the call itemlist(50). Notice that the configuration works the same as,
print "itemvar(%d)" % (value)
where in our example, the itemvar(%d) was the configname.
As a slightly more complex version consider,
from grail import rtv
matrix = rtv.OnDemandRTV(rtvclass=rtv.FloatRTV,
configname="matrix(%d, %d)")
for i in range(3):
for j in range(3):
matrixelement = matrix(i, j)
In this above example you would have a set of variables generated for each i and j used in the for loop. In the end a call such as,
matrix(2, 2).set(1.)
Would retrieve already existing variable matrix(2, 2) and set the value to 1. However the call,
matrix(100, 100).set(10.)
Would create a new variable matrix(100,100) and set that value to 10.
Allows you to dynamically create variables on the fly.
The Validity classes are specialized based on the type of validation you wish to perform. All validity classes inherit from the base Validity class, and have the following hierarchy,
- Validity
- IntegerValidity
- RangedIntegerValidity
- NaturalNumberValidity
- FloatValidity
- RangedFloatValidity
- StringValidity
- AlphaNumericStringValidity
- AlphaStringValidity
- DigitStringValidity
- LengthStringValidity
Base class for all Validity objects.
Validity objects are used to configure RTV objects to process invalid and valid values. The StringRTV, FloatRTV, and IntegerRTV all come with obvious default validity objects.
If you wish to customize your own validity object you can do so by overriding the valid() method.
As a contrived example, consider a Validity object that only only accepts strings starting with the letter "f". We will inherit from the StringValidity since that gives us one part of our validation -- that it is a string, and we will put together the test in the valid() routine.
The python code for this would look like this,
Override
This method to define what valid is for your Validity object.
Note that all the Validity objects in the rtv module have their valid() methods overridden.
Defines a validity that checks for integers. Any value is valid if it satisfies the equality,
Defines a validity that checks for integers between [min, max].
Any value is valid if it is an integer and it lies between the minimum and maximum values.
The min/max use the following [min,max] range notation.
There is no error checking to ensure that the min and max are integers, and min is less than max.
Defines a validity that checks for Natural Numbers.
A natural number is any Integer that is greater than, but not equal to 0. As defined by grail.utils.isnatural() (see grail.utils).
Defines a validity that checks for Floating Point numbers.
If the value tested against was an integer, it will fail. Add '.0' to the integer to ensure that it passes.
Defines a Validity that checks for floats within a set range.
Valid values are floating point numbers and lie in the range [min, max]. The minimum and maximum are inclusive to within grail.const.FLOATING_POINT_TOLERANCE. The inclusivity is defined by the grail.utils.isclose() predicate (see grail.utils).
There is no error checking to ensure min and max are floating point numbers, and that min < max.
Defines a Validity object for strings.
Any value which is a string will be Valid.
Defines a Validity object for Alpha-Numeric Strings.
Any string with either numbers or alphabetic characters (English) will be valid. These would satisfy the string.isalnum() predicate.
Defines a Validity indicating that alpha character strings are valid.
A alpha character is any character that satisfies string.isalpha() [2].
Defines a Validity indicating that digit only strings are valid.
A digit is defined by any value in "1234567890".
Defines a validity that checks for a strings length.
A string that satisfies this has a len(value) that lies in the defined range: [min, max].
The following exceptions may be generated from this module or any classes defined within this module.
Error indicates that a value was invalid or uncovertable.
Removes a reference to a RTV object with the given name.
Since there is an internal map that keeps track of all the RTV objects that exist in the 'grail world', there is a chance that an rtv variable will exist for ever (the internal map holding the 1 last reference to the variable). This function provides a means to remove that rtv.
This function will raise a RTVError if the name is not found in the internal mapping.
Returns a dictionary of all the RTV objects stored in the system.
The returned dictionary is a copy of the internal one and is formatted as,
As you can see every variable's string representation (as supplied by the RTV.tostring() method), is brought into the dictionary.
This dictionary can be used by setvariablemap().
Note
That any RTV with a name prefixed with "__" is for internal use, and will not be returned by this function.
Cleans out the internal RTV map.
Useful for multiple runs within the same execution environment. If you do not understand what that last sentence means, do not call this function.
Sets the internal system variable map.
Variable map (vardict) passed in should be of the form,
Where a variable must be instantiated AND the str_value must be convertable for that variable.
This is a very tricky function to play with.
Warning
Use this function "sparingly" and "judiciously".
Indicates if you want to do a trace dump of variable changes.
Just prior to an onChange() signal, a message will be dumped to the desired stream.
Examples:
Using the trace to see all variables starting with "clown"
Turning of the tracer,
Writes out all system RTV's to the given stream.
[1] | "sys -- System-specific parameters and functions" Python Library Reference. 30 May 2003. 1 June 2004. <http://www.python.org/doc/2.2.3/lib/module-sys.html> |
[2] | "string -- Common string operations" Python Library Reference. 30 May 2003. 1 June 2004. <http://www.python.org/doc/2.2.3/lib/module-string.html> |