Skip to content

Instantly share code, notes, and snippets.

@weralwolf
Last active August 29, 2015 14:07
Show Gist options
  • Save weralwolf/4512f423dc0bd14d4a67 to your computer and use it in GitHub Desktop.
Save weralwolf/4512f423dc0bd14d4a67 to your computer and use it in GitHub Desktop.
Help to find in big project functions with potential problems because of dict/list default params. Colorize its output respect to bpython coloring way.
import sys
import json
import types
import collections
from os import walk
"""
Help to find in big project functions with potential problems
because of dict/list default params. Colorize its output
respect to bpython coloring way.
"""
problem_collection = []
fail_mods = []
# List of types unacceptable for default params in functions
bad_defaults = [
types.ListType,
types.DictionaryType,
collections.defaultdict,
collections.MutableMapping,
collections.MutableSequence,
collections.MutableSet,
collections.Mapping,
collections.OrderedDict,
collections.Set,
collections.deque
]
# List of methods to ignore
ignores = [
# 'include',
# 'reverse',
# 'staticfiles_urlpatterns',
# 'urls'
]
def inspect_module_class(obj, path):
"""
Inspecting of class or python module
:type obj: mixed - class or module to inspect
:type path: str - string which represents path to this element
"""
dic = obj.__dict__
for element in dir(obj):
if element in ignores or element not in dic:
continue
n_path = path + '.' + element
print "\x01m\x03%s\x04" % n_path
me = dic[element]
if isinstance(me, types.ClassType):
inspect_module_class(me, n_path)
elif isinstance(me, types.FunctionType):
inspect_function(me, n_path)
def inspect_function(obj, path):
"""
Inspeting function trying to find bad default params
:type obj: function - function to inspect
:type path: str - string which represents path to this element
"""
if not obj.func_defaults:
return
for func_default in obj.func_defaults:
for bad_default in bad_defaults:
if isinstance(func_default, bad_default):
print obj.func_defaults
print func_default, bad_default
problem_collection.append(
{
'path': path + ':' + str(obj.func_code.co_firstlineno),
'bad_default': str(func_default),
'bad_type': repr(bad_default),
}
)
break
def walk_around(where, output_filename='bad_defaults.json'):
"""
Walks through dir tree finding and expecting all py-files
:type where: str - path where start from
"""
for (dirpath, dirnames, filenames) in walk(where):
for filename in filenames:
if '.py' == filename[-3:]:
fname = (dirpath + '/' + filename[:-3])[2:]
module_name = '.'.join(fname.split('/'))
try:
__import__(module_name)
print "\x01g\x03%s\x04" % module_name
inspect_module_class(sys.modules[module_name], module_name)
except: # (ImportError, IOError):
fail_mods.append(module_name)
print "\x01r\x03%s\x04" % module_name
f = open(output_filename, 'w')
json.dump({
'problems': list(problem_collection),
'fails': fail_mods,
}, f, indent=4)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment