Example #1
0
def add_dicts(*args):
    """
    Adds two or more dicts together. Common keys will have their values added.

    For example::

        >>> t1 = {'a':1, 'b':2}
        >>> t2 = {'b':1, 'c':3}
        >>> t3 = {'d':4}

        >>> add_dicts(t1, t2, t3)
        {'a': 1, 'c': 3, 'b': 3, 'd': 4}

    """

    counters = [Counter(arg) for arg in args]
    return dict(reduce(operator.add, counters))
Example #2
0
    def test_counter(self):
        from faker.shims import Counter

        result = Counter('abbb') + Counter('bcc')
        self.assertEqual(result, Counter({'b': 4, 'c': 2, 'a': 1}))

        result = Counter('abbbc') - Counter('bccd')
        self.assertEqual(result, Counter({'b': 2, 'a': 1}))

        result = Counter('abbb') | Counter('bcc')
        self.assertEqual(result, Counter({'b': 3, 'c': 2, 'a': 1}))

        result = Counter('abbb') & Counter('bcc')
        self.assertEqual(result, Counter({'b': 1}))

        result = sorted(Counter('abracadabra').most_common(3))
        self.assertEqual(result, [('a', 5), ('b', 2), ('r', 2)])

        result = sorted(Counter('ABCABC').elements())
        self.assertEqual(result, ['A', 'A', 'B', 'B', 'C', 'C'])

        counter = Counter('which')
        counter.update('witch')
        d = Counter('watch')
        counter.update(d)
        self.assertEqual(counter['h'], 4)
Example #3
0
    def test_counter(self):
        from faker.shims import Counter

        result = Counter('abbb') + Counter('bcc')
        self.assertEqual(result, Counter({'b': 4, 'c': 2, 'a': 1}))

        result = Counter('abbbc') - Counter('bccd')
        self.assertEqual(result, Counter({'b': 2, 'a': 1}))

        result = Counter('abbb') | Counter('bcc')
        self.assertEqual(result, Counter({'b': 3, 'c': 2, 'a': 1}))

        result = Counter('abbb') & Counter('bcc')
        self.assertEqual(result, Counter({'b': 1}))

        counter = Counter('which')
        counter.update('witch')
        d = Counter('watch')
        counter.update(d)
        self.assertEqual(counter['h'], 4)