Module Task
[hide private]
[frames] | no frames]

Module Task

source code

This module provides the Task class. It extends the MinimalMatch class from the MinimalMatch module with type and range checking on its attributes:

>>> class MyTask(Task):
...     indisk = 0
...     inseq  = 0
...     infile = ''
...     pixavg = 1.0
...     aparms = 10*[0.0]
...     def __init__(self):
...         Task.__init__(self)
...         self._min_dict = {'inseq': 0, 'aparms': 0}
...         self._max_dict = {'inseq': 4, 'aparms': 10}
...         self._strlen_dict = {'infile': 14}
...         self.__dict__['bparms'] = List(self, 'bparms', [None, 1, 2, 3])
...
>>> my_task = MyTask()

It still has the property that attribute names can be abbreviated:

>>> print my_task.ind
0
>>> my_task.ind = 1
>>> print my_task.ind
1

But an exception will be thrown if you try to assign a value that is out of range:

>>> my_task.ins = 5
Traceback (most recent call last):
  ...
ValueError: value '5' is out of range for attribute 'inseq'

Or if you try to assign a value that has the wrong type, such assigning a string to an integer attribute:

>>> my_task.ind = 'now'
Traceback (most recent call last):
  ...
TypeError: value 'now' has invalid type for attribute 'indisk'

Assigning strings to string attributes works fine of course:

>>> my_task.infile = 'short'

As long as there is no limit on the length of a string:

>>> my_task.infile = 'tremendouslylong'
Traceback (most recent call last):
  ...
ValueError: string 'tremendouslylong' is too long for attribute 'infile'

Assigning an integer value to a floating point attribute is perfectly fine of course:

>>> my_task.pixavg = 2
>>> print my_task.pixavg
2.0

The same should happen for lists:

>>> my_task.aparms = 10*[1]
>>> print my_task.aparms
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

For subscripting:

>>> my_task.aparms[0] = 0
>>> print my_task.aparms
[0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

And slice assignment:

>>> my_task.aparms[1:3] = [1, 2]
>>> print my_task.aparms
[0.0, 1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

You're not allowed to change the length of the list through slice assignment though:

>>> my_task.aparms[3:6] = [3, 4, 5, 6]
Traceback (most recent call last):
  ...
TypeError: slice '3:6' changes the array size of attribute 'aparms'

To provide 1-based indexing used by several packages, you can set the element at index zero of an array to 'None'. This prevents setting that element to anything other than 'None'

>>> my_task.bparms[0] = 0
Traceback (most recent call last):
  ...
ValueError: setting element '0' is prohibited
Classes [hide private]
  List
  Task