Giter Site home page Giter Site logo

Tracking Element Changes about traitlets HOT 4 CLOSED

ipython avatar ipython commented on August 21, 2024
Tracking Element Changes

from traitlets.

Comments (4)

SylvainCorlay avatar SylvainCorlay commented on August 21, 2024

Well, we migrated to the new observe API with custom event types in order to support the type of approach that is proposed in #278.

from traitlets.

rmorshea avatar rmorshea commented on August 21, 2024

@SylvainCorlay, this idea of a WatchedType wouldn't be a full replacement for your pr, but would instead be an alternative to #278's edict where each of the required methods was rewritten in order to catch changes. I've actually written an example that describes an "eventful dict" using WatchedType.

from traitlets.

rmorshea avatar rmorshea commented on August 21, 2024

@SylvainCorlay, I'll soon push a commit that allows for this kind of work flow (I'll post on your main thread when I'm do):

EventfulList = watched_type("EventfulList", list, '__setitem__')

def pass_on_args(inst, call):
    return call.args

def print_element_change(inst, answer):
    # answer.before = pass_on_args(call)
    index, old = answer.before
    new = inst[index]
    if new != old:
        print("{%s: %s} -> {%s, %s}" % (index, old, index, inst[index]))

elist = EventfulList([1, 2, 3])
elist.notify_before(__setitem__=pass_on_args)
elist.notify_after(__setitem__=print_element_change)

elist[0] = 1
# No print
elist[0] = -1
# PRINTS: '{0: 1} -> {0: -1}'

This represents a generic way to establish callbacks for any method on any type of instance.

I think it is a very powerful, and useful callback tool that avoids the need for hard coded classes like edict or elist.

While this does not replace an EventfulDict trait type, it would be the internal foundation of one.

from traitlets.

rmorshea avatar rmorshea commented on August 21, 2024

After #317 is merged, this could be a working example:

class HasEvents(HasTraits):

    elist = Eventful(List(), '__setitem__')

    @event(on='elist.__setitem__')
    def elist_set_callbacks(self):
        def before(inst, call):
            m = "set from %s to %s at list index %s"
            print(m % (inst[call.args[0]], call.args[1], call.args[0]))
        return (before, None)

he = HasEvents()
he.elist = [1]
he.elist[0] = 2

Prints: set from 1 to 2 at list index 0

from traitlets.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.