This seeks through the stream until a pattern is matched. The pattern is overloaded and may take several types. The pattern can be a StringType, EOF, a compiled re, or a list of any of those types. Strings will be compiled to re types. This returns the index into the
(self, pattern, timeout=-1, searchwindowsize=-1, async_=False, **kw)
| 252 | return compiled_pattern_list |
| 253 | |
| 254 | def expect(self, pattern, timeout=-1, searchwindowsize=-1, async_=False, **kw): |
| 255 | '''This seeks through the stream until a pattern is matched. The |
| 256 | pattern is overloaded and may take several types. The pattern can be a |
| 257 | StringType, EOF, a compiled re, or a list of any of those types. |
| 258 | Strings will be compiled to re types. This returns the index into the |
| 259 | pattern list. If the pattern was not a list this returns index 0 on a |
| 260 | successful match. This may raise exceptions for EOF or TIMEOUT. To |
| 261 | avoid the EOF or TIMEOUT exceptions add EOF or TIMEOUT to the pattern |
| 262 | list. That will cause expect to match an EOF or TIMEOUT condition |
| 263 | instead of raising an exception. |
| 264 | |
| 265 | If you pass a list of patterns and more than one matches, the first |
| 266 | match in the stream is chosen. If more than one pattern matches at that |
| 267 | point, the leftmost in the pattern list is chosen. For example:: |
| 268 | |
| 269 | # the input is 'foobar' |
| 270 | index = p.expect(['bar', 'foo', 'foobar']) |
| 271 | # returns 1('foo') even though 'foobar' is a "better" match |
| 272 | |
| 273 | Please note, however, that buffering can affect this behavior, since |
| 274 | input arrives in unpredictable chunks. For example:: |
| 275 | |
| 276 | # the input is 'foobar' |
| 277 | index = p.expect(['foobar', 'foo']) |
| 278 | # returns 0('foobar') if all input is available at once, |
| 279 | # but returns 1('foo') if parts of the final 'bar' arrive late |
| 280 | |
| 281 | When a match is found for the given pattern, the class instance |
| 282 | attribute *match* becomes an re.MatchObject result. Should an EOF |
| 283 | or TIMEOUT pattern match, then the match attribute will be an instance |
| 284 | of that exception class. The pairing before and after class |
| 285 | instance attributes are views of the data preceding and following |
| 286 | the matching pattern. On general exception, class attribute |
| 287 | *before* is all data received up to the exception, while *match* and |
| 288 | *after* attributes are value None. |
| 289 | |
| 290 | When the keyword argument timeout is -1 (default), then TIMEOUT will |
| 291 | raise after the default value specified by the class timeout |
| 292 | attribute. When None, TIMEOUT will not be raised and may block |
| 293 | indefinitely until match. |
| 294 | |
| 295 | When the keyword argument searchwindowsize is -1 (default), then the |
| 296 | value specified by the class maxread attribute is used. |
| 297 | |
| 298 | A list entry may be EOF or TIMEOUT instead of a string. This will |
| 299 | catch these exceptions and return the index of the list entry instead |
| 300 | of raising the exception. The attribute 'after' will be set to the |
| 301 | exception type. The attribute 'match' will be None. This allows you to |
| 302 | write code like this:: |
| 303 | |
| 304 | index = p.expect(['good', 'bad', pexpect.EOF, pexpect.TIMEOUT]) |
| 305 | if index == 0: |
| 306 | do_something() |
| 307 | elif index == 1: |
| 308 | do_something_else() |
| 309 | elif index == 2: |
| 310 | do_some_other_thing() |
| 311 | elif index == 3: |