Instruction:
Here you have a function in Python 3, explain what it does
def verify_editor(self):
        powerful_editors = [
            'josm', 'level0', 'merkaartor', 'qgis', 'arcgis', 'upload.py',
            'osmapi', 'Services_OpenStreetMap'
            ]
        if self.editor is not None:
            for editor in powerful_editors:
                if editor in self.editor.lower():
                    self.powerfull_editor = True
                    break

            if 'iD' in self.editor:
                trusted_hosts = [
                    'www.openstreetmap.org/id',
                    'www.openstreetmap.org/edit',
                    'improveosm.org',
                    'strava.github.io/iD',
                    'preview.ideditor.com/release',
                    'preview.ideditor.com/master',
                    'hey.mapbox.com/iD-internal',
                    'projets.pavie.info/id-indoor',
                    'maps.mapcat.com/edit',
                    'id.softek.ir'
                    ]
                if self.host.split('://')[-1].strip('/') not in trusted_hosts:
                    self.label_suspicious('Unknown iD instance')
        else:
            self.powerfull_editor = True
            self.label_suspicious('Software editor was not declared')

Response: Verify if the software used in the changeset is a powerfull_editor.


Instruction:
Can you generate the documentation for the following Python 3 function
def count(self):
        xml = get_changeset(self.id)
        actions = [action.tag for action in xml.getchildren()]
        self.create = actions.count('create')
        self.modify = actions.count('modify')
        self.delete = actions.count('delete')
        self.verify_editor()

        try:
            if (self.create / len(actions) > self.percentage and
                    self.create > self.create_threshold and
                    (self.powerfull_editor or self.create > self.top_threshold)):
                self.label_suspicious('possible import')
            elif (self.modify / len(actions) > self.percentage and
                    self.modify > self.modify_threshold):
                self.label_suspicious('mass modification')
            elif ((self.delete / len(actions) > self.percentage and
                    self.delete > self.delete_threshold) or
                    self.delete > self.top_threshold):
                self.label_suspicious('mass deletion')
        except ZeroDivisionError:
            print('It seems this changeset was redacted')

Response:
Count the number of elements created modified and deleted by the
 archive.


Instruction:
Implement a Python 3 function for
unwrapping a stream from a playlist URI.

Response:
def _unwrap_stream(uri, timeout, scanner, requests_session):
    """
    Get a stream URI from a playlist URI, ``uri``.
    Unwraps nested playlists until something that's not a playlist is found or
    the ``timeout`` is reached.
    """

    original_uri = uri
    seen_uris = set()
    deadline = time.time() + timeout

    while time.time() < deadline:
        if uri in seen_uris:
            logger.info(
                'Unwrapping stream from URI (%s) failed: '
                'playlist referenced itself', uri)
            return None
        else:
            seen_uris.add(uri)

        logger.debug('Unwrapping stream from URI: %s', uri)

        try:
            scan_timeout = deadline - time.time()
            if scan_timeout < 0:
                logger.info(
                    'Unwrapping stream from URI (%s) failed: '
                    'timed out in %sms', uri, timeout)
                return None
            scan_result = scanner.scan(uri, timeout=scan_timeout)
        except exceptions.ScannerError as exc:
            logger.debug('GStreamer failed scanning URI (%s): %s', uri, exc)
            scan_result = None

        if scan_result is not None and not (
                scan_result.mime.startswith('text/') or
                scan_result.mime.startswith('application/')):
            logger.debug(
                'Unwrapped potential %s stream: %s', scan_result.mime, uri)
            return uri

        download_timeout = deadline - time.time()
        if download_timeout < 0:
            logger.info(
                'Unwrapping stream from URI (%s) failed: timed out in %sms',
                uri, timeout)
            return None
        content = http.download(
            requests_session, uri, timeout=download_timeout)

        if content is None:
            logger.info(
                'Unwrapping stream from URI (%s) failed: '
                'error downloading URI %s', original_uri, uri)
            return None

        uris = playlists.parse(content)
        if not uris:
            logger.debug(
                'Failed parsing URI (%s) as playlist; found potential stream.',
                uri)
            return uri

        # TODO Test streams and return first that seems to be playable
        logger.debug(
            'Parsed playlist (%s) and found new URI: %s', uri, uris[0])
        uri = uris[0]


Instruction:
Given the following Python 3 function, write the documentation
def serve(self, sock, request_handler, error_handler, debug=False,
              request_timeout=60, ssl=None, request_max_size=None,
              reuse_port=False, loop=None, protocol=HttpProtocol,
              backlog=100, **kwargs):
        """Start asynchronous HTTP Server on an individual process.

        :param request_handler: Sanic request handler with middleware
        :param error_handler: Sanic error handler with middleware
        :param debug: enables debug output (slows server)
        :param request_timeout: time in seconds
        :param ssl: SSLContext
        :param sock: Socket for the server to accept connections from
        :param request_max_size: size in bytes, `None` for no limit
        :param reuse_port: `True` for multiple workers
        :param loop: asyncio compatible event loop
        :param protocol: subclass of asyncio protocol class
        :return: Nothing
        """
        if debug:
            loop.set_debug(debug)

        server = partial(
            protocol,
            loop=loop,
            connections=self.connections,
            signal=self.signal,
            request_handler=request_handler,
            error_handler=error_handler,
            request_timeout=request_timeout,
            request_max_size=request_max_size,
        )

        server_coroutine = loop.create_server(
            server,
            host=None,
            port=None,
            ssl=ssl,
            reuse_port=reuse_port,
            sock=sock,
            backlog=backlog
        )
        # Instead of pulling time at the end of every request,
        # pull it once per minute
        loop.call_soon(partial(update_current_time, loop))
        return server_coroutine

Response: Start asynchronous HTTP Server on an individual process.


Instruction:
How would you explain what the following Python 3 function does
def spawn(self, generations):
        egg_donors = [god for god in self.gods.values() if god.chromosomes == 'XX']
        sperm_donors = [god for god in self.gods.values() if god.chromosomes == 'XY']

        for i in range(generations):
            print("\nGENERATION %d\n" % (i+1))
            gen_xx = []
            gen_xy = []

            for egg_donor in egg_donors:
                sperm_donor = random.choice(sperm_donors)
                brood = self.breed(egg_donor, sperm_donor)

                for child in brood:
                    if child.divinity > human:
                        # divine offspring join the Pantheon
                        self.add_god(child)
                    if child.chromosomes == 'XX':
                        gen_xx.append(child)
                    else:
                        gen_xy.append(child)

            # elder gods leave the breeding pool
            egg_donors = [ed for ed in egg_donors if ed.generation > (i-2)]
            sperm_donors = [sd for sd in sperm_donors if sd.generation > (i-3)]

            # mature offspring join the breeding pool
            egg_donors += gen_xx
            sperm_donors += gen_xy

Response: Grow this Pantheon by multiplying Gods.


Instruction:
Write a Python 3 function that can
return words from tokens that are most closely related to word.

Response:
def get_matches(word, tokens, limit, offset=0):
    """Return words from <tokens> that are most closely related to <word>."""
    return closest(tokens, word_vec(word), limit, offset)


Instruction:
Given the following Python 3 function, write the documentation
def cosine(vec1, vec2):
    if norm(vec1) > 0 and norm(vec2) > 0:
        return dot(vec1, vec2) / (norm(vec1) * norm(vec2))
    else:
        return 0.0

Response: Compare vectors. Borrowed from A. Parish.


Instruction:
Can you create a Python 3 function that
returns the limit words from tokens whose vectors most closely resemble the search_vec. Skip the first limit results.

Response:
def closest(tokens, search_vec, limit, offset=0):
    """Return the <limit> words from <tokens> whose vectors most closely
    resemble the search_vec. Skip the first <offset> results.
    """
    return sorted(tokens,
                  key=lambda x: cosine(search_vec, word_vec(x)),
                  reverse=True)[offset:offset+limit]


Instruction:
Write a Python 3 script to
generate a json file for each txt file in the / data directory.

Response:
def tokenize_texts():
    """Generate a json file for each txt file in the /data/corpora directory."""
    text_files = [fname for fname in os.listdir(corpora_dir) \
        if fname.split('.')[1] == 'txt']

    for text_fname in text_files:
        json_fname = text_fname.split('.')[0] + '.json'
        if os.path.isfile(corpora_dir + json_fname):
            continue # already tokenized

        print("Tokenizing " + text_fname)
        text = open(corpora_dir + text_fname).read()
        words = nltk.word_tokenize(text)
        with open(corpora_dir + json_fname, 'w') as outjson:
            json.dump(words, outjson)


Instruction:
Implement a Python 3 function for
creating a new directory named dir_. Create a new file within it with sources. json.

Response:
def make_tokens_dir(dir_, sources):
    """Create a new directory named <dir_>. Create a new file within it called
    sources.json. The input <sources> is a list of names of tokenized texts.
    Write <sources> into sources.json.
    """
    os.mkdir(tokens_dir + dir_)
    for source in sources:
        if not os.path.isfile(corpora_dir + source):
            print('Invalid source: ' + source)
            return

    with open(tokens_dir + dir_ + '/sources.json', 'w') as outjson:
        json.dump(sources, outjson)


Instruction:
Here you have a function in Python 3, explain what it does
def make_tokens_list(dir_, filters):
    with open(tokens_dir + dir_ + '/sources.json', 'r') as injson:
        data = json.load(injson)
        sources = [corpora_dir + fname for fname in data]

    with open('data/skipwords.txt', 'r') as f:
        skipwords = [line.rstrip() for line in f]

    tokens_list = []
    for fname in sources:
        print("Incorporating tokens from " + fname)
        with open(fname, 'r') as injson:
            data = json.load(injson)
            words = [w.lower() for w in data if not w == '']
            filtered = [w for w,p in nltk.pos_tag(words) if p in filters]
            sanitized = [w for w in filtered if not w in skipwords]
            tokens_list += sanitized

    tokens_list = list(set(tokens_list)) # unique
    target = tokens_dir + dir_ + '/' + '-'.join(filters) + '.json'
    with open(target, 'w') as outjson:
        json.dump(tokens_list, outjson)

Response: Find sources. json in dir_. It contains a list of tokenized texts. For each tokenized text listed in sources. json read its tokens and filter them and add them to the list.


Instruction:
Given the following Python 3 function, write the documentation
def set_name_lists(ethnicity=None):
    if not ethnicity: ethnicity = random.choice(get_ethnicities())
    print("Loading names from: " + ethnicity)
    filename = names_dir + ethnicity + '.json'

    try:
        with open(filename, 'r') as injson:
            data = json.load(injson)
    except:
        return 'Unable to read from file: ' + filename
    else:
        names = [ tuple(name.split(',')) for name in data ]
        random.shuffle(names)

        global female_names
        female_names = [name for name,gender,*desc in names if gender == 'girl']

        global male_names
        male_names = [name for name,gender,*desc in names if gender == 'boy']

        global nb_names
        nb_names = [name for name,gender,*desc in names if gender == 'boygirl']

Response: Set three globally available lists of names.


Instruction:
Make a summary of the following Python 3 code
def set_chromosomes(self, chromosomes=None):
        if chromosomes and chromosomes in valid_chromosomes:
            self.chromosomes = chromosomes
        else:
            self.chromosomes = random.choice([XX, XY])

Response: This method sets the chromosomes of the log entry.


Instruction:
Explain what the following Python 3 code does
def set_inherited_traits(self, egg_donor, sperm_donor):
        if type(egg_donor) == str:
            self.reproduce_asexually(egg_donor, sperm_donor)
        else:
            self.reproduce_sexually(egg_donor, sperm_donor)

Response: Accept either strings or Gods as inputs.


Instruction:
Write a Python 3 function that can
produce two gametes an egg and a sperm from the input strings. Combine them to produce a genome a la sexual reproduction.

Response:
def reproduce_asexually(self, egg_word, sperm_word):
        """Produce two gametes, an egg and a sperm, from the input strings.
        Combine them to produce a genome a la sexual reproduction.
        """
        egg = self.generate_gamete(egg_word)
        sperm = self.generate_gamete(sperm_word)

        self.genome = list(set(egg + sperm)) # Eliminate duplicates
        self.generation = 1
        self.divinity = god


Instruction:
Given the following Python 3 function, write the documentation
def reproduce_sexually(self, egg_donor, sperm_donor):
        egg_word = random.choice(egg_donor.genome)
        egg = self.generate_gamete(egg_word)
        sperm_word = random.choice(sperm_donor.genome)
        sperm = self.generate_gamete(sperm_word)

        self.genome = list(set(egg + sperm)) # Eliminate duplicates
        self.parents = [egg_donor.name, sperm_donor.name]
        self.generation = max(egg_donor.generation, sperm_donor.generation) + 1
        sum_ = egg_donor.divinity + sperm_donor.divinity
        self.divinity = int(npchoice(divinities, 1, p=p_divinity[sum_])[0])

Response: Combine two gametes an egg and a sperm and assign divinity to the parents.


Instruction:
Write a Python 3 script for
picking a random name from the lists loaded with the model.

Response:
def set_name(self):
        """Pick a random name from the lists loaded with the model. For Gods that
        identify as neither M nor F, the model attempts to retrieve an androgynous
        name. Note: not all of the scraped name lists contain androgynous names.
        """
        if not self.gender: self.set_gender()

        name = ''
        if self.gender == female:
            name = names.female_names.pop()
        elif self.gender == male:
            name = names.male_names.pop()
        else:
            try:
                name = names.nb_names.pop()
            except:
                # No androgynous names available
                name = names.male_names.pop()

        self.name = name


Instruction:
Implement a Python 3 function for
divining an appropriate epithet for this God.

Response:
def set_epithet(self):
        """Divine an appropriate epithet for this God. (See what I did there?)"""
        if self.divinity == human:
            obsession = random.choice(self.genome)
            if self.gender == female:
                self.epithet = 'ordinary woman'
            elif self.gender == male:
                self.epithet = 'ordinary man'
            else:
                self.epithet = 'ordinary human being'
            self.epithet += ' who loves ' + obsession
            return # Return early. The rest of the function deals with gods.

        if self.gender == female:
            title = 'Goddess'
        elif self.gender == male:
            title = 'God'
        else:
            title = 'Divine Being'
        if self.divinity == demi_god:
            title = 'Semi-' + title if self.gender == non_binary else 'Demi-' + title

        num_domains = npchoice([1,2,3,4], 1, p=[0.05, 0.35, 0.55, 0.05])[0]

        if num_domains == 1:
            template = '%s of %s'
        if num_domains == 2:
            template = '%s of %s and %s'
        elif num_domains == 3:
            template = '%s of %s, %s, and %s' # Oxford comma, the most divine punctuation.
        elif num_domains == 4:
            template = '%s of %s, %s, %s, and %s'

        self.domains = [d.title() for d in random.sample(self.genome, num_domains)]

        # Put it all together
        self.epithet = template % (title, *self.domains)


Instruction:
Can you tell what is the following Python 3 function doing
def generate_gamete(self, egg_or_sperm_word):
        p_rate_of_mutation = [0.9, 0.1]
        should_use_mutant_pool = (npchoice([0,1], 1, p=p_rate_of_mutation)[0] == 1)
        if should_use_mutant_pool:
            pool = tokens.secondary_tokens
        else:
            pool = tokens.primary_tokens

        return get_matches(egg_or_sperm_word, pool, 23)

Response: Generate a list of chromosomes from the given egg or SPerm word.


Instruction:
How would you implement a function in Python 3 that
prints parents name and epithets.

Response:
def print_parents(self):
        """Print parents' names and epithets."""
        if self.gender == female:
            title = 'Daughter'
        elif self.gender == male:
            title = 'Son'
        else:
            title = 'Child'

        p1 = self.parents[0]
        p2 = self.parents[1]

        template = '%s of %s, the %s, and %s, the %s.'

        print(template % (title, p1.name, p1.epithet, p2.name, p2.epithet))


Instruction:
Given the following Python 3 function, write the documentation
def instance(self, counter=None, pipeline_counter=None):
        pipeline_counter = pipeline_counter or self.pipeline_counter
        pipeline_instance = None

        if not pipeline_counter:
            pipeline_instance = self.server.pipeline(self.pipeline_name).instance()
            self.pipeline_counter = int(pipeline_instance['counter'])

        if not counter:
            if pipeline_instance is None:
                pipeline_instance = (
                    self.server
                        .pipeline(self.pipeline_name)
                        .instance(pipeline_counter)
                )

            for stages in pipeline_instance['stages']:
                if stages['name'] == self.stage_name:
                    return self.instance(
                        counter=int(stages['counter']),
                        pipeline_counter=pipeline_counter
                    )

        return self._get('/instance/{pipeline_counter:d}/{counter:d}'
                         .format(pipeline_counter=pipeline_counter, counter=counter))

Response: Returns all the information regarding a specific stage run


Instruction:
How would you explain what the following Python 3 function does
def is_json(self):
        return (self.content_type.startswith('application/json') or
                re.match(r'application/vnd.go.cd.v(\d+)\+json', self.content_type))

Response: Returns True if content_type is json


Instruction:
How would you code a function in Python 3 to
return the payload of the message.

Response:
def payload(self):
        """
        Returns:
          `str` when not json.
          `dict` when json.
        """
        if self.is_json:
            if not self._body_parsed:
                if hasattr(self._body, 'decode'):
                    body = self._body.decode('utf-8')
                else:
                    body = self._body

                self._body_parsed = json.loads(body)

            return self._body_parsed
        else:
            return self._body


Instruction:
Implement a Python 3 function for
performing a HTTP request to the Go server.

Response:
def request(self, path, data=None, headers=None, method=None):
        """Performs a HTTP request to the Go server

        Args:
          path (str): The full path on the Go server to request.
            This includes any query string attributes.
          data (str, dict, bool, optional): If any data is present this
            request will become a POST request.
          headers (dict, optional): Headers to set for this particular
            request

        Raises:
          HTTPError: when the HTTP request fails.

        Returns:
          file like object: The response from a
            :func:`urllib2.urlopen` call
        """
        if isinstance(data, str):
            data = data.encode('utf-8')
        response = urlopen(self._request(path, data=data, headers=headers, method=method))
        self._set_session_cookie(response)

        return response


Instruction:
Given the following Python 3 function, write the documentation
def add_logged_in_session(self, response=None):
        if not response:
            response = self.get('go/api/pipelines.xml')

        self._set_session_cookie(response)

        if not self._session_id:
            raise AuthenticationFailed('No session id extracted from request.')

        response = self.get('go/pipelines')
        match = re.search(
            r'name="authenticity_token".+?value="([^"]+)',
            response.read().decode('utf-8')
        )
        if match:
            self._authenticity_token = match.group(1)
        else:
            raise AuthenticationFailed('Authenticity token not found on page')

Response: Add the logged in session to the request object.


Instruction:
Write a Python 3 function that can
return an instance of a Stage class.

Response:
def stage(self, pipeline_name, stage_name, pipeline_counter=None):
        """Returns an instance of :class:`Stage`

        Args:
            pipeline_name (str): Name of the pipeline the stage belongs to
            stage_name (str): Name of the stage to act on
            pipeline_counter (int): The pipeline instance the stage is for.

        Returns:
          Stage: an instantiated :class:`Stage`.
        """
        return Stage(self, pipeline_name, stage_name, pipeline_counter=pipeline_counter)


Instruction:
Given the following Python 3 function, write the documentation
def flatten(d):

    if not isinstance(d, dict):
        return [[d]]

    returned = []
    for key, value in d.items():
        # Each key, value is treated as a row.
        nested = flatten(value)
        for nest in nested:
            current_row = [key]
            current_row.extend(nest)
            returned.append(current_row)

    return returned

Response: Return a dict as a list of lists.


Instruction:
Write a Python 3 function that can
return all the information regarding a specific pipeline instance.

Response:
def instance(self, counter=None):
        """Returns all the information regarding a specific pipeline run

        See the `Go pipeline instance documentation`__ for examples.

        .. __: http://api.go.cd/current/#get-pipeline-instance

        Args:
          counter (int): The pipeline instance to fetch.
            If falsey returns the latest pipeline instance from :meth:`history`.

        Returns:
          Response: :class:`gocd.api.response.Response` object
        """
        if not counter:
            history = self.history()
            if not history:
                return history
            else:
                return Response._from_json(history['pipelines'][0])

        return self._get('/instance/{counter:d}'.format(counter=counter))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def schedule(self, variables=None, secure_variables=None, materials=None,
                 return_new_instance=False, backoff_time=1.0):
        """Schedule a pipeline run

        Aliased as :meth:`run`, :meth:`schedule`, and :meth:`trigger`.

        Args:
          variables (dict, optional): Variables to set/override
          secure_variables (dict, optional): Secure variables to set/override
          materials (dict, optional): Material revisions to be used for
            this pipeline run. The exact format for this is a bit iffy,
            have a look at the official
            `Go pipeline scheduling documentation`__ or inspect a call
            from triggering manually in the UI.
          return_new_instance (bool): Returns a :meth:`history` compatible
            response for the newly scheduled instance. This is primarily so
            users easily can get the new instance number. **Note:** This is done
            in a very naive way, it just checks that the instance number is
            higher than before the pipeline was triggered.
          backoff_time (float): How long between each check for
            :arg:`return_new_instance`.

         .. __: http://api.go.cd/current/#scheduling-pipelines

        Returns:
          Response: :class:`gocd.api.response.Response` object
        """
        scheduling_args = dict(
            variables=variables,
            secure_variables=secure_variables,
            material_fingerprint=materials,
            headers={"Confirm": True},
        )

        scheduling_args = dict((k, v) for k, v in scheduling_args.items() if v is not None)

        # TODO: Replace this with whatever is the official way as soon as gocd#990 is fixed.
        # https://github.com/gocd/gocd/issues/990
        if return_new_instance:
            pipelines = self.history()['pipelines']
            if len(pipelines) == 0:
                last_run = None
            else:
                last_run = pipelines[0]['counter']
            response = self._post('/schedule', ok_status=202, **scheduling_args)
            if not response:
                return response

            max_tries = 10
            while max_tries > 0:
                current = self.instance()
                if not last_run and current:
                    return current
                elif last_run and current['counter'] > last_run:
                    return current
                else:
                    time.sleep(backoff_time)
                    max_tries -= 1

            # I can't come up with a scenario in testing where this would happen, but it seems
            # better than returning None.
            return response
        else:
            return self._post('/schedule', ok_status=202, **scheduling_args)

Response: Schedules a new pipeline run.


Instruction:
Explain what the following Python 3 code does
def artifact(self, counter, stage, job, stage_counter=1):
        return Artifact(self.server, self.name, counter, stage, job, stage_counter)

Response: Helper to instantiate an artifact object for a given pipeline.


Instruction:
How would you code a function in Python 3 to
yield the output and metadata from all jobs in the pipeline

Response:
def console_output(self, instance=None):
        """Yields the output and metadata from all jobs in the pipeline

        Args:
          instance: The result of a :meth:`instance` call, if not supplied
            the latest of the pipeline will be used.

        Yields:
          tuple: (metadata (dict), output (str)).

          metadata contains:
            - pipeline
            - pipeline_counter
            - stage
            - stage_counter
            - job
            - job_result
        """
        if instance is None:
            instance = self.instance()

        for stage in instance['stages']:
            for job in stage['jobs']:
                if job['result'] not in self.final_results:
                    continue

                artifact = self.artifact(
                    instance['counter'],
                    stage['name'],
                    job['name'],
                    stage['counter']
                )
                output = artifact.get('cruise-output/console.log')

                yield (
                    {
                        'pipeline': self.name,
                        'pipeline_counter': instance['counter'],
                        'stage': stage['name'],
                        'stage_counter': stage['counter'],
                        'job': job['name'],
                        'job_result': job['result'],
                    },
                    output.body
                )


Instruction:
Can you generate the documentation for the following Python 3 function
def stage(self, name, pipeline_counter=None):
        return Stage(
            self.server,
            pipeline_name=self.name,
            stage_name=name,
            pipeline_counter=pipeline_counter,
        )

Response: Helper to instantiate a : class : gocd. api. stage. Stage object.


Instruction:
Implement a function in Python 3 to
update template config for specified template name.

Response:
def edit(self, config, etag):
        """Update template config for specified template name.

        .. __: https://api.go.cd/current/#edit-template-config

        Returns:
          Response: :class:`gocd.api.response.Response` object
        """

        data = self._json_encode(config)
        headers = self._default_headers()

        if etag is not None:
            headers["If-Match"] = etag

        return self._request(self.name,
                             ok_status=None,
                             data=data,
                             headers=headers,
                             method="PUT")


Instruction:
Implement a Python 3 function for
creating a new template config for the given template name.

Response:
def create(self, config):
        """Create template config for specified template name.

        .. __: https://api.go.cd/current/#create-template-config

        Returns:
          Response: :class:`gocd.api.response.Response` object
        """

        assert config["name"] == self.name, "Given config is not for this template"

        data = self._json_encode(config)
        headers = self._default_headers()

        return self._request("",
                             ok_status=None,
                             data=data,
                             headers=headers)


Instruction:
Write a Python 3 function that can
delete the template config for the specified template name.

Response:
def delete(self):
        """Delete template config for specified template name.

        .. __: https://api.go.cd/current/#delete-a-template

        Returns:
          Response: :class:`gocd.api.response.Response` object
        """

        headers = self._default_headers()

        return self._request(self.name,
                             ok_status=None,
                             data=None,
                             headers=headers,
                             method="DELETE")


Instruction:
Can you generate a brief explanation for the following Python 3 code
def pipelines(self):
        if not self.response:
            return set()
        elif self._pipelines is None and self.response:
            self._pipelines = set()
            for group in self.response.payload:
                for pipeline in group['pipelines']:
                    self._pipelines.add(pipeline['name'])

        return self._pipelines

Response:
Returns a set of all the pipelines available in the last response
AttributeNames


Instruction:
Write a Python 3 script to
get an artifact directory by its path.

Response:
def get_directory(self, path_to_directory, timeout=30, backoff=0.4, max_wait=4):
        """Gets an artifact directory by its path.

        See the `Go artifact directory documentation`__ for example responses.

        .. __: http://api.go.cd/current/#get-artifact-directory

        .. note::
          Getting a directory relies on Go creating a zip file of the
          directory in question. Because of this Go will zip the file in
          the background and return a 202 Accepted response. It's then up
          to the client to check again later and get the final file.

          To work with normal assumptions this :meth:`get_directory` will
          retry itself up to ``timeout`` seconds to get a 200 response to
          return. At that point it will then return the response as is, no
          matter whether it's still 202 or 200. The retry is done with an
          exponential backoff with a max value between retries. See the
          ``backoff`` and ``max_wait`` variables.

          If you want to handle the retry logic yourself then use :meth:`get`
          and add '.zip' as a suffix on the directory.

        Args:
          path_to_directory (str): The path to the directory to get.
            It can be nested eg ``target/dist.zip``
          timeout (int): How many seconds we will wait in total for a
            successful response from Go when we're receiving 202
          backoff (float): The initial value used for backoff, raises
            exponentially until it reaches ``max_wait``
          max_wait (int): The max time between retries

        Returns:
          Response: :class:`gocd.api.response.Response` object
            A successful response is a zip-file.
        """
        response = None
        started_at = None
        time_elapsed = 0

        i = 0
        while time_elapsed < timeout:
            response = self._get('{0}.zip'.format(path_to_directory))

            if response:
                break
            else:
                if started_at is None:
                    started_at = time.time()

                time.sleep(min(backoff * (2 ** i), max_wait))
                i += 1
                time_elapsed = time.time() - started_at

        return response


Instruction:
Make a summary of the following Python 3 code
def ask(self):
        with self.mutex:
            if not len(self.queue):
                raise Empty
            utcnow = dt.datetime.utcnow()
            if self.queue[0][0] <= utcnow:
                self.ready.notify()
                return 0
            return (self.queue[0][0] - utcnow).total_seconds()

Response: Returns the wait time in seconds required to retrieve the next item in the queue.


Instruction:
Can you tell what is the following Python 3 function doing
def p_transition(p):
    p[3] = None if p[3] == 'NULL' else p[3]
    if p[4] == 'error':
        p[0] = MarionetteTransition(p[1], p[2], p[3], 0, True)
    else:
        p[0] = MarionetteTransition(p[1], p[2], p[3], p[4], False)

Response:
transition : START_KWD KEY NULL_KWD FLOAT
    transition : KEY KEY NULL_KWD FLOAT
    transition : KEY END_KWD NULL_KWD FLOAT
    transition : START_KWD KEY KEY FLOAT
    transition : KEY KEY KEY FLOAT
    transition : KEY END_KWD KEY FLOAT
    transition : START_KWD KEY NULL_KWD INTEGER
    transition : KEY KEY NULL_KWD INTEGER
    transition : KEY END_KWD NULL_KWD INTEGER
    transition : START_KWD KEY KEY INTEGER
    transition : KEY KEY KEY INTEGER
    transition : KEY END_KWD KEY INTEGER
    transition : START_KWD KEY NULL_KWD KEY
    transition : KEY KEY NULL_KWD KEY
    transition : KEY END_KWD NULL_KWD KEY
    transition : START_KWD KEY KEY KEY
    transition : KEY KEY KEY KEY
    transition : KEY END_KWD KEY KEY


Instruction:
Implement a function in Python 3 to
parse the action line.

Response:
def p_action(p):
    """
    action : CLIENT_KWD KEY DOT KEY LPAREN args RPAREN
    action : SERVER_KWD KEY DOT KEY LPAREN args RPAREN
    action : CLIENT_KWD KEY DOT KEY LPAREN args RPAREN IF_KWD REGEX_MATCH_INCOMING_KWD LPAREN p_string_arg RPAREN
    action : SERVER_KWD KEY DOT KEY LPAREN args RPAREN IF_KWD REGEX_MATCH_INCOMING_KWD LPAREN p_string_arg RPAREN
    """
    if len(p)==8:
        p[0] = [p[1], p[2], p[4], p[6], None]
    elif len(p)==13:
        p[0] = [p[1], p[2], p[4], p[6], p[11]]


Instruction:
Can you generate a brief explanation for the following Python 3 code
def app_class():
    try:
        pkg_resources.get_distribution('invenio-files-rest')
        from invenio_files_rest.app import Flask as FlaskBase
    except pkg_resources.DistributionNotFound:
        from flask import Flask as FlaskBase

    # Add Host header validation via APP_ALLOWED_HOSTS configuration variable.
    class Request(TrustedHostsMixin, FlaskBase.request_class):
        pass

    class Flask(FlaskBase):
        request_class = Request

    return Flask

Response: Create Flask application class.


Instruction:
Given the following Python 3 function, write the documentation
def init_app(self, app, **kwargs):
        # Init the configuration
        self.init_config(app)
        # Enable Rate limiter
        self.limiter = Limiter(app, key_func=get_ipaddr)
        # Enable secure HTTP headers
        if app.config['APP_ENABLE_SECURE_HEADERS']:
            self.talisman = Talisman(
                app, **app.config.get('APP_DEFAULT_SECURE_HEADERS', {})
            )
        # Enable PING view
        if app.config['APP_HEALTH_BLUEPRINT_ENABLED']:
            blueprint = Blueprint('invenio_app_ping', __name__)

            @blueprint.route('/ping')
            def ping():
                """Load balancer ping view."""
                return 'OK'

            ping.talisman_view_options = {'force_https': False}

            app.register_blueprint(blueprint)

        requestid_header = app.config.get('APP_REQUESTID_HEADER')
        if requestid_header:
            @app.before_request
            def set_request_id():
                """Extracts a request id from an HTTP header."""
                request_id = request.headers.get(requestid_header)
                if request_id:
                    # Capped at 200 to protect against malicious clients
                    # sending very large headers.
                    g.request_id = request_id[:200]

        # If installed register the Flask-DebugToolbar extension
        try:
            from flask_debugtoolbar import DebugToolbarExtension
            app.extensions['flask-debugtoolbar'] = DebugToolbarExtension(app)
        except ImportError:
            app.logger.debug('Flask-DebugToolbar extension not installed.')

        # Register self
        app.extensions['invenio-app'] = self

Response: Initialize the application object.


Instruction:
Write a Python 3 function for
initializing configuration. :param app: An instance of :class:`~flask.Flask`.

Response:
def init_config(self, app):
        """Initialize configuration.

        :param app: An instance of :class:`~flask.Flask`.
        """
        config_apps = ['APP_', 'RATELIMIT_']
        flask_talisman_debug_mode = ["'unsafe-inline'"]
        for k in dir(config):
            if any([k.startswith(prefix) for prefix in config_apps]):
                app.config.setdefault(k, getattr(config, k))

        if app.config['DEBUG']:
            app.config.setdefault('APP_DEFAULT_SECURE_HEADERS', {})
            headers = app.config['APP_DEFAULT_SECURE_HEADERS']
            # ensure `content_security_policy` is not set to {}
            if headers.get('content_security_policy') != {}:
                headers.setdefault('content_security_policy', {})
                csp = headers['content_security_policy']
                # ensure `default-src` is not set to []
                if csp.get('default-src') != []:
                    csp.setdefault('default-src', [])
                    # add default `content_security_policy` value when debug
                    csp['default-src'] += flask_talisman_debug_mode


Instruction:
Can you generate the documentation for the following Python 3 function
def remove_leading(needle, haystack):
    if haystack[:len(needle)] == needle:
        return haystack[len(needle):]
    return haystack

Response: Remove leading needle from haystack.


Instruction:
Implement a function in Python 3 to
remove trailing needle from haystack.

Response:
def remove_trailing(needle, haystack):
    """Remove trailing needle string (if exists).

    >>> remove_trailing('Test', 'ThisAndThatTest')
    'ThisAndThat'
    >>> remove_trailing('Test', 'ArbitraryName')
    'ArbitraryName'
    """
    if haystack[-len(needle):] == needle:
        return haystack[:-len(needle)]
    return haystack


Instruction:
Can you generate a brief explanation for the following Python 3 code
def camel2word(string):
    def wordize(match):
        return ' ' + match.group(1).lower()

    return string[0] + re.sub(r'([A-Z])', wordize, string[1:])

Response: Covert name from CamelCase to Normal case.


Instruction:
Can you generate the documentation for the following Python 3 function
def complete_english(string):
    for x, y in [("dont", "don't"),
                ("doesnt", "doesn't"),
                ("wont", "won't"),
                ("wasnt", "wasn't")]:
        string = string.replace(x, y)
    return string

Response: Complete a string by removing all non - English characters.


Instruction:
Create a Python 3 function for
formatting a time in seconds.

Response:
def format_seconds(self, n_seconds):
        """Format a time in seconds."""
        func = self.ok
        if n_seconds >= 60:
            n_minutes, n_seconds = divmod(n_seconds, 60)
            return "%s minutes %s seconds" % (
                        func("%d" % n_minutes),
                        func("%.3f" % n_seconds))
        else:
            return "%s seconds" % (
                        func("%.3f" % n_seconds))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def ppdict(dict_to_print, br='\n', html=False, key_align='l', sort_keys=True,
           key_preffix='', key_suffix='', value_prefix='', value_suffix='', left_margin=3, indent=2):
    """Indent representation of a dict"""
    if dict_to_print:
        if sort_keys:
            dic = dict_to_print.copy()
            keys = list(dic.keys())
            keys.sort()
            dict_to_print = OrderedDict()
            for k in keys:
                dict_to_print[k] = dic[k]

        tmp = ['{']
        ks = [type(x) == str and "'%s'" % x or x for x in dict_to_print.keys()]
        vs = [type(x) == str and "'%s'" % x or x for x in dict_to_print.values()]
        max_key_len = max([len(str(x)) for x in ks])

        for i in range(len(ks)):
            k = {1: str(ks[i]).ljust(max_key_len),
                 key_align == 'r': str(ks[i]).rjust(max_key_len)}[1]

            v = vs[i]
            tmp.append(' ' * indent + '{}{}{}:{}{}{},'.format(key_preffix, k, key_suffix,
                                                              value_prefix, v, value_suffix))

        tmp[-1] = tmp[-1][:-1]  # remove the ',' in the last item
        tmp.append('}')

        if left_margin:
            tmp = [' ' * left_margin + x for x in tmp]

        if html:
            return '<code>{}</code>'.format(br.join(tmp).replace(' ', ' '))
        else:
            return br.join(tmp)
    else:
        return '{}'

Response: Pretty print a dict of items


Instruction:
Write a Python 3 function for
asserting that result == expected.

Response:
def eq_(result, expected, msg=None):
    """
    Shadow of the Nose builtin which presents easier to read multiline output.
    """
    params = {'expected': expected, 'result': result}
    aka = """

--------------------------------- aka -----------------------------------------

Expected:
%(expected)r

Got:
%(result)r
""" % params
    default_msg = """
Expected:
%(expected)s

Got:
%(result)s
""" % params
    if (
        (repr(result) != six.text_type(result)) or
        (repr(expected) != six.text_type(expected))
    ):
        default_msg += aka
    assertion_msg = msg or default_msg
    # This assert will bubble up to Nose's failure handling, which at some
    # point calls explicit str() - which will UnicodeDecodeError on any non
    # ASCII text.
    # To work around this, we make sure Unicode strings become bytestrings
    # beforehand, with explicit encode.
    if isinstance(assertion_msg, six.text_type):
        assertion_msg = assertion_msg.encode('utf-8')
    assert result == expected, assertion_msg


Instruction:
Write a Python 3 script for
asserting that haystack contains needle.

Response:
def _assert_contains(haystack, needle, invert, escape=False):
    """
    Test for existence of ``needle`` regex within ``haystack``.

    Say ``escape`` to escape the ``needle`` if you aren't really using the
    regex feature & have special characters in it.
    """
    myneedle = re.escape(needle) if escape else needle
    matched = re.search(myneedle, haystack, re.M)
    if (invert and matched) or (not invert and not matched):
        raise AssertionError("'%s' %sfound in '%s'" % (
            needle,
            "" if invert else "not ",
            haystack
        ))


Instruction:
Write a Python 3 script to
find the. splunk_logger config file in the current directory or in the user s home and parse it.

Response:
def parse_config_file():
    """
    Find the .splunk_logger config file in the current directory, or in the
    user's home and parse it. The one in the current directory has precedence.

    :return: A tuple with:
                - project_id
                - access_token
    """
    for filename in ('.splunk_logger', os.path.expanduser('~/.splunk_logger')):

        project_id, access_token, api_domain = _parse_config_file_impl(filename)

        if project_id is not None\
        and access_token is not None\
        and api_domain is not None:
            return project_id, access_token, api_domain

    else:
        return None, None, None


Instruction:
Can you generate the documentation for the following Python 3 function
def _parse_config_file_impl(filename):
    try:
        doc = yaml.load(file(filename).read())

        project_id = doc["credentials"]["project_id"]
        access_token = doc["credentials"]["access_token"]
        api_domain = doc["credentials"]["api_domain"]

        return project_id, access_token, api_domain
    except:
        return None, None, None

Response: Parse the config file into a tuple with the project_id access_token api_domain


Instruction:
Can you generate a brief explanation for the following Python 3 code
def dem_url_dia(dt_day='2015-06-22'):

    def _url_tipo_dato(str_dia, k):
        url = SERVER + '/archives/{}/download_json?locale=es'.format(D_TIPOS_REQ_DEM[k])
        if type(str_dia) is str:
            return url + '&date=' + str_dia
        else:
            return url + '&date=' + str_dia.date().isoformat()

    urls = [_url_tipo_dato(dt_day, k) for k in D_TIPOS_REQ_DEM.keys()]
    return urls

Response: Obtiene las urls de los datos de demanda energética de un día concreto.


Instruction:
Can you tell what is the following Python 3 function doing
def dem_procesa_datos_dia(key_day, response):
    dfs_import, df_import, dfs_maxmin, hay_errores = [], None, [], 0
    for r in response:
        tipo_datos, data = _extract_func_json_data(r)
        if tipo_datos is not None:
            if ('IND_MaxMin' in tipo_datos) and data:
                df_import = _import_daily_max_min(data)
                dfs_maxmin.append(df_import)
            elif data:
                df_import = _import_json_ts_data(data)
                dfs_import.append(df_import)
        if tipo_datos is None or df_import is None:
            hay_errores += 1
    if hay_errores == 4:
        # No hay nada, salida temprana sin retry:
        print_redb('** No hay datos para el día {}!'.format(key_day))
        return None, -2
    else:  # if hay_errores < 3:
        # TODO formar datos incompletos!! (max-min con NaN's, etc.)
        data_import = {}
        if dfs_import:
            data_import[KEYS_DATA_DEM[0]] = dfs_import[0].join(dfs_import[1])
        if len(dfs_maxmin) == 2:
            data_import[KEYS_DATA_DEM[1]] = dfs_maxmin[0].join(dfs_maxmin[1])
        elif dfs_maxmin:
            data_import[KEYS_DATA_DEM[1]] = dfs_maxmin[0]
        if not data_import:
            print_err('DÍA: {} -> # ERRORES: {}'.format(key_day, hay_errores))
            return None, -2
        return data_import, 0

Response: Procesa los datos descargados en JSON.


Instruction:
Write a Python 3 function that can
flag all inner classes of obj as well as any child classes of obj.

Response:
def flag_inner_classes(obj):
    """
    Mutates any attributes on ``obj`` which are classes, with link to ``obj``.

    Adds a convenience accessor which instantiates ``obj`` and then calls its
    ``setup`` method.

    Recurses on those objects as well.
    """
    for tup in class_members(obj):
        tup[1]._parent = obj
        tup[1]._parent_inst = None
        tup[1].__getattr__ = my_getattr
        flag_inner_classes(tup[1])


Instruction:
Given the following Python 3 function, write the documentation
def autohide(obj):
    # Members on obj
    for name, item in six.iteritems(vars(obj)):
        if callable(item) and name in ('setup', 'teardown'):
            item = hide(item)
    # Recurse into class members
    for name, subclass in class_members(obj):
        autohide(subclass)

Response: Auto hide setup and teardown methods recursively.


Instruction:
Write a Python 3 script for
replacing sys.std(out|err) with a wrapper during execution, restored after. In addition, a new combined-streams output (another wrapper) will appear at ``sys.stdall``. This stream will resemble what a user sees at a terminal, i.e. both out/err streams intermingled.

Response:
def trap(func):
    """
    Replace sys.std(out|err) with a wrapper during execution, restored after.

    In addition, a new combined-streams output (another wrapper) will appear at
    ``sys.stdall``. This stream will resemble what a user sees at a terminal,
    i.e. both out/err streams intermingled.
    """
    @wraps(func)
    def wrapper(*args, **kwargs):
        # Use another CarbonCopy even though we're not cc'ing; for our "write
        # bytes, return strings on py3" behavior. Meh.
        sys.stdall = CarbonCopy()
        my_stdout, sys.stdout = sys.stdout, CarbonCopy(cc=sys.stdall)
        my_stderr, sys.stderr = sys.stderr, CarbonCopy(cc=sys.stdall)
        try:
            return func(*args, **kwargs)
        finally:
            sys.stdout = my_stdout
            sys.stderr = my_stderr
            del sys.stdall
    return wrapper


Instruction:
Explain what the following Python 3 code does
def pvpc_url_dia(dt_day):
    if type(dt_day) is str:
        return SERVER + '/archives/70/download_json?locale=es' + '&date=' + dt_day
    else:
        return SERVER + '/archives/70/download_json?locale=es' + '&date=' + dt_day.date().isoformat()

Response: Obtiene la url de descarga de los datos de PVPC de un día concreto.


Instruction:
How would you explain what the following Python 3 function does
def pvpc_calc_tcu_cp_feu_d(df, verbose=True, convert_kwh=True):
    if 'TCU' + TARIFAS[0] not in df.columns:
        # Pasa de €/MWh a €/kWh:
        if convert_kwh:
            cols_mwh = [c + t for c in COLS_PVPC for t in TARIFAS if c != 'COF']
            df[cols_mwh] = df[cols_mwh].applymap(lambda x: x / 1000.)
        # Obtiene columnas TCU, CP, precio día
        gb_t = df.groupby(lambda x: TARIFAS[np.argmax([t in x for t in TARIFAS])], axis=1)
        for k, g in gb_t:
            if verbose:
                print('TARIFA {}'.format(k))
                print(g.head())

            # Cálculo de TCU
            df['TCU{}'.format(k)] = g[k] - g['TEU{}'.format(k)]

            # Cálculo de CP
            # cols_cp = [c + k for c in ['FOS', 'FOM', 'INT', 'PCAP', 'PMH', 'SAH']]
            cols_cp = [c + k for c in COLS_PVPC if c not in ['', 'COF', 'TEU']]
            df['CP{}'.format(k)] = g[cols_cp].sum(axis=1)

            # Cálculo de PERD --> No es posible así, ya que los valores base ya vienen con PERD
            # dfs_pvpc[k]['PERD{}'.format(k)] = dfs_pvpc[k]['TCU{}'.format(k)] / dfs_pvpc[k]['CP{}'.format(k)]
            # dfs_pvpc[k]['PERD{}'.format(k)] = dfs_pvpc[k]['INT{}'.format(k)] / 1.92

            # Cálculo de FEU diario
            cols_k = ['TEU' + k, 'TCU' + k, 'COF' + k]
            g = df[cols_k].groupby('TEU' + k)
            pr = g.apply(lambda x: x['TCU' + k].dot(x['COF' + k]) / x['COF' + k].sum())
            pr.name = 'PD_' + k
            df = df.join(pr, on='TEU' + k, rsuffix='_r')
            df['PD_' + k] += df['TEU' + k]
    return df

Response: Calculate TCU CP FEU D.


Instruction:
Explain what the following Python 3 code does
def pvpc_procesa_datos_dia(_, response, verbose=True):
    try:
        d_data = response['PVPC']
        df = _process_json_pvpc_hourly_data(pd.DataFrame(d_data))
        return df, 0
    except Exception as e:
        if verbose:
            print('ERROR leyendo información de web: {}'.format(e))
        return None, -2

Response: Procesa la información JSON descargada y forma el dataframe de los datos de un día.


Instruction:
Explain what the following Python 3 code does
def pvpc_data_dia(str_dia, str_dia_fin=None):
    params = {'date_fmt': DATE_FMT, 'usar_multithread': False,
              'func_procesa_data_dia': pvpc_procesa_datos_dia, 'func_url_data_dia': pvpc_url_dia,
              'data_extra_request': {'json_req': True, 'headers': HEADERS}}
    if str_dia_fin is not None:
        params['usar_multithread'] = True
        data, hay_errores, str_import = get_data_en_intervalo(str_dia, str_dia_fin, **params)
    else:
        data, hay_errores, str_import = get_data_en_intervalo(str_dia, str_dia, **params)
    if not hay_errores:
        return data
    else:
        return str_import

Response: Obtiene datos de PVPC en un día concreto o un intervalo accediendo directamente a la web.


Instruction:
Write a Python 3 function that can
compress the log message in order to send less bytes to the wire.

Response:
def _compress(self, input_str):
        """
        Compress the log message in order to send less bytes to the wire.
        """
        compressed_bits = cStringIO.StringIO()

        f = gzip.GzipFile(fileobj=compressed_bits, mode='wb')
        f.write(input_str)
        f.close()

        return compressed_bits.getvalue()


Instruction:
Can you implement a function in Python 3 that
gets data coefficientsicientes de perfilado para 2017.

Response:
def get_data_coeficientes_perfilado_2017(force_download=False):
    """Extrae la información de las dos hojas del Excel proporcionado por REE
    con los perfiles iniciales para 2017.
    :param force_download: Descarga el fichero 'raw' del servidor, en vez de acudir a la copia local.
    :return: perfiles_2017, coefs_alpha_beta_gamma
    :rtype: tuple
    """
    path_perfs = os.path.join(STORAGE_DIR, 'perfiles_consumo_2017.h5')

    if force_download or not os.path.exists(path_perfs):
        # Coeficientes de perfilado y demanda de referencia (1ª hoja)
        cols_sheet1 = ['Mes', 'Día', 'Hora',
                       'Pa,0m,d,h', 'Pb,0m,d,h', 'Pc,0m,d,h', 'Pd,0m,d,h', 'Demanda de Referencia 2017 (MW)']
        perfs_2017 = pd.read_excel(URL_PERFILES_2017, header=None, skiprows=[0, 1], names=cols_sheet1)
        perfs_2017['ts'] = pd.DatetimeIndex(start='2017-01-01', freq='H', tz=TZ, end='2017-12-31 23:59')
        perfs_2017 = perfs_2017.set_index('ts').drop(['Mes', 'Día', 'Hora'], axis=1)

        # Coefs Alfa, Beta, Gamma (2ª hoja):
        coefs_alpha_beta_gamma = pd.read_excel(URL_PERFILES_2017, sheetname=1)
        print('Escribiendo perfiles 2017 en disco, en {}'.format(path_perfs))
        with pd.HDFStore(path_perfs, 'w') as st:
            st.put('coefs', coefs_alpha_beta_gamma)
            st.put('perfiles', perfs_2017)
        print('HDFStore de tamaño {:.3f} KB'.format(os.path.getsize(path_perfs) / 1000))
    else:
        with pd.HDFStore(path_perfs, 'r') as st:
            coefs_alpha_beta_gamma = st['coefs']
            perfs_2017 = st['perfiles']
    return perfs_2017, coefs_alpha_beta_gamma


Instruction:
Given the following Python 3 function, write the documentation
def get_data_perfiles_estimados_2017(force_download=False):
    global DATA_PERFILES_2017
    if (DATA_PERFILES_2017 is None) or force_download:
        perf_demref_2017, _ = get_data_coeficientes_perfilado_2017(force_download=force_download)
        # Conversión de formato de dataframe de perfiles 2017 a finales (para uniformizar):
        cols_usar = ['Pa,0m,d,h', 'Pb,0m,d,h', 'Pc,0m,d,h', 'Pd,0m,d,h']
        perfs_2017 = perf_demref_2017[cols_usar].copy()
        perfs_2017.columns = ['COEF. PERFIL {}'.format(p) for p in 'ABCD']
        DATA_PERFILES_2017 = perfs_2017
        return perfs_2017
    return DATA_PERFILES_2017

Response: Get the data for the 2017 - level perfiles


Instruction:
Write a Python 3 script to
add class to good list.

Response:
def registerGoodClass(self, class_):
        """
        Internal bookkeeping to handle nested classes
        """
        # Class itself added to "good" list
        self._valid_classes.append(class_)
        # Recurse into any inner classes
        for name, cls in class_members(class_):
            if self.isValidClass(cls):
                self.registerGoodClass(cls)


Instruction:
Explain what the following Python 3 code does
def isValidClass(self, class_):
        module = inspect.getmodule(class_)
        valid = (
            module in self._valid_modules
            or (
                hasattr(module, '__file__')
                and module.__file__ in self._valid_named_modules
            )
        )
        return valid and not private(class_)

Response: Returns True if the class is valid.


Instruction:
How would you explain what the following Python 3 function does
def last_entry(self, data_revisar=None, key_revisar=None):
        if data_revisar is None and key_revisar is None:
            data_revisar = self.data[self.masterkey][pd.notnull(self.data[self.masterkey]['dem'])]
            super(DatosREE, self).printif('Últimos valores de generación y demanda:', 'info')
            super(DatosREE, self).printif(data_revisar.tail(), 'info')
            return super(DatosREE, self).last_entry(data_revisar, 'dem')
        else:
            return super(DatosREE, self).last_entry(data_revisar, key_revisar)

Response: Return the last entry in the table.


Instruction:
Explain what the following Python 3 code does
def integridad_data(self, data_integr=None, key=None):
        if data_integr is None and key is None and all(k in self.data.keys() for k in KEYS_DATA_DEM):
            assert(self.data[KEYS_DATA_DEM[0]].index.freq == FREQ_DAT_DEM
                   and self.data[KEYS_DATA_DEM[0]].index.tz == self.TZ)
            if self.data[KEYS_DATA_DEM[1]] is not None:
                assert(self.data[KEYS_DATA_DEM[1]].index.freq == 'D')
        super(DatosREE, self).integridad_data(data_integr, key)

Response: Integra los datos de la base de datos y creciente archivo.


Instruction:
How would you explain what the following Python 3 function does
def sanitize_path(path):

    if path == '/':  # Nothing to do, just return
        return path

    if path[:1] != '/':
        raise InvalidPath('The path must start with a slash')

    # Deduplicate slashes in path
    path = re.sub(r'/+', '/', path)

    # Strip trailing slashes and return
    return path.rstrip('/')

Response: Performs sanitation of the path after validating


Instruction:
Make a summary of the following Python 3 code
def _validate_schema(obj):

    if obj is not None and not isinstance(obj, Schema):
        raise IncompatibleSchema('Schema must be of type {0}'.format(Schema))

    return obj

Response: Ensures that the passed schema instance is of a compatible type


Instruction:
Write a Python 3 script for
journeying route decorator Enables simple serialization, deserialization and validation of Flask routes with the help of Marshmallow. :param bp: :class:`flask.Blueprint` object :param args: args to pass along to `Blueprint.route` :param kwargs: - :strict_slashes: Enable / disable strict slashes (default False) - :validate: Enable / disable body/query validation (default True) - :_query: Unmarshal Query string into this schema - :_body: Unmarshal JSON body into this schema - :marshal_with: Serialize the output with this schema :raises: - ValidationError if the query parameters or JSON body fails validation

Response:
def route(bp, *args, **kwargs):
    """Journey route decorator

    Enables simple serialization, deserialization and validation of Flask routes with the help of Marshmallow.

    :param bp: :class:`flask.Blueprint` object
    :param args: args to pass along to `Blueprint.route`
    :param kwargs:
        - :strict_slashes: Enable / disable strict slashes (default False)
        - :validate: Enable / disable body/query validation (default True)
        - :_query: Unmarshal Query string into this schema
        - :_body: Unmarshal JSON body into this schema
        - :marshal_with: Serialize the output with this schema
    :raises:
        - ValidationError if the query parameters or JSON body fails validation
    """

    kwargs['strict_slashes'] = kwargs.pop('strict_slashes', False)
    body = _validate_schema(kwargs.pop('_body', None))
    query = _validate_schema(kwargs.pop('_query', None))
    output = _validate_schema(kwargs.pop('marshal_with', None))
    validate = kwargs.pop('validate', True)

    def decorator(f):
        @bp.route(*args, **kwargs)
        @wraps(f)
        def wrapper(*inner_args, **inner_kwargs):
            """If a schema (_body and/or _query) was supplied to the route decorator, the deserialized
            :class`marshmallow.Schema` object is injected into the decorated function's kwargs."""

            try:
                if query is not None:
                    query.strict = validate
                    url = furl(request.url)
                    inner_kwargs['_query'] = query.load(data=url.args)

                if body is not None:
                    body.strict = validate
                    json_data = request.get_json()

                    if json_data is None:
                        # Set json_data to empty dict if body is empty, so it gets picked up by the validator
                        json_data = {}

                    inner_kwargs['_body'] = body.load(data=json_data)

            except ValidationError as err:
                return jsonify(err.messages), 422

            if output:
                data = output.dump(f(*inner_args, **inner_kwargs))
                return jsonify(data[0])

            return f(*inner_args, **inner_kwargs)

        return f

    return decorator


Instruction:
Explain what the following Python 3 code does
def attach_bp(self, bp, description=''):

        if not isinstance(bp, Blueprint):
            raise InvalidBlueprint('Blueprints attached to the bundle must be of type {0}'.format(Blueprint))

        self.blueprints.append((bp, description))

Response: Attaches a flask. Blueprint to the bundle


Instruction:
Write a Python 3 function for
returning the DottedRule that results from moving the dot.

Response:
def move_dot(self):
        """Returns the DottedRule that results from moving the dot."""
        return self.__class__(self.production, self.pos + 1, self.lookahead)


Instruction:
Can you create a Python 3 function that
computes the intermediate FIRST set using symbols.

Response:
def first(self, symbols):
        """Computes the intermediate FIRST set using symbols."""
        ret = set()

        if EPSILON in symbols:
            return set([EPSILON])

        for symbol in symbols:
            ret |= self._first[symbol] - set([EPSILON])
            if EPSILON not in self._first[symbol]:
                break
        else:
            ret.add(EPSILON)

        return ret


Instruction:
Explain what the following Python 3 code does
def _compute_first(self):
        for terminal in self.terminals:
            self._first[terminal].add(terminal)
        self._first[END_OF_INPUT].add(END_OF_INPUT)

        while True:
            changed = False

            for nonterminal, productions in self.nonterminals.items():
                for production in productions:
                    new_first = self.first(production.rhs)
                    if new_first - self._first[nonterminal]:
                        self._first[nonterminal] |= new_first
                        changed = True

            if not changed:
                break

Response: Computes the FIRST set for every symbol in the grammar.


Instruction:
Can you generate the documentation for the following Python 3 function
def _compute_follow(self):
        self._follow[self.start_symbol].add(END_OF_INPUT)

        while True:
            changed = False

            for nonterminal, productions in self.nonterminals.items():
                for production in productions:
                    for i, symbol in enumerate(production.rhs):
                        if symbol not in self.nonterminals:
                            continue

                        first = self.first(production.rhs[i + 1:])
                        new_follow = first - set([EPSILON])
                        if EPSILON in first or i == (len(production.rhs) - 1):
                            new_follow |= self._follow[nonterminal]

                        if new_follow - self._follow[symbol]:
                            self._follow[symbol] |= new_follow
                            changed = True

            if not changed:
                break

Response: Computes the FOLLOW set for every non - terminal in the grammar.


Instruction:
Here you have a function in Python 3, explain what it does
def initial_closure(self):
        first_rule = DottedRule(self.start, 0, END_OF_INPUT)
        return self.closure([first_rule])

Response: Computes the initial closure using the START_foo production.


Instruction:
Write a Python 3 function that can
compute the next closure for the given rules based on the symbol we ve just seen.

Response:
def goto(self, rules, symbol):
        """Computes the next closure for rules based on the symbol we got.

        Args:
            rules - an iterable of DottedRules
            symbol - a string denoting the symbol we've just seen

        Returns: frozenset of DottedRules
        """
        return self.closure(
            {rule.move_dot() for rule in rules
             if not rule.at_end and rule.rhs[rule.pos] == symbol},
        )


Instruction:
Explain what the following Python 3 code does
def closure(self, rules):
        closure = set()

        todo = set(rules)
        while todo:
            rule = todo.pop()
            closure.add(rule)

            # If the dot is at the end, there's no need to process it.
            if rule.at_end:
                continue

            symbol = rule.rhs[rule.pos]
            for production in self.nonterminals[symbol]:
                for first in self.first(rule.rest):
                    if EPSILON in production.rhs:
                        # Move immediately to the end if the production
                        # goes to epsilon
                        new_rule = DottedRule(production, 1, first)
                    else:
                        new_rule = DottedRule(production, 0, first)

                    if new_rule not in closure:
                        todo.add(new_rule)

        return frozenset(closure)

Response: Fills out the entire closure based on some initial dotted rules.


Instruction:
Can you generate the documentation for the following Python 3 function
def closures(self):
        initial = self.initial_closure()
        closures = collections.OrderedDict()
        goto = collections.defaultdict(dict)

        todo = set([initial])
        while todo:
            closure = todo.pop()
            closures[closure] = closure

            symbols = {rule.rhs[rule.pos] for rule in closure
                       if not rule.at_end}
            for symbol in symbols:
                next_closure = self.goto(closure, symbol)

                if next_closure in closures or next_closure in todo:
                    next_closure = (closures.get(next_closure)
                                    or todo.get(next_closure))
                else:
                    closures[next_closure] = next_closure
                    todo.add(next_closure)

                goto[closure][symbol] = next_closure

        return initial, closures, goto

Response: Computes all LR1 closures for the grammar.


Instruction:
Can you create a Python 3 function that
initializes the extension for the given app.

Response:
def init_app(self, app):
        """Initializes Journey extension

        :param app: App passed from constructor or directly to init_app
        :raises:
            - NoBundlesAttached if no bundles has been attached attached

        """

        if len(self._attached_bundles) == 0:
            raise NoBundlesAttached("At least one bundle must be attached before initializing Journey")

        for bundle in self._attached_bundles:
            processed_bundle = {
                'path': bundle.path,
                'description': bundle.description,
                'blueprints': []
            }

            for (bp, description) in bundle.blueprints:
                # Register the BP
                blueprint = self._register_blueprint(app, bp, bundle.path,
                                                     self.get_bp_path(bp), description)

                # Finally, attach the blueprints to its parent
                processed_bundle['blueprints'].append(blueprint)

            self._registered_bundles.append(processed_bundle)


Instruction:
Write a Python 3 script for
returning a tuple containing endpoint path and allowed methods for each route

Response:
def routes_simple(self):
        """Returns simple info about registered blueprints

        :return: Tuple containing endpoint, path and allowed methods for each route
        """

        routes = []

        for bundle in self._registered_bundles:
            bundle_path = bundle['path']
            for blueprint in bundle['blueprints']:
                bp_path = blueprint['path']
                for child in blueprint['routes']:
                    routes.append(
                        (
                            child['endpoint'],
                            bundle_path + bp_path + child['path'],
                            child['methods']
                        )
                    )

        return routes


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _bundle_exists(self, path):

        for attached_bundle in self._attached_bundles:
            if path == attached_bundle.path:
                return True

        return False

Response: Checks if a bundle exists at the provided path


Instruction:
Here you have a function in Python 3, explain what it does
def attach_bundle(self, bundle):

        if not isinstance(bundle, BlueprintBundle):
            raise IncompatibleBundle('BlueprintBundle object passed to attach_bundle must be of type {0}'
                                     .format(BlueprintBundle))
        elif len(bundle.blueprints) == 0:
            raise MissingBlueprints("Bundles must contain at least one flask.Blueprint")
        elif self._bundle_exists(bundle.path):
            raise ConflictingPath("Duplicate bundle path {0}".format(bundle.path))
        elif self._journey_path == bundle.path == '/':
            raise ConflictingPath("Bundle path and Journey path cannot both be {0}".format(bundle.path))

        self._attached_bundles.append(bundle)

Response: Attaches a bundle object to the journey.


Instruction:
How would you explain what the following Python 3 function does
def _register_blueprint(self, app, bp, bundle_path, child_path, description):

        base_path = sanitize_path(self._journey_path + bundle_path + child_path)

        app.register_blueprint(bp, url_prefix=base_path)

        return {
            'name': bp.name,
            'path': child_path,
            'import_name': bp.import_name,
            'description': description,
            'routes': self.get_blueprint_routes(app, base_path)
        }

Response: Register and return info about the blueprint


Instruction:
How would you code a function in Python 3 to
return detailed information about registered blueprint routes matching the base_path

Response:
def get_blueprint_routes(app, base_path):
        """Returns detailed information about registered blueprint routes matching the `BlueprintBundle` path

        :param app: App instance to obtain rules from
        :param base_path: Base path to return detailed route info for
        :return: List of route detail dicts
        """

        routes = []

        for child in app.url_map.iter_rules():
            if child.rule.startswith(base_path):
                relative_path = child.rule[len(base_path):]
                routes.append({
                    'path': relative_path,
                    'endpoint': child.endpoint,
                    'methods': list(child.methods)
                })

        return routes


Instruction:
Write a Python 3 script for
computing the precedence of a terminal and production.

Response:
def compute_precedence(terminals, productions, precedence_levels):
        """Computes the precedence of terminal and production.

        The precedence of a terminal is it's level in the PRECEDENCE tuple. For
        a production, the precedence is the right-most terminal (if it exists).
        The default precedence is DEFAULT_PREC - (LEFT, 0).

        Returns:
            precedence - dict[terminal | production] = (assoc, level)

        """
        precedence = collections.OrderedDict()

        for terminal in terminals:
            precedence[terminal] = DEFAULT_PREC

        level_precs = range(len(precedence_levels), 0, -1)
        for i, level in zip(level_precs, precedence_levels):
            assoc = level[0]
            for symbol in level[1:]:
                precedence[symbol] = (assoc, i)

        for production, prec_symbol in productions:
            if prec_symbol is None:
                prod_terminals = [symbol for symbol in production.rhs
                                  if symbol in terminals] or [None]
                precedence[production] = precedence.get(prod_terminals[-1],
                                                        DEFAULT_PREC)
            else:
                precedence[production] = precedence.get(prec_symbol,
                                                        DEFAULT_PREC)

        return precedence


Instruction:
Here you have a function in Python 3, explain what it does
def make_tables(grammar, precedence):
        ACTION = {}
        GOTO = {}

        labels = {}

        def get_label(closure):
            if closure not in labels:
                labels[closure] = len(labels)
            return labels[closure]

        def resolve_shift_reduce(lookahead, s_action, r_action):
            s_assoc, s_level = precedence[lookahead]
            r_assoc, r_level = precedence[r_action[1]]

            if s_level < r_level:
                return r_action
            elif s_level == r_level and r_assoc == LEFT:
                return r_action
            else:
                return s_action

        initial, closures, goto = grammar.closures()
        for closure in closures:
            label = get_label(closure)

            for rule in closure:
                new_action, lookahead = None, rule.lookahead

                if not rule.at_end:
                    symbol = rule.rhs[rule.pos]
                    is_terminal = symbol in grammar.terminals
                    has_goto = symbol in goto[closure]
                    if is_terminal and has_goto:
                        next_state = get_label(goto[closure][symbol])
                        new_action, lookahead = ('shift', next_state), symbol
                elif rule.production == grammar.start and rule.at_end:
                    new_action = ('accept',)
                elif rule.at_end:
                    new_action = ('reduce', rule.production)

                if new_action is None:
                    continue

                prev_action = ACTION.get((label, lookahead))
                if prev_action is None or prev_action == new_action:
                    ACTION[label, lookahead] = new_action
                else:
                    types = (prev_action[0], new_action[0])
                    if types == ('shift', 'reduce'):
                        chosen = resolve_shift_reduce(lookahead,
                                                      prev_action,
                                                      new_action)
                    elif types == ('reduce', 'shift'):
                        chosen = resolve_shift_reduce(lookahead,
                                                      new_action,
                                                      prev_action)
                    else:
                        raise TableConflictError(prev_action, new_action)

                    ACTION[label, lookahead] = chosen

            for symbol in grammar.nonterminals:
                if symbol in goto[closure]:
                    GOTO[label, symbol] = get_label(goto[closure][symbol])

        return get_label(initial), ACTION, GOTO

Response: Generates the ACTION and GOTO tables for the grammar.


Instruction:
Here you have a function in Python 3, explain what it does
def expr(s):
    if isinstance(s, Expr): return s
    if isnumber(s): return Expr(s)
    ## Replace the alternative spellings of operators with canonical spellings
    s = s.replace('==>', '>>').replace('<==', '<<')
    s = s.replace('<=>', '%').replace('=/=', '^')
    ## Replace a symbol or number, such as 'P' with 'Expr("P")'
    s = re.sub(r'([a-zA-Z0-9_.]+)', r'Expr("\1")', s)
    ## Now eval the string.  (A security hole; do not use with an adversary.)
    return eval(s, {'Expr':Expr})

Response: Create an Expr representing a logic expression.


Instruction:
Can you implement a function in Python 3 that
returns a set of the variables in expression s.

Response:
def variables(s):
    """Return a set of the variables in expression s.
    >>> ppset(variables(F(x, A, y)))
    set([x, y])
    >>> ppset(variables(F(G(x), z)))
    set([x, z])
    >>> ppset(variables(expr('F(x, x) & G(x, y) & H(y, z) & R(A, z, z)')))
    set([x, y, z])
    """
    result = set([])
    def walk(s):
        if is_variable(s):
            result.add(s)
        else:
            for arg in s.args:
                walk(arg)
    walk(s)
    return result


Instruction:
Implement a Python 3 function for
returning True if exprs s is of the form A B... C == > D where all literals are positive.

Response:
def is_definite_clause(s):
    """returns True for exprs s of the form A & B & ... & C ==> D,
    where all literals are positive.  In clause form, this is
    ~A | ~B | ... | ~C | D, where exactly one clause is positive.
    >>> is_definite_clause(expr('Farmer(Mac)'))
    True
    >>> is_definite_clause(expr('~Farmer(Mac)'))
    False
    >>> is_definite_clause(expr('(Farmer(f) & Rabbit(r)) ==> Hates(f, r)'))
    True
    >>> is_definite_clause(expr('(Farmer(f) & ~Rabbit(r)) ==> Hates(f, r)'))
    False
    >>> is_definite_clause(expr('(Farmer(f) | Rabbit(r)) ==> Hates(f, r)'))
    False
    """
    if is_symbol(s.op):
        return True
    elif s.op == '>>':
        antecedent, consequent = s.args
        return (is_symbol(consequent.op)
                and every(lambda arg: is_symbol(arg.op), conjuncts(antecedent)))
    else:
        return False


Instruction:
Can you write a function in Python 3 where it
returns the antecedents and the consequent of a definite clause.

Response:
def parse_definite_clause(s):
    "Return the antecedents and the consequent of a definite clause."
    assert is_definite_clause(s)
    if is_symbol(s.op):
        return [], s
    else:
        antecedent, consequent = s.args
        return conjuncts(antecedent), consequent


Instruction:
Create a Python 3 function for
doing kb entail the sentence alpha? Use truth tables. For propositional kb s and sentences.

Response:
def tt_entails(kb, alpha):
    """Does kb entail the sentence alpha? Use truth tables. For propositional
    kb's and sentences. [Fig. 7.10]
    >>> tt_entails(expr('P & Q'), expr('Q'))
    True
    """
    assert not variables(alpha)
    return tt_check_all(kb, alpha, prop_symbols(kb & alpha), {})


Instruction:
Can you tell what is the following Python 3 function doing
def tt_check_all(kb, alpha, symbols, model):
    "Auxiliary routine to implement tt_entails."
    if not symbols:
        if pl_true(kb, model):
            result = pl_true(alpha, model)
            assert result in (True, False)
            return result
        else:
            return True
    else:
        P, rest = symbols[0], symbols[1:]
        return (tt_check_all(kb, alpha, rest, extend(model, P, True)) and
                tt_check_all(kb, alpha, rest, extend(model, P, False)))

Response: Auxiliary routine to implement tt_entails.


Instruction:
Given the following Python 3 function, write the documentation
def prop_symbols(x):
    "Return a list of all propositional symbols in x."
    if not isinstance(x, Expr):
        return []
    elif is_prop_symbol(x.op):
        return [x]
    else:
        return list(set(symbol for arg in x.args
                        for symbol in prop_symbols(arg)))

Response: Return a list of all propositional symbols in x.


Instruction:
Create a Python 3 function for
returning True if the propositional logic expression is true in the model and False if it is false.

Response:
def pl_true(exp, model={}):
    """Return True if the propositional logic expression is true in the model,
    and False if it is false. If the model does not specify the value for
    every proposition, this may return None to indicate 'not obvious';
    this may happen even when the expression is tautological."""
    op, args = exp.op, exp.args
    if exp == TRUE:
        return True
    elif exp == FALSE:
        return False
    elif is_prop_symbol(op):
        return model.get(exp)
    elif op == '~':
        p = pl_true(args[0], model)
        if p is None: return None
        else: return not p
    elif op == '|':
        result = False
        for arg in args:
            p = pl_true(arg, model)
            if p is True: return True
            if p is None: result = None
        return result
    elif op == '&':
        result = True
        for arg in args:
            p = pl_true(arg, model)
            if p is False: return False
            if p is None: result = None
        return result
    p, q = args
    if op == '>>':
        return pl_true(~p | q, model)
    elif op == '<<':
        return pl_true(p | ~q, model)
    pt = pl_true(p, model)
    if pt is None: return None
    qt = pl_true(q, model)
    if qt is None: return None
    if op == '<=>':
        return pt == qt
    elif op == '^':
        return pt != qt
    else:
        raise ValueError, "illegal operator in logic expression" + str(exp)


Instruction:
Implement a Python 3 function for
converting a propositional logical sentence s to conjunctive normal form.

Response:
def to_cnf(s):
    """Convert a propositional logical sentence s to conjunctive normal form.
    That is, to the form ((A | ~B | ...) & (B | C | ...) & ...) [p. 253]
    >>> to_cnf("~(B|C)")
    (~B & ~C)
    >>> to_cnf("B <=> (P1|P2)")
    ((~P1 | B) & (~P2 | B) & (P1 | P2 | ~B))
    >>> to_cnf("a | (b & c) | d")
    ((b | a | d) & (c | a | d))
    >>> to_cnf("A & (B | (D & E))")
    (A & (D | B) & (E | B))
    >>> to_cnf("A | (B | (C | (D & E)))")
    ((D | A | B | C) & (E | A | B | C))
    """
    if isinstance(s, str): s = expr(s)
    s = eliminate_implications(s) # Steps 1, 2 from p. 253
    s = move_not_inwards(s) # Step 3
    return distribute_and_over_or(s)


Instruction:
Can you implement a function in Python 3 that
returns an Expr that is equivalent to s but has only & and | and ~ as logical operators.

Response:
def eliminate_implications(s):
    """Change >>, <<, and <=> into &, |, and ~. That is, return an Expr
    that is equivalent to s, but has only &, |, and ~ as logical operators.
    >>> eliminate_implications(A >> (~B << C))
    ((~B | ~C) | ~A)
    >>> eliminate_implications(A ^ B)
    ((A & ~B) | (~A & B))
    """
    if not s.args or is_symbol(s.op): return s     ## (Atoms are unchanged.)
    args = map(eliminate_implications, s.args)
    a, b = args[0], args[-1]
    if s.op == '>>':
        return (b | ~a)
    elif s.op == '<<':
        return (a | ~b)
    elif s.op == '<=>':
        return (a | ~b) & (b | ~a)
    elif s.op == '^':
        assert len(args) == 2   ## TODO: relax this restriction
        return (a & ~b) | (~a & b)
    else:
        assert s.op in ('&', '|', '~')
        return Expr(s.op, *args)


Instruction:
How would you implement a function in Python 3 that
rewrites sentence s by moving negation sign inward.

Response:
def move_not_inwards(s):
    """Rewrite sentence s by moving negation sign inward.
    >>> move_not_inwards(~(A | B))
    (~A & ~B)
    >>> move_not_inwards(~(A & B))
    (~A | ~B)
    >>> move_not_inwards(~(~(A | ~B) | ~~C))
    ((A | ~B) & ~C)
    """
    if s.op == '~':
        NOT = lambda b: move_not_inwards(~b)
        a = s.args[0]
        if a.op == '~': return move_not_inwards(a.args[0]) # ~~A ==> A
        if a.op =='&': return associate('|', map(NOT, a.args))
        if a.op =='|': return associate('&', map(NOT, a.args))
        return s
    elif is_symbol(s.op) or not s.args:
        return s
    else:
        return Expr(s.op, *map(move_not_inwards, s.args))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def distribute_and_over_or(s):
    if s.op == '|':
        s = associate('|', s.args)
        if s.op != '|':
            return distribute_and_over_or(s)
        if len(s.args) == 0:
            return FALSE
        if len(s.args) == 1:
            return distribute_and_over_or(s.args[0])
        conj = find_if((lambda d: d.op == '&'), s.args)
        if not conj:
            return s
        others = [a for a in s.args if a is not conj]
        rest = associate('|', others)
        return associate('&', [distribute_and_over_or(c|rest)
                               for c in conj.args])
    elif s.op == '&':
        return associate('&', map(distribute_and_over_or, s.args))
    else:
        return s

Response:
Given a sentence s consisting of conjunctions and disjunctions
    of literals return an equivalent sentence in CNF.


Instruction:
Make a summary of the following Python 3 code
def associate(op, args):
    args = dissociate(op, args)
    if len(args) == 0:
        return _op_identity[op]
    elif len(args) == 1:
        return args[0]
    else:
        return Expr(op, *args)

Response:
Given an associative op return an expression with the same
    meaning as Expr ( op * args


Instruction:
Given the following Python 3 function, write the documentation
def dissociate(op, args):
    result = []
    def collect(subargs):
        for arg in subargs:
            if arg.op == op: collect(arg.args)
            else: result.append(arg)
    collect(args)
    return result

Response: Given an associative op return a flattened list result such that Expr ( op args ) means the same as Expr ( op args ).


Instruction:
How would you explain what the following Python 3 function does
def pl_resolution(KB, alpha):
    "Propositional-logic resolution: say if alpha follows from KB. [Fig. 7.12]"
    clauses = KB.clauses + conjuncts(to_cnf(~alpha))
    new = set()
    while True:
        n = len(clauses)
        pairs = [(clauses[i], clauses[j])
                 for i in range(n) for j in range(i+1, n)]
        for (ci, cj) in pairs:
            resolvents = pl_resolve(ci, cj)
            if FALSE in resolvents: return True
            new = new.union(set(resolvents))
        if new.issubset(set(clauses)): return False
        for c in new:
            if c not in clauses: clauses.append(c)

Response: Propositional - logic resolution : say if alpha follows from KB. [ Fig. 7. 12 ]


Instruction:
Can you write a function in Python 3 where it
returns all clauses that can be obtained by resolving clauses ci and cj.

Response:
def pl_resolve(ci, cj):
    """Return all clauses that can be obtained by resolving clauses ci and cj.
    >>> for res in pl_resolve(to_cnf(A|B|C), to_cnf(~B|~C|F)):
    ...    ppset(disjuncts(res))
    set([A, C, F, ~C])
    set([A, B, F, ~B])
    """
    clauses = []
    for di in disjuncts(ci):
        for dj in disjuncts(cj):
            if di == ~dj or ~di == dj:
                dnew = unique(removeall(di, disjuncts(ci)) +
                              removeall(dj, disjuncts(cj)))
                clauses.append(associate('|', dnew))
    return clauses


Instruction:
Make a summary of the following Python 3 code
def pl_fc_entails(KB, q):
    count = dict([(c, len(conjuncts(c.args[0]))) for c in KB.clauses
                                                 if c.op == '>>'])
    inferred = DefaultDict(False)
    agenda = [s for s in KB.clauses if is_prop_symbol(s.op)]
    while agenda:
        p = agenda.pop()
        if p == q: return True
        if not inferred[p]:
            inferred[p] = True
            for c in KB.clauses_with_premise(p):
                count[c] -= 1
                if count[c] == 0:
                    agenda.append(c.args[1])
    return False

Response: Return True if a PropDefiniteKB entails symbol q.


Instruction:
Can you tell what is the following Python 3 function doing
def dpll_satisfiable(s):
    clauses = conjuncts(to_cnf(s))
    symbols = prop_symbols(s)
    return dpll(clauses, symbols, {})

Response: Check satisfiability of a propositional sentence.


Instruction:
Implement a Python 3 function for
seeing if the clauses are true in a partial model.

Response:
def dpll(clauses, symbols, model):
    "See if the clauses are true in a partial model."
    unknown_clauses = [] ## clauses with an unknown truth value
    for c in clauses:
        val =  pl_true(c, model)
        if val == False:
            return False
        if val != True:
            unknown_clauses.append(c)
    if not unknown_clauses:
        return model
    P, value = find_pure_symbol(symbols, unknown_clauses)
    if P:
        return dpll(clauses, removeall(P, symbols), extend(model, P, value))
    P, value = find_unit_clause(clauses, model)
    if P:
        return dpll(clauses, removeall(P, symbols), extend(model, P, value))
    P, symbols = symbols[0], symbols[1:]
    return (dpll(clauses, symbols, extend(model, P, True)) or
            dpll(clauses, symbols, extend(model, P, False)))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def find_pure_symbol(symbols, clauses):
    for s in symbols:
        found_pos, found_neg = False, False
        for c in clauses:
            if not found_pos and s in disjuncts(c): found_pos = True
            if not found_neg and ~s in disjuncts(c): found_neg = True
        if found_pos != found_neg: return s, found_pos
    return None, None

Response:
Find a symbol and its value if it appears only as a positive literal
    or only as a negative literal
   .


Instruction:
Can you tell what is the following Python 3 function doing
def find_unit_clause(clauses, model):
    for clause in clauses:
        P, value = unit_clause_assign(clause, model)
        if P: return P, value
    return None, None

Response:
Find a forced assignment if possible from a clause with only 1
    variable not bound in the model.


Instruction:
Can you create a Python 3 function that
returns a single variable and value pair that makes clause true in the model if possible.

Response:
def unit_clause_assign(clause, model):
    """Return a single variable/value pair that makes clause true in
    the model, if possible.
    >>> unit_clause_assign(A|B|C, {A:True})
    (None, None)
    >>> unit_clause_assign(B|~C, {A:True})
    (None, None)
    >>> unit_clause_assign(~A|~B, {A:True})
    (B, False)
    """
    P, value = None, None
    for literal in disjuncts(clause):
        sym, positive = inspect_literal(literal)
        if sym in model:
            if model[sym] == positive:
                return None, None  # clause already True
        elif P:
            return None, None      # more than 1 unbound variable
        else:
            P, value = sym, positive
    return P, value


Instruction:
Can you tell what is the following Python 3 function doing
def SAT_plan(init, transition, goal, t_max, SAT_solver=dpll_satisfiable):
    "[Fig. 7.22]"
    for t in range(t_max):
        cnf = translate_to_SAT(init, transition, goal, t)
        model = SAT_solver(cnf)
        if model is not False:
            return extract_solution(model)
    return None

Response: Given a set of initial states and a set of goal states return the SAT plan.


Instruction:
Can you implement a function in Python 3 that
unifies expressions x y with substitution s ; return a substitution that would make x y equal or None if x and y can not be unified.

Response:
def unify(x, y, s):
    """Unify expressions x,y with substitution s; return a substitution that
    would make x,y equal, or None if x,y can not unify. x and y can be
    variables (e.g. Expr('x')), constants, lists, or Exprs. [Fig. 9.1]
    >>> ppsubst(unify(x + y, y + C, {}))
    {x: y, y: C}
    """
    if s is None:
        return None
    elif x == y:
        return s
    elif is_variable(x):
        return unify_var(x, y, s)
    elif is_variable(y):
        return unify_var(y, x, s)
    elif isinstance(x, Expr) and isinstance(y, Expr):
        return unify(x.args, y.args, unify(x.op, y.op, s))
    elif isinstance(x, str) or isinstance(y, str):
        return None
    elif issequence(x) and issequence(y) and len(x) == len(y):
        if not x: return s
        return unify(x[1:], y[1:], unify(x[0], y[0], s))
    else:
        return None


Instruction:
Can you generate the documentation for the following Python 3 function
def occur_check(var, x, s):
    if var == x:
        return True
    elif is_variable(x) and x in s:
        return occur_check(var, s[x], s)
    elif isinstance(x, Expr):
        return (occur_check(var, x.op, s) or
                occur_check(var, x.args, s))
    elif isinstance(x, (list, tuple)):
        return some(lambda element: occur_check(var, element, s), x)
    else:
        return False

Response:
Return true if variable var occurs anywhere in x
    or in subst ( s x ).


Instruction:
Write a Python 3 function that can
copy the substitution s and extend it by setting var to val

Response:
def extend(s, var, val):
    """Copy the substitution s and extend it by setting var to val;
    return copy.
    >>> ppsubst(extend({x: 1}, y, 2))
    {x: 1, y: 2}
    """
    s2 = s.copy()
    s2[var] = val
    return s2


Instruction:
How would you explain what the following Python 3 function does
def subst(s, x):
    if isinstance(x, list):
        return [subst(s, xi) for xi in x]
    elif isinstance(x, tuple):
        return tuple([subst(s, xi) for xi in x])
    elif not isinstance(x, Expr):
        return x
    elif is_var_symbol(x.op):
        return s.get(x, x)
    else:
        return Expr(x.op, *[subst(s, arg) for arg in x.args])

Response: Substitute the substitution s into the expression x.


Instruction:
Can you tell what is the following Python 3 function doing
def fol_fc_ask(KB, alpha):
    while True:
        new = {}
        for r in KB.clauses:
            ps, q = parse_definite_clause(standardize_variables(r))
            raise NotImplementedError

Response: Inefficient forward chaining for first - order logic. [ Fig. 9. 3 ]


Instruction:
Can you implement a function in Python 3 that
returns a new expression that contains all the variables in the sentence.

Response:
def standardize_variables(sentence, dic=None):
    """Replace all the variables in sentence with new variables.
    >>> e = expr('F(a, b, c) & G(c, A, 23)')
    >>> len(variables(standardize_variables(e)))
    3
    >>> variables(e).intersection(variables(standardize_variables(e)))
    set([])
    >>> is_variable(standardize_variables(expr('x')))
    True
    """
    if dic is None: dic = {}
    if not isinstance(sentence, Expr):
        return sentence
    elif is_var_symbol(sentence.op):
        if sentence in dic:
            return dic[sentence]
        else:
            v = Expr('v_%d' % standardize_variables.counter.next())
            dic[sentence] = v
            return v
    else:
        return Expr(sentence.op,
                    *[standardize_variables(a, dic) for a in sentence.args])


Instruction:
Create a Python 3 function to
return the symbolic derivative of dy and dx as an Expr.

Response:
def diff(y, x):
    """Return the symbolic derivative, dy/dx, as an Expr.
    However, you probably want to simplify the results with simp.
    >>> diff(x * x, x)
    ((x * 1) + (x * 1))
    >>> simp(diff(x * x, x))
    (2 * x)
    """
    if y == x: return ONE
    elif not y.args: return ZERO
    else:
        u, op, v = y.args[0], y.op, y.args[-1]
        if op == '+': return diff(u, x) + diff(v, x)
        elif op == '-' and len(args) == 1: return -diff(u, x)
        elif op == '-': return diff(u, x) - diff(v, x)
        elif op == '*': return u * diff(v, x) + v * diff(u, x)
        elif op == '/': return (v*diff(u, x) - u*diff(v, x)) / (v * v)
        elif op == '**' and isnumber(x.op):
            return (v * u ** (v - 1) * diff(u, x))
        elif op == '**': return (v * u ** (v - 1) * diff(u, x)
                                 + u ** v * Expr('log')(u) * diff(v, x))
        elif op == 'log': return diff(u, x) / u
        else: raise ValueError("Unknown op: %s in diff(%s, %s)" % (op, y, x))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def pretty_dict(d):
    return '{%s}' % ', '.join('%r: %r' % (k, v)
                              for k, v in sorted(d.items(), key=repr))

Response: Return dictionary d s repr but with the items sorted.


Instruction:
Given the following Python 3 function, write the documentation
def retract(self, sentence):
        "Remove the sentence's clauses from the KB."
        for c in conjuncts(to_cnf(sentence)):
            if c in self.clauses:
                self.clauses.remove(c)

Response: Remove the sentence s clauses from the KB.


Instruction:
Write a Python 3 script for
returning a list of the clauses in KB that have p in their premise.

Response:
def clauses_with_premise(self, p):
        """Return a list of the clauses in KB that have p in their premise.
        This could be cached away for O(1) speed, but we'll recompute it."""
        return [c for c in self.clauses
                if c.op == '>>' and p in conjuncts(c.args[0])]