Instruction:
Can you generate the documentation for the following Python 3 function
def decode_speech(path, keypath=None, save=False, speech_context=None,
                  sample_rate=44100, max_alternatives=1, language_code='en-US',
                  enable_word_time_offsets=True, return_raw=False):
    """
    Decode speech for a file or folder and return results

    This function wraps the Google Speech API and ffmpeg to decode speech for
    free recall experiments.  Note: in order for this to work, you must have a
    Google Speech account, a google speech credentials file referenced in your
    _bash_profile, and ffmpeg installed on your computer.  See our readthedocs
    for more information on how to set this up:
    http://cdl-quail.readthedocs.io/en/latest/.

    Parameters
    ----------
    path : str
        Path to a wav file, or a folder of wav files.

    keypath : str
        Google Cloud Speech API key filepath. This is a JSON file containing
        credentials that was generated when creating a service account key.
        If None, assumes you have a local key that is set with an environmental
        variable. See the speech decoding tutorial for details.

    save : boolean
        False by default, but if set to true, will save a pickle with the results
        object from google speech, and a text file with the decoded words.

    speech_context : list of str
        This allows you to give some context to the speech decoding algorithm.
        For example, this could be the words studied on a given list, or all
        words in an experiment.

    sample_rate : float
        The sample rate of your audio files (default is 44100).

    max_alternatives : int
        You can specify the speech decoding to return multiple guesses to the
        decoding.  This will be saved in the results object (default is 1).

    language_code : str
        Decoding language code.  Default is en-US. See  here for more details:
        https://cloud.google.com/speech/docs/languages

    enable_word_time_offsets : bool
        Returns timing information s(onsets/offsets) for each word (default is
        True).

    return_raw : boolean
        Intead of returning the parsed results objects (i.e. the words), you can
        return the raw reponse object.  This has more details about the decoding,
        such as confidence.

    Returns
    ----------
    words : list of str, or list of lists of str
        The results of the speech decoding. This will be a list if only one file
        is input, or a list of lists if more than one file is decoded.

    raw : google speech object, or list of objects
        You can optionally return the google speech object instead of the parsed
        results by using the return_raw flag.

    """

    # SUBFUNCTIONS
    def decode_file(file_path, client, speech_context, sample_rate,
                    max_alternatives, enable_word_time_offsets):

        def recognize(chunk, file_path):
            """
            Subfunction that loops over audio segments to recognize speech
            """
            # export as flac
            chunk.export(file_path + ".flac", format = "flac", bitrate="44.1k")

            # open flac file
            with open(file_path + ".flac", 'rb') as sc:
                speech_content = sc.read()

            # initialize speech sample
            sample = types.RecognitionAudio(content=speech_content)

            # run speech decoding
            try:
                result = client.recognize(opts, sample)
            except ValueError as e:
                print(e)
                result = None

            return result

        opts = {}
        opts['encoding']=enums.RecognitionConfig.AudioEncoding.FLAC
        opts['language_code'] = language_code
        opts['sample_rate_hertz'] = sample_rate
        opts['max_alternatives'] = max_alternatives
        opts['enable_word_time_offsets'] = enable_word_time_offsets
        if speech_context:
            opts['speech_contexts']=[types.SpeechContext(phrases=speech_context)]

        # read in wav
        audio = AudioSegment.from_wav(file_path)

        # segment into 1 minute chunks
        if len(audio)>60000:
            segments = list(range(0,len(audio),60000))
            if segments[-1]<len(audio):
                segments.append(len(audio)-1)
            print('Audio clip is longer than 1 minute.  Splitting into %d one minute segments...' % (len(segments)-1))
            audio_chunks = []
            for i in range(len(segments)-1):
                audio_chunks.append(audio[segments[i]:segments[i+1]])
        else:
            audio_chunks = [audio]

        # loop over audio segments
        results = []
        for idx, chunk in enumerate(audio_chunks):
            results.append(recognize(chunk, file_path+str(idx)))

        # return list of results
        return results

    def parse_response(results):
        """Parses response from google speech"""

        words = []

        for chunk in results:
            for result in chunk.results:
                    alternative = result.alternatives[0]
                    print('Transcript: {}'.format(alternative.transcript))
                    print('Confidence: {}'.format(alternative.confidence))

                    for word_info in alternative.words:
                        word = word_info.word
                        start_time = word_info.start_time
                        end_time = word_info.end_time
                        print('Word: {}, start_time: {}, end_time: {}'.format(
                            word,
                            start_time.seconds + start_time.nanos * 1e-9,
                            end_time.seconds + end_time.nanos * 1e-9))
                        words.append((str(word).upper(), start_time.seconds + start_time.nanos * 1e-9,
                                        end_time.seconds + end_time.nanos * 1e-9))
        return words

    # def parse_response(results):
    #     """Parses response from google speech"""
    #     words = []
    #     for idx, result in enumerate(results):
    #         if result is None:
    #             warnings.warn('No speech was decoded for segment %d' % (idx+1))
    #             words.append(None)
    #         else:
    #             try:
    #                 for segment in result:
    #                     for chunk in segment.transcript.split(' '):
    #                         if chunk != '':
    #                             words.append(str(chunk).upper())
    #             except:
    #                 warnings.warn('Error parsing response for segment %d' % (idx+1))
    #
    #     return words

    # MAIN #####################################################################

    # initialize speech client
    if keypath:
        credentials = service_account.Credentials.from_service_account_file(keypath)
        scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/cloud-platform'])
        client = speech.SpeechClient(credentials=scoped_credentials)
    else:
        client = speech.SpeechClient()

    # make a list of files
    files = []
    if path.endswith(".wav"):
        files = [path]
    else:
        listdirectory = os.listdir(path)
        for filename in listdirectory:
            if filename.endswith(".wav"):
                files.append(path + filename)

    # initialize list of words
    words = []
    raw = []

    # loop over files
    for i, f in enumerate(files):

        # print progress
        print('Decoding file ' + str(i+1) + ' of ' + str(len(files)))

        try:

            # start timer
            start = time.time()

            # decode file
            results = decode_file(f, client, speech_context, sample_rate,
                                  max_alternatives, enable_word_time_offsets)

            # parsing response
            parsed_results = parse_response(results)

            # save the processed file
            words.append(parsed_results)

            # save the processed file
            raw.append(results)

            if save:
                # save the raw response in a pickle
                pickle.dump(results, open(f + ".p", "wb" ) )

                # save a text file with just the words
                pd.DataFrame(parsed_results).to_csv(f + '.txt', header=False,
                                                    index=False)

            # print when finished
            print('Finished file ' + str(i+1) + ' of ' + str(len(files)) + ' in ' +
                  str(round(time.time()-start,2)) + ' seconds.')

        # handle when something goes wrong
        except ValueError as e:

            words.append("Error")
            print(e)
            print('Decoding of file ' + str(i) + 'failed.  Moving on to next file.')

    if return_raw:
        if len(words)>1:
            return raw
        else:
            return raw[0]
    else:
        if len(words)>1:
            return words
        else:
            return words[0]

Response: Decode a file or folder into a single object.


Instruction:
Given the following Python 3 function, write the documentation
def parse_value(val):

    mobj = re.match('(-{0,1}\d+)\s*(\sseconds|/\s*\w+)$',  val)
    if mobj:
        return int(mobj.group(1))
    mobj = re.match('(-{0,1}\d*\.\d+)\s*(\sseconds|/\s*\w+)$',  val)
    if mobj:
        return float(mobj.group(1))
    re.match('(-{0,1}\d+)\s*([GMK])B$',  val)
    if mobj:
        return int(mobj.group(1)) * memMultiplier[mobj.group(2)]
    mobj = re.match('(-{0,1}\d+(\.\d+){0,1})\s*\%$',  val)
    if mobj:
        return float(mobj.group(1)) / 100
    return val

Response: Parse input string and return int float or str depending on format.


Instruction:
Make a summary of the following Python 3 code
def _connect(self):
        if sys.version_info[:2] < (2,6):
            self._conn = httplib.HTTPConnection(self._host, self._port)
        else:
            self._conn = httplib.HTTPConnection(self._host, self._port,
                                                False, defaultTimeout)

Response: Connect to Squid Proxy Manager interface.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _retrieve(self, map):
        self._conn.request('GET', "cache_object://%s/%s" % (self._host, map),
                           None, self._httpHeaders)
        rp = self._conn.getresponse()
        if rp.status == 200:
            data = rp.read()
            return data
        else:
            raise Exception("Retrieval of stats from Squid Proxy Server"
                            "on host %s and port %s failed.\n"
                            "HTTP - Status: %s    Reason: %s"
                            % (self._host, self._port, rp.status, rp.reason))

Response: Retrieve statistics from Squid Proxy Server Manager Interface Interface.


Instruction:
Make a summary of the following Python 3 code
def _parseCounters(self, data):
        info_dict = util.NestedDict()
        for line in data.splitlines():
            mobj = re.match('^\s*([\w\.]+)\s*=\s*(\S.*)$', line)
            if mobj:
                (key, value) = mobj.groups()
                klist = key.split('.')
                info_dict.set_nested(klist, parse_value(value))
        return info_dict

Response:
Parse simple stats list of key value pairs.
idAdresse


Instruction:
How would you explain what the following Python 3 function does
def _parseSections(self, data):
        section_dict = {}
        lines = data.splitlines()
        idx = 0
        numlines = len(lines)
        section = None
        while idx < numlines:
            line = lines[idx]
            idx += 1
            mobj = re.match('^(\w[\w\s\(\)]+[\w\)])\s*:$', line)
            if mobj:
                section = mobj.group(1)
                section_dict[section] = []
            else:
                mobj = re.match('(\t|\s)\s*(\w.*)$', line)
                if mobj:
                    section_dict[section].append(mobj.group(2))
                else:
                    mobj = re.match('^(\w[\w\s\(\)]+[\w\)])\s*:\s*(\S.*)$', line)
                    if mobj:
                        section = None
                        if not section_dict.has_key(section):
                            section_dict[section] = []
                        section_dict[section].append(line)
                    else:
                        if not section_dict.has_key('PARSEERROR'):
                            section_dict['PARSEERROR'] = []
                        section_dict['PARSEERROR'].append(line)
        return section_dict

Response: Parse data and separate sections. Returns dictionary that maps section names to section data.


Instruction:
Implement a Python 3 function for
getting manager interface section list from Squid Proxy Server

Response:
def getMenu(self):
        """Get manager interface section list from Squid Proxy Server

        @return: List of tuples (section, description, type)

        """
        data = self._retrieve('')
        info_list = []
        for line in data.splitlines():
            mobj = re.match('^\s*(\S.*\S)\s*\t\s*(\S.*\S)\s*\t\s*(\S.*\S)$', line)
            if mobj:
                info_list.append(mobj.groups())
        return info_list


Instruction:
Can you write a function in Python 3 where it
returns dictionary of Traffic Stats for each Wanpipe Interface.

Response:
def getIfaceStats(self):
        """Return dictionary of Traffic Stats for each Wanpipe Interface.

        @return: Nested dictionary of statistics for each interface.

        """
        ifInfo = netiface.NetIfaceInfo()
        ifStats = ifInfo.getIfStats()
        info_dict = {}
        for ifname in ifStats:
            if re.match('^w\d+g\d+$', ifname):
                info_dict[ifname] = ifStats[ifname]
        return info_dict


Instruction:
Can you generate a brief explanation for the following Python 3 code
def getPRIstats(self, iface):
        info_dict = {}
        output = util.exec_command([wanpipemonCmd, '-i', iface, '-c',  'Ta'])
        for line in output.splitlines():
            mobj = re.match('^\s*(Line Code Violation|Far End Block Errors|'
                            'CRC4 Errors|FAS Errors)\s*:\s*(\d+)\s*$',
                            line, re.IGNORECASE)
            if mobj:
                info_dict[mobj.group(1).lower().replace(' ', '')] = int(mobj.group(2))
                continue
            mobj = re.match('^\s*(Rx Level)\s*:\s*>{0,1}\s*([-\d\.]+)db\s*',
                            line, re.IGNORECASE)
            if mobj:
                info_dict[mobj.group(1).lower().replace(' ', '')] = float(mobj.group(2))
                continue
        return info_dict

Response: Return RDSI Operational Stats for given interface.


Instruction:
Create a Python 3 function for
connecting to FreeSWITCH ESL Interface.

Response:
def _connect(self):
        """Connect to FreeSWITCH ESL Interface."""
        try:
            self._eslconn = ESL.ESLconnection(self._eslhost,
                                              str(self._eslport),
                                              self._eslpass)
        except:
            pass
        if not self._eslconn.connected():
            raise Exception(
                "Connection to FreeSWITCH ESL Interface on host %s and port %d failed."
                % (self._eslhost, self._eslport)
                )


Instruction:
Write a Python 3 function that can
execute command and return result body as list of lines.

Response:
def _execCmd(self, cmd, args):
        """Execute command and return result body as list of lines.

            @param cmd:  Command string.
            @param args: Comand arguments string.
            @return:     Result dictionary.

        """
        output = self._eslconn.api(cmd, args)
        if output:
            body = output.getBody()
            if body:
                return body.splitlines()
        return None


Instruction:
Explain what the following Python 3 code does
def _execShowCmd(self, showcmd):
        result = None
        lines = self._execCmd("show", showcmd)
        if lines and len(lines) >= 2 and lines[0] != '' and lines[0][0] != '-':
            result = {}
            result['keys'] = lines[0].split(',')
            items = []
            for line in lines[1:]:
                if line == '':
                    break
                items.append(line.split(','))
            result['items'] = items
        return result

Response: Execute show command and return result dictionary.


Instruction:
Can you generate the documentation for the following Python 3 function
def _execShowCountCmd(self, showcmd):
        result = None
        lines = self._execCmd("show", showcmd + " count")
        for line in lines:
            mobj = re.match('\s*(\d+)\s+total', line)
            if mobj:
                return int(mobj.group(1))
        return result

Response:
Execute show count command and return result dictionary.
alidisoiso


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

Response:
def retrieveVals(self):
        """Retrieve values for graphs."""
        if self.hasGraph('asterisk_calls') or self.hasGraph('asterisk_channels'):
            stats = self._ami.getChannelStats(self._chanList)
            if  self.hasGraph('asterisk_calls')  and stats:
                self.setGraphVal('asterisk_calls', 'active_calls',
                                 stats.get('active_calls'))
                self.setGraphVal('asterisk_calls', 'calls_per_min',
                                 stats.get('calls_processed'))
            if  self.hasGraph('asterisk_channels')  and stats:
                for field in self._chanList:
                    self.setGraphVal('asterisk_channels',
                                     field, stats.get(field))
                if 'dahdi' in self._chanList:
                    self.setGraphVal('asterisk_channels',
                                     'mix', stats.get('mix'))

        if self.hasGraph('asterisk_peers_sip'):
            stats = self._ami.getPeerStats('sip')
            if stats:
                for field in ('online', 'unmonitored', 'unreachable',
                              'lagged', 'unknown'):
                    self.setGraphVal('asterisk_peers_sip',
                                     field, stats.get(field))

        if self.hasGraph('asterisk_peers_iax2'):
            stats = self._ami.getPeerStats('iax2')
            if stats:
                for field in ('online', 'unmonitored', 'unreachable',
                              'lagged', 'unknown'):
                    self.setGraphVal('asterisk_peers_iax2',
                                     field, stats.get(field))

        if self.hasGraph('asterisk_voip_codecs'):
            sipstats = self._ami.getVoIPchanStats('sip', self._codecList) or {}
            iax2stats = self._ami.getVoIPchanStats('iax2', self._codecList) or {}
            if stats:
                for field in self._codecList:
                    self.setGraphVal('asterisk_voip_codecs', field,
                                     sipstats.get(field,0)
                                     + iax2stats.get(field, 0))
                self.setGraphVal('asterisk_voip_codecs', 'other',
                                 sipstats.get('other', 0)
                                 + iax2stats.get('other', 0))

        if self.hasGraph('asterisk_conferences'):
            stats = self._ami.getConferenceStats()
            if stats:
                self.setGraphVal('asterisk_conferences', 'rooms',
                                 stats.get('active_conferences'))
                self.setGraphVal('asterisk_conferences', 'users',
                                 stats.get('conference_users'))

        if self.hasGraph('asterisk_voicemail'):
            stats = self._ami.getVoicemailStats()
            if stats:
                self.setGraphVal('asterisk_voicemail', 'accounts',
                                 stats.get('accounts'))
                self.setGraphVal('asterisk_voicemail', 'msg_avg',
                                 stats.get('avg_messages'))
                self.setGraphVal('asterisk_voicemail', 'msg_max',
                                 stats.get('max_messages'))
                self.setGraphVal('asterisk_voicemail', 'msg_total',
                                 stats.get('total_messages'))

        if self.hasGraph('asterisk_trunks') and len(self._trunkList) > 0:
            stats = self._ami.getTrunkStats(self._trunkList)
            for trunk in self._trunkList:
                self.setGraphVal('asterisk_trunks', trunk[0],
                                 stats.get(trunk[0]))

        if self._queues is not None:
            total_answer = 0
            total_abandon = 0
            for queue in self._queue_list:
                stats = self._queues[queue]
                if self.hasGraph('asterisk_queue_len'):
                    self.setGraphVal('asterisk_queue_len', queue,
                                     stats.get('queue_len'))
                if self.hasGraph('asterisk_queue_avg_hold'):
                    self.setGraphVal('asterisk_queue_avg_hold', queue,
                                     stats.get('avg_holdtime'))
                if self.hasGraph('asterisk_queue_avg_talk'):
                    self.setGraphVal('asterisk_queue_avg_talk', queue,
                                     stats.get('avg_talktime'))
                if self.hasGraph('asterisk_queue_calls'):
                    total_abandon += stats.get('calls_abandoned')
                    total_answer += stats.get('calls_completed')
                if self.hasGraph('asterisk_queue_abandon_pcent'):
                    prev_stats = self._queues_prev.get(queue)
                    if prev_stats is not None:
                        abandon = (stats.get('calls_abandoned', 0) -
                                   prev_stats.get('calls_abandoned', 0))
                        answer = (stats.get('calls_completed', 0) -
                                  prev_stats.get('calls_completed', 0))
                        total = abandon + answer
                        if total > 0:
                            val = 100.0 * float(abandon) / float(total)
                        else:
                            val = 0
                        self.setGraphVal('asterisk_queue_abandon_pcent',
                                     queue, val)
            if self.hasGraph('asterisk_queue_calls'):
                    self.setGraphVal('asterisk_queue_calls', 'abandon',
                                     total_abandon)
                    self.setGraphVal('asterisk_queue_calls', 'answer',
                                     total_answer)

            if self.hasGraph('asterisk_fax_attempts'):
                fax_stats = self._ami.getFaxStatsCounters()
                stats = fax_stats.get('general')
                if stats is not None:
                    self.setGraphVal('asterisk_fax_attempts', 'send',
                                     stats.get('transmit attempts'))
                    self.setGraphVal('asterisk_fax_attempts', 'recv',
                                     stats.get('receive attempts'))
                    self.setGraphVal('asterisk_fax_attempts', 'fail',
                                     stats.get('failed faxes'))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def retrieveVals(self):
        fpminfo = PHPfpmInfo(self._host, self._port, self._user, self._password,
                             self._monpath, self._ssl)
        stats = fpminfo.getStats()
        if self.hasGraph('php_fpm_connections') and stats:
            self.setGraphVal('php_fpm_connections', 'conn',
                             stats['accepted conn'])
        if self.hasGraph('php_fpm_processes') and stats:
            self.setGraphVal('php_fpm_processes', 'active',
                             stats['active processes'])
            self.setGraphVal('php_fpm_processes', 'idle',
                             stats['idle processes'])
            self.setGraphVal('php_fpm_processes', 'total',
                             stats['total processes'])

Response: Retrieve values for graphs.


Instruction:
Can you implement a function in Python 3 that
implements Munin Plugin Auto - Configuration Option.

Response:
def autoconf(self):
        """Implements Munin Plugin Auto-Configuration Option.

        @return: True if plugin can be  auto-configured, False otherwise.

        """
        fpminfo = PHPfpmInfo(self._host, self._port, self._user, self._password,
                             self._monpath, self._ssl)
        return fpminfo is not None


Instruction:
Implement a Python 3 function for
pinging Redis Server and return Round - Trip - Time in seconds as float.

Response:
def ping(self):
        """Ping Redis Server and return Round-Trip-Time in seconds.

        @return: Round-trip-time in seconds as float.

        """
        start = time.time()
        self._conn.ping()
        return (time.time() - start)


Instruction:
How would you code a function in Python 3 to
take a list of lists of strings and lists of strings and returns a multi - indexed dataframe of subject data

Response:
def list2pd(all_data, subjindex=None, listindex=None):
    """
    Makes multi-indexed dataframe of subject data

    Parameters
    ----------
    all_data : list of lists of strings
        strings are either all presented or all recalled items, in the order of presentation or recall
        *should also work for presented / recalled ints and floats, if desired

    Returns
    ----------
    subs_list_of_dfs : multi-indexed dataframe
        dataframe of subject data (presented or recalled words/items), indexed by subject and list number
        cell populated by the term presented or recalled in the position indicated by the column number

    """
    # set default index if it is not defined
    # max_nlists = max(map(lambda x: len(x), all_data))
    listindex = [[idx for idx in range(len(sub))] for sub in all_data] if not listindex else listindex
    subjindex = [idx for idx,subj in enumerate(all_data)] if not subjindex else subjindex

    def make_multi_index(listindex, sub_num):
        return pd.MultiIndex.from_tuples([(sub_num,lst) for lst in listindex], names = ['Subject', 'List'])

    listindex = list(listindex)
    subjindex = list(subjindex)

    subs_list_of_dfs = [pd.DataFrame(sub_data, index=make_multi_index(listindex[sub_num], subjindex[sub_num])) for sub_num,sub_data in enumerate(all_data)]

    return pd.concat(subs_list_of_dfs)


Instruction:
Write a Python 3 script for
returning an Egg object from a zero - indexed recall matrix.

Response:
def recmat2egg(recmat, list_length=None):
        """
        Creates egg data object from zero-indexed recall matrix

        Parameters
        ----------
        recmat : list of lists (subs) of lists (encoding lists) of ints or 2D numpy array
            recall matrix representing serial positions of freely recalled words \
            e.g. [[[16, 15, 0, 2, 3, None, None...], [16, 4, 5, 6, 1, None, None...]]]

        list_length : int
            The length of each list (e.g. 16)

        Returns
        ----------
        egg : Egg data object
            egg data object computed from the recall matrix
        """
        from .egg import Egg as Egg

        pres = [[[str(word) for word in list(range(0,list_length))] for reclist in recsub] for recsub in recmat]
        rec = [[[str(word) for word in reclist if word is not None] for reclist in recsub] for recsub in recmat]

        return Egg(pres=pres,rec=rec)


Instruction:
Make a summary of the following Python 3 code
def default_dist_funcs(dist_funcs, feature_example):

        if dist_funcs is None:
            dist_funcs = dict()

        for key in feature_example:
            if key in dist_funcs:
                pass
            if key == 'item':
                pass
            elif isinstance(feature_example[key], (six.string_types, six.binary_type)):
                dist_funcs[key] = 'match'
            elif isinstance(feature_example[key], (int, np.integer, float)) or all([isinstance(i, (int, np.integer, float)) for i in feature_example[key]]):
                dist_funcs[key] = 'euclidean'

        return dist_funcs

Response: Fill in default distance metrics for fingerprint analyses


Instruction:
Given the following Python 3 function, write the documentation
def stack_eggs(eggs, meta='concatenate'):
    '''
    Takes a list of eggs, stacks them and reindexes the subject number

    Parameters
    ----------
    eggs : list of Egg data objects
        A list of Eggs that you want to combine
    meta : string
        Determines how the meta data of each Egg combines. Default is 'concatenate'
        'concatenate' concatenates keys in meta data dictionary shared between eggs, and copies non-overlapping keys
        'separate' keeps the Eggs' meta data dictionaries separate, with each as a list index in the stacked meta data

    Returns
    ----------
    new_egg : Egg data object
        A mega egg comprised of the input eggs stacked together

    '''
    from .egg import Egg

    pres = [egg.pres.loc[sub,:].values.tolist() for egg in eggs for sub in egg.pres.index.levels[0].values.tolist()]
    rec = [egg.rec.loc[sub,:].values.tolist() for egg in eggs for sub in egg.rec.index.levels[0].values.tolist()]

    if meta is 'concatenate':
        new_meta = {}
        for egg in eggs:
            for key in egg.meta:
                if key in new_meta:
                    new_meta[key] = list(new_meta[key])
                    new_meta[key].extend(egg.meta.get(key))
                else:
                    new_meta[key] = egg.meta.get(key)

    elif meta is 'separate':
        new_meta = list(egg.meta for egg in eggs)

    return Egg(pres=pres, rec=rec, meta=new_meta)

Response: Takes a list of eggs stacks them and reindexes the subject number


Instruction:
Given the following Python 3 function, write the documentation
def crack_egg(egg, subjects=None, lists=None):
    '''
    Takes an egg and returns a subset of the subjects or lists

    Parameters
    ----------
    egg : Egg data object
        Egg that you want to crack

    subjects : list
        List of subject idxs

    lists : list
        List of lists idxs

    Returns
    ----------
    new_egg : Egg data object
        A sliced egg, good on a salad

    '''
    from .egg import Egg

    if hasattr(egg, 'features'):
        all_have_features = egg.features is not None
    else:
        all_have_features=False
    opts = {}

    if subjects is None:
        subjects = egg.pres.index.levels[0].values.tolist()
    elif type(subjects) is not list:
        subjects = [subjects]

    if lists is None:
        lists = egg.pres.index.levels[1].values.tolist()
    elif type(lists) is not list:
        lists = [lists]

    idx = pd.IndexSlice
    pres = egg.pres.loc[idx[subjects,lists],egg.pres.columns]
    rec = egg.rec.loc[idx[subjects,lists],egg.rec.columns]

    pres = [pres.loc[sub,:].values.tolist() for sub in subjects]
    rec = [rec.loc[sub,:].values.tolist() for sub in subjects]

    if all_have_features:
        features = egg.features.loc[idx[subjects,lists],egg.features.columns]
        opts['features'] = [features.loc[sub,:].values.tolist() for sub in subjects]

    return Egg(pres=pres, rec=rec, **opts)

Response:
Takes an egg and returns a subset of the subjects or lists idxs

    Returns a new egg with the same contents


Instruction:
How would you code a function in Python 3 to
convert a MultiIndex DataFrame to list of lists of lists of values

Response:
def df2list(df):
    """
    Convert a MultiIndex df to list

    Parameters
    ----------

    df : pandas.DataFrame
        A MultiIndex DataFrame where the first level is subjects and the second
        level is lists (e.g. egg.pres)

    Returns
    ----------

    lst : a list of lists of lists of values
        The input df reformatted as a list

    """
    subjects = df.index.levels[0].values.tolist()
    lists = df.index.levels[1].values.tolist()
    idx = pd.IndexSlice
    df = df.loc[idx[subjects,lists],df.columns]
    lst = [df.loc[sub,:].values.tolist() for sub in subjects]
    return lst


Instruction:
How would you code a function in Python 3 to
fill in missing lists

Response:
def fill_missing(x):
    """
    Fills in missing lists (assumes end lists are missing)
    """

    # find subject with max number of lists
    maxlen = max([len(xi) for xi in x])

    subs = []

    for sub in x:
        if len(sub)<maxlen:
            for i in range(maxlen-len(sub)):
                sub.append([])
            new_sub = sub
        else:
            new_sub = sub
        subs.append(new_sub)
    return subs


Instruction:
Write a Python 3 script to
parse an egg and returns fields

Response:
def parse_egg(egg):
    """Parses an egg and returns fields"""
    pres_list = egg.get_pres_items().values[0]
    rec_list = egg.get_rec_items().values[0]
    feature_list = egg.get_pres_features().values[0]
    dist_funcs = egg.dist_funcs
    return pres_list, rec_list, feature_list, dist_funcs


Instruction:
Write a Python 3 script for
functioning that calculates the Fisher z - transformation of a given correlation value.

Response:
def r2z(r):
    """
    Function that calculates the Fisher z-transformation

    Parameters
    ----------
    r : int or ndarray
        Correlation value

    Returns
    ----------
    result : int or ndarray
        Fishers z transformed correlation value

    """
    with np.errstate(invalid='ignore', divide='ignore'):
        return 0.5 * (np.log(1 + r) - np.log(1 - r))


Instruction:
Explain what the following Python 3 code does
def z2r(z):
    with np.errstate(invalid='ignore', divide='ignore'):
        return (np.exp(2 * z) - 1) / (np.exp(2 * z) + 1)

Response: Function that calculates the inverse Fisher z - transformation


Instruction:
Create a Python 3 function for
shuffling an Egg s recalls

Response:
def shuffle_egg(egg):
    """ Shuffle an Egg's recalls"""
    from .egg import Egg
    pres, rec, features, dist_funcs = parse_egg(egg)

    if pres.ndim==1:
        pres = pres.reshape(1, pres.shape[0])
        rec = rec.reshape(1, rec.shape[0])
        features = features.reshape(1, features.shape[0])

    for ilist in range(rec.shape[0]):
        idx = np.random.permutation(rec.shape[1])
        rec[ilist,:] = rec[ilist,idx]

    return Egg(pres=pres, rec=rec, features=features, dist_funcs=dist_funcs)


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

Response:
def retrieveVals(self):
        """Retrieve values for graphs."""
        for iface in self._ifaceList:
            stats = self._ifaceStats.get(iface)
            graph_name = 'netiface_traffic_%s' % iface
            if self.hasGraph(graph_name):
                self.setGraphVal(graph_name, 'rx', stats.get('rxbytes') * 8)
                self.setGraphVal(graph_name, 'tx', stats.get('txbytes') * 8)
            graph_name = 'netiface_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))


Instruction:
Implement a Python 3 function for
getting NTP Peer Stats for localhost by querying local NTP Server.

Response:
def getPeerStats(self):
        """Get NTP Peer Stats for localhost by querying local NTP Server.

        @return: Dictionary of NTP stats converted to seconds.

        """
        info_dict = {}
        output = util.exec_command([ntpqCmd, '-n', '-c', 'peers'])
        for line in output.splitlines():
            mobj = re.match('\*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+', line)
            if mobj:
                info_dict['ip'] = mobj.group(1)
                cols = line.split()
                info_dict['stratum'] = int(cols[2])
                info_dict['delay'] = float(cols[7]) / 1000.0
                info_dict['offset'] = float(cols[8]) / 1000.0
                info_dict['jitter'] = float(cols[9]) / 1000.0
                return info_dict
        else:
            raise Exception("Execution of command failed: %s" % ntpqCmd)
        return info_dict


Instruction:
How would you explain what the following Python 3 function does
def getHostOffset(self, host):
        info_dict = {}
        output = util.exec_command([ntpdateCmd, '-u', '-q', host])
        for line in output.splitlines():
            mobj = re.match('server.*,\s*stratum\s+(\d),.*'
                            'offset\s+([\d\.-]+),.*delay\s+([\d\.]+)\s*$',
                            line)
            if mobj:
                info_dict['stratum'] = int(mobj.group(1))
                info_dict['delay'] = float(mobj.group(3))
                info_dict['offset'] = float(mobj.group(2))
                return info_dict
        return info_dict

Response: Get NTP Stats and offset of remote host relative to localhost.


Instruction:
Make a summary of the following Python 3 code
def getHostOffsets(self, hosts):
        info_dict = {}
        output = util.exec_command([ntpdateCmd, '-u', '-q'] + list(hosts))
        for line in output.splitlines():
            mobj = re.match('server\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}),'
                            '\s*stratum\s+(\d),.*offset\s+([\d\.-]+),'
                            '.*delay\s+([\d\.]+)\s*$', line)
            if mobj:
                host_dict = {}
                host = mobj.group(1)
                host_dict['stratum'] = int(mobj.group(2))
                host_dict['delay'] = float(mobj.group(4))
                host_dict['offset'] = float(mobj.group(3))
                info_dict[host] = host_dict
        return info_dict

Response:
Get NTP Stats and offset of multiple remote hosts relative to localhost
        by querying NTP Servers on remote hosts.


Instruction:
Implement a Python 3 function for
returning system uptime in seconds.

Response:
def getUptime(self):
        """Return system uptime in seconds.

        @return: Float that represents uptime in seconds.

        """
        try:
            fp = open(uptimeFile, 'r')
            line = fp.readline()
            fp.close()
        except:
            raise IOError('Failed reading stats from file: %s' % uptimeFile)
        return float(line.split()[0])


Instruction:
Given the following Python 3 function, write the documentation
def getLoadAvg(self):
        try:
            fp = open(loadavgFile, 'r')
            line = fp.readline()
            fp.close()
        except:
            raise IOError('Failed reading stats from file: %s' % loadavgFile)
        arr = line.split()
        if len(arr) >= 3:
            return [float(col) for col in arr[:3]]
        else:
            return None

Response: Return system Load Average.


Instruction:
Make a summary of the following Python 3 code
def getCPUuse(self):
        hz = os.sysconf('SC_CLK_TCK')
        info_dict = {}
        try:
            fp = open(cpustatFile, 'r')
            line = fp.readline()
            fp.close()
        except:
            raise IOError('Failed reading stats from file: %s' % cpustatFile)
        headers = ['user', 'nice', 'system', 'idle', 'iowait', 'irq', 'softirq', 'steal', 'guest']
        arr = line.split()
        if len(arr) > 1 and arr[0] == 'cpu':
            return dict(zip(headers[0:len(arr)], [(float(t) / hz) for t in arr[1:]]))
        return info_dict

Response: Return cpu time utilization in seconds.


Instruction:
Here you have a function in Python 3, explain what it does
def getProcessStats(self):
        info_dict = {}
        try:
            fp = open(cpustatFile, 'r')
            data = fp.read()
            fp.close()
        except:
            raise IOError('Failed reading stats from file: %s' % cpustatFile)
        for line in data.splitlines():
            arr = line.split()
            if len(arr) > 1 and arr[0] in ('ctxt', 'intr', 'softirq',
                                           'processes', 'procs_running',
                                           'procs_blocked'):
                info_dict[arr[0]] = arr[1]
        return info_dict

Response: Return stats for running processes forks context switches and interrupts.


Instruction:
Can you tell what is the following Python 3 function doing
def getMemoryUse(self):
        info_dict = {}
        try:
            fp = open(meminfoFile, 'r')
            data = fp.read()
            fp.close()
        except:
            raise IOError('Failed reading stats from file: %s' % meminfoFile)
        for line in data.splitlines():
            mobj = re.match('^(.+):\s*(\d+)\s*(\w+|)\s*$', line)
            if mobj:
                if mobj.group(3).lower() == 'kb':
                    mult = 1024
                else:
                    mult = 1
                info_dict[mobj.group(1)] = int(mobj.group(2)) * mult
        return info_dict

Response: Return stats for memory utilization.


Instruction:
Can you write a function in Python 3 where it
returns information on swap partition and files.

Response:
def getSwapStats(self):
        """Return information on swap partition and / or files.

            @return: Dictionary of stats.

        """
        info_dict = {}
        try:
            fp = open(swapsFile, 'r')
            data = fp.read()
            fp.close()
        except:
            raise IOError('Failed reading stats from file: %s' % swapsFile)
        lines = data.splitlines()
        if len(lines) > 1:
            colnames = [name.lower() for name in lines[0].split()]
            for line in lines[1:]:
                cols = line.split()
                info_dict[cols[0]] = dict(zip(colnames[1:], cols[1:]))
        return info_dict


Instruction:
Here you have a function in Python 3, explain what it does
def getVMstats(self):
        info_dict = {}
        try:
            fp = open(vmstatFile, 'r')
            data = fp.read()
            fp.close()
        except:
            raise IOError('Failed reading stats from file: %s' % vmstatFile)
        for line in data.splitlines():
            cols = line.split()
            if len(cols) == 2:
                info_dict[cols[0]] = cols[1]
        return info_dict

Response: Return stats for Virtual Memory Subsystem.


Instruction:
Can you tell what is the following Python 3 function doing
def _sendStatCmd(self,  cmd):
        try:
            self._conn.write("%s\r\n" % cmd)
            regex = re.compile('^(END|ERROR)\r\n', re.MULTILINE)
            (idx, mobj, text) = self._conn.expect([regex,], self._timeout) #@UnusedVariable
        except:
            raise Exception("Communication with %s failed" % self._instanceName)
        if mobj is not None:
            if mobj.group(1) == 'END':
                return text.splitlines()[:-1]
            elif mobj.group(1) == 'ERROR':
                raise Exception("Protocol error in communication with %s."
                                % self._instanceName)
        else:
            raise Exception("Connection with %s timed out." % self._instanceName)

Response: Send stat command to Memcached Server and return response lines.


Instruction:
Can you tell what is the following Python 3 function doing
def _parseStats(self, lines, parse_slabs = False):
        info_dict = {}
        info_dict['slabs'] = {}
        for line in lines:
            mobj = re.match('^STAT\s(\w+)\s(\S+)$',  line)
            if mobj:
                info_dict[mobj.group(1)] = util.parse_value(mobj.group(2), True)
                continue
            elif parse_slabs:
                mobj = re.match('STAT\s(\w+:)?(\d+):(\w+)\s(\S+)$',  line)
                if mobj:
                    (slab, key, val) = mobj.groups()[-3:]
                    if not info_dict['slabs'].has_key(slab):
                        info_dict['slabs'][slab] = {}
                    info_dict['slabs'][slab][key] = util.parse_value(val, True)
        return info_dict

Response: Parse memcached stats output from memcached and return dictionary of stats - nanos.


Instruction:
Implement a function in Python 3 to
retrieve values for graphs.

Response:
def retrieveVals(self):
        """Retrieve values for graphs."""
        if self._stats is None:
            serverInfo = MemcachedInfo(self._host,  self._port, self._socket_file)
            stats = serverInfo.getStats()
        else:
            stats = self._stats
        if stats is None:
            raise Exception("Undetermined error accesing stats.")
        stats['set_hits'] = stats.get('total_items')
        if stats.has_key('cmd_set') and stats.has_key('total_items'):
            stats['set_misses'] = stats['cmd_set'] - stats['total_items']
        self.saveState(stats)
        if self.hasGraph('memcached_connections'):
            self.setGraphVal('memcached_connections', 'conn',
                             stats.get('curr_connections'))
        if self.hasGraph('memcached_items'):
            self.setGraphVal('memcached_items', 'items',
                             stats.get('curr_items'))
        if self.hasGraph('memcached_memory'):
            self.setGraphVal('memcached_memory', 'bytes',
                             stats.get('bytes'))
        if self.hasGraph('memcached_connrate'):
            self.setGraphVal('memcached_connrate', 'conn',
                             stats.get('total_connections'))
        if self.hasGraph('memcached_traffic'):
            self.setGraphVal('memcached_traffic', 'rxbytes',
                             stats.get('bytes_read'))
            self.setGraphVal('memcached_traffic', 'txbytes',
                             stats.get('bytes_written'))
        if self.hasGraph('memcached_reqrate'):
            self.setGraphVal('memcached_reqrate', 'set',
                             stats.get('cmd_set'))
            self.setGraphVal('memcached_reqrate', 'get',
                             stats.get('cmd_get'))
            if self.graphHasField('memcached_reqrate', 'del'):
                self.setGraphVal('memcached_reqrate', 'del',
                                 safe_sum([stats.get('delete_hits'),
                                           stats.get('delete_misses')]))
            if self.graphHasField('memcached_reqrate', 'cas'):
                self.setGraphVal('memcached_reqrate', 'cas',
                                 safe_sum([stats.get('cas_hits'),
                                           stats.get('cas_misses'),
                                           stats.get('cas_badval')]))
            if self.graphHasField('memcached_reqrate', 'incr'):
                self.setGraphVal('memcached_reqrate', 'incr',
                                 safe_sum([stats.get('incr_hits'),
                                           stats.get('incr_misses')]))
            if self.graphHasField('memcached_reqrate', 'decr'):
                self.setGraphVal('memcached_reqrate', 'decr',
                                 safe_sum([stats.get('decr_hits'),
                                           stats.get('decr_misses')]))
        if self.hasGraph('memcached_statget'):
            self.setGraphVal('memcached_statget', 'hit',
                             stats.get('get_hits'))
            self.setGraphVal('memcached_statget', 'miss',
                             stats.get('get_misses'))
            self.setGraphVal('memcached_statget', 'total',
                             safe_sum([stats.get('get_hits'),
                                       stats.get('get_misses')]))
        if self.hasGraph('memcached_statset'):
            self.setGraphVal('memcached_statset', 'hit',
                             stats.get('set_hits'))
            self.setGraphVal('memcached_statset', 'miss',
                             stats.get('set_misses'))
            self.setGraphVal('memcached_statset', 'total',
                             safe_sum([stats.get('set_hits'),
                                       stats.get('set_misses')]))
        if self.hasGraph('memcached_statdel'):
            self.setGraphVal('memcached_statdel', 'hit',
                             stats.get('delete_hits'))
            self.setGraphVal('memcached_statdel', 'miss',
                             stats.get('delete_misses'))
            self.setGraphVal('memcached_statdel', 'total',
                             safe_sum([stats.get('delete_hits'),
                                       stats.get('delete_misses')]))
        if self.hasGraph('memcached_statcas'):
            self.setGraphVal('memcached_statcas', 'hit',
                             stats.get('cas_hits'))
            self.setGraphVal('memcached_statcas', 'miss',
                             stats.get('cas_misses'))
            self.setGraphVal('memcached_statcas', 'badval',
                             stats.get('cas_badval'))
            self.setGraphVal('memcached_statcas', 'total',
                             safe_sum([stats.get('cas_hits'),
                                       stats.get('cas_misses'),
                                       stats.get('cas_badval')]))
        if self.hasGraph('memcached_statincrdecr'):
            self.setGraphVal('memcached_statincrdecr', 'incr_hit',
                             stats.get('incr_hits'))
            self.setGraphVal('memcached_statincrdecr', 'decr_hit',
                             stats.get('decr_hits'))
            self.setGraphVal('memcached_statincrdecr', 'incr_miss',
                             stats.get('incr_misses'))
            self.setGraphVal('memcached_statincrdecr', 'decr_miss',
                             stats.get('decr_misses'))
            self.setGraphVal('memcached_statincrdecr', 'total',
                             safe_sum([stats.get('incr_hits'),
                                       stats.get('decr_hits'),
                                       stats.get('incr_misses'),
                                       stats.get('decr_misses')]))
        if self.hasGraph('memcached_statevict'):
            self.setGraphVal('memcached_statevict', 'evict',
                             stats.get('evictions'))
            if self.graphHasField('memcached_statevict', 'reclaim'):
                self.setGraphVal('memcached_statevict', 'reclaim',
                                 stats.get('reclaimed'))
        if self.hasGraph('memcached_statauth'):
            self.setGraphVal('memcached_statauth', 'reqs',
                             stats.get('auth_cmds'))
            self.setGraphVal('memcached_statauth', 'errors',
                             stats.get('auth_errors'))
        if self.hasGraph('memcached_hitpct'):
            prev_stats = self._prev_stats
            for (field_name,  field_hits,  field_misses) in (
                    ('set',  'set_hits',  'set_misses'),
                    ('get',  'get_hits',  'get_misses'),
                    ('del',  'delete_hits',  'delete_misses'),
                    ('cas',  'cas_hits',  'cas_misses'),
                    ('incr',  'incr_hits',  'incr_misses'),
                    ('decr',  'decr_hits',  'decr_misses')
                ):
                if prev_stats:
                    if (stats.has_key(field_hits)
                        and prev_stats.has_key(field_hits)
                        and stats.has_key(field_misses)
                        and prev_stats.has_key(field_misses)):
                        hits = stats[field_hits] - prev_stats[field_hits]
                        misses = stats[field_misses] - prev_stats[field_misses]
                        total = hits + misses
                        if total > 0:
                            val = 100.0 * hits / total
                        else:
                            val = 0
                        self.setGraphVal('memcached_hitpct',  field_name,
                                         round(val,  2))


Instruction:
Create a Python 3 function to
implement Munin Plugin Auto - Configuration Option.

Response:
def autoconf(self):
        """Implements Munin Plugin Auto-Configuration Option.

        @return: True if plugin can be  auto-configured, False otherwise.

        """
        serverInfo = MemcachedInfo(self._host,  self._port, self._socket_file)
        return (serverInfo is not None)


Instruction:
Can you implement a function in Python 3 that
returns correlation distance between a and b

Response:
def correlation(a, b):
    "Returns correlation distance between a and b"
    if isinstance(a, list):
        a = np.array(a)
    if isinstance(b, list):
        b = np.array(b)
    a = a.reshape(1, -1)
    b = b.reshape(1, -1)
    return cdist(a, b, 'correlation')


Instruction:
Explain what the following Python 3 code does
def euclidean(a, b):
    "Returns euclidean distance between a and b"
    return np.linalg.norm(np.subtract(a, b))

Response: Returns euclidean distance between a and b


Instruction:
Write a Python 3 function that can
execute ps command with positional params args and return result as list of lines.

Response:
def execProcCmd(self, *args):
        """Execute ps command with positional params args and return result as
        list of lines.

        @param *args: Positional params for ps command.
        @return:      List of output lines

        """
        out = util.exec_command([psCmd,] + list(args))
        return out.splitlines()


Instruction:
Create a Python 3 function to
execute ps command with custom output format with columns from fields and return result as a nested list of rows and columns.

Response:
def parseProcCmd(self, fields=('pid', 'user', 'cmd',), threads=False):
        """Execute ps command with custom output format with columns from
        fields and return result as a nested list.

        The Standard Format Specifiers from ps man page must be used for the
        fields parameter.

        @param fields:  List of fields included in the output.
                        Default: pid, user, cmd
        @param threads: If True, include threads in output.
        @return:        List of headers and list of rows and columns.

        """
        args = []
        headers = [f.lower() for f in fields]
        args.append('--no-headers')
        args.append('-e')
        if threads:
            args.append('-T')
        field_ranges = []
        fmt_strs = []
        start = 0
        for header in headers:
            field_width = psFieldWidth.get(header, psDefaultFieldWidth)
            fmt_strs.append('%s:%d' % (header, field_width))
            end = start + field_width + 1
            field_ranges.append((start,end))
            start = end
        args.append('-o')
        args.append(','.join(fmt_strs))
        lines = self.execProcCmd(*args)
        if len(lines) > 0:
            stats = []
            for line in lines:
                cols = []
                for (start, end) in field_ranges:
                    cols.append(line[start:end].strip())
                stats.append(cols)
            return {'headers': headers, 'stats': stats}
        else:
            return None


Instruction:
Make a summary of the following Python 3 code
def getProcList(self, fields=('pid', 'user', 'cmd',), threads=False,
                    **kwargs):
        """Execute ps command with custom output format with columns columns
        from fields, select lines using the filters defined by kwargs and return
        result as a nested list.

        The Standard Format Specifiers from ps man page must be used for the
        fields parameter.

        @param fields:   Fields included in the output.
                         Default: pid, user, cmd
        @param threads:  If True, include threads in output.
        @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:         List of headers and list of rows and columns.

        """
        field_list = list(fields)
        for key in kwargs:
            col = re.sub('(_ic)?(_regex)?$', '', key)
            if not col in field_list:
                field_list.append(col)
        pinfo = self.parseProcCmd(field_list, threads)
        if pinfo:
            if len(kwargs) > 0:
                pfilter = util.TableFilter()
                pfilter.registerFilters(**kwargs)
                stats = pfilter.applyFilters(pinfo['headers'], pinfo['stats'])
                return {'headers': pinfo['headers'], 'stats': stats}
            else:
                return pinfo
        else:
            return None

Response:
Execute ps command with custom output format with columns
 from fields and return a nested list of rows and columns.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def getProcDict(self, fields=('user', 'cmd',), threads=False, **kwargs):
        stats = {}
        field_list = list(fields)
        num_cols = len(field_list)
        if threads:
            key = 'spid'
        else:
            key = 'pid'
        try:
            key_idx = field_list.index(key)
        except ValueError:
            field_list.append(key)
            key_idx = len(field_list) - 1
        result = self.getProcList(field_list, threads, **kwargs)
        if result is not None:
            headers = result['headers'][:num_cols]
            lines = result['stats']
            if len(lines) > 1:
                for cols in lines:
                    stats[cols[key_idx]] = dict(zip(headers, cols[:num_cols]))
            return stats
        else:
            return None

Response: Execute ps command with custom output format with columns from fields and return result as a nested dictionary with the keys PID and SPID.


Instruction:
Can you write a function in Python 3 where it
returns a dictionary of process counts per status and priority.

Response:
def getProcStatStatus(self, threads=False, **kwargs):
        """Return process counts per status and priority.

        @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 of process counters.

        """
        procs = self.getProcList(['stat',], threads=threads, **kwargs)
        status = dict(zip(procStatusNames.values(),
                          [0,] * len(procStatusNames)))
        prio = {'high': 0, 'low': 0, 'norm': 0, 'locked_in_mem': 0}
        total = 0
        locked_in_mem = 0
        if procs is not None:
            for cols in procs['stats']:
                col_stat = cols[0]
                status[procStatusNames[col_stat[0]]] += 1
                if '<' in col_stat[1:]:
                    prio['high'] += 1
                elif 'N' in col_stat[1:]:
                    prio['low'] += 1
                else:
                    prio['norm'] += 1
                if 'L' in col_stat[1:]:
                    locked_in_mem += 1
                total += 1
        return {'status': status,
                'prio': prio,
                'locked_in_mem': locked_in_mem,
                'total': total}


Instruction:
Here you have a function in Python 3, explain what it does
def retrieveVals(self):
        ntpinfo = NTPinfo()
        stats = ntpinfo.getPeerStats()
        if stats:
            if self.hasGraph('ntp_peer_stratum'):
                self.setGraphVal('ntp_peer_stratum', 'stratum',
                                 stats.get('stratum'))
            if self.hasGraph('ntp_peer_stats'):
                self.setGraphVal('ntp_peer_stats', 'offset',
                                 stats.get('offset'))
                self.setGraphVal('ntp_peer_stats', 'delay',
                                 stats.get('delay'))
                self.setGraphVal('ntp_peer_stats', 'jitter',
                                 stats.get('jitter'))

Response: Retrieve values for graphs.


Instruction:
How would you explain what the following Python 3 function does
def fixLabel(label, maxlen, delim=None, repl='', truncend=True):
    if len(label) <= maxlen:
        return label
    else:
        maxlen -= len(repl)
        if delim is not None:
            if truncend:
                end = label.rfind(delim, 0, maxlen)
                if end > 0:
                    return label[:end+1] + repl
            else:
                start = label.find(delim, len(label) - maxlen)
                if start > 0:
                    return repl + label[start:]
        if truncend:
            return label[:maxlen] + repl
        else:
            return repl + label[-maxlen:]

Response: Truncate long graph and field labels.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _getGraph(self, graph_name, fail_noexist=False):
        graph = self._graphDict.get(graph_name)
        if fail_noexist and graph is None:
            raise AttributeError("Invalid graph name: %s" % graph_name)
        else:
            return graph

Response: Private method for returning graph object with name graph_name.


Instruction:
Here you have a function in Python 3, explain what it does
def _getMultigraphID(self, graph_name, subgraph_name=None):
        if self.isMultiInstance and self._instanceName is not None:
            if subgraph_name is None:
                return "%s_%s" % (graph_name, self._instanceName)
            else:
                return "%s_%s.%s_%s" % (graph_name, self._instanceName,
                                        subgraph_name, self._instanceName)
        else:
            if subgraph_name is None:
                return graph_name
            else:
                return "%s.%s" % (graph_name, subgraph_name)

Response: Private method for generating Multigraph ID from graph name and subgraph name.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _formatConfig(self, conf_dict):
        confs = []
        graph_dict = conf_dict['graph']
        field_list = conf_dict['fields']

        # Order and format Graph Attributes
        title = graph_dict.get('title')
        if title is not None:
            if self.isMultiInstance and self._instanceLabel is not None:
                if self._instanceLabelType == 'suffix':
                    confs.append("graph_%s %s - %s" % ('title',
                                                       title,
                                                       self._instanceLabel,))
                elif self._instanceLabelType == 'prefix':
                    confs.append("graph_%s %s - %s" % ('title',
                                                       self._instanceLabel,
                                                       title,))
            else:
                confs.append("graph_%s %s" % ('title', title))
        for key in ('category', 'vlabel', 'info', 'args', 'period',
                    'scale', 'total', 'order', 'printf', 'width', 'height'):
            val = graph_dict.get(key)
            if val is not None:
                if isinstance(val, bool):
                    if val:
                        val = "yes"
                    else:
                        val = "no"
                confs.append("graph_%s %s" % (key, val))

        # Order and Format Field Attributes
        for (field_name, field_attrs) in field_list:
            for key in ('label', 'type', 'draw', 'info', 'extinfo', 'colour',
                        'negative', 'graph', 'min', 'max', 'cdef',
                        'line', 'warning', 'critical'):
                val = field_attrs.get(key)
                if val is not None:
                    if isinstance(val, bool):
                        if val:
                            val = "yes"
                        else:
                            val = "no"
                    confs.append("%s.%s %s" % (field_name, key, val))
        return "\n".join(confs)

Response: Formats the configuration dictionary into a list of multi - line text entries.


Instruction:
Create a Python 3 function to
return value for environment variable or None. unityName

Response:
def envGet(self, name, default=None, conv=None):
        """Return value for environment variable or None.

        @param name:    Name of environment variable.
        @param default: Default value if variable is undefined.
        @param conv:    Function for converting value to desired type.
        @return:        Value of environment variable.

        """
        if self._env.has_key(name):
            if conv is not None:
                return conv(self._env.get(name))
            else:
                return self._env.get(name)
        else:
            return default


Instruction:
Can you generate the documentation for the following Python 3 function
def envGetList(self, name, attr_regex = '^\w+$', conv=None):
        key = "list_%s" % name
        item_list = []
        if self._env.has_key(key):
            if attr_regex:
                recomp = re.compile(attr_regex)
            else:
                recomp = None
            for attr in self._env[key].split(','):
                attr = attr.strip()
                if recomp is None or recomp.search(attr):
                    if conv is not None:
                        item_list.append(conv(attr))
                    else:
                        item_list.append(attr)

        return item_list

Response: Parse the environment variables to return list of items.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def envRegisterFilter(self, name, attr_regex = '^\w+$', default = True):
        attrs = {}
        for prefix in ('include', 'exclude'):
            key = "%s_%s" % (prefix, name)
            val = self._env.get(key)
            if val:
                attrs[prefix] = [attr.strip() for attr in val.split(',')]
            else:
                attrs[prefix] = []
        self._filters[name] = MuninAttrFilter(attrs['include'], attrs['exclude'],
                                              attr_regex, default)

Response:
Register filter for including excluding attributes in graphs through
        the use of include_<name and exclude_<name > environment variables.


Instruction:
Implement a function in Python 3 to
check if a specific graph attribute is enabled or disabled through the use of a filter based on include_<name > and exclude_<name > environment variables.

Response:
def envCheckFilter(self, name, attr):
        """Check if a specific graph attribute is enabled or disabled through
        the use of a filter based on include_<name> and exclude_<name>
        environment variables.

        @param name: Name of the Filter.
        @param attr: Name of the Attribute.
        @return:     Return True if the attribute is enabled.

        """
        flt = self._filters.get(name)
        if flt:
            return flt.check(attr)
        else:
            raise AttributeError("Undefined filter: %s" % name)


Instruction:
Can you tell what is the following Python 3 function doing
def envCheckFlag(self, name, default = False):
        if self._flags.has_key(name):
            return self._flags[name]
        else:
            val = self._env.get(name)
            if val is None:
                return default
            elif val.lower() in ['yes', 'on']:
                self._flags[name] = True
                return True
            elif val.lower() in ['no', 'off']:
                self._flags[name] = False
                return False
            else:
                raise AttributeError("Value for flag %s, must be yes, no, on or off"
                                     % name)

Response: Checks if the environment variable name is in the flag list.


Instruction:
Can you generate the documentation for the following Python 3 function
def saveState(self,  stateObj):
        try:
            fp = open(self._stateFile,  'w')
            pickle.dump(stateObj, fp)
        except:
            raise IOError("Failure in storing plugin state in file: %s"
                          % self._stateFile)
        return True

Response:
Utility methos to save plugin state stored in stateObj to persistent
        storage


Instruction:
Given the following Python 3 function, write the documentation
def restoreState(self):
        if os.path.exists(self._stateFile):
            try:
                fp = open(self._stateFile,  'r')
                stateObj = pickle.load(fp)
            except:
                raise IOError("Failure in reading plugin state from file: %s"
                              % self._stateFile)
            return stateObj
        return None

Response:
Utility method to restore plugin state from persistent storage to
        permit access to previous plugin state.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def appendSubgraph(self, parent_name,  graph_name, graph):
        if not self.isMultigraph:
            raise AttributeError("Simple Munin Plugins cannot have subgraphs.")
        if self._graphDict.has_key(parent_name):
            if not self._subgraphDict.has_key(parent_name):
                self._subgraphDict[parent_name] = {}
                self._subgraphNames[parent_name] = []
            self._subgraphDict[parent_name][graph_name] = graph
            self._subgraphNames[parent_name].append(graph_name)
        else:
            raise AttributeError("Invalid parent graph name %s used for subgraph %s."
                % (parent_name,  graph_name))

Response: This method adds a subgraph to the internal dictionary.


Instruction:
Can you generate the documentation for the following Python 3 function
def setGraphVal(self, graph_name, field_name, val):
        graph = self._getGraph(graph_name, True)
        if graph.hasField(field_name):
            graph.setVal(field_name, val)
        else:
            raise AttributeError("Invalid field name %s for graph %s."
                                 % (field_name, graph_name))

Response: Private method to set Value for Field in Graph.


Instruction:
Explain what the following Python 3 code does
def setSubgraphVal(self,  parent_name,  graph_name, field_name, val):
        subgraph = self._getSubGraph(parent_name, graph_name, True)
        if subgraph.hasField(field_name):
            subgraph.setVal(field_name, val)
        else:
            raise AttributeError("Invalid field name %s for subgraph %s "
                                 "of parent graph %s."
                                 % (field_name, graph_name, parent_name))

Response: Set the value of a field in a subgraph.


Instruction:
Implement a function in Python 3 to
return list of names of subgraphs for parent_name.

Response:
def getSubgraphList(self, parent_name):
        """Returns list of names of subgraphs for Root Graph with name parent_name.

        @param parent_name: Name of Root Graph.
        @return:            List of subgraph names.

        """
        if not self.isMultigraph:
            raise AttributeError("Simple Munin Plugins cannot have subgraphs.")
        if self._graphDict.has_key(parent_name):
            return self._subgraphNames[parent_name] or []
        else:
            raise AttributeError("Invalid parent graph name %s."
                                 % (parent_name,))


Instruction:
Write a Python 3 function that can
return true if graph with name graph_name has field with name field_name.

Response:
def graphHasField(self, graph_name, field_name):
        """Return true if graph with name graph_name has field with
        name field_name.

        @param graph_name: Graph Name
        @param field_name: Field Name.
        @return: Boolean

        """
        graph = self._graphDict.get(graph_name, True)
        return graph.hasField(field_name)


Instruction:
Write a Python 3 function for
returning true if subgraph with name graph_name with parent graph with name parent_name has field with name field_name.

Response:
def subGraphHasField(self, parent_name, graph_name, field_name):
        """Return true if subgraph with name graph_name with parent graph with
        name parent_name has field with name field_name.

        @param parent_name: Root Graph Name
        @param graph_name:  Subgraph Name
        @param field_name:  Field Name.
        @return:            Boolean

        """
        subgraph = self._getSubGraph(parent_name, graph_name, True)
        return subgraph.hasField(field_name)


Instruction:
Can you write a function in Python 3 where it
returns list of names of fields for graph with name graph_name.

Response:
def getGraphFieldList(self, graph_name):
        """Returns list of names of fields for graph with name graph_name.

        @param graph_name: Graph Name
        @return:           List of field names for graph.

        """
        graph = self._getGraph(graph_name, True)
        return graph.getFieldList()


Instruction:
Can you implement a function in Python 3 that
returns the number of fields for a given graph with name graph_name.

Response:
def getGraphFieldCount(self, graph_name):
        """Returns number of fields for graph with name graph_name.

        @param graph_name: Graph Name
        @return:           Number of fields for graph.

        """
        graph = self._getGraph(graph_name, True)
        return graph.getFieldCount()


Instruction:
Given the following Python 3 function, write the documentation
def getSubgraphFieldList(self, parent_name, graph_name):
        graph = self._getSubGraph(parent_name, graph_name, True)
        return graph.getFieldList()

Response:
Returns list of names of fields for subgraph with name graph_name
        and parent graph with name parent_name.


Instruction:
Can you tell what is the following Python 3 function doing
def getSubgraphFieldCount(self, parent_name, graph_name):
        graph = self._getSubGraph(parent_name, graph_name, True)
        return graph.getFieldCount()

Response:
Returns the number of fields for subgraph with name graph_name and parent
        graph with name parent_name.


Instruction:
Can you implement a function in Python 3 that
implements Munin Plugin Graph Configuration.

Response:
def config(self):
        """Implements Munin Plugin Graph Configuration.

        Prints out configuration for graphs.

        Use as is. Not required to be overwritten in child classes. The plugin
        will work correctly as long as the Munin Graph objects have been
        populated.

        """
        for parent_name in self._graphNames:
            graph = self._graphDict[parent_name]
            if self.isMultigraph:
                print "multigraph %s" % self._getMultigraphID(parent_name)
            print self._formatConfig(graph.getConfig())
            print
        if (self.isMultigraph and self._nestedGraphs
            and self._subgraphDict and self._subgraphNames):
            for (parent_name, subgraph_names) in self._subgraphNames.iteritems():
                for graph_name in subgraph_names:
                    graph = self._subgraphDict[parent_name][graph_name]
                    print "multigraph %s" % self.getMultigraphID(parent_name,
                                                                 graph_name)
                    print self._formatConfig(graph.getConfig())
                    print
        return True


Instruction:
Write a Python 3 function for
implementing Munin Plugin Fetch Option.

Response:
def fetch(self):
        """Implements Munin Plugin Fetch Option.

        Prints out measured values.

        """
        self.retrieveVals()
        for parent_name in self._graphNames:
            graph = self._graphDict[parent_name]
            if self.isMultigraph:
                print "multigraph %s" % self._getMultigraphID(parent_name)
            print self._formatVals(graph.getVals())
            print
        if (self.isMultigraph and self._nestedGraphs
            and self._subgraphDict and self._subgraphNames):
            for (parent_name, subgraph_names) in self._subgraphNames.iteritems():
                for graph_name in subgraph_names:
                    graph = self._subgraphDict[parent_name][graph_name]
                    print "multigraph %s" % self.getMultigraphID(parent_name,
                                                                 graph_name)
                    print self._formatVals(graph.getVals())
                    print
        return True


Instruction:
Create a Python 3 function for
implementing main entry point for plugin execution.

Response:
def run(self):
        """Implements main entry point for plugin execution."""
        if len(self._argv) > 1 and len(self._argv[1]) > 0:
            oper = self._argv[1]
        else:
            oper = 'fetch'
        if oper == 'fetch':
            ret = self.fetch()
        elif oper == 'config':
            ret = self.config()
            if ret and self._dirtyConfig:
                ret = self.fetch()
        elif oper == 'autoconf':
            ret = self.autoconf()
            if ret:
                print "yes"
            else:
                print "no"
            ret = True
        elif oper == 'suggest':
            ret = self.suggest()
        else:
            raise AttributeError("Invalid command argument: %s" % oper)
        return ret


Instruction:
Write a Python 3 script for
adding a new field to the Munin Graph.

Response:
def addField(self, name, label, type=None,  draw=None, info=None, #@ReservedAssignment
                 extinfo=None, colour=None, negative=None, graph=None,
                 min=None, max=None, cdef=None, line=None, #@ReservedAssignment
                 warning=None, critical=None):
        """Add field to Munin Graph

            @param name:     Field Name
            @param label:    Field Label
            @param type:     Stat Type:
                             'COUNTER' / 'ABSOLUTE' / 'DERIVE' / 'GAUGE'
            @param draw:     Graph Type:
                             'AREA' / 'LINE{1,2,3}' /
                             'STACK' / 'LINESTACK{1,2,3}' / 'AREASTACK'
            @param info:     Detailed Field Info
            @param extinfo:  Extended Field Info
            @param colour:   Field Colour
            @param negative: Mirror Value
            @param graph:    Draw on Graph - True / False (Default: True)
            @param min:      Minimum Valid Value
            @param max:      Maximum Valid Value
            @param cdef:     CDEF
            @param line:     Adds horizontal line at value defined for field.
            @param warning:  Warning Value
            @param critical: Critical Value

        """
        if self._autoFixNames:
            name = self._fixName(name)
            if negative is not None:
                negative = self._fixName(negative)
        self._fieldAttrDict[name] = dict(((k,v) for (k,v) in locals().iteritems()
                                         if (v is not None
                                             and k not in ('self',))))
        self._fieldNameList.append(name)


Instruction:
Here you have a function in Python 3, explain what it does
def hasField(self, name):
        if self._autoFixNames:
            name = self._fixName(name)
        return self._fieldAttrDict.has_key(name)

Response: Returns true if field with field_name exists.


Instruction:
Make a summary of the following Python 3 code
def getConfig(self):
        return {'graph': self._graphAttrDict,
                'fields': [(field_name, self._fieldAttrDict.get(field_name))
                           for field_name in self._fieldNameList]}

Response: Returns dictionary of config entries for Munin Graph.


Instruction:
Implement a function in Python 3 to
set value for the given name.

Response:
def setVal(self, name, val):
        """Set value for field in graph.

        @param name   : Graph Name
        @param value  : Value for field.

        """
        if self._autoFixNames:
            name = self._fixName(name)
        self._fieldValDict[name] = val


Instruction:
Can you generate a brief explanation for the following Python 3 code
def getVals(self):
        return [(name, self._fieldValDict.get(name))
                for name in self._fieldNameList]

Response: Returns value list for Munin Graph


Instruction:
Write a Python 3 script for
querying and parse Web Server Status Page.

Response:
def initStats(self, extras=None):
        """Query and parse Web Server Status Page.

        @param extras: Include extra metrics, which can be computationally more
                       expensive.

        """
        if extras is not None:
            self._extras = extras
        if self._extras:
            detail = 1
        else:
            detail = 0
        url = "%s://%s:%d/%s?detail=%s" % (self._proto, self._host, self._port,
                                           self._monpath, detail)
        response = util.get_url(url, self._user, self._password)
        self._statusDict = {}
        for line in response.splitlines():
            cols = line.split(':')
            if not self._statusDict.has_key(cols[0]):
                self._statusDict[cols[0]] = {}
            self._statusDict[cols[0]][cols[1]] = util.parse_value(cols[2])


Instruction:
Make a summary of the following Python 3 code
def initStats(self):
        url = "%s://%s:%d/%s" % (self._proto, self._host, self._port,
                                 self._statuspath)
        response = util.get_url(url, self._user, self._password)
        self._statusDict = {}
        for line in response.splitlines():
            mobj = re.match('\s*(\d+)\s+(\d+)\s+(\d+)\s*$', line)
            if mobj:
                idx = 0
                for key in ('accepts','handled','requests'):
                    idx += 1
                    self._statusDict[key] = util.parse_value(mobj.group(idx))
            else:
                for (key,val) in re.findall('(\w+):\s*(\d+)', line):
                    self._statusDict[key.lower()] = util.parse_value(val)

Response: Query and parse Nginx Web Server Status Page.


Instruction:
Here you have a function in Python 3, explain what it does
def read_file(filename):
    path = os.path.abspath(os.path.dirname(__file__))
    filepath = os.path.join(path, filename)
    try:
        return open(filepath).read()
    except IOError:
        return ''

Response: Read a file into a string


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

Response:
def retrieveVals(self):
        """Retrieve values for graphs."""
        proc_info = ProcessInfo()
        stats = {}
        for (prefix, is_thread) in (('proc', False),
                                    ('thread', True)):
            graph_name = '%s_status' % prefix
            if self.hasGraph(graph_name):
                if not stats.has_key(prefix):
                    stats[prefix] = proc_info.getProcStatStatus(is_thread)
                for (fname, stat_key) in (
                    ('unint_sleep', 'uninterruptable_sleep'),
                    ('stopped', 'stopped'),
                    ('defunct', 'defunct'),
                    ('running', 'running'),
                    ('sleep', 'sleep')):
                    self.setGraphVal(graph_name, fname,
                                     stats[prefix]['status'].get(stat_key))
            graph_name = '%s_prio' % prefix
            if self.hasGraph(graph_name):
                if not stats.has_key(prefix):
                    stats[prefix] = proc_info.getProcStatStatus(is_thread)
                for (fname, stat_key) in (
                    ('high', 'high'),
                    ('low', 'low'),
                    ('norm', 'norm'),
                    ('locked', 'locked_in_mem')):
                    self.setGraphVal(graph_name, fname,
                                     stats[prefix]['prio'].get(stat_key))


Instruction:
How would you code a function in Python 3 to
retrieve values for graphs.

Response:
def retrieveVals(self):
        """Retrieve values for graphs."""
        file_stats = self._fileInfo.getContainerStats()
        for contname in self._fileContList:
            stats = file_stats.get(contname)
            if stats is not None:
                if self.hasGraph('rackspace_cloudfiles_container_size'):
                    self.setGraphVal('rackspace_cloudfiles_container_size', contname,
                                     stats.get('size'))
                if self.hasGraph('rackspace_cloudfiles_container_count'):
                    self.setGraphVal('rackspace_cloudfiles_container_count', contname,
                                     stats.get('count'))


Instruction:
Create a Python 3 function for
returning nested dictionary of Traffic Stats for each network interface.

Response:
def getIfStats(self):
        """Return dictionary of Traffic Stats for Network Interfaces.

        @return: Nested dictionary of statistics for each interface.

        """
        info_dict = {}
        try:
            fp = open(ifaceStatsFile, 'r')
            data = fp.read()
            fp.close()
        except:
            raise IOError('Failed reading interface stats from file: %s'
                          % ifaceStatsFile)
        for line in data.splitlines():
            mobj = re.match('^\s*([\w\d:]+):\s*(.*\S)\s*$', line)
            if mobj:
                iface = mobj.group(1)
                statline = mobj.group(2)
                info_dict[iface] = dict(zip(
                    ('rxbytes', 'rxpackets', 'rxerrs', 'rxdrop', 'rxfifo',
                     'rxframe', 'rxcompressed', 'rxmulticast',
                     'txbytes', 'txpackets', 'txerrs', 'txdrop', 'txfifo',
                     'txcolls', 'txcarrier', 'txcompressed'),
                    [int(x) for x in statline.split()]))

        return info_dict


Instruction:
Here you have a function in Python 3, explain what it does
def getIfConfig(self):
        conf = {}
        try:
            out = subprocess.Popen([ipCmd, "addr", "show"],
                                   stdout=subprocess.PIPE).communicate()[0]
        except:
            raise Exception('Execution of command %s failed.' % ipCmd)
        for line in out.splitlines():
            mobj = re.match('^\d+: (\S+):\s+<(\S*)>\s+(\S.*\S)\s*$', line)
            if mobj:
                iface = mobj.group(1)
                conf[iface] = {}
                continue
            mobj = re.match('^\s{4}link\/(.*\S)\s*$', line)
            if mobj:
                arr = mobj.group(1).split()
                if len(arr) > 0:
                    conf[iface]['type'] = arr[0]
                if len(arr) > 1:
                    conf[iface]['hwaddr'] = arr[1]
                continue
            mobj = re.match('^\s+(inet|inet6)\s+([\d\.\:A-Za-z]+)\/(\d+)($|\s+.*\S)\s*$', line)
            if mobj:
                proto = mobj.group(1)
                if not conf[iface].has_key(proto):
                    conf[iface][proto] = []
                addrinfo = {}
                addrinfo['addr'] = mobj.group(2).lower()
                addrinfo['mask'] = int(mobj.group(3))
                arr = mobj.group(4).split()
                if len(arr) > 0 and arr[0] == 'brd':
                    addrinfo['brd'] = arr[1]
                conf[iface][proto].append(addrinfo)
                continue
        return conf

Response: Return dictionary of Interface Configuration keyed by if name.


Instruction:
Write a Python 3 script to
get routing table. @return: List of routes.

Response:
def getRoutes(self):
        """Get routing table.

        @return: List of routes.

        """
        routes = []
        try:
            out = subprocess.Popen([routeCmd, "-n"],
                                   stdout=subprocess.PIPE).communicate()[0]
        except:
            raise Exception('Execution of command %s failed.' % ipCmd)
        lines = out.splitlines()
        if len(lines) > 1:
            headers = [col.lower() for col in lines[1].split()]
            for line in lines[2:]:
                routes.append(dict(zip(headers, line.split())))
        return routes


Instruction:
Make a summary of the following Python 3 code
def execNetstatCmd(self, *args):
        out = util.exec_command([netstatCmd,] + list(args))
        return out.splitlines()

Response:
Execute netstat command with positional params args and return result as
        list of lines.


Instruction:
Here you have a function in Python 3, explain what it does
def parseNetstatCmd(self, tcp=True, udp=True, ipv4=True, ipv6=True,
                        include_listen=True, only_listen=False,
                        show_users=False, show_procs=False,
                        resolve_hosts=False, resolve_ports=False,
                        resolve_users=True):
        """Execute netstat command and return result as a nested dictionary.

        @param tcp:            Include TCP ports in ouput if True.
        @param udp:            Include UDP ports in ouput if True.
        @param ipv4:           Include IPv4 ports in output if True.
        @param ipv6:           Include IPv6 ports in output if True.
        @param include_listen: Include listening ports in output if True.
        @param only_listen:    Include only listening ports in output if True.
        @param show_users:     Show info on owning users for ports if True.
        @param show_procs:     Show info on PID and Program Name attached to
                               ports if True.
        @param resolve_hosts:  Resolve IP addresses into names if True.
        @param resolve_ports:  Resolve numeric ports to names if True.
        @param resolve_users:  Resolve numeric user IDs to user names if True.
        @return:               List of headers and list of rows and columns.

        """
        headers = ['proto', 'ipversion', 'recvq', 'sendq',
                   'localaddr', 'localport','foreignaddr', 'foreignport',
                   'state']
        args = []
        proto = []
        if ipv4:
            proto.append('inet')
        if ipv6:
            proto.append('inet6')
        if len(proto) > 0:
            args.append('-A')
            args.append(','.join(proto))
        if tcp:
            args.append('-t')
        if udp:
            args.append('-u')
        if only_listen:
            args.append('-l')
        elif include_listen:
            args.append('-a')
        regexp_str = ('(tcp|udp)(\d*)\s+(\d+)\s+(\d+)\s+'
                      '(\S+):(\w+)\s+(\S+):(\w+|\*)\s+(\w*)')
        if show_users:
            args.append('-e')
            regexp_str += '\s+(\w+)\s+(\d+)'
            headers.extend(['user', 'inode'])
        if show_procs:
            args.append('-p')
            regexp_str += '\s+(\S+)'
            headers.extend(['pid', 'prog'])
        if not resolve_hosts:
            args.append('--numeric-hosts')
        if not resolve_ports:
            args.append('--numeric-ports')
        if not resolve_users:
            args.append('--numeric-users')
        lines = self.execNetstatCmd(*args)
        stats = []
        regexp = re.compile(regexp_str)
        for line in lines[2:]:
            mobj = regexp.match(line)
            if mobj is not None:
                stat = list(mobj.groups())
                if stat[1] == '0':
                    stat[1] = '4'
                if stat[8] == '':
                    stat[8] = None
                if show_procs:
                    proc = stat.pop().split('/')
                    if len(proc) == 2:
                        stat.extend(proc)
                    else:
                        stat.extend([None, None])
                stats.append(stat)
        return {'headers': headers, 'stats': stats}

Response: Execute netstat command and return result as a nested dictionary.


Instruction:
Explain what the following Python 3 code does
def getStats(self, tcp=True, udp=True, ipv4=True, ipv6=True,
                 include_listen=True, only_listen=False,
                 show_users=False, show_procs=False,
                 resolve_hosts=False, resolve_ports=False, resolve_users=True,
                 **kwargs):
        """Execute netstat command and return result as a nested dictionary.

        @param tcp:            Include TCP ports in ouput if True.
        @param udp:            Include UDP ports in ouput if True.
        @param ipv4:           Include IPv4 ports in output if True.
        @param ipv6:           Include IPv6 ports in output if True.
        @param include_listen: Include listening ports in output if True.
        @param only_listen:    Include only listening ports in output if True.
        @param show_users:     Show info on owning users for ports if True.
        @param show_procs:     Show info on PID and Program Name attached to
                               ports if True.
        @param resolve_hosts:  Resolve IP addresses into names if True.
        @param resolve_ports:  Resolve numeric ports to names if True.
        @param resolve_users:  Resolve numeric user IDs to user 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:               List of headers and list of rows and columns.

        """
        pinfo = self.parseNetstatCmd(tcp, udp, ipv4, ipv6,
                                     include_listen, only_listen,
                                     show_users, show_procs,
                                     resolve_hosts, resolve_ports, resolve_users)
        if pinfo:
            if len(kwargs) > 0:
                pfilter = util.TableFilter()
                pfilter.registerFilters(**kwargs)
                stats = pfilter.applyFilters(pinfo['headers'], pinfo['stats'])
                return {'headers': pinfo['headers'], 'stats': stats}
            else:
                return pinfo
        else:
            return None

Response: Execute netstat command and return result as a nested dictionary.


Instruction:
Can you write a function in Python 3 where it
returns the number of TCP endpoints discriminated by status.

Response:
def getTCPportConnStatus(self, ipv4=True, ipv6=True, include_listen=False,
                             **kwargs):
        """Returns the number of TCP endpoints discriminated by status.

        @param ipv4:           Include IPv4 ports in output if True.
        @param ipv6:           Include IPv6 ports in output if True.
        @param include_listen: Include listening ports in output 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 connection status to the
                               number of endpoints.

        """
        status_dict = {}
        result = self.getStats(tcp=True, udp=False,
                               include_listen=include_listen,
                               ipv4=ipv4, ipv6=ipv6,
                               **kwargs)
        stats = result['stats']
        for stat in stats:
            if stat is not None:
                status = stat[8].lower()
            status_dict[status] = status_dict.get(status, 0) + 1
        return status_dict