See content of modules - not just function names - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: See content of modules - not just function names (/thread-8550.html) |
See content of modules - not just function names - glidecode - Feb-25-2018 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? RE: See content of modules - not just function names - Larz60+ - Feb-25-2018 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: 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:
RE: See content of modules - not just function names - snippsat - Feb-25-2018 (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 .Cmder with ptpython: pdir2 give a nice overview of whole package: RE: See content of modules - not just function names - Gribouillis - Feb-25-2018 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 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) RE: See content of modules - not just function names - glidecode - Feb-25-2018 Thanks for the interesting answers! most of it is new to me, so will take my time to look through it. RE: See content of modules - not just function names - nilamo - Apr-12-2018 (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 |