Skip to content

Commit 0dfb17d

Browse files
committed
OverallFunctions.py:
- creation of a function to check if a class contains docstring - update function to evaluate the indentation of lines object to take already existing docstring into account PythonFiles.py: update write_class_docstring function to write docstring only if needed
1 parent a43486e commit 0dfb17d

4 files changed

Lines changed: 40 additions & 9 deletions

File tree

common/OverallFunctions.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def remove_brackets_to_string(string):
9898
string : the string in which we want to remove brackets
9999
100100
"""
101-
return string.replace('(', '').replace(')','')
101+
return string.replace('(', '').replace(')', '')
102102

103103

104104
def get_indentation(string_list):
@@ -111,9 +111,17 @@ def get_indentation(string_list):
111111
112112
"""
113113
indented_lines = [line for line in string_list if
114-
match(r'^ .*[a-zA-Z0-9]*$', line) and string_list.index(line) > 0]
114+
match(r'^ .*[a-zA-Z0-9\"\']*$', line) and string_list.index(line) > 0]
115115
try:
116-
117-
return resplit(r'[a-z0-9]', indented_lines[0], flags=IGNORECASE)[0]
116+
return resplit(r'[a-z0-9\"\']', indented_lines[0], flags=IGNORECASE)[0]
118117
except IndexError:
119118
raise
119+
120+
121+
def check_if_python_class_contains_docstring(class_content):
122+
for line in class_content:
123+
if match('^ .*def', line):
124+
return False
125+
if match('^ .*"""', line) or match("^ .*'''", line):
126+
return True
127+
return False

file/PythonFiles.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from python_objects.Function import Function
44
from shutil import move
55
from common.OverallFunctions import extract_name_in_line, test_regex, add_content_to_string_from_list, \
6-
get_object_name_from_regex
6+
get_object_name_from_regex, check_if_python_class_contains_docstring
77

88

99
class PythonFiles:
@@ -306,9 +306,10 @@ def write_class_docstring(self):
306306
class_name = line.split('class ')[1].split(':')[0]
307307
except:
308308
class_name = line.split('class ')[1].split('(')[0]
309-
self.init_class_docstring(class_name)
310309

311-
file.write(self.build_string_class_with_docstring(class_name))
310+
self.init_class_docstring(class_name)
311+
if not check_if_python_class_contains_docstring(self.__class_dict[class_name].content):
312+
file.write(self.build_string_class_with_docstring(class_name))
312313

313314
if flag and match('^ .*def .*$', line):
314315
method_name = line.split('def ')[1].split('(')[0]

python_objects/Class.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from python_objects.Function import Function
22
from common.OverallFunctions import get_object_name_from_regex, add_content_to_string_from_list, get_indentation
33
from common.Section import Section
4-
from re import compile
4+
from re import compile, match
55

66

77
class Class:
@@ -37,6 +37,7 @@ class Class:
3737
3838
write_docstring : write the class documentation
3939
"""
40+
4041
def __init__(self, class_name):
4142
self.__class_name = class_name
4243
self.__methode_dict = {}
@@ -110,7 +111,6 @@ def get_function_content_from_class_content(self, function_name):
110111
self.__methode_dict[function_name].content = []
111112
start_flag = compile('^ *' + 'def ' + function_name + '\(.*: *$')
112113
stop_flag = compile('^' + get_indentation(self.content) + '[a-zA-Z0-9]' + '.*$')
113-
114114
self.__methode_dict[function_name].content = add_content_to_string_from_list(self.content,
115115
self.__methode_dict[
116116
function_name].content,

tests/partially_documented_project/SeveralClassFileExample.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ def other_function(param1, param2='foo'):
4343

4444

4545
class Class3:
46+
"""
47+
<TO BE COMPLETED>
48+
49+
Parameters
50+
__________
51+
self : <TO BE COMPLETED>
52+
53+
attr1 : <TO BE COMPLETED>
54+
55+
attr2 : <TO BE COMPLETED>
56+
57+
Methods
58+
_______
59+
__init__ : <TO BE COMPLETED>
60+
61+
method1 : <TO BE COMPLETED>
62+
63+
method2 : <TO BE COMPLETED>
64+
65+
method3 : <TO BE COMPLETED>
66+
"""
67+
4668
def __init__(self, attr1, attr2):
4769
self.param1 = attr1
4870
self.param2 = attr2

0 commit comments

Comments
 (0)