当前位置: 首页>>代码示例>>Python>>正文


Python API.browse_node_lookup方法代码示例

本文整理汇总了Python中amazonproduct.api.API.browse_node_lookup方法的典型用法代码示例。如果您正苦于以下问题:Python API.browse_node_lookup方法的具体用法?Python API.browse_node_lookup怎么用?Python API.browse_node_lookup使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在amazonproduct.api.API的用法示例。


在下文中一共展示了API.browse_node_lookup方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: BrowseNodeExplorer

# 需要导入模块: from amazonproduct.api import API [as 别名]
# 或者: from amazonproduct.api.API import browse_node_lookup [as 别名]
class BrowseNodeExplorer (gtk.Window):
    
    """
    Gtk explorer for Amazon BrowseNodes.
    """
    
    def on_delete(self, widget, event, data=None):
        # closes the window and quits.
        gtk.main_quit()
        return False
    
    def on_tree_click(self, widget, event, data=None):
        # if double click
        if event.type == gtk.gdk._2BUTTON_PRESS:
            
            # get data from highlighted selection
            treeselection = self.treeview.get_selection()
            model, iter = treeselection.get_selected()
            name_of_data = self.treestore.get_value(iter, 0)
            
            # and fetch selected node
            self.fetch_nodes(name_of_data)
            
    def __init__(self, locale='de'):
        
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
        
        self.set_title("BrowseNode Explorer")
        self.set_size_request(400, 200)
        self.connect("delete_event", self.on_delete)
        
        self.locale = locale
        self.api = API(AWS_KEY, SECRET_KEY, self.locale)
        
        # create a TreeStore with one string column to use as the model
        self.treestore = gtk.TreeStore(int, str)
        
        # create the TreeView using treestore
        self.treeview = gtk.TreeView(self.treestore)
        
        # add column id
        renderer = gtk.CellRendererText()
        column = gtk.TreeViewColumn('id', renderer, text=0)
        self.treeview.append_column(column)
        
        # add column name
        renderer = gtk.CellRendererText()
        column = gtk.TreeViewColumn('name', renderer, text=1)
        column.set_sort_column_id(1) # Allow sorting on the column
        self.treeview.append_column(column)
        
        # make it clickable
        self.treeview.add_events(gtk.gdk.BUTTON_PRESS_MASK)
        self.treeview.connect('button_press_event', self.on_tree_click)
        
        scrolled = gtk.ScrolledWindow()
        scrolled.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        scrolled.add(self.treeview)
        
        self.add(scrolled)
        self.show_all()
        
        # populate with root nodes
        # but avoid duplicated node ids
        node_ids = set(NODE_IDS[self.locale].values())
        for name, id in NODE_IDS[self.locale].items():
            if id in node_ids:
                self.treestore.append(None, [id, name])
                node_ids.remove(id)
        
    def _find_row(self, node_id):
        def match_func(row, data):
            # data is a tuple containing column number, key
            column, key = data 
            return row[column] == key
        def search(rows, func, data):
            if not rows: return None
            for row in rows:
                if func(row, data):
                    return row
                result = search(row.iterchildren(), func, data)
                if result: return result
            return None
        return search(self.treestore, match_func, (0, node_id))
        
    def fetch_nodes(self, node_id):
        """
        Fetches a BrowseNode from Amazon.
        """
        # fetch matching row from treestore
        row = self._find_row(node_id)
        
        # fetch Amazon data
        node = self.api.browse_node_lookup(node_id).BrowseNodes.BrowseNode
        id = node.BrowseNodeId.pyval
        name = node.Name.pyval
        is_root = hasattr(node, 'IsCategoryRoot') and node.IsCategoryRoot.pyval == 1
        
        #~ from lxml import etree
        #~ print etree.tostring(node, pretty_print=True)
#.........这里部分代码省略.........
开发者ID:Root-nix,项目名称:python-amazon-product-api,代码行数:103,代码来源:node-browser.py


注:本文中的amazonproduct.api.API.browse_node_lookup方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。