Instruction:
Can you create a Python 3 function that
returns the list of contributors in the specified project.

Response:
def list_contributors(self, project_id=None, language_code=None):
        """
        Returns the list of contributors
        """
        data = self._run(
            url_path="contributors/list",
            id=project_id,
            language=language_code
        )
        return data['result'].get('contributors', [])


Instruction:
Write a Python 3 function that can
add a contributor to a project language_code

Response:
def add_contributor(self, project_id, name, email, language_code):
        """
        Adds a contributor to a project language
        """
        self._run(
            url_path="contributors/add",
            id=project_id,
            name=name,
            email=email,
            language=language_code
        )
        return True


Instruction:
Create a Python 3 function to
add a contributor to a project language

Response:
def add_administrator(self, project_id, name, email):
        """
        Adds a contributor to a project language
        """
        self._run(
            url_path="contributors/add",
            id=project_id,
            name=name,
            email=email,
            admin=True
        )
        return True


Instruction:
Here you have a function in Python 3, explain what it does
def remove_contributor(self, project_id, email, language):
        self._run(
            url_path="contributors/remove",
            id=project_id,
            email=email,
            language=language
        )
        return True

Response: Removes a contributor from the cache.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def parse_stations(html):
    html = html.replace('SLs.sls=', '').replace(';SLs.showSuggestion();', '')
    html = json.loads(html)
    return html['suggestions']

Response: Parses the stations from the given HTML.


Instruction:
Create a Python 3 function to
parse the delay from the details view and save the parsed data

Response:
def parse_delay(data):
    """
        Prase the delay
    """
    # parse data from the details view
    rsp = requests.get(data['details'])
    soup = BeautifulSoup(rsp.text, "html.parser")

    # get departure delay
    delay_departure_raw = soup.find('div', class_="routeStart").find('span', class_=["delay", "delayOnTime"])
    if delay_departure_raw:
        delay_departure = calculate_delay(data['departure'],
                                          delay_departure_raw.text)
    else:
        delay_departure = 0

    # get arrival delay
    delay_arrival_raw = soup.find('div', class_=["routeEnd","routeEndAdditional"]).find('span', class_=["delay", "delayOnTime"])
    if delay_arrival_raw:
        delay_arrival = calculate_delay(data['arrival'],
                                        delay_arrival_raw.text)
    else:
        delay_arrival = 0

    # save the parsed data
    if delay_departure + delay_arrival == 0:
        data['ontime'] = True
    else:
        data['ontime'] = False
    data['delay'] = {
        'delay_departure': int(delay_departure),
        'delay_arrival': int(delay_arrival)
    }

    # TODO: this should not be hardcoded!
    data['canceled'] = False

    return data


Instruction:
Implement a Python 3 function for
calculating the delay between original and delayed

Response:
def calculate_delay(original, delay):
    """
        Calculate the delay
    """
    original = datetime.strptime(original, '%H:%M')
    delayed = datetime.strptime(delay, '%H:%M')
    diff = delayed - original
    return diff.total_seconds() // 60


Instruction:
Here you have a function in Python 3, explain what it does
def stations(self, station, limit=10):
        query = {
            'start': 1,
            'S': station + '?',
            'REQ0JourneyStopsB': limit
        }
        rsp = requests.get('http://reiseauskunft.bahn.de/bin/ajax-getstop.exe/dn', params=query)
        return parse_stations(rsp.text)

Response: Returns a list of stations for a given station


Instruction:
Can you write a function in Python 3 where it
finds all connections between two stations

Response:
def connections(self, origin, destination, dt=datetime.now(), only_direct=False):
        """
        Find connections between two stations

        Args:
            origin (str): origin station
            destination (str): destination station
            dt (datetime): date and time for query
            only_direct (bool): only direct connections
        """
        query = {
            'S': origin,
            'Z': destination,
            'date': dt.strftime("%d.%m.%y"),
            'time': dt.strftime("%H:%M"),
            'start': 1,
            'REQ0JourneyProduct_opt0': 1 if only_direct else 0
        }
        rsp = requests.get('http://mobile.bahn.de/bin/mobil/query.exe/dox?', params=query)
        return parse_connections(rsp.text)


Instruction:
Create a Python 3 function for
scattering elements of sequence into n blocks.

Response:
def _scatter(sequence, n):
  """Scatters elements of ``sequence`` into ``n`` blocks."""
  chunklen = int(math.ceil(float(len(sequence)) / float(n)))

  return [
    sequence[ i*chunklen : (i+1)*chunklen ] for i in range(n)
  ]


Instruction:
How would you explain what the following Python 3 function does
def lease(self, seconds=600, num_tasks=1, tag=None):
    tag = tag if tag else None
    tasks = self._api.lease(
      numTasks=num_tasks,
      seconds=seconds,
      groupByTag=(tag is not None),
      tag=tag,
    )

    if not len(tasks):
      raise QueueEmpty

    task = tasks[0]
    return totask(task)

Response: Acquires a lease on the topmost N unowned tasks in the specified queue.


Instruction:
Make a summary of the following Python 3 code
def purge(self):
    try:
      return self._api.purge()
    except AttributeError:
      while True:
        lst = self.list()
        if len(lst) == 0:
          break

        for task in lst:
          self.delete(task)
        self.wait()
      return self

Response: Deletes all tasks in the queue.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def poll(
    self, lease_seconds=LEASE_SECONDS, tag=None,
    verbose=False, execute_args=[], execute_kwargs={},
    stop_fn=None, backoff_exceptions=[], min_backoff_window=30,
    max_backoff_window=120, log_fn=None
  ):
    """
    Poll a queue until a stop condition is reached (default forever). Note
    that this function is not thread safe as it requires a global variable
    to intercept SIGINT.
    lease_seconds: each task should be leased for this many seconds
    tag: if specified, query for only tasks that match this tag
    execute_args / execute_kwargs: pass these arguments to task execution
    backoff_exceptions: A list of exceptions that instead of causing a crash,
      instead cause the polling to back off for an increasing exponential
      random window.
    min_backoff_window: The minimum sized window (in seconds) to select a
      random backoff time.
    max_backoff_window: The window doubles each retry. This is the maximum value
      in seconds.
    stop_fn: A boolean returning function that accepts no parameters. When
      it returns True, the task execution loop will terminate. It is evaluated
      once after every task.
    log_fn: Feed error messages to this function, default print (when verbose is enabled).
    verbose: print out the status of each step
    Return: number of tasks executed
    """
    global LOOP

    if not callable(stop_fn) and stop_fn is not None:
      raise ValueError("stop_fn must be a callable. " + str(stop_fn))
    elif not callable(stop_fn):
      stop_fn = lambda: False

    def random_exponential_window_backoff(n):
      n = min(n, min_backoff_window)
      # 120 sec max b/c on avg a request every ~250msec if 500 containers
      # in contention which seems like a quite reasonable volume of traffic
      # to handle
      high = min(2 ** n, max_backoff_window)
      return random.uniform(0, high)

    def printv(*args, **kwargs):
      if verbose:
        print(*args, **kwargs)

    LOOP = True

    def sigint_handler(signum, frame):
      global LOOP
      printv("Interrupted. Exiting after this task completes...")
      LOOP = False

    prev_sigint_handler = signal.getsignal(signal.SIGINT)
    signal.signal(signal.SIGINT, sigint_handler)

    if log_fn is None:
      log_fn = printv

    tries = 0
    executed = 0

    backoff = False
    backoff_exceptions = tuple(list(backoff_exceptions) + [ QueueEmpty ])

    while LOOP:
      task = 'unknown' # for error message prior to leasing
      try:
        task = self.lease(seconds=int(lease_seconds))
        tries += 1
        printv(task)
        task.execute(*execute_args, **execute_kwargs)
        executed += 1
        printv("Delete enqueued task...")
        self.delete(task)
        log_fn('INFO', task , "succesfully executed")
        tries = 0
      except backoff_exceptions:
        backoff = True
      except Exception as e:
        printv('ERROR', task, "raised {}\n {}".format(e , traceback.format_exc()))
        raise #this will restart the container in kubernetes

      if stop_fn():
        break

      if backoff:
        time.sleep(random_exponential_window_backoff(tries))

      backoff = False

    printv("Task execution loop exited.")
    signal.signal(signal.SIGINT, prev_sigint_handler)

    return executed

Response: Poll a queue until a stop condition is reached.


Instruction:
Can you generate the documentation for the following Python 3 function
def insert(self, task, args=[], kwargs={}, delay_seconds=0):
    body = {
      "payload": task.payload(),
      "queueName": self._queue_name,
      "groupByTag": True,
      "tag": task.__class__.__name__
    }

    def cloud_insertion(api):
      api.insert(body, delay_seconds)

    if len(self._threads):
      self.put(cloud_insertion)
    else:
      cloud_insertion(self._api)

    return self

Response: Insert a task into an existing queue.


Instruction:
Write a Python 3 script to
delete a task from a TaskQueue.

Response:
def delete(self, task_id):
    """Deletes a task from a TaskQueue."""
    if isinstance(task_id, RegisteredTask):
      task_id = task_id.id

    def cloud_delete(api):
      api.delete(task_id)

    if len(self._threads):
      self.put(cloud_delete)
    else:
      cloud_delete(self._api)

    return self


Instruction:
Given the following Python 3 function, write the documentation
def insert(self, task, args=[], kwargs={}, delay_seconds=0):
    body = {
      "payload": task.payload(),
      "queueName": self._queue_name,
      "groupByTag": True,
      "tag": task.__class__.__name__
    }

    def cloud_insertion():
      self._api.insert(body, delay_seconds)

    self._pool.spawn(cloud_insertion)

    return self

Response: Insert a task into the queue.


Instruction:
Implement a function in Python 3 to
check if this is a user upload from the user

Response:
def is_upload(action):
    """Checks if this should be a user upload

    :param action:
    :return: True if this is a file we intend to upload from the user
    """
    return 'r' in action.type._mode and (action.default is None or
                                         getattr(action.default, 'name') not in (sys.stderr.name, sys.stdout.name))


Instruction:
Given the following Python 3 function, write the documentation
def to_django(self):
        exclude = {'name', 'model'}
        field_module = 'models'
        django_kwargs = {}
        if self.node_attrs['model'] == 'CharField':
            django_kwargs['max_length'] = 255
        django_kwargs['blank'] = not self.node_attrs['required']
        try:
            django_kwargs['default'] = self.node_attrs['value']
        except KeyError:
            pass
        return u'{0} = {1}.{2}({3})'.format(self.node_attrs['name'], field_module, self.node_attrs['model'],
                                           ', '.join(['{0}={1}'.format(i,v) for i,v in six.iteritems(django_kwargs)]),)

Response: Convert the node attributes to a django representation


Instruction:
Write a Python 3 script to
return a modified dict where all the keys that are anything but str get converted to str.

Response:
def str_dict_keys(a_dict):
    """return a modified dict where all the keys that are anything but str get
    converted to str.
    E.g.

      >>> result = str_dict_keys({u'name': u'Peter', u'age': 99, 1: 2})
      >>> # can't compare whole dicts in doctests
      >>> result['name']
      u'Peter'
      >>> result['age']
      99
      >>> result[1]
      2

    The reason for this is that in Python <= 2.6.4 doing
    ``MyClass(**{u'name': u'Peter'})`` would raise a TypeError

    Note that only unicode types are converted to str types.
    The reason for that is you might have a class that looks like this::

        class Option(object):
            def __init__(self, foo=None, bar=None, **kwargs):
                ...

    And it's being used like this::

        Option(**{u'foo':1, u'bar':2, 3:4})

    Then you don't want to change that {3:4} part which becomes part of
    `**kwargs` inside the __init__ method.
    Using integers as parameter keys is a silly example but the point is that
    due to the python 2.6.4 bug only unicode keys are converted to str.
    """
    new_dict = {}
    for key in a_dict:
        if six.PY2 and isinstance(key, six.text_type):
            new_dict[str(key)] = a_dict[key]
        else:
            new_dict[key] = a_dict[key]
    return new_dict


Instruction:
How would you explain what the following Python 3 function does
def str_to_boolean(input_str):
    if not isinstance(input_str, six.string_types):
        raise ValueError(input_str)
    input_str = str_quote_stripper(input_str)
    return input_str.lower() in ("true", "t", "1", "y", "yes")

Response: a conversion function for boolean


Instruction:
How would you explain what the following Python 3 function does
def str_to_python_object(input_str):
    if not input_str:
        return None
    if six.PY3 and isinstance(input_str, six.binary_type):
        input_str = to_str(input_str)
    if not isinstance(input_str, six.string_types):
        # gosh, we didn't get a string, we can't convert anything but strings
        # we're going to assume that what we got is actually what was wanted
        # as the output
        return input_str
    input_str = str_quote_stripper(input_str)
    if '.' not in input_str and input_str in known_mapping_str_to_type:
        return known_mapping_str_to_type[input_str]
    parts = [x.strip() for x in input_str.split('.') if x.strip()]
    try:
        try:
            # first try as a complete module
            package = __import__(input_str)
        except ImportError:
            # it must be a class from a module
            if len(parts) == 1:
                # since it has only one part, it must be a class from __main__
                parts = ('__main__', input_str)
            package = __import__('.'.join(parts[:-1]), globals(), locals(), [])
        obj = package
        for name in parts[1:]:
            obj = getattr(obj, name)
        return obj
    except AttributeError as x:
        raise CannotConvertError("%s cannot be found" % input_str)
    except ImportError as x:
        raise CannotConvertError(str(x))

Response: a conversion that will import a module and class name


Instruction:
How would you code a function in Python 3 to
convert a comma delimited list of class names into actual classes within a numbered namespace

Response:
def str_to_classes_in_namespaces(
    template_for_namespace="cls%d",
    name_of_class_option='cls',
    instantiate_classes=False
):
    """take a comma delimited  list of class names, convert each class name
    into an actual class as an option within a numbered namespace.  This
    function creates a closure over a new function.  That new function,
    in turn creates a class derived from RequiredConfig.  The inner function,
    'class_list_converter', populates the InnerClassList with a Namespace for
    each of the classes in the class list.  In addition, it puts the each class
    itself into the subordinate Namespace.  The requirement discovery mechanism
    of configman then reads the InnerClassList's requried config, pulling in
    the namespaces and associated classes within.

    For example, if we have a class list like this: "Alpha, Beta", then this
    converter will add the following Namespaces and options to the
    configuration:

        "cls0" - the subordinate Namespace for Alpha
        "cls0.cls" - the option containing the class Alpha itself
        "cls1" - the subordinate Namespace for Beta
        "cls1.cls" - the option containing the class Beta itself

    Optionally, the 'class_list_converter' inner function can embue the
    InnerClassList's subordinate namespaces with aggregates that will
    instantiate classes from the class list.  This is a convenience to the
    programmer who would otherwise have to know ahead of time what the
    namespace names were so that the classes could be instantiated within the
    context of the correct namespace.  Remember the user could completely
    change the list of classes at run time, so prediction could be difficult.

        "cls0" - the subordinate Namespace for Alpha
        "cls0.cls" - the option containing the class Alpha itself
        "cls0.cls_instance" - an instance of the class Alpha
        "cls1" - the subordinate Namespace for Beta
        "cls1.cls" - the option containing the class Beta itself
        "cls1.cls_instance" - an instance of the class Beta

    parameters:
        template_for_namespace - a template for the names of the namespaces
                                 that will contain the classes and their
                                 associated required config options.  The
                                 namespaces will be numbered sequentially.  By
                                 default, they will be "cls1", "cls2", etc.
        class_option_name - the name to be used for the class option within
                            the nested namespace.  By default, it will choose:
                            "cls1.cls", "cls2.cls", etc.
        instantiate_classes - a boolean to determine if there should be an
                              aggregator added to each namespace that
                              instantiates each class.  If True, then each
                              Namespace will contain elements for the class, as
                              well as an aggregator that will instantiate the
                              class.
                              """

    # these are only used within this method.  No need to pollute the module
    # scope with them and avoid potential circular imports
    from configman.namespace import Namespace
    from configman.required_config import RequiredConfig

    #--------------------------------------------------------------------------
    def class_list_converter(class_list_str):
        """This function becomes the actual converter used by configman to
        take a string and convert it into the nested sequence of Namespaces,
        one for each class in the list.  It does this by creating a proxy
        class stuffed with its own 'required_config' that's dynamically
        generated."""
        if isinstance(class_list_str, six.string_types):
            class_list = [x.strip() for x in class_list_str.split(',')]
            if class_list == ['']:
                class_list = []
        else:
            raise TypeError('must be derivative of %s' % six.string_types)

        #======================================================================
        class InnerClassList(RequiredConfig):
            """This nested class is a proxy list for the classes.  It collects
            all the config requirements for the listed classes and places them
            each into their own Namespace.
            """
            # we're dynamically creating a class here.  The following block of
            # code is actually adding class level attributes to this new class
            required_config = Namespace()  # 1st requirement for configman
            subordinate_namespace_names = []  # to help the programmer know
                                              # what Namespaces we added
            namespace_template = template_for_namespace  # save the template
                                                         # for future reference
            class_option_name = name_of_class_option  # save the class's option
                                                      # name for the future
            # for each class in the class list
            for namespace_index, a_class in enumerate(class_list):
                # figure out the Namespace name
                namespace_name = template_for_namespace % namespace_index
                subordinate_namespace_names.append(namespace_name)
                # create the new Namespace
                required_config[namespace_name] = Namespace()
                # add the option for the class itself
                required_config[namespace_name].add_option(
                    name_of_class_option,
                    #doc=a_class.__doc__  # not helpful if too verbose
                    default=a_class,
                    from_string_converter=class_converter
                )
                if instantiate_classes:
                    # add an aggregator to instantiate the class
                    required_config[namespace_name].add_aggregation(
                        "%s_instance" % name_of_class_option,
                        lambda c, lc, a: lc[name_of_class_option](lc)
                    )

            @classmethod
            def to_str(cls):
                """this method takes this inner class object and turns it back
                into the original string of classnames.  This is used
                primarily as for the output of the 'help' option"""
                return ', '.join(
                    py_obj_to_str(v[name_of_class_option].value)
                    for v in cls.get_required_config().values()
                    if isinstance(v, Namespace)
                )

        return InnerClassList  # result of class_list_converter
    return class_list_converter


Instruction:
Make a summary of the following Python 3 code
def str_to_list(
    input_str,
    item_converter=lambda x: x,
    item_separator=',',
    list_to_collection_converter=None,
):
    """ a conversion function for list
    """
    if not isinstance(input_str, six.string_types):
        raise ValueError(input_str)
    input_str = str_quote_stripper(input_str)
    result = [
        item_converter(x.strip())
        for x in input_str.split(item_separator) if x.strip()
    ]
    if list_to_collection_converter is not None:
        return list_to_collection_converter(result)
    return result

Response: a conversion function for list


Instruction:
Make a summary of the following Python 3 code
def arbitrary_object_to_string(a_thing):
    # is it None?
    if a_thing is None:
        return ''
    # is it already a string?
    if isinstance(a_thing, six.string_types):
        return a_thing
    if six.PY3 and isinstance(a_thing, six.binary_type):
        try:
            return a_thing.decode('utf-8')
        except UnicodeDecodeError:
            pass
    # does it have a to_str function?
    try:
        return a_thing.to_str()
    except (AttributeError, KeyError, TypeError):
        # AttributeError - no to_str function?
        # KeyError - DotDict has no to_str?
        # TypeError - problem converting
        # nope, no to_str function
        pass
    # is this a type proxy?
    try:
        return arbitrary_object_to_string(a_thing.a_type)
    except (AttributeError, KeyError, TypeError):
        #
        # nope, no a_type property
        pass
    # is it a built in?
    try:
        return known_mapping_type_to_str[a_thing]
    except (KeyError, TypeError):
        # nope, not a builtin
        pass
    # is it something from a loaded module?
    try:
        if a_thing.__module__ not in ('__builtin__', 'builtins', 'exceptions'):
            if a_thing.__module__ == "__main__":
                module_name = (
                    sys.modules['__main__']
                    .__file__[:-3]
                    .replace('/', '.')
                    .strip('.')
                )
            else:
                module_name = a_thing.__module__
            return "%s.%s" % (module_name, a_thing.__name__)
    except AttributeError:
        # nope, not one of these
        pass
    # maybe it has a __name__ attribute?
    try:
        return a_thing.__name__
    except AttributeError:
        # nope, not one of these
        pass
    # punt and see what happens if we just cast it to string
    return str(a_thing)

Response: take a python object of some sort and convert it into a human readable string.


Instruction:
Can you generate the documentation for the following Python 3 function
def to_source(node, indent_with=' ' * 4, add_line_information=False):
    generator = SourceGenerator(indent_with, add_line_information)
    generator.visit(node)
    return ''.join(str(s) for s in generator.result)

Response: This function can convert a node tree into a python sourcecode string.


Instruction:
Can you tell what is the following Python 3 function doing
def connection(self, name=None):
        if not name:
            name = threading.currentThread().getName()
        if name in self.pool:
            return self.pool[name]
        self.pool[name] = FakeDatabaseConnection(self.dsn)
        return self.pool[name]

Response: return a named connection.


Instruction:
How would you explain what the following Python 3 function does
def close_connection(self, connection, force=False):
        if force:
            print('PostgresPooled - delegating connection closure')
            try:
                super(PostgresPooled, self).close_connection(connection,
                                                                  force)
            except self.operational_exceptions:
                print('PostgresPooled - failed closing')
            for name, conn in self.pool.iteritems():
                if conn is connection:
                    break
            del self.pool[name]
        else:
            print('PostgresPooled - refusing to close connection')

Response:
overriding the baseclass function this routine will decline to
        close a connection at the end of a transaction context. This routine will close the connection at the end of a transaction context.


Instruction:
How would you implement a function in Python 3 that
executes a function within the context of a transaction

Response:
def do_transaction(self, function, *args, **kwargs):
        """execute a function within the context of a transaction"""
        with self.config.db_transaction() as trans:
            function(trans, *args, **kwargs)


Instruction:
Write a Python 3 function for
sleeping for the specified number of seconds logging every wait_log_interval seconds with progress info.

Response:
def responsive_sleep(self, seconds, wait_reason=''):
        """Sleep for the specified number of seconds, logging every
        'wait_log_interval' seconds with progress info."""
        for x in range(int(seconds)):
            if (self.config.wait_log_interval and
                not x % self.config.wait_log_interval):
                print('%s: %dsec of %dsec' % (wait_reason,
                                              x,
                                              seconds))
            time.sleep(1.0)


Instruction:
Given the following Python 3 function, write the documentation
def do_transaction(self, function, *args, **kwargs):
        for wait_in_seconds in self.backoff_generator():
            try:
                with self.config.db_transaction() as trans:
                    function(trans, *args, **kwargs)
                    trans.commit()
                    break
            except self.config.db_transaction.operational_exceptions:
                pass
            print(('failure in transaction - retry in %s seconds' %
                   wait_in_seconds))
            self.responsive_sleep(wait_in_seconds,
                                  "waiting for retry after failure in "
                                  "transaction")

Response: execute a function within the context of a transaction


Instruction:
Can you tell what is the following Python 3 function doing
def _expand_files(self, file_name, original_path, indent=""):
        expanded_file_contents = []
        with open(file_name) as f:
            for a_line in f:
                match = ConfigObjWithIncludes._include_re.match(a_line)
                if match:
                    include_file = match.group(2)
                    include_file = os.path.join(
                        original_path,
                        include_file
                    )
                    new_lines = self._expand_files(
                        include_file,
                        os.path.dirname(include_file),
                        indent + match.group(1)
                    )
                    expanded_file_contents.extend(new_lines)
                else:
                    expanded_file_contents.append(indent + a_line.rstrip())
        return expanded_file_contents

Response: This function recursively finds all the contents of a file and returns a list of the contents of the file.


Instruction:
How would you explain what the following Python 3 function does
def get_values(self, config_manager, ignore_mismatches, obj_hook=DotDict):
        if self.delayed_parser_instantiation:
            try:
                app = config_manager._get_option('admin.application')
                source = "%s%s" % (app.value.app_name, file_name_extension)
                self.config_obj = configobj.ConfigObj(source)
                self.delayed_parser_instantiation = False
            except AttributeError:
                # we don't have enough information to get the ini file
                # yet.  we'll ignore the error for now
                return obj_hook()  # return empty dict of the obj_hook type
        if isinstance(self.config_obj, obj_hook):
            return self.config_obj
        return obj_hook(initializer=self.config_obj)

Response: Return a nested dictionary representing the values in the ini file.


Instruction:
Make a summary of the following Python 3 code
def error(self, message):
        if (
            "not allowed" in message
            or "ignored" in message
            or "expected" in message
            or "invalid" in message
            or self.add_help
        ):
            # when we have "help" then we must also have proper error
            # processing.  Without "help", we suppress the errors by
            # doing nothing here
            super(IntermediateConfigmanParser, self).error(message)

Response: we can t suppress errors by the expansion process.


Instruction:
How would you explain what the following Python 3 function does
def configman_keys(a_mapping):
    configmanized_keys_dict = DotDict()
    for k, v in iteritems_breadth_first(a_mapping):
        if '__' in k and k != k.upper():
            k = k.replace('__', '.')
        configmanized_keys_dict[k] = v
    return configmanized_keys_dict

Response:
return a DotDict that is a copy of the provided mapping with keys
     transformed into a configman compatible form


Instruction:
Make a summary of the following Python 3 code
def create_key_translating_dot_dict(
    new_class_name,
    translation_tuples,
    base_class=DotDict
):
    """this function will generate a DotDict derivative class that has key
    translation built in.  If the key is not found, translations (as specified
    by the translation_tuples) are performed on the key and the lookup is
    tried again.  Only on failure of this second lookup will the KeyError
    exception be raised.

    parameters:
        new_class_name - the name of the returned class
        translation_tuples - a sequence of 2-tuples of the form:
                             (original_substring, substitution_string)
        base_class - the baseclass on which this new class is to be based
    """
    #==========================================================================
    class DotDictWithKeyTranslations(base_class):

        def __init__(self, *args, **kwargs):
            self.__dict__['_translation_tuples'] = translation_tuples
            super(DotDictWithKeyTranslations, self).__init__(*args, **kwargs)

        #----------------------------------------------------------------------
        @memoize()
        def _translate_key(self, key):
            for original, replacement in self._translation_tuples:
                key = key.replace(original, replacement)
            return key

        #----------------------------------------------------------------------
        def assign(self, key, value):
            super(DotDictWithKeyTranslations, self).assign(
                self._translate_key(key),
                value
            )

        #----------------------------------------------------------------------
        def __setattr__(self, key, value):
            super(DotDictWithKeyTranslations, self).__setattr__(
                self._translate_key(key),
                value
            )

        #----------------------------------------------------------------------
        def __getattr__(self, key):
            alt_key = self._translate_key(key)
            if alt_key == key:
                return super(DotDictWithKeyTranslations, self).__getattr__(key)
            try:
                return getattr(self, alt_key)
            except KeyError:
                raise KeyError(key)

        #----------------------------------------------------------------------
        def __delattr__(self, key):
            super(DotDictWithKeyTranslations, self).__delattr__(
                self._translate_key(key)
            )

    if six.PY2:
        new_class_name = six.binary_type(new_class_name)
    DotDictWithKeyTranslations.__name__ = new_class_name
    return DotDictWithKeyTranslations

Response: This function creates a new class that has key translation built in.


Instruction:
Make a summary of the following Python 3 code
def memoize(max_cache_size=1000):
    def wrapper(f):
        @wraps(f)
        def fn(*args, **kwargs):
            if kwargs:
                key = (args, tuple(kwargs.items()))
            else:
                key = args
            try:
                return fn.cache[key]
            except KeyError:
                if fn.count >= max_cache_size:
                    fn.cache = {}
                    fn.count = 0
                result = f(*args, **kwargs)
                fn.cache[key] = result
                fn.count += 1
                return result
            except TypeError:
                return f(*args, **kwargs)
        fn.cache = {}
        fn.count = 0
        return fn
    return wrapper

Response: A function decorator that creates a cache that can be used to store the result of a function.


Instruction:
How would you explain what the following Python 3 function does
def copy(self):
        o = Option(
            name=self.name,
            default=self.default,
            doc=self.doc,
            from_string_converter=self.from_string_converter,
            to_string_converter=self.to_string_converter,
            value=self.value,
            short_form=self.short_form,
            exclude_from_print_conf=self.exclude_from_print_conf,
            exclude_from_dump_conf=self.exclude_from_dump_conf,
            is_argument=self.is_argument,
            likely_to_be_changed=self.likely_to_be_changed,
            not_for_definition=self.not_for_definition,
            reference_value_from=self.reference_value_from,
            secret=self.secret,
            has_changed=self.has_changed,
            foreign_data=self.foreign_data,
        )
        return o

Response: returns a copy of the option


Instruction:
How would you code a function in Python 3 to
return a config as a context that calls close on every item when it goes out of scope

Response:
def context(self, mapping_class=DotDictWithAcquisition):
        """return a config as a context that calls close on every item when
        it goes out of scope"""
        config = None
        try:
            config = self.get_config(mapping_class=mapping_class)
            yield config
        finally:
            if config:
                self._walk_and_close(config)


Instruction:
Implement a Python 3 function for
printing a summary of the current command line options.

Response:
def output_summary(self, output_stream=sys.stdout):
        """outputs a usage tip and the list of acceptable commands.
        This is useful as the output of the 'help' option.

        parameters:
            output_stream - an open file-like object suitable for use as the
                            target of a print function
        """
        if self.app_name or self.app_description:
            print('Application: ', end='', file=output_stream)
        if self.app_name:
            print(self.app_name, self.app_version, file=output_stream)
        if self.app_description:
            print(self.app_description, file=output_stream)
        if self.app_name or self.app_description:
            print('', file=output_stream)

        names_list = self.get_option_names()
        print(
            "usage:\n%s [OPTIONS]... " % self.app_invocation_name,
            end='', file=output_stream
        )
        bracket_count = 0
        # this section prints the non-switch command line arguments
        for key in names_list:
            an_option = self.option_definitions[key]
            if an_option.is_argument:
                if an_option.default is None:
                    # there's no option, assume the user must set this
                    print(an_option.name, end='', file=output_stream)
                elif (
                    inspect.isclass(an_option.value)
                    or inspect.ismodule(an_option.value)
                ):
                    # this is already set and it could have expanded, most
                    # likely this is a case where a sub-command has been
                    # loaded and we're looking to show the help for it.
                    # display show it as a constant already provided rather
                    # than as an option the user must provide
                    print(an_option.default, end='', file=output_stream)
                else:
                    # this is an argument that the user may alternatively
                    # provide
                    print("[ %s" % an_option.name, end='', file=output_stream)
                    bracket_count += 1
        print(']' * bracket_count, '\n', file=output_stream)

        names_list.sort()
        if names_list:
            print('OPTIONS:', file=output_stream)

        pad = ' ' * 4

        for name in names_list:
            if name in self.options_banned_from_help:
                continue
            option = self._get_option(name)

            line = ' ' * 2  # always start with 2 spaces
            if option.short_form:
                line += '-%s, ' % option.short_form
            line += '--%s' % name
            line += '\n'

            doc = option.doc if option.doc is not None else ''
            if doc:
                line += '%s%s\n' % (pad, doc)
            try:
                value = option.value
                type_of_value = type(value)
                converter_function = to_string_converters[type_of_value]
                default = converter_function(value)
            except KeyError:
                default = option.value
            if default is not None:
                if (
                    (option.secret or 'password' in name.lower()) and
                    not self.option_definitions.admin.expose_secrets.default
                ):
                    default = '*********'
                if name not in ('help',):
                    # don't bother with certain dead obvious ones
                    line += '%s(default: %s)\n' % (pad, default)

            print(line, file=output_stream)


Instruction:
Explain what the following Python 3 code does
def print_conf(self):

        config_file_type = self._get_option('admin.print_conf').value

        @contextlib.contextmanager
        def stdout_opener():
            yield sys.stdout

        skip_keys = [
            k for (k, v)
            in six.iteritems(self.option_definitions)
            if isinstance(v, Option) and v.exclude_from_print_conf
        ]
        self.write_conf(config_file_type, stdout_opener, skip_keys=skip_keys)

Response: write a config file to the pathname specified in the parameter.


Instruction:
Can you create a Python 3 function that
writes a config file to the pathname specified in the parameter.

Response:
def dump_conf(self, config_pathname=None):
        """write a config file to the pathname specified in the parameter.  The
        file extention determines the type of file written and must match a
        registered type.

        parameters:
            config_pathname - the full path and filename of the target config
                               file."""

        if not config_pathname:
            config_pathname = self._get_option('admin.dump_conf').value

        opener = functools.partial(open, config_pathname, 'w')
        config_file_type = os.path.splitext(config_pathname)[1][1:]

        skip_keys = [
            k for (k, v)
            in six.iteritems(self.option_definitions)
            if isinstance(v, Option) and v.exclude_from_dump_conf
        ]

        self.write_conf(config_file_type, opener, skip_keys=skip_keys)


Instruction:
Can you create a Python 3 function that
writes a configuration file to a file - like object.

Response:
def write_conf(self, config_file_type, opener, skip_keys=None):
        """write a configuration file to a file-like object.

        parameters:
            config_file_type - a string containing a registered file type OR
                               a for_XXX module from the value_source
                               package.  Passing in an string that is
                               unregistered will result in a KeyError
            opener - a callable object or function that returns a file like
                     object that works as a context in a with statement."""

        blocked_keys = self.keys_blocked_from_output
        if skip_keys:
            blocked_keys.extend(skip_keys)

        if blocked_keys:
            option_defs = self.option_definitions.safe_copy()
            for a_blocked_key in blocked_keys:
                try:
                    del option_defs[a_blocked_key]
                except (AttributeError, KeyError):
                    # okay that key isn't here
                    pass
            # remove empty namespaces
            all_keys = [k for k in
                        option_defs.keys_breadth_first(include_dicts=True)]
            for key in all_keys:
                candidate = option_defs[key]
                if (isinstance(candidate, Namespace) and not len(candidate)):
                    del option_defs[key]
        else:
            option_defs = self.option_definitions

        # find all of the secret options and overwrite their values with
        # '*' * 16
        if not self.option_definitions.admin.expose_secrets.default:
            for a_key in option_defs.keys_breadth_first():
                an_option = option_defs[a_key]
                if (
                    (not a_key.startswith('admin'))
                    and isinstance(an_option, Option)
                    and an_option.secret
                ):
                    # force the option to be a string of *
                    option_defs[a_key].value = '*' * 16
                    option_defs[a_key].from_string_converter = str

        dispatch_request_to_write(config_file_type, option_defs, opener)


Instruction:
Can you tell what is the following Python 3 function doing
def log_config(self, logger):

        logger.info("app_name: %s", self.app_name)
        logger.info("app_version: %s", self.app_version)
        logger.info("current configuration:")
        config = [(key, self.option_definitions[key].value)
                  for key in self.option_definitions.keys_breadth_first()
                  if key not in self.keys_blocked_from_output]
        config.sort()
        for key, val in config:
            if (
                self.option_definitions[key].secret
                or 'password' in key.lower()
            ):
                logger.info('%s: *********', key)
            else:
                try:
                    logger.info('%s: %s', key,
                                to_string_converters[type(key)](val))
                except KeyError:
                    logger.info('%s: %s', key, val)

Response: write out the current configuration to a log - like object.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_option_names(self):
        return [x for x in self.option_definitions.keys_breadth_first()
                if isinstance(self.option_definitions[x], Option)]

Response: returns a list of fully qualified option names.


Instruction:
Can you tell what is the following Python 3 function doing
def _create_reference_value_options(self, keys, finished_keys):
        # a set of known reference_value_from_links
        set_of_reference_value_option_names = set()
        for key in keys:
            if key in finished_keys:
                continue
            an_option = self.option_definitions[key]
            if an_option.reference_value_from:

                fully_qualified_reference_name = '.'.join((
                    an_option.reference_value_from,
                    an_option.name
                ))
                if fully_qualified_reference_name in keys:
                    continue  # this referenced value has already been defined
                              # no need to repeat it - skip on to the next key
                reference_option = an_option.copy()
                reference_option.reference_value_from = None
                reference_option.name = fully_qualified_reference_name
                # wait, aren't we setting a fully qualified dotted name into
                # the name field?  Yes, 'add_option' below sees that
                # full pathname and does the right thing with it to ensure
                # that the reference_option is created within the
                # correct namespace
                set_of_reference_value_option_names.add(
                    fully_qualified_reference_name
                )
                self.option_definitions.add_option(reference_option)

        for a_reference_value_option_name in set_of_reference_value_option_names:
            for x in range(a_reference_value_option_name.count('.')):
                namespace_path = \
                    a_reference_value_option_name.rsplit('.', x + 1)[0]
                self.option_definitions[namespace_path].ref_value_namespace()

        return set_of_reference_value_option_names

Response: This method creates the reference_value_from links and name fields for the option definitions.


Instruction:
Can you implement a function in Python 3 that
checks for mismatches from value sources

Response:
def _check_for_mismatches(self, known_keys):
        """check for bad options from value sources"""
        for a_value_source in self.values_source_list:
            try:
                if a_value_source.always_ignore_mismatches:
                    continue
            except AttributeError:
                # ok, this values source doesn't have the concept
                # always igoring mismatches, we won't tolerate mismatches
                pass
            # we want to fetch the keys from the value sources so that we can
            # check for mismatches.  Commandline value sources, are different,
            # we never want to allow unmatched keys from the command line.
            # By detecting if this value source is a command line source, we
            # can employ the command line's own mismatch detection.  The
            # boolean 'allow_mismatches' controls application of the tollerance
            # for mismatches.
            if hasattr(a_value_source, 'command_line_value_source'):
                allow_mismatches = False
            else:
                allow_mismatches = True
            # make a set of all the keys from a value source in the form
            # of strings like this: 'x.y.z'
            value_source_mapping = a_value_source.get_values(
                self,
                allow_mismatches,
                self.value_source_object_hook
            )
            value_source_keys_set = set([
                k for k in
                DotDict(value_source_mapping).keys_breadth_first()
            ])
            # make a set of the keys that didn't match any of the known
            # keys in the requirements
            unmatched_keys = value_source_keys_set.difference(known_keys)
            # some of the unmatched keys may actually be ok because the were
            # used during acquisition.
            # remove keys of the form 'y.z' if they match a known key of the
            # form 'x.y.z'
            for key in unmatched_keys.copy():
                key_is_okay = six.moves.reduce(
                    lambda x, y: x or y,
                    (known_key.endswith(key) for known_key in known_keys)
                )
                if key_is_okay:
                    unmatched_keys.remove(key)
            # anything left in the unmatched_key set is a badly formed key.
            # issue a warning
            if unmatched_keys:
                if self.option_definitions.admin.strict.default:
                    # raise hell...
                    if len(unmatched_keys) > 1:
                        raise NotAnOptionError(
                            "%s are not valid Options" % unmatched_keys
                        )
                    elif len(unmatched_keys) == 1:
                        raise NotAnOptionError(
                            "%s is not a valid Option" % unmatched_keys.pop()
                        )
                else:
                    warnings.warn(
                        'Invalid options: %s' % ', '.join(sorted(unmatched_keys))
                    )


Instruction:
Given the following Python 3 function, write the documentation
def _generate_config(self, mapping_class):
        config = mapping_class()
        self._walk_config_copy_values(
            self.option_definitions,
            config,
            mapping_class
        )
        return config

Response: This routine generates a copy of the DotDict based config


Instruction:
Given the following Python 3 function, write the documentation
def close(self):
        print("PGPooledTransaction - shutting down connection pool")
        for name, conn in self.pool.iteritems():
            conn.close()
            print("PGPooledTransaction - connection %s closed" % name)

Response: close all pooled connections


Instruction:
Can you generate a brief explanation for the following Python 3 code
def find_action_name_by_value(registry, target_action_instance):
    target_type = type(target_action_instance)
    for key, value in six.iteritems(registry['action']):
        if value is target_type:
            if key is None:
                return 'store'
            return key
    return None

Response:
This routine finds the name of an action class with a human readable
    string is exposed externally only at the time of argument definitions.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_args_and_values(parser, an_action):
    args = inspect.getargspec(an_action.__class__.__init__).args
    kwargs = dict(
        (an_attr, getattr(an_action, an_attr))
        for an_attr in args
        if (
            an_attr not in ('self', 'required')
            and getattr(an_action, an_attr) is not None
        )
    )
    action_name = find_action_name_by_value(
        parser._optionals._registries,
        an_action
    )
    if 'required' in kwargs:
        del kwargs['required']
    kwargs['action'] = action_name
    if 'option_strings' in kwargs:
        args = tuple(kwargs['option_strings'])
        del kwargs['option_strings']
    else:
        args = ()
    return args, kwargs

Response: this rountine attempts to reconstruct the kwargs that were used in the action object


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_namespace(self, name, a_namespace):
        # save a local copy of the namespace
        self.namespaces[name] = a_namespace
        # iterate through the namespace branding each of the options with the
        # name of the subparser to which they belong
        for k in a_namespace.keys_breadth_first():
            an_option = a_namespace[k]
            if not an_option.foreign_data:
                an_option.foreign_data = DotDict()
            an_option.foreign_data['argparse.owning_subparser_name'] = name

Response: add a namespace to the argparse object


Instruction:
Make a summary of the following Python 3 code
def get_required_config(self):
        required_config = Namespace()
        # add current options to a copy of required config
        for k, v in iteritems_breadth_first(self.required_config):
            required_config[k] = v
        # get any option found in any subparsers
        try:
            subparser_namespaces = (
                self.configman_subparsers_option.foreign_data
                .argparse.subprocessor_from_string_converter
            )
            subparsers = (
                self._argparse_subparsers._configman_option.foreign_data
                .argparse.subparsers
            )
            # each subparser needs to have its configman options set up
            # in the subparser's configman option.  This routine copies
            # the required_config of each subparser into the
            # SubparserFromStringConverter defined above.
            for subparser_name, subparser_data in six.iteritems(subparsers):
                subparser_namespaces.add_namespace(
                    subparser_name,
                    subparser_data.subparser.get_required_config()
                )
        except AttributeError:
            # there is no subparser
            pass
        return required_config

Response:
This method returns the required_config dictionary that is a copy of the namespace rather than the actual embedded
        namespace.


Instruction:
How would you explain what the following Python 3 function does
def add_argument(self, *args, **kwargs):
        # pull out each of the argument definition components from the args
        # so that we can deal with them one at a time in a well labeled manner
        # In this section, variables beginning with the prefix "argparse" are
        # values that define Action object.  Variables that begin with
        # "configman" are the arguments to create configman Options.
        argparse_action_name = kwargs.get('action', None)
        argparse_dest = kwargs.get('dest', None)
        argparse_const = kwargs.get('const', None)
        argparse_default = kwargs.get('default', None)
        if argparse_default is argparse.SUPPRESS:
            # we'll be forcing all options to have the attribute of
            # argparse.SUPPRESS later.  It's our way of making sure that
            # argparse returns only values that the user explicitly added to
            # the command line.
            argparse_default = None
        argparse_nargs = kwargs.get('nargs', None)
        argparse_type = kwargs.get('type', None)
        argparse_suppress_help = kwargs.pop('suppress_help', False)
        if argparse_suppress_help:
            configman_doc = kwargs.get('help', '')
            kwargs['help'] = argparse.SUPPRESS
        else:
            argparse_help = kwargs.get('help', '')
            if argparse_help == argparse.SUPPRESS:
                configman_doc = ''
            else:
                configman_doc = argparse_help

        # we need to make sure that all arguments that the user has not
        # explicily set on the command line have this attribute.  This means
        # that when the argparse parser returns the command line values, it
        # will not return values that the user did not mention on the command
        # line.  The defaults that otherwise would have been returned will be
        # handled by configman.
        kwargs['default'] = argparse.SUPPRESS
        # forward all parameters to the underlying base class to create a
        # normal argparse action object.
        an_action = super(ArgumentParser, self).add_argument(
            *args,
            **kwargs
        )
        argparse_option_strings = an_action.option_strings

        # get a human readable string that identifies the type of the argparse
        # action class that was created
        if argparse_action_name is None:
            argparse_action_name = find_action_name_by_value(
                self._optionals._registries,
                an_action
            )

        configman_is_argument = False

        # each of argparse's Action types must be handled separately.
        #--------------------------------------------------------------------
        # STORE
        if argparse_action_name == 'store':
            if argparse_dest is None:
                configman_name, configman_is_argument = self._get_option_name(
                    args
                )
                if not configman_name:
                    configman_name = args[0]
            else:
                configman_name = argparse_dest
                configman_is_argument = not argparse_option_strings
            configman_default = argparse_default
            if argparse_nargs and argparse_nargs in "1?":
                if argparse_type:
                    configman_from_string = argparse_type
                elif argparse_default:
                    configman_from_string = (
                        str_to_instance_of_type_converters.get(
                            type(argparse_default),
                            str
                        )
                    )
                else:
                    configman_from_string = str
            elif argparse_nargs and argparse_type:
                configman_from_string = partial(
                    str_to_list,
                    item_converter=argparse_type,
                    item_separator=' ',
                )
            elif argparse_nargs and argparse_default:
                configman_from_string = partial(
                    str_to_list,
                    item_converter=str_to_instance_of_type_converters.get(
                        type(argparse_default),
                        str
                    ),
                    item_separator=' ',
                )
            elif argparse_nargs:
                configman_from_string = partial(
                    str_to_list,
                    item_converter=str,
                    item_separator=' ',
                )
            elif argparse_type:
                configman_from_string = argparse_type
            elif argparse_default:
                configman_from_string = str_to_instance_of_type_converters.get(
                    type(argparse_default),
                    str
                )
            else:
                configman_from_string = str
            configman_to_string = to_str

        #--------------------------------------------------------------------
        # STORE_CONST
        elif (
            argparse_action_name == 'store_const'
            or argparse_action_name == 'count'
        ):
            if argparse_dest is None:
                configman_name, configman_is_argument = self._get_option_name(
                    args
                )
                if not configman_name:
                    configman_name = args[0]
            else:
                configman_name = argparse_dest
            configman_default = argparse_default
            if argparse_type:
                configman_from_string = argparse_type
            else:
                configman_from_string = str_to_instance_of_type_converters.get(
                    type(argparse_const),
                    str
                )
            configman_to_string = to_str

        #--------------------------------------------------------------------
        # STORE_TRUE /  STORE_FALSE
        elif (
            argparse_action_name == 'store_true'
            or argparse_action_name == 'store_false'
        ):
            if argparse_dest is None:
                configman_name, configman_is_argument = self._get_option_name(
                    args
                )
                if not configman_name:
                    configman_name = args[0]
            else:
                configman_name = argparse_dest
            configman_default = argparse_default
            configman_from_string = boolean_converter
            configman_to_string = to_str

        #--------------------------------------------------------------------
        # APPEND
        elif argparse_action_name == 'append':
            if argparse_dest is None:
                configman_name, configman_is_argument = self._get_option_name(
                    args
                )
                if not configman_name:
                    configman_name = args[0]
            else:
                configman_name = argparse_dest
            configman_default = argparse_default
            if argparse_type:
                configman_from_string = argparse_type
            else:
                configman_from_string = str
            configman_to_string = to_str

        #--------------------------------------------------------------------
        # APPEND_CONST
        elif argparse_action_name == 'append_const':
            if argparse_dest is None:
                configman_name, configman_is_argument = self._get_option_name(
                    args
                )
                if not configman_name:
                    configman_name = args[0]
            else:
                configman_name = argparse_dest
            configman_default = argparse_default
            if argparse_type:
                configman_from_string = argparse_type
            else:
                configman_from_string = str_to_instance_of_type_converters.get(
                    type(argparse_const),
                    str
                )
            configman_to_string = to_str

        #--------------------------------------------------------------------
        # VERSION
        elif argparse_action_name == 'version':
            return an_action

        #--------------------------------------------------------------------
        # OTHER
        else:
            configman_name = argparse_dest

        # configman uses the switch name as the name of the key inwhich to
        # store values.  argparse is able to use different names for each.
        # this means that configman may encounter repeated targets.  Rather
        # than overwriting Options with new ones with the same name, configman
        # renames them by appending the '$' character.
        while configman_name in self.required_config:
            configman_name = "%s$" % configman_name
        configman_not_for_definition = configman_name.endswith('$')

        # it's finally time to create the configman Option object and add it
        # to the required_config.
        self.required_config.add_option(
            name=configman_name,
            default=configman_default,
            doc=configman_doc,
            from_string_converter=configman_from_string,
            to_string_converter=configman_to_string,
            #short_form=configman_short_form,
            is_argument=configman_is_argument,
            not_for_definition=configman_not_for_definition,
            # we're going to save the args & kwargs that created the
            # argparse Action.  This enables us to perfectly reproduce the
            # the original Action object later during the configman overlay
            # process.
            foreign_data=DotDict({
                'argparse.flags.subcommand': False,
                'argparse.args': args,
                'argparse.kwargs': kwargs,
                'argparse.owning_subparser_name': self.subparser_name,
            })
        )
        return an_action

Response:
This method adds an argument to the required_config
            section of the configman object.


Instruction:
Can you generate the documentation for the following Python 3 function
def add_subparsers(self, *args, **kwargs):

        kwargs['parser_class'] = self.__class__
        kwargs['action'] = ConfigmanSubParsersAction

        subparser_action = super(ArgumentParser, self).add_subparsers(
            *args,
            **kwargs
        )
        self._argparse_subparsers = subparser_action

        if "dest" not in kwargs or kwargs['dest'] is None:
            kwargs['dest'] = 'subcommand'
        configman_name = kwargs['dest']
        configman_default = None
        configman_doc = kwargs.get('help', '')
        subprocessor_from_string_converter = SubparserFromStringConverter()
        configman_to_string = str
        configman_is_argument = True
        configman_not_for_definition = True

        # it's finally time to create the configman Option object and add it
        # to the required_config.
        self.required_config.add_option(
            name=configman_name,
            default=configman_default,
            doc=configman_doc,
            from_string_converter=subprocessor_from_string_converter,
            to_string_converter=configman_to_string,
            is_argument=configman_is_argument,
            not_for_definition=configman_not_for_definition,
            # we're going to save the input parameters that created the
            # argparse Action.  This enables us to perfectly reproduce the
            # the original Action object later during the configman overlay
            # process.
            foreign_data=DotDict({
                'argparse.flags.subcommand': subparser_action,
                'argparse.args': args,
                'argparse.kwargs': kwargs,
                'argparse.subparsers': DotDict(),
                'argparse.subprocessor_from_string_converter':
                subprocessor_from_string_converter
            })
        )

        self.configman_subparsers_option = self.required_config[configman_name]
        subparser_action.add_configman_option(self.configman_subparsers_option)

        return subparser_action

Response: When adding a subparser we need to create the configman Option object for the subparser and pack it s foreign data section with the original args & kwargs.


Instruction:
How would you explain what the following Python 3 function does
def parse_known_args(self, args=None, namespace=None):
        # load the config_manager within the scope of the method that uses it
        # so that we avoid circular references in the outer scope
        from configman.config_manager import ConfigurationManager
        configuration_manager = ConfigurationManager(
            definition_source=[self.get_required_config()],
            values_source_list=self.value_source_list,
            argv_source=args,
            app_name=self.prog,
            app_version=self.version,
            app_description=self.description,
            use_auto_help=False,
        )
        conf = configuration_manager.get_config(
            mapping_class=create_key_translating_dot_dict(
                "HyphenUnderscoreDict",
                (('-', '_'),)
            )
        )
        return conf

Response:
this method hijacks the normal argparse Namespace generation and shimming configman into the process. The return value will be a
        configman DotDict rather than an argparse Namespace.


Instruction:
Explain what the following Python 3 code does
def sequence_to_string(
    a_list,
    open_bracket_char='[',
    close_bracket_char=']',
    delimiter=", "
):
    """a dedicated function that turns a list into a comma delimited string
    of items converted.  This method will flatten nested lists."""
    return "%s%s%s" % (
        open_bracket_char,
        delimiter.join(
            local_to_str(x)
            for x in a_list
        ),
        close_bracket_char
    )

Response:
a dedicated function that turns a list into a comma delimited string
    of items converted.


Instruction:
How would you implement a function in Python 3 that
gives a type return a tuple of the module - path type_name

Response:
def get_import_for_type(t):
    """given a type, return a tuple of the (module-path, type_name)
    or (None, None) if it is a built in."""
    t_as_string = to_str(t)
    if not is_identifier(t_as_string):
        # this class expanded into something other than a single identifier
        # we can ignore it.  This is the case when we encounter something
        # like the configman.converter.str_to_classes_in_namespaces
        # InnerClassList.  We can safely ignore these things here.
        return (None, None)
    if '.' in t_as_string:
        parts = t_as_string.split('.')
        return ('.'.join(parts[:-1]), parts[-1])
    else:
        if t_as_string in known_mapping_str_to_type:
            return (None, None)
        return (None, t_as_string)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def write(source_mapping, output_stream=sys.stdout):
        # a set of classes, modules and/or functions that are values in
        # configman options.  These values will have to be imported in the
        # module that this method is writing.
        set_of_classes_needing_imports = set()
        # once symbols are imported, they are in the namespace of the module,
        # but that's not where we want them.  We only want them to be values
        # in configman Options.  This set will be used to make a list of
        # these symbols, to forewarn a future configman that reads this
        # module, that it can ignore these symbols. This will prevent that
        # future configman from issuing a "mismatced symbols" warinng.
        symbols_to_ignore = set()

        # look ahead to see what sort of imports we're going to have to do
        for key in source_mapping.keys_breadth_first():
            value = source_mapping[key]

            if isinstance(value, Aggregation):
                # Aggregations don't get included, skip on
                continue

            if '.' in key:
                # this indicates that there are things in nested namespaces,
                # we will use the DotDict class to represent namespaces
                set_of_classes_needing_imports.add(DotDict)

            option = None
            if isinstance(value, Option):
                # it's the value inside the option, not the option itself
                # that is of interest to us
                option = value
                value = option.value

            if value is None:
                # we don't need in import anything having to do with None
                continue

            if isclass(value) or ismodule(value) or isfunction(value):
                # we know we need to import any of these types
                set_of_classes_needing_imports.add(value)
            else:
                try:
                    # perhaps the value is an instance of a class?  If so,
                    # we'll likely need to import that class, but only if
                    # we don't have a way to convert a string to that class
                    set_of_classes_needing_imports.add(value.__class__)
                except AttributeError:
                    # it's not a class instance, we can skip on
                    pass

        # for everyone of the imports that we're going to have to create
        # we need to know the dotted module pathname and the name of the
        # of the class/module/function.  This routine make a list of 3-tuples
        # class, dotted_module_path, class_name
        class_and_module_path_and_class_name = []
        for a_class in set_of_classes_needing_imports:
            module_path, class_name = get_import_for_type(a_class)
            if (not module_path) and (not class_name):
                continue
            class_and_module_path_and_class_name.append(
                (a_class, module_path, class_name)
            )

        # using the collection of 3-tuples, create a lookup mapping where a
        # class is the key to a 2-tuple of the dotted_module_path & class_name.
        # This is also the appropriate time to detect any name collisions
        # and create a mapping of aliases, so we can resolve name collisions.
        class_name_by_module_path_list = defaultdict(list)
        alias_by_class = {}
        previously_used_names = set()
        for (
            a_class,
            a_module_path,
            class_name
        ) in class_and_module_path_and_class_name:
            if class_name:
                if class_name in previously_used_names:
                    new_class_name_alias = "%s_%s" % (
                        a_module_path.replace('.', '_'),
                        class_name
                    )
                    alias_by_class[a_class] = new_class_name_alias
                    previously_used_names.add(new_class_name_alias)
                else:
                    previously_used_names.add(class_name)
                class_name_by_module_path_list[a_module_path].append(
                    (a_class, class_name)
                )

        # start writing the output module
        print("# generated Python configman file\n", file=output_stream)

        # the first section that we're going to write is imports of the form:
        #     from X import Y
        # and
        #     from X import (
        #         A,
        #         B,
        #     )
        sorted_list = [x.value for x in sorted([OrderableObj(x) for x in
                       class_name_by_module_path_list.keys()])]
        for a_module_path in sorted_list:
            print(a_module_path)
            # if there is no module path, then it is something that we don't
            # need to import.  If the module path begins with underscore then
            # it is private and we ought not step into that mire.  If that
            # causes the output module to fail, it is up to the implementer
            # of the configman option to have created an approprate
            # "from_string" & "to_string" configman Option function references.
            if a_module_path is None or a_module_path.startswith('_'):
                continue
            list_of_class_names = \
                class_name_by_module_path_list[a_module_path]
            if len(list_of_class_names) > 1:
                output_line = "from %s import (\n" % a_module_path
                sorted_list = [x.value for x in sorted([OrderableTuple(x)
                               for x in list_of_class_names])]
                for a_class, a_class_name in sorted_list:
                    if a_class in alias_by_class:
                        output_line = "%s\n    %s as %s," % (
                            output_line,
                            a_class_name,
                            alias_by_class[a_class]
                        )
                        symbols_to_ignore.add(alias_by_class[a_class])
                    else:
                        output_line = "%s    %s,\n" % (
                            output_line,
                            a_class_name
                        )
                        symbols_to_ignore.add(a_class_name)

                output_line = output_line + ')'
                print(output_line.strip(), file=output_stream)
            else:
                a_class, a_class_name = list_of_class_names[0]
                output_line = "from %s import %s" % (
                    a_module_path,
                    a_class_name
                )
                if a_class in alias_by_class:
                    output_line = "%s as %s" % (
                        output_line,
                        alias_by_class[a_class]
                    )
                    symbols_to_ignore.add(alias_by_class[a_class])
                else:
                    symbols_to_ignore.add(a_class_name)
                print(output_line.strip(), file=output_stream)
        print('', file=output_stream)

        # The next section to write will be the imports of the form:
        #     import X
        sorted_list = [x.value for x in sorted([OrderableObj(x) for x in
                       class_name_by_module_path_list.keys()])]
        for a_module_path in sorted_list:
            list_of_class_names = \
                class_name_by_module_path_list[a_module_path]
            a_class, a_class_name = list_of_class_names[0]
            if a_module_path:
                continue
            import_str = ("import %s" % a_class_name).strip()
            symbols_to_ignore.add(a_class_name)
            print(import_str, file=output_stream)

        # See the explanation of 'symbols_to_ignore' above
        if symbols_to_ignore:
            print(
                "\n" \
                "# the following symbols will be ignored by configman when\n" \
                "# this module is used as a value source.  This will\n" \
                "# suppress the mismatch warning since these symbols are\n" \
                "# values for options, not option names themselves.",
                file=output_stream
            )
            print("ignore_symbol_list = [", file=output_stream)
            for a_symbol in sorted(symbols_to_ignore):
                print('    "%s",' % a_symbol, file=output_stream)
            print(']\n', file=output_stream)

        # finally, as the last step, we need to write out the keys and values
        # will be used by a future configman as Options and values.
        sorted_keys = sorted(
            source_mapping.keys_breadth_first(include_dicts=True)
        )
        for key in sorted_keys:
            value = source_mapping[key]
            if isinstance(value, Namespace):
                ValueSource.write_namespace(key, value, output_stream)
            elif isinstance(value, Option):
                ValueSource.write_option(
                    key,
                    value,
                    alias_by_class,
                    output_stream
                )
            elif isinstance(value, Aggregation):
                # skip Aggregations
                continue
            else:
                ValueSource.write_bare_value(key, value, output_stream)

Response:
This method writes a Python module respresenting all the keys
        and values known to configman.


Instruction:
Given the following Python 3 function, write the documentation
def datetime_from_ISO_string(s):
    try:
        return datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S')
    except ValueError:
        try:
            return datetime.datetime.strptime(s, '%Y-%m-%d')
        except ValueError:
            return datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f')

Response:
Take an ISO date string of the form YYYY - MM - DDTHH - MM - SS. S
    and convert it into an instance of datetime. datetime
IVA.


Instruction:
Here you have a function in Python 3, explain what it does
def str_to_timedelta(input_str):
    try:
        input_str = input_str.replace(' ', ':')
    except (TypeError, AttributeError):
        from configman.converters import to_str
        raise TypeError('%s should have been a string' % to_str(input_str))
    days, hours, minutes, seconds = 0, 0, 0, 0
    details = input_str.split(':')
    if len(details) >= 4:
        days = int(details[-4])
    if len(details) >= 3:
        hours = int(details[-3])
    if len(details) >= 2:
        minutes = int(details[-2])
    if len(details) >= 1:
        seconds = int(details[-1])
    return datetime.timedelta(days=days,
                              hours=hours,
                              minutes=minutes,
                              seconds=seconds)

Response:
a string conversion function for timedelta for strings in the format
    DD HHMMSS or D HHMMSS or D HHMMSS or D HHMMSS


Instruction:
How would you explain what the following Python 3 function does
def timedelta_to_str(aTimedelta):
    days = aTimedelta.days
    temp_seconds = aTimedelta.seconds
    hours = int(temp_seconds / 3600)
    minutes = int((temp_seconds - hours * 3600) / 60)
    seconds = temp_seconds - hours * 3600 - minutes * 60
    return '%d %02d:%02d:%02d' % (days, hours, minutes, seconds)

Response: a conversion function for time deltas to string in the form DDHHMMSS


Instruction:
Can you generate a brief explanation for the following Python 3 code
def parse_source_file(file_name):
    with open(file_name, 'r') as f:
        s = f.read()

    nodes = ast.parse(s)

    module_imports = get_nodes_by_instance_type(nodes, _ast.Import)
    specific_imports = get_nodes_by_instance_type(nodes, _ast.ImportFrom)

    assignment_objs = get_nodes_by_instance_type(nodes, _ast.Assign)
    call_objects = get_nodes_by_instance_type(nodes, _ast.Call)

    argparse_assignments = get_nodes_by_containing_attr(assignment_objs, 'ArgumentParser')
    group_arg_assignments = get_nodes_by_containing_attr(assignment_objs, 'add_argument_group')
    add_arg_assignments = get_nodes_by_containing_attr(call_objects, 'add_argument')
    parse_args_assignment = get_nodes_by_containing_attr(call_objects, 'parse_args')
    # there are cases where we have custom argparsers, such as subclassing ArgumentParser. The above
    # will fail on this. However, we can use the methods known to ArgumentParser to do a duck-type like
    # approach to finding what is the arg parser
    if not argparse_assignments:
        aa_references = set([i.func.value.id for i in chain(add_arg_assignments, parse_args_assignment)])
        argparse_like_objects = [getattr(i.value.func, 'id', None) for p_ref in aa_references for i in get_nodes_by_containing_attr(assignment_objs, p_ref)]
        argparse_like_objects = filter(None, argparse_like_objects)
        argparse_assignments = [get_nodes_by_containing_attr(assignment_objs, i) for i in argparse_like_objects]
        # for now, we just choose one
        try:
            argparse_assignments = argparse_assignments[0]
        except IndexError:
            pass

    # get things that are assigned inside ArgumentParser or its methods
    argparse_assigned_variables = get_node_args_and_keywords(assignment_objs, argparse_assignments, 'ArgumentParser')
    add_arg_assigned_variables = get_node_args_and_keywords(assignment_objs, add_arg_assignments, 'add_argument')
    parse_args_assigned_variables = get_node_args_and_keywords(assignment_objs, parse_args_assignment, 'parse_args')

    ast_argparse_source = chain(
        module_imports,
        specific_imports,
        argparse_assigned_variables,
        add_arg_assigned_variables,
        parse_args_assigned_variables,
        argparse_assignments,
        group_arg_assignments,
        add_arg_assignments,
    )
    return ast_argparse_source

Response: Parses the source file and returns the grammar tree.


Instruction:
Implement a function in Python 3 to
add an option to the namespace. This can take two forms of name args and kwargs are the parameters of the option and the name of the object.

Response:
def add_option(self, name, *args, **kwargs):
        """add an option to the namespace.   This can take two forms:
              'name' is a string representing the name of an option and the
              kwargs are its parameters, or 'name' is an instance of an Option
              object
        """
        if isinstance(name, Option):
            an_option = name
            name = an_option.name
        else:
            an_option = Option(name, *args, **kwargs)

        current_namespace = self
        name_parts = name.split('.')
        for a_path_component in name_parts[:-1]:
            if a_path_component not in current_namespace:
                current_namespace[a_path_component] = Namespace()
            current_namespace = current_namespace[a_path_component]
        an_option.name = name_parts[-1]

        setattr(current_namespace, an_option.name, an_option)
        return an_option


Instruction:
Write a Python 3 script for
measuring the cumulative Regression sum of Squares for each individual component and the sum of Squares for each individual component.

Response:
def _cummulativefit(self, x, y):
        """
        Measure the cumulative Regression sum of Squares for each individual component.

        :param x: Data matrix to fit the PLS model.
        :type x: numpy.ndarray, shape [n_samples, n_features]
        :param y: Data matrix to fit the PLS model.
        :type y: numpy.ndarray, shape [n_samples, n_features]
        :return: dictionary object containing the total Regression Sum of Squares and the Sum of Squares
        per components, for both the X and Y data blocks.
        :rtype: dict
        """
        if y.ndim == 1:
            y = y.reshape(-1, 1)
        if x.ndim == 1:
            x = x.reshape(-1, 1)

        xscaled = self.x_scaler.fit_transform(x)
        yscaled = self.y_scaler.fit_transform(y)

        ssx_comp = list()
        ssy_comp = list()

        # Obtain residual sum of squares for whole data set and per component
        SSX = np.sum(xscaled ** 2)
        SSY = np.sum(yscaled ** 2)
        ssx_comp = list()
        ssy_comp = list()

        for curr_comp in range(1, self.ncomps + 1):
            model = self._reduce_ncomps(curr_comp)

            ypred = self.y_scaler.transform(model.predict(x, y=None))
            xpred = self.x_scaler.transform(model.predict(x=None, y=y))
            rssy = np.sum((yscaled - ypred) ** 2)
            rssx = np.sum((xscaled - xpred) ** 2)
            ssx_comp.append(rssx)
            ssy_comp.append(rssy)

        cumulative_fit = {'SSX': SSX, 'SSY': SSY, 'SSXcomp': np.array(ssx_comp), 'SSYcomp': np.array(ssy_comp)}

        return cumulative_fit


Instruction:
Create a Python 3 function to
plot the leverages of the current sample index

Response:
def plot_leverages(self):
        """
        Leverage (h) per observation, with a red line plotted at y = 1/Number of samples (expected
        :return: Plot with observation leverages (h)
        """
        plt.figure()
        lev = self.leverages()
        plt.xlabel('Sample Index')
        plt.ylabel('Leverage')
        plt.bar(left=range(lev.size), height=lev)
        plt.hlines(y=1/lev.size, xmin=0, xmax=lev.size, colors='r', linestyles='--')
        plt.show()
        return None


Instruction:
Can you generate the documentation for the following Python 3 function
def _handle_zeros_in_scale(scale, copy=True):

    # if we are fitting on 1D arrays, scale might be a scalar
    if numpy.isscalar(scale):
        if scale == .0:
            scale = 1.
        return scale
    elif isinstance(scale, numpy.ndarray):
        if copy:
            # New array to avoid side-effects
            scale = scale.copy()
        scale[scale == 0.0] = 1.0
        return scale

Response: Handles zero - based scalers.


Instruction:
Given the following Python 3 function, write the documentation
def _reset(self):

        # Checking one attribute is enough, because they are all set together
        # in partial_fit
        if hasattr(self, 'scale_'):
            del self.scale_
            del self.n_samples_seen_
            del self.mean_
            del self.var_

Response: Reset internal data - dependent state of the scaler.


Instruction:
How would you implement a function in Python 3 that
computes the mean and standard deviation from a dataset to use in future scaling operations.

Response:
def fit(self, X, y=None):
        """
        Compute the mean and standard deviation from a dataset to use in future scaling operations.

        :param X: Data matrix to scale.
        :type X: numpy.ndarray, shape [n_samples, n_features]
        :param y: Passthrough for Scikit-learn ``Pipeline`` compatibility.
        :type y: None
        :return: Fitted object.
        :rtype: pyChemometrics.ChemometricsScaler
        """

        # Reset internal state before fitting
        self._reset()
        return self.partial_fit(X, y)


Instruction:
Here you have a function in Python 3, explain what it does
def partial_fit(self, X, y=None):

        X = check_array(X, accept_sparse=('csr', 'csc'), copy=self.copy,
                        warn_on_dtype=True, estimator=self, dtype=FLOAT_DTYPES)

        # Even in the case of `with_mean=False`, we update the mean anyway
        # This is needed for the incremental computation of the var
        # See incr_mean_variance_axis and _incremental_mean_variance_axis

        if sparse.issparse(X):
            if self.with_mean:
                raise ValueError(
                    "Cannot center sparse matrices: pass `with_mean=False` "
                    "instead. See docstring for motivation and alternatives.")
            if self.with_std:
                # First pass
                if not hasattr(self, 'n_samples_seen_'):
                    self.mean_, self.var_ = mean_variance_axis(X, axis=0)
                    self.n_samples_seen_ = X.shape[0]
                # Next passes
                else:
                    self.mean_, self.var_, self.n_samples_seen_ = \
                        incr_mean_variance_axis(X, axis=0,
                                                last_mean=self.mean_,
                                                last_var=self.var_,
                                                last_n=self.n_samples_seen_)
            else:
                self.mean_ = None
                self.var_ = None
        else:
            # First pass
            if not hasattr(self, 'n_samples_seen_'):
                self.mean_ = .0
                self.n_samples_seen_ = 0
                if self.with_std:
                    self.var_ = .0
                else:
                    self.var_ = None

            self.mean_, self.var_, self.n_samples_seen_ = \
                _incremental_mean_and_var(X, self.mean_, self.var_,
                                          self.n_samples_seen_)

        if self.with_std:
            self.scale_ = _handle_zeros_in_scale(numpy.sqrt(self.var_)) ** self.scale_power
        else:
            self.scale_ = None

        return self

Response: Performs online computation of mean and standard deviation on X for later scaling.


Instruction:
Given the following Python 3 function, write the documentation
def transform(self, X, y=None, copy=None):
        check_is_fitted(self, 'scale_')

        copy = copy if copy is not None else self.copy

        X = check_array(X, accept_sparse='csr', copy=copy, warn_on_dtype=True,
                        estimator=self, dtype=FLOAT_DTYPES)

        if sparse.issparse(X):
            if self.with_mean:
                raise ValueError(
                    "Cannot center sparse matrices: pass `with_mean=False` "
                    "instead. See docstring for motivation and alternatives.")
            if self.scale_ is not None:
                inplace_column_scale(X, 1 / self.scale_)
        else:
            if self.with_mean:
                X -= self.mean_
            if self.with_std:
                X /= self.scale_
        return X

Response: Perform standardization by centering and scaling using the parameters.


Instruction:
Here you have a function in Python 3, explain what it does
def inverse_transform(self, X, copy=None):
        check_is_fitted(self, 'scale_')

        copy = copy if copy is not None else self.copy
        if sparse.issparse(X):
            if self.with_mean:
                raise ValueError(
                    "Cannot uncenter sparse matrices: pass `with_mean=False` "
                    "instead See docstring for motivation and alternatives.")
            if not sparse.isspmatrix_csr(X):
                X = X.tocsr()
                copy = False
            if copy:
                X = X.copy()
            if self.scale_ is not None:
                inplace_column_scale(X, self.scale_)
        else:
            X = numpy.asarray(X)
            if copy:
                X = X.copy()
            if self.with_std:
                X *= self.scale_
            if self.with_mean:
                X += self.mean_

        return X

Response: Inverse transform the data matrix X to the original representation.


Instruction:
Write a Python 3 script for
calculating the leverages for each observation in the hierarchy.

Response:
def leverages(self, block='X'):
        """
        Calculate the leverages for each observation
        :return:
        :rtype:
        """
        # TODO check with matlab and simca
        try:
            if block == 'X':
                return np.dot(self.scores_t, np.dot(np.linalg.inv(np.dot(self.scores_t.T, self.scores_t), self.scores_t.T)))
            elif block == 'Y':
                return np.dot(self.scores_u, np.dot(np.linalg.inv(np.dot(self.scores_u.T, self.scores_u), self.scores_u.T)))
            else:
                raise ValueError
        except ValueError as verr:
            raise ValueError('block option must be either X or Y')


Instruction:
Here you have a function in Python 3, explain what it does
def _recurse_replace(obj, key, new_key, sub, remove):
    if isinstance(obj, list):
        return [_recurse_replace(x, key, new_key, sub, remove) for x in obj]
    if isinstance(obj, dict):
        for k, v in list(obj.items()):
            if k == key and v in sub:
                obj[new_key] = sub[v]
                if remove:
                    del obj[key]
            else:
                obj[k] = _recurse_replace(v, key, new_key, sub, remove)
    return obj

Response: Recursive helper for replace_by_key


Instruction:
Can you write a function in Python 3 where it
replaces values that match a key and return a new key.

Response:
def replace_by_key(pif, key, subs, new_key=None, remove=False):
    """Replace values that match a key

    Deeply traverses the pif object, looking for `key` and
    replacing values in accordance with `subs`.  If `new_key`
    is set, the replaced values are assigned to that key.  If
    `remove` is `True`, the old `key` pairs are removed.
    """
    if not new_key:
        new_key = key
        remove = False
    orig = pif.as_dictionary()
    new  = _recurse_replace(orig, to_camel_case(key), to_camel_case(new_key), subs, remove)
    return pypif.pif.loads(json.dumps(new))


Instruction:
How would you code a function in Python 3 to
get a property by name

Response:
def get_propety_by_name(pif, name):
    """Get a property by name"""
    warn("This method has been deprecated in favor of get_property_by_name")
    return next((x for x in pif.properties if x.name == name), None)


Instruction:
Make a summary of the following Python 3 code
def get_property_by_name(pif, name):
    return next((x for x in pif.properties if x.name == name), None)

Response: Get a property by name


Instruction:
Given the following Python 3 function, write the documentation
def new_keypair(key, value, ambig, unambig):
    if key in ambig:
        return

    if key in unambig and value != unambig[key]:
            ambig.add(key)
            del unambig[key]
            return

    unambig[key] = value
    return

Response: Check new keypair against existing unambiguous dict


Instruction:
Can you implement a function in Python 3 that
adds information about decodings of a child object.

Response:
def add_child_ambig(child_ambig, child_unambig, ambig, unambig):
    """
    Add information about decodings of a child object

    :param child_ambig: ambiguous set from child
    :param child_unambig: unambiguous set from child
    :param ambig: set of keys storing ambig decodings
    :param unambig: dictionary storing unambiguous decodings
    :return:
    """
    for k in child_ambig:
        ambig.add(k)
        if k in unambig:
            del unambig[k]

    for k, v in child_unambig.items():
        new_keypair(k, v, ambig, unambig)

    return


Instruction:
Make a summary of the following Python 3 code
def get_client(site=None):
    if 'CITRINATION_API_KEY' not in environ:
        raise ValueError("'CITRINATION_API_KEY' is not set as an environment variable")
    if not site:
        site = environ.get("CITRINATION_SITE", "https://citrination.com")
    return CitrinationClient(environ['CITRINATION_API_KEY'], site)

Response: Get a citrination client


Instruction:
Make a summary of the following Python 3 code
def set_uids(pifs, uids=None):
    if not uids:
        uids = [str(hash(dumps(x))) for x in pifs]
    for pif, uid in zip(pifs, uids):
        pif.uid = uid
    return pifs

Response: Set the UIDs in a PIF explicitly if the list of UIDs is passed in


Instruction:
Can you implement a function in Python 3 that
constructs the URL for a PIF on a site

Response:
def get_url(pif, dataset, version=1, site="https://citrination.com"):
    """
    Construct the URL of a PIF on a site
    :param pif: to construct URL for
    :param dataset: the pif will belong to
    :param version: of the PIF (default: 1)
    :param site: for the dataset (default: https://citrination.com)
    :return: the URL as a string
    """
    return "{site}/datasets/{dataset}/version/{version}/pif/{uid}".format(
        uid=pif.uid, version=version, dataset=dataset, site=site
    )