Skip to content

Commit

Permalink
kw unpacking
Browse files Browse the repository at this point in the history
  • Loading branch information
raynardj committed May 7, 2022
1 parent a307605 commit 1e8d74f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion forgebox/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.0.6"
__version__ = "1.0.7"
14 changes: 13 additions & 1 deletion forgebox/unpack.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Tuple, Any
from typing import Tuple, Any, Dict


class Unpack:
Expand All @@ -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):
Expand Down Expand Up @@ -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))
2 changes: 1 addition & 1 deletion settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ author = xiaochen(ray) zhang
author_email = [email protected]
copyright = xiaochen(ray) zhang
branch = master
version = 1.0.6
version = 1.0.7
min_python = 3.6
host = github
audience = Developers
Expand Down
32 changes: 19 additions & 13 deletions test/pipeline_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
assert date == "2022-01-01"
assert data2['name'] == "Lisa"
assert data2['math_final'] == 99
assert data2['date'] == "2022-01-01"

0 comments on commit 1e8d74f

Please sign in to comment.