# SPDX-License-Identifier: MIT
# Independently authored against the Python 3.9 mapping contract.
empty = {}
print(empty, len(empty), bool(empty), empty == dict(), 'x' in empty)
counts = {}
for word in ['red', 'blue', 'red', 'green', 'red', 'blue']:
    counts[word] = counts.get(word, 0) + 1
print(counts, len(counts), bool(counts))
print(counts['red'], counts.get('absent', -1), 'green' in counts, 'absent' not in counts)
alias = counts
alias['blue'] = 20
print(counts, counts is alias)
clone = counts.copy()
print(clone == counts, clone is counts)
clone['red'] = 30
print(counts, clone)
ordered = {'third': 3, 'first': 1, 'second': 2, 'first': 10}
print(ordered)
ordered.update({'fourth': 4, 'second': 22})
ordered.update(ordered)
print(ordered)
print(ordered.pop('first'), ordered.pop('absent', -1), ordered)
print(ordered.pop('third'), ordered.pop('fourth'), ordered)
ordered['third'] = 33
print(ordered)
print(ordered.setdefault('second', 100), ordered.setdefault('new', 99), ordered)
print({'a': 1, 'b': 2} == {'b': 2, 'a': 1})
print({'a': 1} != {'a': 2}, {'a': 1} == {'b': 1}, {} == {'a': 1})
values = [1, 2]
wrapped = {'items': values}
shallow = dict(wrapped)
values.append(3)
print(wrapped, shallow, wrapped == shallow, wrapped['items'] is shallow['items'])
for word in ['red', 'missing']:
    try:
        print(counts[word])
    except KeyError as error:
        print(str(error), repr(error))
integers = {10000000000000000000000000: 'large', -2: 'negative'}
print(integers, integers[-2])
try:
    print(integers[42])
except KeyError as error:
    print(str(error), repr(error))
pairs = {('x', 1): 'one', ('y', 2): 'two'}
print(pairs, pairs[('x', 1)], ('y', 2) in pairs)
try:
    pairs.pop(('z', 3))
except KeyError as error:
    print(str(error), repr(error))
print(str(Exception(42)), repr(Exception(42)))
print(str(ValueError(None)), repr(ValueError(None)))
print(str(KeyError(b'\xff')), repr(KeyError(b'\xff')))
print(str(Exception(('x', 1))), repr(Exception(('x', 1))))
print(str(Exception()), repr(Exception()), repr(Exception('')))

def key():
    print('key')
    return 'a'
def value():
    print('value')
    return 7
def receiver():
    print('receiver')
    return counts
print({key(): value()})
receiver()[key()] = value()
print(counts['a'])
print(counts.get(key(), value()))
clone.clear()
print(clone, bool(clone), len(clone), clone == {}, counts)
clone['after'] = 1
print(clone)
