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


Python AttrDict.values方法代码示例

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


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

示例1: test_iteration_2

# 需要导入模块: from attrdict import AttrDict [as 别名]
# 或者: from attrdict.AttrDict import values [as 别名]
    def test_iteration_2(self):
        """
        Test the iteration methods (items, keys, values[, iteritems,
        iterkeys, itervalues]).
        """
        if not PY2:  # Python2.6 doesn't have skipif/skipunless
            return

        from attrdict import AttrDict

        empty = AttrDict()
        adict = AttrDict({'foo': 'bar', 'lorem': 'ipsum', 'alpha': {
            'beta': 1, 'bravo': empty}})

        self.assertEqual(empty.items(), [])
        self.assertEqual(empty.keys(), [])
        self.assertEqual(empty.values(), [])

        items = adict.items()
        self.assertEqual(len(items), 3)
        self.assertTrue(('foo', 'bar') in items)
        self.assertTrue(('lorem', 'ipsum') in items)
        self.assertTrue(('alpha', {'beta': 1, 'bravo': empty}) in items)

        self.assertEqual(set(adict.keys()), set(['foo', 'lorem', 'alpha']))

        values = adict.values()
        self.assertEqual(len(values), 3)
        self.assertTrue('bar' in values)
        self.assertTrue('ipsum' in values)
        self.assertTrue({'beta': 1, 'bravo': empty} in values)

        # Iterator methods
        iterator = empty.iteritems()
        self.assertFalse(isinstance(iterator, list))
        self.assertEqual(list(iterator), [])

        iterator = empty.iterkeys()
        self.assertFalse(isinstance(iterator, list))
        self.assertEqual(list(iterator), [])

        iterator = empty.itervalues()
        self.assertFalse(isinstance(iterator, list))
        self.assertEqual(list(iterator), [])

        iterator = adict.iteritems()
        self.assertFalse(isinstance(iterator, list))
        self.assertEqual(list(iterator), adict.items())

        iterator = adict.iterkeys()
        self.assertFalse(isinstance(iterator, list))
        self.assertEqual(list(iterator), adict.keys())

        iterator = adict.itervalues()
        self.assertFalse(isinstance(iterator, list))
        self.assertEqual(list(iterator), adict.values())
开发者ID:donsignore,项目名称:AttrDict,代码行数:58,代码来源:tests.py

示例2: Widget

# 需要导入模块: from attrdict import AttrDict [as 别名]
# 或者: from attrdict.AttrDict import values [as 别名]
class Widget(object):
  """ A widget is an object that can generate HTML (in a View instance) and 
  maintain state. 
  
  Each widget has sub widgets, and a dictionary of values.
  Sub widgets are saved in the 'widgets' dictionary.
  Values are saved in the 'valeus' dictionary.
  Values can be changed through an http request. The request will contain the
  id of the widget (in the format: preant1_id-parent2_id-widget_id) along with
  a key and value. This is managed by 'set_value' in report.py. The javascript
  command is in src/static/js/freecell.js/freecell.
  
  How to implement a widget
  -------------------------
  1. Inherit from Widget. 
  2. Add an __init__ like this:
     def __init__(self, id, parent, arg1, arg2, arg3, arg4):
       Widget.__init__(self, id, parent)
  3. Add a view method that returns a View object.
  4. If you want, add a on_load method. 
  
  
  How to change the state of a widget
  -----------------------------------
  Create HTML/JS in a view that will call the set_value
  function defined in static/js/freecell.js. This function 
  requires the widget id, which you can access using the 'id' 
  member. 
  Usually you will want to set state using an ApplyButton. 
  See ApplyButton.py for details.
  
  """
  
  def __init__(self, id, parent):
    """ Inits a new widget.
    The constructor should never be called directly. To add a sub-widgegt use
    _add_widget. To create a root widget see report.py
    """
    self.id = id
    self.parent = parent
    self.unique_counter = 0
    self.widgets = AttrDict()
    self.values = AttrDict()
    self.value_name_to_cache_key = {}
         
  def has_method(self, name):
    """ Tests is the widget has a method with the given name.
    """
    return name in dir(self) and callable(getattr(self, name))

  def set_value(self, name, val):
    self.values[name] = val
    if self.has_method('on_set_value'):
      self.on_set_value(name, val)
    self._save_value_if_needed(name, val)

  def set_default(self, name, val):
    """ Sets a value if no value was defined. """
    if name in self.values and self.values[name] == None:
      self.set_value(name, val)
        
  def _normalize_id(self, id):
    """ Removes illegal characters from the widget id. The id can appear in HTML, 
    as file name, as part of css. """
    return re.sub('[^a-zA-Z_0-9]', '_', id)
    #return id.replace(' ', '').replace('-','').replace('/','_').replace('\\', '_')

  def _get_widget(self, name):
    """ Gets a widget with the specified name. """
    name = self._normalize_id(name)
    if not name in self.widgets:
      raise Exception('name %s not found.' % name)
    return self.widgets[name]
      
  def _add_widget_if_needed(self, name, new_widget_type, *args, **kargs):
    name = self._normalize_id(name)
    if not name in self.widgets:
      return self._add_widget(name, new_widget_type, *args, **kargs)
    return self.widgets[name]
    
  def _add_widget(self, name, new_widget_type, *args, **kargs):
    """ Adds a sub widget. """
    name = self._normalize_id(name)
    if name in self.widgets:
      raise Exception('name %s is already in use' % name)
    new_id = '%s%s%s' % (self.id, ID_SEPERATOR, name)
    new_widget = new_widget_type(new_id, self, *args, **kargs)   
    self.widgets[name] = new_widget
    return new_widget
  
  def _remove_widget(self, widget):
    """ Removes a sub widget. """
    widget_name = widget.id.split(ID_SEPERATOR)[-1]
    del self.widgets[widget_name]
  
  def run_on_load(self):
    """ Called by a Report when a widget is reloaded. """
    for w in self.widgets.values():
      w.run_on_load()
    if 'on_load' in dir(self):
#.........这里部分代码省略.........
开发者ID:ericjsolis,项目名称:danapeerlab,代码行数:103,代码来源:widget.py


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