Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can tokenize Python source code using the <a href="http://docs.python.org/2/library/tokenize.html" rel="nofollow">tokenize</a> module from the standard library. This will allow you to find all variable names used in the script.</p> <p>Now suppose we define a "non-dependency" as any variable name that comes immediately before an <code>=</code> sign. Then, depending on how simple your script code really is (see the Caveats below), you may be able to determine the variable names which are not non-dependencies this way:</p> <pre><code>import tokenize import io import token import collections import keyword kwset = set(keyword.kwlist) class Token(collections.namedtuple('Token', 'num val start end line')): @property def name(self): return token.tok_name[self.num] source = ''' C = A+B D = C * 4 ''' lastname = None names = set() not_dep = set() for tok in tokenize.generate_tokens(io.BytesIO(source).readline): tok = Token(*tok) print(tok.name, tok.val) if tok.name == 'NAME': names.add(tok.val) lastname = tok.val if tok.name == 'OP' and tok.val == '=': not_dep.add(lastname) print(names) # set(['A', 'C', 'B', 'D']) print(not_dep) # set(['C', 'D']) deps = dict.fromkeys(names - not_dep - kwset, 1) print(deps) # {'A': 1, 'B': 1} </code></pre> <hr> <p><strong>Caveats</strong>:</p> <ul> <li><p>If your scripts contain statements other than simple assignments, then <code>names</code> may become populated with undesired variable names. For example,</p> <pre><code>import numpy </code></pre> <p>would add both <code>'import'</code> and <code>'numpy'</code> to the set <code>names</code>.</p></li> <li><p>If your script contains an assignment that makes use of left-hand side tuple unpacking, such as </p> <pre><code>E, F = 1, 2 </code></pre> <p>then the naive code above will only recognize that <code>F</code> is not a dependency.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload