app/lists.py
async def import_sub_activity(self): # pylint: disable=too-many-locals
        """Requests each subscriber's recent activity.

        First, gets a list of subscribers.
        Then makes the requests one-by-one using aiohttp (MailChimp's API is
        very inefficient and you cannot request multiple subscribers' activity
        at the same time).
        After the requests have completed, parses the results, turns them into
        a pandas dataframe, and merges this dataframe with the members
        dataframe created by import_list_members().
        """
        params = (
            ('fields', 'activity.action,activity.timestamp,email_id'),
            ('exclude_fields', 'total_items,_links')
        )

        request_uri = ('https://{}.api.mailchimp.com/3.0/lists/{}/members/'
                       '{}/activity'.format(self.data_center, self.id, '{}'))

        # List of async tasks to do
        tasks = []

        # Placeholder for async responses
        responses = None

        # Semaphore to limit max simultaneous connections to MailChimp API
        sem = asyncio.Semaphore(self.MAX_ACTIVITY_CONNECTIONS)

        # Get a list of unique subscriber ids
        subscriber_list = self.get_list_ids()

        # Store the number of subscribers for later
        self.subscribers = len(subscriber_list)

        # Create a session with which to make requests
        async with ClientSession() as session:
            for subscriber_id in subscriber_list:

                # Format the request string
                request_string = request_uri.format(subscriber_id)

                # Add a new import task to the queue for each list subscriber
                task = asyncio.ensure_future(
                    self.make_async_requests(
                        sem, request_string, params, session))
                tasks.append(task)

            # Await completion of all requests and gather results
            responses = await asyncio.gather(*tasks)

        # Calculate timestamp for one year ago
        now = datetime.now(timezone.utc)
        one_year_ago = now - timedelta(days=365)

        # Flatten responses
        # Filter out activity older than one year
        activities = [{**{'id': response['email_id']},
                       **{'recent_open': d['timestamp']
                          for d in response['activity']
                          if d['action'] == 'open' and
                          iso8601.parse_date(d['timestamp']) > one_year_ago}}
                      for response in responses]

        # Convert results to a dataframe
        subscriber_activities = pd.DataFrame(activities)

        # Merge dataframes if any subscribers have recently opened
        # Else add an empty recent_open column to dataframe
        # This allows us to assume that a 'recent open' column exists
        if 'recent_open' in subscriber_activities:
            self.df = pd.merge(self.df,
                               subscriber_activities,
                               on='id',
                               how='left')
        else:
            self.df['recent_open'] = np.NaN
Requests each subscriber's recent activity.

        First, gets a list of subscribers.
        Then makes the requests one-by-one using aiohttp (MailChimp's API is
        very inefficient and you cannot request multiple subscribers' activity
        at the same time).
        After the requests have completed, parses the results, turns them into
        a pandas dataframe, and merges this dataframe with the members
        dataframe created by import_list_members().
        
Requests each subscriber's recent activity.
First, gets a list of subscribers.
Then makes the requests one-by-one using aiohttp (MailChimp's API is
very inefficient and you cannot request multiple subscribers' activity
at the same time).
After the requests have completed, parses the results, turns them into
a pandas dataframe, and merges this dataframe with the members
dataframe created by import_list_members().
Requests each subscriber ' s recent activity . First gets a list of subscribers . Then makes the requests one - by - one using aiohttp ( MailChimp ' s API is very inefficient and you cannot request multiple subscribers ' activity at the same time ) . After the requests have completed parses the results turns them into a pandas dataframe and merges this dataframe with the members dataframe created by import_list_members () .
async def import_sub_activity(self): 
        params = (
            ('fields', 'activity.action,activity.timestamp,email_id'),
            ('exclude_fields', 'total_items,_links')
        )
        request_uri = ('https://{}.api.mailchimp.com/3.0/lists/{}/members/'
                       '{}/activity'.format(self.data_center, self.id, '{}'))
        tasks = []
        responses = None
        sem = asyncio.Semaphore(self.MAX_ACTIVITY_CONNECTIONS)
        subscriber_list = self.get_list_ids()
        self.subscribers = len(subscriber_list)
        async with ClientSession() as session:
            for subscriber_id in subscriber_list:
                request_string = request_uri.format(subscriber_id)
                task = asyncio.ensure_future(
                    self.make_async_requests(
                        sem, request_string, params, session))
                tasks.append(task)
            responses = await asyncio.gather(*tasks)
        now = datetime.now(timezone.utc)
        one_year_ago = now - timedelta(days=365)
        activities = [{**{'id': response['email_id']},
                       **{'recent_open': d['timestamp']
                          for d in response['activity']
                          if d['action'] == 'open' and
                          iso8601.parse_date(d['timestamp']) > one_year_ago}}
                      for response in responses]
        subscriber_activities = pd.DataFrame(activities)
        if 'recent_open' in subscriber_activities:
            self.df = pd.merge(self.df,
                               subscriber_activities,
                               on='id',
                               how='left')
        else:
            self.df['recent_open'] = np.NaN
async def import_sub_activity ( self ) : params = ( ( 'fields' , 'activity.action,activity.timestamp,email_id' ) , ( 'exclude_fields' , 'total_items,_links' ) ) request_uri = ( 'https://{}.api.mailchimp.com/3.0/lists/{}/members/' '{}/activity' . format ( self . data_center , self . id , '{}' ) ) tasks = [ ] responses = None sem = asyncio . Semaphore ( self . MAX_ACTIVITY_CONNECTIONS ) subscriber_list = self . get_list_ids ( ) self . subscribers = len ( subscriber_list ) async with ClientSession ( ) as session : for subscriber_id in subscriber_list : request_string = request_uri . format ( subscriber_id ) task = asyncio . ensure_future ( self . make_async_requests ( sem , request_string , params , session ) ) tasks . append ( task ) responses = await asyncio . gather ( * tasks ) now = datetime . now ( timezone . utc ) one_year_ago = now - timedelta ( days = 365 ) activities = [ { ** { 'id' : response [ 'email_id' ] } , ** { 'recent_open' : d [ 'timestamp' ] for d in response [ 'activity' ] if d [ 'action' ] == 'open' and iso8601 . parse_date ( d [ 'timestamp' ] ) > one_year_ago } } for response in responses ] subscriber_activities = pd . DataFrame ( activities ) if 'recent_open' in subscriber_activities : self . df = pd . merge ( self . df , subscriber_activities , on = 'id' , how = 'left' ) else : self . df [ 'recent_open' ] = np . NaN
Requests each subscriber's recent activity.
Requests each subscriber ' s recent activity .
# pylint: disable=too-many-locals
 """Requests each subscriber's recent activity.

        First, gets a list of subscribers.
        Then makes the requests one-by-one using aiohttp (MailChimp's API is
        very inefficient and you cannot request multiple subscribers' activity
        at the same time).
        After the requests have completed, parses the results, turns them into
        a pandas dataframe, and merges this dataframe with the members
        dataframe created by import_list_members().
        """ # List of async tasks to do
 # Placeholder for async responses
 # Semaphore to limit max simultaneous connections to MailChimp API
 # Get a list of unique subscriber ids
 # Store the number of subscribers for later
 # Create a session with which to make requests
 # Format the request string
 # Add a new import task to the queue for each list subscriber
 # Await completion of all requests and gather results
 # Calculate timestamp for one year ago
 # Flatten responses
 # Filter out activity older than one year
 # Convert results to a dataframe
 # Merge dataframes if any subscribers have recently opened
 # Else add an empty recent_open column to dataframe
 # This allows us to assume that a 'recent open' column exists

self