Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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] != '_']
share|improve this question
    
Looking at the repo, that's because you haven't included 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
    
I don't know about clarity, but they mean I can dump the code into a file and actually run it! –  jonrsharpe May 5 at 16:54
    
Any code you want reviewing should be in the question. –  jonrsharpe May 5 at 18:54
    
@jonrsharpe deleting these comments. –  Aaron Hall May 5 at 18:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.