python类iskeyword()的实例源码

prefilter.py 文件源码 项目:leetcode 作者: thomasyimgit 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def is_shadowed(identifier, ip):
    """Is the given identifier defined in one of the namespaces which shadow
    the alias and magic namespaces?  Note that an identifier is different
    than ifun, because it can not contain a '.' character."""
    # This is much safer than calling ofind, which can change state
    return (identifier in ip.user_ns \
            or identifier in ip.user_global_ns \
            or identifier in ip.ns_table['builtin']\
            or iskeyword(identifier))


#-----------------------------------------------------------------------------
# Main Prefilter manager
#-----------------------------------------------------------------------------
PyColorize.py 文件源码 项目:leetcode 作者: thomasyimgit 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def __call__(self, toktype, toktext, start_pos, end_pos, line):
        """ Token handler, with syntax highlighting."""
        (srow,scol) = start_pos
        (erow,ecol) = end_pos
        colors = self.colors
        owrite = self.out.write

        # line separator, so this works across platforms
        linesep = os.linesep

        # calculate new positions
        oldpos = self.pos
        newpos = self.lines[srow] + scol
        self.pos = newpos + len(toktext)

        # send the original whitespace, if needed
        if newpos > oldpos:
            owrite(self.raw[oldpos:newpos])

        # skip indenting tokens
        if toktype in [token.INDENT, token.DEDENT]:
            self.pos = newpos
            return

        # map token type to a color group
        if token.LPAR <= toktype <= token.OP:
            toktype = token.OP
        elif toktype == token.NAME and keyword.iskeyword(toktext):
            toktype = _KEYWORD
        color = colors.get(toktype, colors[_TEXT])

        #print '<%s>' % toktext,    # dbg

        # Triple quoted strings must be handled carefully so that backtracking
        # in pagers works correctly. We need color terminators on _each_ line.
        if linesep in toktext:
            toktext = toktext.replace(linesep, '%s%s%s' %
                                      (colors.normal,linesep,color))

        # send text
        owrite('%s%s%s' % (color,toktext,colors.normal))
__init__.py 文件源码 项目:cattrs 作者: Tinche 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def gen_attr_names():
    """
    Generate names for attributes, 'a'...'z', then 'aa'...'zz'.
    ~702 different attribute names should be enough in practice.
    Some short strings (such as 'as') are keywords, so we skip them.
    """
    lc = string.ascii_lowercase
    for c in lc:
        yield c
    for outer in lc:
        for inner in lc:
            res = outer + inner
            if keyword.iskeyword(res):
                continue
            yield outer + inner
python.py 文件源码 项目:zippy 作者: securesystemslab 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def python(expr, **settings):
    """Return Python interpretation of passed expression
    (can be passed to the exec() function without any modifications)"""

    printer = PythonPrinter(settings)
    exprp = printer.doprint(expr)

    result = ''
    # Returning found symbols and functions
    renamings = {}
    for symbolname in printer.symbols:
        newsymbolname = symbolname
        # Escape symbol names that are reserved python keywords
        if kw.iskeyword(newsymbolname):
            while True:
                newsymbolname += "_"
                if (newsymbolname not in printer.symbols and
                        newsymbolname not in printer.functions):
                    renamings[sympy.Symbol(
                        symbolname)] = sympy.Symbol(newsymbolname)
                    break
        result += newsymbolname + ' = Symbol(\'' + symbolname + '\')\n'

    for functionname in printer.functions:
        newfunctionname = functionname
        # Escape function names that are reserved python keywords
        if kw.iskeyword(newfunctionname):
            while True:
                newfunctionname += "_"
                if (newfunctionname not in printer.symbols and
                        newfunctionname not in printer.functions):
                    renamings[sympy.Function(
                        functionname)] = sympy.Function(newfunctionname)
                    break
        result += newfunctionname + ' = Function(\'' + functionname + '\')\n'

    if not len(renamings) == 0:
        exprp = expr.subs(renamings)
    result += 'e = ' + printer._str(exprp)
    return result
sympy_parser.py 文件源码 项目:zippy 作者: securesystemslab 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def auto_symbol(tokens, local_dict, global_dict):
    """Inserts calls to ``Symbol`` for undefined variables."""
    result = []
    prevTok = (None, None)

    tokens.append((None, None))  # so zip traverses all tokens
    for tok, nextTok in zip(tokens, tokens[1:]):
        tokNum, tokVal = tok
        nextTokNum, nextTokVal = nextTok
        if tokNum == NAME:
            name = tokVal

            if (name in ['True', 'False', 'None']
                or iskeyword(name)
                or name in local_dict
                # Don't convert attribute access
                or (prevTok[0] == OP and prevTok[1] == '.')
                # Don't convert keyword arguments
                or (prevTok[0] == OP and prevTok[1] in ('(', ',')
                    and nextTokNum == OP and nextTokVal == '=')):
                result.append((NAME, name))
                continue
            elif name in global_dict:
                obj = global_dict[name]
                if isinstance(obj, (Basic, type)) or callable(obj):
                    result.append((NAME, name))
                    continue

            result.extend([
                (NAME, 'Symbol'),
                (OP, '('),
                (NAME, repr(str(name))),
                (OP, ')'),
            ])
        else:
            result.append((tokNum, tokVal))

        prevTok = (tokNum, tokVal)

    return result
HyperParser.py 文件源码 项目:zippy 作者: securesystemslab 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def _eat_identifier(self, str, limit, pos):
        i = pos
        while i > limit and str[i-1] in self._id_chars:
            i -= 1
        if i < pos and (str[i] not in self._id_first_chars or \
                        keyword.iskeyword(str[i:pos])):
            i = pos
        return pos - i
ecore.py 文件源码 项目:pyecore 作者: pyecore 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def normalized_name(self):
        name = self.name
        if keyword.iskeyword(name):
            name = '_' + name
        return name
discovery.py 文件源码 项目:REMAP 作者: REMAPApp 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def fix_method_name(name):
  """Fix method names to avoid reserved word conflicts.

  Args:
    name: string, method name.

  Returns:
    The name with a '_' prefixed if the name is a reserved word.
  """
  if keyword.iskeyword(name) or name in RESERVED_WORDS:
    return name + '_'
  else:
    return name
utils.py 文件源码 项目:acspec 作者: retresco 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def is_valid_identifier(name):
    if not name:
        return False
    return re.match(
        "[_A-Za-z][_a-zA-Z0-9]*$", name
    ) is not None and not keyword.iskeyword(name)
service.py 文件源码 项目:krpcScripts 作者: jwvanderbeck 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def _update_param_names(names):
    """ Given a list of parameter names, append underscores to reserved keywords
        without causing parameter names to clash """
    newnames = []
    for name in names:
        if keyword.iskeyword(name):
            name = name+'_'
            while name in names:
                name += '_'
        newnames.append(name)
    return newnames
configuration.py 文件源码 项目:aiohttp-three-template 作者: RobertoPrevato 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __init__(self, mapping):
        self.__data = {}
        for key, value in mapping.items():
            if iskeyword(key):
                key += '_'
            self.__data[key] = value
codegen.py 文件源码 项目:sqlacodegen 作者: agronholm 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def _convert_to_valid_identifier(name):
    assert name, 'Identifier cannot be empty'
    if name[0].isdigit() or iskeyword(name):
        name = '_' + name
    return _re_invalid_identifier.sub('_', name)
codegen.py 文件源码 项目:sqlacodegen 作者: agronholm 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _convert_to_valid_identifier(name):
        assert name, 'Identifier cannot be empty'
        if name[0].isdigit() or iskeyword(name):
            name = '_' + name
        return _re_invalid_identifier.sub('_', name)
utils.py 文件源码 项目:pycrate 作者: ANSSI-FR 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def name_to_defin(n):
    if iskeyword(n):
        # n is a Python keyword
        n += '_'
    return n.replace('-', '_').replace(' ', '_')


#------------------------------------------------------------------------------#
# _String character parsing routine
#------------------------------------------------------------------------------#
generator.py 文件源码 项目:pycrate 作者: ANSSI-FR 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def name_to_defin(n):
    if iskeyword(n):
        # n is a Python keyword
        n += '_'
    return n.replace('-', '_').replace(' ', '_')
sections.py 文件源码 项目:configmanager 作者: jbasko 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def _get_item_or_section(self, key, handle_not_found=True):
        """
        This method must NOT be called from outside the Section class.

        Do not override this method.

        If handle_not_found is set to False, hooks won't be called.
        This is needed when checking key existence -- the whole
        purpose of key existence checking is to avoid errors (and error handling).
        """
        if isinstance(key, six.string_types):
            if self.settings.str_path_separator in key:
                return self._get_item_or_section(key.split(self.settings.str_path_separator))

            if key.endswith('_') and keyword.iskeyword(key[:-1]):
                key = key[:-1]

            if key in self._tree:
                resolution = self._tree[key]
            else:
                if handle_not_found:
                    result = self.dispatch_event(self.hooks.not_found, name=key, section=self)
                    if result is not None:
                        resolution = result
                    else:
                        raise NotFound(key, section=self)
                else:
                    raise NotFound(key, section=self)

        elif isinstance(key, (tuple, list)) and len(key) > 0:
            if len(key) == 1:
                resolution = self._get_item_or_section(key[0], handle_not_found=handle_not_found)
            else:
                resolution = self._get_item_or_section(
                    key[0], handle_not_found=handle_not_found
                )._get_item_or_section(key[1:], handle_not_found=handle_not_found)
        else:
            raise TypeError('Expected either a string or a tuple as key, got {!r}'.format(key))

        return resolution
HyperParser.py 文件源码 项目:oil 作者: oilshell 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def _eat_identifier(self, str, limit, pos):
        i = pos
        while i > limit and str[i-1] in self._id_chars:
            i -= 1
        if (i < pos and (str[i] not in self._id_first_chars or
            keyword.iskeyword(str[i:pos]))):
            i = pos
        return pos - i
HyperParser.py 文件源码 项目:python2-tracer 作者: extremecoders-re 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def _eat_identifier(self, str, limit, pos):
        i = pos
        while i > limit and str[i-1] in self._id_chars:
            i -= 1
        if (i < pos and (str[i] not in self._id_first_chars or
            keyword.iskeyword(str[i:pos]))):
            i = pos
        return pos - i
common.py 文件源码 项目:ml-utils 作者: LinxiFan 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def is_variable_name(name):
    return name.isidentifier() and not iskeyword(name)
recoop.py 文件源码 项目:java8-memory-analysis 作者: deeso 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def add_field(self, name, value):
        pvname = name
        if pvname.find('$') > -1:
            pvname = name.replace('$', '_')
        if keyword.iskeyword(pvname):
            pvname = pvname + '__'
        if name != pvname:
            setattr(self, pvname, value)

        setattr(self, name, value)
        if not name in self.__fields:
            self.__fields.append(name)


问题


面经


文章

微信
公众号

扫码关注公众号