-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert-python
51 lines (48 loc) · 1.61 KB
/
convert-python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
The following function converts data from a google sheet into data that a map with keys that represent the week of the year that the data is for. I'd like you to convert it to python code for me.
```typescript
export type CheckinChartData = {
id: string,
data: {
x: string;
y: number;
checkedIn: boolean;
}[];
}[];
export function dataToHeatmapData(data: Data): {
[weekOfYear: string]: CheckinChartData,
} {
const weekdayIndex = data.headers.indexOf('Day of Week');
const timeIndex = data.headers.indexOf('time');
const nameIndex = data.headers.indexOf('Name');
const groupedByWeeks = groupBy(data.rows, timeIndex, getWeekNumber);
const weeksGroupedByName = Object.keys(groupedByWeeks).reduce((groups, week) => ({
...groups,
[week]: groupBy(
groupedByWeeks[week],
nameIndex,
(t: string) => {
return t;
}
)}), {});
const heatMapData = Object.keys(weeksGroupedByName).reduce((groups,week) => ({
...groups,
// @ts-ignore TODO
[week]: Object.keys(weeksGroupedByName[week]).map(name => {
// @ts-ignore TODO
const sortedCheckins = sortCheckinByWeekday(weeksGroupedByName[week][name], weekdayIndex);
return {
id: name,
data: weekdays.map((weekday, i) => {
const checkinIndex = sortedCheckins.findIndex(checkin => checkin[weekdayIndex] === weekday);
return {
//@ts-ignore TODO
x: weekday,//weeksGroupedByName[week][name].find(checkin => checkin[nameIndex] === name) ? 1 : 0,
y: checkinIndex + 1,
checkedIn: Boolean(checkinIndex + 1),
}
})
}
})}), {});
return heatMapData;
}
```