Skip to content

Instantly share code, notes, and snippets.

@junmakii
Created August 12, 2017 21:41
Show Gist options
  • Select an option

  • Save junmakii/4d5f50f2fcdeda5182b49729d036f139 to your computer and use it in GitHub Desktop.

Select an option

Save junmakii/4d5f50f2fcdeda5182b49729d036f139 to your computer and use it in GitHub Desktop.
"""A module to get the argument names.
Example::
write('markdown_to_video2') # create a arguments.json file.
"""
import os
import importlib
import inspect
import types
import json
def get_function_arguments(path):
"""
:rtype: dict
"""
arguments = {}
files = [
os.path.join(root, file)
for root, dirs, files in os.walk(path)
for file in files
if file.endswith('.py')
]
for path in files:
module_path = path.replace('/', '.')
module_path, ext = os.path.splitext(module_path)
module = importlib.import_module(module_path)
for fn_name, v in vars(module).items():
if isinstance(v, types.FunctionType):
argspec = inspect.getargspec(v)
argspec_defaults = (
argspec.defaults
if argspec.defaults else [])
fn_args = [(arg, None) for arg in argspec.args[
:len(argspec.args) - len(argspec_defaults)
]]
fn_defaults = zip(
argspec.args[-len(argspec_defaults):],
argspec_defaults
)
fn_all_args = fn_args + fn_defaults
for k, v in fn_all_args:
arguments[k] = (
(arguments[k] if k in arguments else [])
+ [{"function": fn_name, "path": path}])
return arguments
def write(path, output=u'arguments.json', encoding='utf-8'):
"""
:rtype: None
"""
open(output, 'w').write(json.dumps(
get_function_arguments(path),
indent=4).encode(encoding))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment