Project

General

Profile

« Previous | Next » 

Revision 1785

util.py: Added all_equal(), all_equal_ignore_none(), have_same_value()

View differences:

lib/util.py
155 155
    sort_by_len(lists)
156 156
    return lists[-1]
157 157

  
158
def all_equal(vals, default=False):
159
    '''@return default if len(vals) < 2'''
160
    if len(vals) < 2: return default
161
    return reduce(and_, map(lambda v: v == vals[0], vals), True)
162

  
163
def all_equal_ignore_none(vals):
164
    return all_equal(filter(lambda v: v != None, vals), default=False)
165

  
158 166
#### Dicts
159 167

  
160 168
def rename_key(dict_, orig, new): dict_[new] = dict_.pop(orig)
......
166 174
        except KeyError: pass
167 175
    return subset
168 176

  
177
def have_same_value(dict_, *keys):
178
    return all_equal_ignore_none([dict_.get(k, None) for k in keys])
179

  
169 180
class DefaultDict(collections.defaultdict):
170 181
    def __init__(self, dict_, default=None):
171 182
        collections.defaultdict.__init__(self, lambda: default, dict_)

Also available in: Unified diff