| 92 | |
| 93 | |
| 94 | def _partition_dict(item_dict, scalar_keys): |
| 95 | # Given a dictionary, partition it into two list based on the |
| 96 | # values associated with the keys. |
| 97 | # {'foo': 'scalar', 'bar': 'scalar', 'baz': ['not, 'scalar']} |
| 98 | # scalar = [('foo', 'scalar'), ('bar', 'scalar')] |
| 99 | # non_scalar = [('baz', ['not', 'scalar'])] |
| 100 | scalar = [] |
| 101 | non_scalar = [] |
| 102 | if scalar_keys is None: |
| 103 | # scalar_keys can have more than just the keys in the item_dict, |
| 104 | # but if user does not provide scalar_keys, we'll grab the keys |
| 105 | # from the current item_dict |
| 106 | for key, value in sorted(item_dict.items()): |
| 107 | if isinstance(value, (dict, list)): |
| 108 | non_scalar.append((key, value)) |
| 109 | else: |
| 110 | scalar.append(str(value)) |
| 111 | else: |
| 112 | for key in scalar_keys: |
| 113 | scalar.append(str(item_dict.get(key, ''))) |
| 114 | remaining_keys = sorted(set(item_dict.keys()) - set(scalar_keys)) |
| 115 | for remaining_key in remaining_keys: |
| 116 | non_scalar.append((remaining_key, item_dict[remaining_key])) |
| 117 | return scalar, non_scalar |