Python - euccas/expmap GitHub Wiki

Python

Race condition when writing/reading files

Write race free code.

Think about this situation:

Check the existence of file, create a file when the check result is unexist

Race condition could happen: the moment when checking is performed, the file doesn't exist. When the program tries to create the file then, it exists.

One way to avoid the race condition is action first and use exception to handle the unexpected situation.

Built-in functions: any, all

if all(k.isspace() for k in comment_lines):
  pass
elif len(comment_lines) == 1:
  if not comment_lines[0].isspace():
    added_comment += line(indent_level, "/* {0} */".format(comment_lines[0].rstrip()))
if any(r["good"] for r in info["fruits"]):
  content += line(1, "switch (fruits)")
  content += line(1, "{")