Write a python function `dedent(str)` that takes as input a string of python code and de-dents it maximally. Lines that are entirely blank don't count.
Write a python function `dedent(str)` that takes as input a string of python code and de-dents it maximally. Lines that are entirely blank don't count.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | import re import sys def get_leading_whitespace(line): """Returns the leading whitespace string of a line.""" match = re.match(r'(\s*)', line) # Should always match at least an empty string return match.group(1) if match else "" def dedent(code_str): """ De-dents a string of Python code maximally. Removes the longest common leading whitespace prefix from all non-blank lines. Lines containing only whitespace are considered blank and do not influence the indentation level, but their original whitespace is preserved in the output relative to the dedented block. Args: code_str: A string containing the code to be dedented. Returns: A string with the common leading whitespace removed. Examples: >>> code = ''' ... def example(): ... print("Hello") ... ... # A comment ... ''' >>> print(dedent(code)) <BLANKLINE> def example(): print("Hello") <BLANKLINE> # A comment <BLANKLINE> >>> code_no_common_indent = ''' ... a = 1 ... b = 2 ... ''' >>> print(dedent(code_no_common_indent)) <BLANKLINE> a = 1 b = 2 <BLANKLINE> >>> code_with_tabs_and_spaces = ''' ... \t def func(): ... \t pass # indented with tab + 2 spaces ... ''' >>> print(dedent(code_with_tabs_and_spaces)) <BLANKLINE> def func(): pass # indented with tab + 2 spaces <BLANKLINE> >>> code_only_blanks = ''' ... ... ... \t ... ''' >>> print(dedent(code_only_blanks)) # Should preserve blank/whitespace-only lines <BLANKLINE> <BLANKLINE> <BLANKLINE> >>> code_empty = "" >>> print(dedent(code_empty)) <BLANKLINE> >>> code_single_line = " print('indented')" >>> print(dedent(code_single_line)) print('indented') >>> code_single_line_no_indent = "print('no indent')" >>> print(dedent(code_single_line_no_indent)) print('no indent') """ lines = code_str.splitlines() common_prefix = None # Find the longest common whitespace prefix of all non-blank lines for i, line in enumerate(lines): # Ignore blank lines (lines containing only whitespace) if line.strip() == "": continue leading_whitespace = get_leading_whitespace(line) if common_prefix is None: # First non-blank line establishes the potential common prefix common_prefix = leading_whitespace else: # Find the common part between the current common_prefix and this line's whitespace current_len = min(len(common_prefix), len(leading_whitespace)) j = 0 while j < current_len and common_prefix[j] == leading_whitespace[j]: j += 1 common_prefix = common_prefix[:j] # If we've determined there's no common prefix, we can stop checking if not common_prefix: break # If no non-blank lines were found, or no common prefix exists, return original if common_prefix is None or not common_prefix: return code_str # Dedent the lines dedented_lines = [] prefix_len = len(common_prefix) for line in lines: # Preserve blank lines as they are (or almost, splitlines might change endings) if line.strip() == "": dedented_lines.append(line) # Only dedent lines that actually start with the common prefix elif line.startswith(common_prefix): dedented_lines.append(line[prefix_len:]) # If a non-blank line somehow doesn't start with the prefix (shouldn't happen with correct logic above) # keep it as is. This handles cases where the first line might have less indent than others. else: dedented_lines.append(line) return "\n".join(dedented_lines) # Example Usage: code1 = """ def foo(): print("Hello") if True: print("World") # Another part x = 10 """ code2 = """ def bar(): pass # Already dedented """ code3 = """ Line 1 Line 2 # Inconsistent indentation """ code4 = """ Whitespace only line Another line """ code5 = "\t\tMixed tabs\n\t\t and spaces" # Common prefix is '\t\t' print("--- Code 1 ---") print(f"Original:\n{code1}") print(f"Dedented:\n{dedent(code1)}") print("--------------") print("--- Code 2 ---") print(f"Original:\n{code2}") print(f"Dedented:\n{dedent(code2)}") print("--------------") print("--- Code 3 ---") print(f"Original:\n{code3}") print(f"Dedented:\n{dedent(code3)}") # Should not change print("--------------") print("--- Code 4 ---") print(f"Original:\n{code4}") print(f"Dedented:\n{dedent(code4)}") print("--------------") print("--- Code 5 ---") print(f"Original:\n{code5}") print(f"Dedented:\n{dedent(code5)}") print("--------------") # You can also run the doctests: if __name__ == "__main__": import doctest doctest.testmod(verbose=True) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | import re import sys def get_leading_whitespace(line): """Returns the leading whitespace string of a line.""" match = re.match(r'(\s*)', line) # Should always match at least an empty string return match.group(1) if match else "" def dedent(code_str): """ De-dents a string of Python code maximally. Removes the longest common leading whitespace prefix from all non-blank lines. Lines containing only whitespace are considered blank and do not influence the indentation level, but their original whitespace is preserved in the output relative to the dedented block. Args: code_str: A string containing the code to be dedented. Returns: A string with the common leading whitespace removed. Examples: >>> code = ''' ... def example(): ... print("Hello") ... ... # A comment ... ''' >>> print(dedent(code)) <BLANKLINE> def example(): print("Hello") <BLANKLINE> # A comment <BLANKLINE> >>> code_no_common_indent = ''' ... a = 1 ... b = 2 ... ''' >>> print(dedent(code_no_common_indent)) <BLANKLINE> a = 1 b = 2 <BLANKLINE> >>> code_with_tabs_and_spaces = ''' ... \t def func(): ... \t pass # indented with tab + 2 spaces ... ''' >>> print(dedent(code_with_tabs_and_spaces)) <BLANKLINE> def func(): pass # indented with tab + 2 spaces <BLANKLINE> >>> code_only_blanks = ''' ... ... ... \t ... ''' >>> print(dedent(code_only_blanks)) # Should preserve blank/whitespace-only lines <BLANKLINE> <BLANKLINE> <BLANKLINE> >>> code_empty = "" >>> print(dedent(code_empty)) <BLANKLINE> >>> code_single_line = " print('indented')" >>> print(dedent(code_single_line)) print('indented') >>> code_single_line_no_indent = "print('no indent')" >>> print(dedent(code_single_line_no_indent)) print('no indent') """ lines = code_str.splitlines() common_prefix = None # Find the longest common whitespace prefix of all non-blank lines for i, line in enumerate(lines): # Ignore blank lines (lines containing only whitespace) if line.strip() == "": continue leading_whitespace = get_leading_whitespace(line) if common_prefix is None: # First non-blank line establishes the potential common prefix common_prefix = leading_whitespace else: # Find the common part between the current common_prefix and this line's whitespace current_len = min(len(common_prefix), len(leading_whitespace)) j = 0 while j < current_len and common_prefix[j] == leading_whitespace[j]: j += 1 common_prefix = common_prefix[:j] # If we've determined there's no common prefix, we can stop checking if not common_prefix: break # If no non-blank lines were found, or no common prefix exists, return original if common_prefix is None or not common_prefix: return code_str # Dedent the lines dedented_lines = [] prefix_len = len(common_prefix) for line in lines: # Preserve blank lines as they are (or almost, splitlines might change endings) if line.strip() == "": dedented_lines.append(line) # Only dedent lines that actually start with the common prefix elif line.startswith(common_prefix): dedented_lines.append(line[prefix_len:]) # If a non-blank line somehow doesn't start with the prefix (shouldn't happen with correct logic above) # keep it as is. This handles cases where the first line might have less indent than others. else: dedented_lines.append(line) return "\n".join(dedented_lines) # Example Usage: code1 = """ def foo(): print("Hello") if True: print("World") # Another part x = 10 """ code2 = """ def bar(): pass # Already dedented """ code3 = """ Line 1 Line 2 # Inconsistent indentation """ code4 = """ Whitespace only line Another line """ code5 = "\t\tMixed tabs\n\t\t and spaces" # Common prefix is '\t\t' print("--- Code 1 ---") print(f"Original:\n{code1}") print(f"Dedented:\n{dedent(code1)}") print("--------------") print("--- Code 2 ---") print(f"Original:\n{code2}") print(f"Dedented:\n{dedent(code2)}") print("--------------") print("--- Code 3 ---") print(f"Original:\n{code3}") print(f"Dedented:\n{dedent(code3)}") # Should not change print("--------------") print("--- Code 4 ---") print(f"Original:\n{code4}") print(f"Dedented:\n{dedent(code4)}") print("--------------") print("--- Code 5 ---") print(f"Original:\n{code5}") print(f"Dedented:\n{dedent(code5)}") print("--------------") # You can also run the doctests: if __name__ == "__main__": import doctest doctest.testmod(verbose=True) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | import re import sys def get_leading_whitespace(line): """Returns the leading whitespace string of a line.""" match = re.match(r'(\s*)', line) # Should always match at least an empty string return match.group(1) if match else "" def dedent(code_str): """ De-dents a string of Python code maximally. Removes the longest common leading whitespace prefix from all non-blank lines. Lines containing only whitespace are considered blank and do not influence the indentation level, but their original whitespace is preserved in the output relative to the dedented block. Args: code_str: A string containing the code to be dedented. Returns: A string with the common leading whitespace removed. Examples: >>> code = ''' ... def example(): ... print("Hello") ... ... # A comment ... ''' >>> print(dedent(code)) <BLANKLINE> def example(): print("Hello") <BLANKLINE> # A comment <BLANKLINE> >>> code_no_common_indent = ''' ... a = 1 ... b = 2 ... ''' >>> print(dedent(code_no_common_indent)) <BLANKLINE> a = 1 b = 2 <BLANKLINE> >>> code_with_tabs_and_spaces = ''' ... \t def func(): ... \t pass # indented with tab + 2 spaces ... ''' >>> print(dedent(code_with_tabs_and_spaces)) <BLANKLINE> def func(): pass # indented with tab + 2 spaces <BLANKLINE> >>> code_only_blanks = ''' ... ... ... \t ... ''' >>> print(dedent(code_only_blanks)) # Should preserve blank/whitespace-only lines <BLANKLINE> <BLANKLINE> <BLANKLINE> >>> code_empty = "" >>> print(dedent(code_empty)) <BLANKLINE> >>> code_single_line = " print('indented')" >>> print(dedent(code_single_line)) print('indented') >>> code_single_line_no_indent = "print('no indent')" >>> print(dedent(code_single_line_no_indent)) print('no indent') """ lines = code_str.splitlines() common_prefix = None # Find the longest common whitespace prefix of all non-blank lines for i, line in enumerate(lines): # Ignore blank lines (lines containing only whitespace) if line.strip() == "": continue leading_whitespace = get_leading_whitespace(line) if common_prefix is None: # First non-blank line establishes the potential common prefix common_prefix = leading_whitespace else: # Find the common part between the current common_prefix and this line's whitespace current_len = min(len(common_prefix), len(leading_whitespace)) j = 0 while j < current_len and common_prefix[j] == leading_whitespace[j]: j += 1 common_prefix = common_prefix[:j] # If we've determined there's no common prefix, we can stop checking if not common_prefix: break # If no non-blank lines were found, or no common prefix exists, return original if common_prefix is None or not common_prefix: return code_str # Dedent the lines dedented_lines = [] prefix_len = len(common_prefix) for line in lines: # Preserve blank lines as they are (or almost, splitlines might change endings) if line.strip() == "": dedented_lines.append(line) # Only dedent lines that actually start with the common prefix elif line.startswith(common_prefix): dedented_lines.append(line[prefix_len:]) # If a non-blank line somehow doesn't start with the prefix (shouldn't happen with correct logic above) # keep it as is. This handles cases where the first line might have less indent than others. else: dedented_lines.append(line) return "\n".join(dedented_lines) # Example Usage: code1 = """ def foo(): print("Hello") if True: print("World") # Another part x = 10 """ code2 = """ def bar(): pass # Already dedented """ code3 = """ Line 1 Line 2 # Inconsistent indentation """ code4 = """ Whitespace only line Another line """ code5 = "\t\tMixed tabs\n\t\t and spaces" # Common prefix is '\t\t' print("--- Code 1 ---") print(f"Original:\n{code1}") print(f"Dedented:\n{dedent(code1)}") print("--------------") print("--- Code 2 ---") print(f"Original:\n{code2}") print(f"Dedented:\n{dedent(code2)}") print("--------------") print("--- Code 3 ---") print(f"Original:\n{code3}") print(f"Dedented:\n{dedent(code3)}") # Should not change print("--------------") print("--- Code 4 ---") print(f"Original:\n{code4}") print(f"Dedented:\n{dedent(code4)}") print("--------------") print("--- Code 5 ---") print(f"Original:\n{code5}") print(f"Dedented:\n{dedent(code5)}") print("--------------") # You can also run the doctests: if __name__ == "__main__": import doctest doctest.testmod(verbose=True) answer = dedent(''' print("hello") print("world") if True: print("true") else: print("false")''') expected = """print("hello") print("world") if True: print("true") else: print("false")""" assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | --- Code 1 --- Original: def foo(): print("Hello") if True: print("World") # Another part x = 10 Dedented: def foo(): print("Hello") if True: print("World")</p> <h1>Another part</h1> <h2>x = 10</h2> <p>--- Code 2 --- Original: def bar(): pass # Already dedented Dedented: def bar(): pass # Already dedented</p> <hr /> <p>--- Code 3 --- Original: Line 1 Line 2 # Inconsistent indentation Dedented: Line 1 Line 2 # Inconsistent indentation</p> <hr /> <p>--- Code 4 --- Original: Whitespace only line Another line Dedented: Whitespace only line Another line</p> <hr /> <p>--- Code 5 --- Original: Mixed tabs and spaces Dedented: Mixed tabs and spaces</p> <hr /> <p>Trying: code = ''' def example(): print("Hello") # A comment ''' Expecting nothing ok Trying: print(dedent(code)) Expecting: <BLANKLINE> def example(): print("Hello") <BLANKLINE> # A comment <BLANKLINE></p> <hr /> <p>File "/usr/src/app/main.py", line 33, in <strong>main</strong>.dedent Failed example: print(dedent(code)) Expected: <BLANKLINE> def example(): print("Hello") <BLANKLINE> # A comment <BLANKLINE> Got: <BLANKLINE> def example(): print("Hello") <BLANKLINE> # A comment Trying: code_no_common_indent = ''' a = 1 b = 2 ''' Expecting nothing ok Trying: print(dedent(code_no_common_indent)) Expecting: <BLANKLINE> a = 1 b = 2 <BLANKLINE> ok Trying: code_with_tabs_and_spaces = ''' def func(): pass # indented with tab + 2 spaces ''' Expecting nothing ok Trying: print(dedent(code_with_tabs_and_spaces)) Expecting: <BLANKLINE> def func(): pass # indented with tab + 2 spaces <BLANKLINE></p> <hr /> <p>File "/usr/src/app/main.py", line 55, in <strong>main</strong>.dedent Failed example: print(dedent(code_with_tabs_and_spaces)) Expected: <BLANKLINE> def func(): pass # indented with tab + 2 spaces <BLANKLINE> Got: <BLANKLINE> def func(): pass # indented with tab + 2 spaces Trying: code_only_blanks = '''</p> <pre><code>''' </code></pre> <p>Expecting nothing ok Trying: print(dedent(code_only_blanks)) # Should preserve blank/whitespace-only lines Expecting: <BLANKLINE> <BLANKLINE></p> <hr /> <p>File "/usr/src/app/main.py", line 66, in <strong>main</strong>.dedent Failed example: print(dedent(code_only_blanks)) # Should preserve blank/whitespace-only lines Expected: <BLANKLINE> <BLANKLINE> Got: <BLANKLINE> <BLANKLINE> <BLANKLINE> <BLANKLINE> <BLANKLINE> Trying: code_empty = "" Expecting nothing ok Trying: print(dedent(code_empty)) Expecting: <BLANKLINE> ok Trying: code_single_line = " print('indented')" Expecting nothing ok Trying: print(dedent(code_single_line)) Expecting: print('indented') ok Trying: code_single_line_no_indent = "print('no indent')" Expecting nothing ok Trying: print(dedent(code_single_line_no_indent)) Expecting: print('no indent') ok 2 items had no tests: <strong>main</strong> <strong>main</strong>.get_leading_whitespace</p> <hr /> <p>1 items had failures: 3 of 14 in <strong>main</strong>.dedent 14 tests in 3 items. 11 passed and 3 failed. <strong><em>Test Failed</em></strong> 3 failures. All tests passed |
Testing if the previous output contains the string All tests passed
: True