Python Forum
See content of modules - not just function names
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
See content of modules - not just function names
#1
Hi all

I'd like to see the actual content of various modules. With
dir(modulename)
I can check which functions are included in the module.

However I would like to see how the module is actually coded. For instance I'm curious about some of the functions in the 're' module.

While writing this question I discovered I could find the actual 're.py' file under the python folder. However is there a function to see the content of any module specified from without the python environment?
Reply
#2
You can find out where a module is located,
and built_in docs with something like:

import importlib

def get_module_info(module):
    mod = importlib.import_module(module)
    print('Module location: {}'.format(mod.__file__))
    mdoc = mod.__doc__
    print(mdoc)

get_module_info('tkinter')
results:
Output:
Module location: C:\Python364\lib\tkinter\__init__.py Wrapper functions for Tcl/Tk. Tkinter provides classes which allow the display, positioning and control of widgets. Toplevel widgets are Tk and Toplevel. Other widgets are Frame, Label, Entry, Text, Canvas, Button, Radiobutton, Checkbutton, Scale, Listbox, Scrollbar, OptionMenu, Spinbox LabelFrame and PanedWindow. Properties of the widgets are specified with keyword arguments. Keyword arguments have the same name as the corresponding resource under Tk. Widgets are positioned with one of the geometry managers Place, Pack or Grid. These managers can be called with methods place, pack, grid available in every Widget. Actions are bound to events by resources (e.g. keyword argument command) or with the method bind. Example (Hello, World): import tkinter from tkinter.constants import * tk = tkinter.Tk() frame = tkinter.Frame(tk, relief=RIDGE, borderwidth=2) frame.pack(fill=BOTH,expand=1) label = tkinter.Label(frame, text="Hello, World") label.pack(fill=X, expand=1) button = tkinter.Button(frame,text="Exit",command=tk.destroy) button.pack(side=BOTTOM) tk.mainloop()

but there is no 'dis-interpreter'
there is however dis (must be imported) for cpython byte code,
see: https://docs.python.org/3/library/dis.html
example:
import importlib
import dis

def disassemble_pcode(module):
    mod = importlib.import_module(module)
    print(dis.dis(mod))
    disassemble_pcode('tkinter')
partial results:
Output:
Disassembly of BaseWidget: Disassembly of __init__: 2283 0 LOAD_FAST 4 (kw) 2 POP_JUMP_IF_FALSE 16 2284 4 LOAD_GLOBAL 0 (_cnfmerge) 6 LOAD_FAST 3 (cnf) 8 LOAD_FAST 4 (kw) 10 BUILD_TUPLE 2 12 CALL_FUNCTION 1 14 STORE_FAST 3 (cnf) 2285 >> 16 LOAD_FAST 2 (widgetName) 18 LOAD_FAST 0 (self) 20 STORE_ATTR 1 (widgetName) 2286 22 LOAD_GLOBAL 2 (BaseWidget) 24 LOAD_ATTR 3 (_setup) 26 LOAD_FAST 0 (self) 28 LOAD_FAST 1 (master) 30 LOAD_FAST 3 (cnf) 32 CALL_FUNCTION 3 34 POP_TOP
Reply
#3
(Feb-25-2018, 04:50 PM)glidecode Wrote: While writing this question I discovered I could find the actual 're.py' file under the python folder.
There is a link do source code in documentation Source code: Lib/re.py
Or if wonder where code for timeit is Source code: Lib/timeit.py

(Feb-25-2018, 04:50 PM)glidecode Wrote: However I would like to see how the module is actually coded. For instance I'm curious about some of the functions in the 're' module.
A good REPL(ptpython ,IPython) or a editor will help a lot,i never do dir(modulename) because function/methods comes up automatic.

Here some images of my setup,i use VS Code and ptpython as REPL.
See autocomplete and mouse over re.findall() will show help,and also(Ctrl+mouse) will take you straight to source code of method findall() in re.py.
[Image: TkjVUD.jpg]
Cmder with ptpython:
[Image: mxL7WY.jpg]
pdir2 give a nice overview of whole package:
[Image: fyNDWB.png]
Reply
#4
I once wrote this program named 'code'. It finds the python source of a module and opens it with xdg-open. So I type for example
Output:
code re
in a linux terminal and it launches my default editor with the file 're.py'. I think it could be modified in a straightforward way in windows by replacing xdg-open with a windows command.

#!/usr/bin/env python
# -*-coding: utf8-*-
# Title: sourcecode.py
# Python version: >= 2.7
# Author: Gribouillis
# Created: 2012-01-01 12:10:03.179236 (isoformat date)
# License: Public Domain
# Use this code freely.
"""Script to find and edit the source code of a python module."""

from __future__ import (absolute_import, division,
                        print_function, unicode_literals)
import argparse
import os.path

__version__ = '0.2.5'

def main(args):
    modname = args.modname
    do_find = True
    if '.' in modname:
        packagename, modname = modname.rsplit('.', 1)
        import importlib
        package = importlib.import_module(packagename)
        try:
            a = (package.__path__,)
        except AttributeError:
            # handle special cases such as os.path (os is not a package)
            f = importlib.import_module(args.modname).__file__
            do_find = False
    else:
        a = ()
    if do_find:
        import imp
        file, f, description =  imp.find_module(modname, *a)
    if f.endswith((".pyc", ".pyo")):
        if os.path.isfile(f[:-1]):
            f = f[:-1]
    elif os.path.isdir(f) and os.path.isfile(os.path.join(f, '__init__.py')):
        f = os.path.join(f, '__init__.py')
    if f.endswith(".py"):
        import subprocess
        command = ['/usr/bin/xdg-open', '{}'.format(f)]
        print(command)
        subprocess.call(command)
    else:
        print('Found at {} (no *.py file to edit).'.format(repr(f)))

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description = """Open a python module's source file with default editor.
        
        Only filenames with extension '.py' are opened.""",
        formatter_class =argparse.RawTextHelpFormatter,
    )
    parser.add_argument('modname',
        action = 'store',
        help = 'Module name (may contain dots, e.g xml.dom)',
        metavar = 'MODNAME',
    )
    args = parser.parse_args()
    main(args)
Reply
#5
Thanks for the interesting answers! most of it is new to me, so will take my time to look through it.
Reply
#6
(Feb-25-2018, 04:50 PM)glidecode Wrote: However I would like to see how the module is actually coded. For instance I'm curious about some of the functions in the 're' module.

Python is open source, you can view any part of the language itself, or any of it's modules, on github: https://github.com/python/cpython/tree/master/Lib

re, for example, is here: https://github.com/python/cpython/blob/master/Lib/re.py
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star NameError – function doesn't recognize imported modules Sir 4 3,419 Dec-01-2020, 06:36 AM
Last Post: Sir
  function 'return' and file content into a list shiro26 4 3,243 Jul-06-2018, 10:20 PM
Last Post: volcano63
  Modules issue, pip3 download modules only to pyhton3.5.2 not the latest 3.6.1 bmohanraj91 6 8,353 May-25-2017, 08:15 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020