Search code examples
pythondocumentation-generationinspectpydoc

How to look up functions of a library in python?


I just installed this library that scrapes twitter data: https://github.com/kennethreitz/twitter-scraper

I wanted to find out the library's functions and methods so I can start interacting with the library. I have looked around StackOverflow on this topic and tried the following:

  • pydoc twitter_scraper

  • help(twitter_scraper)

  • dir(twitter_scraper)

  • imported inspect and ran functions = inspect.getmembers(module, inspect.isfunction)

Of the four things I have tried, I have only gotten an output from the inspect option so far. I am also unsure (excluding inspect) whether these codes should go in the terminal or a scratch file.

Still quite new at this. Thank you so much for reading everybody!


Solution

  • Excellent question! There are a few options in trying to grok (fully understand) a new library. In your specific case, twitter-scraper, the only function is get-tweets() and the entire library is under 80 lines long.

    For the general case, in decreasing order of usefulness.

    1. Carefully read the project's description on GitHub. The ReadMe is usually the most carefully written piece of documentation.
    2. Larger libraries have formatted documentation at http://(package-name).readthedocs.org.
    3. pydoc module_name works when the module is installed. ``help(module_name)works in an interactive Python session after you have done animport module_name. These both work from the "docstrings" or strategically placed comments in the source code. This is also whatmodule_name?` does in iPython.
    4. dir(module_name) also requires an import. It lists all the entrypoints to the module, including lots of odd "dunder", or double underscore, you would not normally call or change.
    5. Read the source code. Often, this is easier and more complete than the documentation. If you can bring up the code in an IDE, then jumping around works quickly.

    Also, you asked about what can be used within a script:

    import os
    
    print("Welcome, human.")
    print("dir() is a function, returning a list.")
    print("This has no output")
    a_list = dir(os)
    print("but this does", dir(os))
    print("The help() command uses pydoc to print to stdout")
    help(os)
    print("This program is gratified to be of use.")