问题
I have a simple problem. I am new to object oriented programming.. I have one big list of nested dictionaries, that should be presented within several tables. What I don't understand is how I can use objects to make this simpler.
I am getting Google Analytics data for each user.
LOGIC OF MY CODE:
- get the data for a single user (This data is in the format like this:
{'sampleRate': 1,
# Pageview activity
'sessions': [{'activityTime': '2020-01-08T15:48:38.012671Z',
'activityType': 'PAGEVIEW',
'campaign': '(not set)',
'channelGrouping': 'Direct',
'customDimension': [{'index': 1}],
'hostname': 'company.domain.com',
'keyword': '(not set)',
'landingPagePath': '/login',
'medium': '(none)',
'pageview': {'pagePath': '/login',
'pageTitle': 'titleofthepage'},
'source': '(direct)'},
# Event activity
{'activityTime': '2020-01-08T15:48:37.915105Z',
'activityType': 'EVENT',
'campaign': '(not set)',
'channelGrouping': 'Direct',
'customDimension': [{'index': 1}],
'event': {'eventAction': 'Successfully Logged '
'In',
'eventCategory': 'Auth',
'eventCount': '1',
'eventLabel': '(not set)'},
'hostname': 'company.domain.com',
'keyword': '(not set)',
'landingPagePath': '/login',
'medium': '(none)',
'source': '(direct)'}]
'dataSource': 'web',
'deviceCategory': 'desktop',
'platform': 'Windows',
'sessionDate': '2020-01-08',
'sessionId': '1578491317'}
- (This probably where I can impliment object oriented programming) Now I iterate through the list of users, and store each user's ouput (in the format mentioned in step 1) in a list
- This creates one big list of step1's
WHAT I WANT:
I want two tables at the end of the day, to do queries with Peewee:
Table 1:
SessionId User dataSource deviceCategory platform sessionDuration
12345 123 web desktop windows 00:15:12
14824 188 web desktop Linux 01:04:48
...
Table 2:
ActivityTime Session pageTitle pagePath EventCategory eventCount eventLabel eventAction
20:30:12 12345 domain webpage NaN NaN NaN NaN
20:45:47 12345 NaN NaN Aut 1 (not_set) LoggedIn
21:12:48 14824 NaN NaN Aut 2 (not_set) LoggedIn
I don't really know how to approach this.. because I know object oriented programming/ ORM can make this cleaner and much pythionic.. I don't expect code as an answer, because the question is quite vague, but any logic will help. I cannot seem to find a way to accomplish this anywhere
来源:https://stackoverflow.com/questions/59895060/peewee-orm-sqlite-create-db-with-list-of-nested-dictionaries-from-google-analyti