Skip to content

Commit

Permalink
aggs ability to have list of strings for quick shortcut -> terms
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardbinet committed Jun 28, 2020
1 parent c3cb2f4 commit f59a220
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
11 changes: 5 additions & 6 deletions pandagg/tree/aggs/aggs.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ def _fill(self, *args, **kwargs):
self.insert_node(shadow_root)
pid = shadow_root.identifier
for ar in arg:
if not isinstance(ar, (AggNode, Aggs)):
if isinstance(ar, string_types):
ar = Terms(ar, field=ar)
elif not isinstance(ar, (AggNode, Aggs)):
raise ValueError("Invalid type %s, %s" % (type(ar), ar))
self.insert(ar, parent_id=pid)

Expand Down Expand Up @@ -331,11 +333,8 @@ def groupby(self, *args, **kwargs):
inserted_aggs = [Aggs(arg) for arg in args]
# groupby([{}, {}])
elif len(args) == 1 and isinstance(args[0], (list, tuple)):
if kwargs:
raise ValueError(
"Kwargs not allowed when passing multiple aggregations in args."
)
inserted_aggs = [Aggs(arg) for arg in args[0]]
# kwargs applied on all
inserted_aggs = [Aggs(arg, **kwargs) for arg in args[0]]
# groupby({})
# groupby(Terms())
# groupby('terms', name='per_tag', field='tag')
Expand Down
9 changes: 9 additions & 0 deletions tests/tree/aggs/test_aggs.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,15 @@ def test_aggs_at_root(self):
},
)

def test_aggs_strings(self):
self.assertEqual(
Aggs().aggs(["yolo1", "yolo2"]).to_dict(),
{
"yolo1": {"terms": {"field": "yolo1"}},
"yolo2": {"terms": {"field": "yolo2"}},
},
)

def test_validate_aggs_parent_id(self):
"""
<Aggregation>
Expand Down

0 comments on commit f59a220

Please sign in to comment.