Instruction:
How would you explain what the following Python 3 function does
def getTCPportConnCount(self, ipv4=True, ipv6=True, resolve_ports=False,
                            **kwargs):
        """Returns TCP connection counts for each local port.

        @param ipv4:          Include IPv4 ports in output if True.
        @param ipv6:          Include IPv6 ports in output if True.
        @param resolve_ports: Resolve numeric ports to names if True.
        @param **kwargs:      Keyword variables are used for filtering the
                              results depending on the values of the columns.
                              Each keyword must correspond to a field name with
                              an optional suffix:
                              field:          Field equal to value or in list
                                              of values.
                              field_ic:       Field equal to value or in list of
                                              values, using case insensitive
                                              comparison.
                              field_regex:    Field matches regex value or
                                              matches with any regex in list of
                                              values.
                              field_ic_regex: Field matches regex value or
                                              matches with any regex in list of
                                              values using case insensitive
                                              match.
        @return:              Dictionary mapping port number or name to the
                              number of established connections.

        """
        port_dict = {}
        result = self.getStats(tcp=True, udp=False,
                               include_listen=False, ipv4=ipv4,
                               ipv6=ipv6, resolve_ports=resolve_ports,
                               **kwargs)
        stats = result['stats']
        for stat in stats:
            if stat[8] == 'ESTABLISHED':
                port_dict[stat[5]] = port_dict.get(5, 0) + 1
        return port_dict

Response: Returns a dictionary mapping local port number or name to the number of established connections.


Instruction:
Write a Python 3 script to
compute the accuracy of the recalled item and item in the specified egg.

Response:
def accuracy_helper(egg, match='exact', distance='euclidean',
                    features=None):
    """
    Computes proportion of words recalled

    Parameters
    ----------
    egg : quail.Egg
        Data to analyze

    match : str (exact, best or smooth)
        Matching approach to compute recall matrix.  If exact, the presented and
        recalled items must be identical (default).  If best, the recalled item
        that is most similar to the presented items will be selected. If smooth,
        a weighted average of all presented items will be used, where the
        weights are derived from the similarity between the recalled item and
        each presented item.

    distance : str
        The distance function used to compare presented and recalled items.
        Applies only to 'best' and 'smooth' matching approaches.  Can be any
        distance function supported by numpy.spatial.distance.cdist.

    Returns
    ----------
    prop_recalled : numpy array
      proportion of words recalled

    """

    def acc(lst):
        return len([i for i in np.unique(lst) if i>=0])/(egg.list_length)

    opts = dict(match=match, distance=distance, features=features)
    if match is 'exact':
        opts.update({'features' : 'item'})
    recmat = recall_matrix(egg, **opts)

    if match in ['exact', 'best']:
        result = [acc(lst) for lst in recmat]
    elif match is 'smooth':
        result = np.mean(recmat, axis=1)
    else:
        raise ValueError('Match must be set to exact, best or smooth.')

    return np.nanmean(result, axis=0)


Instruction:
Write a Python 3 script to
establish connection to PostgreSQL Database.

Response:
def _connect(self):
        """Establish connection to PostgreSQL Database."""
        if self._connParams:
            self._conn = psycopg2.connect(**self._connParams)
        else:
            self._conn = psycopg2.connect('')
        try:
            ver_str = self._conn.get_parameter_status('server_version')
        except AttributeError:
            ver_str = self.getParam('server_version')
        self._version = util.SoftwareVersion(ver_str)


Instruction:
Implement a Python 3 function for
executing a simple query which returns a single column.

Response:
def _simpleQuery(self, query):
        """Executes simple query which returns a single column.

        @param query: Query string.
        @return:      Query result string.

        """
        cur = self._conn.cursor()
        cur.execute(query)
        row = cur.fetchone()
        return util.parse_value(row[0])


Instruction:
Implement a function in Python 3 to
return value of Run - time Database Parameter key.

Response:
def getParam(self, key):
        """Returns value of Run-time Database Parameter 'key'.

        @param key: Run-time parameter name.
        @return:    Run-time parameter value.

        """
        cur = self._conn.cursor()
        cur.execute("SHOW %s" % key)
        row = cur.fetchone()
        return util.parse_value(row[0])


Instruction:
Can you generate a brief explanation for the following Python 3 code
def getConnectionStats(self):
        cur = self._conn.cursor()
        cur.execute("""SELECT datname,numbackends FROM pg_stat_database;""")
        rows = cur.fetchall()
        if rows:
            return dict(rows)
        else:
            return {}

Response: Returns dictionary with number of connections for each database.


Instruction:
Implement a function in Python 3 to
return database block read transaction and tuple stats for each database.

Response:
def getDatabaseStats(self):
        """Returns database block read, transaction and tuple stats for each
        database.

        @return: Nested dictionary of stats.

        """
        headers = ('datname', 'numbackends', 'xact_commit', 'xact_rollback',
                   'blks_read', 'blks_hit', 'tup_returned', 'tup_fetched',
                   'tup_inserted', 'tup_updated', 'tup_deleted', 'disk_size')
        cur = self._conn.cursor()
        cur.execute("SELECT %s, pg_database_size(datname) FROM pg_stat_database;"
                    % ",".join(headers[:-1]))
        rows = cur.fetchall()
        dbstats = self._createStatsDict(headers, rows)
        totals = self._createTotalsDict(headers, rows)
        return {'databases': dbstats, 'totals': totals}


Instruction:
Write a Python 3 function that can
return the number of active lock discriminated by lock mode.

Response:
def getLockStatsMode(self):
        """Returns the number of active lock discriminated by lock mode.

        @return: : Dictionary of stats.

        """
        info_dict = {'all': dict(zip(self.lockModes, (0,) * len(self.lockModes))),
                     'wait': dict(zip(self.lockModes, (0,) * len(self.lockModes)))}
        cur = self._conn.cursor()
        cur.execute("SELECT TRIM(mode, 'Lock'), granted, COUNT(*) FROM pg_locks "
                    "GROUP BY TRIM(mode, 'Lock'), granted;")
        rows = cur.fetchall()
        for (mode, granted, cnt) in rows:
            info_dict['all'][mode] += cnt
            if not granted:
                info_dict['wait'][mode] += cnt
        return info_dict


Instruction:
Can you generate the documentation for the following Python 3 function
def getLockStatsDB(self):
        info_dict = {'all': {},
                     'wait': {}}
        cur = self._conn.cursor()
        cur.execute("SELECT d.datname, l.granted, COUNT(*) FROM pg_database d "
                    "JOIN pg_locks l ON d.oid=l.database "
                    "GROUP BY d.datname, l.granted;")
        rows = cur.fetchall()
        for (db, granted, cnt) in rows:
            info_dict['all'][db] = info_dict['all'].get(db, 0) + cnt
            if not granted:
                info_dict['wait'][db] = info_dict['wait'].get(db, 0) + cnt
        return info_dict

Response: Returns the number of active lock discriminated by database.


Instruction:
Write a Python 3 script for
returning Global Background Writer and Checkpoint Activity stats.

Response:
def getBgWriterStats(self):
        """Returns Global Background Writer and Checkpoint Activity stats.

        @return: Nested dictionary of stats.

        """
        info_dict = {}
        if self.checkVersion('8.3'):
            cur = self._conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
            cur.execute("SELECT * FROM pg_stat_bgwriter")
            info_dict = cur.fetchone()
        return info_dict


Instruction:
Create a Python 3 function to
return a dictionary of status items.

Response:
def getXlogStatus(self):
        """Returns Transaction Logging or Recovery Status.

        @return: Dictionary of status items.

        """
        inRecovery = None
        if self.checkVersion('9.0'):
            inRecovery = self._simpleQuery("SELECT pg_is_in_recovery();")
        cur = self._conn.cursor()
        if inRecovery:
            cols = ['pg_last_xlog_receive_location()',
                    'pg_last_xlog_replay_location()',]
            headers = ['xlog_receive_location',
                       'xlog_replay_location',]
            if self.checkVersion('9.1'):
                cols.extend(['pg_last_xact_replay_timestamp()',
                             'pg_is_xlog_replay_paused()',])
                headers.extend(['xact_replay_timestamp',
                                'xlog_replay_paused',])
            cur.execute("""SELECT %s;""" % ','.join(cols))
            headers = ('xlog_receive_location', 'xlog_replay_location')
        else:
            cur.execute("""SELECT
                pg_current_xlog_location(),
                pg_xlogfile_name(pg_current_xlog_location());""")
            headers = ('xlog_location', 'xlog_filename')
        row = cur.fetchone()
        info_dict = dict(zip(headers, row))
        if inRecovery is not None:
            info_dict['in_recovery'] = inRecovery
        return info_dict


Instruction:
Given the following Python 3 function, write the documentation
def getSlaveStatus(self):
        info_dict = {}
        if self.checkVersion('9.1'):
            cols = ['procpid', 'usename', 'application_name',
                    'client_addr', 'client_port', 'backend_start', 'state',
                    'sent_location', 'write_location', 'flush_location',
                    'replay_location', 'sync_priority', 'sync_state',]
            cur = self._conn.cursor()
            cur.execute("""SELECT %s FROM pg_stat_replication;"""
                        % ','.join(cols))
            rows = cur.fetchall()
            for row in rows:
                info_dict[row[0]] = dict(zip(cols[1:], row[1:]))
        else:
            return None
        return info_dict

Response: Returns status of replication slaves.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _connect(self):
        if self._connParams:
            self._conn = MySQLdb.connect(**self._connParams)
        else:
            self._conn = MySQLdb.connect('')

Response: Establish connection to MySQL Database.


Instruction:
Write a Python 3 script to
return list of supported storage engines.

Response:
def getStorageEngines(self):
        """Returns list of supported storage engines.

        @return: List of storage engine names.

        """
        cur = self._conn.cursor()
        cur.execute("""SHOW STORAGE ENGINES;""")
        rows = cur.fetchall()
        if rows:
            return [row[0].lower() for row in rows if row[1] in ['YES', 'DEFAULT']]
        else:
            return []


Instruction:
Can you generate the documentation for the following Python 3 function
def getParam(self, key):
        cur = self._conn.cursor()
        cur.execute("SHOW GLOBAL VARIABLES LIKE %s", key)
        row = cur.fetchone()
        return int(row[1])

Response: Returns value of Run - time Database Parameter key.


Instruction:
Can you generate the documentation for the following Python 3 function
def getParams(self):
        cur = self._conn.cursor()
        cur.execute("SHOW GLOBAL VARIABLES")
        rows = cur.fetchall()
        info_dict = {}
        for row in rows:
            key = row[0]
            val = util.parse_value(row[1])
            info_dict[key] = val
        return info_dict

Response: Returns dictionary of all run - time parameters.


Instruction:
Can you tell what is the following Python 3 function doing
def getProcessStatus(self):
        info_dict = {}
        cur = self._conn.cursor()
        cur.execute("""SHOW FULL PROCESSLIST;""")
        rows = cur.fetchall()
        if rows:
            for row in rows:
                if row[6] == '':
                    state = 'idle'
                elif row[6] is None:
                    state = 'other'
                else:
                    state = str(row[6]).replace(' ', '_').lower()
                info_dict[state] = info_dict.get(state, 0) + 1
        return info_dict

Response:
Returns the number of processes discriminated by state.
AttributeNames - The process names of the processes in the process list.


Instruction:
How would you implement a function in Python 3 that
returns number of processes discriminated by database name.

Response:
def getProcessDatabase(self):
        """Returns number of processes discriminated by database name.

        @return: Dictionary mapping database name to number of processes.

        """
        info_dict = {}
        cur = self._conn.cursor()
        cur.execute("""SHOW FULL PROCESSLIST;""")
        rows = cur.fetchall()
        if rows:
            for row in rows:
                db = row[3]
                info_dict[db] = info_dict.get(db, 0) + 1
        return info_dict


Instruction:
Here you have a function in Python 3, explain what it does
def getDatabases(self):
        cur = self._conn.cursor()
        cur.execute("""SHOW DATABASES;""")
        rows = cur.fetchall()
        if rows:
            return [row[0] for row in rows]
        else:
            return []

Response: Returns list of databases.


Instruction:
Can you tell what is the following Python 3 function doing
def spc_helper(egg, match='exact', distance='euclidean',
               features=None):
    """
    Computes probability of a word being recalled (in the appropriate recall list), given its presentation position

    Parameters
    ----------
    egg : quail.Egg
        Data to analyze

    match : str (exact, best or smooth)
        Matching approach to compute recall matrix.  If exact, the presented and
        recalled items must be identical (default).  If best, the recalled item
        that is most similar to the presented items will be selected. If smooth,
        a weighted average of all presented items will be used, where the
        weights are derived from the similarity between the recalled item and
        each presented item.

    distance : str
        The distance function used to compare presented and recalled items.
        Applies only to 'best' and 'smooth' matching approaches.  Can be any
        distance function supported by numpy.spatial.distance.cdist.

    Returns
    ----------
    prec : numpy array
      each number represents the probability of recall for a word presented in given position/index

    """

    def spc(lst):
        d = np.zeros_like(egg.pres.values[0])
        inds = np.array(lst[~np.isnan(lst)]).astype(int)
        d[inds-1]=1
        return d

    opts = dict(match=match, distance=distance, features=features)
    if match is 'exact':
        opts.update({'features' : 'item'})
    recmat = recall_matrix(egg, **opts)

    if match in ['exact', 'best']:
        result = [spc(lst) for lst in recmat]
    elif match == 'smooth':
        result = np.nanmean(recmat, 2)
    else:
        raise ValueError('Match must be set to exact, best or smooth.')
    return np.mean(result, 0)

Response: Returns a sequence of probability of a word being recalled given its presentation position and the number of items presented in the item.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _retrieve(self):
        url = "%s://%s:%d/manager/status" % (self._proto, self._host, self._port)
        params = {}
        params['XML'] = 'true'
        response = util.get_url(url, self._user, self._password, params)
        tree = ElementTree.XML(response)
        return tree

Response:
Query Apache Tomcat Server Status Page in XML format and return
        the result as an ElementTree object.


Instruction:
Write a Python 3 script to
return JVM Memory Stats for Apache Tomcat Server.

Response:
def getMemoryStats(self):
        """Return JVM Memory Stats for Apache Tomcat Server.

        @return: Dictionary of memory utilization stats.

        """
        if self._statusxml is None:
            self.initStats()
        node = self._statusxml.find('jvm/memory')
        memstats = {}
        if node is not None:
            for (key,val) in node.items():
                memstats[key] = util.parse_value(val)
        return memstats


Instruction:
Can you create a Python 3 function that
returns nested dictionary of Connector Stats for Apache Tomcat Server.

Response:
def getConnectorStats(self):
        """Return dictionary of Connector Stats for Apache Tomcat Server.

        @return: Nested dictionary of Connector Stats.

        """
        if self._statusxml is None:
            self.initStats()
        connnodes = self._statusxml.findall('connector')
        connstats = {}
        if connnodes:
            for connnode in connnodes:
                namestr = connnode.get('name')
                if namestr is not None:
                    mobj = re.match('(.*)-(\d+)', namestr)
                    if mobj:
                        proto = mobj.group(1)
                        port = int(mobj.group(2))
                        connstats[port] = {'proto': proto}
                        for tag in ('threadInfo', 'requestInfo'):
                            stats = {}
                            node = connnode.find(tag)
                            if node is not None:
                                for (key,val) in node.items():
                                    if re.search('Time$', key):
                                        stats[key] = float(val) / 1000.0
                                    else:
                                        stats[key] = util.parse_value(val)
                            if stats:
                                connstats[port][tag] = stats
        return connstats


Instruction:
How would you implement a function in Python 3 that
loads an egg or fried eggs and example data from disk.

Response:
def load(filepath, update=True):
    """
    Loads eggs, fried eggs ands example data

    Parameters
    ----------
    filepath : str
        Location of file

    update : bool
        If true, updates egg to latest format

    Returns
    ----------
    data : quail.Egg or quail.FriedEgg
        Data loaded from disk

    """

    if filepath == 'automatic' or filepath == 'example':
        fpath = os.path.dirname(os.path.abspath(__file__)) + '/data/automatic.egg'
        return load_egg(fpath)
    elif filepath == 'manual':
        fpath = os.path.dirname(os.path.abspath(__file__)) + '/data/manual.egg'
        return load_egg(fpath, update=False)
    elif filepath == 'naturalistic':
        fpath = os.path.dirname(os.path.abspath(__file__)) + '/data/naturalistic.egg'
    elif filepath.split('.')[-1]=='egg':
        return load_egg(filepath, update=update)
    elif filepath.split('.')[-1]=='fegg':
        return load_fegg(filepath, update=False)
    else:
        raise ValueError('Could not load file.')


Instruction:
Make a summary of the following Python 3 code
def load_fegg(filepath, update=True):
    try:
        egg = FriedEgg(**dd.io.load(filepath))
    except ValueError as e:
        print(e)
        # if error, try loading old format
        with open(filepath, 'rb') as f:
            egg = pickle.load(f)

    if update:
        return egg.crack()
    else:
        return egg

Response: Loads a pickled version of the object at filepath.


Instruction:
Can you create a Python 3 function that
loads a pickled egg into a new object

Response:
def load_egg(filepath, update=True):
    """
    Loads pickled egg

    Parameters
    ----------
    filepath : str
        Location of pickled egg

    update : bool
        If true, updates egg to latest format

    Returns
    ----------
    egg : Egg data object
        A loaded unpickled egg

    """
    try:
        egg = Egg(**dd.io.load(filepath))
    except:
        # if error, try loading old format
        with open(filepath, 'rb') as f:
            egg = pickle.load(f)

    if update:
        if egg.meta:
            old_meta = egg.meta
            egg.crack()
            egg.meta = old_meta
            return egg
        else:
            return egg.crack()
    else:
        return egg


Instruction:
Explain what the following Python 3 code does
def loadEL(dbpath=None, recpath=None, remove_subs=None, wordpool=None, groupby=None, experiments=None,
    filters=None):
    '''
    Function that loads sql files generated by autoFR Experiment
    '''

    assert (dbpath is not None), "You must specify a db file or files."
    assert (recpath is not None), "You must specify a recall folder."
    assert (wordpool is not None), "You must specify a wordpool file."
    assert (experiments is not None), "You must specify a list of experiments"

    ############################################################################
    # subfunctions #############################################################

    def db2df(db, filter_func=None):
        '''
        Loads db file and converts to dataframe
        '''
        db_url = "sqlite:///" + db
        table_name = 'turkdemo'
        data_column_name = 'datastring'

        # boilerplace sqlalchemy setup
        engine = create_engine(db_url)
        metadata = MetaData()
        metadata.bind = engine
        table = Table(table_name, metadata, autoload=True)

        # make a query and loop through
        s = table.select()
        rows = s.execute()

        data = []
        for row in rows:
            data.append(row[data_column_name])

        # parse each participant's datastring as json object
        # and take the 'data' sub-object
        data = [json.loads(part)['data'] for part in data if part is not None]

        # remove duplicate subject data for debugXG82XV:debug7XPXQA
        # data[110] = data[110][348:]

        # insert uniqueid field into trialdata in case it wasn't added
        # in experiment:
        for part in data:
            for record in part:
        #         print(record)
                if type(record['trialdata']) is list:

                    record['trialdata'] = {record['trialdata'][0]:record['trialdata'][1]}
                record['trialdata']['uniqueid'] = record['uniqueid']

        # flatten nested list so we just have a list of the trialdata recorded
        # each time psiturk.recordTrialData(trialdata) was called.
        def isNotNumber(s):
            try:
                float(s)
                return False
            except ValueError:
                return True

        data = [record['trialdata'] for part in data for record in part]

        # filter out fields that we dont want using isNotNumber function
        filtered_data = [{k:v for (k,v) in list(part.items()) if isNotNumber(k)} for part in data]

        # Put all subjects' trial data into a dataframe object from the
        # 'pandas' python library: one option among many for analysis
        data_frame = pd.DataFrame(filtered_data)

        data_column_name = 'codeversion'

        # boilerplace sqlalchemy setup
        engine = create_engine(db_url)
        metadata = MetaData()
        metadata.bind = engine
        table = Table(table_name, metadata, autoload=True)

        # make a query and loop through
        s = table.select()
        rows = s.execute()

        versions = []
        version_dict = {}
        for row in rows:
            version_dict[row[0]]=row[data_column_name]

        version_col = []
        for idx,sub in enumerate(data_frame['uniqueid'].unique()):
            for i in range(sum(data_frame['uniqueid']==sub)):
                version_col.append(version_dict[sub])
        data_frame['exp_version']=version_col

        if filter_func:
            for idx,filt in enumerate(filter_func):
                data_frame = filt(data_frame)
        return data_frame

    # custom filter to clean db file
    def experimenter_filter(data_frame):
        data=[]
        indexes=[]
        for line in data_frame.iterrows():
            try:
                if json.loads(line[1]['responses'])['Q1'].lower() in ['kirsten','allison','allison\nallison','marisol', 'marisol ','marisiol', 'maddy','campbell', 'campbell field', 'kirsten\nkirsten', 'emily', 'bryan', 'armando', 'armando ortiz',
                                                                      'maddy/lucy','paxton', 'lucy','campbell\ncampbell','madison','darya','rachael']:
                    delete = False
                else:
                    delete = True
            except:
                pass

            if delete:
                indexes.append(line[0])

        return data_frame.drop(indexes)

    def adaptive_filter(data_frame):
        data=[]
        indexes=[]
        subjcb={}
        for line in data_frame.iterrows():
            try:
                if 'Q2' in json.loads(line[1]['responses']):
                    delete = False
                else:
                    delete = False
            except:
                pass

            if delete:
                indexes.append(line[0])
        return data_frame.drop(indexes)

    def experiments_filter(data_frame):
        indexes=[]
        for line in data_frame.iterrows():
            try:
                if line[1]['exp_version'] in experiments:
                    delete = False
                else:
                    delete = True
            except:
                pass

            if delete:
                indexes.append(line[0])
        return data_frame.drop(indexes)

    # this function takes the data frame and returns subject specific data based on the subid variable
    def filterData(data_frame,subid):
        filtered_stim_data = data_frame[data_frame['stimulus'].notnull() & data_frame['listNumber'].notnull()]
        filtered_stim_data = filtered_stim_data[filtered_stim_data['trial_type']=='single-stim']
        filtered_stim_data =  filtered_stim_data[filtered_stim_data['uniqueid']==subid]
        return filtered_stim_data

    def createStimDict(data):
        stimDict = []
        for index, row in data.iterrows():
            try:
                stimDict.append({
                        'text': str(re.findall('>(.+)<',row['stimulus'])[0]),
                        'color' : { 'r' : int(re.findall('rgb\((.+)\)',row['stimulus'])[0].split(',')[0]),
                                   'g' : int(re.findall('rgb\((.+)\)',row['stimulus'])[0].split(',')[1]),
                                   'b' : int(re.findall('rgb\((.+)\)',row['stimulus'])[0].split(',')[2])
                                   },
                        'location' : {
                            'top': float(re.findall('top:(.+)\%;', row['stimulus'])[0]),
                            'left' : float(re.findall('left:(.+)\%', row['stimulus'])[0])
                            },
                        'category' : wordpool['CATEGORY'].iloc[list(wordpool['WORD'].values).index(str(re.findall('>(.+)<',row['stimulus'])[0]))],
                        'size' : wordpool['SIZE'].iloc[list(wordpool['WORD'].values).index(str(re.findall('>(.+)<',row['stimulus'])[0]))],
                        'wordLength' : len(str(re.findall('>(.+)<',row['stimulus'])[0])),
                        'firstLetter' : str(re.findall('>(.+)<',row['stimulus'])[0])[0],
                        'listnum' : row['listNumber']
                    })
            except:
                stimDict.append({
                        'text': str(re.findall('>(.+)<',row['stimulus'])[0]),
                        'color' : { 'r' : 0,
                                   'g' : 0,
                                   'b' : 0
                                   },
                        'location' : {
                            'top': 50,
                            'left' : 50
                            },
                        'category' : wordpool['CATEGORY'].iloc[list(wordpool['WORD'].values).index(str(re.findall('>(.+)<',row['stimulus'])[0]))],
                        'size' : wordpool['SIZE'].iloc[list(wordpool['WORD'].values).index(str(re.findall('>(.+)<',row['stimulus'])[0]))],
                        'wordLength' : len(str(re.findall('>(.+)<',row['stimulus'])[0])),
                        'firstLetter' : str(re.findall('>(.+)<',row['stimulus'])[0])[0],
                        'listnum' : row['listNumber']
                    })
        return stimDict

    # this function loads in the recall data into an array of arrays, where each array represents a list of words
    def loadRecallData(subid):
        recalledWords = []
        for i in range(0,16):
            try:
                f = open(recpath + subid + '/' + subid + '-' + str(i) + '.wav.txt', 'r')
                spamreader = csv.reader(f, delimiter=',', quotechar='|')
            except (IOError, OSError) as e:
                try:
                    f = open(recpath + subid + '-' + str(i) + '.wav.txt', 'r')
                    spamreader = csv.reader(f, delimiter=',', quotechar='|')
                except (IOError, OSError) as e:
                    print(e)
            try:
                words=[]
                altformat=True
                for row in spamreader:
                    if len(row)>1:
                        recalledWords.append(row)
                        altformat=False
                        break
                    else:
                        try:
                            words.append(row[0])
                        except:
                            pass
                if altformat:
                    recalledWords.append(words)
            except:
                print('couldnt process '+ recpath + subid + '/' + subid + '-' + str(i) + '.wav.txt')
        return recalledWords

    # this function computes accuracy for a series of lists
    def computeListAcc(stimDict,recalledWords):
        accVec = []
        for i in range(0,16):
            stim = [stim['text'] for stim in stimDict if stim['listnum']==i]
            recalled= recalledWords[i]

            acc = 0
            tmpstim = stim[:]
            for word in recalled:
                if word in tmpstim:
                    tmpstim.remove(word)
                    acc+=1
            accVec.append(acc/len(stim))
        return accVec

    def getFeatures(stimDict):
        stimDict_copy = stimDict[:]
        for item in stimDict_copy:
            item['location'] = [item['location']['top'], item['location']['left']]
            item['color'] = [item['color']['r'], item['color']['g'], item['color']['b']]
            item.pop('text', None)
            item.pop('listnum', None)
        stimDict_copy = [stimDict_copy[i:i+16] for i in range(0, len(stimDict_copy), 16)]
        return stimDict_copy

    ############################################################################
    # main program #############################################################

    # if its not a list, make it one
    if type(dbpath) is not list:
        dbpath = [dbpath]

    # read in stimulus library
    wordpool = pd.read_csv(wordpool)

    # add custom filters
    if filters:
        filter_func = [adaptive_filter, experimeter_filter, experiments_filter] + filters
    else:
        filter_func = [adaptive_filter, experimenter_filter, experiments_filter]

    # load in dbs and convert to df, and filter
    dfs = [db2df(db, filter_func=filter_func) for db in dbpath]

    # concatenate the db files
    df = pd.concat(dfs)

    # subjects who have completed the exp
    subids = list(df[df['listNumber']==15]['uniqueid'].unique())

    # remove problematic subjects
    if remove_subs:
        for sub in remove_subs:
            try:
                subids.remove(sub)
            except:
                print('Could not find subject: ' + sub + ', skipping...')

    # set up data structure to load in subjects
    if groupby:
        pres = [[] for i in range(len(groupby['exp_version']))]
        rec = [[] for i in range(len(groupby['exp_version']))]
        features = [[] for i in range(len(groupby['exp_version']))]
        subs = [[] for i in range(len(groupby['exp_version']))]

        # make each groupby item a list
        groupby = [exp if type(exp) is list else [exp] for exp in groupby['exp_version']]

    else:
        pres = [[]]
        rec = [[]]
        features = [[]]
        subs = [[]]

    # for each subject that completed the experiment
    for idx,sub in enumerate(subids):

        # get the subjects data
        filteredStimData = filterData(df,sub)

        if filteredStimData['exp_version'].values[0] in experiments:

            # create stim dict
            stimDict = createStimDict(filteredStimData)

            sub_data = pd.DataFrame(stimDict)
            sub_data['subject']=idx
            sub_data['experiment']=filteredStimData['exp_version'].values[0]
            sub_data = sub_data[['experiment','subject','listnum','text','category','color','location','firstLetter','size','wordLength']]

            # get features from stim dict
            feats = getFeatures(stimDict)

            # load in the recall data
            recalledWords = loadRecallData(sub)

            # get experiment version
            exp_version = filteredStimData['exp_version'].values[0]

            # find the idx of the experiment for this subjects
            if groupby:
                exp_idx = list(np.where([exp_version in item for item in groupby])[0])
            else:
                exp_idx = [0]

            if exp_idx != []:
                pres[exp_idx[0]].append([list(sub_data[sub_data['listnum']==lst]['text'].values) for lst in sub_data['listnum'].unique()])
                rec[exp_idx[0]].append(recalledWords)
                features[exp_idx[0]].append(feats)
                subs[exp_idx[0]].append(sub)

    eggs = [Egg(pres=ipres, rec=irec, features=ifeatures, meta={'ids' : isub}) for ipres,irec,ifeatures,isub in zip(pres, rec, features, subs)]

    # map feature dictionaries in pres df to rec df
    def checkword(x):
        if x is None:
            return x
        else:
            try:
                return stim_dict[x['item']]
            except:
                return x

    # convert utf-8 bytes type to string
    def update_types(egg):
        featlist = list(egg.pres.loc[0].loc[0].values.tolist()[0].keys())
        def update1df(df):
            for sub in range(egg.n_subjects):
                for liszt in range(egg.n_lists):
                    for item in range(len(df.loc[sub].loc[liszt].values.tolist())):
                        for feat in featlist:
                            if feat in df.loc[sub].loc[liszt].values.tolist()[item].keys():
                                if isinstance(df.loc[sub].loc[liszt].values.tolist()[item][feat], np.bytes_):
                                    try:
                                        df.loc[sub].loc[liszt].values.tolist()[item][feat] = str(df.loc[sub].loc[liszt].values.tolist()[item][feat], 'utf-8')
                                    except:
                                        print("Subject " + str(sub) + ", list " + str(liszt) + ", item " + str(item) + ", feature " + str(feat) + ": Could not convert type " + str(type(egg.rec.loc[sub].loc[liszt].values.tolist()[item][feat])) + " to string.")
        update1df(egg.pres)
        update1df(egg.rec)

    for egg in eggs:
        update_types(egg)
        old_meta = egg.meta
        temp_eggs = [egg]

        for i in range(egg.n_subjects):
            e = egg.crack(subjects=[i])
            stim = e.pres.values.ravel()
            stim_dict = {str(x['item']) : {k:v for k, v in iter(x.items())} for x in stim}

            e.rec = e.rec.applymap(lambda x: checkword(x))
            temp_eggs.append(e)

        edited_egg = stack_eggs(temp_eggs)
        mapped_egg = edited_egg.crack(subjects=[i for i in range(egg.n_subjects,egg.n_subjects*2)])
        mapped_egg.meta = old_meta
        eggs[eggs.index(egg)] = mapped_egg

    if len(eggs)>1:
        return eggs
    else:
        return eggs[0]

Response: Function that loads an el file and converts it to a dataframe.


Instruction:
How would you explain what the following Python 3 function does
def load_example_data(dataset='automatic'):

    # can only be auto or manual
    assert dataset in ['automatic', 'manual', 'naturalistic'], "Dataset can only be automatic, manual, or naturalistic"

    if dataset == 'naturalistic':
        # open naturalistic egg
        egg = Egg(**dd.io.load(os.path.dirname(os.path.abspath(__file__)) + '/data/' + dataset + '.egg'))
    else:
    # open pickled egg
        try:
            with open(os.path.dirname(os.path.abspath(__file__)) + '/data/' + dataset + '.egg', 'rb') as handle:
                egg = pickle.load(handle)
        except:
            f = dd.io.load(os.path.dirname(os.path.abspath(__file__)) + '/data/' + dataset + '.egg')
            egg = Egg(pres=f['pres'], rec=f['rec'], dist_funcs=f['dist_funcs'],
                      subjgroup=f['subjgroup'], subjname=f['subjname'],
                      listgroup=f['listgroup'], listname=f['listname'],
                      date_created=f['date_created'])
    return egg.crack()

Response: Loads example data for a specific dataset.


Instruction:
Given the following Python 3 function, write the documentation
def retrieveVals(self):
        fs = FSinfo(self._fshost, self._fsport, self._fspass)
        if self.hasGraph('fs_calls'):
            count = fs.getCallCount()
            self.setGraphVal('fs_calls', 'calls', count)
        if self.hasGraph('fs_channels'):
            count = fs.getChannelCount()
            self.setGraphVal('fs_channels', 'channels', count)

Response: Retrieve values for graphs.


Instruction:
Make a summary of the following Python 3 code
def autoconf(self):
        fs = FSinfo(self._fshost, self._fsport, self._fspass)
        return fs is not None

Response: Implements Munin Plugin Auto - Configuration Option.


Instruction:
Create a Python 3 function for
solving the sparse linear system Ax = b.

Response:
def spsolve(A, b):
    """Solve the sparse linear system Ax=b, where b may be a vector or a matrix.

    Parameters
    ----------
    A : ndarray or sparse matrix
        The square matrix A will be converted into CSC or CSR form
    b : ndarray or sparse matrix
        The matrix or vector representing the right hand side of the equation.

    Returns
    -------
    x : ndarray or sparse matrix
        the solution of the sparse linear equation.
        If b is a vector, then x is a vector of size A.shape[0]
        If b is a matrix, then x is a matrix of size (A.shape[0],)+b.shape[1:]

    """
    x = UmfpackLU(A).solve(b)

    if b.ndim == 2 and b.shape[1] == 1:
        # compatibility with scipy.sparse.spsolve quirk
        return x.ravel()
    else:
        return x


Instruction:
Write a Python 3 script to
solve linear equation A x = b for all the entries in the logarithm A.

Response:
def solve(self, b):
        """
        Solve linear equation A x = b for x

        Parameters
        ----------
        b : ndarray
            Right-hand side of the matrix equation. Can be vector or a matrix.

        Returns
        -------
        x : ndarray
            Solution to the matrix equation

        """
        if isspmatrix(b):
            b = b.toarray()

        if b.shape[0] != self._A.shape[1]:
            raise ValueError("Shape of b is not compatible with that of A")

        b_arr = asarray(b, dtype=self._A.dtype).reshape(b.shape[0], -1)
        x = np.zeros((self._A.shape[0], b_arr.shape[1]), dtype=self._A.dtype)
        for j in range(b_arr.shape[1]):
            x[:,j] = self.umf.solve(UMFPACK_A, self._A, b_arr[:,j], autoTranspose=True)
        return x.reshape((self._A.shape[0],) + b.shape[1:])


Instruction:
Given the following Python 3 function, write the documentation
def recall_matrix(egg, match='exact', distance='euclidean', features=None):

    if match in ['best', 'smooth']:
        if not features:
            features = [k for k,v in egg.pres.loc[0][0].values[0].items() if k!='item']
            if not features:
                raise('No features found.  Cannot match with best or smooth strategy')

    if not isinstance(features, list):
        features = [features]

    if match=='exact':
        features=['item']
        return _recmat_exact(egg.pres, egg.rec, features)
    else:
        return _recmat_smooth(egg.pres, egg.rec, features, distance, match)

Response: Compute the recall matrix given a list of presented and list of recalled words and a distance function.


Instruction:
Can you tell what is the following Python 3 function doing
def retrieveVals(self):
        net_info = NetstatInfo()
        if self.hasGraph('netstat_conn_status'):
            stats = net_info.getTCPportConnStatus(include_listen=True)
            for fname in ('listen', 'established', 'syn_sent', 'syn_recv',
                          'fin_wait1', 'fin_wait2', 'time_wait',
                          'close','close_wait', 'last_ack', 'closing',
                          'unknown',):
                self.setGraphVal('netstat_conn_status', fname,
                                 stats.get(fname,0))
        if self.hasGraph('netstat_conn_server'):
            stats = net_info.getTCPportConnCount(localport=self._port_list)
            for srv in self._srv_list:
                numconn = 0
                for port in self._srv_dict[srv]:
                    numconn += stats.get(port, 0)
                self.setGraphVal('netstat_conn_server', srv, numconn)

Response: Retrieve values for graphs.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _parseFreePBXconf(self):
        amiuser = None
        amipass = None
        if os.path.isfile(confFileFreePBX):
            try:
                fp = open(confFileFreePBX, 'r')
                data = fp.read()
                fp.close()
            except:
                raise IOError('Failed reading FreePBX configuration file: %s'
                    % confFileFreePBX)
            for (key, val) in re.findall('^(AMPMGR\w+)\s*=\s*(\S+)\s*$',
                data, re.MULTILINE):
                if key == 'AMPMGRUSER':
                    amiuser = val
                elif key == 'AMPMGRPASS':
                    amipass = val
            if amiuser and amipass:
                self._amiuser = amiuser
                self._amipass = amipass
                return True
        return False

Response: Parses the FreePBX configuration file and sets the attributes self. _amiuser and self. _amipass attributes.


Instruction:
Write a Python 3 function that can
parse Asterisk configuration file for a specific Asterisk class. Returns True on success.

Response:
def _parseAsteriskConf(self):
        """Parses Asterisk configuration file /etc/asterisk/manager.conf for
        user and password for Manager Interface. Returns True on success.

        @return: True if configuration file is found and parsed successfully.

        """
        if os.path.isfile(confFileAMI):
            try:
                fp = open(confFileAMI, 'r')
                data = fp.read()
                fp.close()
            except:
                raise IOError('Failed reading Asterisk configuration file: %s'
                    % confFileAMI)
            mobj = re.search('^\[(\w+)\]\s*\r{0,1}\nsecret\s*=\s*(\S+)\s*$',
                             data, re.MULTILINE)
            if mobj:
                self._amiuser = mobj.group(1)
                self._amipass = mobj.group(2)
                return True
        return False


Instruction:
Here you have a function in Python 3, explain what it does
def _connect(self):
        try:
            if sys.version_info[:2] >= (2,6):
                self._conn = telnetlib.Telnet(self._amihost, self._amiport,
                                              connTimeout)
            else:
                self._conn = telnetlib.Telnet(self._amihost, self._amiport)
        except:
            raise Exception(
                "Connection to Asterisk Manager Interface on "
                "host %s and port %s failed."
                % (self._amihost, self._amiport)
                )

Response: Connect to Asterisk Manager Interface.


Instruction:
Can you tell what is the following Python 3 function doing
def _sendAction(self, action, attrs=None, chan_vars=None):
        self._conn.write("Action: %s\r\n" % action)
        if attrs:
            for (key,val) in attrs:
                self._conn.write("%s: %s\r\n" % (key, val))
        if chan_vars:
            for (key,val) in chan_vars:
                self._conn.write("Variable: %s=%s\r\n" % (key, val))
        self._conn.write("\r\n")

Response: Send an action to Asterisk Manager Interface.


Instruction:
Can you create a Python 3 function that
reads and parse response from Asterisk Manager Interface.

Response:
def _getResponse(self):
        """Read and parse response from Asterisk Manager Interface.

        @return: Dictionary with response key-value pairs.

        """
        resp_dict= dict()
        resp_str = self._conn.read_until("\r\n\r\n", connTimeout)
        for line in resp_str.split("\r\n"):
            mobj = re.match('(\w+):\s*(\S.*)$', line);
            if mobj:
                resp_dict[mobj.group(1)] = mobj.group(2)
            else:
                mobj = re.match('(.*)--END COMMAND--\s*$', line, flags=re.DOTALL)
                if mobj:
                    resp_dict['command_response'] = mobj.group(1)
        return resp_dict


Instruction:
Can you tell what is the following Python 3 function doing
def _getGreeting(self):
        greeting = self._conn.read_until("\r\n", connTimeout)
        mobj = re.match('Asterisk Call Manager\/([\d\.]+)\s*$', greeting)
        if mobj:
            self._ami_version = util.SoftwareVersion(mobj.group(1))
        else:
            raise Exception("Asterisk Manager Interface version cannot be determined.")

Response:
Read and parse Asterisk Manager Interface Greeting to determine and
        set Manager Interface version.


Instruction:
Create a Python 3 function for
querying Asterisk Manager Interface for Asterisk Version to configure with multiple versions of Asterisk Version.

Response:
def _initAsteriskVersion(self):
        """Query Asterisk Manager Interface for Asterisk Version to configure
        system for compatibility with multiple versions
        .
        CLI Command - core show version

        """
        if self._ami_version > util.SoftwareVersion('1.0'):
            cmd = "core show version"
        else:
            cmd = "show version"
        cmdresp = self.executeCommand(cmd)
        mobj = re.match('Asterisk\s*(SVN-branch-|\s)(\d+(\.\d+)*)', cmdresp)
        if mobj:
            self._asterisk_version = util.SoftwareVersion(mobj.group(2))
        else:
            raise Exception('Asterisk version cannot be determined.')


Instruction:
Here you have a function in Python 3, explain what it does
def _login(self):
        self._sendAction("login", (
            ("Username", self._amiuser),
            ("Secret", self._amipass),
            ("Events", "off"),
        ))
        resp = self._getResponse()
        if resp.get("Response") == "Success":
            return True
        else:
            raise Exception("Authentication to Asterisk Manager Interface Failed.")

Response: Login to Asterisk Manager Interface.


Instruction:
How would you implement a function in Python 3 that
sends Command to Asterisk Manager Interface to execute CLI Command.

Response:
def executeCommand(self, command):
        """Send Action to Asterisk Manager Interface to execute CLI Command.

        @param command: CLI command to execute.
        @return:        Command response string.

        """
        self._sendAction("Command", (
            ("Command", command),
        ))
        resp = self._getResponse()
        result = resp.get("Response")
        if result == "Follows":
            return resp.get("command_response")
        elif result == "Error":
            raise Exception("Execution of Asterisk Manager Interface Command "
                            "(%s) failed with error message: %s" %
                            (command, str(resp.get("Message"))))
        else:
            raise Exception("Execution of Asterisk Manager Interface Command "
                            "failed: %s" % command)


Instruction:
Can you generate the documentation for the following Python 3 function
def _initModuleList(self):
        if self.checkVersion('1.4'):
            cmd = "module show"
        else:
            cmd = "show modules"
        cmdresp = self.executeCommand(cmd)
        self._modules = set()
        for line in cmdresp.splitlines()[1:-1]:
            mobj = re.match('\s*(\S+)\s', line)
            if mobj:
                self._modules.add(mobj.group(1).lower())

Response:
Query Asterisk Manager Interface to initialize internal list of
        loaded modules.


Instruction:
Write a Python 3 function for
querying Asterisk Manager Interface to initialize internal list of available applications.

Response:
def _initApplicationList(self):
        """Query Asterisk Manager Interface to initialize internal list of
        available applications.

        CLI Command - core show applications

        """
        if self.checkVersion('1.4'):
            cmd = "core show applications"
        else:
            cmd = "show applications"
        cmdresp = self.executeCommand(cmd)
        self._applications = set()
        for line in cmdresp.splitlines()[1:-1]:
            mobj = re.match('\s*(\S+):', line)
            if mobj:
                self._applications.add(mobj.group(1).lower())


Instruction:
Explain what the following Python 3 code does
def _initChannelTypesList(self):
        if self.checkVersion('1.4'):
            cmd = "core show channeltypes"
        else:
            cmd = "show channeltypes"
        cmdresp = self.executeCommand(cmd)
        self._chantypes = set()
        for line in cmdresp.splitlines()[2:]:
            mobj = re.match('\s*(\S+)\s+.*\s+(yes|no)\s+', line)
            if mobj:
                self._chantypes.add(mobj.group(1).lower())

Response:
Query Asterisk Manager Interface to initialize internal list of
        supported channel types.


Instruction:
Implement a Python 3 function for
returning True if mod is among the loaded modules.

Response:
def hasModule(self, mod):
        """Returns True if mod is among the loaded modules.

        @param mod: Module name.
        @return:    Boolean

        """
        if self._modules is None:
            self._initModuleList()
        return mod in self._modules


Instruction:
Create a Python 3 function to
return True if app is among the loaded modules.

Response:
def hasApplication(self, app):
        """Returns True if app is among the loaded modules.

        @param app: Module name.
        @return:    Boolean

        """
        if self._applications is None:
            self._initApplicationList()
        return app in self._applications


Instruction:
Write a Python 3 function for
returning True if chan is among the supported channel types.

Response:
def hasChannelType(self, chan):
        """Returns True if chan is among the supported channel types.

        @param app: Module name.
        @return:    Boolean

        """
        if self._chantypes is None:
            self._initChannelTypesList()
        return chan in self._chantypes


Instruction:
Can you create a Python 3 function that
queries Asterisk Manager Interface for defined codecs.

Response:
def getCodecList(self):
        """Query Asterisk Manager Interface for defined codecs.

        CLI Command - core show codecs

        @return: Dictionary - Short Name -> (Type, Long Name)

        """
        if self.checkVersion('1.4'):
            cmd = "core show codecs"
        else:
            cmd = "show codecs"
        cmdresp = self.executeCommand(cmd)
        info_dict = {}
        for line in cmdresp.splitlines():
            mobj = re.match('\s*(\d+)\s+\((.+)\)\s+\((.+)\)\s+(\w+)\s+(\w+)\s+\((.+)\)$',
                            line)
            if mobj:
                info_dict[mobj.group(5)] = (mobj.group(4), mobj.group(6))
        return info_dict


Instruction:
Write a Python 3 script to
query Asterisk Manager Interface for Channel Stats.

Response:
def getChannelStats(self, chantypes=('dahdi', 'zap', 'sip', 'iax2', 'local')):
        """Query Asterisk Manager Interface for Channel Stats.

        CLI Command - core show channels

        @return: Dictionary of statistics counters for channels.
            Number of active channels for each channel type.

        """
        if self.checkVersion('1.4'):
            cmd = "core show channels"
        else:
            cmd = "show channels"
        cmdresp = self.executeCommand(cmd)
        info_dict ={}
        for chanstr in chantypes:
            chan = chanstr.lower()
            if chan in ('zap', 'dahdi'):
                info_dict['dahdi'] = 0
                info_dict['mix'] = 0
            else:
                info_dict[chan] = 0
        for k in ('active_calls', 'active_channels', 'calls_processed'):
            info_dict[k] = 0
        regexstr = ('(%s)\/(\w+)' % '|'.join(chantypes))
        for line in cmdresp.splitlines():
            mobj = re.match(regexstr,
                            line, re.IGNORECASE)
            if mobj:
                chan_type = mobj.group(1).lower()
                chan_id = mobj.group(2).lower()
                if chan_type == 'dahdi' or chan_type == 'zap':
                    if chan_id == 'pseudo':
                        info_dict['mix'] += 1
                    else:
                        info_dict['dahdi'] += 1
                else:
                    info_dict[chan_type] += 1
                continue

            mobj = re.match('(\d+)\s+(active channel|active call|calls processed)',
                            line, re.IGNORECASE)
            if mobj:
                if mobj.group(2) == 'active channel':
                    info_dict['active_channels'] = int(mobj.group(1))
                elif mobj.group(2) == 'active call':
                    info_dict['active_calls'] = int(mobj.group(1))
                elif mobj.group(2) == 'calls processed':
                    info_dict['calls_processed'] = int(mobj.group(1))
                continue

        return info_dict


Instruction:
Explain what the following Python 3 code does
def getPeerStats(self, chantype):
        chan = chantype.lower()
        if not self.hasChannelType(chan):
            return None
        if chan == 'iax2':
            cmd = "iax2 show peers"
        elif chan == 'sip':
            cmd = "sip show peers"
        else:
            raise AttributeError("Invalid channel type in query for Peer Stats.")
        cmdresp = self.executeCommand(cmd)

        info_dict = dict(
            online = 0, unreachable = 0, lagged = 0,
            unknown = 0, unmonitored = 0)
        for line in cmdresp.splitlines():
            if re.search('ok\s+\(\d+\s+ms\)\s*$', line, re.IGNORECASE):
                info_dict['online'] += 1
            else:
                mobj = re.search('(unreachable|lagged|unknown|unmonitored)\s*$',
                                 line, re.IGNORECASE)
                if mobj:
                    info_dict[mobj.group(1).lower()] += 1

        return info_dict

Response: Query Asterisk Manager Interface for SIP or IAX2 Peer Stats.


Instruction:
Can you implement a function in Python 3 that
queries Asterisk Manager Interface for Channel Stats.

Response:
def getVoIPchanStats(self, chantype,
                         codec_list=('ulaw', 'alaw', 'gsm', 'g729')):
        """Query Asterisk Manager Interface for SIP / IAX2 Channel / Codec Stats.

        CLI Commands - sip show channels
                       iax2 show channnels

        @param chantype:   Must be 'sip' or 'iax2'.
        @param codec_list: List of codec names to parse.
                           (Codecs not in the list are summed up to the other
                           count.)
        @return:           Dictionary of statistics counters for Active VoIP
                           Channels.

        """
        chan = chantype.lower()
        if not self.hasChannelType(chan):
            return None
        if chan == 'iax2':
            cmd = "iax2 show channels"
        elif chan == 'sip':
            cmd = "sip show channels"
        else:
            raise AttributeError("Invalid channel type in query for Channel Stats.")
        cmdresp = self.executeCommand(cmd)
        lines = cmdresp.splitlines()
        headers = re.split('\s\s+', lines[0])
        try:
            idx = headers.index('Format')
        except ValueError:
            try:
                idx = headers.index('Form')
            except:
                raise Exception("Error in parsing header line of %s channel stats."
                                % chan)
        codec_list = tuple(codec_list) + ('other', 'none')
        info_dict = dict([(k,0) for k in codec_list])
        for line in lines[1:-1]:
            codec = None
            cols = re.split('\s\s+', line)
            colcodec = cols[idx]
            mobj = re.match('0x\w+\s\((\w+)\)$', colcodec)
            if mobj:
                codec = mobj.group(1).lower()
            elif re.match('\w+$', colcodec):
                codec = colcodec.lower()
            if codec:
                if codec in info_dict:
                    info_dict[codec] += 1
                elif codec == 'nothing' or codec[0:4] == 'unkn':
                    info_dict['none'] += 1
                else:
                    info_dict['other'] += 1
        return info_dict


Instruction:
Make a summary of the following Python 3 code
def getConferenceStats(self):
        if not self.hasConference():
            return None
        if self.checkVersion('1.6'):
            cmd = "meetme list"
        else:
            cmd = "meetme"
        cmdresp = self.executeCommand(cmd)

        info_dict = dict(active_conferences = 0, conference_users = 0)
        for line in cmdresp.splitlines():
            mobj = re.match('\w+\s+0(\d+)\s', line)
            if mobj:
                info_dict['active_conferences'] += 1
                info_dict['conference_users'] += int(mobj.group(1))

        return info_dict

Response: Query Asterisk Manager Interface for Conference Room Stats. meetme list


Instruction:
Given the following Python 3 function, write the documentation
def getVoicemailStats(self):
        if not self.hasVoicemail():
            return None
        if self.checkVersion('1.4'):
            cmd = "voicemail show users"
        else:
            cmd = "show voicemail users"
        cmdresp = self.executeCommand(cmd)

        info_dict = dict(accounts = 0, avg_messages = 0, max_messages = 0,
                         total_messages = 0)
        for line in cmdresp.splitlines():
            mobj = re.match('\w+\s+\w+\s+.*\s+(\d+)\s*$', line)
            if mobj:
                msgs = int(mobj.group(1))
                info_dict['accounts'] += 1
                info_dict['total_messages'] += msgs
                if msgs > info_dict['max_messages']:
                    info_dict['max_messages'] = msgs
        if info_dict['accounts'] > 0:
            info_dict['avg_messages'] = (float(info_dict['total_messages'])
                                         / info_dict['accounts'])

        return info_dict

Response: Query Asterisk Manager Interface for Voicemail Stats.


Instruction:
Can you implement a function in Python 3 that
queries Asterisk Manager Interface for Trunk Stats.

Response:
def getTrunkStats(self, trunkList):
        """Query Asterisk Manager Interface for Trunk Stats.

        CLI Command - core show channels

        @param trunkList: List of tuples of one of the two following types:
                            (Trunk Name, Regular Expression)
                            (Trunk Name, Regular Expression, MIN, MAX)
        @return: Dictionary of trunk utilization statistics.

        """
        re_list = []
        info_dict = {}
        for filt in trunkList:
            info_dict[filt[0]] = 0
            re_list.append(re.compile(filt[1], re.IGNORECASE))

        if self.checkVersion('1.4'):
            cmd = "core show channels"
        else:
            cmd = "show channels"
        cmdresp = self.executeCommand(cmd)

        for line in cmdresp.splitlines():
            for idx in range(len(re_list)):
                recomp = re_list[idx]
                trunkid = trunkList[idx][0]
                mobj = recomp.match(line)
                if mobj:
                    if len(trunkList[idx]) == 2:
                        info_dict[trunkid] += 1
                        continue
                    elif len(trunkList[idx]) == 4:
                        num = mobj.groupdict().get('num')
                        if num is not None:
                            (vmin,vmax) = trunkList[idx][2:4]
                            if int(num) >= int(vmin) and int(num) <= int(vmax):
                                info_dict[trunkid] += 1
                                continue
        return info_dict


Instruction:
Can you generate a brief explanation for the following Python 3 code
def getQueueStats(self):
        if not self.hasQueue():
            return None
        info_dict = {}
        if self.checkVersion('1.4'):
            cmd = "queue show"
        else:
            cmd = "show queues"
        cmdresp = self.executeCommand(cmd)

        queue = None
        ctxt = None
        member_states = ("unknown", "not in use", "in use", "busy", "invalid",
                         "unavailable", "ringing", "ring+inuse", "on hold",
                         "total")
        member_state_dict = dict([(k.lower().replace(' ', '_'),0)
                                  for k in member_states])
        for line in cmdresp.splitlines():
            mobj = re.match(r"([\w\-]+)\s+has\s+(\d+)\s+calls\s+"
                            r"\(max (\d+|unlimited)\)\s+in\s+'(\w+)'\s+strategy\s+"
                            r"\((.+)\),\s+W:(\d+),\s+C:(\d+),\s+A:(\d+),\s+"
                            r"SL:([\d\.]+)%\s+within\s+(\d+)s", line)
            if mobj:
                ctxt = None
                queue = mobj.group(1)
                info_dict[queue] = {}
                info_dict[queue]['queue_len'] = int(mobj.group(2))
                try:
                    info_dict[queue]['queue_maxlen'] = int(mobj.group(3))
                except ValueError:
                    info_dict[queue]['queue_maxlen'] = None
                info_dict[queue]['strategy'] = mobj.group(4)
                for tkn in mobj.group(5).split(','):
                    mobjx = re.match(r"\s*(\d+)s\s+(\w+)\s*", tkn)
                    if mobjx:
                        info_dict[queue]['avg_' + mobjx.group(2)] = int(mobjx.group(1))
                info_dict[queue]['queue_weight'] = int(mobj.group(6))
                info_dict[queue]['calls_completed'] = int(mobj.group(7))
                info_dict[queue]['calls_abandoned'] = int(mobj.group(8))
                info_dict[queue]['sla_pcent'] = float(mobj.group(9))
                info_dict[queue]['sla_cutoff'] = int(mobj.group(10))
                info_dict[queue]['members'] = member_state_dict.copy()
                continue
            mobj = re.match('\s+(Members|Callers):\s*$', line)
            if mobj:
                ctxt = mobj.group(1).lower()
                continue
            if ctxt == 'members':
                mobj = re.match(r"\s+\S.*\s\((.*)\)\s+has\s+taken.*calls", line)
                if mobj:
                    info_dict[queue]['members']['total'] += 1
                    state = mobj.group(1).lower().replace(' ', '_')
                    if info_dict[queue]['members'].has_key(state):
                        info_dict[queue]['members'][state] += 1
                    else:
                        raise AttributeError("Undefined queue member state %s"
                                             % state)
                    continue
        return info_dict

Response: Query Asterisk Manager Interface for Queue Stats.


Instruction:
How would you explain what the following Python 3 function does
def getFaxStatsCounters(self):
        if not self.hasFax():
            return None
        info_dict = {}
        cmdresp = self.executeCommand('fax show stats')
        ctxt = 'general'
        for section in cmdresp.strip().split('\n\n')[1:]:
            i = 0
            for line in section.splitlines():
                mobj = re.match('(\S.*\S)\s*:\s*(\d+)\s*$', line)
                if mobj:
                    if not info_dict.has_key(ctxt):
                        info_dict[ctxt] = {}
                    info_dict[ctxt][mobj.group(1).lower()] = int(mobj.group(2).lower())
                elif i == 0:
                    ctxt = line.strip().lower()
                i += 1
        return info_dict

Response: Query Asterisk Manager Interface for Fax Stats.


Instruction:
Here you have a function in Python 3, explain what it does
def getFaxStatsSessions(self):
        if not self.hasFax():
            return None
        info_dict = {}
        info_dict['total'] = 0
        fax_types = ('g.711', 't.38')
        fax_operations = ('send', 'recv')
        fax_states = ('uninitialized', 'initialized', 'open',
                      'active', 'inactive', 'complete', 'unknown',)
        info_dict['type'] = dict([(k,0) for k in fax_types])
        info_dict['operation'] = dict([(k,0) for k in fax_operations])
        info_dict['state'] = dict([(k,0) for k in fax_states])
        cmdresp = self.executeCommand('fax show sessions')
        sections = cmdresp.strip().split('\n\n')
        if len(sections) >= 3:
            for line in sections[1][1:]:
                cols = re.split('\s\s+', line)
                if len(cols) == 7:
                    info_dict['total'] += 1
                    if cols[3].lower() in fax_types:
                        info_dict['type'][cols[3].lower()] += 1
                    if cols[4] == 'receive':
                        info_dict['operation']['recv'] += 1
                    elif cols[4] == 'send':
                        info_dict['operation']['send'] += 1
                    if cols[5].lower() in fax_states:
                        info_dict['state'][cols[5].lower()] += 1
        return info_dict

Response: Query Asterisk Manager Interface for Fax Stats.


Instruction:
Can you implement a function in Python 3 that
retrieves values for graphs.

Response:
def retrieveVals(self):
        """Retrieve values for graphs."""
        for iface in self._ifaceList:
            if self._reqIfaceList is None or iface in self._reqIfaceList:
                if (self.graphEnabled('wanpipe_traffic')
                    or self.graphEnabled('wanpipe_errors')):
                    stats = self._ifaceStats.get(iface)
                    if stats:
                        graph_name = 'wanpipe_traffic_%s' % iface
                        if self.hasGraph(graph_name):
                            for field in ('rxpackets', 'txpackets'):
                                self.setGraphVal(graph_name, field,
                                                 stats.get(field))
                        graph_name = 'wanpipe_errors_%s' % iface
                        if self.hasGraph(graph_name):
                            for field in ('rxerrs', 'txerrs', 'rxframe', 'txcarrier',
                                'rxdrop', 'txdrop', 'rxfifo', 'txfifo'):
                                self.setGraphVal(graph_name, field,
                                                 stats.get(field))
                if (self.graphEnabled('wanpipe_pri_errors')
                    or self.graphEnabled('wanpipe_rxlevel')):
                    try:
                        stats = self._wanpipeInfo.getPRIstats(iface)
                    except:
                        stats = None
                    if stats:
                        graph_name = 'wanpipe_pri_errors_%s' % iface
                        if self.hasGraph(graph_name):
                            for field in ('linecodeviolation',
                                          'farendblockerrors',
                                          'crc4errors', 'faserrors'):
                                self.setGraphVal(graph_name, field,
                                                 stats.get(field))
                        if self.hasGraph('wanpipe_rxlevel'):
                            self.setGraphVal('wanpipe_pri_rxlevel',
                                             iface, stats.get('rxlevel'))


Instruction:
Explain what the following Python 3 code does
def simulate_list(nwords=16, nrec=10, ncats=4):

    # load wordpool
    wp = pd.read_csv('data/cut_wordpool.csv')

    # get one list
    wp = wp[wp['GROUP']==np.random.choice(list(range(16)), 1)[0]].sample(16)

    wp['COLOR'] = [[int(np.random.rand() * 255) for i in range(3)] for i in range(16)]

Response: A function to simulate a list


Instruction:
Write a Python 3 function for
retrieving values for graphs.

Response:
def retrieveVals(self):
        """Retrieve values for graphs."""
        if self._genStats is None:
            self._genStats = self._dbconn.getStats()
        if self._genVars is None:
            self._genVars = self._dbconn.getParams()
        if self.hasGraph('mysql_connections'):
            self.setGraphVal('mysql_connections', 'conn',
                             self._genStats.get('Connections'))
            self.setGraphVal('mysql_connections', 'abort_conn',
                             self._genStats.get('Aborted_connects'))
            self.setGraphVal('mysql_connections', 'abort_client',
                             self._genStats.get('Aborted_clients'))
        if self.hasGraph('mysql_traffic'):
            self.setGraphVal('mysql_traffic', 'rx',
                             self._genStats.get('Bytes_received'))
            self.setGraphVal('mysql_traffic', 'tx',
                             self._genStats.get('Bytes_sent'))
        if self.graphEnabled('mysql_slowqueries'):
            self.setGraphVal('mysql_slowqueries', 'queries',
                             self._genStats.get('Slow_queries'))
        if self.hasGraph('mysql_rowmodifications'):
            self.setGraphVal('mysql_rowmodifications', 'insert',
                             self._genStats.get('Handler_write'))
            self.setGraphVal('mysql_rowmodifications', 'update',
                             self._genStats.get('Handler_update'))
            self.setGraphVal('mysql_rowmodifications', 'delete',
                             self._genStats.get('Handler_delete'))
        if self.hasGraph('mysql_rowreads'):
            for field in self.getGraphFieldList('mysql_rowreads'):
                self.setGraphVal('mysql_rowreads', field,
                                 self._genStats.get('Handler_read_%s' % field))
        if self.hasGraph('mysql_tablelocks'):
            self.setGraphVal('mysql_tablelocks', 'waited',
                             self._genStats.get('Table_locks_waited'))
            self.setGraphVal('mysql_tablelocks', 'immediate',
                             self._genStats.get('Table_locks_immediate'))
        if self.hasGraph('mysql_threads'):
            self.setGraphVal('mysql_threads', 'running',
                             self._genStats.get('Threads_running'))
            self.setGraphVal('mysql_threads', 'idle',
                             self._genStats.get('Threads_connected')
                             - self._genStats.get('Threads_running'))
            self.setGraphVal('mysql_threads', 'cached',
                             self._genStats.get('Threads_cached'))
            self.setGraphVal('mysql_threads', 'total',
                             self._genStats.get('Threads_connected')
                             + self._genStats.get('Threads_cached'))
        if self.hasGraph('mysql_commits_rollbacks'):
            self.setGraphVal('mysql_commits_rollbacks', 'commit',
                             self._genStats.get('Handler_commit'))
            self.setGraphVal('mysql_commits_rollbacks', 'rollback',
                             self._genStats.get('Handler_rollback'))
        if self.hasGraph('mysql_qcache_memory'):
            try:
                total = self._genVars['query_cache_size']
                free = self._genStats['Qcache_free_memory']
                used = total - free
            except KeyError:
                free = None
                used = None
            self.setGraphVal('mysql_qcache_memory', 'used', used)
            self.setGraphVal('mysql_qcache_memory', 'free', free)
        if self.hasGraph('mysql_qcache_hits'):
            try:
                hits = self._genStats['Qcache_hits']
                misses = self._genStats['Com_select'] - hits
            except KeyError:
                hits = None
                misses = None
            self.setGraphVal('mysql_qcache_hits', 'hits', hits)
            self.setGraphVal('mysql_qcache_hits', 'misses', misses)

        if self.hasGraph('mysql_qcache_prunes'):
            self.setGraphVal('mysql_qcache_prunes', 'insert',
                             self._genStats.get('Qcache_inserts'))
            self.setGraphVal('mysql_qcache_prunes', 'prune',
                             self._genStats.get('Qcache_lowmem_prunes'))
        if self.hasGraph('mysql_proc_status'):
            self._procStatus = self._dbconn.getProcessStatus()
            if self._procStatus:
                stats = {}
                for field in self.getGraphFieldList('mysql_proc_status'):
                    stats[field] = 0
                for (k, v) in self._procStatus.items():
                    if stats.has_key(k):
                        stats[k] = v
                    else:
                        stats['unknown'] += v
                for (k,v) in stats.items():
                    self.setGraphVal('mysql_proc_status', k, v)
        if self.hasGraph('mysql_proc_db'):
            self._procDB = self._dbconn.getProcessDatabase()
            for db in self._dbList:
                self.setGraphVal('mysql_proc_db', db, self._procDB.get(db, 0))

        if self.engineIncluded('myisam'):

            if self.hasGraph('mysql_myisam_key_buffer_util'):
                try:
                    bsize = self._genVars['key_cache_block_size']
                    total = self._genVars['key_buffer_size']
                    free = self._genStats['Key_blocks_unused'] * bsize
                    dirty = self._genStats['Key_blocks_not_flushed'] * bsize
                    clean = total - free - dirty
                except KeyError:
                    total = None
                    free = None
                    dirty = None
                    clean = None
                for (field,val) in (('dirty', dirty),
                                    ('clean', clean),
                                    ('free', free),
                                    ('total', total)):
                    self.setGraphVal('mysql_myisam_key_buffer_util',
                                     field, val)
            if self.hasGraph('mysql_myisam_key_read_reqs'):
                try:
                    misses = self._genStats['Key_reads']
                    hits = (self._genStats['Key_read_requests']
                            - misses)
                except KeyError:
                    misses = None
                    hits = None
                self.setGraphVal('mysql_myisam_key_read_reqs', 'disk', misses)
                self.setGraphVal('mysql_myisam_key_read_reqs', 'buffer', hits)

        if self.engineIncluded('innodb'):

            if self.hasGraph('mysql_innodb_buffer_pool_util'):
                self._genStats['Innodb_buffer_pool_pages_clean'] = (
                    self._genStats.get('Innodb_buffer_pool_pages_data')
                    - self._genStats.get('Innodb_buffer_pool_pages_dirty'))
                page_size = int(self._genStats.get('Innodb_page_size'))
                for field in ('dirty', 'clean', 'misc', 'free', 'total'):
                    self.setGraphVal('mysql_innodb_buffer_pool_util',
                                     field,
                                     self._genStats.get('Innodb_buffer_pool_pages_%s'
                                                        % field)
                                     * page_size)
            if self.hasGraph('mysql_innodb_buffer_pool_activity'):
                for field in ('created', 'read', 'written'):
                    self.setGraphVal('mysql_innodb_buffer_pool_activity', field,
                                     self._genStats.get('Innodb_pages_%s' % field))
            if self.hasGraph('mysql_innodb_buffer_pool_read_reqs'):
                try:
                    misses = self._genStats['Innodb_buffer_pool_reads']
                    hits = (self._genStats['Innodb_buffer_pool_read_requests']
                            - misses)
                except KeyError:
                    misses = None
                    hits = None
                self.setGraphVal('mysql_innodb_buffer_pool_read_reqs', 'disk',
                                 misses)
                self.setGraphVal('mysql_innodb_buffer_pool_read_reqs', 'buffer',
                                 hits)
            if self.hasGraph('mysql_innodb_row_ops'):
                for field in ('inserted', 'updated', 'deleted', 'read'):
                    self.setGraphVal('mysql_innodb_row_ops', field,
                                     self._genStats.get('Innodb_rows_%s' % field))


Instruction:
Make a summary of the following Python 3 code
def engineIncluded(self, name):
        if self._engines is None:
            self._engines = self._dbconn.getStorageEngines()
        return self.envCheckFilter('engine', name) and name in self._engines

Response:
Utility method to check if a storage engine is included in graphs.
egerName is a string that represents the name of the storage engine.


Instruction:
Implement a Python 3 function for
getting disk space usage.

Response:
def getSpaceUse(self):
        """Get disk space usage.

        @return: Dictionary of filesystem space utilization stats for filesystems.

        """
        stats = {}
        try:
            out = subprocess.Popen([dfCmd, "-Pk"],
                                   stdout=subprocess.PIPE).communicate()[0]
        except:
            raise Exception('Execution of command %s failed.' % dfCmd)
        lines = out.splitlines()
        if len(lines) > 1:
            for line in lines[1:]:
                fsstats = {}
                cols = line.split()
                fsstats['device'] = cols[0]
                fsstats['type'] = self._fstypeDict[cols[5]]
                fsstats['total'] = 1024 * int(cols[1])
                fsstats['inuse'] = 1024 * int(cols[2])
                fsstats['avail'] = 1024 * int(cols[3])
                fsstats['inuse_pcent'] = int(cols[4][:-1])
                stats[cols[5]] = fsstats
        return stats


Instruction:
Implement a Python 3 function for
retrieving values for graphs.

Response:
def retrieveVals(self):
        """Retrieve values for graphs."""
        stats = self._dbconn.getDatabaseStats()
        databases = stats.get('databases')
        totals = stats.get('totals')
        if self.hasGraph('pg_connections'):
            limit = self._dbconn.getParam('max_connections')
            self.setGraphVal('pg_connections', 'max_conn', limit)
            for (db, dbstats) in databases.iteritems():
                if self.dbIncluded(db):
                    self.setGraphVal('pg_connections', db,
                                     dbstats['numbackends'])
            self.setGraphVal('pg_connections', 'total', totals['numbackends'])
        if self.hasGraph('pg_diskspace'):
            for (db, dbstats) in databases.iteritems():
                if self.dbIncluded(db):
                    self.setGraphVal('pg_diskspace', db, dbstats['disk_size'])
            self.setGraphVal('pg_diskspace', 'total', totals['disk_size'])
        if self.hasGraph('pg_blockreads'):
            self.setGraphVal('pg_blockreads', 'blk_hit', totals['blks_hit'])
            self.setGraphVal('pg_blockreads', 'blk_read', totals['blks_read'])
        if self.hasGraph('pg_xact'):
            self.setGraphVal('pg_xact', 'commits', totals['xact_commit'])
            self.setGraphVal('pg_xact', 'rollbacks', totals['xact_rollback'])
        if self.hasGraph('pg_tup_read'):
            self.setGraphVal('pg_tup_read', 'fetch', totals['tup_fetched'])
            self.setGraphVal('pg_tup_read', 'return', totals['tup_returned'])
        if self.hasGraph('pg_tup_write'):
            self.setGraphVal('pg_tup_write', 'delete', totals['tup_deleted'])
            self.setGraphVal('pg_tup_write', 'update', totals['tup_updated'])
            self.setGraphVal('pg_tup_write', 'insert', totals['tup_inserted'])
        lock_stats = None
        for lock_state in ('all', 'wait',):
            graph_name = "pg_lock_%s" % lock_state
            if self.hasGraph(graph_name):
                if lock_stats is None:
                    lock_stats = self._dbconn.getLockStatsMode()
                mode_iter = iter(PgInfo.lockModes)
                for mode in ('AccessExcl', 'Excl', 'ShrRwExcl', 'Shr',
                             'ShrUpdExcl', 'RwExcl', 'RwShr', 'AccessShr',):
                    self.setGraphVal(graph_name, mode,
                                     lock_stats[lock_state].get(mode_iter.next()))

        stats = None
        if self.hasGraph('pg_checkpoints'):
            if stats is None:
                stats = self._dbconn.getBgWriterStats()
            self.setGraphVal('pg_checkpoints', 'req',
                             stats.get('checkpoints_req'))
            self.setGraphVal('pg_checkpoints', 'timed',
                             stats.get('checkpoints_timed'))
        if self.hasGraph('pg_bgwriter'):
            if stats is None:
                stats = self._dbconn.getBgWriterStats()
            self.setGraphVal('pg_bgwriter', 'backend',
                             stats.get('buffers_backend'))
            self.setGraphVal('pg_bgwriter', 'clean',
                             stats.get('buffers_clean'))
            self.setGraphVal('pg_bgwriter', 'chkpoint',
                             stats.get('buffers_checkpoint'))

        if self._detailGraphs:
            for (db, dbstats) in databases.iteritems():
                if self.dbIncluded(db):
                    if self.hasGraph('pg_blockread_detail'):
                        self.setGraphVal('pg_blockread_detail', db,
                            dbstats['blks_hit'] + dbstats['blks_read'])
                    for (graph_name, attr_name) in (
                            ('pg_xact_commit_detail', 'xact_commit'),
                            ('pg_xact_rollback_detail', 'xact_rollback'),
                            ('pg_tup_return_detail', 'tup_returned'),
                            ('pg_tup_fetch_detail', 'tup_fetched'),
                            ('pg_tup_delete_detail', 'tup_deleted'),
                            ('pg_tup_update_detail', 'tup_updated'),
                            ('pg_tup_insert_detail', 'tup_inserted'),
                        ):
                        if self.hasGraph(graph_name):
                            self.setGraphVal(graph_name, db, dbstats[attr_name])
                    lock_stats_db = None
                    for lock_state in ('all', 'wait',):
                        graph_name = "pg_lock_%s_detail" % lock_state
                        if self.hasGraph(graph_name):
                            if lock_stats_db is None:
                                lock_stats_db = self._dbconn.getLockStatsDB()
                            self.setGraphVal(graph_name, db,
                                             lock_stats_db[lock_state].get(db, 0))

        if self._replGraphs:
            repl_stats = self._dbconn.getSlaveConflictStats()
            if self.hasGraph('pg_repl_conflicts'):
                for field in self.getGraphFieldList('pg_repl_conflicts'):
                    self.setGraphVal('pg_repl_conflicts', field,
                                     repl_stats['totals'].get("confl_%s" % field))
            if self._detailGraphs and self.hasGraph('pg_repl_conflicts_detail'):
                for (db, dbstats) in repl_stats['databases'].iteritems():
                    if self.dbIncluded(db):
                        self.setGraphVal('pg_repl_conflicts_detail', db,
                                         sum(dbstats.values()))


Instruction:
Can you tell what is the following Python 3 function doing
def connect(self, host, port):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((host, port))
        self._reader = sock.makefile(mode='rb')
        self._writer = sock.makefile(mode='wb')

Response: Connects via an RS - 485 to Ethernet adapter.


Instruction:
Implement a Python 3 function for
processing data ; returns when the reader signals EOF. data_changed_callback is notified when any data changes.

Response:
def process(self, data_changed_callback):
        """Process data; returns when the reader signals EOF.
        Callback is notified when any data changes."""
        # pylint: disable=too-many-locals,too-many-branches,too-many-statements
        while True:
            byte = self._reader.read(1)

            while True:
                # Search for FRAME_DLE + FRAME_STX
                if not byte:
                    return
                if byte[0] == self.FRAME_DLE:
                    next_byte = self._reader.read(1)
                    if not next_byte:
                        return
                    if next_byte[0] == self.FRAME_STX:
                        break
                    else:
                        continue
                byte = self._reader.read(1)

            frame = bytearray()
            byte = self._reader.read(1)

            while True:
                if not byte:
                    return
                if byte[0] == self.FRAME_DLE:
                    # Should be FRAME_ETX or 0 according to
                    # the AQ-CO-SERIAL manual
                    next_byte = self._reader.read(1)
                    if not next_byte:
                        return
                    if next_byte[0] == self.FRAME_ETX:
                        break
                    elif next_byte[0] != 0:
                        # Error?
                        pass

                frame.append(byte[0])
                byte = self._reader.read(1)

            # Verify CRC
            frame_crc = int.from_bytes(frame[-2:], byteorder='big')
            frame = frame[:-2]

            calculated_crc = self.FRAME_DLE + self.FRAME_STX
            for byte in frame:
                calculated_crc += byte

            if frame_crc != calculated_crc:
                _LOGGER.warning('Bad CRC')
                continue

            frame_type = frame[0:2]
            frame = frame[2:]

            if frame_type == self.FRAME_TYPE_KEEP_ALIVE:
                # Keep alive
                # If a frame has been queued for transmit, send it.
                if not self._send_queue.empty():
                    data = self._send_queue.get(block=False)
                    self._writer.write(data['frame'])
                    self._writer.flush()
                    _LOGGER.info('Sent: %s', binascii.hexlify(data['frame']))

                    try:
                        if data['desired_states'] is not None:
                            # Set a timer to verify the state changes
                            # Wait 2 seconds as it can take a while for
                            # the state to change.
                            Timer(2.0, self._check_state, [data]).start()
                    except KeyError:
                        pass

                continue
            elif frame_type == self.FRAME_TYPE_KEY_EVENT:
                _LOGGER.info('Key: %s', binascii.hexlify(frame))
            elif frame_type == self.FRAME_TYPE_LEDS:
                _LOGGER.debug('LEDs: %s', binascii.hexlify(frame))
                # First 4 bytes are the LEDs that are on;
                # second 4 bytes_ are the LEDs that are flashing
                states = int.from_bytes(frame[0:4], byteorder='little')
                flashing_states = int.from_bytes(frame[4:8],
                                                 byteorder='little')
                states |= flashing_states
                if (states != self._states
                        or flashing_states != self._flashing_states):
                    self._states = states
                    self._flashing_states = flashing_states
                    data_changed_callback(self)
            elif frame_type == self.FRAME_TYPE_PUMP_SPEED_REQUEST:
                value = int.from_bytes(frame[0:2], byteorder='big')
                _LOGGER.debug('Pump speed request: %d%%', value)
                if self._pump_speed != value:
                    self._pump_speed = value
                    data_changed_callback(self)
            elif frame_type == self.FRAME_TYPE_PUMP_STATUS:
                # Pump status messages sent out by Hayward VSP pumps
                self._multi_speed_pump = True
                speed = frame[2]
                # Power is in BCD
                power = ((((frame[3] & 0xf0) >> 4) * 1000)
                         + (((frame[3] & 0x0f)) * 100)
                         + (((frame[4] & 0xf0) >> 4) * 10)
                         + (((frame[4] & 0x0f))))
                _LOGGER.debug('Pump speed: %d%%, power: %d watts',
                              speed, power)
                if self._pump_power != power:
                    self._pump_power = power
                    data_changed_callback(self)
            elif frame_type == self.FRAME_TYPE_DISPLAY_UPDATE:
                parts = frame.decode('latin-1').split()
                _LOGGER.debug('Display update: %s', parts)

                try:
                    if parts[0] == 'Pool' and parts[1] == 'Temp':
                        # Pool Temp <temp>°[C|F]
                        value = int(parts[2][:-2])
                        if self._pool_temp != value:
                            self._pool_temp = value
                            self._is_metric = parts[2][-1:] == 'C'
                            data_changed_callback(self)
                    elif parts[0] == 'Spa' and parts[1] == 'Temp':
                        # Spa Temp <temp>°[C|F]
                        value = int(parts[2][:-2])
                        if self._spa_temp != value:
                            self._spa_temp = value
                            self._is_metric = parts[2][-1:] == 'C'
                            data_changed_callback(self)
                    elif parts[0] == 'Air' and parts[1] == 'Temp':
                        # Air Temp <temp>°[C|F]
                        value = int(parts[2][:-2])
                        if self._air_temp != value:
                            self._air_temp = value
                            self._is_metric = parts[2][-1:] == 'C'
                            data_changed_callback(self)
                    elif parts[0] == 'Pool' and parts[1] == 'Chlorinator':
                        # Pool Chlorinator <value>%
                        value = int(parts[2][:-1])
                        if self._pool_chlorinator != value:
                            self._pool_chlorinator = value
                            data_changed_callback(self)
                    elif parts[0] == 'Spa' and parts[1] == 'Chlorinator':
                        # Spa Chlorinator <value>%
                        value = int(parts[2][:-1])
                        if self._spa_chlorinator != value:
                            self._spa_chlorinator = value
                            data_changed_callback(self)
                    elif parts[0] == 'Salt' and parts[1] == 'Level':
                        # Salt Level <value> [g/L|PPM|
                        value = float(parts[2])
                        if self._salt_level != value:
                            self._salt_level = value
                            self._is_metric = parts[3] == 'g/L'
                            data_changed_callback(self)
                    elif parts[0] == 'Check' and parts[1] == 'System':
                        # Check System <msg>
                        value = ' '.join(parts[2:])
                        if self._check_system_msg != value:
                            self._check_system_msg = value
                            data_changed_callback(self)
                except ValueError:
                    pass
            else:
                _LOGGER.info('Unknown frame: %s %s',
                             binascii.hexlify(frame_type),
                             binascii.hexlify(frame))


Instruction:
Create a Python 3 function for
sending a key to the send queue.

Response:
def send_key(self, key):
        """Sends a key."""
        _LOGGER.info('Queueing key %s', key)
        frame = self._get_key_event_frame(key)

        # Queue it to send immediately following the reception
        # of a keep-alive packet in an attempt to avoid bus collisions.
        self._send_queue.put({'frame': frame})


Instruction:
Make a summary of the following Python 3 code
def states(self):
        state_list = []
        for state in States:
            if state.value & self._states != 0:
                state_list.append(state)

        if (self._flashing_states & States.FILTER) != 0:
            state_list.append(States.FILTER_LOW_SPEED)

        return state_list

Response: Returns a list containing the enabled states.


Instruction:
Given the following Python 3 function, write the documentation
def get_state(self, state):
        # Check to see if we have a change request pending; if we do
        # return the value we expect it to change to.
        for data in list(self._send_queue.queue):
            desired_states = data['desired_states']
            for desired_state in desired_states:
                if desired_state['state'] == state:
                    return desired_state['enabled']
        if state == States.FILTER_LOW_SPEED:
            return (States.FILTER.value & self._flashing_states) != 0
        return (state.value & self._states) != 0

Response: Returns True if the specified state is enabled.


Instruction:
Given the following Python 3 function, write the documentation
def set_state(self, state, enable):

        is_enabled = self.get_state(state)
        if is_enabled == enable:
            return True

        key = None
        desired_states = [{'state': state, 'enabled': not is_enabled}]

        if state == States.FILTER_LOW_SPEED:
            if not self._multi_speed_pump:
                return False
            # Send the FILTER key once.
            # If the pump is in high speed, it wil switch to low speed.
            # If the pump is off the retry mechanism will send an additional
            # FILTER key to switch into low speed.
            # If the pump is in low speed then we pretend the pump is off;
            # the retry mechanism will send an additional FILTER key
            # to switch into high speed.
            key = Keys.FILTER
            desired_states.append({'state': States.FILTER, 'enabled': True})
        else:
            # See if this state has a corresponding Key
            try:
                key = Keys[state.name]
            except KeyError:
                # TODO: send the appropriate combination of keys
                # to enable the state
                return False

        frame = self._get_key_event_frame(key)

        # Queue it to send immediately following the reception
        # of a keep-alive packet in an attempt to avoid bus collisions.
        self._send_queue.put({'frame': frame, 'desired_states': desired_states,
                              'retries': 10})

        return True

Response: Sets the state of the Keyton.


Instruction:
Write a Python 3 script to
decorate a function by tracing the begining and the end of the function execution.

Response:
def trace(function, *args, **k) :
	"""Decorates a function by tracing the begining and
	end of the function execution, if doTrace global is True"""

	if doTrace : print ("> "+function.__name__, args, k)
	result = function(*args, **k)
	if doTrace : print ("< "+function.__name__, args, k, "->", result)
	return result


Instruction:
Implement a Python 3 function for
getting a geocode of a location.

Response:
def geocode(self, location) :
		url = QtCore.QUrl("http://maps.googleapis.com/maps/api/geocode/xml")
		url.addQueryItem("address", location)
		url.addQueryItem("sensor", "false")
		"""
		url = QtCore.QUrl("http://maps.google.com/maps/geo/")
		url.addQueryItem("q", location)
		url.addQueryItem("output", "csv")
		url.addQueryItem("sensor", "false")
		"""
		request = QtNetwork.QNetworkRequest(url)
		reply = self.get(request)
		while reply.isRunning() :
			QtGui.QApplication.processEvents()

		reply.deleteLater()
		self.deleteLater()
		return self._parseResult(reply)


Instruction:
Write a Python 3 script for
forcing a correlation matrix on a set of statistically distributed objects. This function works on objects in-place. Parameters ---------- params : array An array of of uv objects. corrmat : 2d-array The correlation matrix to be imposed

Response:
def correlate(params, corrmat):
    """
    Force a correlation matrix on a set of statistically distributed objects.
    This function works on objects in-place.

    Parameters
    ----------
    params : array
        An array of of uv objects.
    corrmat : 2d-array
        The correlation matrix to be imposed

    """
    # Make sure all inputs are compatible
    assert all(
        [isinstance(param, UncertainFunction) for param in params]
    ), 'All inputs to "correlate" must be of type "UncertainFunction"'

    # Put each ufunc's samples in a column-wise matrix
    data = np.vstack([param._mcpts for param in params]).T

    # Apply the correlation matrix to the sampled data
    new_data = induce_correlations(data, corrmat)

    # Re-set the samples to the respective variables
    for i in range(len(params)):
        params[i]._mcpts = new_data[:, i]