Search code examples
pythonpython-3.xkeywordcountingidentifier

Counting the occurrence of each keyword and each identifier


I tried to count the keywords and identifiers within the words that the quesiton gave me but I'm not sure if did it right or not so please help me to find out the issues that I made within the question. The question goes like this:

Write a program that reads in a Python source code file and counts the occurrence of each keyword and each identifier (variables, class and method names) in the file using two dictionaries: one for keywords and one for identifiers. Your program should prompt the user to enter the Python source code filename. Keywords in Python programming language: False class finally is return None continue for l ambda try True def from nonlocal while and del global not or yield assert else import pass break except with as in elif raise

Keywords in Python programming language:

False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise

This is the code that I wrote:

import tokenize
import keyword
import collections 
with open('source.py') as f:
    tokens = (token for _, token, _, _, _ in tokenize.generate_tokens(f.readline))
    c = collections.Counter(token for token in tokens if keyword.iskeyword(token))

print(c)

Solution

  • In response to your comment, technically c is not a dictionary, it is a collections.Counter object:

    print(c, type(c))
    

    Output:

    Counter({'False': 1, 'True': 1}) <class 'collections.Counter'>
    

    You can cast it to a built-in dictionary like so:

    c = dict(c)
    print(c, type(c))
    

    Output:

    {'False': 1, 'True': 1} <class 'dict'>
    

    As you can see it got rid of the Counter() object.

    It's not exactly pretty but if all you know is that there should be two dictionaries, the second bit is the most correct.

    However reading your question again it looks like you need to make a dictionary for (1): Keywords (which you have already provided) and (2) Identifiers: An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate one entity from another. Could you provide your python file: source.py?