def IsSurroundedByBrackets(tok):
 paren_count = 0
 brace_count = 0
 sq_bracket_count = 0
 previous_token = tok.previous_token
 while previous_token:
  if (previous_token.value == ')'):
   paren_count -= 1
  elif (previous_token.value == '}'):
   brace_count -= 1
  elif (previous_token.value == ']'):
   sq_bracket_count -= 1
  if (previous_token.value == '('):
   if (paren_count == 0):
    return True
   paren_count += 1
  elif (previous_token.value == '{'):
   if (brace_count == 0):
    return True
   brace_count += 1
  elif (previous_token.value == '['):
   if (sq_bracket_count == 0):
    return True
   sq_bracket_count += 1
  previous_token = previous_token.previous_token
 return False

def IsSurroundedByBrackets ( tok ) : paren_count = 0 brace_count = 0 sq_bracket_count = 0 previous_token = tok . previous_token while previous_token : if ( previous_token . value == ')' ) : paren_count -= 1 elif ( previous_token . value == '}' ) : brace_count -= 1 elif ( previous_token . value == ']' ) : sq_bracket_count -= 1 if ( previous_token . value == '(' ) : if ( paren_count == 0 ) : return True paren_count += 1 elif ( previous_token . value == '{' ) : if ( brace_count == 0 ) : return True brace_count += 1 elif ( previous_token . value == '[' ) : if ( sq_bracket_count == 0 ) : return True sq_bracket_count += 1 previous_token = previous_token . previous_token return False
return true if the token is surrounded by brackets .