ClassList#
- class ratapi.ClassList(init_list=None, name_field='name')#
List of instances of a particular class.
This class subclasses collections.UserList to construct a list intended to store ONLY instances of a particular class, given on initialisation. Any attempt to introduce an object of a different type will raise a ValueError. The class must be able to accept attribute values using keyword arguments. In addition, if the class has the attribute given in the ClassList’s “name_field” attribute (the default is “name”), the ClassList will ensure that all objects within the ClassList have unique values for that attribute. It is then possible to use this attribute of an object in the .remove(), .count(), and .index() routines in place of the full object. Due to the requirement of unique values of the
name_field
attribute, the multiplication operators __mul__, __rmul__, and __imul__ have been disabled, since they cannot allow for unique attribute values by definition.We extend the UserList class to enable objects to be added and modified using just the keyword arguments, enable the object
name_field
attribute to be used in place of the full object, and ensure all elements are of the specified type, with uniquename_field
attributes defined.- Parameters:
init_list (Sequence [T] or T, optional) – An instance, or list of instance(s), of the class to be used in this ClassList.
name_field (str, optional) – The field used to define unique objects in the ClassList (default is “name”).