9 examples of 'how to unindent in python' in Python

Every line of 'how to unindent in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
338def force_unindent(text, ignore_first_line=False):
339 """Unindent a piece of text, line by line"""
340
341 lines = text.split("\n")
342
343 if ignore_first_line:
344 return "\n".join(lines[:1] + [l.lstrip() for l in lines[1:]])
345 else:
346 return "\n".join([l.lstrip() for l in lines])
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

31def indent_to_spaces(self, indent):
32 """Converts indentation to spaces respecting Vim settings."""
33 indent = indent.expandtabs(self._tabstop)
34 right = (len(indent) - len(indent.rstrip(" "))) * " "
35 indent = indent.replace(" ", "")
36 indent = indent.replace("\t", " " * self._tabstop)
37 return indent + right
283def test_deindent():
284 from py.__.code.source import deindent as deindent
285 assert deindent(['\tfoo', '\tbar', ]) == ['foo', 'bar']
286
287 def f():
288 c = '''while True:
289 pass
290'''
291 import inspect
292 lines = deindent(inspect.getsource(f).splitlines())
293 assert lines == ["def f():", " c = '''while True:", " pass", "'''"]
294
295 source = """
296 def f():
297 def g():
298 pass
299 """
300 lines = deindent(source.splitlines())
301 assert lines == ['', 'def f():', ' def g():', ' pass', ' ']
153@staticmethod
154def unindent( code ):
155 """
156 function that implements code unindent algorithm.
157
158 @param code: C++ code block.
159 @type code: str
160 @rtype: str
161 """
162 assert isinstance( code, types.StringTypes )
163 if code.startswith(code_creator_t.__INDENTATION):
164 code = code[ len( code_creator_t.__INDENTATION ):]
165 return code.replace( os.linesep + code_creator_t.__INDENTATION
166 , os.linesep )
170@staticmethod
171def reindent(code):
172 """
173 :param code:
174 :return: string
175 """
176 tab = code.split("\n")
177 new_code = ""
178 prof = 0
179 for line in tab:
180 if line.startswith("else") and prof > 0:
181 prof -= 1
182 line = (prof * "\t") + line
183 if line.endswith(":"):
184 prof += 1
185 # else :
186 # if prof > 0 :
187 # prof -= 1
188 new_code += "\n" + line
189
190 return new_code
15def fixBadIndent(toModify,indent):
16 err_string=indent.replace(' Bad indentation. Found ','').replace('(bad-indentation)','').split('spaces, expected')
17 bad=int(err_string[0])
18 good=int(err_string[1])
19 return toModify.replace(' '*bad,' '*good)
191def outdent(text):
192 line_list = text.splitlines()
193 smallest_indentation = min([
194 len(line) - len(line.lstrip(" "))
195 for line in line_list if line
196 ])
197 return "\n".join([line[smallest_indentation:] for line in line_list])
32def indent(lines):
33 if lines is None:
34 return ''
35 return '\n'.join(' '+line for line in lines.split('\n'))
563def indent(s: str, n: int) -> str:
564 """Indent all the lines in s (separated by newlines) by n spaces."""
565 s = ' ' * n + s
566 s = s.replace('\n', '\n' + ' ' * n)
567 return s

Related snippets