Count Comprehensions

Count Comprehensions

You have to write a function that receives a Python source code, counts list/set/dict/generator comprehensions and returns these counts.
List comprehensions, set comprehensions, dict comprehensions, generator expressions are respectively represented by "ListComp", "SetComp", "DictComp", "GeneratorExp" in the result dictionary.

Input: A string.
Output: A dict with strings as keys and integers as values.


Examples:

count_comprehensions('[n ** 2 for n in range(5)]') == {'ListComp': 1}
count_comprehensions('list(n ** 2 for n in range(5))') == {'GeneratorExp': 1}
count_comprehensions('''\
limit = 100
assert all(
    sum(n ** 3 for n in range(N)) == (N * (N - 1) // 2) ** 2
    for N in range(1, limit)
)
''') == {'GeneratorExp': 2}

Note: Python codes are valid (no syntax error) but do not necessarily comply to PEP8 rules or any implicit rule you may think of.

49