Generates all the possible even-word line breaks in a string of text, each in the form of a (line, remainder) 2-tuple where *line* contains the text before the break and *remainder* the text after as a |_LineSource| object. Its boolean value is |True| when it contains text, |False|
| 213 | |
| 214 | |
| 215 | class _LineSource(object): |
| 216 | """ |
| 217 | Generates all the possible even-word line breaks in a string of text, |
| 218 | each in the form of a (line, remainder) 2-tuple where *line* contains the |
| 219 | text before the break and *remainder* the text after as a |_LineSource| |
| 220 | object. Its boolean value is |True| when it contains text, |False| when |
| 221 | its text is the empty string or whitespace only. |
| 222 | """ |
| 223 | |
| 224 | def __init__(self, text): |
| 225 | self._text = text |
| 226 | |
| 227 | def __bool__(self): |
| 228 | """ |
| 229 | Gives this object boolean behaviors (in Python 3). bool(line_source) |
| 230 | is False if it contains the empty string or whitespace only. |
| 231 | """ |
| 232 | return self._text.strip() != "" |
| 233 | |
| 234 | def __eq__(self, other): |
| 235 | return self._text == other._text |
| 236 | |
| 237 | def __iter__(self): |
| 238 | """ |
| 239 | Generate a (text, remainder) pair for each possible even-word line |
| 240 | break in this line source, where *text* is a str value and remainder |
| 241 | is a |_LineSource| value. |
| 242 | """ |
| 243 | words = self._text.split() |
| 244 | for idx in range(1, len(words) + 1): |
| 245 | line_text = " ".join(words[:idx]) |
| 246 | remainder_text = " ".join(words[idx:]) |
| 247 | remainder = _LineSource(remainder_text) |
| 248 | yield _Line(line_text, remainder) |
| 249 | |
| 250 | def __nonzero__(self): |
| 251 | """ |
| 252 | Gives this object boolean behaviors (in Python 2). bool(line_source) |
| 253 | is False if it contains the empty string or whitespace only. |
| 254 | """ |
| 255 | return self._text.strip() != "" |
| 256 | |
| 257 | def __repr__(self): |
| 258 | return "<_LineSource('%s')>" % self._text |
| 259 | |
| 260 | |
| 261 | class _Line(tuple): |
no outgoing calls
searching dependent graphs…