예제 #1
0
    def result(self, index_names=[], level=-1):
        dfs = list()
        keys = list()
        unique_authors = 0
        interactions = 0

        for i, analysis in enumerate(self.analyses):
            if i in self.results and self.results[i]['status'] == 'completed':
                x = Utils.pylon_response_to_dataframes(self.results[i])
                dfs.append(x[level] if len(x) > 0 else None)
                keys.append(self.results[i]['name'])
                unique_authors += self.results[i]['result']['unique_authors']
                interactions += self.results[i]['result']['interactions']

        if dfs:
            if len(index_names) == 0:
                if isinstance(keys[0], tuple):
                    index_names = [
                        'level_%i' % e for e, i in enumerate(keys[0])
                    ]
                else:
                    index_names = ['level_0']

            index_names += dfs[0].index.names

            return AnalysisResult(pd.concat(dfs, keys=keys, names=index_names),
                                  unique_authors, interactions)
예제 #2
0
    def get(self, slug, period=None, country=None):

        try:
            resource = self.client.get_resource(slug,
                                                service=self.service,
                                                period=period,
                                                country=country).process()

            return AnalysisResult(
                Utils.pylon_response_to_dataframes(resource)[0],
                resource['result']['unique_authors'],
                resource['result']['interactions'])

        except DataSiftApiException as e:

            if e.response.status_code == 404:
                logging.warning(
                    'The requested resource was not found (slug: {0}, period: {1}, country: {2})'
                    .format(slug, period, country))
                raise ResourceNotFound('The requested resource was not found.',
                                       slug, period, country)
            else:
                logging.warning('Failed to fetch resource: ' +
                                str(e.response.status_code) + ', ' + str(e))
                raise e
예제 #3
0
    def result(self):
        if 0 in self.results and self.results[0]['status'] == 'completed':

            if self.results[0]['result']['analysis']['redacted'] or len(
                    self.results[0]['result']['analysis']['results']) == 0:
                raise RedactedResults

            return AnalysisResult(
                Utils.pylon_response_to_dataframes(self.results[0])[0],
                self.results[0]['result']['unique_authors'],
                self.results[0]['result']['interactions'])
예제 #4
0
    def complete_params(self, analysis, start=None, end=None, filter=None):

        if start is not None:
            analysis['start'] = start
        elif self.start is not None:
            analysis['start'] = self.start

        if end is not None:
            analysis['end'] = end
        elif self.end is not None:
            analysis['end'] = self.end

        if self.filter is not None:
            if filter is None:
                analysis['filter'] = self.filter
            else:
                analysis['filter'] = Utils.join_filters(
                    'AND', self.filter, filter)
        elif filter is not None and len(filter.strip()) > 0:
            analysis['filter'] = filter
 def test_both_none_returns_none(self):
     res = Utils.join_filters('AND', None, None)
     self.assertEqual(None, res)
 def test_both_non_empty_returns_joined(self):
     res = Utils.join_filters('AND', 'li.type exists', 'li.type exists')
     self.assertEqual(res, '(li.type exists) AND (li.type exists)')
 def test_first_empty_returns_second(self):
     res = Utils.join_filters('AND', '', 'li.type exists')
     self.assertEqual(res, 'li.type exists')
 def test_second_none_returns_first(self):
     res = Utils.join_filters('AND', 'li.type exists', None)
     self.assertEqual(res, 'li.type exists')
 def test_both_empty_returns_empty(self):
     res = Utils.join_filters('AND', '', '')
     self.assertEqual(len(res), 0)