Python string.whitespace() Examples

The following are 30 code examples of string.whitespace(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module string , or try the search function .
Example #1
Source File: utilities.py    From hyper-h2 with MIT License 6 votes vote down vote up
def _reject_surrounding_whitespace(headers, hdr_validation_flags):
    """
    Raises a ProtocolError if any header name or value is surrounded by
    whitespace characters.
    """
    # For compatibility with RFC 7230 header fields, we need to allow the field
    # value to be an empty string. This is ludicrous, but technically allowed.
    # The field name may not be empty, though, so we can safely assume that it
    # must have at least one character in it and throw exceptions if it
    # doesn't.
    for header in headers:
        if header[0][0] in _WHITESPACE or header[0][-1] in _WHITESPACE:
            raise ProtocolError(
                "Received header name surrounded by whitespace %r" % header[0])
        if header[1] and ((header[1][0] in _WHITESPACE) or
           (header[1][-1] in _WHITESPACE)):
            raise ProtocolError(
                "Received header value surrounded by whitespace %r" % header[1]
            )
        yield header 
Example #2
Source File: pyparsing.py    From kobo-predict with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def originalTextFor(expr, asString=True):
    """Helper to return the original, untokenized text for a given expression.  Useful to
       restore the parsed fields of an HTML start tag into the raw tag text itself, or to
       revert separate tokens with intervening whitespace back to the original matching
       input text. By default, returns astring containing the original parsed text.  
       
       If the optional C{asString} argument is passed as C{False}, then the return value is a 
       C{L{ParseResults}} containing any results names that were originally matched, and a 
       single token containing the original matched text from the input string.  So if 
       the expression passed to C{L{originalTextFor}} contains expressions with defined
       results names, you must set C{asString} to C{False} if you want to preserve those
       results name values."""
    locMarker = Empty().setParseAction(lambda s,loc,t: loc)
    endlocMarker = locMarker.copy()
    endlocMarker.callPreparse = False
    matchExpr = locMarker("_original_start") + expr + endlocMarker("_original_end")
    if asString:
        extractText = lambda s,l,t: s[t._original_start:t._original_end]
    else:
        def extractText(s,l,t):
            t[:] = [s[t.pop('_original_start'):t.pop('_original_end')]]
    matchExpr.setParseAction(extractText)
    return matchExpr 
Example #3
Source File: IDLEenvironment.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def _NextTok(str, pos):
	# Returns (token, endPos)
	end = len(str)
	if pos>=end: return None, 0
	while pos < end and str[pos] in string.whitespace:
		pos = pos + 1
	# Special case for +-
	if str[pos] in '+-':
		return str[pos],pos+1
	# Digits also a special case.
	endPos = pos
	while endPos < end and str[endPos] in string.digits+".":
		endPos = endPos + 1
	if pos!=endPos: return str[pos:endPos], endPos
	endPos = pos
	while endPos < end and str[endPos] not in string.whitespace + string.digits + "+-":
		endPos = endPos + 1
	if pos!=endPos: return str[pos:endPos], endPos
	return None, 0 
Example #4
Source File: pyparsing.py    From phpsploit with GNU General Public License v3.0 5 votes vote down vote up
def setDefaultWhitespaceChars( chars ):
        """Overrides the default whitespace chars
        """
        ParserElement.DEFAULT_WHITE_CHARS = chars 
Example #5
Source File: textwrap.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def wrap(text, width=70, **kwargs):
    """Wrap a single paragraph of text, returning a list of wrapped lines.

    Reformat the single paragraph in 'text' so it fits in lines of no
    more than 'width' columns, and return a list of wrapped lines.  By
    default, tabs in 'text' are expanded with string.expandtabs(), and
    all other whitespace characters (including newline) are converted to
    space.  See TextWrapper class for available keyword args to customize
    wrapping behaviour.
    """
    w = TextWrapper(width=width, **kwargs)
    return w.wrap(text) 
Example #6
Source File: json.py    From talklog with MIT License 5 votes vote down vote up
def _eatWhitespace(self):
        p = self._peek()
        while p is not None and p in string.whitespace or p == '/':
            if p == '/':
                self._readComment()
            else:
                self._next()
            p = self._peek() 
Example #7
Source File: pyparsing.py    From phpsploit with GNU General Public License v3.0 5 votes vote down vote up
def parseImpl( self, instring, loc, doActions=True ):
        if loc != 0:
            # see if entire string up to here is just whitespace and ignoreables
            if loc != self.preParse( instring, 0 ):
                raise ParseException(instring, loc, self.errmsg, self)
        return loc, [] 
Example #8
Source File: pyparsing.py    From phpsploit with GNU General Public License v3.0 5 votes vote down vote up
def parseImpl( self, instring, loc, doActions=True ):
        result = instring[loc] == self.firstQuoteChar and self.re.match(instring,loc) or None
        if not result:
            raise ParseException(instring, loc, self.errmsg, self)

        loc = result.end()
        ret = result.group()

        if self.unquoteResults:

            # strip off quotes
            ret = ret[self.quoteCharLen:-self.endQuoteCharLen]

            if isinstance(ret,basestring):
                # replace escaped whitespace
                if '\\' in ret and self.convertWhitespaceEscapes:
                    ws_map = {
                        r'\t' : '\t',
                        r'\n' : '\n',
                        r'\f' : '\f',
                        r'\r' : '\r',
                    }
                    for wslit,wschar in ws_map.items():
                        ret = ret.replace(wslit, wschar)

                # replace escaped characters
                if self.escChar:
                    ret = re.sub(self.escCharReplacePattern,"\g<1>",ret)

                # replace escaped quotes
                if self.escQuote:
                    ret = ret.replace(self.escQuote, self.endQuoteChar)

        return loc, ret 
Example #9
Source File: pyparsing.py    From phpsploit with GNU General Public License v3.0 5 votes vote down vote up
def setWhitespaceChars( self, chars ):
        """Overrides the default whitespace chars
        """
        self.skipWhitespace = True
        self.whiteChars = chars
        self.copyDefaultWhiteChars = False
        return self 
Example #10
Source File: pyparsing.py    From phpsploit with GNU General Public License v3.0 5 votes vote down vote up
def leaveWhitespace( self ):
        """Disables the skipping of whitespace before matching the characters in the
           C{ParserElement}'s defined pattern.  This is normally only used internally by
           the pyparsing module, but may be needed in some whitespace-sensitive grammars.
        """
        self.skipWhitespace = False
        return self 
Example #11
Source File: textwrap.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def _munge_whitespace(self, text):
        """_munge_whitespace(text : string) -> string

        Munge whitespace in text: expand tabs and convert all other
        whitespace characters to spaces.  Eg. " foo\\tbar\\n\\nbaz"
        becomes " foo    bar  baz".
        """
        if self.expand_tabs:
            text = text.expandtabs()
        if self.replace_whitespace:
            if isinstance(text, str):
                text = text.translate(self.whitespace_trans)
            elif isinstance(text, _unicode):
                text = text.translate(self.unicode_whitespace_trans)
        return text 
Example #12
Source File: json.py    From talklog with MIT License 5 votes vote down vote up
def _eatWhitespace(self):
        p = self._peek()
        while p is not None and p in string.whitespace or p == '/':
            if p == '/':
                self._readComment()
            else:
                self._next()
            p = self._peek() 
Example #13
Source File: json.py    From talklog with MIT License 5 votes vote down vote up
def _eatWhitespace(self):
        p = self._peek()
        while p is not None and p in string.whitespace or p == '/':
            if p == '/':
                self._readComment()
            else:
                self._next()
            p = self._peek() 
Example #14
Source File: json.py    From talklog with MIT License 5 votes vote down vote up
def _eatWhitespace(self):
        p = self._peek()
        while p is not None and p in string.whitespace or p == '/':
            if p == '/':
                self._readComment()
            else:
                self._next()
            p = self._peek() 
Example #15
Source File: pyparsing.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def setDefaultWhitespaceChars( chars ):
        """Overrides the default whitespace chars
        """
        ParserElement.DEFAULT_WHITE_CHARS = chars 
Example #16
Source File: pyparsing.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def leaveWhitespace( self ):
        """Disables the skipping of whitespace before matching the characters in the
           C{ParserElement}'s defined pattern.  This is normally only used internally by
           the pyparsing module, but may be needed in some whitespace-sensitive grammars.
        """
        self.skipWhitespace = False
        return self 
Example #17
Source File: pyparsing.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def setWhitespaceChars( self, chars ):
        """Overrides the default whitespace chars
        """
        self.skipWhitespace = True
        self.whiteChars = chars
        self.copyDefaultWhiteChars = False
        return self 
Example #18
Source File: textwrap.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
        """_handle_long_word(chunks : [string],
                             cur_line : [string],
                             cur_len : int, width : int)

        Handle a chunk of text (most likely a word, not whitespace) that
        is too long to fit in any line.
        """
        # Figure out when indent is larger than the specified width, and make
        # sure at least one character is stripped off on every pass
        if width < 1:
            space_left = 1
        else:
            space_left = width - cur_len

        # If we're allowed to break long words, then do so: put as much
        # of the next chunk onto the current line as will fit.
        if self.break_long_words:
            cur_line.append(reversed_chunks[-1][:space_left])
            reversed_chunks[-1] = reversed_chunks[-1][space_left:]

        # Otherwise, we have to preserve the long word intact.  Only add
        # it to the current line if there's nothing already there --
        # that minimizes how much we violate the width constraint.
        elif not cur_line:
            cur_line.append(reversed_chunks.pop())

        # If we're not allowed to break long words, and there's already
        # text on the current line, do nothing.  Next time through the
        # main loop of _wrap_chunks(), we'll wind up here again, but
        # cur_len will be zero, so the next line will be entirely
        # devoted to the long word that we can't handle right now. 
Example #19
Source File: check_whitespace.py    From D-VAE with MIT License 5 votes vote down vote up
def parse_stdout_filelist(hg_out_filelist):
    files = hg_out_filelist.split()
    files = [f.strip(string.whitespace + "'") for f in files]
    files = list(filter(operator.truth, files))  # get rid of empty entries
    return files 
Example #20
Source File: test_string.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_attrs(self):
        string.whitespace
        string.lowercase
        string.uppercase
        string.letters
        string.digits
        string.hexdigits
        string.octdigits
        string.punctuation
        string.printable 
Example #21
Source File: cmdline.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def ParseArgs( str ):
	import string
	ret=[]
	pos = 0
	length=len(str)
	while pos<length:
		try:
			while str[pos] in string.whitespace: pos = pos+1
		except IndexError:
			break 	
		if pos>=length:
			break
		if str[pos]=='"':
			pos=pos+1
			try:
				endPos = str.index('"', pos)-1
				nextPos = endPos+2
			except ValueError:
				endPos=length
				nextPos=endPos+1
		else:
			endPos = pos
			while endPos<length and not str[endPos] in string.whitespace: endPos = endPos+1
			nextPos=endPos+1
		ret.append(str[pos:endPos+1].strip())
		pos = nextPos
	return ret 
Example #22
Source File: PyParse.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def get_continuation_type(self):
        self._study1()
        return self.continuation

    # study1 was sufficient to determine the continuation status,
    # but doing more requires looking at every character.  study2
    # does this for the last interesting statement in the block.
    # Creates:
    #     self.stmt_start, stmt_end
    #         slice indices of last interesting stmt
    #     self.lastch
    #         last non-whitespace character before optional trailing
    #         comment
    #     self.lastopenbracketpos
    #         if continuation is C_BRACKET, index of last open bracket 
Example #23
Source File: document.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def HookViewNotifications(self, view):
		parent = view.GetParentFrame()
		parent.HookNotify(ViewNotifyDelegate(self, "OnBraceMatch"), scintillacon.SCN_CHECKBRACE)
		parent.HookNotify(ViewNotifyDelegate(self, "OnMarginClick"), scintillacon.SCN_MARGINCLICK)
		parent.HookNotify(ViewNotifyDelegate(self, "OnNeedShown"), scintillacon.SCN_NEEDSHOWN)

		parent.HookNotify(DocumentNotifyDelegate(self, "OnSavePointReached"), scintillacon.SCN_SAVEPOINTREACHED)
		parent.HookNotify(DocumentNotifyDelegate(self, "OnSavePointLeft"), scintillacon.SCN_SAVEPOINTLEFT)
		parent.HookNotify(DocumentNotifyDelegate(self, "OnModifyAttemptRO"), scintillacon.SCN_MODIFYATTEMPTRO)
		# Tell scintilla what characters should abort auto-complete.
		view.SCIAutoCStops(string.whitespace+"()[]:;+-/*=\\?'!#@$%^&,<>\"'|" )

		if view != self.GetFirstView():
			view.SCISetDocPointer(self.GetFirstView().SCIGetDocPointer()) 
Example #24
Source File: version.py    From nn_dataflow with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_version(with_local=False):
    ''' Get the version number, optionally with the local version number. '''

    version = __version__

    if with_local:
        cwd = os.path.dirname(os.path.abspath(__file__))

        with open(os.devnull, 'w') as devnull:
            result = subprocess.call(['git', 'rev-parse'], cwd=cwd,
                                     stderr=subprocess.STDOUT,
                                     stdout=devnull)
        if result != 0:
            # Not in git repo.
            return version  # pragma: no cover

        # Dirty summary.
        short_stat = _command_output(['git', 'diff', 'HEAD', '--shortstat'],
                                     cwd).decode() \
                .replace('files changed', 'fc').replace('file changed', 'fc') \
                .replace('insertions(+)', 'a').replace(' insertion(+)', 'a') \
                .replace('deletions(-)', 'd').replace(' deletion(-)', 'd') \
                .replace(',', '').replace(' ', '')
        diff_hash = hashlib.md5(_command_output(['git', 'diff', 'HEAD'], cwd)) \
                .hexdigest()[:8]
        dirty = '' if not short_stat else '-' + short_stat + '-' + diff_hash

        # Git describe.
        desc = _command_output(['git', 'describe', '--tags', '--always',
                                '--dirty={}'.format(dirty)],
                               cwd).decode()
        version += '+' + desc

    assert not any(w in version for w in string.whitespace)
    return version 
Example #25
Source File: web_cloner.py    From king-phisher with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def patch_html(self, data, encoding='utf-8'):
		"""
		Patch the HTML data to include the King Phisher javascript resource.
		The script tag is inserted just before the closing head tag. If no head
		tag is present, the data is left unmodified.

		:param str data: The HTML data to patch.
		:return: The patched HTML data.
		:rtype: str
		"""
		try:
			codec = codecs.lookup(encoding)
		except LookupError as error:
			self.logger.warning('failed to decode data from web response, ' + error.args[0])
			return data

		try:
			data = codec.decode(data)[0]
		except Exception as error:
			self.logger.error("failed to decode data from web response ({0}) using encoding {1}".format(error.__class__.__name__, encoding))
			return data

		match = re.search(r'</head>', data, flags=re.IGNORECASE)
		if not match:
			return codec.encode(data)[0]
		end_head = match.start(0)
		patched = ''
		patched += data[:end_head]
		patched += '<script src="/kp.js" type="text/javascript"></script>'
		ws_cursor = end_head - 1
		while ws_cursor > 0 and data[ws_cursor] in string.whitespace:
			ws_cursor -= 1
		patched += data[ws_cursor + 1:end_head]
		patched += data[end_head:]
		return codec.encode(patched)[0] 
Example #26
Source File: textwrap.py    From meddle with MIT License 5 votes vote down vote up
def fill(text, width=70, **kwargs):
    """Fill a single paragraph of text, returning a new string.

    Reformat the single paragraph in 'text' to fit in lines of no more
    than 'width' columns, and return a new string containing the entire
    wrapped paragraph.  As with wrap(), tabs are expanded and other
    whitespace characters converted to space.  See TextWrapper class for
    available keyword args to customize wrapping behaviour.
    """
    w = TextWrapper(width=width, **kwargs)
    return w.fill(text)


# -- Loosely related functionality ------------------------------------- 
Example #27
Source File: textwrap.py    From meddle with MIT License 5 votes vote down vote up
def wrap(text, width=70, **kwargs):
    """Wrap a single paragraph of text, returning a list of wrapped lines.

    Reformat the single paragraph in 'text' so it fits in lines of no
    more than 'width' columns, and return a list of wrapped lines.  By
    default, tabs in 'text' are expanded with string.expandtabs(), and
    all other whitespace characters (including newline) are converted to
    space.  See TextWrapper class for available keyword args to customize
    wrapping behaviour.
    """
    w = TextWrapper(width=width, **kwargs)
    return w.wrap(text) 
Example #28
Source File: textwrap.py    From meddle with MIT License 5 votes vote down vote up
def _munge_whitespace(self, text):
        """_munge_whitespace(text : string) -> string

        Munge whitespace in text: expand tabs and convert all other
        whitespace characters to spaces.  Eg. " foo\tbar\n\nbaz"
        becomes " foo    bar  baz".
        """
        if self.expand_tabs:
            text = text.expandtabs()
        if self.replace_whitespace:
            if isinstance(text, str):
                text = text.translate(self.whitespace_trans)
            elif isinstance(text, unicode):
                text = text.translate(self.unicode_whitespace_trans)
        return text 
Example #29
Source File: util.py    From meddle with MIT License 5 votes vote down vote up
def _init_regex():
    global _wordchars_re, _squote_re, _dquote_re
    _wordchars_re = re.compile(r'[^\\\'\"%s ]*' % string.whitespace)
    _squote_re = re.compile(r"'(?:[^'\\]|\\.)*'")
    _dquote_re = re.compile(r'"(?:[^"\\]|\\.)*"') 
Example #30
Source File: pescanner.py    From CapTipper with GNU General Public License v3.0 5 votes vote down vote up
def convert_char(char):
    if char in string.ascii_letters or \
       char in string.digits or \
       char in string.punctuation or \
       char in string.whitespace:
        return char
    else:
        return r'\x%02x' % ord(char)