Tcl style traces

D

Derek Fountain

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?
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Derek said:
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?

No. However, sys.settrace might come close.

Regards,
Martin
 
A

Andrew Bennetts

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?

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.

-Andrew.
 
M

Miki Tebeka

Hello Derek,
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?
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.

HTH.
Miki
 
D

Derek Fountain

What do you need this for? If you can be more specific, we might be able
to help you a little better.

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)
 
C

Cameron Laird

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)

Newsgroups: comp.lang.python
Subject: Re: Tcl style traces
Summary:
Expires:
References: <[email protected]> <[email protected]> <[email protected]>
Sender:
Followup-To:
Reply-To: (e-mail address removed)
Distribution:
Organization: The Lairds
Keywords:
Cc:

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)

The Pythonic thing to do is to intercept the data store's set or
__setattr__ or ... method.
 
P

Peter Otten

Derek said:
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?

Would it be sufficient to keep track of attribute changes? Then you could
try something like

class TraceChanges(object):
def __init__(self, name, onChange):
# bypass the tracking mechanism
object.__setattr__(self, "name", name)
object.__setattr__(self, "changed", onChange)

def __setattr__(self, name, value):
object.__setattr__(self, name, value)
self.changed(self, name, value)


def changed(sender, name, value):
# could also be a method of TraceChanges
print "%s.%s changed to %s --> updating canvas" % (sender.name, name,
value)


rectangle = TraceChanges("rectangle", changed)
rectangle.color = "blue"

text = TraceChanges("text", changed)
text.color = "red"
text.font = "Helvetica"

Peter
 
J

John Roth

Derek Fountain said:
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)

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.

John Roth
 
C

Cameron Laird

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.
.
.
.
textvariable--but that applies only for a few widgets,
and only for equivalence of the widget content and the
indicated variable; there's no formatting or other pro-
cedural computation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top