From f9faae2038627aa846dd6e0cd8e6d4196ad5af5e Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Wed, 13 Nov 2024 04:09:07 +0000 Subject: [PATCH 01/25] improved docs --- .gitignore | 1 + param/parameterized.py | 38 ++++++++++++++++++++++++++++++++++++++ param/reactive.py | 39 +++++++++++++++++++++++++++++++++++---- 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index fc5adc44..88d3bb41 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,4 @@ benchmarks/param # pixi .pixi pixi.lock +script.* diff --git a/param/parameterized.py b/param/parameterized.py index 2576659d..f49a205b 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -1320,6 +1320,44 @@ def schema(self, safe=False, subset=None, mode='json'): @property def rx(self): + """The reactive namespace. + + Provides reactive versions of the operations that cannot be made reactive through overloading, such as + `.rx.and_` and `.rx.bool`. Call it (`()`) to obtain a reactive expression. + + Reference: https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx + + Examples: + + Turn your parameter into a reactive expression: + + ```python + import param + + class MyClass(param.Parameterized): + value = param.Parameter() + + my_instance = MyClass(value=0) + ``` + + Get the current value: + + ```python + a = my_instance.rx.value + ``` + + Set the current value: + + ```python + my_instance.rx.value = 1 + ``` + + Call it to get a reactive expression: + + ```python + rx_value = my_instance.rx() + ``` + """ from .reactive import reactive_ops return reactive_ops(self) diff --git a/param/reactive.py b/param/reactive.py index 00e9c06e..296474f3 100644 --- a/param/reactive.py +++ b/param/reactive.py @@ -180,11 +180,42 @@ class NestedResolver(Resolver): class reactive_ops: - """ - Namespace for reactive operators. + """The reactive namespace. + + Provides reactive versions of the operations that cannot be made reactive through overloading, such as + `.rx.and_` and `.rx.bool`. Call it (`()`) to obtain a reactive expression. + + Reference: https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx + + Examples: + + Turn your parameter into a reactive expression: + + ```python + import param + + class MyClass(param.Parameterized): + value = param.Parameter() + + my_instance = MyClass(value=0) + ``` + + Get the current value: + + ```python + a = my_instance.rx.value + ``` + + Set the current value: + + ```python + my_instance.rx.value = 1 + ``` + + Call it to get a reactive expression: - Implements operators that cannot be implemented using regular - Python syntax. + ```python + rx_value = my_instance.rx() """ def __init__(self, reactive): From 563ea82ba0001286fd0c83f47fdd6f80f859025e Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Wed, 13 Nov 2024 04:12:53 +0000 Subject: [PATCH 02/25] pre-commit --- param/parameterized.py | 2 +- param/reactive.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/param/parameterized.py b/param/parameterized.py index f49a205b..fb700d95 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -1321,7 +1321,7 @@ def schema(self, safe=False, subset=None, mode='json'): @property def rx(self): """The reactive namespace. - + Provides reactive versions of the operations that cannot be made reactive through overloading, such as `.rx.and_` and `.rx.bool`. Call it (`()`) to obtain a reactive expression. diff --git a/param/reactive.py b/param/reactive.py index 296474f3..eae8cbef 100644 --- a/param/reactive.py +++ b/param/reactive.py @@ -181,7 +181,7 @@ class NestedResolver(Resolver): class reactive_ops: """The reactive namespace. - + Provides reactive versions of the operations that cannot be made reactive through overloading, such as `.rx.and_` and `.rx.bool`. Call it (`()`) to obtain a reactive expression. From c5f6167eb1318384c4ac9a3424d967da4ad25648 Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Wed, 13 Nov 2024 04:42:45 +0000 Subject: [PATCH 03/25] document .param namespace --- param/parameterized.py | 31 +++++++++++++++++++++++++++++ param/reactive.py | 45 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/param/parameterized.py b/param/parameterized.py index fb700d95..26c18cc9 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -4244,6 +4244,37 @@ def __init__(self, **params): @property def param(self): + """ + The `.param` namespace for Parameterized classes and instances. + + This namespace provides access to powerful methods and properties for managing parameters in a `Parameterized` object. + It includes utilities for adding parameters, updating parameters, debugging, serialization, logging, and more. + + Reference: https://param.holoviz.org/user_guide/Parameters.html#parameter-objects-and-instances + + Example: + + ```python + import param + + class MyClass(param.Parameterized): + value = param.Parameter() + + my_instance = MyClass(value=0) + ``` + + Access the `value` parameter of `my_instance`: + + ```python + my_instance.param.value # the Parameter instance + ``` + + Note this is different from the current `value` of `my_instance`: + + ```python + my_instance.value # 0, the current parameter value + ``` + """ return Parameters(self.__class__, self=self) #PARAM3_DEPRECATION diff --git a/param/reactive.py b/param/reactive.py index eae8cbef..bb4e0443 100644 --- a/param/reactive.py +++ b/param/reactive.py @@ -225,6 +225,7 @@ def _as_rx(self): return self._reactive if isinstance(self._reactive, rx) else self() def __call__(self): + """Creates a reactive expression.""" rxi = self._reactive return rxi if isinstance(rx, rx) else rx(rxi) @@ -786,7 +787,7 @@ def __init__( ] self._setup_invalidations(depth) self._kwargs = kwargs - self.rx = reactive_ops(self) + self._rx = reactive_ops(self) self._init = True for name, accessor in _display_accessors.items(): setattr(self, name, accessor(self)) @@ -794,6 +795,48 @@ def __init__( if predicate is None or predicate(self._current): setattr(self, name, accessor(self)) + @property + def rx(self): + """The reactive namespace. + + Provides reactive versions of the operations that cannot be made reactive through overloading, such as + `.rx.and_` and `.rx.bool`. Call it (`()`) to obtain a reactive expression. + + Reference: https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx + + Examples: + + Turn your parameter into a reactive expression: + + ```python + import param + + class MyClass(param.Parameterized): + value = param.Parameter() + + my_instance = MyClass(value=0) + ``` + + Get the current value: + + ```python + a = my_instance.rx.value + ``` + + Set the current value: + + ```python + my_instance.rx.value = 1 + ``` + + Call it to get a reactive expression: + + ```python + rx_value = my_instance.rx() + ``` + """ + return self._rx + @property def _obj(self): if self._shared_obj is None: From ed229c32e12b63756c4988638555510bc414fc77 Mon Sep 17 00:00:00 2001 From: MarcSkovMadsen Date: Wed, 13 Nov 2024 06:26:22 +0000 Subject: [PATCH 04/25] fix --- param/parameterized.py | 157 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 149 insertions(+), 8 deletions(-) diff --git a/param/parameterized.py b/param/parameterized.py index 26c18cc9..c2a03604 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -2298,12 +2298,50 @@ def set_default(self_,param_name,value): cls = self_.cls setattr(cls,param_name,value) - def add_parameter(self_, param_name, param_obj): + def add_parameter(self_, param_name: str, param_obj: Parameter): """ Add a new Parameter object into this object's class. Should result in a Parameter equivalent to one declared in the class's source code. + + Parameters + ---------- + param_name : str + The name of the parameter to add. + param_obj : Parameter + The Parameter object to add. + + Example + ------- + + ```python + import param + + + class P(param.Parameterized): + a = param.Number() + b = param.String() + + + p = P() + ``` + + Add a new parameter to the class via the class: + + ```python + P.param.add_parameter('c', param.Tuple(default=(1,2,3))) + print(p.c) + # (1, 2, 3) + ``` + + Add a new parameter to the class via the instance: + + ```python + p.param.add_parameter('d', param.Tuple(default=(3,2,1))) + print(p.d) + # (3, 2, 1) + ``` """ # Could have just done setattr(cls,param_name,param_obj), # which is supported by the metaclass's __setattr__ , but @@ -2347,12 +2385,46 @@ def params(self_, parameter_name=None): def update(self_, arg=Undefined, /, **kwargs): """ - For the given dictionary or iterable or set of param=value - keyword arguments, sets the corresponding parameter of this - object or class to the given value. + Updates one or more parameters of this object or class. + + This method allows you to set the parameters of the object or class using a dictionary, + an iterable, or keyword arguments in the form of param=value. The specified parameters + will be updated to the given values. + + This method can also be used as a context manager to temporarily set and then reset + parameter values. + + Reference: https://param.holoviz.org/user_guide/Parameters.html#other-parameterized-methods + + Examples: + + ```python + import param + + class P(param.Parameterized): + a = param.Number() + b = param.String() + + p = P() + ``` + + Update parameters permanently: + + ```python + p.param.update(a=1, b="Hello") + print(p.a, p.b) + # Output: 1 Hello + ``` - May also be used as a context manager to temporarily set and - then reset parameter values. + Update parameters temporarily: + + ```python + with p.param.update(a=2, b="World"): + print(p.a, p.b) + # Output: 2 World + print(p.a, p.b) + # Output: 1 Hello + ``` """ refs = {} if self_.self is not None: @@ -2635,7 +2707,45 @@ class or instance that contains an iterable collection of for obj in sublist: obj.param.set_dynamic_time_fn(time_fn,sublistattr) - def serialize_parameters(self_, subset=None, mode='json'): + def serialize_parameters(self_, subset=None, mode='json')->str: + """Returns the serialized parameters of the Parameterized object. + + Parameters + ---------- + subset (list, optional): + A list of parameter names to serialize. If None, all parameters will be serialized. Defaults to None. + mode (str, optional): + The serialization format. By default, only 'json' is supported. Defaults to 'json'. + + Raises: + ValueError: If the `mode` is not valid. + + Returns + ------- + dict: The serialized string + + Reference + --------- + For more details visit https://param.holoviz.org/user_guide/Serialization_and_Persistence.html#serializing-with-json + + Example + ------- + + ```python + import param + + class P(param.Parameterized): + a = param.Number() + b = param.String() + + + p = P(a=1, b="hello") + + serialized_data = p.param.serialize_parameters() + print(type(serialized_data)) + # {"name": "P00002", "a": 1, "b": "hello"} + ``` + """ self_or_cls = self_.self_or_cls if mode not in Parameter._serializers: raise ValueError(f'Mode {mode!r} not in available serialization formats {list(Parameter._serializers.keys())!r}') @@ -2649,7 +2759,38 @@ def serialize_value(self_, pname, mode='json'): serializer = Parameter._serializers[mode] return serializer.serialize_parameter_value(self_or_cls, pname) - def deserialize_parameters(self_, serialization, subset=None, mode='json'): + def deserialize_parameters(self_, serialization, subset=None, mode='json') -> dict: + """ + Deserialize the given serialized data. You may use the deserialized to create a Parameteried object or + update the parameters of a Parameterized object. + + Parameters: + - serialization (str): The serialized parameter data as a JSON string. + - subset (list, optional): A list of parameter names to deserialize. If None, all parameters will be deserialized. Defaults to None. + - mode (str, optional): The serialization format. By default, only 'json' is supported. Defaults to 'json'. + + Returns: + dict: A dictionary with parameter names as keys and deserialized values. + + Reference: https://param.holoviz.org/user_guide/Serialization_and_Persistence.html#serializing-with-json + + Example: + + ```python + import param + + class P(param.Parameterized): + a = param.Number() + b = param.String() + + serialized_data = '{"a": 1, "b": "hello"}' + deserialized_data = P.param.deserialize_parameters(serialized_data) + print(deserialized_data) + # Output: {'a': 1, 'b': 'hello'} + + instance = P(**deserialized_data) + ``` + """ self_or_cls = self_.self_or_cls serializer = Parameter._serializers[mode] return serializer.deserialize_parameters(self_or_cls, serialization, subset=subset) From 896f94d1a00c0ed0d25395c9fd361bf1fa6abe1e Mon Sep 17 00:00:00 2001 From: MarcSkovMadsen Date: Wed, 13 Nov 2024 06:34:16 +0000 Subject: [PATCH 05/25] align rx docstring --- param/reactive.py | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/param/reactive.py b/param/reactive.py index bb4e0443..998daabc 100644 --- a/param/reactive.py +++ b/param/reactive.py @@ -180,7 +180,7 @@ class NestedResolver(Resolver): class reactive_ops: - """The reactive namespace. + """The reactive namespace. Provides reactive versions of the operations that cannot be made reactive through overloading, such as `.rx.and_` and `.rx.bool`. Call it (`()`) to obtain a reactive expression. @@ -189,33 +189,30 @@ class reactive_ops: Examples: - Turn your parameter into a reactive expression: + Lets create a Parameterized instance: ```python import param - class MyClass(param.Parameterized): - value = param.Parameter() + class P(param.Parameterized): + a = param.Number() + b = param.String() - my_instance = MyClass(value=0) - ``` - - Get the current value: - ```python - a = my_instance.rx.value + p = P(a=1, b="hello") ``` - Set the current value: + Get the current value: ```python - my_instance.rx.value = 1 + a = p.param.a.rx.value ``` Call it to get a reactive expression: ```python - rx_value = my_instance.rx() + rx_value = p.param.a.rx() + ``` """ def __init__(self, reactive): @@ -806,33 +803,29 @@ def rx(self): Examples: - Turn your parameter into a reactive expression: + Lets create a Parameterized instance: ```python import param - class MyClass(param.Parameterized): - value = param.Parameter() + class P(param.Parameterized): + a = param.Number() + b = param.String() - my_instance = MyClass(value=0) - ``` - - Get the current value: - ```python - a = my_instance.rx.value + p = P(a=1, b="hello") ``` - Set the current value: + Get the current value: ```python - my_instance.rx.value = 1 + a = p.param.a.rx.value ``` Call it to get a reactive expression: ```python - rx_value = my_instance.rx() + rx_value = p.param.a.rx() ``` """ return self._rx From 755496dfff877b183df38b3a2bb4f2653d884497 Mon Sep 17 00:00:00 2001 From: MarcSkovMadsen Date: Wed, 13 Nov 2024 06:36:51 +0000 Subject: [PATCH 06/25] add type --- param/reactive.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/param/reactive.py b/param/reactive.py index 998daabc..a31fa6a5 100644 --- a/param/reactive.py +++ b/param/reactive.py @@ -793,7 +793,7 @@ def __init__( setattr(self, name, accessor(self)) @property - def rx(self): + def rx(self) -> reactive_ops: """The reactive namespace. Provides reactive versions of the operations that cannot be made reactive through overloading, such as From b712cc1930cb8062fa0494a86cd206464936ceb4 Mon Sep 17 00:00:00 2001 From: MarcSkovMadsen Date: Wed, 13 Nov 2024 06:40:23 +0000 Subject: [PATCH 07/25] align rx docstring --- param/parameterized.py | 10 +++++----- param/reactive.py | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/param/parameterized.py b/param/parameterized.py index c2a03604..dd128f0c 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -2386,16 +2386,16 @@ def params(self_, parameter_name=None): def update(self_, arg=Undefined, /, **kwargs): """ Updates one or more parameters of this object or class. - - This method allows you to set the parameters of the object or class using a dictionary, - an iterable, or keyword arguments in the form of param=value. The specified parameters + + This method allows you to set the parameters of the object or class using a dictionary, + an iterable, or keyword arguments in the form of param=value. The specified parameters will be updated to the given values. - This method can also be used as a context manager to temporarily set and then reset + This method can also be used as a context manager to temporarily set and then reset parameter values. Reference: https://param.holoviz.org/user_guide/Parameters.html#other-parameterized-methods - + Examples: ```python diff --git a/param/reactive.py b/param/reactive.py index a31fa6a5..3920ef1b 100644 --- a/param/reactive.py +++ b/param/reactive.py @@ -180,7 +180,7 @@ class NestedResolver(Resolver): class reactive_ops: - """The reactive namespace. + """The reactive namespace. Provides reactive versions of the operations that cannot be made reactive through overloading, such as `.rx.and_` and `.rx.bool`. Call it (`()`) to obtain a reactive expression. From 25733d2d697b040b302cc89449ff63d2ca02856c Mon Sep 17 00:00:00 2001 From: MarcSkovMadsen Date: Wed, 13 Nov 2024 06:44:09 +0000 Subject: [PATCH 08/25] align docstrings --- param/parameterized.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/param/parameterized.py b/param/parameterized.py index dd128f0c..439fe211 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -1329,33 +1329,29 @@ def rx(self): Examples: - Turn your parameter into a reactive expression: + Lets create a Parameterized instance: ```python import param - class MyClass(param.Parameterized): - value = param.Parameter() - - my_instance = MyClass(value=0) - ``` + class P(param.Parameterized): + a = param.Number() + b = param.String() - Get the current value: - ```python - a = my_instance.rx.value + p = P(a=1, b="hello") ``` - Set the current value: + Get the current value: ```python - my_instance.rx.value = 1 + a = p.param.a.rx.value ``` Call it to get a reactive expression: ```python - rx_value = my_instance.rx() + rx_value = p.param.a.rx() ``` """ from .reactive import reactive_ops From 65ae984cbfacb4eca6c03625b05656cfb69ecaf0 Mon Sep 17 00:00:00 2001 From: MarcSkovMadsen Date: Wed, 13 Nov 2024 14:48:52 +0000 Subject: [PATCH 09/25] fix --- .gitignore | 1 - param/parameterized.py | 3 ++- param/reactive.py | 6 ++++-- tests/testreactive.py | 9 +++++++-- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 88d3bb41..fc5adc44 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,3 @@ benchmarks/param # pixi .pixi pixi.lock -script.* diff --git a/param/parameterized.py b/param/parameterized.py index 439fe211..190b77b5 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -1320,7 +1320,8 @@ def schema(self, safe=False, subset=None, mode='json'): @property def rx(self): - """The reactive namespace. + """ + The reactive namespace. Provides reactive versions of the operations that cannot be made reactive through overloading, such as `.rx.and_` and `.rx.bool`. Call it (`()`) to obtain a reactive expression. diff --git a/param/reactive.py b/param/reactive.py index 3920ef1b..d593173c 100644 --- a/param/reactive.py +++ b/param/reactive.py @@ -180,7 +180,8 @@ class NestedResolver(Resolver): class reactive_ops: - """The reactive namespace. + """ + The reactive namespace. Provides reactive versions of the operations that cannot be made reactive through overloading, such as `.rx.and_` and `.rx.bool`. Call it (`()`) to obtain a reactive expression. @@ -794,7 +795,8 @@ def __init__( @property def rx(self) -> reactive_ops: - """The reactive namespace. + """ + The reactive namespace. Provides reactive versions of the operations that cannot be made reactive through overloading, such as `.rx.and_` and `.rx.bool`. Call it (`()`) to obtain a reactive expression. diff --git a/tests/testreactive.py b/tests/testreactive.py index 68e6b9ee..97a4b6ca 100644 --- a/tests/testreactive.py +++ b/tests/testreactive.py @@ -4,6 +4,7 @@ import os import unittest import time +from textwrap import dedent try: import numpy as np @@ -24,8 +25,8 @@ import param import pytest -from param.parameterized import Skip -from param.reactive import bind, rx +from param.parameterized import Skip, Parameter +from param.reactive import bind, rx, reactive_ops from .utils import async_wait_until @@ -767,3 +768,7 @@ def test_reactive_callback_resolve_accessor(): dfx = rx(df) out = dfx["name"].str._callback() assert out is df["name"].str + +def test_docstrings_in_sync(): + assert dedent(reactive_ops.__doc__) == dedent(Parameter.rx.__doc__) + assert dedent(reactive_ops.__doc__) == dedent(rx.rx.__doc__) From 6a7be7033dbd4f45d776dbd3758bc1c58659c2a7 Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Wed, 13 Nov 2024 18:39:45 +0100 Subject: [PATCH 10/25] Update tests/testreactive.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Simon Høxbro Hansen --- tests/testreactive.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testreactive.py b/tests/testreactive.py index 97a4b6ca..a995484a 100644 --- a/tests/testreactive.py +++ b/tests/testreactive.py @@ -770,5 +770,6 @@ def test_reactive_callback_resolve_accessor(): assert out is df["name"].str def test_docstrings_in_sync(): + # The docstring needs to be explicitly written to work with LSP. assert dedent(reactive_ops.__doc__) == dedent(Parameter.rx.__doc__) assert dedent(reactive_ops.__doc__) == dedent(rx.rx.__doc__) From edda46b87429e98d67c346d190d0b32ab73e602c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20H=C3=B8xbro=20Hansen?= Date: Wed, 13 Nov 2024 21:29:54 +0100 Subject: [PATCH 11/25] Fix lint --- tests/testreactive.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testreactive.py b/tests/testreactive.py index a995484a..afe08a1b 100644 --- a/tests/testreactive.py +++ b/tests/testreactive.py @@ -770,6 +770,6 @@ def test_reactive_callback_resolve_accessor(): assert out is df["name"].str def test_docstrings_in_sync(): - # The docstring needs to be explicitly written to work with LSP. + # The docstring needs to be explicitly written to work with LSP. assert dedent(reactive_ops.__doc__) == dedent(Parameter.rx.__doc__) assert dedent(reactive_ops.__doc__) == dedent(rx.rx.__doc__) From ecbd92b3b5baf9c4235cabc63f2c4ac70c1e1499 Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Thu, 14 Nov 2024 20:26:07 +0100 Subject: [PATCH 12/25] Update param/parameterized.py Co-authored-by: Maxime Liquet <35924738+maximlt@users.noreply.github.com> --- param/parameterized.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/param/parameterized.py b/param/parameterized.py index 190b77b5..39cc4fe8 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -2393,7 +2393,8 @@ def update(self_, arg=Undefined, /, **kwargs): Reference: https://param.holoviz.org/user_guide/Parameters.html#other-parameterized-methods - Examples: + Examples + -------- ```python import param From 3a17a10f33961b44443dd134e5b8462625af8fc7 Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Thu, 14 Nov 2024 20:28:11 +0100 Subject: [PATCH 13/25] Update param/parameterized.py Co-authored-by: Maxime Liquet <35924738+maximlt@users.noreply.github.com> --- param/parameterized.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/param/parameterized.py b/param/parameterized.py index 39cc4fe8..765f01b7 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -2309,8 +2309,8 @@ def add_parameter(self_, param_name: str, param_obj: Parameter): param_obj : Parameter The Parameter object to add. - Example - ------- + Examples + -------- ```python import param From b55ef26876b2ada6a3dd00af01b8ec3cd7ca6408 Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Thu, 14 Nov 2024 20:28:21 +0100 Subject: [PATCH 14/25] Update param/parameterized.py Co-authored-by: Maxime Liquet <35924738+maximlt@users.noreply.github.com> --- param/parameterized.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/param/parameterized.py b/param/parameterized.py index 765f01b7..a2aecdba 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -2710,7 +2710,7 @@ def serialize_parameters(self_, subset=None, mode='json')->str: Parameters ---------- - subset (list, optional): + subset: list, optional A list of parameter names to serialize. If None, all parameters will be serialized. Defaults to None. mode (str, optional): The serialization format. By default, only 'json' is supported. Defaults to 'json'. From 173ff9c58f1f7804c319781fc432b79822221ca3 Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Thu, 14 Nov 2024 20:31:28 +0100 Subject: [PATCH 15/25] Update param/reactive.py Co-authored-by: Philipp Rudiger --- param/reactive.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/param/reactive.py b/param/reactive.py index d593173c..3689d614 100644 --- a/param/reactive.py +++ b/param/reactive.py @@ -803,7 +803,8 @@ def rx(self) -> reactive_ops: Reference: https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx - Examples: + Examples + -------- Lets create a Parameterized instance: From f7c92da93d169e029b24d06caab356cf9f55d316 Mon Sep 17 00:00:00 2001 From: MarcSkovMadsen Date: Thu, 14 Nov 2024 19:34:08 +0000 Subject: [PATCH 16/25] review feedback --- param/parameterized.py | 110 +++++++++++++++++++++-------------------- param/reactive.py | 4 +- 2 files changed, 59 insertions(+), 55 deletions(-) diff --git a/param/parameterized.py b/param/parameterized.py index 190b77b5..9962b06b 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -1326,7 +1326,7 @@ def rx(self): Provides reactive versions of the operations that cannot be made reactive through overloading, such as `.rx.and_` and `.rx.bool`. Call it (`()`) to obtain a reactive expression. - Reference: https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx + User Guide: https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx Examples: @@ -2391,7 +2391,7 @@ def update(self_, arg=Undefined, /, **kwargs): This method can also be used as a context manager to temporarily set and then reset parameter values. - Reference: https://param.holoviz.org/user_guide/Parameters.html#other-parameterized-methods + User Guide: https://param.holoviz.org/user_guide/Parameters.html#other-parameterized-methods Examples: @@ -2704,7 +2704,7 @@ class or instance that contains an iterable collection of for obj in sublist: obj.param.set_dynamic_time_fn(time_fn,sublistattr) - def serialize_parameters(self_, subset=None, mode='json')->str: + def serialize_parameters(self_, subset=None, mode='json'): """Returns the serialized parameters of the Parameterized object. Parameters @@ -2714,12 +2714,9 @@ def serialize_parameters(self_, subset=None, mode='json')->str: mode (str, optional): The serialization format. By default, only 'json' is supported. Defaults to 'json'. - Raises: - ValueError: If the `mode` is not valid. - Returns ------- - dict: The serialized string + Any: The serialized value Reference --------- @@ -2758,35 +2755,42 @@ def serialize_value(self_, pname, mode='json'): def deserialize_parameters(self_, serialization, subset=None, mode='json') -> dict: """ - Deserialize the given serialized data. You may use the deserialized to create a Parameteried object or - update the parameters of a Parameterized object. - - Parameters: - - serialization (str): The serialized parameter data as a JSON string. - - subset (list, optional): A list of parameter names to deserialize. If None, all parameters will be deserialized. Defaults to None. - - mode (str, optional): The serialization format. By default, only 'json' is supported. Defaults to 'json'. - - Returns: - dict: A dictionary with parameter names as keys and deserialized values. - - Reference: https://param.holoviz.org/user_guide/Serialization_and_Persistence.html#serializing-with-json - - Example: - - ```python - import param + Deserialize the given serialized data. This data can be used to create a + `Parameterized` object or update the parameters of an existing `Parameterized` object. - class P(param.Parameterized): - a = param.Number() - b = param.String() + Parameters + ---------- + serialization : str + The serialized parameter data as a JSON string. + subset : list of str, optional + A list of parameter names to deserialize. If `None`, all parameters will be + deserialized. Defaults to `None`. + mode : str, optional + The serialization format. By default, only 'json' is supported. + Defaults to 'json'. - serialized_data = '{"a": 1, "b": "hello"}' - deserialized_data = P.param.deserialize_parameters(serialized_data) - print(deserialized_data) - # Output: {'a': 1, 'b': 'hello'} + Returns + ------- + dict + A dictionary with parameter names as keys and deserialized values. - instance = P(**deserialized_data) - ``` + References + ---------- + For more details on parameter serialization, see: + https://param.holoviz.org/user_guide/Serialization_and_Persistence.html#serializing-with-json + + Examples + -------- + >>> import param + >>> class P(param.Parameterized): + ... a = param.Number() + ... b = param.String() + ... + >>> serialized_data = '{"a": 1, "b": "hello"}' + >>> deserialized_data = P.param.deserialize_parameters(serialized_data) + >>> print(deserialized_data) + {'a': 1, 'b': 'hello'} + >>> instance = P(**deserialized_data) """ self_or_cls = self_.self_or_cls serializer = Parameter._serializers[mode] @@ -4383,35 +4387,35 @@ def __init__(self, **params): @property def param(self): """ - The `.param` namespace for Parameterized classes and instances. + The `.param` namespace for `Parameterized` classes and instances. - This namespace provides access to powerful methods and properties for managing parameters in a `Parameterized` object. - It includes utilities for adding parameters, updating parameters, debugging, serialization, logging, and more. + This namespace provides access to powerful methods and properties for managing + parameters in a `Parameterized` object. It includes utilities for adding parameters, + updating parameters, debugging, serialization, logging, and more. - Reference: https://param.holoviz.org/user_guide/Parameters.html#parameter-objects-and-instances - - Example: - - ```python - import param + User Guide + ---------- + For more details on parameter objects and instances, see: + https://param.holoviz.org/user_guide/Parameters.html#parameter-objects-and-instances - class MyClass(param.Parameterized): - value = param.Parameter() + Examples + -------- + Basic usage of `.param` in a `Parameterized` class: - my_instance = MyClass(value=0) - ``` + >>> import param + >>> + >>> class MyClass(param.Parameterized): + ... value = param.Parameter() + >>> + >>> my_instance = MyClass(value=0) Access the `value` parameter of `my_instance`: - ```python - my_instance.param.value # the Parameter instance - ``` + >>> my_instance.param.value # the Parameter instance - Note this is different from the current `value` of `my_instance`: + Note that this is different from the current `value` of `my_instance`: - ```python - my_instance.value # 0, the current parameter value - ``` + >>> my_instance.value # 0, the current parameter value """ return Parameters(self.__class__, self=self) diff --git a/param/reactive.py b/param/reactive.py index d593173c..8f2c752b 100644 --- a/param/reactive.py +++ b/param/reactive.py @@ -186,7 +186,7 @@ class reactive_ops: Provides reactive versions of the operations that cannot be made reactive through overloading, such as `.rx.and_` and `.rx.bool`. Call it (`()`) to obtain a reactive expression. - Reference: https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx + User Guide: https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx Examples: @@ -801,7 +801,7 @@ def rx(self) -> reactive_ops: Provides reactive versions of the operations that cannot be made reactive through overloading, such as `.rx.and_` and `.rx.bool`. Call it (`()`) to obtain a reactive expression. - Reference: https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx + User Guide: https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx Examples: From 2d5b83215a58d8441424da82a0cb84bec7c3cce8 Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Thu, 14 Nov 2024 20:48:49 +0100 Subject: [PATCH 17/25] Update param/parameterized.py Co-authored-by: Maxime Liquet <35924738+maximlt@users.noreply.github.com> --- param/parameterized.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/param/parameterized.py b/param/parameterized.py index d5c8f411..a262ee48 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -1328,7 +1328,8 @@ def rx(self): User Guide: https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx - Examples: + Examples + -------- Lets create a Parameterized instance: From 63f96dfc0a53b18db9ebcf0ea006ab9d66ba264e Mon Sep 17 00:00:00 2001 From: MarcSkovMadsen Date: Thu, 14 Nov 2024 19:58:28 +0000 Subject: [PATCH 18/25] review feedback --- param/parameterized.py | 180 +++++++++++++++++++---------------------- param/reactive.py | 86 ++++++++++---------- 2 files changed, 130 insertions(+), 136 deletions(-) diff --git a/param/parameterized.py b/param/parameterized.py index a262ee48..1efa0e8d 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -1323,38 +1323,39 @@ def rx(self): """ The reactive namespace. - Provides reactive versions of the operations that cannot be made reactive through overloading, such as - `.rx.and_` and `.rx.bool`. Call it (`()`) to obtain a reactive expression. + Provides reactive versions of operations that cannot be made reactive through operator overloading, such as + `.rx.and_` and `.rx.bool`. Calling this namespace (`()`) returns a reactive expression. - User Guide: https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx - - Examples - -------- - - Lets create a Parameterized instance: + Parameters + ---------- + None - ```python - import param + Returns + ------- + Reactive expression + The result of calling the reactive namespace is a reactive expression. - class P(param.Parameterized): - a = param.Number() - b = param.String() + User Guide + ---------- + https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx + Examples + -------- + Create a Parameterized instance: - p = P(a=1, b="hello") - ``` + >>> import param + >>> class P(param.Parameterized): + >>> a = param.Number() + >>> b = param.String() + >>> p = P(a=1, b="hello") Get the current value: - ```python - a = p.param.a.rx.value - ``` + >>> a = p.param.a.rx.value Call it to get a reactive expression: - ```python - rx_value = p.param.a.rx() - ``` + >>> rx_value = p.param.a.rx() """ from .reactive import reactive_ops return reactive_ops(self) @@ -2298,10 +2299,10 @@ def set_default(self_,param_name,value): def add_parameter(self_, param_name: str, param_obj: Parameter): """ - Add a new Parameter object into this object's class. + Add a new Parameter object to this class. - Should result in a Parameter equivalent to one declared - in the class's source code. + This method allows dynamically adding a Parameter to the class, resulting in behavior equivalent to declaring + the Parameter in the class's source code. Parameters ---------- @@ -2312,34 +2313,25 @@ def add_parameter(self_, param_name: str, param_obj: Parameter): Examples -------- + Create a Parameterized class: - ```python - import param - - - class P(param.Parameterized): - a = param.Number() - b = param.String() - - - p = P() - ``` + >>> import param + >>> class P(param.Parameterized): + >>> a = param.Number() + >>> b = param.String() + >>> p = P() Add a new parameter to the class via the class: - ```python - P.param.add_parameter('c', param.Tuple(default=(1,2,3))) - print(p.c) - # (1, 2, 3) - ``` + >>> P.param.add_parameter('c', param.Tuple(default=(1, 2, 3))) + >>> print(p.c) + (1, 2, 3) Add a new parameter to the class via the instance: - ```python - p.param.add_parameter('d', param.Tuple(default=(3,2,1))) - print(p.d) - # (3, 2, 1) - ``` + >>> p.param.add_parameter('d', param.Tuple(default=(3, 2, 1))) + >>> print(p.d) + (3, 2, 1) """ # Could have just done setattr(cls,param_name,param_obj), # which is supported by the metaclass's __setattr__ , but @@ -2383,48 +2375,47 @@ def params(self_, parameter_name=None): def update(self_, arg=Undefined, /, **kwargs): """ - Updates one or more parameters of this object or class. + Update one or more parameters of this object or class. - This method allows you to set the parameters of the object or class using a dictionary, - an iterable, or keyword arguments in the form of param=value. The specified parameters - will be updated to the given values. + Allows setting the parameters of the object or class using a dictionary, an iterable, or keyword arguments + in the form of `param=value`. The specified parameters will be updated to the given values. - This method can also be used as a context manager to temporarily set and then reset - parameter values. + This method can also be used as a context manager to temporarily set and then reset parameter values. + + Parameters + ---------- + **params : dict or iterable or keyword arguments + The parameters to update, provided as a dictionary, iterable, or keyword arguments in `param=value` format. + References + ---------- User Guide: https://param.holoviz.org/user_guide/Parameters.html#other-parameterized-methods Examples -------- + Create a Parameterized instance: - ```python - import param - - class P(param.Parameterized): - a = param.Number() - b = param.String() - - p = P() - ``` + >>> import param + >>> class P(param.Parameterized): + >>> a = param.Number() + >>> b = param.String() + >>> p = P() Update parameters permanently: - ```python - p.param.update(a=1, b="Hello") - print(p.a, p.b) - # Output: 1 Hello - ``` + >>> p.param.update(a=1, b="Hello") + >>> print(p.a, p.b) + 1 Hello Update parameters temporarily: - ```python - with p.param.update(a=2, b="World"): - print(p.a, p.b) - # Output: 2 World - print(p.a, p.b) - # Output: 1 Hello - ``` + >>> with p.param.update(a=2, b="World"): + >>> print(p.a, p.b) + 2 World + >>> print(p.a, p.b) + 1 Hello """ + refs = {} if self_.self is not None: private = self_.self._param__private @@ -2707,40 +2698,40 @@ class or instance that contains an iterable collection of obj.param.set_dynamic_time_fn(time_fn,sublistattr) def serialize_parameters(self_, subset=None, mode='json'): - """Returns the serialized parameters of the Parameterized object. + """ + Return the serialized parameters of the Parameterized object. Parameters ---------- - subset: list, optional + subset : list, optional A list of parameter names to serialize. If None, all parameters will be serialized. Defaults to None. - mode (str, optional): + mode : str, optional The serialization format. By default, only 'json' is supported. Defaults to 'json'. Returns ------- - Any: The serialized value - - Reference - --------- - For more details visit https://param.holoviz.org/user_guide/Serialization_and_Persistence.html#serializing-with-json - - Example - ------- + Any + The serialized value. - ```python - import param + User Guide + ---------- + https://param.holoviz.org/user_guide/Serialization_and_Persistence.html#serializing-with-json - class P(param.Parameterized): - a = param.Number() - b = param.String() + Examples + -------- + Create a Parameterized instance and serialize its parameters: + >>> import param + >>> class P(param.Parameterized): + >>> a = param.Number() + >>> b = param.String() + >>> p = P(a=1, b="hello") - p = P(a=1, b="hello") + Serialize parameters: - serialized_data = p.param.serialize_parameters() - print(type(serialized_data)) - # {"name": "P00002", "a": 1, "b": "hello"} - ``` + >>> serialized_data = p.param.serialize_parameters() + >>> print(serialized_data) + {"name": "P00002", "a": 1, "b": "hello"} """ self_or_cls = self_.self_or_cls if mode not in Parameter._serializers: @@ -2776,9 +2767,8 @@ def deserialize_parameters(self_, serialization, subset=None, mode='json') -> di dict A dictionary with parameter names as keys and deserialized values. - References + User Guide ---------- - For more details on parameter serialization, see: https://param.holoviz.org/user_guide/Serialization_and_Persistence.html#serializing-with-json Examples diff --git a/param/reactive.py b/param/reactive.py index cc38c8bf..fec62fd4 100644 --- a/param/reactive.py +++ b/param/reactive.py @@ -183,39 +183,42 @@ class reactive_ops: """ The reactive namespace. - Provides reactive versions of the operations that cannot be made reactive through overloading, such as - `.rx.and_` and `.rx.bool`. Call it (`()`) to obtain a reactive expression. + Provides reactive versions of operations that cannot be made reactive through operator overloading, such as + `.rx.and_` and `.rx.bool`. Calling this namespace (`()`) returns a reactive expression. - User Guide: https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx - - Examples: - - Lets create a Parameterized instance: + Parameters + ---------- + None - ```python - import param + Returns + ------- + Reactive expression + The result of calling the reactive namespace is a reactive expression. - class P(param.Parameterized): - a = param.Number() - b = param.String() + User Guide + ---------- + https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx + Examples + -------- + Create a Parameterized instance: - p = P(a=1, b="hello") - ``` + >>> import param + >>> class P(param.Parameterized): + >>> a = param.Number() + >>> b = param.String() + >>> p = P(a=1, b="hello") Get the current value: - ```python - a = p.param.a.rx.value - ``` + >>> a = p.param.a.rx.value Call it to get a reactive expression: - ```python - rx_value = p.param.a.rx() - ``` + >>> rx_value = p.param.a.rx() """ + def __init__(self, reactive): self._reactive = reactive @@ -798,38 +801,39 @@ def rx(self) -> reactive_ops: """ The reactive namespace. - Provides reactive versions of the operations that cannot be made reactive through overloading, such as - `.rx.and_` and `.rx.bool`. Call it (`()`) to obtain a reactive expression. - - User Guide: https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx - - Examples - -------- + Provides reactive versions of operations that cannot be made reactive through operator overloading, such as + `.rx.and_` and `.rx.bool`. Calling this namespace (`()`) returns a reactive expression. - Lets create a Parameterized instance: + Parameters + ---------- + None - ```python - import param + Returns + ------- + Reactive expression + The result of calling the reactive namespace is a reactive expression. - class P(param.Parameterized): - a = param.Number() - b = param.String() + User Guide + ---------- + https://param.holoviz.org/user_guide/Reactive_Expressions.html#special-methods-on-rx + Examples + -------- + Create a Parameterized instance: - p = P(a=1, b="hello") - ``` + >>> import param + >>> class P(param.Parameterized): + >>> a = param.Number() + >>> b = param.String() + >>> p = P(a=1, b="hello") Get the current value: - ```python - a = p.param.a.rx.value - ``` + >>> a = p.param.a.rx.value Call it to get a reactive expression: - ```python - rx_value = p.param.a.rx() - ``` + >>> rx_value = p.param.a.rx() """ return self._rx From aa1b61c7323ffef608134d552be045234fed38f8 Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Sat, 23 Nov 2024 06:27:39 +0100 Subject: [PATCH 19/25] Update param/parameterized.py Co-authored-by: Maxime Liquet <35924738+maximlt@users.noreply.github.com> --- param/parameterized.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/param/parameterized.py b/param/parameterized.py index 1efa0e8d..fef3ec4c 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -4407,7 +4407,8 @@ def param(self): Note that this is different from the current `value` of `my_instance`: - >>> my_instance.value # 0, the current parameter value + >>> my_instance.value # the current parameter value + 0 """ return Parameters(self.__class__, self=self) From f26a0e2aa488c557b89680197fc65e89a02824f2 Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Sat, 23 Nov 2024 06:29:27 +0100 Subject: [PATCH 20/25] Update param/parameterized.py Co-authored-by: Maxime Liquet <35924738+maximlt@users.noreply.github.com> --- param/parameterized.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/param/parameterized.py b/param/parameterized.py index fef3ec4c..3a245633 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -2387,9 +2387,9 @@ def update(self_, arg=Undefined, /, **kwargs): **params : dict or iterable or keyword arguments The parameters to update, provided as a dictionary, iterable, or keyword arguments in `param=value` format. - References + User Guide ---------- - User Guide: https://param.holoviz.org/user_guide/Parameters.html#other-parameterized-methods + https://param.holoviz.org/user_guide/Parameters.html#other-parameterized-methods Examples -------- From 44cbfedb3f20105bc59dc6c06cb0f57cb239bb26 Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Sat, 23 Nov 2024 05:30:22 +0000 Subject: [PATCH 21/25] review feedback --- param/parameterized.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/param/parameterized.py b/param/parameterized.py index fef3ec4c..20802c65 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -1346,8 +1346,7 @@ def rx(self): >>> import param >>> class P(param.Parameterized): >>> a = param.Number() - >>> b = param.String() - >>> p = P(a=1, b="hello") + >>> p = P(a=1) Get the current value: From 27a11e5e286de18483e51ac0980e2c1f51e58156 Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Sat, 23 Nov 2024 05:46:37 +0000 Subject: [PATCH 22/25] review feedback --- param/parameterized.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/param/parameterized.py b/param/parameterized.py index 493633f2..e11298eb 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -1345,7 +1345,7 @@ def rx(self): >>> import param >>> class P(param.Parameterized): - >>> a = param.Number() + ... a = param.Number() >>> p = P(a=1) Get the current value: @@ -2320,16 +2320,16 @@ def add_parameter(self_, param_name: str, param_obj: Parameter): >>> b = param.String() >>> p = P() - Add a new parameter to the class via the class: + Add a new parameter to the class `P` via the class namespace `P.param`: >>> P.param.add_parameter('c', param.Tuple(default=(1, 2, 3))) >>> print(p.c) (1, 2, 3) - Add a new parameter to the class via the instance: + Add a new parameter to the class `P` via the instance namespace `p.param`: >>> p.param.add_parameter('d', param.Tuple(default=(3, 2, 1))) - >>> print(p.d) + >>> p.d (3, 2, 1) """ # Could have just done setattr(cls,param_name,param_obj), @@ -2396,8 +2396,8 @@ def update(self_, arg=Undefined, /, **kwargs): >>> import param >>> class P(param.Parameterized): - >>> a = param.Number() - >>> b = param.String() + ... a = param.Number() + ... b = param.String() >>> p = P() Update parameters permanently: @@ -2409,7 +2409,7 @@ def update(self_, arg=Undefined, /, **kwargs): Update parameters temporarily: >>> with p.param.update(a=2, b="World"): - >>> print(p.a, p.b) + ... print(p.a, p.b) 2 World >>> print(p.a, p.b) 1 Hello @@ -2722,8 +2722,8 @@ def serialize_parameters(self_, subset=None, mode='json'): >>> import param >>> class P(param.Parameterized): - >>> a = param.Number() - >>> b = param.String() + ... a = param.Number() + ... b = param.String() >>> p = P(a=1, b="hello") Serialize parameters: From 009261726573b3ebc8d07a0fd13bc9cabd571008 Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Sat, 23 Nov 2024 06:27:09 +0000 Subject: [PATCH 23/25] review feedback --- param/parameterized.py | 44 +++++++++++++++++++++++++----------------- param/reactive.py | 4 ---- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/param/parameterized.py b/param/parameterized.py index e11298eb..b73059dd 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -1326,10 +1326,6 @@ def rx(self): Provides reactive versions of operations that cannot be made reactive through operator overloading, such as `.rx.and_` and `.rx.bool`. Calling this namespace (`()`) returns a reactive expression. - Parameters - ---------- - None - Returns ------- Reactive expression @@ -2374,7 +2370,7 @@ def params(self_, parameter_name=None): def update(self_, arg=Undefined, /, **kwargs): """ - Update one or more parameters of this object or class. + Update multiple parameters of this object or class before triggering events. Allows setting the parameters of the object or class using a dictionary, an iterable, or keyword arguments in the form of `param=value`. The specified parameters will be updated to the given values. @@ -2392,27 +2388,39 @@ def update(self_, arg=Undefined, /, **kwargs): Examples -------- - Create a Parameterized instance: + + Create a Parameterized class: >>> import param >>> class P(param.Parameterized): - ... a = param.Number() - ... b = param.String() - >>> p = P() + ... a = param.String() + ... b = param.String() + + Define the instance: + + >>> p = P(a="0. Hello", b="0. World") - Update parameters permanently: + Usea `.update` to update the parameters: - >>> p.param.update(a=1, b="Hello") - >>> print(p.a, p.b) - 1 Hello + >>> p.param.update(a="1. Hello", b="2. World") + p.a, p.b - Update parameters temporarily: + Update the parameters temporarily: - >>> with p.param.update(a=2, b="World"): + >>> with p.param.update(a="2. Hello", b="2. World"): + ... p.a, p.b + 2. Hello, 2. World + >>> p.a, p.b + 1. Hello, 1. World + + Lets see that events are **after** all parameters have been updated + + >>> @param.depends(p.param.a, watch=True) + ... def print_a_b(a): ... print(p.a, p.b) - 2 World - >>> print(p.a, p.b) - 1 Hello + + >>> my_param.param.update(a="3. Hello",b="3. World") + 3. Hello 3. World """ refs = {} diff --git a/param/reactive.py b/param/reactive.py index fec62fd4..4983f267 100644 --- a/param/reactive.py +++ b/param/reactive.py @@ -804,10 +804,6 @@ def rx(self) -> reactive_ops: Provides reactive versions of operations that cannot be made reactive through operator overloading, such as `.rx.and_` and `.rx.bool`. Calling this namespace (`()`) returns a reactive expression. - Parameters - ---------- - None - Returns ------- Reactive expression From 930ca17fbd11136d4418ef5485f10770b420ff0b Mon Sep 17 00:00:00 2001 From: MarcSkovMadsen Date: Sat, 23 Nov 2024 06:38:02 +0000 Subject: [PATCH 24/25] review feedback --- param/parameterized.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/param/parameterized.py b/param/parameterized.py index b73059dd..3245ad4f 100644 --- a/param/parameterized.py +++ b/param/parameterized.py @@ -2388,7 +2388,7 @@ def update(self_, arg=Undefined, /, **kwargs): Examples -------- - + Create a Parameterized class: >>> import param @@ -2400,25 +2400,26 @@ def update(self_, arg=Undefined, /, **kwargs): >>> p = P(a="0. Hello", b="0. World") - Usea `.update` to update the parameters: + Use `.update` to update the parameters: >>> p.param.update(a="1. Hello", b="2. World") - p.a, p.b + >>> p.a, p.b + ('1. Hello', '1. World') Update the parameters temporarily: >>> with p.param.update(a="2. Hello", b="2. World"): - ... p.a, p.b - 2. Hello, 2. World + ... print(p.a, p.b) + 2. Hello 2. World + >>> p.a, p.b - 1. Hello, 1. World + ('1. Hello', '1. World') - Lets see that events are **after** all parameters have been updated + Lets see that events are triggered **after** all parameters have been updated >>> @param.depends(p.param.a, watch=True) ... def print_a_b(a): ... print(p.a, p.b) - >>> my_param.param.update(a="3. Hello",b="3. World") 3. Hello 3. World """ @@ -4329,10 +4330,10 @@ class Parameterized(metaclass=ParameterizedMetaclass): parameter in the instance will get the value given as a keyword argument. For example: - class Foo(Parameterized): - xx = Parameter(default=1) + >>> class Foo(Parameterized): + ... xx = Parameter(default=1) - foo = Foo(xx=20) + >>> foo = Foo(xx=20) in this case foo.xx gets the value 20. From 8964a56f40788ad7c52d0bba7297538e8c374621 Mon Sep 17 00:00:00 2001 From: MarcSkovMadsen Date: Sat, 23 Nov 2024 06:45:34 +0000 Subject: [PATCH 25/25] fix failing test --- param/reactive.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/param/reactive.py b/param/reactive.py index 4983f267..98e44f0f 100644 --- a/param/reactive.py +++ b/param/reactive.py @@ -186,10 +186,6 @@ class reactive_ops: Provides reactive versions of operations that cannot be made reactive through operator overloading, such as `.rx.and_` and `.rx.bool`. Calling this namespace (`()`) returns a reactive expression. - Parameters - ---------- - None - Returns ------- Reactive expression @@ -205,9 +201,8 @@ class reactive_ops: >>> import param >>> class P(param.Parameterized): - >>> a = param.Number() - >>> b = param.String() - >>> p = P(a=1, b="hello") + ... a = param.Number() + >>> p = P(a=1) Get the current value: @@ -819,9 +814,8 @@ def rx(self) -> reactive_ops: >>> import param >>> class P(param.Parameterized): - >>> a = param.Number() - >>> b = param.String() - >>> p = P(a=1, b="hello") + ... a = param.Number() + >>> p = P(a=1) Get the current value: