def _on_bind(self, _input): from ezmlex.util import is_string if _input is None: self._do_extend = self._do_extend_default return _input # If string given as input, we'll walk through it using our own seek # pointer. if is_string(_input): self._tell = 0 self._do_extend = self._do_extend_by_string return _input # If it's something that have 'read()' and 'readline()' methods, we'll # read characters in chunks. try: if callable(_input.read) and callable(_input.readline): self._do_extend = self._do_extend_by_read return _input except AttributeError: pass # Otherwise we'll try to read input's native portions using iterator # interface _input = iter(_input) self._do_extend = self._do_extend_by_iter return _input
def recompile(regex, *args): import re from ezmlex.util import is_string try: regex = regex.pattern except AttributeError: if not is_string(regex): raise TypeError("not a pattern: %r" % regex) return re.compile(regex, *args)
def _do_assign(self, content, **kw): from ezmlex.util import is_string, is_list if content is None: self._content = [] self._fsm_str = self._make_fsm_str([]) return if is_string(content): self._content = content.splitlines() elif not is_list(content): self._content = list(content) else: self._content = content if 'fsm_str' in kw: self._fsm_str = kw['fsm_str'] else: self._fsm_str = self._make_fsm_str(self._content)
def ccat(*reseq): """Concatenate sequence of regular expressions into one. Example """ import re from ezmlex.util import is_string result = r'' for ri in reseq: if is_string(ri): result += ri else: try: # NOTE: If `ri' is not a string, then we expect it is a # compiled regular expression, so it should have `pattern' # attribute and it should be a string (if it's not a string, # the += operator throws TypeError). result += ri.pattern except AttributeError, TypeError: raise TypeError('not a regular expression: %r' % ri)