So I put together a program I'm calling Python Trainer and I'm trying to keep it cross-compatible with older and new versions of Python to optimize its utility.
I think I need to factor out the filtering into functions. My main question is am I missing things with my filtering? Or is there a better or more canonical way to test for the things I'm trying to filter in?
from __future__ import print_function
import sys
import keyword
import string
import pkgutil
import pydoc_data.topics
import collections # Container or Sized
import numbers # Number
import inspect # getdoc
if not isinstance(__builtins__, dict):
__builtins__ = vars(__builtins__)
DATA = {}
DATA['functions'] = dict((k, inspect.getdoc(v)) for k, v in __builtins__.items()
if k[0].islower()
and k not in ('copyright', 'credits'))
DATA['datatypes'] = dict(('{0}.{1}'.format(k, attr), inspect.getdoc(method))
for k, v in __builtins__.items()
if isinstance(v, type) and
issubclass(v, (collections.Sized, numbers.Number))
for attr, method in vars(v).items()
if attr[0].islower())
DATA['keywords'] = dict((k, pydoc_data.topics.topics[k]) for k in set(keyword.kwlist) & set(pydoc_data.topics.topics))
DATA['exceptions'] = dict((k,v) for k,v in __builtins__.items()
if k[0].isupper()
and type(v) == type
and issubclass(v, BaseException)
)
DATA['modules'] = [i[1] for i in pkgutil.iter_modules()
if i[1][0] != '_']
if not isinstance(__builtins__, dict): __builtins__ = vars(__builtins__)
- please ensure that the code you post can be tested without editing wherever possible. – jonrsharpe May 5 at 16:47