Tcl style traces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Derek Fountain

    Tcl style traces

    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?
  • Martin v. Löwis

    #2
    Re: Tcl style traces

    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]

    No. However, sys.settrace might come close.

    Regards,
    Martin

    Comment

    • Andrew Bennetts

      #3
      Re: Tcl style traces

      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.

      -Andrew.


      Comment

      • Miki Tebeka

        #4
        Re: Tcl style traces

        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.

        HTH.
        Miki

        Comment

        • Derek Fountain

          #5
          Re: Tcl style traces

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

          Comment

          • Cameron Laird

            #6
            Re: Tcl style traces

            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]

            Newsgroups: comp.lang.pytho n
            Subject: Re: Tcl style traces
            Summary:
            Expires:
            References: <40171be5$0$174 2$5a62ac22@free news.iinet.net. au> <mailman.890.10 75270902.12720. python-list@python.org > <40177f07$0$173 0$5a62ac22@free news.iinet.net. au>
            Sender:
            Followup-To:
            Reply-To: claird@phaseit. net
            Distribution:
            Organization: The Lairds
            Keywords:
            Cc:

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

            Cameron Laird <claird@phaseit .net>
            Business: http://www.Phaseit.net

            Comment

            • Peter Otten

              #7
              Re: Tcl style traces

              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 __setattr__(sel f, name, value):
              object.__setatt r__(self, name, value)
              self.changed(se lf, 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("r ectangle", changed)
              rectangle.color = "blue"

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

              Peter

              Comment

              • John Roth

                #8
                Re: Tcl style traces


                "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.

                John Roth


                Comment

                • Cameron Laird

                  #9
                  Re: Tcl style traces

                  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

                  Working...