diff --git a/forgebox/__init__.py b/forgebox/__init__.py index 382021f..9e604c0 100644 --- a/forgebox/__init__.py +++ b/forgebox/__init__.py @@ -1 +1 @@ -__version__ = "1.0.6" +__version__ = "1.0.7" diff --git a/forgebox/unpack.py b/forgebox/unpack.py index a91c21b..433ba09 100644 --- a/forgebox/unpack.py +++ b/forgebox/unpack.py @@ -1,4 +1,4 @@ -from typing import Tuple, Any +from typing import Tuple, Any, Dict class Unpack: @@ -19,6 +19,11 @@ class Unpack: } student = Unpack(data) name, final_math_score, date = student(["info","name"],['grad','math','final'],'date') + + data = student.to_dict(name=['info','name'], final_math_score=['grad','math','final'], date='date') + data + [out]: {'name': 'Lisa', 'final_math_score': 99, 'date': '2022-01-01'} + """ def __init__(self, obj, raise_error: bool = False): @@ -60,3 +65,10 @@ def __call__(self, *args) -> Tuple[Any]: return rt[0] else: return tuple(rt) + + def to_dict(self, **kwargs) -> Dict[str, Any]: + """ + Return not tuple, but a dictionary + """ + vals = list(self(*(kwargs.values()))) + return dict(zip(kwargs.keys(), vals)) diff --git a/settings.ini b/settings.ini index 3eed568..5d283e6 100644 --- a/settings.ini +++ b/settings.ini @@ -7,7 +7,7 @@ author = xiaochen(ray) zhang author_email = b2ray2c@gmail.com copyright = xiaochen(ray) zhang branch = master -version = 1.0.6 +version = 1.0.7 min_python = 3.6 host = github audience = Developers diff --git a/test/pipeline_test.py b/test/pipeline_test.py index 98be604..36f6bf6 100644 --- a/test/pipeline_test.py +++ b/test/pipeline_test.py @@ -85,24 +85,30 @@ def test_cosine_search(): cos = CosineSearch(base) assert (cos(vec) == [2, 1, 0]).all() assert (cos(vec2) == [0, 1, 2]).all() - assert (cos.search(vec) == [2, 1 ,0]).all() + assert (cos.search(vec) == [2, 1, 0]).all() def test_unpack(): from forgebox.imports import Unpack data = { - "info":{ - "name": "Lisa", "age":12, + "info": { + "name": "Lisa", "age": 12, + }, + "grad": { + "math": { + "mid": 98, "final": 99 }, - "grad":{ - "math":{ - "mid":98, "final":99 - }, - "history":{"mid":90, "final":95}, - }, - "date":"2022-01-01", - } - name, math_final, date = Unpack(data, False)(["info","name"],['grad','math','final'],'date') + "history": {"mid": 90, "final": 95}, + }, + "date": "2022-01-01", + } + name, math_final, date = Unpack(data, True)( + ["info", "name"], ['grad', 'math', 'final'], 'date') + data2 = Unpack(data, True).to_dict(name=["info", "name"], math_final=[ + 'grad', 'math', 'final'], date='date') assert name == "Lisa" assert math_final == 99 - assert date == "2022-01-01" \ No newline at end of file + assert date == "2022-01-01" + assert data2['name'] == "Lisa" + assert data2['math_final'] == 99 + assert data2['date'] == "2022-01-01"