Derek Fountain wrote:[color=blue]
> Does Python have something similar to Tcl style tracing? That is, the
> ability to call a subroutine when a variable is written to or read from?[/color]
On Wed, Jan 28, 2004 at 10:18:59AM +0800, Derek Fountain wrote:[color=blue]
> Does Python have something similar to Tcl style tracing? That is, the
> ability to call a subroutine when a variable is written to or read from?[/color]
Not for variables in general, unless you feel like mucking about with
sys.settrace.
For attributes of objects, you can use __getattr__/__setattr__ methods, use
__getattribute_ _/__setattribute_ _ methods, use properties, or even
roll-your-own descriptor, depending on what you need.
What do you need this for? If you can be more specific, we might be able to
help you a little better.
Hello Derek,
[color=blue]
> Does Python have something similar to Tcl style tracing? That is, the
> ability to call a subroutine when a variable is written to or read from?[/color]
Not that I know of.
The only way I can think you'll be able to pull this one is by using
pdb.
Just set breakpoints where the variable can change a give as a
condition a function that prints the value of the variable and returns
0.
> What do you need this for? If you can be more specific, we might be able[color=blue]
> to help you a little better.[/color]
I have a Tcl/Tk script which uses a canvas to graphically represent the data
stored in the program. Whenever the data store is updated, a Tcl trace
automatically triggers to ensure the graphical display is kept consistent
with the data. I was after a simple way to convert the script to Python
Tkinter. Without the eqivalent of Tcl traces I'm probably going to have to
think about it... ;o)
In article <40177f07$0$173 0$5a62ac22@free news.iinet.net. au>,
Derek Fountain <nomail@hursley .ibm.com> wrote:[color=blue][color=green]
>> What do you need this for? If you can be more specific, we might be able
>> to help you a little better.[/color]
>
>I have a Tcl/Tk script which uses a canvas to graphically represent the data
>stored in the program. Whenever the data store is updated, a Tcl trace
>automaticall y triggers to ensure the graphical display is kept consistent
>with the data. I was after a simple way to convert the script to Python
>Tkinter. Without the eqivalent of Tcl traces I'm probably going to have to
>think about it... ;o)[/color]
In article <40177f07$0$173 0$5a62ac22@free news.iinet.net. au>,
Derek Fountain <nomail@hursley .ibm.com> wrote:[color=blue][color=green]
>> What do you need this for? If you can be more specific, we might be able
>> to help you a little better.[/color]
>
>I have a Tcl/Tk script which uses a canvas to graphically represent the data
>stored in the program. Whenever the data store is updated, a Tcl trace
>automaticall y triggers to ensure the graphical display is kept consistent
>with the data. I was after a simple way to convert the script to Python
>Tkinter. Without the eqivalent of Tcl traces I'm probably going to have to
>think about it... ;o)[/color]
The Pythonic thing to do is to intercept the data store's set or
__setattr__ or ... method.
--
Derek Fountain wrote:
[color=blue]
> Does Python have something similar to Tcl style tracing? That is, the
> ability to call a subroutine when a variable is written to or read from?[/color]
Would it be sufficient to keep track of attribute changes? Then you could
try something like
class TraceChanges(ob ject):
def __init__(self, name, onChange):
# bypass the tracking mechanism
object.__setatt r__(self, "name", name)
object.__setatt r__(self, "changed", onChange)
def changed(sender, name, value):
# could also be a method of TraceChanges
print "%s.%s changed to %s --> updating canvas" % (sender.name, name,
value)
"Derek Fountain" <nomail@hursley .ibm.com> wrote in message
news:40177f07$0 $1730$5a62ac22@ freenews.iinet. net.au...[color=blue][color=green]
> > What do you need this for? If you can be more specific, we might be[/color][/color]
able[color=blue][color=green]
> > to help you a little better.[/color]
>
> I have a Tcl/Tk script which uses a canvas to graphically represent the[/color]
data[color=blue]
> stored in the program. Whenever the data store is updated, a Tcl trace
> automatically triggers to ensure the graphical display is kept consistent
> with the data. I was after a simple way to convert the script to Python
> Tkinter. Without the eqivalent of Tcl traces I'm probably going to have to
> think about it... ;o)[/color]
There's a facility where you can associate a widget with a
variable so that when the variable changes the widget's
value changes, and vice versa. I don't offhand remember the
details though. You'll need to look at one of the references.
The variable has to be a subclass of a special Tkinter class
for this to work, though.
In article <101fkp3nd3ea84 c@news.supernew s.com>,
John Roth <newsgroups@jhr othjr.com> wrote:[color=blue]
>
>"Derek Fountain" <nomail@hursley .ibm.com> wrote in message
>news:40177f07$ 0$1730$5a62ac22 @freenews.iinet .net.au...[color=green][color=darkred]
>> > What do you need this for? If you can be more specific, we might be[/color][/color]
>able[color=green][color=darkred]
>> > to help you a little better.[/color]
>>
>> I have a Tcl/Tk script which uses a canvas to graphically represent the[/color]
>data[color=green]
>> stored in the program. Whenever the data store is updated, a Tcl trace
>> automatically triggers to ensure the graphical display is kept consistent
>> with the data. I was after a simple way to convert the script to Python
>> Tkinter. Without the eqivalent of Tcl traces I'm probably going to have to
>> think about it... ;o)[/color]
>
>There's a facility where you can associate a widget with a
>variable so that when the variable changes the widget's
>value changes, and vice versa. I don't offhand remember the
>details though. You'll need to look at one of the references.
>The variable has to be a subclass of a special Tkinter class
>for this to work, though.[/color]
Comment