Instruction:
Can you implement a function in Python 3 that
compiles a pvariable expression expr into a TensorFluent in the given scope with optional batch size.

Response:
def _compile_pvariable_expression(self,
                                      expr: Expression,
                                      scope: Dict[str, TensorFluent],
                                      batch_size: Optional[int] = None,
                                      noise: Optional[List[tf.Tensor]] = None) -> TensorFluent:
        '''Compile a pvariable expression `expr` into a TensorFluent
        in the given `scope` with optional batch size.

        Args:
            expr (:obj:`rddl2tf.expr.Expression`): A RDDL pvariable expression.
            scope (Dict[str, :obj:`rddl2tf.fluent.TensorFluent`]): A fluent scope.
            batch_size (Optional[size]): The batch size.

        Returns:
            :obj:`rddl2tf.fluent.TensorFluent`: The compiled expression as a TensorFluent.
        '''
        etype = expr.etype
        args = expr.args
        name = expr._pvar_to_name(args)
        if name not in scope:
            raise ValueError('Variable {} not in scope.'.format(name))
        fluent = scope[name]
        scope = args[1] if args[1] is not None else []
        if isinstance(fluent, TensorFluent):
            fluent = TensorFluent(fluent.tensor, scope, batch=fluent.batch)
        elif isinstance(fluent, tf.Tensor):
            fluent = TensorFluent(fluent, scope, batch=self.batch_mode)
        else:
            raise ValueError('Variable in scope must be TensorFluent-like: {}'.format(fluent))
        return fluent


Instruction:
Create a Python 3 function to
compile a random variable expression expr into a TensorFluent in the given scope with optional batch size and optional noise.

Response:
def _compile_random_variable_expression(self,
                                            expr: Expression,
                                            scope: Dict[str, TensorFluent],
                                            batch_size: Optional[int] = None,
                                            noise: Optional[List[tf.Tensor]] = None) -> TensorFluent:
        '''Compile a random variable expression `expr` into a TensorFluent
        in the given `scope` with optional batch size.

        If `reparam` tensor is given, then it conditionally stops gradient
        backpropagation at the batch level where `reparam` is False.

        Args:
            expr (:obj:`rddl2tf.expr.Expression`): A RDDL random variable expression.
            scope (Dict[str, :obj:`rddl2tf.fluent.TensorFluent`]): A fluent scope.
            batch_size (Optional[size]): The batch size.

        Returns:
            :obj:`rddl2tf.fluent.TensorFluent`: The compiled expression as a TensorFluent.
        '''
        etype = expr.etype
        args = expr.args

        if etype[1] == 'KronDelta':
            sample = self._compile_expression(args[0], scope, batch_size, noise)
        elif etype[1] == 'Bernoulli':
            mean = self._compile_expression(args[0], scope, batch_size, noise)
            dist, sample = TensorFluent.Bernoulli(mean, batch_size)
        elif etype[1] == 'Uniform':
            low = self._compile_expression(args[0], scope, batch_size, noise)
            high = self._compile_expression(args[1], scope, batch_size, noise)
            dist, sample = TensorFluent.Uniform(low, high, batch_size)
        elif etype[1] == 'Normal':
            if noise is None:
                mean = self._compile_expression(args[0], scope, batch_size, noise)
                variance = self._compile_expression(args[1], scope, batch_size, noise)
                dist, sample = TensorFluent.Normal(mean, variance, batch_size)
            else:
                xi = noise.pop()
                xi = TensorFluent(xi, scope=[], batch=True)
                mean = self._compile_expression(args[0], scope, batch_size, noise)
                variance = self._compile_expression(args[1], scope, batch_size, noise)
                sample = mean + TensorFluent.sqrt(variance) * xi
        elif etype[1] == 'Laplace':
            mean = self._compile_expression(args[0], scope, batch_size, noise)
            variance = self._compile_expression(args[1], scope, batch_size, noise)
            dist, sample = TensorFluent.Laplace(mean, variance, batch_size)
        elif etype[1] == 'Gamma':
            shape = self._compile_expression(args[0], scope, batch_size, noise)
            scale = self._compile_expression(args[1], scope, batch_size, noise)
            dist, sample = TensorFluent.Gamma(shape, scale, batch_size)
        elif etype[1] == 'Exponential':
            mean = self._compile_expression(args[0], scope, batch_size, noise)
            dist, sample = TensorFluent.Exponential(mean, batch_size)
        else:
            raise ValueError('Invalid random variable expression:\n{}.'.format(expr))

        return sample


Instruction:
Implement a function in Python 3 to
compile an arithmetic expression expr into a TensorFluent in the given scope with optional batch size.

Response:
def _compile_arithmetic_expression(self,
                                       expr: Expression,
                                       scope: Dict[str, TensorFluent],
                                       batch_size: Optional[int] = None,
                                       noise: Optional[List[tf.Tensor]] = None) -> TensorFluent:
        '''Compile an arithmetic expression `expr` into a TensorFluent
        in the given `scope` with optional batch size.

        Args:
            expr (:obj:`rddl2tf.expr.Expression`): A RDDL arithmetic expression.
            scope (Dict[str, :obj:`rddl2tf.fluent.TensorFluent`]): A fluent scope.
            batch_size (Optional[size]): The batch size.

        Returns:
            :obj:`rddl2tf.fluent.TensorFluent`: The compiled expression as a TensorFluent.
        '''
        etype = expr.etype
        args = expr.args

        if len(args) == 1:
            etype2op = {
                '+': lambda x: x,
                '-': lambda x: -x
            }

            if etype[1] not in etype2op:
                raise ValueError('Invalid binary arithmetic expression:\n{}'.format(expr))

            op = etype2op[etype[1]]
            x = self._compile_expression(args[0], scope, batch_size, noise)
            fluent = op(x)

        else:
            etype2op = {
                '+': lambda x, y: x + y,
                '-': lambda x, y: x - y,
                '*': lambda x, y: x * y,
                '/': lambda x, y: x / y,
            }

            if etype[1] not in etype2op:
                raise ValueError('Invalid binary arithmetic expression:\n{}'.format(expr))

            op = etype2op[etype[1]]
            x = self._compile_expression(args[0], scope, batch_size, noise)
            y = self._compile_expression(args[1], scope, batch_size, noise)
            fluent = op(x, y)

        return fluent


Instruction:
How would you implement a function in Python 3 that
compiles a RDDL unary function expression expr into a TensorFluent in the given scope with optional batch size.

Response:
def _compile_function_expression(self,
                                     expr: Expression,
                                     scope: Dict[str, TensorFluent],
                                     batch_size: Optional[int] = None,
                                     noise: Optional[List[tf.Tensor]] = None) -> TensorFluent:
        '''Compile a function expression `expr` into a TensorFluent
        in the given `scope` with optional batch size.

        Args:
            expr (:obj:`rddl2tf.expr.Expression`): A RDDL function expression.
            scope (Dict[str, :obj:`rddl2tf.fluent.TensorFluent`]): A fluent scope.
            batch_size (Optional[size]): The batch size.

        Returns:
            :obj:`rddl2tf.fluent.TensorFluent`: The compiled expression as a TensorFluent.
        '''
        etype = expr.etype
        args = expr.args

        if len(args) == 1:

            etype2func = {
                'abs':    TensorFluent.abs,
                'exp':    TensorFluent.exp,
                'log':    TensorFluent.log,
                'sqrt':   TensorFluent.sqrt,
                'cos':    TensorFluent.cos,
                'sin':    TensorFluent.sin,
                'tan':    TensorFluent.tan,
                'acos':   TensorFluent.acos,
                'arccos': TensorFluent.acos,
                'asin':   TensorFluent.asin,
                'arcsin': TensorFluent.asin,
                'atan':   TensorFluent.atan,
                'arctan': TensorFluent.atan,
                'round':  TensorFluent.round,
                'ceil':   TensorFluent.ceil,
                'floor':  TensorFluent.floor
            }

            if etype[1] not in etype2func:
                raise ValueError('Invalid unary function expression:\n{}'.format(expr))

            op = etype2func[etype[1]]
            x = self._compile_expression(args[0], scope, batch_size, noise)
            fluent = op(x)

        else:
            etype2func = {
                'pow': TensorFluent.pow,
                'max': TensorFluent.max,
                'min': TensorFluent.min
            }

            if etype[1] not in etype2func:
                raise ValueError('Invalid binary function expression:\n{}'.format(expr))

            op = etype2func[etype[1]]
            x = self._compile_expression(args[0], scope, batch_size, noise)
            y = self._compile_expression(args[1], scope, batch_size, noise)
            fluent = op(x, y)

        return fluent


Instruction:
How would you explain what the following Python 3 function does
def _compile_control_flow_expression(self,
                                         expr: Expression,
                                         scope: Dict[str, TensorFluent],
                                         batch_size: Optional[int] = None,
                                         noise: Optional[List[tf.Tensor]] = None) -> TensorFluent:
        '''Compile a control flow expression `expr` into a TensorFluent
        in the given `scope` with optional batch size.

        Args:
            expr (:obj:`rddl2tf.expr.Expression`): A RDDL control flow expression.
            scope (Dict[str, :obj:`rddl2tf.fluent.TensorFluent`]): A fluent scope.
            batch_size (Optional[size]): The batch size.

        Returns:
            :obj:`rddl2tf.fluent.TensorFluent`: The compiled expression as a TensorFluent.
        '''
        etype = expr.etype
        args = expr.args
        if etype[1] == 'if':
            condition = self._compile_expression(args[0], scope, batch_size, noise)
            true_case = self._compile_expression(args[1], scope, batch_size, noise)
            false_case = self._compile_expression(args[2], scope, batch_size, noise)
            fluent = TensorFluent.if_then_else(condition, true_case, false_case)
        else:
            raise ValueError('Invalid control flow expression:\n{}'.format(expr))
        return fluent

Response: Compile a control flow expression expr into a TensorFluent in the given scope with optional batch size.


Instruction:
How would you code a function in Python 3 to
compile an aggregation expression expr into a TensorFluent in the given scope with optional batch size.

Response:
def _compile_aggregation_expression(self,
                                        expr: Expression,
                                        scope: Dict[str, TensorFluent],
                                        batch_size: Optional[int] = None,
                                        noise: Optional[List[tf.Tensor]] = None) -> TensorFluent:
        '''Compile an aggregation expression `expr` into a TensorFluent
        in the given `scope` with optional batch size.

        Args:
            expr (:obj:`rddl2tf.expr.Expression`): A RDDL aggregation expression.
            scope (Dict[str, :obj:`rddl2tf.fluent.TensorFluent`]): A fluent scope.
            batch_size (Optional[size]): The batch size.

        Returns:
            :obj:`rddl2tf.fluent.TensorFluent`: The compiled expression as a TensorFluent.
        '''
        etype = expr.etype
        args = expr.args

        typed_var_list = args[:-1]
        vars_list = [var for _, (var, _) in typed_var_list]
        expr = args[-1]

        x = self._compile_expression(expr, scope)

        etype2aggr = {
            'sum':     x.sum,
            'prod':    x.prod,
            'avg':     x.avg,
            'maximum': x.maximum,
            'minimum': x.minimum,
            'exists':  x.exists,
            'forall':  x.forall
        }

        if etype[1] not in etype2aggr:
            raise ValueError('Invalid aggregation expression {}.'.format(expr))

        aggr = etype2aggr[etype[1]]
        fluent = aggr(vars_list=vars_list)

        return fluent


Instruction:
Can you write a function in Python 3 where it
returns a numpy array that contains the number of entries in table that satisfy the queries.

Response:
def mask(self, table):
        """
        Use the current Query object to count the number of entries in `table`
        that satisfy `queries`.

        Parameters
        ----------
        table : NumPy structured array, astropy Table, etc.

        Returns
        -------
        mask : numpy bool array
        """
        if self._operator is None:
            if self._operands is None:
                return np.ones(self._get_table_len(table), dtype=np.bool)
            else:
                return self._create_mask(table, self._operands)

        if self._operator == 'NOT':
            return ~self._operands.mask(table)

        if self._operator == 'AND':
            op_func = np.logical_and
        elif self._operator == 'OR':
            op_func = np.logical_or
        elif self._operator == 'XOR':
            op_func = np.logical_xor

        mask_this = self._operands[0].mask(table)
        for op in self._operands[1:]:
            mask_this = op_func(mask_this, op.mask(table), out=mask_this)

        return mask_this


Instruction:
Can you tell what is the following Python 3 function doing
def filter(self, table, column_slice=None):
        if self._operator is None and self._operands is None:
            return table if column_slice is None else self._get_table_column(table, column_slice)

        if self._operator == 'AND' and column_slice is None:
            for op in self._operands:
                table = op.filter(table)
            return table

        return self._mask_table(
            table if column_slice is None else self._get_table_column(table, column_slice),
            self.mask(table)
        )

Response: Returns a filtered version of the table.


Instruction:
Create a Python 3 function to
return the number of entries in table that satisfy the queries.

Response:
def count(self, table):
        """
        Use the current Query object to count the number of entries in `table`
        that satisfy `queries`.

        Parameters
        ----------
        table : NumPy structured array, astropy Table, etc.

        Returns
        -------
        count : int
        """
        if self._operator is None and self._operands is None:
            return self._get_table_len(table)

        return np.count_nonzero(self.mask(table))


Instruction:
How would you explain what the following Python 3 function does
def variable_names(self):
        if self._variable_names is None:

            if self._operator is None:
                if self._operands is None:
                    self._variable_names = tuple()
                else:
                    self._variable_names = self._get_variable_names(self._operands)

            elif self._operator == 'NOT':
                self._variable_names = self._operands.variable_names

            else:
                v = list()
                for op in self._operands:
                    v.extend(op.variable_names)
                self._variable_names = tuple(set(v))

        return self._variable_names

Response: Get all variable names required for this query.


Instruction:
Explain what the following Python 3 code does
def user(self):
        try:
            return self._user
        except AttributeError:
            self._user = MatrixUser(self.mxid, self.Api(identity=self.mxid))
            return self._user

Response: Creates a User object when requested.


Instruction:
Can you implement a function in Python 3 that
creates a Room object when requested.

Response:
def room(self):
        """Creates a Room object when requested."""
        try:
            return self._room
        except AttributeError:
            room_id = self.json["room_id"]
            self._room = MatrixRoom(room_id, self.Api)
            return self._room


Instruction:
Explain what the following Python 3 code does
def join(self, room_str):
        response = self.user_api.join_room(room_str)
        return self._mkroom(response["room_id"])

Response: Joins a room id or alias even if it must first be created.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def refresh_rooms(self):
        for room_id in self.user_api.get_joined_rooms()["joined_rooms"]:
            self._rooms[room_id] = MatrixRoom(room_id, self.user_api)

Response: Calls GET / joined_rooms to refresh rooms list.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def not_empty(_object):
    if is_callable(_object):
        _validator = _object

        @wraps(_validator)
        @instance_of()
        def decorated(value):
            ensure(value, "%s is empty" % safe_repr(value))
            return _validator(value)
        return decorated
    try:
        ensure(len(_object), "%s is empty" % safe_repr(_object))
    except TypeError:
        raise AssertionError("not of any valid types: [list, dict, str]")

Response: Validates the given input is not empty.


Instruction:
How would you explain what the following Python 3 function does
def set_walltime(self, walltime):
        if not isinstance(walltime, timedelta):
            raise TypeError(
                'walltime must be an instance of datetime.timedelta. %s given' %
                type(walltime)
            )

        self._options['walltime'] = walltime
        return self

Response: Set a walltime for the job


Instruction:
Can you generate the documentation for the following Python 3 function
def get_url(url, parser='html'):
    url = request.quote(url, safe=':/?=&')
    logger.debug('URL: %s', url)
    req = request.Request(url, headers={'User-Agent': 'foobar'})
    try:
        response = request.urlopen(req)
    except HTTPError:
        raise
    except (ssl.SSLError, URLError):
        # Some websites (like metal-archives) use older TLS versions and can
        # make the ssl module trow a VERSION_TOO_LOW error. Here we try to use
        # the older TLSv1 to see if we can fix that
        context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
        response = request.urlopen(req, context=context)

    response = response.read()
    if parser == 'html':
        return BeautifulSoup(response, 'html.parser', from_encoding='utf-8')
    elif parser == 'json':
        return json.loads(response)
    elif parser == 'raw':
        return response.decode()
    raise ValueError('Unrecognized parser')

Response:
Requests the specified url and returns a BeautifulSoup object with its
    contents.


Instruction:
Can you tell what is the following Python 3 function doing
def get_lastfm(method, lastfm_key='', **kwargs):
    if not lastfm_key:
        if 'lastfm_key' not in CONFIG or not CONFIG['lastfm_key']:
            logger.warning('No lastfm key configured')
            return ''
        else:
            lastfm_key = CONFIG['lastfm_key']

    url = 'http://ws.audioscrobbler.com/2.0/?method={}&api_key={}&format=json'
    url = url.format(method, lastfm_key)
    for key in kwargs:
        url += '&{}={}'.format(key, kwargs[key])

    response = get_url(url, parser='json')
    if 'error' in response:
        logger.error('Error number %d in lastfm query: %s',
                     response['error'], response['message'])
        return ''

    return response

Response: Request the specified method from the lastfm api.


Instruction:
Here you have a function in Python 3, explain what it does
def normalize(string, chars_to_remove=None, replacement=''):
    ret = string.translate(str.maketrans({
        'á': 'a',
        'ä': 'a',
        'æ': 'ae',
        'é': 'e',
        'í': 'i',
        'ó': 'o',
        'ö': 'o',
        'ú': 'u',
        'ü': 'u',
        'ñ': 'n',
    }))

    if isinstance(chars_to_remove, dict):
        for chars, replace in chars_to_remove.items():
            reg = '[' + re.escape(chars) + ']'
            ret = re.sub(reg, replace, ret)

    elif isinstance(chars_to_remove, str):
        reg = '[' + re.escape(chars_to_remove) + ']'
        ret = re.sub(reg, replacement, ret)

    return ret

Response: Normalizes a string according to the given characters.


Instruction:
Create a Python 3 function for
returning the lyrics found in metrolyrics for the specified mp3 file or an empty string if not found.

Response:
def metrolyrics(song):
    """
    Returns the lyrics found in metrolyrics for the specified mp3 file or an
    empty string if not found.
    """
    translate = {URLESCAPE: '', ' ': '-'}
    title = song.title.lower()
    title = normalize(title, translate)
    title = re.sub(r'\-{2,}', '-', title)
    artist = song.artist.lower()
    artist = normalize(artist, translate)
    artist = re.sub(r'\-{2,}', '-', artist)

    url = 'http://www.metrolyrics.com/{}-lyrics-{}.html'.format(title, artist)
    soup = get_url(url)
    body = soup.find(id='lyrics-body-text')
    if body is None:
        return ''

    text = ''
    verses = body.find_all('p')
    for verse in verses:
        text += verse.get_text().strip()
        text += '\n\n'

    return text.strip()


Instruction:
How would you code a function in Python 3 to
return the lyrics found in darklyrics for the specified mp3 file.

Response:
def darklyrics(song):
    """
    Returns the lyrics found in darklyrics for the specified mp3 file or an
    empty string if not found.
    """

    # Darklyrics relies on the album name
    if not hasattr(song, 'album') or not song.album:
        song.fetch_album_name()
        if not hasattr(song, 'album') or not song.album:
            # If we don't have the name of the album, there's nothing we can do
            # on darklyrics
            return ''

    artist = song.artist.lower()
    artist = normalize(artist, URLESCAPES, '')
    album = song.album.lower()
    album = normalize(album, URLESCAPES, '')
    title = song.title

    url = 'http://www.darklyrics.com/lyrics/{}/{}.html'.format(artist, album)
    soup = get_url(url)
    text = ''
    for header in soup.find_all('h3'):
        song = str(header.get_text())
        next_sibling = header.next_sibling
        if song.lower().find(title.lower()) != -1:
            while next_sibling is not None and\
                    (next_sibling.name is None or next_sibling.name != 'h3'):
                if next_sibling.name is None:
                    text += str(next_sibling)
                next_sibling = next_sibling.next_sibling

    return text.strip()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def azlyrics(song):
    artist = song.artist.lower()
    if artist[0:2] == 'a ':
        artist = artist[2:]
    artist = normalize(artist, URLESCAPES, '')
    title = song.title.lower()
    title = normalize(title, URLESCAPES, '')

    url = 'https://www.azlyrics.com/lyrics/{}/{}.html'.format(artist, title)
    soup = get_url(url)
    body = soup.find_all('div', class_='')[-1]
    return body.get_text().strip()

Response: Returns the lyrics found in azlyrics for the specified mp3 file.


Instruction:
How would you implement a function in Python 3 that
returns the lyrics found in genius. com for the specified mp3 file or an empty string if not found.

Response:
def genius(song):
    """
    Returns the lyrics found in genius.com for the specified mp3 file or an
    empty string if not found.
    """
    translate = {
        '@': 'at',
        '&': 'and',
        URLESCAPE: '',
        ' ': '-'
    }
    artist = song.artist.capitalize()
    artist = normalize(artist, translate)
    title = song.title.capitalize()
    title = normalize(title, translate)

    url = 'https://www.genius.com/{}-{}-lyrics'.format(artist, title)
    soup = get_url(url)
    for content in soup.find_all('p'):
        if content:
            text = content.get_text().strip()
            if text:
                return text

    return ''


Instruction:
How would you explain what the following Python 3 function does
def metalarchives(song):
    artist = normalize(song.artist)
    title = normalize(song.title)

    url = 'https://www.metal-archives.com/search/ajax-advanced/searching/songs'
    url += f'/?songTitle={title}&bandName={artist}&ExactBandMatch=1'
    soup = get_url(url, parser='json')
    if not soup:
        return ''

    song_id_re = re.compile(r'lyricsLink_([0-9]*)')
    ids = set(re.search(song_id_re, a) for sub in soup['aaData'] for a in sub)
    if not ids:
        return ''

    if None in ids:
        ids.remove(None)
    ids = map(lambda a: a.group(1), ids)
    for song_id in ids:
        url = 'https://www.metal-archives.com/release/ajax-view-lyrics/id/{}'
        lyrics = get_url(url.format(song_id), parser='html')
        lyrics = lyrics.get_text().strip()
        if not re.search('lyrics not available', lyrics):
            return lyrics

    return ''

Response: Returns the lyrics found in MetalArchives for the specified mp3 file or an empty string if not found.


Instruction:
Can you tell what is the following Python 3 function doing
def lyricswikia(song):
    artist = song.artist.title()
    artist = normalize(artist, ' ', '_')
    title = song.title
    title = normalize(title, ' ', '_')

    url = 'https://lyrics.wikia.com/wiki/{}:{}'.format(artist, title)
    soup = get_url(url)
    text = ''
    content = soup.find('div', class_='lyricbox')
    if not content:
        return ''

    for unformat in content.findChildren(['i', 'b']):
        unformat.unwrap()
    for remove in content.findChildren(['div', 'span']):
        remove.decompose()

    nlcount = 0
    for line in content.children:
        if line is None or line == '
' or line == '\n':
            if nlcount == 2:
                text += '\n\n'
                nlcount = 0
            else:
                nlcount += 1
        else:
            nlcount = 0
            text += str(line).replace('
', '\n')
    return text.strip()

Response: Returns the lyrics found in lyrics. wikia. com for the specified mp3 file or an empty string if not found.


Instruction:
How would you implement a function in Python 3 that
returns the lyrics found in musixmatch for the specified mp3 file or an empty string if not found.

Response:
def musixmatch(song):
    """
    Returns the lyrics found in musixmatch for the specified mp3 file or an
    empty string if not found.
    """
    escape = re.sub("'-¡¿", '', URLESCAPE)
    translate = {
        escape: '',
        ' ': '-'
    }
    artist = song.artist.title()
    artist = re.sub(r"( '|' )", '', artist)
    artist = re.sub(r"'", '-', artist)
    title = song.title
    title = re.sub(r"( '|' )", '', title)
    title = re.sub(r"'", '-', title)

    artist = normalize(artist, translate)
    artist = re.sub(r'\-{2,}', '-', artist)
    title = normalize(title, translate)
    title = re.sub(r'\-{2,}', '-', title)

    url = 'https://www.musixmatch.com/lyrics/{}/{}'.format(artist, title)
    soup = get_url(url)
    text = ''
    contents = soup.find_all('p', class_='mxm-lyrics__content')
    for p in contents:
        text += p.get_text().strip()
        if p != contents[-1]:
            text += '\n\n'

    return text.strip()


Instruction:
Explain what the following Python 3 code does
def songlyrics(song):
    translate = {
        URLESCAPE: '',
        ' ': '-'
    }
    artist = song.artist.lower()
    artist = normalize(artist, translate)
    title = song.title.lower()
    title = normalize(title, translate)

    artist = re.sub(r'\-{2,}', '-', artist)
    title = re.sub(r'\-{2,}', '-', title)

    url = 'http://www.songlyrics.com/{}/{}-lyrics'.format(artist, title)
    soup = get_url(url)
    text = soup.find(id='songLyricsDiv')
    if not text:
        return ''

    text = text.getText().strip()
    if not text or text.lower().startswith('we do not have the lyrics for'):
        return ''

    return text

Response: Returns the lyrics found in songlyrics. com for the specified mp3 file or an empty string if not found.


Instruction:
Create a Python 3 function for
returning the lyrics found in lyrics. com for the specified mp3 file or an empty string if not found.

Response:
def lyricscom(song):
    """
    Returns the lyrics found in lyrics.com for the specified mp3 file or an
    empty string if not found.
    """
    artist = song.artist.lower()
    artist = normalize(artist, ' ', '+')
    title = song.title

    url = 'https://www.lyrics.com/artist/{}'.format(artist)
    soup = get_url(url)
    location = ''
    for a in soup.select('tr a'):
        if a.string.lower() == title.lower():
            location = a['href']
            break
    if location == '':
        return ''

    url = 'https://www.lyrics.com' + location
    soup = get_url(url)
    body = soup.find(id='lyric-body-text')
    if not body:
        return ''

    return body.get_text().strip()


Instruction:
How would you explain what the following Python 3 function does
def vagalume(song):
    translate = {
        '@': 'a',
        URLESCAPE: '',
        ' ': '-'
    }
    artist = song.artist.lower()
    artist = normalize(artist, translate)
    artist = re.sub(r'\-{2,}', '-', artist)
    title = song.title.lower()
    title = normalize(title, translate)
    title = re.sub(r'\-{2,}', '-', title)

    url = 'https://www.vagalume.com.br/{}/{}.html'.format(artist, title)
    soup = get_url(url)
    body = soup.select('div#lyrics')
    if body == []:
        return ''

    content = body[0]
    for br in content.find_all('br'):
        br.replace_with('\n')

    return content.get_text().strip()

Response: Returns the lyrics found in vagalume. com. br for the specified mp3 file or an empty string if not found.


Instruction:
Make a summary of the following Python 3 code
def lyricsmode(song):
    translate = {
        URLESCAPE: '',
        ' ': '_'
    }
    artist = song.artist.lower()
    artist = normalize(artist, translate)
    title = song.title.lower()
    title = normalize(title, translate)

    artist = re.sub(r'\_{2,}', '_', artist)
    title = re.sub(r'\_{2,}', '_', title)

    if artist[0:4].lower() == 'the ':
        artist = artist[4:]

    if artist[0:2].lower() == 'a ':
        prefix = artist[2]
    else:
        prefix = artist[0]

    url = 'http://www.lyricsmode.com/lyrics/{}/{}/{}.html'
    url = url.format(prefix, artist, title)
    soup = get_url(url)
    content = soup.find(id='lyrics_text')

    return content.get_text().strip()

Response: Returns the lyrics found in lyricsmode. com for the specified mp3 file.


Instruction:
Implement a Python 3 function for
returning the lyrics found in letras. com for the specified mp3 file or an empty string if not found.

Response:
def letras(song):
    """
    Returns the lyrics found in letras.com for the specified mp3 file or an
    empty string if not found.
    """
    translate = {
        '&': 'a',
        URLESCAPE: '',
        ' ': '-'
    }
    artist = song.artist.lower()
    artist = normalize(artist, translate)
    title = song.title.lower()
    title = normalize(title, translate)

    url = 'https://www.letras.com/{}/{}/'.format(artist, title)
    soup = get_url(url)
    if not soup:
        return ''

    found_title = soup.select_one('div.cnt-head_title h1')
    if not found_title:
        # The site didn't find lyrics and took us to the homepage
        return ''

    found_title = found_title.get_text()
    found_title = re.sub(r'[\W_]+', '', found_title.lower())
    if found_title != re.sub(r'[\W_]+', '', song.title.lower()):
        # The site took us to the wrong song page
        return ''

    content = soup.find('article')
    if not content:
        return ''

    text = ''
    for br in content.find_all('br'):
        br.replace_with('\n')

    for p in content.find_all('p'):
        text += p.get_text() + '\n\n'

    return text.strip()


Instruction:
Create a Python 3 function for
returning the name of a website - scrapping function.

Response:
def id_source(source, full=False):
    """
    Returns the name of a website-scrapping function.
    """
    if source not in source_ids:
        return ''

    if full:
        return source_ids[source][1]
    else:
        return source_ids[source][0]


Instruction:
Implement a Python 3 function for
updating metrics for a single gitlab instance.

Response:
async def update_gitlab_loop(update_metrics, params):
    """
    app = Sanic()
    mserver = MetricsServer(app)
    mserver.add_task(update_gitlab_loop, params={'url': GITLAB_URL, 'token': token})
    """
    gitlab_api = gitlab.Gitlab(url=params['url'], private_token=params['token'], api_version=4)
    while True:
        try:
            metrics = update_gitlab_runners(gitlab_api)
            update_metrics(metrics)
        except Exception:
            update_metrics({})
            log.exception('During loop')
        await asyncio.sleep(LOOP)


Instruction:
Can you generate the documentation for the following Python 3 function
def set_probe_position(self, new_probe_position):
        if new_probe_position is not None:
            # convert the probe position to a FloatPoint and limit it to the 0.0 to 1.0 range in both axes.
            new_probe_position = Geometry.FloatPoint.make(new_probe_position)
            new_probe_position = Geometry.FloatPoint(y=max(min(new_probe_position.y, 1.0), 0.0),
                                                     x=max(min(new_probe_position.x, 1.0), 0.0))
        old_probe_position = self.__probe_position_value.value
        if ((old_probe_position is None) != (new_probe_position is None)) or (old_probe_position != new_probe_position):
            # this path is only taken if set_probe_position is not called as a result of the probe_position model
            # value changing.
            self.__probe_position_value.value = new_probe_position
        # update the probe position for listeners and also explicitly update for probe_graphic_connections.
        self.probe_state_changed_event.fire(self.probe_state, self.probe_position)

Response: Sets the probe position in normalized coordinates with origin at top left.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def apply_metadata_groups(self, properties: typing.Dict, metatdata_groups: typing.Tuple[typing.List[str], str]) -> None:
        pass

Response: Applies metadata groups to properties.


Instruction:
Can you tell what is the following Python 3 function doing
def create_analytic_backend(settings):
    backend = settings.get('backend')
    if isinstance(backend, basestring):
        backend = import_string(backend)
    elif backend:
        backend = backend
    else:
        raise KeyError('backend')

    return backend(settings.get("settings", {}))

Response: Creates a new Analytics backend from the settings


Instruction:
Here you have a function in Python 3, explain what it does
def initialize_state(self):
        if self.__scan_hardware_source:
            self.__profile_changed_event_listener = self.__scan_hardware_source.profile_changed_event.listen(self.__update_profile_index)
            self.__frame_parameters_changed_event_listener = self.__scan_hardware_source.frame_parameters_changed_event.listen(self.__update_frame_parameters)
            self.__data_item_states_changed_event_listener = self.__scan_hardware_source.data_item_states_changed_event.listen(self.__data_item_states_changed)
            self.__acquisition_state_changed_event_listener = self.__scan_hardware_source.acquisition_state_changed_event.listen(self.__acquisition_state_changed)
            self.__probe_state_changed_event_listener = self.__scan_hardware_source.probe_state_changed_event.listen(self.__probe_state_changed)
            self.__channel_state_changed_event_listener = self.__scan_hardware_source.channel_state_changed_event.listen(self.__channel_state_changed)
            subscan_state_model = self.__scan_hardware_source.subscan_state_model

            def subscan_state_changed(name):
                if callable(self.on_subscan_state_changed):
                    self.on_subscan_state_changed(subscan_state_model.value)

            self.__subscan_state_changed_listener = subscan_state_model.property_changed_event.listen(subscan_state_changed)
            subscan_state_changed("value")
        if self.on_display_name_changed:
            self.on_display_name_changed(self.display_name)
        if self.on_subscan_state_changed:
            self.on_subscan_state_changed(self.__scan_hardware_source.subscan_state)
        channel_count = self.__scan_hardware_source.channel_count
        if self.on_channel_count_changed:
            self.on_channel_count_changed(channel_count)
        self.__channel_enabled = [False] * channel_count
        for channel_index in range(channel_count):
            channel_id, name, enabled = self.__scan_hardware_source.get_channel_state(channel_index)
            self.__channel_state_changed(channel_index, channel_id, name, enabled)
            self.__channel_enabled[channel_index] = enabled
        self.__update_buttons()
        if self.on_profiles_changed:
            profile_items = list(ScanControlStateController.profiles.items())
            profile_items.sort(key=lambda k_v: k_v[1])
            profiles = map(lambda k_v: k_v[0], profile_items)
            self.on_profiles_changed(profiles)
            self.__update_profile_index(self.__scan_hardware_source.selected_profile_index)
        if self.on_linked_changed:
            self.on_linked_changed(self.__linked)
        if self.on_simulate_button_state_changed:
            use_simulator = self.__scan_hardware_source.use_hardware_simulator
            self.on_simulate_button_state_changed(use_simulator, use_simulator)
        if self.on_data_item_states_changed:
            self.on_data_item_states_changed(list())
        probe_state = self.__scan_hardware_source.probe_state
        probe_position = self.__scan_hardware_source.probe_position
        self.__probe_state_changed(probe_state, probe_position)

Response: Initialize the state of the UI.


Instruction:
Write a Python 3 script for
calling this when the user clicks the play or pause button.

Response:
def handle_play_pause_clicked(self):
        """ Call this when the user clicks the play/pause button. """
        if self.__scan_hardware_source:
            if self.is_playing:
                self.__scan_hardware_source.stop_playing()
            else:
                self.__scan_hardware_source.start_playing()


Instruction:
Can you generate the documentation for the following Python 3 function
def handle_record_clicked(self, callback_fn):
        assert callable(callback_fn)
        if self.__scan_hardware_source:

            def finish_record(data_and_metadata_list):
                record_index = self.__scan_hardware_source.record_index
                for data_and_metadata in data_and_metadata_list:
                    data_item = DataItem.DataItem()
                    data_item.ensure_data_source()

                    display_name = data_and_metadata.metadata.get("hardware_source", dict()).get("hardware_source_name")
                    display_name = display_name if display_name else _("Record")
                    channel_name = data_and_metadata.metadata.get("hardware_source", dict()).get("channel_name")
                    title_base = "{} ({})".format(display_name, channel_name) if channel_name else display_name
                    data_item.title = "{} {}".format(title_base, record_index)

                    data_item.set_xdata(data_and_metadata)
                    callback_fn(data_item)
                self.__scan_hardware_source.record_index += 1

            self.__scan_hardware_source.record_async(finish_record)

Response: Handle the record button clicked.


Instruction:
Can you generate the documentation for the following Python 3 function
def size_to_content(self, horizontal_padding=None, vertical_padding=None):

        if horizontal_padding is None:
            horizontal_padding = 0

        if vertical_padding is None:
            vertical_padding = 0

        self.sizing.set_fixed_size(Geometry.IntSize(18 + 2 * horizontal_padding, 18 + 2 * vertical_padding))

Response: Size the canvas item to the text content.


Instruction:
How would you explain what the following Python 3 function does
def round_controlled(cycled_iterable, rounds=1):
    round_start = None
    rounds_completed = 0

    for item in cycled_iterable:
        if round_start is None:
            round_start = item
        elif item == round_start:
            rounds_completed += 1

        if rounds_completed == rounds:
            return

        yield item

Response: Return after <rounds > passes through a cycled iterable.


Instruction:
Here you have a function in Python 3, explain what it does
def success_rate(self):
        if self.successes + self.fails == 0:
            success_rate = 0
        else:
            total_attempts = self.successes + self.fails
            success_rate = (self.successes * 100 / total_attempts)

        return success_rate

Response: Returns a float with the rate of success from all the logged results.


Instruction:
Here you have a function in Python 3, explain what it does
def add_result(self, source, found, runtime):
        self.source_stats[source.__name__].add_runtime(runtime)
        if found:
            self.source_stats[source.__name__].successes += 1
        else:
            self.source_stats[source.__name__].fails += 1

Response: Adds a new record to the statistics database.


Instruction:
Write a Python 3 script to
return the average time taken to scrape the lyrics from a source or the total average time taken to scrape the lyrics from that source.

Response:
def avg_time(self, source=None):
        """
        Returns the average time taken to scrape lyrics. If a string or a
        function is passed as source, return the average time taken to scrape
        lyrics from that source, otherwise return the total average.
        """
        if source is None:
            runtimes = []
            for rec in self.source_stats.values():
                runtimes.extend([r for r in rec.runtimes if r != 0])
            return avg(runtimes)
        else:
            if callable(source):
                return avg(self.source_stats[source.__name__].runtimes)
            else:
                return avg(self.source_stats[source].runtimes)


Instruction:
How would you implement a function in Python 3 that
calculates the overall counts of best worst fastest slowest total not found and total runtime.

Response:
def calculate(self):
        """
        Calculate the overall counts of best, worst, fastest, slowest, total
        found, total not found and total runtime

        Results are returned in a dictionary with the above parameters as keys.
        """
        best, worst, fastest, slowest = (), (), (), ()
        found = notfound = total_time = 0
        for source, rec in self.source_stats.items():
            if not best or rec.successes > best[1]:
                best = (source, rec.successes, rec.success_rate())
            if not worst or rec.successes < worst[1]:
                worst = (source, rec.successes, rec.success_rate())

            avg_time = self.avg_time(source)
            if not fastest or (avg_time != 0 and avg_time < fastest[1]):
                fastest = (source, avg_time)
            if not slowest or (avg_time != 0 and avg_time > slowest[1]):
                slowest = (source, avg_time)

            found += rec.successes
            notfound += rec.fails
            total_time += sum(rec.runtimes)

        return {
            'best': best,
            'worst': worst,
            'fastest': fastest,
            'slowest': slowest,
            'found': found,
            'notfound': notfound,
            'total_time': total_time
        }


Instruction:
Write a Python 3 script to
print a series of relevant stats about a full execution. This function is meant to be called at the end of the program.

Response:
def print_stats(self):
        """
        Print a series of relevant stats about a full execution. This function
        is meant to be called at the end of the program.
        """
        stats = self.calculate()
        total_time = '%d:%02d:%02d' % (stats['total_time'] / 3600,
                                       (stats['total_time'] / 3600) / 60,
                                       (stats['total_time'] % 3600) % 60)
        output = """\
Total runtime: {total_time}
    Lyrics found: {found}
    Lyrics not found:{notfound}
    Most useful source:\
{best} ({best_count} lyrics found) ({best_rate:.2f}% success rate)
    Least useful source:\
{worst} ({worst_count} lyrics found) ({worst_rate:.2f}% success rate)
    Fastest website to scrape: {fastest} (Avg: {fastest_time:.2f}s per search)
    Slowest website to scrape: {slowest} (Avg: {slowest_time:.2f}s per search)
    Average time per website: {avg_time:.2f}s

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxx    PER WEBSITE STATS:      xxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
"""
        output = output.format(total_time=total_time,
                               found=stats['found'],
                               notfound=stats['notfound'],
                               best=stats['best'][0].capitalize(),
                               best_count=stats['best'][1],
                               best_rate=stats['best'][2],
                               worst=stats['worst'][0].capitalize(),
                               worst_count=stats['worst'][1],
                               worst_rate=stats['worst'][2],
                               fastest=stats['fastest'][0].capitalize(),
                               fastest_time=stats['fastest'][1],
                               slowest=stats['slowest'][0].capitalize(),
                               slowest_time=stats['slowest'][1],
                               avg_time=self.avg_time())
        for source in sources:
            stat = str(self.source_stats[source.__name__])
            output += f'\n{source.__name__.upper()}\n{stat}\n'

        print(output)


Instruction:
How would you explain what the following Python 3 function does
def broadcast(cls, s1: ParamsList, s2: ParamsList) -> BroadcastTuple:
        '''It broadcasts the smaller scope over the larger scope.

        It handles scope intersection as well as differences in scopes
        in order to output a resulting scope so that input scopes are
        contained within it (i.e., input scopes are subscopes of the
        output scope). Also, if necessary, it outputs permutations of
        the input scopes so that tensor broadcasting invariants are
        not violated.

        Note:
            For more information on broadcasting, please report to
            NumPy's official documentation available at the following URLs:
            1. https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html
            2. https://docs.scipy.org/doc/numpy/reference/generated/numpy.broadcast.html

        Args:
            s1: A fluent's scope.
            s2: A fluent's scope.

        Returns:
            A tuple with the output scope and permutations of the input scopes.
        '''
        if len(s1) == 0:
            return s2, [], []
        if len(s2) == 0:
            return s1, [], []

        subscope = list(set(s1) & set(s2))
        if len(subscope) == len(s1):
            subscope = s1
        elif len(subscope) == len(s2):
            subscope = s2

        perm1 = []
        if s1[-len(subscope):] != subscope:
            i = 0
            for var in s1:
                if var not in subscope:
                    perm1.append(i)
                    i += 1
                else:
                    j = subscope.index(var)
                    perm1.append(len(s1) - len(subscope) + j)
        perm2 = []
        if s2[-len(subscope):] != subscope:
            i = 0
            for var in s2:
                if var not in subscope:
                    perm2.append(i)
                    i += 1
                else:
                    j = subscope.index(var)
                    perm2.append(len(s2) - len(subscope) + j)

        scope = [] # type: ParamsList
        if len(s1) >= len(s2):
            if perm1 == []:
                scope = s1
            else:
                for i in range(len(s1)):
                    scope.append(s1[perm1.index(i)])
        else:
            if perm2 == []:
                scope = s2
            else:
                for i in range(len(s2)):
                    scope.append(s2[perm2.index(i)])

        return (scope, perm1, perm2)

Response:
This function broadcasts the smaller scope over the larger scope. It broadcasts the smaller scope over the larger scope. It handles scope intersection as well as differences in scopes
        and outputs a resulting scope so that input scopes are subscopes of the resulting scope.


Instruction:
Here you have a function in Python 3, explain what it does
def get_baseparser_extended_df(sample, bp_lines, ref, alt):
    columns = "chrom\tpos\tref\tcov\tA\tC\tG\tT\t*\t-\t+".split()
    if bp_lines is None:
        return None

    # change baseparser output to get most common maf per indel
    bpdf = pd.DataFrame([[sample] + l.rstrip('\n').split("\t") for l in bp_lines if len(l) > 0],
                        columns=["sample"] + columns, dtype=np.object)
    bpdf[bpdf == ""] = None

    # remove zero coverage rows
    bpdf = bpdf[bpdf["cov"].astype(int) > 0]

    if len(bpdf) == 0:
        return None

    if ref and alt:
        # add columns for validation allele
        bpdf = pd.concat([bpdf, pd.DataFrame({"val_ref": pd.Series(ref), "val_alt": pd.Series(alt)})], axis=1)
        bpdf = pd.concat([bpdf, bpdf.apply(_val_al, axis=1)], axis=1)

    bpdf = pd.concat([bpdf, bpdf.apply(_most_common_indel, axis=1)], axis=1)
    bpdf = pd.concat([bpdf, bpdf.apply(_most_common_al, axis=1)], axis=1)
    bpdf["most_common_count"] = bpdf.apply(lambda x: max([x.most_common_al_count, x.most_common_indel_count]), axis=1)
    bpdf["most_common_maf"] = bpdf.apply(lambda x: max([x.most_common_al_maf, x.most_common_indel_maf]), axis=1)

    return bpdf

Response: Turn baseParser results into a dataframe


Instruction:
Create a Python 3 function to
filter out mutations that are in normal.

Response:
def filter_out_mutations_in_normal(tumordf, normaldf, most_common_maf_min=0.2,
                                   most_common_count_maf_threshold=20,
                                   most_common_count_min=1):
    """Remove mutations that are in normal"""
    df = tumordf.merge(normaldf, on=["chrom", "pos"], suffixes=("_T", "_N"))

    # filters
    common_al = (df.most_common_al_count_T == df.most_common_count_T) & (df.most_common_al_T == df.most_common_al_N)
    common_indel = (df.most_common_indel_count_T == df.most_common_count_T) & \
        (df.most_common_indel_T == df.imost_common_indel_N)
    normal_criteria = ((df.most_common_count_N >= most_common_count_maf_threshold) &
                       (df.most_common_maf_N > most_common_maf_min)) | \
        ((df.most_common_count_N < most_common_count_maf_threshold) &
         (df.most_common_count_N > most_common_count_min))
    df = df[~(common_al | common_indel) & normal_criteria]

    # restore column names of tumor
    for c in df.columns:
        if c.endswith("_N"):
            del df[c]
            df.columns = [c[:-2] if c.endswith("_T") else c for c in df.columns]

    return df


Instruction:
Can you create a Python 3 function that
selects only mutations that revert the given mutations in a single event.

Response:
def select_only_revertant_mutations(bpdf, snv=None, ins=None, dlt=None):
    """
    Selects only mutations that revert the given mutations in a single event.
    """
    if sum([bool(snv), bool(ins), bool(dlt)]) != 1:
        raise(Exception("Should be either snv, ins or del".format(snv)))

    if snv:
        if snv not in ["A", "C", "G", "T"]:
            raise(Exception("snv {} should be A, C, G or T".format(snv)))
        return bpdf[(bpdf.most_common_al == snv) & (bpdf.most_common_al_count == bpdf.most_common_count)]
    elif bool(ins):
        return \
            bpdf[((bpdf.most_common_indel.apply(lambda x: len(x) + len(ins) % 3 if x else None) == 0) &
                  (bpdf.most_common_indel_type == "+") & (bpdf.most_common_count == bpdf.most_common_indel_count)) |
                 ((bpdf.most_common_indel.apply(lambda x: len(ins) - len(x) % 3 if x else None) == 0) &
                  (bpdf.most_common_indel_type == "-") & (bpdf.most_common_count == bpdf.most_common_indel_count))]
    elif bool(dlt):
        return \
            bpdf[((bpdf.most_common_indel.apply(lambda x: len(x) - len(dlt) % 3 if x else None) == 0) &
                  (bpdf.most_common_indel_type == "+") & (bpdf.most_common_count == bpdf.most_common_indel_count)) |
                 ((bpdf.most_common_indel.apply(lambda x: -len(dlt) - len(x) % 3 if x else None) == 0) &
                  (bpdf.most_common_indel_type == "-") & (bpdf.most_common_count == bpdf.most_common_indel_count))]
    else:
        # should never happen
        raise(Exception("No mutation given?"))


Instruction:
Implement a function in Python 3 to
validate mutations in vcf file.

Response:
def validate_mutations(vcffile, bams, reffa, chr_reffa, samples, output_format, outfile,
                       mpileup_parameters=mpileup_parser.MPILEUP_DEFAULT_PARAMS):
    """Check if mutations in vcf are in bam"""
    output_header = "sample chrom pos ref cov A C G T * - + " \
        "val_ref val_alt val_al_type val_al_count val_maf "\
        "most_common_indel most_common_indel_count most_common_indel_maf most_common_indel_type most_common_al " \
        "most_common_al_count most_common_al_maf most_common_count most_common_maf".split()

    # for backwards compatibility
    # if bam or samples is a string, convert to list instead
    if isinstance(samples, six.string_types):
        samples = [samples]
    if isinstance(bams, six.string_types):
        bams = [bams]

    if output_format == 'vcf':
        vcf_reader = vcf.Reader(open(vcffile))
        vcf_reader.samples = samples
        vcf_reader.formats['GT'] = vcf.parser._Format(id='GT', num=1, type='String', desc="Genotype")
        vcf_reader.formats['AD'] = vcf.parser._Format(id='AD', num='R', type='Integer', desc="Allelic depth")
        vcf_reader.formats['DP'] = vcf.parser._Format(id='DP', num=1, type='Integer', desc="Depth")
        vcf_writer = vcf.Writer(outfile, vcf_reader)
    else:
        vcf_reader = open(vcffile)

    if output_format == "sufam":
        outfile.write("\t".join(output_header))
        outfile.write("\n")
    for record in vcf_reader:
        if output_format != 'vcf':
            line = record
            if line.startswith("#CHROM"):
                header = line[1:].rstrip('\n').split("\t")
                # create spoof pyvcf record if vcf_reader is not used
                _Record = namedtuple('Record', header)
            if line.startswith("#"):
                continue
            if len(header) == 0:
                raise(Exception("No header found in vcf file #CHROM not found"))
            # zip all column values, except alt (needs to be list in pyvcf)
            record_args = dict(zip(header, line.rstrip('\n').split("\t")))
            record_args['ALT'] = [record_args['ALT']]
            record = _Record(**record_args)

        # determine type of mutation
        record_type = "snv"
        if len(record.ALT) > 1:
            warnings.warn("Multiple ALT in one record is not implemented - using first")
        if len(record.REF) > len(record.ALT[0]):
            record_type = "deletion"
        elif len(record.ALT[0]) > len(record.REF):
            record_type = "insertion"

        # no coverage results
        no_cov = pd.Series({
            "chrom": str(record.CHROM), "pos": str(record.POS),
            "ref": str(record.REF),
            "cov": 0, "A": 0, "C": 0, "G": 0, "T": 0,
            "val_ref": str(record.REF), "val_alt": str(record.ALT[0]),
            "val_al_type": record_type, "val_al_count": 0, "val_maf": 0})

        # collect mpileup baseparser results per bam
        bps = []
        for i, bam in enumerate(bams):
            sample = samples[i]
            no_cov['sample'] = sample
            bp_lines = mpileup_parser.run_and_parse(bam, str(record.CHROM), str(record.POS), str(record.POS), reffa,
                                                    chr_reffa, mpileup_parameters)
            bpdf = get_baseparser_extended_df(sample, bp_lines, str(record.REF), str(record.ALT[0]))
            if bpdf is None:
                bp = no_cov
            else:
                bp = bpdf.ix[0, :]
            bps += [bp]

        # output call
        if output_format == "vcf":
            _write_bp_vcf(outfile, bps, vcf_writer, record)
        else:
            # only one bam file supported for outputs other than vcf
            _write_bp(outfile, bps[0], output_header, output_format)


Instruction:
Can you generate the documentation for the following Python 3 function
def _prep_clients(self, clients):
        for pool_id, client in enumerate(clients):
            # Tag it with an id we'll use to identify it in the pool
            if hasattr(client, "pool_id"):
                raise ValueError("%r is already part of a pool.", client)
            setattr(client, "pool_id", pool_id)
            # Wrap all public functions
            self._wrap_functions(client)
        return clients

Response: Prepare a list of clients by tagging it with and id and wrapping methods.


Instruction:
Write a Python 3 function that can
wrap public functions to catch ConnectionError and TimeoutError.

Response:
def _wrap_functions(self, client):
        """Wrap public functions to catch ConnectionError.

        When an error happens, it puts the client in the penalty box
        so that it won't be retried again for a little while.
        """

        def wrap(fn):
            def wrapper(*args, **kwargs):
                """Simple wrapper for to catch dead clients."""
                try:
                    return fn(*args, **kwargs)
                except (ConnectionError, TimeoutError):  # TO THE PENALTY BOX!
                    self._penalize_client(client)
                    raise

            return functools.update_wrapper(wrapper, fn)

        for name in dir(client):
            if name.startswith("_"):
                continue
            # Some things aren't wrapped
            if name in ("echo", "execute_command", "parse_response"):
                continue
            obj = getattr(client, name)
            if not callable(obj):
                continue
            log.debug("Wrapping %s", name)
            setattr(client, name, wrap(obj))


Instruction:
Can you write a function in Python 3 where it
restores clients that have reconnected.

Response:
def _prune_penalty_box(self):
        """Restores clients that have reconnected.

        This function should be called first for every public method.
        """
        added = False
        for client in self.penalty_box.get():
            log.info("Client %r is back up.", client)
            self.active_clients.append(client)
            added = True
        if added:
            self._sort_clients()


Instruction:
Make a summary of the following Python 3 code
def get_client(self, shard_key):
        self._prune_penalty_box()

        if len(self.active_clients) == 0:
            raise ClusterEmptyError("All clients are down.")

        # So that hashing is consistent when a node is down, check against
        # the initial client list. Only use the active client list when
        # the desired node is down.
        # N.B.: I know this is not technically "consistent hashing" as
        #       academically defined. It's a hack so that keys which need to
        #       go elsewhere do, while the rest stay on the same instance.
        if not isinstance(shard_key, bytes):
            shard_key = shard_key.encode("utf-8")
        hashed = mmh3.hash(shard_key)
        pos = hashed % len(self.initial_clients)
        if self.initial_clients[pos] in self.active_clients:
            return self.initial_clients[pos]
        else:
            pos = hashed % len(self.active_clients)
            return self.active_clients[pos]

Response: Get the client for a given shard.


Instruction:
Write a Python 3 function that can
place client in the penalty box.

Response:
def _penalize_client(self, client):
        """Place client in the penalty box.

        :param client: Client object
        """
        if client in self.active_clients:  # hasn't been removed yet
            log.warning("%r marked down.", client)
            self.active_clients.remove(client)
            self.penalty_box.add(client)
        else:
            log.info("%r not in active client list.")


Instruction:
Write a Python 3 script for
getting the zrevrangebyscore across the cluster.

Response:
def zrevrange_with_int_score(self, key, max_score, min_score):
        """Get the zrevrangebyscore across the cluster.
        Highest score for duplicate element is returned.
        A faster method should be written if scores are not needed.
        """
        self._prune_penalty_box()

        if len(self.active_clients) == 0:
            raise ClusterEmptyError("All clients are down.")

        element__score = defaultdict(int)
        for client in self.active_clients:
            revrange = client.zrevrangebyscore(
                key, max_score, min_score, withscores=True, score_cast_func=int
            )

            for element, count in revrange:
                element__score[element] = max(element__score[element], int(count))

        return element__score


Instruction:
Implement a Python 3 function for
taking files from React side creates SolveBio Object containing signed S3 URL.

Response:
def generate_s3_url(files):
    """Takes files from React side, creates
    SolveBio Object containing signed S3 URL."""
    if files:
        vault = g.client.Vault.get_personal_vault()
        files = json.loads(files)
        objects = []
        for i in xrange(len(files)):
            obj = g.client.Object.create(
                vault_id=vault.id,
                object_type='file',
                filename=files[i].get('filename'),
                mimetype=files[i].get('mimetype'),
                size=files[i].get('size')
            )
            objects.append({
                'id': obj.id,
                'filename': obj.filename,
                'upload_url': obj.upload_url
            })
        return json.dumps(objects)


Instruction:
Write a Python 3 script to
handle downstream processes using metadata about the uploaded files from React side.

Response:
def handle_uploaded_files(uploaded_files):
    """Handles downstream processes using metadata
    about the uploaded files from React side."""
    if uploaded_files:
        uploaded_files = json.loads(uploaded_files)[0]
        _id = uploaded_files.get('id')
        # Strip extension from filename
        _filename = os.path.splitext(uploaded_files.get('filename'))[0]

        # Create a dataset
        dataset = g.client.Dataset.get_or_create_by_full_path('~/' + _filename)

        # Import the file into the dataset
        g.client.DatasetImport.create(
            dataset_id=dataset.id,
            object_id=_id
        )

        # Wait until activity is completed
        dataset.activity(follow=True)

        SELECTED_COLS = ['col_a', 'col_b', 'col_c']
        query = dataset.query(fields=SELECTED_COLS)

        return html.Div(
            dt.DataTable(
                id='data-table',
                rows=list(query),
                columns=SELECTED_COLS
            )
        )


Instruction:
Create a Python 3 function to
create a custom camera panel.

Response:
def create_camera_panel(document_controller, panel_id, properties):
    """Create a custom camera panel.

    The camera panel type is specified in the 'camera_panel_type' key in the properties dict.

    The camera panel type must match a the 'camera_panel_type' of a camera panel factory in the Registry.

    The matching camera panel factory must return a ui_handler for the panel which is used to produce the UI.
    """
    camera_panel_type = properties.get("camera_panel_type")
    for component in Registry.get_components_by_type("camera_panel"):
        if component.camera_panel_type == camera_panel_type:
            hardware_source_id = properties["hardware_source_id"]
            hardware_source = HardwareSource.HardwareSourceManager().get_hardware_source_for_hardware_source_id(hardware_source_id)
            camera_device = getattr(hardware_source, "camera", None)
            camera_settings = getattr(hardware_source, "camera_settings", None)
            ui_handler = component.get_ui_handler(api_broker=PlugInManager.APIBroker(), event_loop=document_controller.event_loop, hardware_source_id=hardware_source_id, camera_device=camera_device, camera_settings=camera_settings)
            panel = Panel.Panel(document_controller, panel_id, properties)
            panel.widget = Declarative.DeclarativeWidget(document_controller.ui, document_controller.event_loop, ui_handler)
            return panel
    return None


Instruction:
Create a Python 3 function for
initializing the state of the class.

Response:
def initialize_state(self):
        """ Call this to initialize the state of the UI after everything has been connected. """
        if self.__hardware_source:
            self.__profile_changed_event_listener = self.__hardware_source.profile_changed_event.listen(self.__update_profile_index)
            self.__frame_parameters_changed_event_listener = self.__hardware_source.frame_parameters_changed_event.listen(self.__update_frame_parameters)
            self.__data_item_states_changed_event_listener = self.__hardware_source.data_item_states_changed_event.listen(self.__data_item_states_changed)
            self.__acquisition_state_changed_event_listener = self.__hardware_source.acquisition_state_changed_event.listen(self.__acquisition_state_changed)
            self.__log_messages_event_listener = self.__hardware_source.log_messages_event.listen(self.__log_messages)
        if self.on_display_name_changed:
            self.on_display_name_changed(self.display_name)
        if self.on_binning_values_changed:
            self.on_binning_values_changed(self.__hardware_source.binning_values)
        if self.on_monitor_button_state_changed:
            has_monitor = self.__hardware_source and self.__hardware_source.features.get("has_monitor", False)
            self.on_monitor_button_state_changed(has_monitor, has_monitor)
        self.__update_buttons()
        if self.on_profiles_changed:
            profile_items = self.__hardware_source.modes
            self.on_profiles_changed(profile_items)
            self.__update_profile_index(self.__hardware_source.selected_profile_index)
        if self.on_data_item_states_changed:
            self.on_data_item_states_changed(list())


Instruction:
How would you code a function in Python 3 to
call this when the user clicks the play or pause button.

Response:
def handle_play_pause_clicked(self):
        """ Call this when the user clicks the play/pause button. """
        if self.__hardware_source:
            if self.is_playing:
                self.__hardware_source.stop_playing()
            else:
                self.__hardware_source.start_playing()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False):
        c = self.copy()
        c.is_summary = summarize
        c.for_save = for_save
        final_points = []
        for i, p in enumerate(self.params):
            try:
                float(p)
            except:
                _, source, _, join_list, last = query.setup_joins(
                        six.text_type(p).split('__'), query.model._meta, query.get_initial_alias())[:5]
                target, alias, _ = query.trim_joins(source, join_list, last)
                final_points.append("%s.%s" % (alias, target[0].get_attname_column()[1]))
            else:
                final_points.append(six.text_type(p))
            c.params = final_points
        return c

Response: Setup any data here this method will be called before final SQL is generated


Instruction:
Can you tell what is the following Python 3 function doing
def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False):
        c = self.copy()
        c.is_summary = summarize
        c.for_save = for_save
        for pos, expression in enumerate(self.expressions):
            c.expressions[pos] = expression.resolve_expression(query, allow_joins, reuse, summarize)
        return c

Response: Prepare SQL from inner funcions.


Instruction:
Write a Python 3 script for
filtering rows inside a circunference of radius distance distance

Response:
def in_distance(self, distance, fields, points, annotate='_ed_distance'):
        """Filter rows inside a circunference of radius distance `distance`

            :param distance: max distance to allow
            :param fields: `tuple` with the fields to filter (latitude, longitude)
            :param points: center of the circunference (latitude, longitude)
            :param annotate: name where the distance will be annotated

        """
        clone = self._clone()
        return clone.annotate(
            **{annotate: EarthDistance([
                LlToEarth(fields), LlToEarth(points)])
            }).filter(**{'{0}__lte'.format(annotate): distance})


Instruction:
How would you implement a function in Python 3 that
gets a value for the specified configuration key.

Response:
def get(self, path, default=_NoDefault, as_type=None, resolve_references=True):
        """
        Gets a value for the specified path.

        :param path: the configuration key to fetch a value for, steps
            separated by the separator supplied to the constructor (default
            ``.``)
        :param default: a value to return if no value is found for the
            supplied path (``None`` is allowed)
        :param as_type: an optional callable to apply to the value found for
            the supplied path (possibly raising exceptions of its own if the
            value can not be coerced to the expected type)
        :param resolve_references: whether to resolve references in values
        :return: the value associated with the supplied configuration key, if
            available, or a supplied default value if the key was not found
        :raises ConfigurationError: when no value was found for *path* and
            *default* was not provided or a reference could not be resolved
        """
        value = self._source
        steps_taken = []
        try:
            # walk through the values dictionary
            for step in path.split(self._separator):
                steps_taken.append(step)
                value = value[step]

            if as_type:
                return as_type(value)
            elif isinstance(value, Mapping):
                # create an instance of our current type, copying 'configured' properties / policies
                namespace = type(self)(separator=self._separator, missing=self._missing)
                namespace._source = value
                # carry the root object from namespace to namespace, references are always resolved from root
                namespace._root = self._root
                return namespace
            elif resolve_references and isinstance(value, str):
                # only resolve references in str-type values (the only way they can be expressed)
                return self._resolve(value)
            else:
                return value
        except ConfiguredReferenceError:
            # also a KeyError, but this one should bubble to caller
            raise
        except KeyError as e:
            if default is not _NoDefault:
                return default
            else:
                missing_key = self._separator.join(steps_taken)
                raise NotConfiguredError('no configuration for key {}'.format(missing_key), key=missing_key) from e


Instruction:
Explain what the following Python 3 code does
def record_async(self, callback_fn):
        assert callable(callback_fn)

        def record_thread():
            current_frame_time = self.get_current_frame_time()

            def handle_finished(xdatas):
                callback_fn(xdatas)

            self.start_recording(current_frame_time, finished_callback_fn=handle_finished)

        self.__thread = threading.Thread(target=record_thread)
        self.__thread.start()

Response: Call this when the user clicks the record button.


Instruction:
How would you explain what the following Python 3 function does
def get_buffer_data(self, start: int, count: int) -> typing.Optional[typing.List[typing.List[typing.Dict]]]:
        if hasattr(self.__device, "get_buffer_data"):
            buffer_data = self.__device.get_buffer_data(start, count)

            enabled_channel_states = list()
            for channel_index in range(self.channel_count):
                channel_state = self.get_channel_state(channel_index)
                if channel_state.enabled:
                    enabled_channel_states.append(channel_state)

            scan_id = uuid.uuid4()

            for data_element_group in buffer_data:
                for channel_index, (data_element, channel_state) in enumerate(zip(data_element_group, enabled_channel_states)):
                    channel_name = channel_state.name
                    channel_id = channel_state.channel_id
                    if self.subscan_enabled:
                        channel_id += "_subscan"
                    properties = data_element["properties"]
                    update_autostem_properties(data_element, self.__stem_controller)
                    update_calibration_metadata(data_element, None, data_element["data"].shape, scan_id, None, channel_name, channel_id, properties, None, 0)
                    data_element["properties"]["channel_index"] = channel_index
                    data_element["properties"]["hardware_source_name"] = self.display_name
                    data_element["properties"]["hardware_source_id"] = self.hardware_source_id

            return buffer_data

        return None

Response: Get the recently acquired buffer data.


Instruction:
Explain what the following Python 3 code does
def get_plain_logname(base_name, root_dir, enable_json):
    if enable_json:
        nested_dir = os.path.join(root_dir, 'plain')
        if os.path.exists(root_dir) and not os.path.exists(nested_dir):
            os.mkdir(nested_dir)
        root_dir = nested_dir
    return os.path.join(root_dir, '{}.log'.format(base_name))

Response: Get the log name for a plain log file.


Instruction:
Given the following Python 3 function, write the documentation
def setup_logger(
    base_name,
    root_dir=None,
    enable_json=True,
    json_formatter='tipsi_tools.tipsi_logging.JSFormatter',
    loggers={},
):
    """
    json_formatter:
    'fan.contrib.django.span_formatter.SpanFormatter' - add INSTALLATION_ID, SPAN and etc
    """
    if not root_dir:
        root_dir = os.environ.get('LOG_DIR')
    assert root_dir, 'You should pass root_dir parameter or set env LOG_DIR'

    JSON_FORMATTER = {
        '()': json_formatter,
        'env_vars': ['HOST_TYPE', 'TIPSI_CONFIG', 'TIPSI_BRANCH', 'CONTAINER_TYPE'],
    }

    default_loggers = {
        '': {'handlers': ['default'], 'level': 'DEBUG', 'propagate': True},
        'googleapicliet.discovery_cache': {'level': 'ERROR'},
        'boto3': {'level': 'INFO'},
        'botocore': {'level': 'INFO'},
        'kazoo': {'level': 'INFO'},
        'urllib3': {'level': 'INFO'},
    }

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'json': JSON_FORMATTER,
            'standard': {'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'},
        },
        'handlers': {'default': base_handler(get_plain_logname(base_name, root_dir, enable_json))},
        'loggers': {**default_loggers, **loggers},
    }
    if enable_json:
        LOGGING['handlers']['json'] = base_handler(
            os.path.join(root_dir, '{}.json_log'.format(base_name)), formatter='json'
        )
        LOGGING['loggers']['']['handlers'].append('json')
    logging.config.dictConfig(LOGGING)

Response: Setup the logger for the given base_name root_dir and loggers.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def run(command):
    '''
    Run command in shell, accepts command construction from list
    Return (return_code, stdout, stderr)
    stdout and stderr - as list of strings
    '''
    if isinstance(command, list):
        command = ' '.join(command)
    out = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return (out.returncode, _prepare(out.stdout), _prepare(out.stderr))

Response:
Run command in shell accepts command construction from list
    Return return code stdout and stderr - as list of strings


Instruction:
Can you generate a brief explanation for the following Python 3 code
def succ(cmd, check_stderr=True, stdout=None, stderr=None):
    '''
    Alias to run with check return code and stderr
    '''
    code, out, err = run(cmd)

    # Because we're raising error, sometimes we want to process stdout/stderr after catching error
    # so we're copying these outputs if required
    if stdout is not None:
        stdout[:] = out
    if stderr is not None:
        stderr[:] = err

    if code != 0:
        for l in out:
            print(l)
    assert code == 0, 'Return: {} {}\nStderr: {}'.format(code, cmd, err)
    if check_stderr:
        assert err == [], 'Error: {} {}'.format(err, code)
    return code, out, err

Response: Run a command and return the output and error.


Instruction:
Given the following Python 3 function, write the documentation
def wait_socket(host, port, timeout=120):
    '''
    Wait for socket opened on remote side. Return False after timeout
    '''
    return wait_result(lambda: check_socket(host, port), True, timeout)

Response: Wait for socket opened on remote side. Return False after timeout.


Instruction:
Can you generate the documentation for the following Python 3 function
def interpolate_sysenv(line, defaults={}):
    '''
    Format line system environment variables + defaults
    '''
    map = ChainMap(os.environ, defaults)
    return line.format(**map)

Response: Interpolate line system environment variables + defaults


Instruction:
Can you generate a brief explanation for the following Python 3 code
def source(fname):
    '''
    Acts similar to bash 'source' or '.' commands.
    '''
    rex = re.compile('(?:export |declare -x )?(.*?)="(.*?)"')
    out = call_out('source {} && export'.format(fname))
    out = [x for x in out if 'export' in x or 'declare' in x]
    out = {k: v for k, v in [rex.match(x).groups() for x in out if rex.match(x)]}
    for k, v in out.items():
        os.environ[k] = v

Response:
Acts similar to bash source or. commands.
    Acts similar to bash source or. commands.
    Acts similar to bash source or. commands.


Instruction:
Can you generate the documentation for the following Python 3 function
def cd(dir_name):
    old_path = os.path.abspath('.')
    os.chdir(dir_name)
    try:
        yield
        os.chdir(old_path)
    except Exception:
        os.chdir(old_path)
        raise

Response: cd - yield the current directory and return back after block ended


Instruction:
Can you create a Python 3 function that
returns True if identifier has not been seen before.

Response:
def _is_new(self, identifier):
        """Returns True if identifier hasn't been seen before."""
        if identifier in self.tracker:
            return False
        else:
            self.tracker.append(identifier)
            self.tracker.pop(0)
            return True


Instruction:
Can you create a Python 3 function that
calls when a GET request is sent to the room

Response:
def on_get(self, request, response, room_alias=None):
        """Called when a GET request is sent to /rooms/{room_alias}"""
        response.body = "{}"
        if self.handler(room_alias):
            response.status = falcon.HTTP_200
            self.api.create_room(alias=room_alias)
        else:
            response.status = falcon.HTTP_404


Instruction:
Write a Python 3 script to
respond to PUT request containing events.

Response:
def on_put(self, request, response, txn_id=None):
        """Responds to PUT request containing events."""
        response.body = "{}"

        # Check whether repeat txn_id
        if not self._is_new(txn_id):
            response.status = falcon.HTTP_200
            return

        request.context["body"] = request.stream.read()
        try:
            events = json.loads(request.context["body"].decode("utf-8"))["events"]
        except(KeyError, ValueError, UnicodeDecodeError):
            response.status = falcon.HTTP_400
            response.body = "Malformed request body"
            return

        if self.handler(EventStream(events, self.Api)):
            response.status = falcon.HTTP_200
        else:
            response.status = falcon.HTTP_400


Instruction:
Can you implement a function in Python 3 that
responds to GET request for users.

Response:
def on_get(self, request, response, user_id=None):
        """Responds to GET request for users."""
        response.body = "{}"
        if self.handler(user_id):
            response.status = falcon.HTTP_200
            self.api.register(utils.mxid2localpart(user_id))
        else:
            response.status = falcon.HTTP_404


Instruction:
Create a Python 3 function for
setting the frame parameters with the settings index and fire the frame parameters changed event.

Response:
def set_frame_parameters(self, profile_index: int, frame_parameters) -> None:
        """Set the frame parameters with the settings index and fire the frame parameters changed event.

        If the settings index matches the current settings index, call set current frame parameters.

        If the settings index matches the record settings index, call set record frame parameters.
        """
        self.frame_parameters_changed_event.fire(profile_index, frame_parameters)


Instruction:
How would you explain what the following Python 3 function does
def verlet(dfun, xzero, vzero, timerange, timestep):
    '''Verlet method integration. This function wraps the Verlet class.

        :param dfun:
            second derivative function of the system.
            The differential system arranged as a series of second-order
            equations: \ddot{X} = dfun(t, x)
        :param xzero:
            the initial condition of the system
        :param vzero:
            the initial condition of first derivative of the system
        :param timerange:
            the start and end times as (starttime, endtime)
        :param timestep:
            the timestep
        :returns: t, x, v:
            as lists.
    '''
    return zip(*list(Verlet(dfun, xzero, vzero, timerange, timestep)))

Response: This function is used to wrap the Verlet class in order to generate a list of verlets for a given differential system.


Instruction:
How would you explain what the following Python 3 function does
def add_request(self, request):

        # Duplicate request name?
        if request.name in self.requests:
            raise ValueError('redefinition of request "{0}"'.format(request.name))
        self.requests[request.name] = request

        # Add the request URLs
        for method, url in request.urls:

            # URL with arguments?
            if RE_URL_ARG.search(url):
                request_regex = '^' + RE_URL_ARG_ESC.sub(r'/(?P<\1>[^/]+)', re.escape(url)) + '$'
                self.__request_regex.append((method, re.compile(request_regex), request))
            else:
                request_key = (method, url)
                if request_key in self.__request_urls:
                    raise ValueError('redefinition of request URL "{0}"'.format(url))
                self.__request_urls[request_key] = request

Response: Add a request object to the internal list of requests and URLs.


Instruction:
How would you explain what the following Python 3 function does
def add_header(self, key, value):

        assert isinstance(key, str), 'header key must be of type str'
        assert isinstance(value, str), 'header value must be of type str'
        self.headers[key] = value

Response: Add a response header to the internal dictionary.


Instruction:
Here you have a function in Python 3, explain what it does
def response(self, status, content_type, content, headers=None):

        assert not isinstance(content, (str, bytes)), 'response content cannot be of type str or bytes'
        response_headers = [('Content-Type', content_type)]
        if headers:
            response_headers.extend(headers)
        self.start_response(status, response_headers)
        return content

Response: Send an HTTP response to the application.


Instruction:
How would you code a function in Python 3 to
send a plain - text response.

Response:
def response_text(self, status, text=None, content_type='text/plain', encoding='utf-8', headers=None):
        """
        Send a plain-text response
        """

        if text is None:
            if isinstance(status, str):
                text = status
            else:
                text = status.phrase
        return self.response(status, content_type, [text.encode(encoding)], headers=headers)


Instruction:
Create a Python 3 function for
sending a JSON response

Response:
def response_json(self, status, response, content_type='application/json', encoding='utf-8', headers=None, jsonp=None):
        """
        Send a JSON response
        """

        encoder = JSONEncoder(
            check_circular=self.app.validate_output,
            allow_nan=False,
            sort_keys=True,
            indent=2 if self.app.pretty_output else None,
            separators=(',', ': ') if self.app.pretty_output else (',', ':')
        )
        content = encoder.encode(response)
        if jsonp:
            content_list = [jsonp.encode(encoding), b'(', content.encode(encoding), b');']
        else:
            content_list = [content.encode(encoding)]
        return self.response(status, content_type, content_list, headers=headers)


Instruction:
Implement a function in Python 3 to
reconstruct the URL using the algorithm provided by PEP3333_get_url_from_environ.

Response:
def reconstruct_url(self, path_info=None, query_string=None, relative=False):
        """
        Reconstructs the request URL using the algorithm provided by PEP3333
        """

        environ = self.environ
        if relative:
            url = ''
        else:
            url = environ['wsgi.url_scheme'] + '://'

            if environ.get('HTTP_HOST'):
                url += environ['HTTP_HOST']
            else:
                url += environ['SERVER_NAME']

                if environ['wsgi.url_scheme'] == 'https':
                    if environ['SERVER_PORT'] != '443':
                        url += ':' + environ['SERVER_PORT']
                else:
                    if environ['SERVER_PORT'] != '80':
                        url += ':' + environ['SERVER_PORT']

        url += quote(environ.get('SCRIPT_NAME', ''))
        if path_info is None:
            url += quote(environ.get('PATH_INFO', ''))
        else:
            url += path_info
        if query_string is None:
            if environ.get('QUERY_STRING'):
                url += '?' + environ['QUERY_STRING']
        else:
            if query_string:
                if isinstance(query_string, str):
                    url += '?' + query_string
                else:
                    url += '?' + encode_query_string(query_string)

        return url


Instruction:
Implement a function in Python 3 to
read from files found in XDG - specified system - wide configuration directories.

Response:
def read_xdg_config_dirs(name, extension):
    """
    Read from files found in XDG-specified system-wide configuration paths,
    defaulting to ``/etc/xdg``. Depends on ``XDG_CONFIG_DIRS`` environment
    variable.

    :param name: application or configuration set name
    :param extension: file extension to look for
    :return: a `.Configuration` instance with values read from XDG-specified
        directories
    """
    # find optional value of ${XDG_CONFIG_DIRS}
    config_dirs = environ.get('XDG_CONFIG_DIRS')
    if config_dirs:
        # PATH-like env vars operate in decreasing precedence, reverse this path set to mimic the end result
        config_dirs = reversed(config_dirs.split(path.pathsep))
    else:
        # XDG spec: "If $XDG_CONFIG_DIRS is either not set or empty, a value equal to /etc/xdg should be used."
        config_dirs = ['/etc/xdg']

    # load a file from all config dirs, default to NotConfigured
    fname = '{name}.{extension}'.format(name=name, extension=extension)
    return loadf(*(path.join(config_dir, fname) for config_dir in config_dirs),
                 default=NotConfigured)


Instruction:
Write a Python 3 script to
read from file found in XDG - specified configuration home directory.

Response:
def read_xdg_config_home(name, extension):
    """
    Read from file found in XDG-specified configuration home directory,
    expanding to ``${HOME}/.config/name.extension`` by default. Depends on
    ``XDG_CONFIG_HOME`` or ``HOME`` environment variables.

    :param name: application or configuration set name
    :param extension: file extension to look for
    :return: a `.Configuration` instance, possibly `.NotConfigured`
    """
    # find optional value of ${XDG_CONFIG_HOME}
    config_home = environ.get('XDG_CONFIG_HOME')
    if not config_home:
        # XDG spec: "If $XDG_CONFIG_HOME is either not set or empty, a default equal to $HOME/.config should be used."
        # see https://specifications.freedesktop.org/basedir-spec/latest/ar01s03.html
        config_home = path.expanduser('~/.config')

    # expand to full path to configuration file in XDG config path
    return loadf(path.join(config_home, '{name}.{extension}'.format(name=name, extension=extension)),
                 default=NotConfigured)


Instruction:
Implement a function in Python 3 to
read environment variables starting with NAME_ and returns a Configuration instance.

Response:
def read_envvars(name, extension):
    """
    Read environment variables starting with ``NAME_``, where subsequent
    underscores are interpreted as namespaces. Underscores can be retained as
    namespaces by doubling them up, e.g. ``NAME_SPA__CE_KEY`` would be
    accessible in the resulting `.Configuration` as
    ``c.spa_ce.key``, where ``c`` is the `.Configuration` instance.

    .. note::

        Environment variables are always `str`s, this function makes no effort
        to changes this. All values read from command line variables will be
        `str` instances.

    :param name: environment variable prefix to look for (without the ``_``)
    :param extension: *(unused)*
    :return: a `.Configuration` instance, possibly `.NotConfigured`
    """
    prefix = '{}_'.format(name)
    prefix_len = len(prefix)
    envvar_file = '{}_config_file'.format(name)
    # create a new mapping from environment values starting with the prefix (but stripped of that prefix)
    values = {var.lower()[prefix_len:]: value
              for var, value in environ.items()
              # TODO: document ignoring envvar_file
              if var.lower().startswith(prefix) and var.lower() != envvar_file}
    # TODO: envvar values can only be str, how do we configure non-str values?
    if not values:
        return NotConfigured

    def dotted(name):
        # replace 'regular' underscores (those between alphanumeric characters) with dots first
        name = re.sub(r'([0-9A-Za-z])_([0-9A-Za-z])', r'\1.\2', name)
        # unescape double underscores back to a single one
        return re.sub(r'__', '_', name)

    return Configuration({dotted(name): value for name, value in values.items()})


Instruction:
How would you explain what the following Python 3 function does
def read_envvar_file(name, extension):
    envvar_file = environ.get('{}_config_file'.format(name).upper())
    if envvar_file:
        # envvar set, load value as file
        return loadf(envvar_file)
    else:
        # envvar not set, return an empty source
        return NotConfigured

Response:
Read values from a file provided as a environment variable
   .


Instruction:
How would you explain what the following Python 3 function does
def read_envvar_dir(envvar, name, extension):
    config_dir = environ.get(envvar)
    if not config_dir:
        return NotConfigured

    # envvar is set, construct full file path, expanding user to allow the envvar containing a value like ~/config
    config_path = path.join(path.expanduser(config_dir), '{name}.{extension}'.format(name=name, extension=extension))
    return loadf(config_path, default=NotConfigured)

Response: Read values from a file located in a directory specified by an environment variable.


Instruction:
Implement a Python 3 function for
generating a generator that yields the available locality loaders for the specified locality.

Response:
def loaders(*specifiers):
    """
    Generates loaders in the specified order.

    Arguments can be `.Locality` instances, producing the loader(s) available
    for that locality, `str` instances (used as file path templates) or
    `callable`s. These can be mixed:

    .. code-block:: python

        # define a load order using predefined user-local locations,
        # an explicit path, a template and a user-defined function
        load_order = loaders(Locality.user,
                             '/etc/defaults/hard-coded.yaml',
                             '/path/to/{name}.{extension}',
                             my_loader)

        # load configuration for name 'my-application' using the load order
        # defined above
        config = load_name('my-application', load_order=load_order)

    :param specifiers:
    :return: a `generator` of configuration loaders in the specified order
    """
    for specifier in specifiers:
        if isinstance(specifier, Locality):
            # localities can carry multiple loaders, flatten this
            yield from _LOADERS[specifier]
        else:
            # something not a locality, pass along verbatim
            yield specifier


Instruction:
Can you write a function in Python 3 where it
reads a. Configuration instance from file - like objects.

Response:
def load(*fps, missing=Missing.silent):
    """
    Read a `.Configuration` instance from file-like objects.

    :param fps: file-like objects (supporting ``.read()``)
    :param missing: policy to be used when a configured key is missing, either
        as a `.Missing` instance or a default value
    :return: a `.Configuration` instance providing values from *fps*
    :rtype: `.Configuration`
    """
    return Configuration(*(yaml.safe_load(fp.read()) for fp in fps), missing=missing)


Instruction:
Write a Python 3 function for
reading a. Configuration instance from named files.

Response:
def loadf(*fnames, default=_NoDefault, missing=Missing.silent):
    """
    Read a `.Configuration` instance from named files.

    :param fnames: name of the files to ``open()``
    :param default: `dict` or `.Configuration` to use when a file does not
        exist (default is to raise a `FileNotFoundError`)
    :param missing: policy to be used when a configured key is missing, either
        as a `.Missing` instance or a default value
    :return: a `.Configuration` instance providing values from *fnames*
    :rtype: `.Configuration`
    """
    def readf(fname):
        if default is _NoDefault or path.exists(fname):
            # (attempt to) open fname if it exists OR if we're expected to raise an error on a missing file
            with open(fname, 'r') as fp:
                # default to empty dict, yaml.safe_load will return None for an empty document
                return yaml.safe_load(fp.read()) or {}
        else:
            return default

    return Configuration(*(readf(path.expanduser(fname)) for fname in fnames), missing=missing)


Instruction:
Can you implement a function in Python 3 that
reads a. Configuration instance from a list of strings.

Response:
def loads(*strings, missing=Missing.silent):
    """
    Read a `.Configuration` instance from strings.

    :param strings: configuration contents
    :param missing: policy to be used when a configured key is missing, either
        as a `.Missing` instance or a default value
    :return: a `.Configuration` instance providing values from *strings*
    :rtype: `.Configuration`
    """
    return Configuration(*(yaml.safe_load(string) for string in strings), missing=missing)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def load_name(*names, load_order=DEFAULT_LOAD_ORDER, extension='yaml', missing=Missing.silent):
    def generate_sources():
        # argument order for product matters, for names "foo" and "bar":
        # /etc/foo.yaml before /etc/bar.yaml, but both of them before ~/.foo.yaml and ~/.bar.yaml
        for source, name in product(load_order, names):
            if callable(source):
                yield source(name, extension)
            else:
                # expand user to turn ~/.name.yaml into /home/user/.name.yaml
                candidate = path.expanduser(source.format(name=name, extension=extension))
                yield loadf(candidate, default=NotConfigured)

    return Configuration(*generate_sources(), missing=missing)

Response: Read a. Configuration instance by name.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def acquire(self, block=True):
        while True:
            # Try to set the lock
            if self.redis.set(self.name, self.value, px=self.timeout, nx=True):
                # It's ours until the timeout now
                return True
            # Lock is taken
            if not block:
                return False
            # If blocking, try again in a bit
            time.sleep(self.sleep)

Response: Acquire lock. Blocks until acquired if block is True otherwise returns False.