Instruction:
Explain what the following Python 3 code does
def _process_connect_init(self):
        # INFO {...}
        line = yield self.io.read_until(_CRLF_, max_bytes=None)
        _, args = line.split(INFO_OP + _SPC_, 1)
        self._server_info = tornado.escape.json_decode((args))

        if 'max_payload' in self._server_info:
            self._max_payload_size = self._server_info["max_payload"]

        # Check whether we need to upgrade to TLS first of all
        if 'tls_required' in self._server_info and self._server_info['tls_required']:
            # Detach and prepare for upgrading the TLS connection.
            self._loop.remove_handler(self._socket.fileno())

            tls_opts = {}
            if "tls" in self.options:
                # Allow customizing the TLS version though default
                # to one that the server supports at least.
                tls_opts = self.options["tls"]

            # Rewrap using a TLS connection, can't do handshake on connect
            # as the socket is non blocking.
            self._socket = ssl.wrap_socket(
                self._socket, do_handshake_on_connect=False, **tls_opts)

            # Use the TLS stream instead from now
            self.io = tornado.iostream.SSLIOStream(self._socket)
            self.io.set_close_callback(self._process_op_err)
            self.io._do_ssl_handshake()

        # Refresh state of the parser upon reconnect.
        if self.is_reconnecting:
            self._ps.reset()

        # CONNECT then send a PING expecting a PONG to make a
        # roundtrip to the server and assert that sent commands sent
        # this far have been processed already.
        cmd = self.connect_command()
        yield self.io.write(cmd)
        yield self.io.write(PING_PROTO)

        # FIXME: Add readline timeout for these.
        next_op = yield self.io.read_until(
            _CRLF_, max_bytes=MAX_CONTROL_LINE_SIZE)
        if self.options["verbose"] and OK_OP in next_op:
            next_op = yield self.io.read_until(
                _CRLF_, max_bytes=MAX_CONTROL_LINE_SIZE)
        if ERR_OP in next_op:
            err_line = next_op.decode()
            _, err_msg = err_line.split(_SPC_, 1)
            # FIXME: Maybe handling could be more special here,
            # checking for ErrAuthorization for example.
            # yield from self._process_err(err_msg)
            raise NatsError("nats: " + err_msg.rstrip('\r\n'))

        if PONG_PROTO in next_op:
            self._status = Client.CONNECTED

        self._loop.spawn_callback(self._read_loop)
        self._pongs = []
        self._pings_outstanding = 0
        self._ping_timer = tornado.ioloop.PeriodicCallback(
            self._ping_interval, self.options["ping_interval"] * 1000)
        self._ping_timer.start()

        # Queue and flusher for coalescing writes to the server.
        self._flush_queue = tornado.queues.Queue(maxsize=1024)
        self._loop.spawn_callback(self._flusher_loop)

Response: Handles the initial part of the NATS protocol connecting to the server.


Instruction:
Here you have a function in Python 3, explain what it does
def _process_info(self, info_line):
        info = tornado.escape.json_decode(info_line.decode())

        if 'connect_urls' in info:
            if info['connect_urls']:
                connect_urls = []
                for connect_url in info['connect_urls']:
                    uri = urlparse("nats://%s" % connect_url)
                    srv = Srv(uri)
                    srv.discovered = True

                    # Filter for any similar server in the server pool already.
                    should_add = True
                    for s in self._server_pool:
                        if uri.netloc == s.uri.netloc:
                            should_add = False
                    if should_add:
                        connect_urls.append(srv)

                if self.options["dont_randomize"] is not True:
                    shuffle(connect_urls)
                for srv in connect_urls:
                    self._server_pool.append(srv)

Response:
Process INFO lines sent by the server to reconfigure client
        with latest updates from cluster.


Instruction:
Can you write a function in Python 3 where it
returns the next available server.

Response:
def _next_server(self):
        """
        Chooses next available server to connect.
        """
        if self.options["dont_randomize"]:
            server = self._server_pool.pop(0)
            self._server_pool.append(server)
        else:
            shuffle(self._server_pool)

        s = None
        for server in self._server_pool:
            if self.options["max_reconnect_attempts"] > 0 and (
                    server.reconnects >
                    self.options["max_reconnect_attempts"]):
                continue
            else:
                s = server
        return s


Instruction:
Can you generate the documentation for the following Python 3 function
def _process_op_err(self, err=None):
        if self.is_connecting or self.is_closed or self.is_reconnecting:
            return

        if self.options["allow_reconnect"] and self.is_connected:
            self._status = Client.RECONNECTING
            yield self._attempt_reconnect()
        else:
            # Transition into CLOSED state
            self._status = Client.DISCONNECTED
            self._err = err
            yield self._close(Client.CLOSED)

Response: Process errors which occured while reading the protocol.


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

        # Continue trying to connect until there is an available server
        # or bail in case there are no more available servers.
        while True:
            if len(self._server_pool) == 0:
                self._current_server = None
                raise ErrNoServers

            now = time.time()
            s = self._server_pool.pop(0)
            if self.options["max_reconnect_attempts"] > 0:
                if s.reconnects > self.options["max_reconnect_attempts"]:
                    # Discard server since already tried to reconnect too many times.
                    continue

            # Not yet exceeded max_reconnect_attempts so can still use
            # this server in the future.
            self._server_pool.append(s)
            if s.last_attempt is not None and now < s.last_attempt + self.options["reconnect_time_wait"]:
                # Backoff connecting to server if we attempted recently.
                yield tornado.gen.sleep(self.options["reconnect_time_wait"])

            try:
                yield self._server_connect(s)
                self._current_server = s
                break
            except Exception as e:
                s.last_attempt = time.time()
                s.reconnects += 1

                self._err = e
                if self._error_cb is not None:
                    self._error_cb(e)
                self._status = Client.RECONNECTING
                continue

Response: Iterates over the server pool and attempts to connect to the next available server.


Instruction:
Can you write a function in Python 3 where it
closes the connection and closes the connection.

Response:
def _close(self, status, do_callbacks=True):
        """
        Takes the status on which it should leave the connection
        and an optional boolean parameter to dispatch the disconnected
        and close callbacks if there are any.
        """
        if self.is_closed:
            self._status = status
            return
        self._status = Client.CLOSED

        # Stop background tasks
        yield self._end_flusher_loop()

        if self._ping_timer is not None and self._ping_timer.is_running():
            self._ping_timer.stop()

        if not self.io.closed():
            self.io.close()

        # Cleanup subscriptions since not reconnecting so no need
        # to replay the subscriptions anymore.
        for ssid, sub in self._subs.items():
            self._subs.pop(ssid, None)
            self._remove_subscription(sub)

        if do_callbacks:
            if self._disconnected_cb is not None:
                self._disconnected_cb()
            if self._closed_cb is not None:
                self._closed_cb()


Instruction:
Can you tell what is the following Python 3 function doing
def drain(self, sid=None):
        if self.is_draining:
            return

        if self.is_closed:
            raise ErrConnectionClosed

        if self.is_connecting or self.is_reconnecting:
            raise ErrConnectionReconnecting

        # Drain a single subscription
        if sid is not None:
            raise tornado.gen.Return(self._drain_sub(sid))

        # Start draining the subscriptions
        self._status = Client.DRAINING_SUBS

        drain_tasks = []
        for ssid, sub in self._subs.items():
            task = self._drain_sub(ssid)
            drain_tasks.append(task)

        # Wait for subscriptions to stop handling messages.
        drain_is_done = tornado.gen.multi(drain_tasks)
        try:
            yield tornado.gen.with_timeout(
                timedelta(seconds=self.options["drain_timeout"]),
                drain_is_done,
                )
        except tornado.gen.TimeoutError:
            if self._error_cb is not None:
                yield self._error_cb(ErrDrainTimeout())
        finally:
            self._status = Client.DRAINING_PUBS
            yield self.flush()
            yield self.close()

Response: Drain a single subscription or publishers.


Instruction:
Can you implement a function in Python 3 that
processes an error from the server and dispatches the error callback.

Response:
def _process_err(self, err=None):
        """
        Stores the last received error from the server and dispatches the error callback.
        """
        self.stats['errors_received'] += 1

        if err == "'Authorization Violation'":
            self._err = ErrAuthorization
        elif err == "'Slow Consumer'":
            self._err = ErrSlowConsumer
        elif err == "'Stale Connection'":
            self._err = ErrStaleConnection
        else:
            self._err = Exception(err)

        if self._error_cb is not None:
            self._error_cb(err)


Instruction:
Can you write a function in Python 3 where it
reads loop for gathering bytes from the server in a buffer containing MAX_CONTROL_LINE_SIZE and parsing them.

Response:
def _read_loop(self, data=''):
        """
        Read loop for gathering bytes from the server in a buffer
        of maximum MAX_CONTROL_LINE_SIZE, then received bytes are streamed
        to the parsing callback for processing.
        """
        while True:
            if not self.is_connected or self.is_connecting or self.io.closed():
                break

            try:
                yield self.io.read_bytes(
                    DEFAULT_READ_CHUNK_SIZE,
                    streaming_callback=self._ps.parse,
                    partial=True)
            except tornado.iostream.StreamClosedError as e:
                self._err = e
                if self._error_cb is not None and not self.is_reconnecting and not self.is_closed:
                    self._error_cb(e)
                break


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _flusher_loop(self):
        while True:
            pending = []
            pending_size = 0
            try:
                # Block and wait for the flusher to be kicked
                yield self._flush_queue.get()

                # Check whether we should bail first
                if not self.is_connected or self.is_connecting or self.io.closed():
                    break

                # Flush only when we actually have something in buffer...
                if self._pending_size > 0:
                    cmds = b''.join(self._pending)

                    # Reset pending queue and store tmp in case write fails
                    self._pending, pending = [], self._pending
                    self._pending_size, pending_size = 0, self._pending_size
                    yield self.io.write(cmds)
            except tornado.iostream.StreamBufferFullError:
                # Acumulate as pending data size and flush when possible.
                self._pending = pending + self._pending
                self._pending_size += pending_size
            except tornado.iostream.StreamClosedError as e:
                self._pending = pending + self._pending
                self._pending_size += pending_size
                yield self._process_op_err(e)

Response: Coroutine which continuously flushes pending commands to the socket.


Instruction:
Can you generate the documentation for the following Python 3 function
def _end_flusher_loop(self):
        if not self.is_connected or self.is_connecting or self.io.closed():
            if self._flush_queue is not None and self._flush_queue.empty():
                self._flush_pending(check_connected=False)
            yield tornado.gen.moment

Response: End the flusher loop.


Instruction:
Implement a function in Python 3 to
load the YAML files found in source into a dict.

Response:
def yaml_load(source, defaultdata=NO_DEFAULT):
    """merge YAML data from files found in source

    Always returns a dict. The YAML files are expected to contain some kind of
    key:value structures, possibly deeply nested. When merging, lists are
    appended and dict keys are replaced. The YAML files are read with the
    yaml.safe_load function.

    source can be a file, a dir, a list/tuple of files or a string containing
    a glob expression (with ?*[]).

    For a directory, all *.yaml files will be read in alphabetical order.

    defaultdata can be used to initialize the data.
    """
    logger = logging.getLogger(__name__)
    logger.debug("initialized with source=%s, defaultdata=%s", source, defaultdata)
    if defaultdata is NO_DEFAULT:
        data = None
    else:
        data = defaultdata
    files = []
    if type(source) is not str and len(source) == 1:
        # when called from __main source is always a list, even if it contains only one item.
        # turn into a string if it contains only one item to support our different call modes
        source = source[0]
    if type(source) is list or type(source) is tuple:
        # got a list, assume to be files
        files = source
    elif os.path.isdir(source):
        # got a dir, read all *.yaml files
        files = sorted(glob.glob(os.path.join(source, "*.yaml")))
    elif os.path.isfile(source):
        # got a single file, turn it into list to use the same code
        files = [source]
    else:
        # try to use the source as a glob
        files = sorted(glob.glob(source))
    if files:
        logger.debug("Reading %s\n", ", ".join(files))
        for yaml_file in files:
            try:
                with open(yaml_file) as f:
                    new_data = safe_load(f)
                logger.debug("YAML LOAD: %s", new_data)
            except MarkedYAMLError as e:
                logger.error("YAML Error: %s", e)
                raise YamlReaderError("YAML Error: %s" % str(e))
            if new_data is not None:
                data = data_merge(data, new_data)
    else:
        if defaultdata is NO_DEFAULT:
            logger.error("No YAML data found in %s and no default data given", source)
            raise YamlReaderError("No YAML data found in %s" % source)

    return data


Instruction:
Create a Python 3 function to
generate a widget visualization using the widget

Response:
def widget(self, which_viz='viz'):
    '''
    Generate a widget visualization using the widget. The export_viz_to_widget
    method passes the visualization JSON to the instantiated widget, which is
    returned and visualized on the front-end.
    '''
    if hasattr(self, 'widget_class') == True:
      self.widget_instance = self.widget_class(network = self.export_viz_to_widget(which_viz))

      return self.widget_instance
    else:
      print('Can not make widget because Network has no attribute widget_class')
      print('Please instantiate Network with clustergrammer_widget using: Network(clustergrammer_widget)')


Instruction:
How would you code a function in Python 3 to
date iterator returning dates in d1 < x < d2

Response:
def iterdays(self, d1, d2):
        """
        Date iterator returning dates in d1 <= x < d2
        """
        curr = datetime.datetime.combine(d1, datetime.time())
        end = datetime.datetime.combine(d2, datetime.time())
        if d1.date() == d2.date():
            yield curr
            return
        while curr < end:
            yield curr
            curr = curr + datetime.timedelta(days=1)


Instruction:
Create a Python 3 function for
returning an iterator over the weekdays between d1 and d2 excluding weekends.

Response:
def iterweekdays(self, d1, d2):
        """
        Date iterator returning dates in d1 <= x < d2, excluding weekends
        """
        for dt in self.iterdays(d1, d2):
            if not self.isweekend(dt):
                yield dt


Instruction:
Can you generate the documentation for the following Python 3 function
def iterbusinessdays(self, d1, d2):
        assert d2 >= d1
        if d1.date() == d2.date() and d2.time() < self.business_hours[0]:
            return
        first = True
        for dt in self.iterdays(d1, d2):
            if first and d1.time() > self.business_hours[1]:
                first = False
                continue
            first = False
            if not self.isweekend(dt) and not self.isholiday(dt):
                yield dt

Response: Date iterator returning dates in d1 < x < d2 excluding weekends and holidays


Instruction:
Write a Python 3 function that can
return a datetime. timedelta with the number of full business days between d1 and d2 and the business time between d1 and d2.

Response:
def businesstimedelta(self, d1, d2):
        """
        Returns a datetime.timedelta with the number of full business days
        and business time between d1 and d2
        """

        if d1 > d2:
            d1, d2, timedelta_direction = d2, d1, -1
        else:
            timedelta_direction = 1
        businessdays = self._build_spanning_datetimes(d1, d2)
        time = datetime.timedelta()

        if len(businessdays) == 0:
            # HACK: manually handle the case when d1 is after business hours while d2 is during
            if self.isduringbusinesshours(d2):
                time += d2 - datetime.datetime.combine(d2,
                                                       self.business_hours[0])

            # HACK: manually handle the case where d1 is on an earlier non-business day and d2 is after hours on a business day
            elif not self.isbusinessday(d1) and self.isbusinessday(d2):
                if d2.time() > self.business_hours[1]:
                    time += datetime.datetime.combine(
                        d2,
                        self.business_hours[1]) - datetime.datetime.combine(
                            d2, self.business_hours[0])
                elif d2.time() > self.business_hours[0]:
                    time += d2 - datetime.datetime.combine(
                        d2, self.business_hours[0])

        else:
            prev = None
            current = None
            count = 0
            for d in businessdays:
                if current is None:
                    current = d
                current = datetime.datetime.combine(d, current.time())
                if prev is not None:
                    if prev.date() != current.date():
                        time += datetime.timedelta(days=1)
                    if count == len(businessdays) - 1:
                        if current > d:
                            # We went too far
                            time -= datetime.timedelta(days=1)
                            time += self.open_hours - (current - d)
                        else:
                            time += d - current
                count += 1
                prev = current

        return time * timedelta_direction


Instruction:
Can you tell what is the following Python 3 function doing
def businesstime_hours(self, d1, d2):
        open_hours = self.open_hours.seconds / 3600
        btd = self.businesstimedelta(d1, d2)
        btd_hours = btd.seconds / 3600
        return datetime.timedelta(hours=(btd.days * open_hours + btd_hours))

Response: Returns a datetime. timedelta of business hours between d1 and d2 based on the length of the businessday


Instruction:
Can you tell what is the following Python 3 function doing
def _day_rule_matches(self, rule, dt):
        if dt.weekday() == 4:
            sat = dt + datetime.timedelta(days=1)
            if super(USFederalHolidays, self)._day_rule_matches(rule, sat):
                return True
        elif dt.weekday() == 0:
            sun = dt - datetime.timedelta(days=1)
            if super(USFederalHolidays, self)._day_rule_matches(rule, sun):
                return True
        return super(USFederalHolidays, self)._day_rule_matches(rule, dt)

Response: Return True if the rule matches the given datetime.


Instruction:
Can you implement a function in Python 3 that
does something with a cloned repo.

Response:
def act_on_cloned_repo(self, path: Union[str, pathlib.Path],
                           api) -> Optional[HookResult]:
        """Do something with a cloned repo.

        Args:
            path: Path to the repo.
            api: An instance of :py:class:`repobee.github_api.GitHubAPI`.

        Returns:
            optionally returns a HookResult namedtuple for reporting the
            outcome of the hook. May also return None, in which case no
            reporting will be performed for the hook.
        """


Instruction:
How would you implement a function in Python 3 that
generates a mapping of each student repository to a list of reviewers.

Response:
def generate_review_allocations(
            self, master_repo_name: str, students: Iterable[str],
            num_reviews: int,
            review_team_name_function: Callable[[str, str], str]
    ) -> Mapping[str, List[str]]:
        """Generate a (peer_review_team -> reviewers) mapping for each student
        repository (i.e. <student>-<master_repo_name>), where len(reviewers) =
        num_reviews.

        review_team_name_function should be used to generate review team names.
        It should be called like:

        .. code-block:: python

            review_team_name_function(master_repo_name, student)

        .. important::

            There must be strictly more students than reviewers per repo
            (`num_reviews`). Otherwise, allocation is impossible.

        Args:
            master_repo_name: Name of a master repository.
            students: Students for which to generate peer review allocations.
            num_reviews: Amount of reviews each student should perform (and
                consequently amount of reviewers per repo)
            review_team_name_function: A function that takes a master repo name
                as its first argument, and a student username as its second, and
                returns a review team name.
        Returns:
            a (peer_review_team -> reviewers) mapping for each student repository.
        """


Instruction:
Here you have a function in Python 3, explain what it does
def hset(self, hashroot, key, value):
        hroot = self.root / hashroot
        if not hroot.is_dir():
            hroot.mkdir()
        hfile = hroot / gethashfile(key)
        d = self.get(hfile, {})
        d.update( {key : value})
        self[hfile] = d

Response: set a hash entry


Instruction:
Implement a Python 3 function for
compressing category hashroot so that fast_only is True for compressed items.

Response:
def hcompress(self, hashroot):
        """ Compress category 'hashroot', so hset is fast again

        hget will fail if fast_only is True for compressed items (that were
        hset before hcompress).

        """
        hfiles = self.keys(hashroot + "/*")
        all = {}
        for f in hfiles:
            # print "using",f
            all.update(self[f])
            self.uncache(f)

        self[hashroot + '/xx'] = all
        for f in hfiles:
            p = self.root / f
            if p.name == 'xx':
                continue
            p.unlink()


Instruction:
Write a Python 3 script for
returning a list of all keys in DB or all keys matching a globpat

Response:
def keys(self, globpat = None):
        """ All keys in DB, or all keys matching a glob"""

        if globpat is None:
            files = self.root.rglob('*')
        else:
            files = self.root.glob(globpat)
        return [self._normalized(p) for p in files if p.is_file()]


Instruction:
Make a summary of the following Python 3 code
def uncache(self,*items):
        if not items:
            self.cache = {}
        for it in items:
            self.cache.pop(it,None)

Response: Removes all or specified items from the cache.


Instruction:
Write a Python 3 script for
waiting for a key to get a value from the database.

Response:
def waitget(self,key, maxwaittime = 60 ):
        """ Wait (poll) for a key to get a value

        Will wait for `maxwaittime` seconds before raising a KeyError.
        The call exits normally if the `key` field in db gets a value
        within the timeout period.

        Use this for synchronizing different processes or for ensuring
        that an unfortunately timed "db['key'] = newvalue" operation
        in another process (which causes all 'get' operation to cause a
        KeyError for the duration of pickling) won't screw up your program
        logic.
        """

        wtimes = [0.2] * 3 + [0.5] * 2 + [1]
        tries = 0
        waited = 0
        while 1:
            try:
                val = self[key]
                return val
            except KeyError:
                pass

            if waited > maxwaittime:
                raise KeyError(key)

            time.sleep(wtimes[tries])
            waited+=wtimes[tries]
            if tries < len(wtimes) -1:
                tries+=1


Instruction:
Explain what the following Python 3 code does
def hacking_todo_format(physical_line, tokens):
    # TODO(jogo): make the following doctests pass:
    #            H101: #TODO(jogo fail
    #            H101: #TODO(jogo
    # TODO(jogo): make this check docstrings as well (don't have to be at top
    # of function)
    for token_type, text, start_index, _, _ in tokens:
        if token_type == tokenize.COMMENT:
            pos = text.find('TODO')
            pos1 = text.find('TODO(')
            if (pos != pos1):
                return pos + start_index[1], "H101: Use TODO(NAME)"

Response: Check for the todo section in the file.


Instruction:
Can you implement a function in Python 3 that
checks for Apache 2. 0 license.

Response:
def hacking_has_license(physical_line, filename, lines, line_number):
    """Check for Apache 2.0 license.

    H102 license header not found
    """
    # don't work about init files for now
    # TODO(sdague): enforce license in init file if it's not empty of content
    license_found = False

    # skip files that are < 10 lines, which isn't enough for a license to fit
    # this allows us to handle empty files, as well as not fail on the Okay
    # doctests.
    if line_number is 1 and len(lines) > 10 and _project_is_apache():
        for idx, line in enumerate(lines):
            # if it's more than 10 characters in, it's probably not in the
            # header
            if 0 <= line.find('Licensed under the Apache License') < 10:
                    license_found = True
            if 0 <= line.find('SPDX-License-Identifier:') < 10:
                    license_found = True
        if not license_found:
            return (0, "H102: Apache 2.0 license header not found")


Instruction:
How would you code a function in Python 3 to
check for Apache 2. 0 license.

Response:
def hacking_has_correct_license(physical_line, filename, lines, line_number):
    """Check for Apache 2.0 license.

    H103 header does not match Apache 2.0 License notice
    """
    # don't work about init files for now
    # TODO(sdague): enforce license in init file if it's not empty of content

    # skip files that are < 10 lines, which isn't enough for a license to fit
    # this allows us to handle empty files, as well as not fail on the Okay
    # doctests.
    if line_number is 1 and len(lines) > 10 and _project_is_apache():
        for idx, line in enumerate(lines):
            column = line.find('Licensed under the Apache License')
            if (0 < column < 10 and not
                    _check_for_exact_apache(idx, lines)):
                if (line.find('SPDX-License-Identifier: Apache-2.0') <= 0):
                    return (column, "H103: Header does not match Apache 2.0 "
                            "License notice")


Instruction:
Write a Python 3 script for
checking for empty files with only comments H104 empty file with only comments

Response:
def hacking_has_only_comments(physical_line, filename, lines, line_number):
    """Check for empty files with only comments

    H104 empty file with only comments
    """
    if line_number == 1 and all(map(EMPTY_LINE_RE.match, lines)):
        return (0, "H104: File contains nothing but comments")


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

    global _is_apache_cache
    if _is_apache_cache is not None:
        return _is_apache_cache
    license_files = ["LICENSE"]
    for filename in license_files:
        try:
            with open(filename, "r") as file:
                for line in file:
                    if re.search('Apache License', line):
                        _is_apache_cache = True
                        return True
        except IOError:
            pass
    _is_apache_cache = False
    return False

Response: Determine if a project is Apache.


Instruction:
Implement a Python 3 function for
checking if the Apache 2. 0 license header matches the contents of the source tree.

Response:
def _check_for_exact_apache(start, lines):
    """Check for the Apache 2.0 license header.

    We strip all the newlines and extra spaces so this license string
    should work regardless of indentation in the file.
    """
    APACHE2 = """
Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License."""

    # out of all the formatting I've seen, a 12 line version seems to be the
    # longest in the source tree. So just take the 12 lines starting with where
    # the Apache starting words were found, strip all the '#' and collapse the
    # spaces.
    content = ''.join(lines[start:(start + 12)])
    content = re.sub('\#', '', content)
    content = re.sub('\s+', ' ', content).strip()
    stripped_apache2 = re.sub('\s+', ' ', APACHE2).strip()

    if stripped_apache2 in content:
        return True
    else:
        print("<license>!=<apache2>:\n'%s' !=\n'%s'" %
              (content, stripped_apache2))
        return False


Instruction:
Can you generate the documentation for the following Python 3 function
def hacking_no_author_tags(physical_line):
    for regex in AUTHOR_TAG_RE:
        if regex.match(physical_line):
            physical_line = physical_line.lower()
            pos = physical_line.find('moduleauthor')
            if pos < 0:
                pos = physical_line.find('author')
            return (pos, "H105: Don't use author tags")

Response: Check that no author tags are used. H105 don t use author tags


Instruction:
How would you explain what the following Python 3 function does
def hacking_python3x_except_compatible(logical_line, noqa):
    if noqa:
        return

    def is_old_style_except(logical_line):
        return (',' in logical_line and
                ')' not in logical_line.rpartition(',')[2])

    if (logical_line.startswith("except ") and
            logical_line.endswith(':') and
            is_old_style_except(logical_line)):
        yield 0, "H231: Python 3.x incompatible 'except x,y:' construct"

Response: Check for Python 3. x compatible except statements.


Instruction:
How would you explain what the following Python 3 function does
def hacking_python3x_octal_literals(logical_line, tokens, noqa):
    if noqa:
        return

    for token_type, text, _, _, _ in tokens:
        if token_type == tokenize.NUMBER:
            match = RE_OCTAL.match(text)
            if match:
                yield 0, ("H232: Python 3.x incompatible octal %s should be "
                          "written as 0o%s " %
                          (match.group(0)[1:], match.group(1)))

Response: Check for octal literals in Python 3. x compatible form.


Instruction:
Can you generate the documentation for the following Python 3 function
def hacking_no_assert_equals(logical_line, tokens, noqa):

    if noqa:
        return
    for token_type, text, start_index, _, _ in tokens:

        if token_type == tokenize.NAME:
            if text == "assertEquals" or text == "assertNotEquals":
                yield (start_index[1],
                       "H234: %s is deprecated, use %s" % (text, text[:-1]))

Response: r Check for deprecated assertEquals and assertNotEquals.


Instruction:
Can you write a function in Python 3 where it
checks for removed modules in Python 3.

Response:
def hacking_no_removed_module(logical_line, noqa):
    r"""Check for removed modules in Python 3.

    Examples:
    Okay: from os import path
    Okay: from os import path as p
    Okay: from os import (path as p)
    Okay: import os.path
    H237: import thread
    Okay: import thread  # noqa
    H237: import commands
    H237: import md5 as std_md5
    """
    if noqa:
        return
    line = core.import_normalize(logical_line.strip())
    if line and line.split()[0] == 'import':
        module_name = line.split()[1].split('.')[0]
        if module_name in removed_modules:
            yield 0, ("H237: module %s is "
                      "removed in Python 3" % module_name)


Instruction:
Here you have a function in Python 3, explain what it does
def hacking_no_old_style_class(logical_line, noqa):
    if noqa:
        return
    line = core.import_normalize(logical_line.strip())
    if line.startswith("class ") and not RE_NEW_STYLE_CLASS.match(line):
            yield (0, "H238: old style class declaration, "
                   "use new style (inherit from `object`)")

Response: Check for old style classes.


Instruction:
Write a Python 3 function for
checking for vim editor configuration in source files.

Response:
def no_vim_headers(physical_line, line_number, lines):
    r"""Check for vim editor configuration in source files.

    By default vim modelines can only appear in the first or
    last 5 lines of a source file.

    Examples:
    H106: # vim: set tabstop=4 shiftwidth=4\n#\n#\n#\n#\n#
    H106: # Lic\n# vim: set tabstop=4 shiftwidth=4\n#\n#\n#\n#\n#
    H106: # Lic\n#\n#\n#\n#\n#\n#\n#\n#\n# vim: set tabstop=4 shiftwidth=4
    Okay: # Lic\n#\n#\n#\n#\n#\n#\n#
    Okay: # viminal hill is located in Rome
    Okay: # vim, ze nemluvis cesky
    """
    if ((line_number <= 5 or line_number > len(lines) - 5) and
            vim_header_re.match(physical_line)):
        return 0, "H106: Don't put vim configuration in source files"


Instruction:
Here you have a function in Python 3, explain what it does
def hacking_import_rules(logical_line, filename, noqa):
    # TODO(jogo): make the following doctests pass:
    #            H301: import os, sys
    # TODO(mordred: We need to split this into different checks so that they
    # can be disabled by command line switches properly

    if noqa:
        return

    split_line = logical_line.split()
    split_line_len = len(split_line)
    if (split_line_len > 1 and split_line[0] in ('import', 'from') and
            not core.is_import_exception(split_line[1])):
        pos = logical_line.find(',')
        if pos != -1:
            if split_line[0] == 'from':
                yield pos, "H301: one import per line"
        pos = logical_line.find('*')
        if pos != -1:
            yield pos, "H303: No wildcard (*) import."
            return

        if split_line_len in (2, 4, 6) and split_line[1] != "__future__":
            if 'from' == split_line[0] and split_line_len > 3:
                mod = '.'.join((split_line[1], split_line[3]))
                if core.is_import_exception(mod):
                    return
                if RE_RELATIVE_IMPORT.search(logical_line):
                    yield logical_line.find('.'), (
                        "H304: No relative imports. '%s' is a relative import"
                        % logical_line)
                    return

Response: r Check for imports.


Instruction:
How would you explain what the following Python 3 function does
def is_import_exception(mod):
    return (mod in IMPORT_EXCEPTIONS or
            any(mod.startswith(m + '.') for m in IMPORT_EXCEPTIONS))

Response: Check if import has been whitelisted.


Instruction:
Make a summary of the following Python 3 code
def run(self):
        if self.name and self.name not in self.__class__._has_run:
            self.__class__._has_run.add(self.name)
            ret = self.run_once()
            if ret is not None:
                yield ret

Response: A context manager that runs the check once.


Instruction:
Can you tell what is the following Python 3 function doing
def hacking_no_locals(logical_line, tokens, noqa):
    if noqa:
        return
    for_formatting = False
    for token_type, text, start, _, _ in tokens:
        if text == "%" and token_type == tokenize.OP:
            for_formatting = True
        if for_formatting and token_type == tokenize.NAME:
            for k, v in LOCALS_TEXT_MAP.items():
                if text == k and v in logical_line:
                    yield (start[1],
                           "H501: Do not use %s for string formatting" % v)

Response: Do not use locals or self. __dict__ for string formatting.


Instruction:
Can you write a function in Python 3 where it
checks for docstring not starting with space.

Response:
def hacking_docstring_start_space(physical_line, previous_logical, tokens):
    r"""Check for docstring not starting with space.

    OpenStack HACKING guide recommendation for docstring:
    Docstring should not start with space

    Okay: def foo():\n    '''This is good.'''
    Okay: def foo():\n    r'''This is good.'''
    Okay: def foo():\n    a = ''' This is not a docstring.'''
    Okay: def foo():\n    pass\n    ''' This is not.'''
    H401: def foo():\n    ''' This is not.'''
    H401: def foo():\n    r''' This is not.'''
    """
    docstring = is_docstring(tokens, previous_logical)
    if docstring:
        start, start_triple = _find_first_of(docstring, START_DOCSTRING_TRIPLE)
        if docstring[len(start_triple)] == ' ':
            # docstrings get tokenized on the last line of the docstring, so
            # we don't know the exact position.
            return (0, "H401: docstring should not start with"
                    " a space")


Instruction:
Here you have a function in Python 3, explain what it does
def hacking_docstring_multiline_end(physical_line, previous_logical, tokens):
    docstring = is_docstring(tokens, previous_logical)
    if docstring:
        if '\n' not in docstring:
            # not a multi line
            return
        else:
            last_line = docstring.split('\n')[-1]
        pos = max(last_line.rfind(i) for i in END_DOCSTRING_TRIPLE)
        if len(last_line[:pos].strip()) > 0:
            # Something before the end docstring triple
            return (pos,
                    "H403: multi line docstrings should end on a new line")

Response: Check multi line docstring end.


Instruction:
Write a Python 3 function for
checking multi line docstring starts immediately with summary.

Response:
def hacking_docstring_multiline_start(physical_line, previous_logical, tokens):
    r"""Check multi line docstring starts immediately with summary.

    OpenStack HACKING guide recommendation for docstring:
    Docstring should start with a one-line summary, less than 80 characters.

    Okay: '''foobar\n\nfoo\nbar\n'''
    Okay: def foo():\n    a = '''\nnot\na docstring\n'''
    H404: def foo():\n    '''\nfoo\nbar\n'''\n\n
    H404: def foo():\n    r'''\nfoo\nbar\n'''\n\n
    """
    docstring = is_docstring(tokens, previous_logical)
    if docstring:
        if '\n' not in docstring:
            # single line docstring
            return
        start, start_triple = _find_first_of(docstring, START_DOCSTRING_TRIPLE)
        lines = docstring.split('\n')
        if lines[0].strip() == start_triple:
            # docstrings get tokenized on the last line of the docstring, so
            # we don't know the exact position.
            return (0, "H404: multi line docstring "
                    "should start without a leading new line")


Instruction:
Can you create a Python 3 function that
checks multi line docstring summary is separated with empty line.

Response:
def hacking_docstring_summary(physical_line, previous_logical, tokens):
    r"""Check multi line docstring summary is separated with empty line.

    OpenStack HACKING guide recommendation for docstring:
    Docstring should start with a one-line summary, less than 80 characters.

    Okay: def foo():\n    a = '''\nnot\na docstring\n'''
    Okay: '''foobar\n\nfoo\nbar\n'''
    H405: def foo():\n    '''foobar\nfoo\nbar\n'''
    H405: def foo():\n    r'''foobar\nfoo\nbar\n'''
    H405: def foo():\n    '''foobar\n'''
    """
    docstring = is_docstring(tokens, previous_logical)
    if docstring:
        if '\n' not in docstring:
            # not a multi line docstring
            return
        lines = docstring.split('\n')
        if len(lines) > 1 and len(lines[1].strip()) is not 0:
            # docstrings get tokenized on the last line of the docstring, so
            # we don't know the exact position.
            return (0, "H405: multi line docstring "
                    "summary not separated with an empty line")


Instruction:
Write a Python 3 script to
return found docstring 'A docstring is a string literal that occurs as the first statement in a module, function, class,' http://www.python.org/dev/peps/pep-0257/#what-is-a-docstring

Response:
def is_docstring(tokens, previous_logical):
    """Return found docstring

    'A docstring is a string literal that occurs as the first statement in a
    module, function, class,'
    http://www.python.org/dev/peps/pep-0257/#what-is-a-docstring
    """
    for token_type, text, start, _, _ in tokens:
        if token_type == tokenize.STRING:
            break
        elif token_type != tokenize.INDENT:
            return False
    else:
        return False
    line = text.lstrip()
    start, start_triple = _find_first_of(line, START_DOCSTRING_TRIPLE)
    if (previous_logical.startswith("def ") or
            previous_logical.startswith("class ")):
        if start == 0:
            return text


Instruction:
Here you have a function in Python 3, explain what it does
def _find_first_of(line, substrings):
    starts = ((line.find(i), i) for i in substrings)
    found = [(i, sub) for i, sub in starts if i != -1]
    if found:
        return min(found)
    else:
        return -1, None

Response: Find the first occurrence of one of substrings in line.


Instruction:
Can you generate the documentation for the following Python 3 function
def hacking_localization_strings(logical_line, tokens, noqa):
    if noqa:
        return
    gen = check_i18n()
    next(gen)
    try:
        list(map(gen.send, tokens))
        gen.close()
    except LocalizationError as e:
        yield e.args

Response: r Check localization in line.


Instruction:
How would you explain what the following Python 3 function does
def is_none(node):
    '''Check whether an AST node corresponds to None.

    In Python 2 None uses the same ast.Name class that variables etc. use,
    but in Python 3 there is a new ast.NameConstant class.
    '''
    if PY2:
        return isinstance(node, ast.Name) and node.id == 'None'
    return isinstance(node, ast.NameConstant) and node.value is None

Response: Check whether an AST node corresponds to None.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def hacking_assert_is_none(logical_line, noqa):
    if noqa:
        return
    for func_name in ('assertEqual', 'assertIs', 'assertNotEqual',
                      'assertIsNot'):
        try:
            start = logical_line.index('.%s(' % func_name) + 1
        except ValueError:
            continue
        checker = NoneArgChecker(func_name)
        checker.visit(ast.parse(logical_line))
        if checker.none_found:
            yield start, "H203: Use assertIs(Not)None to check for None"

Response: Hacking assertIsNone for None


Instruction:
Given the following Python 3 function, write the documentation
def hacking_assert_greater_less(logical_line, noqa):
    if noqa:
        return

    methods = ['assertTrue', 'assertFalse']
    for method in methods:
        start = logical_line.find('.%s' % method) + 1
        if start != 0:
            break
    else:
        return
    comparisons = [ast.Gt, ast.GtE, ast.Lt, ast.LtE]
    checker = AssertTrueFalseChecker(methods, comparisons)
    checker.visit(ast.parse(logical_line))
    if checker.error:
        yield start, 'H205: Use assert{Greater,Less}[Equal]'

Response: r Check that self. assertGreaterEqual and self. assertLessEqual are used.


Instruction:
Make a summary of the following Python 3 code
def hacking_no_cr(physical_line):
    pos = physical_line.find('\r')
    if pos != -1 and pos == (len(physical_line) - 2):
        return (pos, "H903: Windows style line endings not allowed in code")

Response: Check that we only use newlines not carriage returns.


Instruction:
Write a Python 3 function that can
download a dataset by owner and id.

Response:
def download_dataset(self, owner, id, **kwargs):
        """
        Download dataset
        This endpoint will return a .zip containing all files within the dataset as originally uploaded.   If you are interested retrieving clean data extracted from those files by data.world, check out `GET:/sql` and `GET:/sparql`.
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please define a `callback` function
        to be invoked when receiving the response.
        >>> def callback_function(response):
        >>>     pprint(response)
        >>>
        >>> thread = api.download_dataset(owner, id, callback=callback_function)

        :param callback function: The callback function
            for asynchronous request. (optional)
        :param str owner: User name and unique identifier of the creator of a dataset or project. For example, in the URL: [https://data.world/jonloyens/an-intro-to-dataworld-dataset](https://data.world/jonloyens/an-intro-to-dataworld-dataset), jonloyens is the unique identifier of the owner. (required)
        :param str id: Dataset unique identifier. For example, in the URL:[https://data.world/jonloyens/an-intro-to-dataworld-dataset](https://data.world/jonloyens/an-intro-to-dataworld-dataset), an-intro-to-dataworld-dataset is the unique identifier of the dataset. (required)
        :return: None
                 If the method is called asynchronously,
                 returns the request thread.
        """
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.download_dataset_with_http_info(owner, id, **kwargs)
        else:
            (data) = self.download_dataset_with_http_info(owner, id, **kwargs)
            return data


Instruction:
Can you generate a brief explanation for the following Python 3 code
def download_file(self, owner, id, file, **kwargs):
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.download_file_with_http_info(owner, id, file, **kwargs)
        else:
            (data) = self.download_file_with_http_info(owner, id, file, **kwargs)
            return data

Response: Download a file within the dataset.


Instruction:
Can you generate the documentation for the following Python 3 function
def count(self, count):
        if count is None:
            raise ValueError("Invalid value for `count`, must not be `None`")
        if count is not None and count < 0:
            raise ValueError("Invalid value for `count`, must be a value greater than or equal to `0`")

        self._count = count

Response: Sets the count of this PaginatedDatasetResults.


Instruction:
Can you create a Python 3 function that
sets the type of this WebAuthorization.

Response:
def type(self, type):
        """
        Sets the type of this WebAuthorization.
        The authorization scheme.  Usually this is \"Bearer\" but it could be other values like \"Token\" or \"Basic\" etc.

        :param type: The type of this WebAuthorization.
        :type: str
        """
        if type is None:
            raise ValueError("Invalid value for `type`, must not be `None`")
        if type is not None and len(type) > 50:
            raise ValueError("Invalid value for `type`, length must be less than or equal to `50`")
        if type is not None and not re.search('[\\x21-\\x7E \\t]*', type):
            raise ValueError("Invalid value for `type`, must be a follow pattern or equal to `/[\\x21-\\x7E \\t]*/`")

        self._type = type


Instruction:
How would you code a function in Python 3 to
set the credentials of this WebAuthorization.

Response:
def credentials(self, credentials):
        """
        Sets the credentials of this WebAuthorization.
        The confidential portion of the `Authorization` header that follows the `type` field.  This field is write-only. It is omitted by read operations.  If authorization is required, the `credentials` value must be provided whenever a File Source is created or modified. An update to a dataset that does not change the File Source may omit the `credentials` field--the update will preserve the previous value.

        :param credentials: The credentials of this WebAuthorization.
        :type: str
        """
        if credentials is not None and len(credentials) > 1024:
            raise ValueError("Invalid value for `credentials`, length must be less than or equal to `1024`")
        if credentials is not None and len(credentials) < 1:
            raise ValueError("Invalid value for `credentials`, length must be greater than or equal to `1`")
        if credentials is not None and not re.search('[\\x21-\\x7E \\t]*', credentials):
            raise ValueError("Invalid value for `credentials`, must be a follow pattern or equal to `/[\\x21-\\x7E \\t]*/`")

        self._credentials = credentials


Instruction:
Can you generate a brief explanation for the following Python 3 code
def describe(self, resource=None):
        if resource is None:
            # Show simpler descriptor, omitting schema definitions
            simple_descriptor = copy.deepcopy(self._datapackage.descriptor)
            for resource in simple_descriptor['resources']:
                resource.pop('schema', None)
            return simple_descriptor
        else:
            return self.__resources[resource].descriptor

Response: Describe dataset or resource within dataset.


Instruction:
Create a Python 3 function for
extracting raw data from resource

Response:
def _load_raw_data(self, resource_name):
        """Extract raw data from resource

        :param resource_name:
        """
        # Instantiating the resource again as a simple `Resource` ensures that
        # ``data`` will be returned as bytes.
        upcast_resource = datapackage.Resource(
            self.__resources[resource_name].descriptor,
            default_base_path=self.__base_path)
        return upcast_resource.data


Instruction:
Write a Python 3 function for
building table structure from resource data

Response:
def _load_table(self, resource_name):
        """Build table structure from resource data

        :param resource_name:
        """
        tabular_resource = self.__tabular_resources[resource_name]

        try:
            # Sorting fields in the same order as they appear in the schema
            # is necessary for tables to be converted into pandas.DataFrame
            fields = []
            if 'schema' in tabular_resource.descriptor:
                fields = [f['name'] for f in
                          tabular_resource.descriptor['schema']['fields']]
            elif len(tabular_resource.data) > 0:
                fields = tabular_resource.data[0].keys()

            return [order_columns_in_row(fields, row) for row in
                    tabular_resource.data]
        except (SchemaValidationError, ValueError, TypeError) as e:
            warnings.warn(
                'Unable to set column types automatically using {} schema. '
                'Data types may need to be adjusted manually. '
                'Error: {}'.format(resource_name, e))
            self.__invalid_schemas.append(resource_name)
            file_format = tabular_resource.descriptor['format']
            with Stream(io.BytesIO(self.raw_data[resource_name]),
                        format=file_format, headers=1,
                        scheme='stream', encoding='utf-8') as stream:
                return [OrderedDict(zip(stream.headers, row))
                        for row in stream.iter()]


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _load_dataframe(self, resource_name):
        try:
            import pandas
        except ImportError:
            raise RuntimeError('To enable dataframe support, '
                               'run \'pip install datadotworld[pandas]\'')

        tabular_resource = self.__tabular_resources[resource_name]
        field_dtypes = fields_to_dtypes(tabular_resource.descriptor['schema'])

        try:
            return pandas.read_csv(
                path.join(
                    self.__base_path,
                    tabular_resource.descriptor['path']),
                dtype=field_dtypes['other'],
                parse_dates=list(field_dtypes['dates'].keys()),
                infer_datetime_format=True)
        except ValueError as e:
            warnings.warn(
                'Unable to set data frame dtypes automatically using {} '
                'schema. Data types may need to be adjusted manually. '
                'Error: {}'.format(resource_name, e))
            return pandas.read_csv(
                path.join(
                    self.__base_path,
                    tabular_resource.descriptor['path']))

Response: Build pandas. DataFrame from resource data


Instruction:
Write a Python 3 script for
appending records to a stream.

Response:
def append_records(self, owner, id, stream_id, body, **kwargs):
        """
        Append records to a stream.
        This endpoint appends JSON data to a stream associated with a dataset. Streams don't need to be created before you can append data to it. They will be created on-demand, the first time they are used.  Multiple records can be appended at once by using JSON-L (`application/json-l`) as the request content type.  Currently, data uploaded to a dataset via a stream is not immediatelly processed. Instead, it is processed automatically at least once a day or as a result of a \"Sync files\" endpoint invocation.  Once processed, the contents of a stream will appear as part of the respective dataset as a `.jsonl` file (e.g. `my-stream` will produce a file named `my-stream.jsonl`).
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please define a `callback` function
        to be invoked when receiving the response.
        >>> def callback_function(response):
        >>>     pprint(response)
        >>>
        >>> thread = api.append_records(owner, id, stream_id, body, callback=callback_function)

        :param callback function: The callback function
            for asynchronous request. (optional)
        :param str owner: User name and unique identifier of the creator of a dataset or project. For example, in the URL: [https://data.world/jonloyens/an-intro-to-dataworld-dataset](https://data.world/jonloyens/an-intro-to-dataworld-dataset), jonloyens is the unique identifier of the owner. (required)
        :param str id: Dataset unique identifier. For example, in the URL:[https://data.world/jonloyens/an-intro-to-dataworld-dataset](https://data.world/jonloyens/an-intro-to-dataworld-dataset), an-intro-to-dataworld-dataset is the unique identifier of the dataset. (required)
        :param str stream_id: Stream unique identifier as defined by the user the first time the stream was used. Only lower case letters, numbers and dashes are allowed. Maximum length of 95 characters. (required)
        :param object body: (required)
        :return: None
                 If the method is called asynchronously,
                 returns the request thread.
        """
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.append_records_with_http_info(owner, id, stream_id, body, **kwargs)
        else:
            (data) = self.append_records_with_http_info(owner, id, stream_id, body, **kwargs)
            return data


Instruction:
Can you tell what is the following Python 3 function doing
def create_insight(self, project_owner, project_id, **kwargs):
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.create_insight_with_http_info(project_owner, project_id, **kwargs)
        else:
            (data) = self.create_insight_with_http_info(project_owner, project_id, **kwargs)
            return data

Response:
Create an insight
        Create a new insight.
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please define a `callback` function
        to be invoked when receiving the response.
        >>> def callback_function(response):
        >>>     pprint(response)
        >>>
        >>> thread = api.create_insight(project_owner, project_id, callback=callback_function)

        :param callback function: The callback function
            for asynchronous request. (optional)
        :param str project_owner: User name and unique identifier of the creator of a project. For example, in the URL: [https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs](https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs), `government` is the unique identifier of the owner. (required)
        :param str project_id: User name and unique identifier of the project. For example, in the URL: [https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs](https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs), `how-to-add-depth-to-your-data-with-the-us-census-acs` is the unique identifier of the owner. (required)
        :param InsightCreateRequest body:
        :return: CreateInsightResponse
                 If the method is called asynchronously,
                 returns the request thread.


Instruction:
Make a summary of the following Python 3 code
def delete_insight(self, project_owner, project_id, id, **kwargs):
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.delete_insight_with_http_info(project_owner, project_id, id, **kwargs)
        else:
            (data) = self.delete_insight_with_http_info(project_owner, project_id, id, **kwargs)
            return data

Response: Delete an existing Insight.


Instruction:
Make a summary of the following Python 3 code
def get_insight(self, project_owner, project_id, id, **kwargs):
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.get_insight_with_http_info(project_owner, project_id, id, **kwargs)
        else:
            (data) = self.get_insight_with_http_info(project_owner, project_id, id, **kwargs)
            return data

Response:
Retrieve an insight
        Retrieve an insight.
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please define a `callback` function
        to be invoked when receiving the response.
        >>> def callback_function(response):
        >>>     pprint(response)
        >>>
        >>> thread = api.get_insight(project_owner, project_id, id, callback=callback_function)

        :param callback function: The callback function
            for asynchronous request. (optional)
        :param str project_owner: User name and unique identifier of the creator of a project. For example, in the URL: [https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs](https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs), `government` is the unique identifier of the owner. (required)
        :param str project_id: User name and unique identifier of the project. For example, in the URL: [https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs](https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs), `how-to-add-depth-to-your-data-with-the-us-census-acs` is the unique identifier of the owner. (required)
        :param str id: Insight unique identifier. (required)
        :return: InsightSummaryResponse
                 If the method is called asynchronously,
                 returns the request thread.


Instruction:
Given the following Python 3 function, write the documentation
def get_insights_for_project(self, project_owner, project_id, **kwargs):
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.get_insights_for_project_with_http_info(project_owner, project_id, **kwargs)
        else:
            (data) = self.get_insights_for_project_with_http_info(project_owner, project_id, **kwargs)
            return data

Response: Get the list of insights for a project.


Instruction:
Explain what the following Python 3 code does
def replace_insight(self, project_owner, project_id, id, **kwargs):
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.replace_insight_with_http_info(project_owner, project_id, id, **kwargs)
        else:
            (data) = self.replace_insight_with_http_info(project_owner, project_id, id, **kwargs)
            return data

Response:
Replace an insight
        Replace an insight.
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please define a `callback` function
        to be invoked when receiving the response.
        >>> def callback_function(response):
        >>>     pprint(response)
        >>>
        >>> thread = api.replace_insight(project_owner, project_id, id, callback=callback_function)

        :param callback function: The callback function
            for asynchronous request. (optional)
        :param str project_owner: User name and unique identifier of the creator of a project. For example, in the URL: [https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs](https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs), `government` is the unique identifier of the owner. (required)
        :param str project_id: User name and unique identifier of the project. For example, in the URL: [https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs](https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs), `how-to-add-depth-to-your-data-with-the-us-census-acs` is the unique identifier of the owner. (required)
        :param str id: Insight unique identifier. (required)
        :param InsightPutRequest body:
        :return: SuccessMessage
                 If the method is called asynchronously,
                 returns the request thread.


Instruction:
Can you implement a function in Python 3 that
updates an insight entry.

Response:
def update_insight(self, project_owner, project_id, id, **kwargs):
        """
        Update an insight
        Update an insight. Note that only elements included in the request will be updated. All omitted elements will remain untouched.
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please define a `callback` function
        to be invoked when receiving the response.
        >>> def callback_function(response):
        >>>     pprint(response)
        >>>
        >>> thread = api.update_insight(project_owner, project_id, id, callback=callback_function)

        :param callback function: The callback function
            for asynchronous request. (optional)
        :param str project_owner: User name and unique identifier of the creator of a project. For example, in the URL: [https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs](https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs), `government` is the unique identifier of the owner. (required)
        :param str project_id: User name and unique identifier of the project. For example, in the URL: [https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs](https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs), `how-to-add-depth-to-your-data-with-the-us-census-acs` is the unique identifier of the owner. (required)
        :param str id: Insight unique identifier. (required)
        :param InsightPatchRequest body:
        :return: SuccessMessage
                 If the method is called asynchronously,
                 returns the request thread.
        """
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.update_insight_with_http_info(project_owner, project_id, id, **kwargs)
        else:
            (data) = self.update_insight_with_http_info(project_owner, project_id, id, **kwargs)
            return data


Instruction:
Implement a function in Python 3 to
set the summary of this DatasetPatchRequest.

Response:
def summary(self, summary):
        """
        Sets the summary of this DatasetPatchRequest.
        Long-form dataset summary (Markdown supported).

        :param summary: The summary of this DatasetPatchRequest.
        :type: str
        """
        if summary is not None and len(summary) > 25000:
            raise ValueError("Invalid value for `summary`, length must be less than or equal to `25000`")
        if summary is not None and len(summary) < 0:
            raise ValueError("Invalid value for `summary`, length must be greater than or equal to `0`")

        self._summary = summary


Instruction:
Can you generate a brief explanation for the following Python 3 code
def license(self, license):
        allowed_values = ["Public Domain", "PDDL", "CC-0", "CC-BY", "ODC-BY", "CC-BY-SA", "ODC-ODbL", "CC BY-NC", "CC BY-NC-SA", "Other"]
        if license not in allowed_values:
            raise ValueError(
                "Invalid value for `license` ({0}), must be one of {1}"
                .format(license, allowed_values)
            )

        self._license = license

Response: Sets the license of this DatasetPatchRequest.


Instruction:
Make a summary of the following Python 3 code
def visibility(self, visibility):
        allowed_values = ["OPEN", "PRIVATE"]
        if visibility not in allowed_values:
            raise ValueError(
                "Invalid value for `visibility` ({0}), must be one of {1}"
                .format(visibility, allowed_values)
            )

        self._visibility = visibility

Response: Sets the visibility of this DatasetPatchRequest.


Instruction:
Make a summary of the following Python 3 code
def objective(self, objective):
        if objective is not None and len(objective) > 120:
            raise ValueError("Invalid value for `objective`, length must be less than or equal to `120`")
        if objective is not None and len(objective) < 0:
            raise ValueError("Invalid value for `objective`, length must be greater than or equal to `0`")

        self._objective = objective

Response: Sets the objective of this ProjectPutRequest.


Instruction:
Can you tell what is the following Python 3 function doing
def add_linked_dataset(self, owner, id, linked_dataset_owner, linked_dataset_id, **kwargs):
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.add_linked_dataset_with_http_info(owner, id, linked_dataset_owner, linked_dataset_id, **kwargs)
        else:
            (data) = self.add_linked_dataset_with_http_info(owner, id, linked_dataset_owner, linked_dataset_id, **kwargs)
            return data

Response: Link dataset add a linked dataset to a project.


Instruction:
Here you have a function in Python 3, explain what it does
def create_project(self, owner, **kwargs):
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.create_project_with_http_info(owner, **kwargs)
        else:
            (data) = self.create_project_with_http_info(owner, **kwargs)
            return data

Response: Create a project. This method creates a new project.


Instruction:
Can you create a Python 3 function that
deletes a project and all data associated with it.

Response:
def delete_project(self, owner, id, **kwargs):
        """
        Delete a project
        Permanently deletes a project and all data associated with it. This operation cannot be undone, although a new project may be created with the same id.
        This method makes a synchronous HTTP request by default. To make an
        asynchronous HTTP request, please define a `callback` function
        to be invoked when receiving the response.
        >>> def callback_function(response):
        >>>     pprint(response)
        >>>
        >>> thread = api.delete_project(owner, id, callback=callback_function)

        :param callback function: The callback function
            for asynchronous request. (optional)
        :param str owner: User name and unique identifier of the creator of a project. For example, in the URL: [https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs](https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs), government is the unique identifier of the owner. (required)
        :param str id: Project unique identifier. For example, in the URL:[https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs](https://data.world/government/how-to-add-depth-to-your-data-with-the-us-census-acs), how-to-add-depth-to-your-data-with-the-us-census-acs is the unique identifier of the project. (required)
        :return: SuccessMessage
                 If the method is called asynchronously,
                 returns the request thread.
        """
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.delete_project_with_http_info(owner, id, **kwargs)
        else:
            (data) = self.delete_project_with_http_info(owner, id, **kwargs)
            return data


Instruction:
Can you generate the documentation for the following Python 3 function
def get_project(self, owner, id, **kwargs):
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.get_project_with_http_info(owner, id, **kwargs)
        else:
            (data) = self.get_project_with_http_info(owner, id, **kwargs)
            return data

Response: Retrieve a project by its unique identifier.


Instruction:
Make a summary of the following Python 3 code
def patch_project(self, owner, id, **kwargs):
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.patch_project_with_http_info(owner, id, **kwargs)
        else:
            (data) = self.patch_project_with_http_info(owner, id, **kwargs)
            return data

Response: Update a project with the specified attributes.


Instruction:
Can you tell what is the following Python 3 function doing
def remove_linked_dataset(self, owner, id, linked_dataset_owner, linked_dataset_id, **kwargs):
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.remove_linked_dataset_with_http_info(owner, id, linked_dataset_owner, linked_dataset_id, **kwargs)
        else:
            (data) = self.remove_linked_dataset_with_http_info(owner, id, linked_dataset_owner, linked_dataset_id, **kwargs)
            return data

Response: Remove a linked dataset from a project.


Instruction:
Given the following Python 3 function, write the documentation
def replace_project(self, owner, id, **kwargs):
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.replace_project_with_http_info(owner, id, **kwargs)
        else:
            (data) = self.replace_project_with_http_info(owner, id, **kwargs)
            return data

Response: Replace a project with a given id or completely rewrite it.


Instruction:
Implement a Python 3 function for
loading a dataset from the local filesystem if necessary.

Response:
def load_dataset(dataset_key, force_update=False, auto_update=False,
                 profile='default', **kwargs):
    """Load a dataset from the local filesystem, downloading it from data.world
    first, if necessary.

    This function returns an object of type `LocalDataset`. The object
    allows access to metedata via it's `describe()` method and to all the data
    via three properties `raw_data`, `tables` and `dataframes`, all of which
    are mappings (dict-like structures).

    :param dataset_key: Dataset identifier, in the form of owner/id or of a url
    :type dataset_key: str
    :param force_update: Flag, indicating if a new copy of the dataset should
        be downloaded replacing any previously downloaded copy
        (Default value = False)
    :type force_update: bool
    :param auto_update: Flag, indicating that dataset be updated to the latest
        version
    :type auto_update: bool
    :param profile: Configuration profile (account) to use.
        (Default value = 'default')
    :type profile: str, optional
    :returns: The object representing the dataset
    :rtype: LocalDataset
    :raises RestApiError: If a server error occurs

    Examples
    --------
    >>> import datadotworld as dw
    >>> dataset = dw.load_dataset('jonloyens/an-intro-to-dataworld-dataset')
    >>> list(dataset.dataframes)
    ['changelog', 'datadotworldbballstats', 'datadotworldbballteam']
    """
    return _get_instance(profile, **kwargs). \
        load_dataset(dataset_key,
                     force_update=force_update,
                     auto_update=auto_update)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def query(dataset_key, query, query_type='sql', profile='default',
          parameters=None, **kwargs):
    """Query an existing dataset

    :param dataset_key: Dataset identifier, in the form of owner/id or of a url
    :type dataset_key: str
    :param query: SQL or SPARQL query
    :type query: str
    :param query_type: The type of the query. Must be either 'sql' or 'sparql'.
        (Default value = 'sql')
    :type query_type: {'sql', 'sparql'}, optional
    :param parameters: parameters to the query - if SPARQL query, this should
        be a dict containing named parameters, if SQL query, then this should
        be a list containing positional parameters.  Boolean values will be
        converted to xsd:boolean, Integer values to xsd:integer, and other
        Numeric values to xsd:decimal. anything else is treated as a String
        literal (Default value = None)
    :type parameters: query parameters, optional
    :param profile: Configuration profile (account) to use.
        (Default value = 'default')
    :type profile: str, optional
    :returns: Object containing the results of the query
    :rtype: Results
    :raises RuntimeError: If a server error occurs

    Examples
    --------
    >>> import datadotworld as dw
    >>> results = dw.query(
    ...     'jonloyens/an-intro-to-dataworld-dataset',
    ...     'SELECT * FROM `DataDotWorldBBallStats`, `DataDotWorldBBallTeam` '
    ...     'WHERE DataDotWorldBBallTeam.Name = DataDotWorldBBallStats.Name')
    >>> df = results.dataframe
    >>> df.shape
    (8, 6)
    """
    return _get_instance(profile, **kwargs).query(dataset_key, query,
                                                  query_type=query_type,
                                                  parameters=parameters,
                                                  **kwargs)

Response: Query an existing dataset with the specified query


Instruction:
Here you have a function in Python 3, explain what it does
def open_remote_file(dataset_key, file_name, profile='default',
                     mode='w', **kwargs):
    """Open a remote file object that can be used to write to or read from
    a file in a data.world dataset

    :param dataset_key: Dataset identifier, in the form of owner/id
    :type dataset_key: str
    :param file_name: The name of the file to open
    :type file_name: str
    :param mode: the mode for the file - must be 'w', 'wb', 'r', or 'rb' -
        indicating read/write ('r'/'w') and optionally "binary"
        handling of the file data. (Default value = 'w')
    :type mode: str, optional
    :param chunk_size: size of chunked bytes to return when reading streamed
        bytes in 'rb' mode
    :type chunk_size: int, optional
    :param decode_unicode: whether to decode textual responses as unicode when
        returning streamed lines in 'r' mode
    :type decode_unicode: bool, optional
    :param profile:  (Default value = 'default')
    :param **kwargs:

    Examples
    --------
    >>> import datadotworld as dw
    >>>
    >>> # write a text file
    >>> with dw.open_remote_file('username/test-dataset',
    ...                          'test.txt') as w:
    ...   w.write("this is a test.")
    >>>
    >>> # write a jsonlines file
    >>> import json
    >>> with dw.open_remote_file('username/test-dataset',
    ...                          'test.jsonl') as w:
    ...   json.dump({'foo':42, 'bar':"A"}, w)
    ...   w.write("\\n")
    ...   json.dump({'foo':13, 'bar':"B"}, w)
    ...   w.write("\\n")
    >>>
    >>> # write a csv file
    >>> import csv
    >>> with dw.open_remote_file('username/test-dataset',
    ...                          'test.csv') as w:
    ...   csvw = csv.DictWriter(w, fieldnames=['foo', 'bar'])
    ...   csvw.writeheader()
    ...   csvw.writerow({'foo':42, 'bar':"A"})
    ...   csvw.writerow({'foo':13, 'bar':"B"})
    >>>
    >>> # write a pandas dataframe as a csv file
    >>> import pandas as pd
    >>> df = pd.DataFrame({'foo':[1,2,3,4],'bar':['a','b','c','d']})
    >>> with dw.open_remote_file('username/test-dataset',
    ...                          'dataframe.csv') as w:
    ...   df.to_csv(w, index=False)
    >>>
    >>> # write a binary file
    >>> with dw.open_remote_file('username/test-dataset',
    >>>                          'test.txt', mode='wb') as w:
    ...   w.write(bytes([100,97,116,97,46,119,111,114,108,100]))
    >>>
    >>> # read a text file
    >>> with dw.open_remote_file('username/test-dataset',
    ...                          'test.txt', mode='r') as r:
    ...   print(r.read())
    >>>
    >>> # read a csv file
    >>> with dw.open_remote_file('username/test-dataset',
    ...                          'test.csv', mode='r') as r:
    ...   csvr = csv.DictReader(r)
    ...   for row in csvr:
    ...      print(row['column a'], row['column b'])
    >>>
    >>> # read a binary file
    >>> with dw.open_remote_file('username/test-dataset',
    ...                          'test', mode='rb') as r:
    ...   bytes = r.read()
    """
    return _get_instance(profile, **kwargs).open_remote_file(
        dataset_key, file_name,
        mode=mode, **kwargs)

Response: Open a remote file object that can be used to read from or write to or read from a file in a data. world dataset.


Instruction:
Can you generate the documentation for the following Python 3 function
def table(self):
        if self._table is None:
            self._table = list(self._iter_rows())

        return self._table

Response: Build and cache a table from query results


Instruction:
Can you generate the documentation for the following Python 3 function
def dataframe(self):

        if self._dataframe is None:
            try:
                import pandas as pd
            except ImportError:
                raise RuntimeError('To enable dataframe support, '
                                   'run \'pip install datadotworld[pandas]\'')

            self._dataframe = pd.DataFrame.from_records(self._iter_rows(),
                                                        coerce_float=True)

        return self._dataframe

Response: Build and cache a dataframe from query results


Instruction:
Can you tell what is the following Python 3 function doing
def fields_to_dtypes(schema):
    datetime_types = ['date', 'datetime']
    datetime_fields = {
        f['name']: _TABLE_SCHEMA_DTYPE_MAPPING.get(f['type'], 'object')
        for f in schema['fields']
        if f['type'] in datetime_types}

    other_fields = {
        f['name']: _TABLE_SCHEMA_DTYPE_MAPPING.get(f['type'], 'object')
        for f in schema['fields']
        if f['type'] not in datetime_types}

    return {'dates': datetime_fields, 'other': other_fields}

Response: Maps table schema fields types to dtypes separating date fields


Instruction:
Here you have a function in Python 3, explain what it does
def sanitize_resource_schema(r):
    if 'schema' in r.descriptor:
        r.descriptor['schema'] = _sanitize_schema(r.descriptor['schema'])

    return r

Response: Sanitize resource schema for increased compatibility


Instruction:
Can you write a function in Python 3 where it
infers Table Schema from SPARQL results JSON Results Spec

Response:
def infer_table_schema(sparql_results_json):
    """Infer Table Schema from SPARQL results JSON

    SPARQL JSON Results Spec:
    https://www.w3.org/TR/2013/REC-sparql11-results-json-20130321

    :param sparql_results_json: SPARQL JSON results of a query
    :returns: A schema descriptor for the inferred schema
    :rtype: dict (json)
    """
    if ('results' in sparql_results_json and
            'bindings' in sparql_results_json['results'] and
            len(sparql_results_json['results']['bindings']) > 0):

        # SQL results include metadata, SPARQL results don't
        result_metadata = sparql_results_json.get('metadata', [])
        metadata_names = [item['name'] for item in result_metadata]
        result_vars = sparql_results_json['head']['vars']

        _verify_unique_names(result_vars, metadata_names)

        # SQL results require var name mapping, SPARQL results vars don't
        result_vars_mapping = dict(zip(
            result_vars, (metadata_names
                          if metadata_names != []
                          else result_vars)))

        homogeneous_types = _get_types_from_sample(
            result_vars, sparql_results_json)

        fields = []
        if homogeneous_types is None:
            for result_var in result_vars:
                fields.append({
                    'name': result_vars_mapping.get(result_var),
                    'type': 'string'
                })
        else:
            for index, var in enumerate(result_vars):
                field = {
                    'name': result_vars_mapping.get(var),
                    'type': infer_table_schema_type_from_rdf_term(
                        homogeneous_types[var].get('type'),
                        homogeneous_types[var].get('datatype')
                    )}

                if 'datatype' in homogeneous_types.get(var):
                    field['rdfType'] = homogeneous_types[var].get('datatype')

                term_metadata = (result_metadata[index]
                                 if result_metadata != [] else {})
                if 'description' in term_metadata:
                    field['description'] = term_metadata['description']

                fields.append(field)

        return _sanitize_schema({'fields': fields})
    elif 'boolean' in sparql_results_json:
        # ASK query results
        return {'fields': [{'name': 'boolean', 'type': 'boolean'}]}
    else:
        warn('Unable to infer table schema from empty query results')
        return None


Instruction:
How would you code a function in Python 3 to
ensure columns appear in the same order for every row in table

Response:
def order_columns_in_row(fields, unordered_row):
    """Ensure columns appear in the same order for every row in table

    :param fields:
    :param unordered_row:
    """
    fields_idx = {f: pos for pos, f in enumerate(fields)}
    return OrderedDict(sorted(unordered_row.items(),
                              key=lambda i: fields_idx[i[0]]))


Instruction:
How would you explain what the following Python 3 function does
def _get_types_from_sample(result_vars, sparql_results_json):
    total_bindings = len(sparql_results_json['results']['bindings'])
    homogeneous_types = {}
    for result_var in result_vars:
        var_types = set()
        var_datatypes = set()
        for i in range(0, min(total_bindings, 10)):
            binding = sparql_results_json['results']['bindings'][i]
            rdf_term = binding.get(result_var)
            if rdf_term is not None:  # skip missing values
                var_types.add(rdf_term.get('type'))
                var_datatypes.add(rdf_term.get('datatype'))
        if len(var_types) > 1 or len(var_datatypes) > 1:
            return None  # Heterogeneous types
        else:
            homogeneous_types[result_var] = {
                'type': var_types.pop() if var_types else None,
                'datatype': var_datatypes.pop() if var_datatypes else None
            }

    return homogeneous_types

Response: Return types if homogenous within sample


Instruction:
How would you implement a function in Python 3 that
applies supplier_func to each element in seq returns 1st not None

Response:
def _first_not_none(seq, supplier_func):
        """Applies supplier_func to each element in seq, returns 1st not None

        :param seq: Sequence of object
        :type seq: iterable
        :param supplier_func: Function that extracts the desired value from
            elements in seq
        :type supplier_func: function
        """
        for i in seq:
            obj = supplier_func(i)
            if obj is not None:
                return obj

        return None


Instruction:
Here you have a function in Python 3, explain what it does
def cli(ctx, profile):
    if ctx.obj is None:
        ctx.obj = {}
    ctx.obj['profile'] = profile
    pass

Response: dw commands support working with multiple data. world accounts


Instruction:
Write a Python 3 script for
using this command to configure API tokens

Response:
def configure(obj, token):
    """Use this command to configure API tokens
    """
    config = obj.get('config') or FileConfig(obj['profile'])
    config.auth_token = token
    config.save()


Instruction:
Here you have a function in Python 3, explain what it does
def request_entity(self, request_entity):
        if request_entity is not None and len(request_entity) > 10000:
            raise ValueError("Invalid value for `request_entity`, length must be less than or equal to `10000`")

        self._request_entity = request_entity

Response: Sets the request_entity of this FileSourceCreateOrUpdateRequest.


Instruction:
Here you have a function in Python 3, explain what it does
def title(self, title):
        if title is None:
            raise ValueError("Invalid value for `title`, must not be `None`")
        if title is not None and len(title) > 128:
            raise ValueError("Invalid value for `title`, length must be less than or equal to `128`")
        if title is not None and len(title) < 1:
            raise ValueError("Invalid value for `title`, length must be greater than or equal to `1`")

        self._title = title

Response: Sets the title of this InsightPutRequest.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def sparql_get(self, owner, id, query, **kwargs):
        kwargs['_return_http_data_only'] = True
        if kwargs.get('callback'):
            return self.sparql_get_with_http_info(owner, id, query, **kwargs)
        else:
            (data) = self.sparql_get_with_http_info(owner, id, query, **kwargs)
            return data

Response: SPARQL query for a specific resource.


Instruction:
Implement a function in Python 3 to
retrieve an existing dataset definition This method retrieves metadata about an existing :param dataset_key: Dataset identifier, in the form of owner/id :type dataset_key: str :returns: Dataset definition, with all attributes :rtype: dict :raises RestApiException: If a server error occurs Examples -------- >>> import datadotworld as dw >>> api_client = dw.api_client() >>> intro_dataset = api_client.get_dataset( ... 'jonloyens/an-intro-to-dataworld-dataset') # doctest: +SKIP >>> intro_dataset['title'] # doctest: +SKIP 'An Intro to data.world Dataset'

Response:
def get_dataset(self, dataset_key):
        """Retrieve an existing dataset definition

        This method retrieves metadata about an existing

        :param dataset_key: Dataset identifier, in the form of owner/id
        :type dataset_key: str
        :returns: Dataset definition, with all attributes
        :rtype: dict
        :raises RestApiException: If a server error occurs

        Examples
        --------
        >>> import datadotworld as dw
        >>> api_client = dw.api_client()
        >>> intro_dataset = api_client.get_dataset(
        ...     'jonloyens/an-intro-to-dataworld-dataset')  # doctest: +SKIP
        >>> intro_dataset['title']  # doctest: +SKIP
        'An Intro to data.world Dataset'
        """
        try:
            return self._datasets_api.get_dataset(
                *(parse_dataset_key(dataset_key))).to_dict()
        except _swagger.rest.ApiException as e:
            raise RestApiError(cause=e)


Instruction:
How would you code a function in Python 3 to
create a new dataset with the specified properties

Response:
def create_dataset(self, owner_id, **kwargs):
        """Create a new dataset

        :param owner_id: Username of the owner of the new dataset
        :type owner_id: str
        :param title: Dataset title (will be used to generate dataset id on
            creation)
        :type title: str
        :param description: Dataset description
        :type description: str, optional
        :param summary: Dataset summary markdown
        :type summary: str, optional
        :param tags: Dataset tags
        :type tags: list, optional
        :param license: Dataset license
        :type license: {'Public Domain', 'PDDL', 'CC-0', 'CC-BY', 'ODC-BY',
            'CC-BY-SA', 'ODC-ODbL', 'CC BY-NC', 'CC BY-NC-SA', 'Other'}
        :param visibility: Dataset visibility
        :type visibility: {'OPEN', 'PRIVATE'}
        :param files: File name as dict, source URLs, description and labels()
        as properties
        :type files: dict, optional
            *Description and labels are optional*
        :returns: Newly created dataset key
        :rtype: str
        :raises RestApiException: If a server error occurs

        Examples
        --------
        >>> import datadotworld as dw
        >>> api_client = dw.api_client()
        >>> url = 'http://www.acme.inc/example.csv'
        >>> api_client.create_dataset(
        ...     'username', title='Test dataset', visibility='PRIVATE',
        ...     license='Public Domain',
        ...     files={'dataset.csv':{'url': url}})  # doctest: +SKIP
        """
        request = self.__build_dataset_obj(
            lambda: _swagger.DatasetCreateRequest(
                title=kwargs.get('title'),
                visibility=kwargs.get('visibility')),
            lambda name, url, expand_archive, description, labels:
            _swagger.FileCreateRequest(
                name=name,
                source=_swagger.FileSourceCreateRequest(
                    url=url,
                    expand_archive=expand_archive),
                description=description,
                labels=labels),
            kwargs)
        try:
            (_, _, headers) = self._datasets_api.create_dataset_with_http_info(
                owner_id, request, _return_http_data_only=False)
            if 'Location' in headers:
                return headers['Location']
        except _swagger.rest.ApiException as e:
            raise RestApiError(cause=e)


Instruction:
How would you code a function in Python 3 to
update an existing dataset

Response:
def update_dataset(self, dataset_key, **kwargs):
        """Update an existing dataset

        :param description: Dataset description
        :type description: str, optional
        :param summary: Dataset summary markdown
        :type summary: str, optional
        :param tags: Dataset tags
        :type tags: list, optional
        :param license: Dataset license
        :type license: {'Public Domain', 'PDDL', 'CC-0', 'CC-BY', 'ODC-BY',
            'CC-BY-SA', 'ODC-ODbL', 'CC BY-NC', 'CC BY-NC-SA', 'Other'}
        :param visibility: Dataset visibility
        :type visibility: {'OPEN', 'PRIVATE'}, optional
        :param files: File names and source URLs to add or update
        :type files: dict, optional
        :param dataset_key: Dataset identifier, in the form of owner/id
        :type dataset_key: str
        :raises RestApiException: If a server error occurs

        Examples
        --------
        >>> import datadotworld as dw
        >>> api_client = dw.api_client()
        >>> api_client.update_dataset(
        ...    'username/test-dataset',
        ...    tags=['demo', 'datadotworld'])  # doctest: +SKIP
        """
        request = self.__build_dataset_obj(
            lambda: _swagger.DatasetPatchRequest(),
            lambda name, url, expand_archive, description, labels:
            _swagger.FileCreateOrUpdateRequest(
                name=name,
                source=_swagger.FileSourceCreateOrUpdateRequest(
                    url=url,
                    expand_archive=expand_archive)
                if url is not None else None,
                description=description,
                labels=labels),
            kwargs)
        owner_id, dataset_id = parse_dataset_key(dataset_key)
        try:
            self._datasets_api.patch_dataset(owner_id, dataset_id, request)
        except _swagger.rest.ApiException as e:
            raise RestApiError(cause=e)


Instruction:
Can you tell what is the following Python 3 function doing
def replace_dataset(self, dataset_key, **kwargs):
        request = self.__build_dataset_obj(
            lambda: _swagger.DatasetPutRequest(
                title=kwargs.get('title'),
                visibility=kwargs.get('visibility')
            ),
            lambda name, url, expand_archive, description, labels:
            _swagger.FileCreateRequest(
                name=name,
                source=_swagger.FileSourceCreateRequest(
                    url=url,
                    expand_archive=expand_archive),
                description=description,
                labels=labels),
            kwargs)

        owner_id, dataset_id = parse_dataset_key(dataset_key)
        try:
            self._datasets_api.replace_dataset(owner_id, dataset_id, request)
        except _swagger.rest.ApiException as e:
            raise RestApiError(cause=e)

Response: This method replaces an existing dataset in datadotworld.


Instruction:
Explain what the following Python 3 code does
def delete_dataset(self, dataset_key):
        owner_id, dataset_id = parse_dataset_key(dataset_key)
        try:
            self._datasets_api.delete_dataset(owner_id, dataset_id)
        except _swagger.rest.ApiException as e:
            raise RestApiError(cause=e)

Response: Deletes a dataset and all associated data


Instruction:
Can you create a Python 3 function that
adds or update dataset files linked to source URLs

Response:
def add_files_via_url(self, dataset_key, files={}):
        """Add or update dataset files linked to source URLs

        :param dataset_key: Dataset identifier, in the form of owner/id
        :type dataset_key: str
        :param files: Dict containing the name of files and metadata
            Uses file name as a dict containing File description, labels and
            source URLs to add or update (Default value = {})
            *description and labels are optional.*
        :type files: dict
        :raises RestApiException: If a server error occurs

        Examples
        --------
        >>> import datadotworld as dw
        >>> url = 'http://www.acme.inc/example.csv'
        >>> api_client = dw.api_client()
        >>> api_client.add_files_via_url(
        ...     'username/test-dataset',
        ...     {'example.csv': {
        ...         'url': url,
        ...         'labels': ['raw data'],
        ...         'description': 'file description'}})  # doctest: +SKIP
        """
        file_requests = [_swagger.FileCreateOrUpdateRequest(
            name=file_name,
            source=_swagger.FileSourceCreateOrUpdateRequest(
                url=file_info['url'],
                expand_archive=file_info.get('expand_archive',
                                             False)),
            description=file_info.get('description'),
            labels=file_info.get('labels'),
        ) for file_name, file_info in files.items()]
        owner_id, dataset_id = parse_dataset_key(dataset_key)
        try:
            self._datasets_api.add_files_by_source(
                owner_id, dataset_id,
                _swagger.FileBatchUpdateRequest(files=file_requests))
        except _swagger.rest.ApiException as e:
            raise RestApiError(cause=e)