-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1563 from rstudio/black-py-files
Format Python files with Black
- Loading branch information
Showing
10 changed files
with
533 additions
and
449 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Format Python files with Black | ||
fc430b4dbf1854b68de44d927add8a41f64beecc | ||
|
||
# whitespace only commits | ||
e1df4e54c0a4d15cbb01a3af7e8466d29ed64305 | ||
02a0d984171dcacd322e7c071edf3d6b1cefb7b1 | ||
95573e7f3e0a11e4d703c4dbc6c29a409ee7b242 | ||
64acdfa98c29c70917ca7f60f05ad4192272e244 | ||
da67e9dff1be9b5c344aa3431eae520b57142bde | ||
1a62650d952460ce047e06354b84d1a3b3e683de | ||
|
||
|
||
## Snippet to inspect/filter commits quickly: | ||
# library(tidyverse) | ||
# df <- as_tibble(git2r::repository(".")) | ||
# df |> filter(grepl("white[- ]?space", summary)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,140 +1,152 @@ | ||
|
||
import sys | ||
import types | ||
import inspect | ||
|
||
|
||
def isstring(s): | ||
# if we use Python 3 | ||
if (sys.version_info[0] >= 3): | ||
if sys.version_info[0] >= 3: | ||
return isinstance(s, str) | ||
# we use Python 2 | ||
return isinstance(s, basestring) | ||
|
||
|
||
def normalize_func(func): | ||
# return None for builtins | ||
if (inspect.isbuiltin(func)): | ||
if inspect.isbuiltin(func): | ||
return None | ||
return func | ||
|
||
|
||
def get_doc(func): | ||
doc = inspect.getdoc(func) | ||
if doc is None: | ||
func = normalize_func(func) | ||
if func is None: | ||
return None | ||
else: | ||
doc = inspect.getdoc(func) | ||
return doc | ||
doc = inspect.getdoc(func) | ||
if doc is None: | ||
func = normalize_func(func) | ||
if func is None: | ||
return None | ||
else: | ||
doc = inspect.getdoc(func) | ||
return doc | ||
|
||
|
||
def get_property_doc(target, prop): | ||
for name, obj in inspect.getmembers(type(target), inspect.isdatadescriptor): | ||
if (isinstance(obj, property) and name == prop): | ||
return inspect.getdoc(obj.fget) | ||
return None | ||
for name, obj in inspect.getmembers(type(target), inspect.isdatadescriptor): | ||
if isinstance(obj, property) and name == prop: | ||
return inspect.getdoc(obj.fget) | ||
return None | ||
|
||
|
||
def get_argspec(func): | ||
try: | ||
if sys.version_info[0] >= 3: | ||
return inspect.getfullargspec(func) | ||
else: | ||
return inspect.getargspec(func) | ||
except TypeError: | ||
return None | ||
try: | ||
if sys.version_info[0] >= 3: | ||
return inspect.getfullargspec(func) | ||
else: | ||
return inspect.getargspec(func) | ||
except TypeError: | ||
return None | ||
|
||
|
||
def get_arguments(func): | ||
func = normalize_func(func) | ||
if func is None: | ||
return None | ||
return None | ||
argspec = get_argspec(func) | ||
if argspec is None: | ||
return None | ||
return None | ||
args = argspec.args | ||
if 'self' in args: | ||
args.remove('self') | ||
if "self" in args: | ||
args.remove("self") | ||
return args | ||
|
||
|
||
def get_r_representation(default): | ||
if callable(default) and hasattr(default, '__name__'): | ||
arg_value = default.__name__ | ||
else: | ||
if default is None: | ||
arg_value = "NULL" | ||
elif type(default) == type(True): | ||
if default == True: | ||
arg_value = "TRUE" | ||
else: | ||
arg_value = "FALSE" | ||
elif isstring(default): | ||
arg_value = "\"%s\"" % default | ||
elif isinstance(default, int): | ||
arg_value = "%rL" % default | ||
elif isinstance(default, float): | ||
arg_value = "%r" % default | ||
elif isinstance(default, list): | ||
arg_value = "c(" | ||
for i, item in enumerate(default): | ||
if i is (len(default) - 1): | ||
arg_value += "%s)" % get_r_representation(item) | ||
else: | ||
arg_value += "%s, " % get_r_representation(item) | ||
elif isinstance(default, (tuple, set)): | ||
arg_value = "list(" | ||
for i, item in enumerate(default): | ||
if i is (len(default) - 1): | ||
arg_value += "%s)" % get_r_representation(item) | ||
else: | ||
arg_value += "%s, " % get_r_representation(item) | ||
elif isinstance(default, dict): | ||
dict_entries = ", ".join([ | ||
("%s = %s" % (k, get_r_representation(v))) | ||
for k, v in default.items() | ||
]) | ||
arg_value = "list(" + dict_entries + ")" | ||
if callable(default) and hasattr(default, "__name__"): | ||
arg_value = default.__name__ | ||
else: | ||
arg_value = "%r" % default | ||
|
||
# if the value starts with "tf." then convert to $ usage | ||
if (arg_value.startswith("tf.")): | ||
arg_value = arg_value.replace(".", "$") | ||
|
||
return(arg_value) | ||
if default is None: | ||
arg_value = "NULL" | ||
elif type(default) == type(True): | ||
if default == True: | ||
arg_value = "TRUE" | ||
else: | ||
arg_value = "FALSE" | ||
elif isstring(default): | ||
arg_value = '"%s"' % default | ||
elif isinstance(default, int): | ||
arg_value = "%rL" % default | ||
elif isinstance(default, float): | ||
arg_value = "%r" % default | ||
elif isinstance(default, list): | ||
arg_value = "c(" | ||
for i, item in enumerate(default): | ||
if i is (len(default) - 1): | ||
arg_value += "%s)" % get_r_representation(item) | ||
else: | ||
arg_value += "%s, " % get_r_representation(item) | ||
elif isinstance(default, (tuple, set)): | ||
arg_value = "list(" | ||
for i, item in enumerate(default): | ||
if i is (len(default) - 1): | ||
arg_value += "%s)" % get_r_representation(item) | ||
else: | ||
arg_value += "%s, " % get_r_representation(item) | ||
elif isinstance(default, dict): | ||
dict_entries = ", ".join( | ||
[ | ||
("%s = %s" % (k, get_r_representation(v))) | ||
for k, v in default.items() | ||
] | ||
) | ||
arg_value = "list(" + dict_entries + ")" | ||
else: | ||
arg_value = "%r" % default | ||
|
||
# if the value starts with "tf." then convert to $ usage | ||
if arg_value.startswith("tf."): | ||
arg_value = arg_value.replace(".", "$") | ||
|
||
return arg_value | ||
|
||
|
||
def generate_signature_for_function(func): | ||
"""Given a function, returns a string representing its args.""" | ||
|
||
func = normalize_func(func) | ||
if func is None: | ||
return None | ||
return None | ||
|
||
args_list = [] | ||
|
||
argspec = get_argspec(func) | ||
if argspec is None: | ||
return None | ||
|
||
first_arg_with_default = ( | ||
len(argspec.args or []) - len(argspec.defaults or [])) | ||
return None | ||
|
||
first_arg_with_default = len(argspec.args or []) - len( | ||
argspec.defaults or [] | ||
) | ||
for arg in argspec.args[:first_arg_with_default]: | ||
if arg == "self": | ||
# Python documentation typically skips `self` when printing method | ||
# signatures. | ||
continue | ||
args_list.append(arg) | ||
if arg == "self": | ||
# Python documentation typically skips `self` when printing method | ||
# signatures. | ||
continue | ||
args_list.append(arg) | ||
|
||
if argspec.varargs == "args" and hasattr(argspec, 'keywords') and argspec.keywords == "kwds": | ||
original_func = func.__closure__[0].cell_contents | ||
return generate_signature_for_function(original_func) | ||
if ( | ||
argspec.varargs == "args" | ||
and hasattr(argspec, "keywords") | ||
and argspec.keywords == "kwds" | ||
): | ||
original_func = func.__closure__[0].cell_contents | ||
return generate_signature_for_function(original_func) | ||
|
||
if argspec.defaults: | ||
for arg, default in zip( | ||
argspec.args[first_arg_with_default:], argspec.defaults): | ||
arg_value = get_r_representation(default) | ||
args_list.append("%s = %s" % (arg, arg_value)) | ||
for arg, default in zip( | ||
argspec.args[first_arg_with_default:], argspec.defaults | ||
): | ||
arg_value = get_r_representation(default) | ||
args_list.append("%s = %s" % (arg, arg_value)) | ||
if argspec.varargs: | ||
args_list.append("...") | ||
if hasattr(argspec, 'keywords') and argspec.keywords: | ||
args_list.append("...") | ||
args_list.append("...") | ||
if hasattr(argspec, "keywords") and argspec.keywords: | ||
args_list.append("...") | ||
return "(" + ", ".join(args_list) + ")" | ||
|
Oops, something went wrong.