Python tokenize.Name() Examples

The following are 6 code examples of tokenize.Name(). 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 tokenize , or try the search function .
Example #1
Source File: SimpleGladeApp.py    From gnome-connection-manager with GNU General Public License v3.0 6 votes vote down vote up
def normalize_names(self):
        """
        It is internally used to normalize the name of the widgets.
        It means a widget named foo:vbox-dialog in glade
        is refered self.vbox_dialog in the code.

        It also sets a data "prefixes" with the list of
        prefixes a widget has for each widget.
        """
        for widget in self.get_widgets():
            widget_name = gtk.Widget.get_name(widget)
            prefixes_name_l = widget_name.split(":")
            prefixes = prefixes_name_l[ : -1]
            widget_api_name = prefixes_name_l[-1]
            widget_api_name = "_".join( re.findall(tokenize.Name, widget_api_name) )
            gtk.Widget.set_name(widget, widget_api_name)
            if hasattr(self, widget_api_name):
                raise AttributeError("instance %s already has an attribute %s" % (self,widget_api_name))
            else:
                setattr(self, widget_api_name, widget)
                if prefixes:
                    gtk.Widget.set_data(widget, "prefixes", prefixes) 
Example #2
Source File: file_format_parser.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def _validate_string(self, text):
    """Validates a string is composed of valid characters.

    Args:
      text: any str to validate.

    Raises:
      ValueError: when text contains illegal characters.
    """
    if not re.match(tokenize.Name, text):
      raise ValueError('%s should only contain ascii letters or digits.' %
                       text) 
Example #3
Source File: file_format_parser.py    From locality-sensitive-hashing with MIT License 5 votes vote down vote up
def _validate_string(self, text):
    """Validates a string is composed of valid characters.

    Args:
      text: any str to validate.

    Raises:
      ValueError: when text contains illegal characters.
    """
    if not re.match(tokenize.Name, text):
      raise ValueError('%s should only contain ascii letters or digits.' %
                       text) 
Example #4
Source File: py3compat.py    From spyder-kernels with MIT License 5 votes vote down vote up
def isidentifier(string):
        """Check if string can be a variable name."""
        return re.match(tokenize.Name + r'\Z', string) is not None 
Example #5
Source File: vfp2py_convert_visitor.py    From vfp2py with MIT License 5 votes vote down vote up
def valid_identifier(name):
    return re.match(tokenize.Name + '$', name) and not keyword.iskeyword(name) 
Example #6
Source File: utils.py    From qdb with Apache License 2.0 4 votes vote down vote up
def register_last_expr(tree, register):
    """
    Registers the last expression as register in the context of an AST.
    tree may either be a list of nodes, or an ast node with a body.
    Returns the newly modified structure AND mutates the original.
    """
    if isinstance(tree, list):
        if not tree:
            # Empty body.
            return tree
        # Allows us to use cases like directly passing orelse bodies.
        last_node = tree[-1]
    else:
        last_node = tree.body[-1]
    if type(last_node) in NO_REGISTER_STATEMENTS:
        return tree

    def mk_register_node(final_node):
        return ast.Expr(
            value=ast.Call(
                func=ast.Name(
                    id=register,
                    ctx=ast.Load(),
                ),
                args=[
                    final_node.value,
                ],
                keywords=[],
                starargs=None,
                kwargs=None,
            )
        )

    if hasattr(last_node, 'body'):
        # Deep inspect the body of the nodes.
        register_last_expr(last_node, register)

        # Try to register in all the body types.
        try:
            register_last_expr(last_node.orelse, register)
        except AttributeError:
            pass
        try:
            for handler in last_node.handlers:
                register_last_expr(handler, register)
        except AttributeError:
            pass
        try:
            register_last_expr(last_node.finalbody, register)
        except AttributeError:
            pass
    else:
        # Nodes with no body require no recursive inspection.
        tree.body[-1] = mk_register_node(last_node)

    return ast.fix_missing_locations(tree)