Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Finding the variables (read or write)

Reply
Thread Tools

Finding the variables (read or write)

 
 
servekarimi@gmail.com
Guest
Posts: n/a
 
      01-14-2013
I'd like to develop a small debugging tool for python programs.In Dynamic Slicing How can I find the variables that are accessed in a statement? And find the type of access (read or write) for those variables (in Python).
### Write: A statement can change the program state.
### Read : A statement can read the program state .
**For example in these 4 lines we have:
(1) x = a+b => write{x} & read{a,b}
(2) y=6 => write{y} & read{}
(3) while(n>1) => write{} & read{n}
(4) n=n-1 => write{n} & read{n}

@@If I use dis(disassembler) How can I get output of dis in python as dictionary?

Thanks
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      01-14-2013
On Tue, Jan 15, 2013 at 6:48 AM, <(E-Mail Removed)> wrote:
> I'd like to develop a small debugging tool for python programs.In Dynamic Slicing How can I find the variables that are accessed in a statement? And find the type of access (read or write) for those variables (in Python).
> ### Write: A statement can change the program state.
> ### Read : A statement can read the program state .
> **For example in these 4 lines we have:
> (1) x = a+b => write{x} & read{a,b}
> (2) y=6 => write{y} & read{}
> (3) while(n>1) => write{} & read{n}
> (4) n=n-1 => write{n} & read{n}


An interesting question. What's your definition of "variable"? For
instance, what is written and what is read by this statement:

self.lst[2] += 4

Is "self.lst" considered a variable? (In C++ etc, this would be a
member function manipulating an instance variable.) Or is "self" the
variable? And in either case, was it written to? What about:

self.lst.append(self.lst[-1]+self.lst[-2])

(which might collect Fibonacci numbers)?

ChrisA
 
Reply With Quote
 
 
 
 
Chris Kaynor
Guest
Posts: n/a
 
      01-14-2013
On Mon, Jan 14, 2013 at 1:28 PM, Chris Angelico <(E-Mail Removed)> wrote:

> On Tue, Jan 15, 2013 at 6:48 AM, <(E-Mail Removed)> wrote:
> > I'd like to develop a small debugging tool for python programs.In

> Dynamic Slicing How can I find the variables that are accessed in a
> statement? And find the type of access (read or write) for those variables
> (in Python).
> > ### Write: A statement can change the program state.
> > ### Read : A statement can read the program state .
> > **For example in these 4 lines we have:
> > (1) x = a+b => write{x} & read{a,b}
> > (2) y=6 => write{y} & read{}
> > (3) while(n>1) => write{} & read{n}
> > (4) n=n-1 => write{n} & read{n}

>
> An interesting question. What's your definition of "variable"? For
> instance, what is written and what is read by this statement:
>
> self.lst[2] += 4
>
> Is "self.lst" considered a variable? (In C++ etc, this would be a
> member function manipulating an instance variable.) Or is "self" the
> variable? And in either case, was it written to? What about:
>
> self.lst.append(self.lst[-1]+self.lst[-2])
>
> (which might collect Fibonacci numbers)?
>


And those aren't even covering the case that a, normally non-mutating,
method actually mutates. Consider the following class (untested):

class Test(object):
def __init__(self, value):
self.value = value
self.adds = 0
def __add__(self, other):
self.adds += 1
other.adds += 1
return Test(self.value + other.value)

With that class,
x = a + b
would mutate x, a, and b, presuming a and b are instances of Test.


>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list
>


 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      01-14-2013
On 1/14/2013 4:28 PM, Chris Angelico wrote:
> On Tue, Jan 15, 2013 at 6:48 AM, <(E-Mail Removed)> wrote:
>> I'd like to develop a small debugging tool for python programs.In Dynamic Slicing How can I find the variables that are accessed in a statement? And find the type of access (read or write) for those variables (in Python).
>> ### Write: A statement can change the program state.
>> ### Read : A statement can read the program state .
>> **For example in these 4 lines we have:
>> (1) x = a+b => write{x} & read{a,b}
>> (2) y=6 => write{y} & read{}
>> (3) while(n>1) => write{} & read{n}
>> (4) n=n-1 => write{n} & read{n}


I would try compiling the source code to an ast (abstract syntax tree).
See the ast module for how to do that and how to 'read' the resulting tree.

> An interesting question. What's your definition of "variable"? For
> instance, what is written and what is read by this statement:
>
> self.lst[2] += 4
>
> Is "self.lst" considered a variable? (In C++ etc, this would be a
> member function manipulating an instance variable.) Or is "self" the
> variable? And in either case, was it written to?


'self' is read, 'lst' is written to.

What about:
>
> self.lst.append(self.lst[-1]+self.lst[-2])
>
> (which might collect Fibonacci numbers)?


'self' read, 'lst' read and written. Knowing that for non-builtins is
another matter .

--
Terry Jan Reedy

 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      01-14-2013
On Tue, Jan 15, 2013 at 8:37 AM, Chris Kaynor <(E-Mail Removed)> wrote:
> And those aren't even covering the case that a, normally non-mutating,
> method actually mutates.


If it's static analysis, I'd quietly ignore those sorts of cases.
Anything can be changed any time, including stuff that's completely
unrelated to what you're working on.

Now, if the OP just wants to know what names get referenced (without
distinguishing reads from writes), that's quite possible, and in fact
easy - if you're willing to analyze a whole function instead of a
single statement.

>>> def foo():

x=y+1

>>> foo.__code__.co_varnames

('x',)
>>> foo.__code__.co_names

('y',)

The first one is the ones that get assigned to (not quite the same as
"written to"), the second is ones that don't. Well, more or less. In
simple cases.

ChrisA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
finding global variables sinbad C Programming 9 11-12-2008 11:40 AM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Need a Ruby helper for finding undefined variables and methods CharlesWinstead@gmail.com Ruby 1 08-21-2007 07:26 PM
Finding Hidden ASP Variables robby@angleinteractive.com ASP General 3 07-03-2007 06:00 PM
Finding Server... Finding Host.... enough already!!! Leesa_Tay@softhome.net Computer Support 2 01-20-2006 10:23 AM



Advertisments