Ever wanted to make a block of documentation line up with the prefix on the first line? For example, if you ever had a string like this:
:param test: This is an example parameter, and it's too long to fit in one line, so I want to indent it and make it look nice and pretty.
One way to do so is to indent the line like this:
:param test: This is an example parameter, and it's too long to fit in one
line, so I want to indent it and make it look nice and pretty.
This can be annoying to do by hand, so the function pad_string does it automatically.
To convert the string, we need the left component, which is :param test:, and we need what is to the right, which
is This is an example parameter, and it's too long to fit in one line, so I want to indent it and make it look nice and pretty.
The code signature would look like this:
left=":param test:"
right="This is an example parameter, and it's too long to fit in one line, so I want to indent it and make it look nice and pretty."
output=pad_string(left, right, space_left=True)An alternative method is to just add the space to the left component directly, so this code is also going to give
the same result:
left=":param test: "
right="This is an example parameter, and it's too long to fit in one line, so I want to indent it and make it look nice and pretty."
output=pad_string(left, right)You can also add indent to the string, so it's perfect for usage in docstrings.
left=":param test: "
right="This is an example parameter, and it's too long to fit in one line, so I want to indent it and make it look nice and pretty."
output=pad_string(left, right, indent_level=1)The output here will be:
:param test: This is an example parameter, and it's too long to fit in one
line, so I want to indent it and make it look nice and
pretty.
Finally, you can just indent a block without a prefix, to wrap a long phrase into a docstring without adding any
prefix. For example, if we want to indent the previous string without adding :param test:, we could do:
right="This is an example parameter, and it's too long to fit in one line, so I want to indent it and make it look nice and pretty."
output=pad_string("", right, indent_level=1)The output here will be:
This is an example parameter, and it's too long to fit in one line, so I
want to indent it and make it look nice and pretty.