Skip to content

Commit a872eb0

Browse files
committed
Add keyword constructor for Models
1 parent 970a7cc commit a872eb0

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# Contributing to LTK
22

3-
If you found a bug with LTK, please [open an issue](https://github.com/laffra/ltk/issues/new).
3+
If you found a bug with LTK, please [open an issue](https://github.com/pyscript/ltk/issues/new).
44

55
If you have an idea on making LTK better, please send a Pull Request.
66

7-
Build something with LTK and add your app to the [list of applications built with LTK](https://github.com/laffra/ltk/blob/main/README.md#applications-built-using-ltk).
7+
Build something with LTK and add your app to the [list of applications built with LTK](https://github.com/pyscript/ltk/blob/main/README.md#applications-built-using-ltk).
88

9-
Come watch a talk by [Chris Laffra](https://chrislaffra.com) on LTK at [PyData Eindhoven](https://eindhoven2023.pydata.org/pydata/talk/RJVRMT/) on November 30, 2023.

ltk/widgets.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,20 @@ def _set_value(self, value):
423423
class Model():
424424
""" A model that can be bound to a widget """
425425

426-
def __init__(self):
427-
for name, value in self.__class__.__dict__.items():
428-
if not name.startswith("__") and not callable(value):
429-
object.__setattr__(self, name,
430-
ModelAttribute(self, name, getattr(self.__class__, name))
431-
)
426+
def __init__(self, **kwargs):
427+
fields = [
428+
name
429+
for name,value in self.__class__.__dict__.items()
430+
if not name.startswith("__") and not callable(value)
431+
]
432+
for name in fields:
433+
object.__setattr__(self, name,
434+
ModelAttribute(self, name, getattr(self.__class__, name))
435+
)
436+
for name, value in kwargs.items():
437+
if not name in fields:
438+
raise ValueError(f"Argument '{name}' not found in {fields} for {self.__class__.__name__}")
439+
object.__setattr__(self, name, value)
432440

433441
def __setattr__(self, name: str, value):
434442
if hasattr(self, name) and isinstance(getattr(self, name), ModelAttribute):
@@ -460,22 +468,29 @@ def encode(self):
460468
def changed(self, name, value):
461469
""" Called when an attribute of the model has changed """
462470

471+
def __repr__(self):
472+
fields = ', '.join(
473+
f'{name}={repr(value)}'
474+
for name, value in self.__dict__.items()
475+
if not name.startswith("_") and not callable(value)
476+
)
477+
return f"{self.__class__.__name__}({fields})"
463478

464479
class LocalStorageModel(Model):
465480
""" A model that is stored in the browser's local storage """
466481

467482
__store = window.localStorage
468483

469-
def __init__(self, key=None):
484+
def __init__(self, _key=None, **kwargs):
470485
Model.__init__(self)
471-
key = key or f"{self.__class__.__name__}-{get_time()}"
472-
self.decode(self.__store.getItem(key))
473-
self.key = key
486+
_key = _key or f"{self.__class__.__name__}-{get_time()}"
487+
self.decode(self.__store.getItem(_key))
488+
self._key = _key
474489

475490
def changed(self, name, value):
476491
""" Called when an attribute of the model has changed """
477-
if hasattr(self, "key"):
478-
self.__store.setItem(self.key, self.encode())
492+
if hasattr(self, "_key"):
493+
self.__store.setItem(self._key, self.encode())
479494

480495
@classmethod
481496
def load(cls):

0 commit comments

Comments
 (0)