rmorshea / spectate Goto Github PK
View Code? Open in Web Editor NEWObserve the evolution of mutable data types like lists, dicts, and sets.
Home Page: https://python-spectate.readthedocs.io/
License: MIT License
Observe the evolution of mutable data types like lists, dicts, and sets.
Home Page: https://python-spectate.readthedocs.io/
License: MIT License
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.
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
Using the rollback()
event handler without an undo function specified is the same as the mute()
event handler:
https://github.com/rmorshea/spectate/blob/master/docs/source/usage/events.rst#rolling-back-events
We should make the undo
function a required parameter.
Once the source code is documented, the next step it so integrate with sphinx and readthedocs.
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.
Empty
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=())
Hey Ryan, would you be interested in having a CI for spectate?
Also, I'm interested in knowing if spectate runs in Windows.
The behavior of rollback
is the same as mute
if no undo=...
param is passed. To avoid confusion in which ought to be used undo
should be made into a required parameter.
Link to rollback method is broken in docs:
https://github.com/rmorshea/spectate/blame/master/docs/source/usage/events.rst#L107
https://github.com/rmorshea/spectate/blame/master/docs/source/usage/events.rst#L82
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! =)
Since the recent shift in file location the API docs are now broken.
Basically we should just be able to copy-past spectate/mvc.py
to spectate/__init__.py
We should also update the documentation to reflect this.
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
We call __init__
here just before Python calls it. Not sure why we do that.
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.
Add tests to the MVC framework so it can be officially supported.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.