3.2. pci.nspio module¶
Applications can generate various kinds of nonspatial I/O (NSPIO), including the following:
Debug information: highly detailed information that can be used for debugging errors.
Audit/logging information: information that is required by specific applications; for example, parameters of a CATALYST function.
Progress information: information that is related to the amount of work that has been done.
Report information: information that describes the outcome of an execution; for example, a graph or a table of results.
Warnings: information that indicates exceptional situations that are not fatal.
The NSPIO framework classes are used to provide a standard interface for this data in all supported programming environments. These frameworks ensure that information, such as that from a Python application that uses CATALYST Python API, is handled consistently.
The NSPIO framework classes share a common basic interface that provides common methods for dynamically registering and removing custom implementations. With each NSPIO framework, you can register interfaces or call a previously registered interface.
3.2.1. Registering custom NSPIO implementations¶
You can register a custom NSPIO implementation by using the addInstance() or registerInstance() function for
the appropriate NSPIO framework class.
3.2.1.1. Function signature¶
When writing a function, ensure that it matches the interface of appropriate function for the framework type.
With the
Counterframework, the function should match the interface ofCounter.update().With the
Debuggerframework, the function should match the interface ofDebugger.debug().With the
Loggerframework, the function should match the interface ofLogger.logEvent().With the
PCIWarningframework, the function should match the interface ofPCIWarning.warn().With the
Reportframework, the function should match the interface ofReport.addInfo().
Notice that most of these functions contain default arguments. The function that you write can either have these arguments in the signature, or it may omit them. Additionally, your function can also take variable arguments using *args as an argument. Passing parameters as keyword arguments, with **kwargs, is not supported.
For example, any of the following functions can be registered as a Counter implementation
1def update(d):
2 '''
3 Counter instance to handle only the progress
4 '''
5 status = '{}% complete'.format(d*100)
6 print(status)
7
8def update_with_msg(d, msg):
9 '''
10 Counter instance to handle progress and message
11 '''
12 status = '{}% complete'.format(d*100)
13 if msg:
14 status += ' - ' + msg
15 print(status)
16
17def update_with_vargargs(*args):
18 '''
19 Counter instance to handle only progress and message as varargs
20 '''
21 status = '{}% complete'.format(args[0]*100)
22 if args[1]:
23 status += ' - ' + args[1]
24 print(status)
3.2.1.2. Registering function objects¶
In addition to being able to register a function as an NSPIO handler, you can also register a function object. In Python, you
can create a function object by writing a class with a __call__() member function. This member function is called
by the NSPIO framework.
The following example shows how to write a function object class and then register an instance as a
Report handler.
1class MyReport:
2 '''
3 Report instance
4 '''
5 def __init__(self, title):
6 self.title = title
7
8 def __call__(self, msg):
9 print('{}:\t{}".format(self.title, msg[:-1]))
10
11 # register MyReport instance
12 instance_name = pci.nspio.Report.registerInstance(MyReport('my_title'))
3.2.1.3. NSPIO implementation naming¶
NSPIO provides you with the ability to register handlers, and then unregister them by name.
The following criteria is used to determine the name of your instance (for registered function myfunc()).
If your instance has a
nameattribute, the name will be myfunc.name.If your instance has a
__module__attribute and a__name__attribute, the name will be myfunc.__module__ + ‘.’ + myfunc.__name__.If your instance has a
__name__attribute, the name will be myfunc.__name__.If your instance’s class has a
__name__attribute, the name will be myfunc.__class__.__name__.In the unlikely event that none of these attributes are defined, then a default name is used for each framework; for example, the default name for Report is ‘pci::nspio::python::Report’.
New in version 2017.
3.2.2. NSPIO framework classes¶
- class pci.nspio.Counter¶
Bases:
instanceThe
Counterinterface allows you to register and use instances to handle an update in progress.The default
Counterimplementation ignores all updates.- static addInstance((object)f) str :¶
Add python function f as implementation for
Counter, if another python function was registered as an implementation, it will be removed. The function must have the same signature as theupdate()static method.
- static hasInstance((string_)name) bool :¶
Return True if a
Counterinstance called name is currently registered.
- class pci.nspio.Debugger¶
Bases:
instance- static addInstance((object)f) str :¶
Add python function f as implementation for
Debugger, if another python function was registered as an implementation, it will be removed. The function must have the same signature as thedebug()static method.
- static debug((string_)message[, (uint_)level=0[, (string_)group='Python'[, (string_)file=''[, (uint_)line=0]]]]) None :¶
Set the debug message message, with debug level level belonging to user defined debug group group. Optionally, you can indicate the file and line of the debug information.
All registered
Debuggerimplementations are called when this function is called.
- static getDebugLevel() int :¶
Get the current debug level.
- static hasInstance((string_)name) bool :¶
Return True if a
Debuggerinstance called name is currently registered.
- static isEmpty() bool :¶
Return True if there are no registered
Debuggerinstances, False otherwise.
- class pci.nspio.Logger¶
Bases:
instanceThe
Loggerinterface allows you to register and use instances to handle audit/logging information.The default
Loggerimplementation ignores all logged events.- static addInstance((object)f) str :¶
Add python function f as implementation for
Logger, if another python function was registered as an implementation, it will be removed. The function must have the same signature as thelogEvent()static method.
- static hasInstance((string_)name) bool :¶
Return True if a
Loggerinstance called name is currently registered.
- static logEvent((string_)message[, (string_)file=''[, (uint_)line=0]]) None :¶
Log the event with message message. Optionally, you can indicate the file and line of the event information.
All registered
Loggerimplementations are called when this function is called.
- static registerInstance((object)f) str :¶
Add python function f as implementation for
Logger. The function must have the same signature as thelogEvent()static method.
- class pci.nspio.PCIWarning¶
Bases:
instanceThe
PCIWarninginterface allows you to register and use instances to handle non-fatal warning messages.The default
PCIWarningimplementation prints all report information to stdout.- static addInstance((object)f) str :¶
Add python function f as implementation for
PCIWarning, if another python function was registered as an implementation, it will be removed. The function must have the same signature as thewarn()static method.
- static clear() None :¶
Remove all registered
PCIWarningimplementations.
- static getInstanceNames() StringVec :¶
Get a list of names of registered
PCIWarninginstances.
- static hasInstance((string_)name) bool :¶
Return True if a
PCIWarninginstance called name is currently registered.
- static isEmpty() bool :¶
Return True if there are no registered
PCIWarninginstances, False otherwise.
- static registerInstance((object)f) str :¶
Add python function f as implementation for
PCIWarning. The function must have the same signature as thewarn()static method.
- static removeInstance((string_)name) None :¶
Remove the
PCIWarninginstance identified by name.
- static warn((string_)message[, (string_)file='.py'[, (uint_)line=0]]) None :¶
Indicate a warning with message message. Optionally, you can indicate the file and line of the event information.
All registered
PCIWarningimplementations are called when this function is called.
- class pci.nspio.Report¶
Bases:
instanceThe
Reportinterface allows you to register and use instances to handle report information.The default
Reportimplementation prints all report information to stdout.- static addInfo((string_)message) None :¶
Add the message message to the
Report.All registered
Reportimplementations are called when this function is called.
- static addInstance((object)f) str :¶
Add python function f as implementation for
Report, if another python function was registered as an implementation, it will be removed. The function must have the same signature as theaddInfo()static method.
- static hasInstance((string_)name) bool :¶
Return True if a
Reportinstance called name is currently registered.
- pci.nspio.enableDefaultCounter([(str)mode='ON']) bool :¶
Change the status of default
Counterimplementation. Set mode to ‘ON’ to turn it on or ‘OFF’ to turn it on.
- pci.nspio.enableDefaultReport([(str)output='term']) bool :¶
Enable the default
Reportimplementation. set output to ‘term’ to send report information to the terminal window(or stdout), or sety it to filename to send report information to that file.
3.2.2.1. Example¶
The following example shows how to write a function and register it as a report handler, and the result from running
fexport() and fimport():
1import os
2from pci.fimport import fimport
3from pci.fexport import fexport
4import pci.nspio
5
6def my_report(msg):
7 print("Python:\t"+msg[:-1])
8
9def _run():
10 pci.nspio.Report.clear()
11 instance_name = pci.nspio.Report.registerInstance(my_report)
12 pci.nspio.Report.addInfo("Running algorithms\n")
13 irvine_pix = os.path.join(pci.getHomePath(), 'demo', 'irvine.pix')
14 fexport(irvine_pix,"irvine.tif",[],[1,2,3],[],[],[],[],"TIF","")
15 fimport("irvine.tif","irv.pix",[],"OFF","PIXEL")
16 pci.nspio.Report.addInfo("\nExiting algorithms\n")
17
18if __name__ == "__main__":
19 _run()
In this example, the my_report() function handles report information from the FEXPORT and
FIMPORT algorithms.
Note that the signature of my_report() matches with that of Report.addInfo().
The output from this script is as follows:
1PCI Pluggable Framework environment successfully loaded.
2Python: Running algorithms
3Python:
4Python: Source File: irvine.tif
5Python: Source File Type: TIF/TIFF 6.0
6Python:
7Creating 512P 512L 3C file: irv.pix
8Python: Creating segment: 1 [ 150: Georeferencing ] 8 Blocks long
9Python:
10Exiting algorithms