docs: def expand_brackets(s):
      """Remove whitespace and expand all brackets."""
      s = ''.join(s.split())
      while True:
          start = s.find('(')
          if start == -1:
              break
          count = 1 # Number of hanging open brackets
          p = start + 1
          while p < len(s):
              if s[p] == '(':
                  count += 1
              if s[p] == ')':
                  count -= 1
              if not count:
                  break
              p += 1
          if count:
              raise ValueError("Unbalanced parenthesis in '{0}'.".format(s))
          if start == 0 or s[start - 1] != '*':
              s = s[0:start] + s[start + 1:p] + s[p + 1:]
          else:
              m = BRACKET_RE.search(s)
              if m:
                  factor = int(m.group('factor'))
                  matchstart = m.start('factor')
                  s = s[0:matchstart] + (factor - 1) * (s[start + 1:p] + ',') + s[start + 1:p] + s[p + 1:]
              else:
                  raise ValueError("Failed to parse '{0}'.".format(s))
      return s