Skip to content

Commit

Permalink
review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcSkovMadsen committed Nov 14, 2024
1 parent 2d5b832 commit 63f96df
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 136 deletions.
180 changes: 85 additions & 95 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
----------
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
86 changes: 45 additions & 41 deletions param/reactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 63f96df

Please sign in to comment.