title: Extract dependency information between the variables of a numpy expression
body: <p>I want to extract dependency information between the variables in a string that contains a Python 3 <code>NumPy</code> expression. For example:</p>
  
  <pre><code>import numpy as np
  
  a = np.array([10, 20, 30])
  b = np.array([10, 20, 30])
  c = np.array([10, 20, 30])
  d = 100
  
  # The expression string will only contain numbers, arrays, and `NumPy` functions.
  expression = 'b[1:3] = a[0:3:2] + np.sum(c[:]) + d'
  
  deps = extract_dependencies(expression)
  </code></pre>
  
  <p>Then, the result should be:</p>
  
  <pre><code>deps: {
      'b[0]': [],
      'b[1]': ['a[0]', 'c[0]', 'c[1]', 'c[2]', 'd']
      'b[2]': ['a[2]', 'c[0]', 'c[1]', 'c[2]', 'd']
  }
  </code></pre>
  
  <p>My problem is that I can't figure out how to implement <code>extract_dependencies()</code>. </p>
  
  <p>This is easy to solve if all the symbols in the expression are either not arrays or arrays with single-element indexing (e.g., <code>foo</code>, <code>bar[0]</code>, <code>baz[2]</code>). It can be done by using either regular expressions or some basic text parsing.</p>
  
  <p>However, if the variables are arrays, things get more complicated. For basic operations, one could use regular expressions to find the variables in the expression string, and then extract and map array indices accordingly. For example it is easy to extract and match the indices of the expression <code>a[0:2] = b[1:3]</code>. Things become more tricky when functions are used as part of the expression string because they are essentially "black boxes". You can't account for all possible function signatures, behaviors, and return values unless you hard code every single one of them. </p>
  
  <p>I was wondering if this could be solved using some clever use of Python's <code>eval</code>, <code>exec</code>, or <code>ast</code> trees.</p>
  
  <p>Any ideas? :)</p>
  
  <p>Thank you.</p>
  
  <p>PS: The expression string is eventually evaluated using the <code>asteval</code> library. Hence, a solution that utilizes <code>asteval</code> will get extra points! :) </p>