GithubHelp home page GithubHelp logo

rmorshea / spectate Goto Github PK

View Code? Open in Web Editor NEW
36.0 36.0 4.0 204 KB

Observe the evolution of mutable data types like lists, dicts, and sets.

Home Page: https://python-spectate.readthedocs.io/

License: MIT License

Python 100.00%

spectate's Introduction

spectate's People

Contributors

agoose77 avatar bollwyvl avatar rmorshea avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

spectate's Issues

Make 1.0 Release

Spectate seems pretty stable and fairly well documented at this point. This is the last chance to make breaking changes for (probably) quite some time.

How to use in class (mimic traitlets)

Hi! I finally decided to have a try with spectate, but I am having trouble migrating from traitlets.

This is what I'd do in traitlets

from traitlets import List, HasTraits, observe
class TestTrait(HasTraits):
    attr = List()
    number = 1
    
    @observe('attr')
    def _ob_attr(self, change):
        print('number is {}'.format(self.number))
        print(change)

But of course this do not work with spectate

from spectate import mvc
class TestSpec(object):
    attr = mvc.List()
    number = 1
    
    @mvc.view(attr)
    def _ob_attr(attr, change):
        # print('number is {}'.format(self.number)) # can't access self
        print(change)

I read this but couldn't find the right path.

Thanks

Improve Documentation

The docs on RTD are pretty lame. It would be great to give a more thorough demonstration of the ways in which spectate can be used. At the moment the reader is expected to understand the possibilities the library opens up.

Likewise I'd like to add tests for the MVC framework so that it can be officially supported and added to the docs as well.

Rework of Oracle

Proposed rework of the Oracle model type that was recently removed due to bugs:

import functools
from spectate import mvc


class MetaOracle(type):
    
    def __init__(self, name, bases, attrs):
        if "_model_capture_types" in attrs:
            class_to_key = functools.cmp_to_key(lambda t1, t2: -1 if issubclass(t1, t2) else 1)
            class_key_from_item = lambda i: class_to_key(i[1])
            self._model_capture_order = tuple(sorted(self._model_capture_types.items(), key=class_key_from_item))


class Oracle(mvc.Object, metaclass=MetaOracle):

    _model_capture_types = {
        "_capture_dict": mvc.Dict,
        "_capture_object": mvc.Object,
        "_capture_list": mvc.List,
    }

    def _capture_model_events(self, model, path=()):
        mvc.view(model)(lambda *args, **kwargs: self._capture(path=path, *args, **kwargs))

    def _capture(self, value, events, path):
        for method, model_type in self._model_capture_order:
            if isinstance(value, model_type):
                super()._notify_model_views(tuple(
                    getattr(self, method)(value, e, path)
                    for e in events
                ))
                break
        else:
            raise TypeError("Oracle has no capture type %r" % value)

    def _capture_object(self, value, event, path):
        path += (event.attr,)
        if isinstance(event.new, mvc.Model):
            self._capture_model_events(event.new, path)
        return event["path": path]

    def _capture_dict(self, value, event, path):
        path += (event.key,)
        if isinstance(event.new, mvc.Model):
            self._capture_model_events(event.new, path)
        return event["path": path]
    
    def _capture_list(self, value, event, path):
        path += (event.index,)
        if isinstance(event.new, mvc.Model):
            self._capture_model_events(event.new, path)
        return event["path": path]
    
    def _capture_set(self, value, event, path):
        # We don't need to capture the contents of a set because
        # anything stored in it must be hashable and thus not mutable
        return event["path": path]

    def _notify_model_views(self, events):
        self._capture(self, events, path=())

CI for spectate

Hey Ryan, would you be interested in having a CI for spectate?

Also, I'm interested in knowing if spectate runs in Windows.

Error code 1 on pip install.

When running

pip install spectate

I get the following error;
t440p-screenshot-8142017-1233 pm

I think this could be fixed pretty easily by uploading with twine.

Create a tag for version releases

Hi @rmorshea I'm using your package in a jupyter widget I'm creating and I need to support it on conda forge.
For that I need to create a feedstock in the conda repository and it's recommended to add a tag to support versioning. The tag should have the same version number as the Pypi package.

It's not really mandatory and I'll make the package anyway, but according to the maintainers it's recommended.

Let me know if I can help with that and if you're interested in being added as a maintainer of the conda feedstock package.

Thanks for the great work! =)

Maintaining after merger into ipywidgets or traitlets.

Hi @rmorshea,

I'm very impressed with you work. I've been wanting something like EventfulDict for building my custom ipywidgets for a while now and this exceeds all expectations. I've read in the of the old traitlets and ipwidgets issues and PRs that there are plans to incorporate these types. All that would be great but it appears like that may take while before it is implemented. So I was planning on using this in the interim and I'd like to know if you plan to keep this repo open and up to date with whatever flavor of merger that ends up going down? If so I'd like to volunteer to help maintain it.

Best,

James

Refactor To Simplify Core Logic

For a while now I've wanted to basically remove all the logic in spectate/core.py. It's certainly clever, but totally unnecessary in hindsight. I think we can get away with a much simpler solution using the new mvc interface. In short, the planned refactor will not change the mvc API and will simply change its underlying implementation.

In the refactor Model classes will be implemented using the notiifier context manager:

from spectate import notifier, Model, Undefined

class Dict(Model, dict):

    def __setitem__(self, key, value):
        with notifier(self) as notify:
            old = self.get(key, Undefined)
            super().__setitem__(key, value)
            notify(old=old, new=new, key=key)

This is far simpler than the whole Control interface used now which is far more complicated.

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.