@@ -563,3 +563,59 @@ def _cycler(label, itr):
563
563
itr = (v [lab ] for v in itr )
564
564
565
565
return Cycler ._from_iter (label , itr )
566
+
567
+
568
+ def from_iter_of_dicts (inp ):
569
+ """Construct a summation-only cycler from a list of dicts
570
+
571
+ Given an iterable of dictionaries (such as you would get from
572
+ iterating over a `Cycler`) and constructs a new `Cycler`.
573
+
574
+ The following are equivalent ::
575
+
576
+ from_iter_of_dicts(list(c)) == c.simplify()
577
+
578
+ Parameters
579
+ ----------
580
+ inp : Iterable[Mapping[Any, Any]]
581
+ An iterable of dictionaries. All must have the same keys.
582
+
583
+ Returns
584
+ -------
585
+ ret : Cycler
586
+ """
587
+ # TODO better validation that all keys match, not just using
588
+ # the keys from the first entry
589
+ # TODO deal with empty list correctly
590
+ inp = list (inp )
591
+ return reduce (add , (cycler (k , [_ [k ] for _ in inp ]) for k in inp [0 ]))
592
+
593
+
594
+ def merge_suplemental (source , indx_key , sumplemental_data ):
595
+ """Update a cycler with some supplemental data
596
+
597
+ Given a cycler, add extra keys to each entry based
598
+ on the value of ``index_key`` in that entry.
599
+
600
+ Parameters
601
+ ----------
602
+ source : Cycler
603
+ The cycler to augment.
604
+
605
+ indx_key : Any
606
+ Must be one of the keys in ``source``
607
+
608
+ sumplemental_data : Mapping[Any, Any]
609
+ A mapping between the values of ``index_key`` in ``source``
610
+ and mappings of additional keys and values.
611
+
612
+ Each mapping must have the same set of keys.
613
+
614
+ Returns
615
+ -------
616
+ ret : Cycler
617
+
618
+ """
619
+ return (source +
620
+ from_iter_of_dicts (sumplemental_data [v [indx_key ]]
621
+ for v in source ))
0 commit comments