Skip to main content
deleted 92 characters in body
Source Link
SuperBiasedMan
  • 13.5k
  • 5
  • 37
  • 62

Instead of a ternary, use the or syntax for setting your booleans here. If is_first_line is True then True is returned, if it's not then not lines[index - 1] is evaluated and the result of that is returned whether it's True or False.

preceding_blank = is_first_line or not lines[index - 1]

But since you're setting is_first_line one line before and never using it again, I'd even fold that into this expression.

preceding_blank = index == 0 or not lines[index - 1]

I'd make both the same changes with is_last_line. Also, variable names should be isFirstLine. Underscore separators are used for function names. I would substitute index for i, since i is idiomatic for index anyway and will save some characters.

One last thing that you may want, adding .strip() to not line will strip out any whitespace on the line and make sure that even an empty whitespace line will be considered False because any character in a string will make it evaluate as True. This may or may not be beneficial to you, as whitespace could be meaningful.

with open(sys.argv[1], 'r') as markdown:
    lines = markdown.read().splitlines()
    for i, line in enumerate(lines):
        precedingBlank = i == 0 or not lines[i - 1].strip()
        followingBlank = i == len(lines) - 1 or not lines[i + 1].strip()
        is_standalone = preceding_blank and following_blank

        is_image = line.startswith('![') and '](' in line and line.endswith(')')
        print(line + ('\\\n' if is_standalone and is_image else ''))

Instead of a ternary, use the or syntax for setting your booleans here. If is_first_line is True then True is returned, if it's not then not lines[index - 1] is evaluated and the result of that is returned whether it's True or False.

preceding_blank = is_first_line or not lines[index - 1]

But since you're setting is_first_line one line before and never using it again, I'd even fold that into this expression.

preceding_blank = index == 0 or not lines[index - 1]

I'd make both the same changes with is_last_line. Also, variable names should be isFirstLine. Underscore separators are used for function names. I would substitute index for i, since i is idiomatic for index anyway and will save some characters.

One last thing that you may want, adding .strip() to not line will strip out any whitespace on the line and make sure that even an empty whitespace line will be considered False because any character in a string will make it evaluate as True.

with open(sys.argv[1], 'r') as markdown:
    lines = markdown.read().splitlines()
    for i, line in enumerate(lines):
        precedingBlank = i == 0 or not lines[i - 1].strip()
        followingBlank = i == len(lines) - 1 or not lines[i + 1].strip()
        is_standalone = preceding_blank and following_blank

        is_image = line.startswith('![') and '](' in line and line.endswith(')')
        print(line + ('\\\n' if is_standalone and is_image else ''))

Instead of a ternary, use the or syntax for setting your booleans here. If is_first_line is True then True is returned, if it's not then not lines[index - 1] is evaluated and the result of that is returned whether it's True or False.

preceding_blank = is_first_line or not lines[index - 1]

But since you're setting is_first_line one line before and never using it again, I'd even fold that into this expression.

preceding_blank = index == 0 or not lines[index - 1]

I'd make both the same changes with is_last_line. Also I would substitute index for i, since i is idiomatic for index anyway and will save some characters.

One last thing that you may want, adding .strip() to not line will strip out any whitespace on the line and make sure that even an empty whitespace line will be considered False because any character in a string will make it evaluate as True. This may or may not be beneficial to you, as whitespace could be meaningful.

with open(sys.argv[1], 'r') as markdown:
    lines = markdown.read().splitlines()
    for i, line in enumerate(lines):
        precedingBlank = i == 0 or not lines[i - 1].strip()
        followingBlank = i == len(lines) - 1 or not lines[i + 1].strip()
        is_standalone = preceding_blank and following_blank

        is_image = line.startswith('![') and '](' in line and line.endswith(')')
        print(line + ('\\\n' if is_standalone and is_image else ''))
deleted 5 characters in body
Source Link
SuperBiasedMan
  • 13.5k
  • 5
  • 37
  • 62

Instead of a ternary, use the or syntax for setting your booleans here. If is_first_line is True then True is returned, if it's not then not lines[index - 1] is evaluated and the result of that is returned whether it's True or False.

preceding_blank = is_first_line or not lines[index - 1]

But since you're setting is_first_line one line before and never using it again, I'd even fold that into this expression.

preceding_blank = index == 0 or not lines[index - 1]

I'd make both the same changes with is_last_line. Also, variable names should be isFirstLine. Underscore separators are used for function names. Also I would substitute index for i, since i is idiomatic for index anyway and will save some characters.

One last thing that you may want, adding .strip() to not line will strip out any whitespace on the line and make sure that even an empty whitespace line will be considered False because any character in a string will make it evaluate as True.

with open(sys.argv[1], 'r') as markdown:
    lines = markdown.read().splitlines()
    for i, line in enumerate(lines):
        precedingBlank = i == 0 or not lines[i - 1].strip()
        followingBlank = i == len(lines) - 1 or not lines[i + 1].strip()
        is_standalone = preceding_blank and following_blank

        is_image = line.startswith('![') and '](' in line and line.endswith(')')
        print(line + ('\\\n' if is_standalone and is_image else ''))

Instead of a ternary, use the or syntax for setting your booleans here. If is_first_line is True then True is returned, if it's not then not lines[index - 1] is evaluated and the result of that is returned whether it's True or False.

preceding_blank = is_first_line or not lines[index - 1]

But since you're setting is_first_line one line before and never using it again, I'd even fold that into this expression.

preceding_blank = index == 0 or not lines[index - 1]

I'd make both the same changes with is_last_line. Also, variable names should be isFirstLine. Underscore separators are used for function names. Also I would substitute index for i, since i is idiomatic for index anyway and will save some characters.

One last thing that you may want, adding .strip() to not line will strip out any whitespace on the line and make sure that even an empty whitespace line will be considered False because any character in a string will make it evaluate as True.

with open(sys.argv[1], 'r') as markdown:
    lines = markdown.read().splitlines()
    for i, line in enumerate(lines):
        precedingBlank = i == 0 or not lines[i - 1].strip()
        followingBlank = i == len(lines) - 1 or not lines[i + 1].strip()
        is_standalone = preceding_blank and following_blank

        is_image = line.startswith('![') and '](' in line and line.endswith(')')
        print(line + ('\\\n' if is_standalone and is_image else ''))

Instead of a ternary, use the or syntax for setting your booleans here. If is_first_line is True then True is returned, if it's not then not lines[index - 1] is evaluated and the result of that is returned whether it's True or False.

preceding_blank = is_first_line or not lines[index - 1]

But since you're setting is_first_line one line before and never using it again, I'd even fold that into this expression.

preceding_blank = index == 0 or not lines[index - 1]

I'd make both the same changes with is_last_line. Also, variable names should be isFirstLine. Underscore separators are used for function names. I would substitute index for i, since i is idiomatic for index anyway and will save some characters.

One last thing that you may want, adding .strip() to not line will strip out any whitespace on the line and make sure that even an empty whitespace line will be considered False because any character in a string will make it evaluate as True.

with open(sys.argv[1], 'r') as markdown:
    lines = markdown.read().splitlines()
    for i, line in enumerate(lines):
        precedingBlank = i == 0 or not lines[i - 1].strip()
        followingBlank = i == len(lines) - 1 or not lines[i + 1].strip()
        is_standalone = preceding_blank and following_blank

        is_image = line.startswith('![') and '](' in line and line.endswith(')')
        print(line + ('\\\n' if is_standalone and is_image else ''))
added 270 characters in body
Source Link
SuperBiasedMan
  • 13.5k
  • 5
  • 37
  • 62

Instead of a ternary, use the or syntax for setting your booleans here. If is_first_line is True then True is returned, if it's not then not lines[index - 1] is evaluated and the result of that is returned whether it's True or False.

preceding_blank = is_first_line or not lines[index - 1]

But since you're setting is_first_line one line before and never using it again, I'd even fold that into this expression.

preceding_blank = index == 0 or not lines[index - 1]

I'd make both the same changes with is_last_line. Also, variable names should be isFirstLine. Underscore separators are used for function names. Also I would substitute index for i, since i is idiomatic for index anyway and will save some characters.

One last thing that you may want, adding .strip() to not line will strip out any whitespace on the line and make sure that even an empty whitespace line will be considered False because any character in a string will make it evaluate as True.

with open(sys.argv[1], 'r') as markdown:
    lines = markdown.read().splitlines()
    for i, line in enumerate(lines):
        precedingBlank = i == 0 or not lines[i - 1].strip()
        followingBlank = i == len(lines) - 1 or not lines[i + 1].strip()
        is_standalone = preceding_blank and following_blank

        is_image = line.startswith('![') and '](' in line and line.endswith(')')
        print(line + ('\\\n' if is_standalone and is_image else ''))

Instead of a ternary, use the or syntax for setting your booleans here. If is_first_line is True then True is returned, if it's not then not lines[index - 1] is evaluated and the result of that is returned whether it's True or False.

preceding_blank = is_first_line or not lines[index - 1]

But since you're setting is_first_line one line before and never using it again, I'd even fold that into this expression.

preceding_blank = index == 0 or not lines[index - 1]

I'd make both the same changes with is_last_line. Also, variable names should be isFirstLine. Underscore separators are used for function names. Also I would substitute index for i, since i is idiomatic for index anyway and will save some characters.

with open(sys.argv[1], 'r') as markdown:
    lines = markdown.read().splitlines()
    for i, line in enumerate(lines):
        precedingBlank = i == 0 or not lines[i - 1]
        followingBlank = i == len(lines) - 1 or not lines[i + 1]
        is_standalone = preceding_blank and following_blank

        is_image = line.startswith('![') and '](' in line and line.endswith(')')
        print(line + ('\\\n' if is_standalone and is_image else ''))

Instead of a ternary, use the or syntax for setting your booleans here. If is_first_line is True then True is returned, if it's not then not lines[index - 1] is evaluated and the result of that is returned whether it's True or False.

preceding_blank = is_first_line or not lines[index - 1]

But since you're setting is_first_line one line before and never using it again, I'd even fold that into this expression.

preceding_blank = index == 0 or not lines[index - 1]

I'd make both the same changes with is_last_line. Also, variable names should be isFirstLine. Underscore separators are used for function names. Also I would substitute index for i, since i is idiomatic for index anyway and will save some characters.

One last thing that you may want, adding .strip() to not line will strip out any whitespace on the line and make sure that even an empty whitespace line will be considered False because any character in a string will make it evaluate as True.

with open(sys.argv[1], 'r') as markdown:
    lines = markdown.read().splitlines()
    for i, line in enumerate(lines):
        precedingBlank = i == 0 or not lines[i - 1].strip()
        followingBlank = i == len(lines) - 1 or not lines[i + 1].strip()
        is_standalone = preceding_blank and following_blank

        is_image = line.startswith('![') and '](' in line and line.endswith(')')
        print(line + ('\\\n' if is_standalone and is_image else ''))
Source Link
SuperBiasedMan
  • 13.5k
  • 5
  • 37
  • 62
Loading