Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
818 B
6
Indexable
Never
def remove_comment_delimiters(docstring: str, remove_whitespace: bool=True) -> str:
    """
    Remove comment delimiters.
    Example: //, /*, */, #, etc
    
    Args:
        docstring (str): raw (line or block) comment
        remove_whitespace (bool): remove leading whitespace or not
    Returns:
        str: removed delimiters docstring/comment
    
    """
    
    clean_pattern1 = re.compile(r'([#]+)$|^([#]+)')  # special single-line comment with #
    clean_pattern2 = re.compile(r'([\/*=-]+)$|^([\/*!=-]+)')
    
    new_docstring = []
    for line in docstring.split('\n'):
        if remove_whitespace:
            line = line.strip()
        line = re.sub(clean_pattern1, '', line)
        line = re.sub(clean_pattern2, '', line)
        new_docstring.append(line)

    return '\n'.join(new_docstring)