将对象传递给自己的初始化?

时间:2013-02-18 16:11:42

标签: python recursion google-analytics

我目前正在尝试使用GA报告API。我创建了一个Query对象,它基本上获取了查询实例时的所有参数。

但是,我想在查询本身的初始化中获取查询结果。我有一个方法将查询对象转换为一组结果,称为query_to_results(x),其中参数x是查询对象。

现在我正在创建一个带有空结果参数的Query对象,然后使用setter方法将结果添加到查询中。但是,我想在初始化对象的过程中得到结果。有没有简单的方法呢?

我会假设有以下几点:

self.results = query_to_results(self)

会起作用,但这不起作用。

这是创建查询对象的代码

class Query(object):
    def __init__(self,number_of_date_ranges):
        self.segments = gw.get_segments()
        self.sites = gw.get_sites_for_query()
        self.dates = []
        self.service = gw.service
        self.dim_met = gw.get_arguments()
        self.dimensions = self.dim_met[0]
        self.metrics = self.dim_met[1]
        self.results = []
        for x in range (number_of_date_ranges):
            self.dates.append(gw.get_dates())
    def __str__(self):
        output_string = 'segments:'
        for x in self.segments:
            output_string += str(x)
        output_string += '\n\nsites:\n'
        for y in self.sites:
            output_string += str(y)
        output_string += '\n\ndates:\n' 
        for z in self.dates:
            output_string+=str(z.items())
        output_string += ('\n\n'+'dimensions:'+self.dimensions+'\n\n'+'metrics:\n'+self.metrics)
        return output_string
    def get_segments(self):
        return self.segments
    def get_sites(self):
        return self.sites
    def get_dates(self):
        return self.dates
    def get_dimensions(self):
        return self.dimensions
    def get_metrics(self):
        return self.metrics
    def get_service(self):
        return self.service
    def get_results(self):
        return self.results
    def set_results(self,query_object):
        self.results = query_to_results(query_object)

这是将对象转换为一组结果的代码:

def get_results(service,profile_id,segment_id,date1,date2,dimensionString,metricString):
    try:
        return service.data().ga().get(
            ids='ga:' + profile_id,
            start_date=date1,
            end_date=date2,
            segment=segment_id,
            dimensions=dimensionString,
            metrics=metricString).execute()

def query_to_results(query_object):
    service = hello_analytics_api_v3_auth.initialize_service()
    accounts = service.management().accounts().list().execute()
    results_list = []
    parameters = extract_arguments(query_object)
    for site in parameters['profiles']:
        for segment in parameters['segments']:
            for dates in range (len(parameters['dates'])):
                results_list.append(get_results(service,site,segment,parameters['dates'][dates]['start'],parameters['dates'][dates]['end'],parameters['dimensions'],parameters['metrics']))
                time.sleep(0.5)
            time.sleep(0.1)
        time.sleep(0.1)
    time.sleep(0.1)
    return results_list


def extract_arguments(query_object):
    """Gets Arguments From Query Object. If no Segments given, assumes all visits. If no metrics given, assumes visits"""
    metricString = ''
    dimensionString = ''
    dateList = []
    segmentList = []
    webPropertyIdList = []
    for x in query_object.get_sites():
        webPropertyIdList.append(gw.site_name_to_true_webPropertyId[x])
    for y in query_object.get_segments():
        segmentList.append('gaid::'+str(y))
    for z in query_object.get_dates():
        dateList.append(z)
    if len(segmentList)==0:
        segmentList.append('gaid::-1')
    dimensionString = query_object.get_dimensions()
    metricString = query_object.get_metrics()
    if len(metricString)==0:
        metricString = 'ga:visits'
    return {"metrics":metricString,
            "dimensions":dimensionString,
            "dates":dateList,
            "segments":segmentList,
            "profiles":webPropertyIdList}

0 个答案:

没有答案