Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix basic pickling #783

Merged
merged 10 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,24 +694,30 @@ def _skip_event(*events, **kwargs):
return True


# Two callers at the module top level to support pickling.
async def _async_caller(*events, what='value', changed=None, callback=None, function=None):
if callback:
callback(*events)
if not _skip_event or not _skip_event(*events, what=what, changed=changed):
await function()


def _sync_caller(*events, what='value', changed=None, callback=None, function=None):
if callback:
callback(*events)
if not _skip_event(*events, what=what, changed=changed):
return function()


def _m_caller(self, method_name, what='value', changed=None, callback=None):
"""
Wraps a method call adding support for scheduling a callback
before it is executed and skipping events if a subobject has
changed but its values have not.
"""
function = getattr(self, method_name)
if iscoroutinefunction(function):
async def caller(*events):
if callback:
callback(*events)
if not _skip_event or not _skip_event(*events, what=what, changed=changed):
await function()
else:
def caller(*events):
if callback: callback(*events)
if not _skip_event(*events, what=what, changed=changed):
return function()
_caller = _async_caller if iscoroutinefunction(function) else _sync_caller
caller = partial(_caller, what=what, changed=changed, callback=callback, function=function)
caller._watcher_name = method_name
return caller

Expand Down Expand Up @@ -1698,7 +1704,8 @@ def self_or_cls(self_):

def __setstate__(self, state):
# Set old parameters state on Parameterized._parameters_state
self_or_cls = state.get('self', state.get('cls'))
self_, cls = state.get('self'), state.get('cls')
self_or_cls = self_ if self_ is not None else cls
for k in self_or_cls._parameters_state:
key = '_'+k
if key in state:
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ tests-full = [
"ipython",
"jsonschema",
"gmpy",
"cloudpickle",
"nest_asyncio",
]
lint = [
"flake8",
Expand Down Expand Up @@ -121,6 +123,8 @@ dependencies = [
"jsonschema",
"numpy",
"pandas",
"cloudpickle",
"nest_asyncio",
# To keep __version__ up-to-date in editable installs
"setuptools_scm",
]
Expand Down Expand Up @@ -179,6 +183,8 @@ name."^(?!pypy).*".dependencies = [
"odfpy",
"feather-format",
"pyarrow",
"cloudpickle",
"nest_asyncio",
]
# Only install gmpy on Linux on these version
# Only install tables (deser HDF5) on Linux on these version
Expand Down
Loading