Skip to content

Instantly share code, notes, and snippets.

@anxiousmodernman
Created May 3, 2014 18:41
Show Gist options
  • Save anxiousmodernman/11503408 to your computer and use it in GitHub Desktop.
Save anxiousmodernman/11503408 to your computer and use it in GitHub Desktop.
example beginnings of a Query class
class Query:
def __init__(self, params={}):
# make sure params is a dict
if isinstance(params, unicode):
self.params = dict(params)
self.params = params
# QUESTION assign defaults elsewhere?
self.ids = params['ids'] if 'ids' in params else None
self.dimensions = params['dimensions'] if 'dimensions' in params else None
self.filters = params['filters'] if 'filters' in params else None
self.start_date = params['start_date'] if 'start_date' in params else None
self.end_date = params['end_date'] if 'end_date' in params else None
self.metrics = params['metrics'] if 'metrics' in params else None
if __name__ == '__main__':
# VIEWID and parameters hardcoded in this routine, but could be fetched from a database
VIEWID = 123456
parameters = {'ids': 'ga' + VIEWID,
'dimensions': 'ga:pagePath',
'filters':
'ga:pagePath=~(signupSystem\/subscribe\.action\?pageSequence=1&briefName=BusinessTraveler).*',
'metrics': 'ga:pageviews'
}
# make a Query object with this data by calling the query constructor with the parameters dict
q = Query(params=parameters)
# assign date parameters dynamically, perhaps by generating one using the datetime module; hardcoded here though
q.start_date = '2014-03-10'
q.end_date = '2014-04-09'
# make give the query to something that can run it
# (how we got one of these isn't shown here so this code will fail)
result = query_runner(q)
# save results to database
result.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment