Object to split a string at a given delimiter or at given places. Parameters ---------- delimiter : str, int, or sequence of ints, optional If a string, character used to delimit consecutive fields. If an integer or a sequence of integers, width(s) of each field.
| 131 | |
| 132 | |
| 133 | class LineSplitter: |
| 134 | """ |
| 135 | Object to split a string at a given delimiter or at given places. |
| 136 | |
| 137 | Parameters |
| 138 | ---------- |
| 139 | delimiter : str, int, or sequence of ints, optional |
| 140 | If a string, character used to delimit consecutive fields. |
| 141 | If an integer or a sequence of integers, width(s) of each field. |
| 142 | comments : str, optional |
| 143 | Character used to mark the beginning of a comment. Default is '#'. |
| 144 | autostrip : bool, optional |
| 145 | Whether to strip each individual field. Default is True. |
| 146 | |
| 147 | """ |
| 148 | |
| 149 | def autostrip(self, method): |
| 150 | """ |
| 151 | Wrapper to strip each member of the output of `method`. |
| 152 | |
| 153 | Parameters |
| 154 | ---------- |
| 155 | method : function |
| 156 | Function that takes a single argument and returns a sequence of |
| 157 | strings. |
| 158 | |
| 159 | Returns |
| 160 | ------- |
| 161 | wrapped : function |
| 162 | The result of wrapping `method`. `wrapped` takes a single input |
| 163 | argument and returns a list of strings that are stripped of |
| 164 | white-space. |
| 165 | |
| 166 | """ |
| 167 | return lambda input: [_.strip() for _ in method(input)] |
| 168 | |
| 169 | def __init__(self, delimiter=None, comments='#', autostrip=True, |
| 170 | encoding=None): |
| 171 | delimiter = _decode_line(delimiter) |
| 172 | comments = _decode_line(comments) |
| 173 | |
| 174 | self.comments = comments |
| 175 | |
| 176 | # Delimiter is a character |
| 177 | if (delimiter is None) or isinstance(delimiter, str): |
| 178 | delimiter = delimiter or None |
| 179 | _handyman = self._delimited_splitter |
| 180 | # Delimiter is a list of field widths |
| 181 | elif hasattr(delimiter, '__iter__'): |
| 182 | _handyman = self._variablewidth_splitter |
| 183 | idx = np.cumsum([0] + list(delimiter)) |
| 184 | delimiter = [slice(i, j) for (i, j) in itertools.pairwise(idx)] |
| 185 | # Delimiter is a single integer |
| 186 | elif int(delimiter): |
| 187 | (_handyman, delimiter) = ( |
| 188 | self._fixedwidth_splitter, int(delimiter)) |
| 189 | else: |
| 190 | (_handyman, delimiter) = (self._delimited_splitter, None) |
no outgoing calls
searching dependent graphs…