Skip to content

Commit

Permalink
Make all Element with Template pickable natively (#144)
Browse files Browse the repository at this point in the history
* Make _parse_size robust to already parsed values
(needed for unpickling a folium Map)

* Add the _template_str to recover all the Elements

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* __setstate__ simplification

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* More change cleaning, keep only the Element change

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Clean verbosity & set attribute privates

* Remove one action in Element setstate
_template is already in self, no need to update it if not in state.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
BastienGauthier and pre-commit-ci[bot] authored Oct 16, 2023
1 parent ce17e54 commit 655382e
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions branca/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def __init__(self, template=None, template_name=None):
self._env = ENV
self._children = OrderedDict()
self._parent = None
self._template_str = template
self._template_name = template_name

if template is not None:
self._template = Template(template)
Expand All @@ -71,11 +73,18 @@ def __getstate__(self):
"""
state: dict = self.__dict__.copy()
state["_env"] = None
state.pop("_template", None)
return state

def __setstate__(self, state: dict):
"""Re-add ._env attribute when unpickling"""
state["_env"] = ENV

if state["_template_str"] is not None:
state["_template"] = Template(state["_template_str"])
elif state["_template_name"] is not None:
state["_template"] = ENV.get_template(state["_template_name"])

self.__dict__.update(state)

def get_name(self):
Expand Down

0 comments on commit 655382e

Please sign in to comment.