|
| 1 | +test_data = { |
| 2 | + "data": [ |
| 3 | + { |
| 4 | + "6": { |
| 5 | + "value": "Andre Harris" |
| 6 | + }, |
| 7 | + "7": { |
| 8 | + "value": 10 |
| 9 | + }, |
| 10 | + "8": { |
| 11 | + "value": "2019-12-18T08:00:00.000Z" |
| 12 | + } |
| 13 | + } |
| 14 | + ], |
| 15 | + "fields": [ |
| 16 | + { |
| 17 | + "id": 6, |
| 18 | + "label": "Full Name", |
| 19 | + "type": "text" |
| 20 | + }, |
| 21 | + { |
| 22 | + "id": 7, |
| 23 | + "label": "Amount", |
| 24 | + "type": "numeric" |
| 25 | + }, |
| 26 | + { |
| 27 | + "id": 8, |
| 28 | + "label": "Date time", |
| 29 | + "type": "date time" |
| 30 | + } |
| 31 | + ], |
| 32 | + "metadata": { |
| 33 | + "totalRecords": 10, |
| 34 | + "numRecords": 1, |
| 35 | + "numFields": 3, |
| 36 | + "skip": 0 |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +class QBResponse(dict): |
| 42 | + def __init__(self, response_type): |
| 43 | + self.response_type = response_type |
| 44 | + super().__init__() |
| 45 | + |
| 46 | + def denest(self): |
| 47 | + denested_list = [] |
| 48 | + for record in self.get('data'): |
| 49 | + denested = {} |
| 50 | + for k, v in record.items(): |
| 51 | + denested.update({k: v.get('value')}) |
| 52 | + denested_list.append(denested) |
| 53 | + self.update({'data': denested_list}) |
| 54 | + return self |
| 55 | + |
| 56 | + def orient(self, orient, **kwargs): |
| 57 | + |
| 58 | + if orient == 'records': |
| 59 | + if not kwargs.get('key'): |
| 60 | + raise ValueError('Missing required "key" argument for records orientation') |
| 61 | + else: |
| 62 | + if isinstance(kwargs.get('key'), int): |
| 63 | + selector = kwargs.get('key') |
| 64 | + selector = str(selector) |
| 65 | + else: |
| 66 | + raise TypeError(f'Key must be an {int}') |
| 67 | + |
| 68 | + # loop data list, create dict |
| 69 | + records = {} |
| 70 | + for i in self.get('data'): |
| 71 | + print(i) |
| 72 | + key = i.pop(selector).get('value') if self.get('data') else i.pop(selector) |
| 73 | + records.update({key: i}) |
| 74 | + |
| 75 | + # set data to records |
| 76 | + self.update({'data': records}) |
| 77 | + |
| 78 | + return self |
| 79 | + |
| 80 | + else: |
| 81 | + raise ValueError(f'{orient} is not a valid orientation.') |
| 82 | + |
| 83 | + def currency(self, currency_type): |
| 84 | + return self |
| 85 | + |
| 86 | + def transform(self, transformation): |
| 87 | + if transformation == 'labels': |
| 88 | + |
| 89 | + # transform fids into labels |
| 90 | + if not self.get('data'): |
| 91 | + raise KeyError( |
| 92 | + 'Try transforming the data before applying additional methods.') |
| 93 | + |
| 94 | + data = self.get('data') |
| 95 | + print(data) |
| 96 | + fields = self.get('fields') |
| 97 | + |
| 98 | + fields_dict = {i['id']: i for i in fields} |
| 99 | + |
| 100 | + records = [] |
| 101 | + for idx, d in enumerate(data): |
| 102 | + record = {} |
| 103 | + print('data', d) |
| 104 | + for k, v in d.items(): |
| 105 | + record.update({ |
| 106 | + fields_dict[int(k)].get('label'): v if v.get('value') is None else v.get('value') |
| 107 | + }) |
| 108 | + records.append(record) |
| 109 | + print(k, v) |
| 110 | + |
| 111 | + self.update({'data': records}) |
| 112 | + |
| 113 | + return self |
| 114 | + |
| 115 | + |
| 116 | +qbr = QBResponse('records') |
| 117 | +qbr.update(test_data) |
| 118 | +qbr.transform('labels').orient('records', key=6) |
| 119 | + |
| 120 | +print(qbr) |
| 121 | + |
| 122 | +# # handle transform |
| 123 | +# if kwargs.get('transform'): |
| 124 | +# # labels |
| 125 | +# if kwargs.get('transform') == 'labels': |
| 126 | +# |
| 127 | +# # transform fids into labels |
| 128 | +# data = json_data.get('data') |
| 129 | +# fields = json_data.get('fields') |
| 130 | +# |
| 131 | +# fields_dict = {i['id']: i for i in fields} |
| 132 | +# print(fields_dict) |
| 133 | +# |
| 134 | +# records = [] |
| 135 | +# for idx, d in enumerate(data): |
| 136 | +# record = {} |
| 137 | +# print('data', d) |
| 138 | +# for k, v in d.items(): |
| 139 | +# record.update({ |
| 140 | +# fields_dict[int(k)].get('label'): v if kwargs.get('denested') else v.get('value') |
| 141 | +# }) |
| 142 | +# records.append(record) |
| 143 | +# print(k, v) |
| 144 | +# |
| 145 | +# json_data.update({'data': records}) |
| 146 | +# |
| 147 | + |
| 148 | +# |
| 149 | +# # handle data orientation |
| 150 | +# if kwargs.get('orient'): |
| 151 | +# if kwargs.get('orient') == 'records': |
| 152 | +# |
0 commit comments