Skip to content

Commit

Permalink
added helper function to convert spockspaces to dict that is outside …
Browse files Browse the repository at this point in the history
…of the SpockBulder object -- lol forgoat to add file
  • Loading branch information
ncilfone committed Jan 17, 2023
1 parent a8d9f89 commit f11f3ce
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions spock/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-

# SPDX-License-Identifier: Apache-2.0

"""Helper functions for Spock"""

from typing import Dict, List, Optional, Tuple, Union

from spock.backend.saver import AttrSaver
from spock.backend.wrappers import Spockspace
from spock.exceptions import _SpockValueError
from spock.utils import _C, _is_spock_instance


def to_dict(
objs: Union[_C, List[_C], Tuple[_C, ...]], saver: Optional[AttrSaver] = AttrSaver()
) -> Dict[str, Dict]:
"""Converts spock classes from a Spockspace into their dictionary representations
Args:
objs: single spock class or an iterable of spock classes
saver: optional saver class object
Returns:
dictionary where the class names are keys and the values are the dictionary
representations
"""
if isinstance(objs, (List, Tuple)):
obj_dict = {}
for val in objs:
if not _is_spock_instance(val):
raise _SpockValueError(
f"Object is not a @spock decorated class object -- "
f"currently `{type(val)}`"
)
obj_dict.update({type(val).__name__: val})
elif _is_spock_instance(objs):
obj_dict = {type(objs).__name__: objs}
else:
raise _SpockValueError(
f"Object is not a @spock decorated class object -- "
f"currently `{type(objs)}`"
)
return saver.dict_payload(Spockspace(**obj_dict))

0 comments on commit f11f3ce

Please sign in to comment.