An extension of `str` that provides the category label as its string value, and additional attributes representing other aspects of the category.
| 143 | |
| 144 | |
| 145 | class Category(str): |
| 146 | """ |
| 147 | An extension of `str` that provides the category label as its string |
| 148 | value, and additional attributes representing other aspects of the |
| 149 | category. |
| 150 | """ |
| 151 | |
| 152 | def __new__(cls, pt, *args): |
| 153 | category_label = "" if pt is None else pt.v.text |
| 154 | return str.__new__(cls, category_label) |
| 155 | |
| 156 | def __init__(self, pt, idx=None): |
| 157 | """ |
| 158 | *idx* is a required attribute of a c:pt element, but must be |
| 159 | specified when pt is None, as when a "placeholder" category is |
| 160 | created to represent a missing c:pt element. |
| 161 | """ |
| 162 | self._element = self._pt = pt |
| 163 | self._idx = idx |
| 164 | |
| 165 | @property |
| 166 | def idx(self): |
| 167 | """ |
| 168 | Return an integer representing the index reference of this category. |
| 169 | For a leaf node, the index identifies the category. For a parent (or |
| 170 | other ancestor) category, the index specifies the first leaf category |
| 171 | that ancestor encloses. |
| 172 | """ |
| 173 | if self._pt is None: |
| 174 | return self._idx |
| 175 | return self._pt.idx |
| 176 | |
| 177 | @property |
| 178 | def label(self): |
| 179 | """ |
| 180 | Return the label of this category as a string. |
| 181 | """ |
| 182 | return str(self) |
| 183 | |
| 184 | |
| 185 | class CategoryLevel(Sequence): |
no outgoing calls
searching dependent graphs…