The comment block for Python

I’d go for <<<>>>, feels much easier to get which are the opening and the closing ones.

1 Like

If I’m not mistaken, the second line will undo the effect of the first line, no? If you want to manipulate lines with sed, you should use markers in your comments:

#:section 1
blah blah
#:end
##:section 2
#blah blah
##:end

Then you can use

sed -i '/:section 1/,/:end/s/^/#/' file.py
sed -i '/:section 2/,/:end/s/^#//' file.py

to swap which part is commented out. No changes to Python needed, it works now. Plus, you have readable labels to the sections.

Manipulating Python code with sed is a very unusual use-case, but it’s one that today’s Python is already accommodating.

1 Like

That looks quite confusing to read. You need to take notice of exactly
how many #s there are to tell which parts are active. – but only on
lines where the # is followed by “”". No longer can you just skim and
ignore any line that starts with #.

Also it’s rather specialised, allowing you to toggle between 2 versions,
but not choose one of 3 or more.

def verify(filename, clear_payload):
    #"""
    # I would like to verify if the PNG is valid and not corrupt but Pillow .verify() is not yet implemented
    im = Image.open(filename)
    im.verify()
    im.close()
    #"""
    content = open(filename, "rb").read()

This is actually not a totally uncommon pattern; the code snippet can be “deactivated” by one keystroke:

def verify(filename, clear_payload):
    """
    # I would like to verify if the PNG is valid and not corrupt but Pillow .verify() is not yet implemented
    im = Image.open(filename)
    im.verify()
    im.close()
    #"""
    content = open(filename, "rb").read()

This turns these few lines into a multi-line string literal that is just discarded. This construct can be (ab)used as a multi-line comment. (In this particular case, it is a doc string, but maybe the author did not care about this.)