Indent a string a given number of spaces or tabstops. indent(str, nspaces=4, ntabs=0) -> indent str by ntabs+nspaces. Parameters ---------- instr : basestring The string to be indented. nspaces : int (default: 4) The number of spaces to be indented. ntabs :
(instr: str, nspaces: int = 4, ntabs: int = 0, flatten: bool = False)
| 256 | |
| 257 | |
| 258 | def indent(instr: str, nspaces: int = 4, ntabs: int = 0, flatten: bool = False) -> str: |
| 259 | """Indent a string a given number of spaces or tabstops. |
| 260 | |
| 261 | indent(str, nspaces=4, ntabs=0) -> indent str by ntabs+nspaces. |
| 262 | |
| 263 | Parameters |
| 264 | ---------- |
| 265 | instr : basestring |
| 266 | The string to be indented. |
| 267 | nspaces : int (default: 4) |
| 268 | The number of spaces to be indented. |
| 269 | ntabs : int (default: 0) |
| 270 | The number of tabs to be indented. |
| 271 | flatten : bool (default: False) |
| 272 | Whether to scrub existing indentation. If True, all lines will be |
| 273 | aligned to the same indentation. If False, existing indentation will |
| 274 | be strictly increased. |
| 275 | |
| 276 | Returns |
| 277 | ------- |
| 278 | str : string indented by ntabs and nspaces. |
| 279 | |
| 280 | """ |
| 281 | ind = "\t" * ntabs + " " * nspaces |
| 282 | if flatten: |
| 283 | pat = re.compile(r'^\s*', re.MULTILINE) |
| 284 | else: |
| 285 | pat = re.compile(r'^', re.MULTILINE) |
| 286 | outstr = re.sub(pat, ind, instr) |
| 287 | if outstr.endswith(os.linesep+ind): |
| 288 | return outstr[:-len(ind)] |
| 289 | else: |
| 290 | return outstr |
| 291 | |
| 292 | |
| 293 | def list_strings(arg: Union[str, List[str]]) -> List[str]: |
no outgoing calls
searching dependent graphs…