Instruction:
Can you create a Python 3 function that
creates and return an OptionParser with the given defaults.

Response:
def get_option_parser(defaults):
    """Create and return an OptionParser with the given defaults."""
    # based on recipe from:
    # http://stackoverflow.com/questions/1880404/using-a-file-to-store-optparse-arguments

    # process command line parameters
    # e.g. skosify yso.owl -o yso-skos.rdf
    usage = "Usage: %prog [options] voc1 [voc2 ...]"
    parser = optparse.OptionParser(usage=usage)
    parser.set_defaults(**defaults)
    parser.add_option('-c', '--config', type='string',
                      help='Read default options '
                           'and transformation definitions '
                           'from the given configuration file.')
    parser.add_option('-o', '--output', type='string',
                      help='Output file name. Default is "-" (stdout).')
    parser.add_option('-D', '--debug', action="store_true",
                      help='Show debug output.')
    parser.add_option('-d', '--no-debug', dest="debug",
                      action="store_false", help='Hide debug output.')
    parser.add_option('-O', '--log', type='string',
                      help='Log file name. Default is to use standard error.')

    group = optparse.OptionGroup(parser, "Input and Output Options")
    group.add_option('-f', '--from-format', type='string',
                     help='Input format. '
                           'Default is to detect format '
                           'based on file extension. '
                           'Possible values: xml, n3, turtle, nt...')
    group.add_option('-F', '--to-format', type='string',
                     help='Output format. '
                           'Default is to detect format '
                           'based on file extension. '
                           'Possible values: xml, n3, turtle, nt...')
    group.add_option('--update-query', type='string',
                     help='SPARQL update query. '
                     'This query is executed against the input '
                     'data before processing it. '
                     'The value can be either the actual query, '
                     'or "@filename".')
    group.add_option('--construct-query', type='string',
                     help='SPARQL CONSTRUCT query. '
                     'This query is executed against the input '
                     'data and the result graph is used as the '
                     'actual input. '
                     'The value can be either the actual query, '
                     'or "@filename".')
    group.add_option('-I', '--infer', action="store_true",
                     help='Perform RDFS subclass/subproperty inference '
                          'before transforming input.')
    group.add_option('-i', '--no-infer', dest="infer", action="store_false",
                     help="Don't perform RDFS subclass/subproperty inference "
                          "before transforming input.")
    parser.add_option_group(group)

    group = optparse.OptionGroup(
        parser, "Concept Scheme and Labelling Options")
    group.add_option('-s', '--namespace', type='string',
                     help='Namespace of vocabulary '
                          '(usually optional; used to create a ConceptScheme)')
    group.add_option('-L', '--label', type='string',
                     help='Label/title for the vocabulary '
                          '(usually optional; used to label a ConceptScheme)')
    group.add_option('-l', '--default-language', type='string',
                     help='Language tag to set for labels '
                          'with no defined language.')
    group.add_option('-p', '--preflabel-policy', type='string',
                     help='Policy for handling multiple prefLabels '
                          'with the same language tag. '
                          'Possible values: shortest, longest, all.')
    group.add_option('--set-modified', dest="set_modified",
                     action="store_true",
                     help='Set modification date on the ConceptScheme')
    group.add_option('--no-set-modified', dest="set_modified",
                     action="store_false",
                     help="Don't set modification date on the ConceptScheme")
    parser.add_option_group(group)

    group = optparse.OptionGroup(parser, "Vocabulary Structure Options")
    group.add_option('-E', '--mark-top-concepts', action="store_true",
                     help='Mark top-level concepts in the hierarchy '
                          'as top concepts (entry points).')
    group.add_option('-e', '--no-mark-top-concepts',
                     dest="mark_top_concepts", action="store_false",
                     help="Don't mark top-level concepts in the hierarchy "
                          "as top concepts.")
    group.add_option('-N', '--narrower', action="store_true",
                     help='Include narrower/narrowerGeneric/narrowerPartitive '
                          'relationships in the output vocabulary.')
    group.add_option('-n', '--no-narrower',
                     dest="narrower", action="store_false",
                     help="Don't include "
                          "narrower/narrowerGeneric/narrowerPartitive "
                          "relationships in the output vocabulary.")
    group.add_option('-T', '--transitive', action="store_true",
                     help='Include transitive hierarchy relationships '
                          'in the output vocabulary.')
    group.add_option('-t', '--no-transitive',
                     dest="transitive", action="store_false",
                     help="Don't include transitive hierarchy relationships "
                          "in the output vocabulary.")
    group.add_option('-M', '--enrich-mappings', action="store_true",
                     help='Perform SKOS enrichments on mapping relationships.')
    group.add_option('-m', '--no-enrich-mappings', dest="enrich_mappings",
                     action="store_false",
                     help="Don't perform SKOS enrichments "
                          "on mapping relationships.")
    group.add_option('-A', '--aggregates', action="store_true",
                     help='Keep AggregateConcepts completely '
                          'in the output vocabulary.')
    group.add_option('-a', '--no-aggregates',
                     dest="aggregates", action="store_false",
                     help='Remove AggregateConcepts completely '
                          'from the output vocabulary.')
    group.add_option('-R', '--keep-related', action="store_true",
                     help="Keep skos:related relationships "
                          "within the same hierarchy.")
    group.add_option('-r', '--no-keep-related',
                     dest="keep_related", action="store_false",
                     help="Remove skos:related relationships "
                          "within the same hierarchy.")
    group.add_option('-B', '--break-cycles', action="store_true",
                     help="Break any cycles in the skos:broader hierarchy.")
    group.add_option('-b', '--no-break-cycles', dest="break_cycles",
                     action="store_false",
                     help="Don't break cycles in the skos:broader hierarchy.")
    group.add_option('--eliminate-redundancy', action="store_true",
                     help="Eliminate hierarchical redundancy in the "
                          "skos:broader hierarchy.")
    group.add_option('--no-eliminate-redundancy', dest="eliminate_redundancy",
                     action="store_false",
                     help="Don't eliminate hierarchical redundancy in the "
                          "skos:broader hierarchy.")
    parser.add_option_group(group)

    group = optparse.OptionGroup(parser, "Cleanup Options")
    group.add_option('--cleanup-classes', action="store_true",
                     help="Remove definitions of classes with no instances.")
    group.add_option('--no-cleanup-classes', dest='cleanup_classes',
                     action="store_false",
                     help="Don't remove definitions "
                          "of classes with no instances.")
    group.add_option('--cleanup-properties', action="store_true",
                     help="Remove definitions of properties "
                          "which have not been used.")
    group.add_option('--no-cleanup-properties', action="store_false",
                     dest='cleanup_properties',
                     help="Don't remove definitions of properties "
                          "which have not been used.")
    group.add_option('--cleanup-unreachable', action="store_true",
                     help="Remove triples which can not be reached "
                          "by a traversal from the main vocabulary graph.")
    group.add_option('--no-cleanup-unreachable', action="store_false",
                     dest='cleanup_unreachable',
                     help="Don't remove triples which can not be reached "
                          "by a traversal from the main vocabulary graph.")
    parser.add_option_group(group)

    return parser


Instruction:
Write a Python 3 function for
reading command line parameters and make a transform based on them.

Response:
def main():
    """Read command line parameters and make a transform based on them."""

    config = Config()

    # additional options for command line client only
    defaults = vars(config)
    defaults['to_format'] = None
    defaults['output'] = '-'
    defaults['log'] = None
    defaults['debug'] = False

    options, remainingArgs = get_option_parser(defaults).parse_args()
    for key in vars(options):
        if hasattr(config, key):
            setattr(config, key, getattr(options, key))

    # configure logging, messages to stderr by default
    logformat = '%(levelname)s: %(message)s'
    loglevel = logging.INFO
    if options.debug:
        loglevel = logging.DEBUG
    if options.log:
        logging.basicConfig(filename=options.log,
                            format=logformat, level=loglevel)
    else:
        logging.basicConfig(format=logformat, level=loglevel)

    output = options.output
    to_format = options.to_format

    # read config file as defaults and override from command line arguments
    if options.config is not None:
        config.read_and_parse_config_file(options.config)
        options, remainingArgs = get_option_parser(vars(config)).parse_args()
        for key in vars(options):
            if hasattr(config, key):
                setattr(config, key, getattr(options, key))

    if remainingArgs:
        inputfiles = remainingArgs
    else:
        inputfiles = ['-']

    voc = skosify(*inputfiles, **vars(config))
    write_rdf(voc, output, to_format)


Instruction:
How would you explain what the following Python 3 function does
def set_handler(self, handler):
        if self._handler:
            raise Exception('Handler was already set')
        if handler:
            self._handler = async_task(handler, loop=self._loop)

Response: Connect with a coroutine which will be scheduled when connection is made.


Instruction:
Write a Python 3 function for
starting a new request by sending given headers on a new stream.

Response:
def start_request(self, headers, *, end_stream=False):
        """
        Start a request by sending given headers on a new stream, and return
        the ID of the new stream.

        This may block until the underlying transport becomes writable, and
        the number of concurrent outbound requests (open outbound streams) is
        less than the value of peer config MAX_CONCURRENT_STREAMS.

        The completion of the call to this method does not mean the request is
        successfully delivered - data is only correctly stored in a buffer to
        be sent. There's no guarantee it is truly delivered.

        :param headers: A list of key-value tuples as headers.
        :param end_stream: To send a request without body, set `end_stream` to
                           `True` (default `False`).
        :return: Stream ID as a integer, used for further communication.
        """
        yield from _wait_for_events(self._resumed, self._stream_creatable)
        stream_id = self._conn.get_next_available_stream_id()
        self._priority.insert_stream(stream_id)
        self._priority.block(stream_id)
        self._conn.send_headers(stream_id, headers, end_stream=end_stream)
        self._flush()
        return stream_id


Instruction:
Can you generate the documentation for the following Python 3 function
def start_response(self, stream_id, headers, *, end_stream=False):
        yield from self._resumed.wait()
        self._conn.send_headers(stream_id, headers, end_stream=end_stream)
        self._flush()

Response: Start a response by sending given headers on the given stream.


Instruction:
Make a summary of the following Python 3 code
def send_data(self, stream_id, data, *, end_stream=False):
        try:
            with (yield from self._get_stream(stream_id).wlock):
                while True:
                    yield from _wait_for_events(
                        self._resumed, self._get_stream(stream_id).window_open)
                    self._priority.unblock(stream_id)
                    waiter = asyncio.Future()
                    if not self._priority_events:
                        self._loop.call_soon(self._priority_step)
                    self._priority_events[stream_id] = waiter
                    try:
                        yield from waiter
                        data_size = len(data)
                        size = min(
                            data_size,
                            self._conn.local_flow_control_window(stream_id),
                            self._conn.max_outbound_frame_size)
                        if data_size == 0 or size == data_size:
                            self._conn.send_data(stream_id, data,
                                                 end_stream=end_stream)
                            self._flush()
                            break
                        elif size > 0:
                            self._conn.send_data(stream_id, data[:size])
                            data = data[size:]
                            self._flush()
                    finally:
                        self._priority_events.pop(stream_id, None)
                        self._priority.block(stream_id)
                        if self._priority_events:
                            self._loop.call_soon(self._priority_step)
        except ProtocolError:
            raise exceptions.SendException(data)

Response: Send data to a given stream.


Instruction:
Write a Python 3 function for
sending trailers on the given stream.

Response:
def send_trailers(self, stream_id, headers):
        """
        Send trailers on the given stream, closing the stream locally.

        This may block until the underlying transport becomes writable, or
        other coroutines release the wlock on this stream.

        :param stream_id: Which stream to send trailers on.
        :param headers: A list of key-value tuples as trailers.
        """
        with (yield from self._get_stream(stream_id).wlock):
            yield from self._resumed.wait()
            self._conn.send_headers(stream_id, headers, end_stream=True)
            self._flush()


Instruction:
Write a Python 3 script for
closing the given stream locally.

Response:
def end_stream(self, stream_id):
        """
        Close the given stream locally.

        This may block until the underlying transport becomes writable, or
        other coroutines release the wlock on this stream.

        :param stream_id: Which stream to close.
        """
        with (yield from self._get_stream(stream_id).wlock):
            yield from self._resumed.wait()
            self._conn.end_stream(stream_id)
            self._flush()


Instruction:
Implement a Python 3 function for
reading data from the given stream.

Response:
def read_stream(self, stream_id, size=None):
        """
        Read data from the given stream.

        By default (`size=None`), this returns all data left in current HTTP/2
        frame. In other words, default behavior is to receive frame by frame.

        If size is given a number above zero, method will try to return as much
        bytes as possible up to the given size, block until enough bytes are
        ready or stream is remotely closed.

        If below zero, it will read until the stream is remotely closed and
        return everything at hand.

        `size=0` is a special case that does nothing but returns `b''`. The
        same result `b''` is also returned under other conditions if there is
        no more data on the stream to receive, even under `size=None` and peer
        sends an empty frame - you can use `b''` to safely identify the end of
        the given stream.

        Flow control frames will be automatically sent while reading clears the
        buffer, allowing more data to come in.

        :param stream_id: Stream to read
        :param size: Expected size to read, `-1` for all, default frame.
        :return: Bytes read or empty if there is no more to expect.
        """
        rv = []
        try:
            with (yield from self._get_stream(stream_id).rlock):
                if size is None:
                    rv.append((
                        yield from self._get_stream(stream_id).read_frame()))
                    self._flow_control(stream_id)
                elif size < 0:
                    while True:
                        rv.extend((
                            yield from self._get_stream(stream_id).read_all()))
                        self._flow_control(stream_id)
                else:
                    while size > 0:
                        bufs, count = yield from self._get_stream(
                            stream_id).read(size)
                        rv.extend(bufs)
                        size -= count
                        self._flow_control(stream_id)
        except StreamClosedError:
            pass
        except _StreamEndedException as e:
            try:
                self._flow_control(stream_id)
            except StreamClosedError:
                pass
            rv.extend(e.bufs)
        return b''.join(rv)


Instruction:
Can you create a Python 3 function that
waits until the connection becomes functional.

Response:
def wait_functional(self):
        """
        Wait until the connection becomes functional.

        The connection is count functional if it was active within last few
        seconds (defined by `functional_timeout`), where a newly-made
        connection and received data indicate activeness.

        :return: Most recently calculated round-trip time if any.
        """
        while not self._is_functional():
            self._rtt = None
            self._ping_index += 1
            self._ping_time = self._loop.time()
            self._conn.ping(struct.pack('Q', self._ping_index))
            self._flush()
            try:
                yield from asyncio.wait_for(self._functional.wait(),
                                            self._functional_timeout)
            except asyncio.TimeoutError:
                pass
        return self._rtt


Instruction:
How would you implement a function in Python 3 that
updates the priority status of an existing stream. :param stream_id: The stream ID of the stream being updated. :param depends_on: (optional) The ID of the stream that the stream now depends on. If ``None``, will be moved to depend on stream 0. :param weight: (optional) The new weight to give the stream. Defaults to 16. :param exclusive: (optional) Whether this stream should now be an exclusive dependency of the new parent.

Response:
def reprioritize(self, stream_id,
                     depends_on=None, weight=16, exclusive=False):
        """
        Update the priority status of an existing stream.

        :param stream_id: The stream ID of the stream being updated.
        :param depends_on: (optional) The ID of the stream that the stream now
            depends on. If ``None``, will be moved to depend on stream 0.
        :param weight: (optional) The new weight to give the stream. Defaults
            to 16.
        :param exclusive: (optional) Whether this stream should now be an
            exclusive dependency of the new parent.
        """
        self._priority.reprioritize(stream_id, depends_on, weight, exclusive)


Instruction:
Given the following Python 3 function, write the documentation
def superuser_required(view_func=None, redirect_field_name=REDIRECT_FIELD_NAME,
                       login_url='admin:login'):
    """
    Decorator for views that checks that the user is logged in and is a superuser
    member, redirecting to the login page if necessary.
    """
    actual_decorator = user_passes_test(
        lambda u: u.is_active and u.is_superuser,
        login_url=login_url,
        redirect_field_name=redirect_field_name
    )
    if view_func:
        return actual_decorator(view_func)
    return actual_decorator

Response: Decorator for views that checks that the user is logged in and is a superuser member.


Instruction:
How would you explain what the following Python 3 function does
def _condition_as_sql(self, qn, connection):
        '''
        Return sql for condition.
        '''
        def escape(value):
            if isinstance(value, bool):
                value = str(int(value))
            if isinstance(value, six.string_types):
                # Escape params used with LIKE
                if '%' in value:
                    value = value.replace('%', '%%')
                # Escape single quotes
                if "'" in value:
                    value = value.replace("'", "''")
                # Add single quote to text values
                value = "'" + value + "'"
            return value

        sql, param = self.condition.query.where.as_sql(qn, connection)
        param = map(escape, param)

        return sql % tuple(param)

Response: Return sql for condition.


Instruction:
Can you generate the documentation for the following Python 3 function
def computeVoronoiDiagram(points):
    siteList = SiteList(points)
    context  = Context()
    voronoi(siteList,context)
    return (context.vertices, context.edges, context.polygons)

Response: Computes the Voronoi diagram of a list of points.


Instruction:
Write a Python 3 script for
computing the Delaunay triangulation of a list of points.

Response:
def computeDelaunayTriangulation(points):
    """ Takes a list of point objects (which must have x and y fields).
        Returns a list of 3-tuples: the indices of the points that form a
        Delaunay triangle.
    """
    siteList = SiteList(points)
    context  = Context()
    context.triangulate = True
    voronoi(siteList,context)
    return context.triangles


Instruction:
Can you tell what is the following Python 3 function doing
def build_messages_metrics(messages):
    count_types = collections.Counter(
        line.get('type') or None
        for line in messages)
    count_modules = collections.Counter(
        line.get('module') or None
        for line in messages)
    count_symbols = collections.Counter(
        line.get('symbol') or None
        for line in messages)
    count_paths = collections.Counter(
        line.get('path') or None
        for line in messages)

    return {
        'types': count_types,
        'modules': count_modules,
        'symbols': count_symbols,
        'paths': count_paths,
    }

Response: Build reports s metrics


Instruction:
Here you have a function in Python 3, explain what it does
def build_messages_modules(messages):
    data = collections.defaultdict(list)
    for line in messages:
        module_name = line.get('module')
        module_path = line.get('path')
        module_info = ModuleInfo(
            module_name,
            module_path,
        )
        data[module_info].append(line)

    for module, module_messages in data.items():
        yield (
            module,
            sorted(module_messages, key=lambda x: x.get('line')))

Response: Build and yield sorted list of messages per module.


Instruction:
Explain what the following Python 3 code does
def stats_evaluation(stats):
    statement = stats.get('statement')
    error = stats.get('error', 0)
    warning = stats.get('warning', 0)
    refactor = stats.get('refactor', 0)
    convention = stats.get('convention', 0)

    if not statement or statement <= 0:
        return None

    malus = float(5 * error + warning + refactor + convention)
    malus_ratio = malus / statement
    return 10.0 - (malus_ratio * 10)

Response: Generate an evaluation for the given pylint stats.


Instruction:
Here you have a function in Python 3, explain what it does
def build_command_parser():
    parser = argparse.ArgumentParser(
        description='Transform Pylint JSON report to HTML')
    parser.add_argument(
        'filename',
        metavar='FILENAME',
        type=argparse.FileType('r'),
        nargs='?',
        default=sys.stdin,
        help='Pylint JSON report input file (or stdin)')
    parser.add_argument(
        '-o', '--output',
        metavar='FILENAME',
        type=argparse.FileType('w'),
        default=sys.stdout,
        help='Pylint HTML report output file (or stdout)')
    parser.add_argument(
        '-f', '--input-format',
        metavar='FORMAT',
        choices=[SIMPLE_JSON, EXTENDED_JSON],
        action='store',
        dest='input_format',
        default='json',
        help='Pylint JSON Report input type (json or jsonextended)')

    return parser

Response: Build command parser using argparse module.


Instruction:
Write a Python 3 function that can
render the report to HTML

Response:
def render(self):
        """Render report to HTML"""
        template = self.get_template()
        return template.render(
            messages=self._messages,
            metrics=self.metrics,
            report=self)


Instruction:
Create a Python 3 function for
storing new message for later use.

Response:
def handle_message(self, msg):
        """Store new message for later use.

        .. seealso:: :meth:`~JsonExtendedReporter.on_close`
        """
        self._messages.append({
            'type': msg.category,
            'module': msg.module,
            'obj': msg.obj,
            'line': msg.line,
            'column': msg.column,
            'path': msg.path,
            'symbol': msg.symbol,
            'message': str(msg.msg) or '',
            'message-id': msg.msg_id,
        })


Instruction:
Implement a function in Python 3 to
print the extended JSON report to the reporter s output.

Response:
def on_close(self, stats, previous_stats):
        """Print the extended JSON report to reporter's output.

        :param dict stats: Metrics for the current pylint run
        :param dict previous_stats: Metrics for the previous pylint run
        """
        reports = {
            'messages': self._messages,
            'stats': stats,
            'previous': previous_stats,
        }
        print(json.dumps(reports, cls=JSONSetEncoder, indent=4), file=self.out)


Instruction:
Explain what the following Python 3 code does
def triangulate(points):
    # Remove duplicate xy points bc that would make delauney fail, and must remember z (if any) for retrieving originals from index results
    seen = set()
    uniqpoints = [ p for p in points if str( p[:2] ) not in seen and not seen.add( str( p[:2] ) )]
    classpoints = [_Point(*point[:2]) for point in uniqpoints]

    # Compute Delauney
    triangle_ids = tesselator.computeDelaunayTriangulation(classpoints)

    # Get vertices from result indexes
    triangles = [[uniqpoints[i] for i in triangle] for triangle in triangle_ids]

    return triangles

Response: Computes the smallest possible Delauney triangles between the input list of xy points.


Instruction:
How would you implement a function in Python 3 that
returns a list of 2 - tuples with the first item being the original point and the second item being the corressponding Voronoi polygon.

Response:
def voronoi(points, buffer_percent=100):
    """
    Surrounds each point in an input list of xy tuples with a
    unique Voronoi polygon.

    Arguments:

    - **points**: A list of xy or xyz point tuples to triangulate.
    - **buffer_percent** (optional): Controls how much bigger than
        the original bbox of the input points to set the bbox of fake points,
        used to account for lacking values around the edges (default is 100 percent).

    Returns:

    - Returns a list of 2-tuples, with the first item in each tuple being the
        original input point (or None for each corner of the bounding box buffer),
        and the second item being the point's corressponding Voronoi polygon.

    """
    # Remove duplicate xy points bc that would make delauney fail, and must remember z (if any) for retrieving originals from index results
    seen = set()
    uniqpoints = [ p for p in points if str( p[:2] ) not in seen and not seen.add( str( p[:2] ) )]
    classpoints = [_Point(*point[:2]) for point in uniqpoints]

    # Create fake sitepoints around the point extent to correct for infinite polygons
    # For a similar approach and problem see: http://gis.stackexchange.com/questions/11866/voronoi-polygons-that-run-out-to-infinity
    xs,ys = list(zip(*uniqpoints))[:2]
    pointswidth = max(xs) - min(xs)
    pointsheight = max(ys) - min(ys)
    xbuff,ybuff = ( pointswidth / 100.0 * buffer_percent , pointsheight / 100.0 * buffer_percent )
    midx,midy = ( sum(xs) / float(len(xs)) , sum(ys) / float(len(ys)) )
    #bufferbox = [(midx-xbuff,midy-ybuff),(midx+xbuff,midy-ybuff),(midx+xbuff,midy+ybuff),(midx-xbuff,midy+ybuff)] # corner buffer
    bufferbox = [(midx-xbuff,midy),(midx+xbuff,midy),(midx,midy+ybuff),(midx,midy-ybuff)] # mid sides buffer
    classpoints.extend([_Point(*corner) for corner in bufferbox])

    # Compute Voronoi
    vertices,edges,poly_dict = tesselator.computeVoronoiDiagram(classpoints)

    # Turn unordered result edges into ordered polygons
    polygons = list()
    for sitepoint,polyedges in list(poly_dict.items()):
        polyedges = [edge[1:] for edge in polyedges]
        poly = list()
        firststart,firstend = polyedges.pop(0)
        poly.append(firstend)
        while polyedges:
            curend = poly[-1]
            for i,other in enumerate(polyedges):
                otherstart,otherend = other
                if otherstart == curend:
                    poly.append(otherend)
                    ##print otherstart,otherend
                    polyedges.pop(i)
                    break
                elif otherend == curend:
                    ##print otherend,otherstart
                    poly.append(otherstart)
                    polyedges.pop(i)
                    break
        # Get vertices from indexes
        try: sitepoint = uniqpoints[sitepoint]
        except IndexError:
            sitepoint = None # fake bbox sitepoints shouldnt be in the results
        poly = [vertices[vi] for vi in poly if vi != -1]
        polygons.append((sitepoint, poly))

    # Maybe clip parts of polygons that stick outside screen?
    # ...

    return polygons


Instruction:
Explain what the following Python 3 code does
def equals(val1, val2):
    if len(val1) != len(val2):
        return False
    result = 0
    for x, y in zip(val1, val2):
        result |= ord(x) ^ ord(y)
    return result == 0

Response: Returns True if the two strings are equal False otherwise.


Instruction:
Can you implement a function in Python 3 that
encodes integer into variable - length format into data.

Response:
def _encode_uvarint(data, n):
    ''' Encodes integer into variable-length format into data.'''
    if n < 0:
        raise ValueError('only support positive integer')
    while True:
        this_byte = n & 0x7f
        n >>= 7
        if n == 0:
            data.append(this_byte)
            break
        data.append(this_byte | 0x80)


Instruction:
Can you tell what is the following Python 3 function doing
def _parse_section_v2(self, data):
        ''' Parses a sequence of packets in data.

        The sequence is terminated by a packet with a field type of EOS
        :param data bytes to be deserialized.
        :return: the rest of data and an array of packet V2
        '''

        from pymacaroons.exceptions import MacaroonDeserializationException

        prev_field_type = -1
        packets = []
        while True:
            if len(data) == 0:
                raise MacaroonDeserializationException(
                    'section extends past end of buffer')
            rest, packet = self._parse_packet_v2(data)
            if packet.field_type == self._EOS:
                return rest, packets
            if packet.field_type <= prev_field_type:
                raise MacaroonDeserializationException('fields out of order')
            packets.append(packet)
            prev_field_type = packet.field_type
            data = rest

Response: Parses a sequence of packets in data.


Instruction:
Explain what the following Python 3 code does
def _parse_packet_v2(self, data):
        ''' Parses a V2 data packet at the start of the given data.

        The format of a packet is as follows:

        field_type(varint) payload_len(varint) data[payload_len bytes]

        apart from EOS which has no payload_en or data (it's a single zero
        byte).

        :param data:
        :return: rest of data, PacketV2
        '''
        from pymacaroons.exceptions import MacaroonDeserializationException

        ft, n = _decode_uvarint(data)
        data = data[n:]
        if ft == self._EOS:
            return data, PacketV2(ft, None)
        payload_len, n = _decode_uvarint(data)
        data = data[n:]
        if payload_len > len(data):
            raise MacaroonDeserializationException(
                'field data extends past end of buffer')
        return data[payload_len:], PacketV2(ft, data[0:payload_len])

Response: Parses a V2 data packet at the start of the given data.


Instruction:
Can you generate the documentation for the following Python 3 function
def prepare_for_request(self, discharge_macaroon):
        ''' Return a new discharge macaroon bound to the receiving macaroon's
        current signature so that it can be used in a request.

        This must be done before a discharge macaroon is sent to a server.

        :param discharge_macaroon:
        :return: bound discharge macaroon
        '''
        protected = discharge_macaroon.copy()
        return HashSignaturesBinder(self).bind(protected)

Response:
Returns a new discharge macaroon bound to the receiving macaroon s
        current signature so that it can be used in a request.


Instruction:
Implement a function in Python 3 to
return a caveat as a dictionary for export as the JSON macaroon v1 format.

Response:
def _caveat_v1_to_dict(c):
    ''' Return a caveat as a dictionary for export as the JSON
    macaroon v1 format.
    '''
    serialized = {}
    if len(c.caveat_id) > 0:
        serialized['cid'] = c.caveat_id
    if c.verification_key_id:
        serialized['vid'] = utils.raw_urlsafe_b64encode(
            c.verification_key_id).decode('utf-8')
    if c.location:
        serialized['cl'] = c.location
    return serialized


Instruction:
Explain what the following Python 3 code does
def _caveat_v2_to_dict(c):
    ''' Return a caveat as a dictionary for export as the JSON
    macaroon v2 format.
    '''
    serialized = {}
    if len(c.caveat_id_bytes) > 0:
        _add_json_binary_field(c.caveat_id_bytes, serialized, 'i')
    if c.verification_key_id:
        _add_json_binary_field(c.verification_key_id, serialized, 'v')
    if c.location:
        serialized['l'] = c.location
    return serialized

Response:
Return a caveat as a dictionary for export as the JSON
    macaroon v2 format.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _add_json_binary_field(b, serialized, field):
    ''' Set the given field to the given val (a bytearray) in the serialized
    dictionary.

    If the value isn't valid utf-8, we base64 encode it and use field+"64"
    as the field name.
    '''
    try:
        val = b.decode("utf-8")
        serialized[field] = val
    except UnicodeDecodeError:
        val = utils.raw_urlsafe_b64encode(b).decode('utf-8')
        serialized[field + '64'] = val

Response: Add a field to the given val in the given serialized dictionary.


Instruction:
Make a summary of the following Python 3 code
def _read_json_binary_field(deserialized, field):
    ''' Read the value of a JSON field that may be string or base64-encoded.
    '''
    val = deserialized.get(field)
    if val is not None:
        return utils.convert_to_bytes(val)
    val = deserialized.get(field + '64')
    if val is None:
        return None
    return utils.raw_urlsafe_b64decode(val)

Response: Read the value of a JSON field that may be string or base64 - encoded.


Instruction:
How would you explain what the following Python 3 function does
def serialize(self, m):
        '''Serialize the macaroon in JSON format indicated by the version field.

        @param macaroon the macaroon to serialize.
        @return JSON macaroon.
        '''
        from pymacaroons import macaroon
        if m.version == macaroon.MACAROON_V1:
            return self._serialize_v1(m)
        return self._serialize_v2(m)

Response: Serialize the macaroon in JSON format indicated by the version field.


Instruction:
Write a Python 3 function for
serializing the macaroon in JSON format v1. @param macaroon the macaroon to serialize. @return JSON macaroon.

Response:
def _serialize_v1(self, macaroon):
        '''Serialize the macaroon in JSON format v1.

        @param macaroon the macaroon to serialize.
        @return JSON macaroon.
        '''
        serialized = {
            'identifier': utils.convert_to_string(macaroon.identifier),
            'signature': macaroon.signature,
        }
        if macaroon.location:
            serialized['location'] = macaroon.location
        if macaroon.caveats:
            serialized['caveats'] = [
                _caveat_v1_to_dict(caveat) for caveat in macaroon.caveats
            ]
        return json.dumps(serialized)


Instruction:
Here you have a function in Python 3, explain what it does
def _serialize_v2(self, macaroon):
        '''Serialize the macaroon in JSON format v2.

        @param macaroon the macaroon to serialize.
        @return JSON macaroon in v2 format.
        '''
        serialized = {}
        _add_json_binary_field(macaroon.identifier_bytes, serialized, 'i')
        _add_json_binary_field(binascii.unhexlify(macaroon.signature_bytes),
                               serialized, 's')

        if macaroon.location:
            serialized['l'] = macaroon.location
        if macaroon.caveats:
            serialized['c'] = [
                _caveat_v2_to_dict(caveat) for caveat in macaroon.caveats
            ]
        return json.dumps(serialized)

Response: Serialize the macaroon in JSON format v2.


Instruction:
Make a summary of the following Python 3 code
def _deserialize_v1(self, deserialized):
        '''Deserialize a JSON macaroon in v1 format.

        @param serialized the macaroon in v1 JSON format.
        @return the macaroon object.
        '''
        from pymacaroons.macaroon import Macaroon, MACAROON_V1
        from pymacaroons.caveat import Caveat

        caveats = []
        for c in deserialized.get('caveats', []):
            caveat = Caveat(
                caveat_id=c['cid'],
                verification_key_id=(
                    utils.raw_b64decode(c['vid']) if c.get('vid')
                    else None
                ),
                location=(
                    c['cl'] if c.get('cl') else None
                ),
                version=MACAROON_V1
            )
            caveats.append(caveat)

        return Macaroon(
            location=deserialized.get('location'),
            identifier=deserialized['identifier'],
            caveats=caveats,
            signature=deserialized['signature'],
            version=MACAROON_V1
        )

Response: Deserialize a JSON macaroon in v1 format.


Instruction:
Make a summary of the following Python 3 code
def _deserialize_v2(self, deserialized):
        '''Deserialize a JSON macaroon v2.

        @param serialized the macaroon in JSON format v2.
        @return the macaroon object.
        '''
        from pymacaroons.macaroon import Macaroon, MACAROON_V2
        from pymacaroons.caveat import Caveat
        caveats = []
        for c in deserialized.get('c', []):
            caveat = Caveat(
                caveat_id=_read_json_binary_field(c, 'i'),
                verification_key_id=_read_json_binary_field(c, 'v'),
                location=_read_json_binary_field(c, 'l'),
                version=MACAROON_V2
            )
            caveats.append(caveat)
        return Macaroon(
            location=_read_json_binary_field(deserialized, 'l'),
            identifier=_read_json_binary_field(deserialized, 'i'),
            caveats=caveats,
            signature=binascii.hexlify(
                _read_json_binary_field(deserialized, 's')),
            version=MACAROON_V2
        )

Response: Deserialize a JSON macaroon in JSON format v2.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def find(cls, id='', user=None, project=None):

        if not id:
            if not (user and project):
                raise ValueError('Both user and project required')
            if (
                isinstance(user, User)
                and isinstance(project, Project)
            ):
                _user_id = user.id
                _project_id = project.id
            elif (
                isinstance(user, (int, str,))
                and isinstance(project, (int, str,))
            ):
                _user_id = user
                _project_id = project
            else:
                raise TypeError
            id = cls.where(user_id=_user_id, project_id=_project_id).next().id
        return super(ProjectPreferences, cls).find(id)

Response: Like PaloptesObject. find but can also query by user and project.


Instruction:
Write a Python 3 function for
saving settings for a user and project.

Response:
def save_settings(cls, project=None, user=None, settings=None):
        """
        Save settings for a user without first fetching their preferences.

        - **user** and **project** can be either a :py:class:`.User` and
          :py:class:`.Project` instance respectively, or they can be given as
          IDs. If either argument is given, the other is also required.
        - **settings** is a :py:class:`dict` containing the settings to be
          saved.
        """

        if (isinstance(settings, dict)):
            _to_update = settings
            if (
                isinstance(user, User)
                and isinstance(project, Project)
            ):
                _user_id = user.id
                _project_id = project.id
            elif (
                isinstance(user, (int, str,))
                and isinstance(project, (int, str,))
            ):
                _user_id = user
                _project_id = project
            else:
                raise TypeError
            cls.http_post(
                'update_settings',
                json={
                    'project_preferences': {
                        'user_id': _user_id,
                        'project_id': _project_id,
                        'settings': _to_update,
                    }
                }
            )
        else:
            raise TypeError


Instruction:
Can you implement a function in Python 3 that
returns a context manager that allows asynchronously creating subjects.

Response:
def async_saves(cls):
        """
        Returns a context manager to allow asynchronously creating subjects.
        Using this context manager will create a pool of threads which will
        create multiple subjects at once and upload any local files
        simultaneously.

        The recommended way to use this is with the `with` statement::

            with Subject.async_saves():
                local_files = [...]
                for filename in local_files:
                    s = Subject()
                    s.links.project = 1234
                    s.add_location(filename)
                    s.save()

        Alternatively, you can manually shut down the thread pool::

            pool = Subject.async_saves()
            local_files = [...]
            try:
                for filename in local_files:
                    s = Subject()
                    s.links.project = 1234
                    s.add_location(filename)
                    s.save()
            finally:
                pool.shutdown()
        """
        cls._local.save_exec = ThreadPoolExecutor(
            max_workers=ASYNC_SAVE_THREADS
        )
        return cls._local.save_exec


Instruction:
Given the following Python 3 function, write the documentation
def save(self, client=None):
        if not client:
            client = Panoptes.client()

        async_save = hasattr(self._local, 'save_exec')

        with client:
            if async_save:
                try:
                    # The recursive call will exec in a new thread, so
                    # self._local.save_exec will be undefined above
                    self._async_future = self._local.save_exec.submit(
                        self.save,
                        client=client,
                    )
                    return
                except RuntimeError:
                    del self._local.save_exec
                    async_save = False

            if not self.metadata == self._original_metadata:
                self.modified_attributes.add('metadata')

            response = retry(
                super(Subject, self).save,
                attempts=UPLOAD_RETRY_LIMIT,
                sleeptime=RETRY_BACKOFF_INTERVAL,
                retry_exceptions=(PanoptesAPIException,),
                log_args=False,
            )

            if not response:
                return

            try:
                if async_save:
                    upload_exec = self._local.save_exec
                else:
                    upload_exec = ThreadPoolExecutor(
                        max_workers=ASYNC_SAVE_THREADS,
                    )

                for location, media_data in zip(
                    response['subjects'][0]['locations'],
                    self._media_files
                ):
                    if not media_data:
                        continue

                    for media_type, url in location.items():
                        upload_exec.submit(
                            retry,
                            self._upload_media,
                            args=(url, media_data, media_type),
                            attempts=UPLOAD_RETRY_LIMIT,
                            sleeptime=RETRY_BACKOFF_INTERVAL,
                            retry_exceptions=(
                                requests.exceptions.RequestException,
                            ),
                            log_args=False,
                        )
            finally:
                if not async_save:
                    upload_exec.shutdown()

Response: Save the object to the server.


Instruction:
How would you implement a function in Python 3 that
returns the result of this subject s asynchronous save.

Response:
def async_save_result(self):
        """
        Retrieves the result of this subject's asynchronous save.

        - Returns `True` if the subject was saved successfully.
        - Raises `concurrent.futures.CancelledError` if the save was cancelled.
        - If the save failed, raises the relevant exception.
        - Returns `False` if the subject hasn't finished saving or if the
          subject has not been queued for asynchronous save.
        """
        if hasattr(self, "_async_future") and self._async_future.done():
            self._async_future.result()
            return True
        else:
            return False


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_location(self, location):
        if type(location) is dict:
            self.locations.append(location)
            self._media_files.append(None)
            return
        elif type(location) in (str,) + _OLD_STR_TYPES:
            f = open(location, 'rb')
        else:
            f = location

        try:
            media_data = f.read()
            if MEDIA_TYPE_DETECTION == 'magic':
                media_type = magic.from_buffer(media_data, mime=True)
            else:
                media_type = imghdr.what(None, media_data)
                if not media_type:
                    raise UnknownMediaException(
                        'Could not detect file type. Please try installing '
                        'libmagic: https://panoptes-python-client.readthedocs.'
                        'io/en/latest/user_guide.html#uploading-non-image-'
                        'media-types'
                    )
                media_type = 'image/{}'.format(media_type)
            self.locations.append(media_type)
            self._media_files.append(media_data)
        finally:
            f.close()

Response: Add a location to this subject.


Instruction:
How would you code a function in Python 3 to
download a data export over HTTP and returns a Requests Response_object containing the content of the export.

Response:
def get_export(
        self,
        export_type,
        generate=False,
        wait=False,
        wait_timeout=None,
    ):
        """
        Downloads a data export over HTTP. Returns a `Requests Response
        <http://docs.python-requests.org/en/master/api/#requests.Response>`_
        object containing the content of the export.

        - **export_type** is a string specifying which type of export should be
          downloaded.
        - **generate** is a boolean specifying whether to generate a new export
          and wait for it to be ready, or to just download the latest export.
        - **wait** is a boolean specifying whether to wait for an in-progress
          export to finish, if there is one. Has no effect if ``generate`` is
          ``True``.
        - **wait_timeout** is the number of seconds to wait if ``wait`` is
          ``True``. Has no effect if ``wait`` is ``False`` or if ``generate``
          is ``True``.

        The returned :py:class:`.Response` object has two additional attributes
        as a convenience for working with the CSV content; **csv_reader** and
        **csv_dictreader**, which are wrappers for :py:meth:`.csv.reader`
        and :py:class:`csv.DictReader` respectively. These wrappers take care
        of correctly decoding the export content for the CSV parser.

        Example::

            classification_export = Project(1234).get_export('classifications')
            for row in classification_export.csv_reader():
                print(row)

            classification_export = Project(1234).get_export('classifications')
            for row in classification_export.csv_dictreader():
                print(row)
        """

        if generate:
            self.generate_export(export_type)

        if generate or wait:
            export = self.wait_export(export_type, wait_timeout)
        else:
            export = self.describe_export(export_type)

        if export_type in TALK_EXPORT_TYPES:
            media_url = export['data_requests'][0]['url']
        else:
            media_url = export['media'][0]['src']

        response = requests.get(media_url, stream=True)
        response.csv_reader = functools.partial(
            csv.reader,
            response.iter_lines(decode_unicode=True),
        )
        response.csv_dictreader = functools.partial(
            csv.DictReader,
            response.iter_lines(decode_unicode=True),
        )
        return response


Instruction:
Can you tell what is the following Python 3 function doing
def wait_export(
        self,
        export_type,
        timeout=None,
    ):
        """
        Blocks until an in-progress export is ready.

        - **export_type** is a string specifying which type of export to wait
          for.
        - **timeout** is the maximum number of seconds to wait.

        If ``timeout`` is given and the export is not ready by the time limit,
        :py:class:`.PanoptesAPIException` is raised.
        """

        success = False
        if timeout:
            end_time = datetime.datetime.now() + datetime.timedelta(
                seconds=timeout
            )

        while (not timeout) or (datetime.datetime.now() < end_time):
            export_description = self.describe_export(
                export_type,
            )

            if export_type in TALK_EXPORT_TYPES:
                export_metadata = export_description['data_requests'][0]
            else:
                export_metadata = export_description['media'][0]['metadata']

            if export_metadata.get('state', '') in ('ready', 'finished'):
                success = True
                break

            time.sleep(2)

        if not success:
            raise PanoptesAPIException(
                '{}_export not ready within {} seconds'.format(
                    export_type,
                    timeout
                )
            )

        return export_description

Response: Waits until an export is ready.


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

        if export_type in TALK_EXPORT_TYPES:
            return talk.post_data_request(
                'project-{}'.format(self.id),
                export_type.replace('talk_', '')
            )

        return self.http_post(
            self._export_path(export_type),
            json={"media": {"content_type": "text/csv"}},
        )[0]

Response: Generate a new export.


Instruction:
Can you tell what is the following Python 3 function doing
def describe_export(self, export_type):
        if export_type in TALK_EXPORT_TYPES:
            return talk.get_data_request(
                'project-{}'.format(self.id),
                export_type.replace('talk_', '')
            )[0]

        return self.http_get(
            self._export_path(export_type),
        )[0]

Response: Get metadata for an export.


Instruction:
Can you generate the documentation for the following Python 3 function
def find(cls, id='', slug=None):

        if not id and not slug:
            return None
        try:
            return cls.where(id=id, slug=slug).next()
        except StopIteration:
            raise PanoptesAPIException(
                "Could not find collection with slug='{}'".format(slug)
            )

Response:
Similar to : py : meth :. PanoptesObject. find but allows lookup by slug
            as well as ID.


Instruction:
Given the following Python 3 function, write the documentation
def set_default_subject(self, subject):
        if not (
            isinstance(subject, Subject)
            or isinstance(subject, (int, str,))
        ):
            raise TypeError
        if isinstance(subject, Subject):
            _subject_id = subject.id
        else:
            _subject_id = str(subject)

        self.http_post(
            '{}/links/default_subject'.format(self.id),
            json={'default_subject': _subject_id},
        )

Response: Sets the subject s location media URL as a link.


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

        for sms in SetMemberSubject.where(subject_set_id=self.id):
            yield sms.links.subject

Response: A generator which yields all subjects in this set.


Instruction:
Write a Python 3 function that can
retire the given subjects in this workflow.

Response:
def retire_subjects(self, subjects, reason='other'):
        """
        Retires subjects in this workflow.

        - **subjects** can be a list of :py:class:`Subject` instances, a list
          of subject IDs, a single :py:class:`Subject` instance, or a single
          subject ID.
        - **reason** gives the reason the :py:class:`Subject` has been retired.
          Defaults to **other**.

        Examples::

            workflow.retire_subjects(1234)
            workflow.retire_subjects([1,2,3,4])
            workflow.retire_subjects(Subject(1234))
            workflow.retire_subjects([Subject(12), Subject(34)])
        """

        subjects = [ s.id if isinstance(s, Subject) else s for s in subjects ]

        return Workflow.http_post(
            '{}/retired_subjects'.format(self.id),
            json={
                'subject_ids': subjects,
                'retirement_reason': reason
            }
        )


Instruction:
Here you have a function in Python 3, explain what it does
def collaborators(self, *roles):

        return [
            r.links.owner for r in ProjectRole.where(project_id=self.id)
            if len(roles) == 0 or len(set(roles) & set(r.roles)) > 0
        ]

Response: Returns a list of users who are collaborators on this project.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def connect(cls, *args, **kwargs):
        cls._local.panoptes_client = cls(*args, **kwargs)
        cls._local.panoptes_client.login()
        return cls._local.panoptes_client

Response: Connect to the PANOPTES API.


Instruction:
Here you have a function in Python 3, explain what it does
def where(cls, **kwargs):

        _id = kwargs.pop('id', '')
        return cls.paginated_results(*cls.http_get(_id, params=kwargs))

Response:
Returns a generator which yields instances matching the given query
            arguments.


Instruction:
How would you implement a function in Python 3 that
returns the individual instance with the given ID if it exists. Raises a PanoptesAPIException if the object with the given ID is not found.

Response:
def find(cls, _id):
        """
        Returns the individual instance with the given ID, if it exists. Raises
        :py:class:`PanoptesAPIException` if the object with that ID is not
        found.
        """

        if not _id:
            return None
        try:
            return next(cls.where(id=_id))
        except StopIteration:
            raise PanoptesAPIException(
                "Could not find {} with id='{}'".format(cls.__name__, _id)
            )


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

        if not self.id:
            save_method = Panoptes.client().post
            force_reload = False
        else:
            if not self.modified_attributes:
                return
            if not self._loaded:
                self.reload()
            save_method = Panoptes.client().put
            force_reload = True

        response, response_etag = save_method(
            self.url(self.id),
            json={self._api_slug: self._savable_dict(
                modified_attributes=self.modified_attributes
            )},
            etag=self.etag
        )

        raw_resource_response = response[self._api_slug][0]
        self.set_raw(raw_resource_response, response_etag)

        if force_reload:
            self._loaded = False

        return response

Response: Saves the object to the API.


Instruction:
Can you implement a function in Python 3 that
reloads the object from the API discarding any local changes.

Response:
def reload(self):
        """
        Re-fetches the object from the API, discarding any local changes.
        Returns without doing anything if the object is new.
        """

        if not self.id:
            return
        reloaded_object = self.__class__.find(self.id)
        self.set_raw(
            reloaded_object.raw,
            reloaded_object.etag
        )


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

        if not self.id:
            return
        if not self._loaded:
            self.reload()
        return self.http_delete(self.id, etag=self.etag)

Response: Deletes the object. Returns without doing anything if the object is new.


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

        if self.readonly:
            raise NotImplementedError(
                '{} links can\'t be modified'.format(self._slug)
            )

        if not self._parent.id:
            raise ObjectNotSavedException(
                "Links can not be modified before the object has been saved."
            )

        _objs = [obj for obj in self._build_obj_list(objs) if obj not in self]
        if not _objs:
            return

        self._parent.http_post(
            '{}/links/{}'.format(self._parent.id, self._slug),
            json={self._slug: _objs},
            retry=True,
        )
        self._linked_object_ids.extend(_objs)

Response: Adds the given objects to the LinkCollection.


Instruction:
Explain what the following Python 3 code does
def remove(self, objs):

        if self.readonly:
            raise NotImplementedError(
                '{} links can\'t be modified'.format(self._slug)
            )

        if not self._parent.id:
            raise ObjectNotSavedException(
                "Links can not be modified before the object has been saved."
            )

        _objs = [obj for obj in self._build_obj_list(objs) if obj in self]
        if not _objs:
            return

        _obj_ids = ",".join(_objs)
        self._parent.http_delete(
            '{}/links/{}/{}'.format(self._parent.id, self._slug, _obj_ids),
            retry=True,
        )
        self._linked_object_ids = [
            obj for obj in self._linked_object_ids if obj not in _objs
        ]

Response: Removes the given objects from this LinkCollection.


Instruction:
Can you implement a function in Python 3 that
parses the command line input arguments.

Response:
def parse():
    """Parse the command line input arguments."""
    parser = argparse.ArgumentParser()

    parser.add_argument('--version', action='version',
                        version='f90nml {0}'.format(f90nml.__version__))

    parser.add_argument('--group', '-g', action='store',
                        help="specify namelist group to modify. "
                        "When absent, the first group is used")
    parser.add_argument('--variable', '-v', action='append',
                        help="specify the namelist variable to add or modify, "
                        "followed by the new value. Expressions are of the "
                        "form `VARIABLE=VALUE`")
    parser.add_argument('--patch', '-p', action='store_true',
                        help="modify the existing namelist as a patch")
    parser.add_argument('--format', '-f', action='store',
                        help="specify the output format (json, yaml, or nml)")
    parser.add_argument('--output', '-o', action='store',
                        help="specify namelist group to modify. "
                        "When absent, the first group is used")

    parser.add_argument('input', nargs='?')
    parser.add_argument('output', nargs='?')

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit()

    args = parser.parse_args()

    input_fname = args.input
    output_fname = args.output

    # Get input format
    # TODO: Combine with output format
    if input_fname:
        _, input_ext = os.path.splitext(input_fname)
        if input_ext == '.json':
            input_fmt = 'json'
        elif input_ext == '.yaml':
            input_fmt = 'yaml'
        else:
            input_fmt = 'nml'
    else:
        input_fmt = 'nml'

    # Output format flag validation
    valid_formats = ('json', 'yaml', 'nml')
    if args.format and args.format not in valid_formats:
        print('f90nml: error: format must be one of the following: {0}'
              ''.format(valid_formats), file=sys.stderr)
        sys.exit(-1)

    # Get output format
    # TODO: Combine with input format
    if not args.format:
        if output_fname:
            _, output_ext = os.path.splitext(output_fname)
            if output_ext == '.json':
                output_fmt = 'json'
            elif output_ext in ('.yaml', '.yml'):
                output_fmt = 'yaml'
            else:
                output_fmt = 'nml'
        else:
            output_fmt = 'nml'
    else:
        output_fmt = args.format

    # Confirm that YAML module is available
    if (input_fmt == 'yaml' or output_fmt == 'yaml') and not has_yaml:
        print('f90nml: error: YAML module could not be found.',
              file=sys.stderr)
        sys.exit(-1)

    # Do not patch non-namelist output
    if any(fmt != 'nml' for fmt in (input_fmt, output_fmt)) and args.patch:
        print('f90nml: error: Only namelist files can be patched.',
              file=sys.stderr)
        sys.exit(-1)

    # Read the input file
    if input_fname:
        if input_fmt in ('json', 'yaml'):
            if input_fmt == 'json':
                with open(input_fname) as input_file:
                    input_data = json.load(input_file)
            elif input_ext == '.yaml':
                with open(input_fname) as input_file:
                    input_data = yaml.safe_load(input_file)
        else:
            input_data = f90nml.read(input_fname)
    else:
        input_data = {}

    input_data = f90nml.Namelist(input_data)

    # Construct the update namelist
    update_nml = {}
    if args.variable:
        if not args.group:
            # Use the first available group
            grp = list(input_data.keys())[0]
            warnings.warn('f90nml: warning: Assuming variables are in group \'{0}\'.'.format(grp))
        else:
            grp = args.group

        update_nml_str = '&{0} {1} /\n'.format(grp, ', '.join(args.variable))
        update_io = StringIO(update_nml_str)
        update_nml = f90nml.read(update_io)
        update_io.close()

    # Target output
    output_file = open(output_fname, 'w') if output_fname else sys.stdout

    if args.patch:
        # We have to read the file twice for a patch.  The main reason is
        # to identify the default group, in case this is not provided.
        # It could be avoided if a group is provided, but logically that could
        # a mess that I do not want to sort out right now.
        f90nml.patch(input_fname, update_nml, output_file)

    else:
        # Update the input namelist directly
        if update_nml:
            try:
                input_data[grp].update(update_nml[grp])
            except KeyError:
                input_data[grp] = update_nml[grp]

        # Write to output
        if not args.patch:
            if output_fmt in ('json', 'yaml'):
                if output_fmt == 'json':
                    input_data = input_data.todict(complex_tuple=True)
                    json.dump(input_data, output_file,
                              indent=4, separators=(',', ': '))
                    output_file.write('\n')

                elif output_fmt == 'yaml':
                    input_data = input_data.todict(complex_tuple=True)
                    yaml.dump(input_data, output_file,
                              default_flow_style=False)
            else:
                # Default to namelist output
                f90nml.write(input_data, output_file)

    # Cleanup
    if output_file != sys.stdout:
        output_file.close()


Instruction:
Can you tell what is the following Python 3 function doing
def write(nml, nml_path, force=False, sort=False):
    # Promote dicts to Namelists
    if not isinstance(nml, Namelist) and isinstance(nml, dict):
        nml_in = Namelist(nml)
    else:
        nml_in = nml

    nml_in.write(nml_path, force=force, sort=sort)

Response: Save a namelist to disk using either a file object or its file path.


Instruction:
Given the following Python 3 function, write the documentation
def patch(nml_path, nml_patch, out_path=None):
    parser = Parser()

    return parser.read(nml_path, nml_patch, out_path)

Response: Create a new namelist based on an input namelist and a reference dict.


Instruction:
Create a Python 3 function to
set coordinates of the window to run from x1 y1 x2 y2.

Response:
def setCoords(self, x1, y1, x2, y2):
        """Set coordinates of window to run from (x1,y1) in the
        lower-left corner to (x2,y2) in the upper-right corner."""
        self.trans = Transform(self.size[0], self.size[1], x1, y1, x2, y2)


Instruction:
Make a summary of the following Python 3 code
def convert(self, format="png", **kwargs):
        if format.upper() in cairosvg.SURFACES:
            surface = cairosvg.SURFACES[format.upper()]
        else:
            raise Exception("'%s' image format unavailable: use one of %s" %
                            (format.upper(), list(cairosvg.SURFACES.keys())))
        return surface.convert(bytestring=str(self), **kwargs)

Response: Convert image to specified format


Instruction:
Write a Python 3 function that can
convert canvas to PIL image

Response:
def toPIL(self, **attribs):
        """
        Convert canvas to a PIL image
        """
        import PIL.Image
        bytes = self.convert("png")
        sfile = io.BytesIO(bytes)
        pil = PIL.Image.open(sfile)
        return pil


Instruction:
Explain what the following Python 3 code does
def toGIF(self, **attribs):
        im = self.toPIL(**attribs)
        sfile = io.BytesIO()
        im.save(sfile, format="gif")
        return sfile.getvalue()

Response: Convert canvas to GIF bytes


Instruction:
Here you have a function in Python 3, explain what it does
def getPixels(self):
        array = self.toArray()
        (width, height, depth) = array.size
        for x in range(width):
            for y in range(height):
                yield Pixel(array, x, y)

Response: Return a generator of Pixels from the current Canvas.


Instruction:
Here you have a function in Python 3, explain what it does
def getP1(self):
        return Point(self.center[0] - self.radius,
                     self.center[1] - self.radius)

Response: getP1 - Returns the first point in the region


Instruction:
How would you explain what the following Python 3 function does
def getP2(self):
        return Point(self.center[0] + self.radius,
                     self.center[1] + self.radius)

Response: getP2 - Returns the right lower point of the center of the region


Instruction:
Can you create a Python 3 function that
starts the simulation in the background showing the GUI by default.

Response:
def start_sim(self, gui=True, set_values={}, error=None):
        """
        Run the simulation in the background, showing the GUI by default.
        """
        self.error = error
        if not self.is_running.is_set():
            def loop():
                self.need_to_stop.clear()
                self.is_running.set()
                for robot in self.robots:
                    if robot.brain:
                        self.runBrain(robot.brain)
                count = 0
                while not self.need_to_stop.isSet():
                    if not self.paused.is_set():
                        self.clock += self.sim_time
                        for robot in self.robots:
                            try:
                                robot.update()
                            except Exception as exc:
                                self.need_to_stop.set()
                                if error:
                                    error.value = "Error: %s. Now stopping simulation." % str(exc)
                                else:
                                    raise
                    if gui:
                        self.draw()
                    if count % self.gui_update == 0:
                        if "canvas" in set_values:
                            set_values["canvas"].value = str(self.render())
                        if "energy" in set_values:
                            if len(self.robots) > 0:
                                set_values["energy"].value = str(self.robots[0].energy)
                    count += 1
                    self.realsleep(self.sim_time)
                    if self.robots[0].energy <= 0:
                        self.need_to_stop.set()
                self.is_running.clear()
                for robot in self.robots:
                    robot.stop()
            threading.Thread(target=loop).start()


Instruction:
How would you explain what the following Python 3 function does
def draw(self):
        from calysto.display import display, clear_output
        canvas = self.render()
        clear_output(wait=True)
        display(canvas)

Response: Render and draw the world and robots.


Instruction:
How would you implement a function in Python 3 that
sleeps in simulated time.

Response:
def sleep(self, seconds):
        """
        Sleep in simulated time.
        """
        start = self.time()
        while (self.time() - start < seconds and
               not self.need_to_stop.is_set()):
            self.need_to_stop.wait(self.sim_time)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def runBrain(self, f):
        if self.error:
            self.error.value = ""
        def wrapper():
            self.brain_running.set()
            try:
                f()
            except KeyboardInterrupt:
                # Just stop
                pass
            except Exception as e:
                if self.error:
                    self.error.value = "
" + traceback.format_exc() + "
"
                else:
                    raise
            finally:
                self.brain_running.clear()
            # Otherwise, will show error
        threading.Thread(target=wrapper).start()

Response: Run a brain program in the background.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def addCluster(self, cx, cy, item, count, lam_percent=.25):
        dx, dy = map(lambda v: v * lam_percent, self.psize)
        total = 0
        while total < count:
            points = np.random.poisson(lam=(dx, dy), size=(count, 2))
            for x, y in points:
                px, py = (int(x - dx + cx), int(y - dy + cy))
                if self.getPatch(px, py) is None:
                    self.setPatch(px, py, item)
                    total += 1
                    if total == count:
                        break

Response: Add a Poisson cluster of count items around x y.


Instruction:
Make a summary of the following Python 3 code
def forward(self, seconds, vx=5):
        self.vx = vx
        self.sleep(seconds)
        self.vx = 0

Response: Move continuously in simulator for seconds and velocity vx.


Instruction:
Can you implement a function in Python 3 that
turns a codon of 000 to 999 to a number between - 5. 0 and 5. 0.

Response:
def codon2weight(self, codon):
        """
        Turn a codon of "000" to "999" to a number between
        -5.0 and 5.0.
        """
        length = len(codon)
        retval = int(codon)
        return retval/(10 ** (length - 1)) - 5.0


Instruction:
Write a Python 3 function for
giving a weight between - 5 and 5 turn it into a codon eg 000 to 999

Response:
def weight2codon(self, weight, length=None):
        """
        Given a weight between -5 and 5, turn it into
        a codon, eg "000" to "999"
        """
        if length is None:
            length = self.clen
        retval = 0
        weight = min(max(weight + 5.0, 0), 10.0) * (10 ** (length - 1))
        for i in range(length):
            if i == length - 1: # last one
                d = int(round(weight / (10 ** (length - i - 1))))
            else:
                d = int(weight / (10 ** (length - i - 1)))
            weight = weight % (10 ** (length - i - 1))
            retval += d * (10 ** (length - i - 1))
        return ("%0" + str(length) + "d") % retval


Instruction:
Write a Python 3 script for
returning True if list contains either values of type vtype or None.

Response:
def is_nullable_list(val, vtype):
    """Return True if list contains either values of type `vtype` or None."""
    return (isinstance(val, list) and
            any(isinstance(v, vtype) for v in val) and
            all((isinstance(v, vtype) or v is None) for v in val))


Instruction:
How would you implement a function in Python 3 that
validates and set the column width.

Response:
def column_width(self, width):
        """Validate and set the column width."""
        if isinstance(width, int):
            if width >= 0:
                self._column_width = width
            else:
                raise ValueError('Column width must be nonnegative.')
        else:
            raise TypeError('Column width must be a nonnegative integer.')


Instruction:
Make a summary of the following Python 3 code
def indent(self, value):
        # Explicit indent setting
        if isinstance(value, str):
            if value.isspace() or len(value) == 0:
                self._indent = value
            else:
                raise ValueError('String indentation can only contain '
                                 'whitespace.')

        # Set indent width
        elif isinstance(value, int):
            if value >= 0:
                self._indent = value * ' '
            else:
                raise ValueError('Indentation spacing must be nonnegative.')

        else:
            raise TypeError('Indentation must be specified by string or space '
                            'width.')

Response: Validate and set the indent width.


Instruction:
Create a Python 3 function to
validate and set the comma termination flag.

Response:
def end_comma(self, value):
        """Validate and set the comma termination flag."""
        if not isinstance(value, bool):
            raise TypeError('end_comma attribute must be a logical type.')
        self._end_comma = value


Instruction:
Given the following Python 3 function, write the documentation
def index_spacing(self, value):
        if not isinstance(value, bool):
            raise TypeError('index_spacing attribute must be a logical type.')
        self._index_spacing = value

Response: Validate and set the index_spacing flag.


Instruction:
Write a Python 3 function that can
validate and set the uppercase flag.

Response:
def uppercase(self, value):
        """Validate and set the uppercase flag."""
        if not isinstance(value, bool):
            raise TypeError('uppercase attribute must be a logical type.')
        self._uppercase = value


Instruction:
Given the following Python 3 function, write the documentation
def float_format(self, value):
        if isinstance(value, str):
            # Duck-test the format string; raise ValueError on fail
            '{0:{1}}'.format(1.23, value)

            self._float_format = value
        else:
            raise TypeError('Floating point format code must be a string.')

Response: Validate and set the upper case flag.


Instruction:
Can you generate the documentation for the following Python 3 function
def logical_repr(self, value):
        if not any(isinstance(value, t) for t in (list, tuple)):
            raise TypeError("Logical representation must be a tuple with "
                            "a valid true and false value.")
        if not len(value) == 2:
            raise ValueError("List must contain two values.")

        self.false_repr = value[0]
        self.true_repr = value[1]

Response: Set the string representation of logical values.


Instruction:
Here you have a function in Python 3, explain what it does
def false_repr(self, value):
        if isinstance(value, str):
            if not (value.lower().startswith('f') or
                    value.lower().startswith('.f')):
                raise ValueError("Logical false representation must start "
                                 "with 'F' or '.F'.")
            else:
                self._logical_repr[0] = value
        else:
            raise TypeError('Logical false representation must be a string.')

Response: Validate and set the logical false representation.


Instruction:
Given the following Python 3 function, write the documentation
def start_index(self, value):
        # TODO: Validate contents?  (May want to set before adding the data.)
        if not isinstance(value, dict):
            raise TypeError('start_index attribute must be a dict.')
        self._start_index = value

Response: Validate and set the vector start index.


Instruction:
Here you have a function in Python 3, explain what it does
def write(self, nml_path, force=False, sort=False):
        nml_is_file = hasattr(nml_path, 'read')
        if not force and not nml_is_file and os.path.isfile(nml_path):
            raise IOError('File {0} already exists.'.format(nml_path))

        nml_file = nml_path if nml_is_file else open(nml_path, 'w')
        try:
            self._writestream(nml_file, sort)
        finally:
            if not nml_is_file:
                nml_file.close()

Response: Write a Fortran 90 namelist file.


Instruction:
Can you implement a function in Python 3 that
updates the namelist from another partial or full namelist.

Response:
def patch(self, nml_patch):
        """Update the namelist from another partial or full namelist.

        This is different from the intrinsic `update()` method, which replaces
        a namelist section.  Rather, it updates the values within a section.
        """
        for sec in nml_patch:
            if sec not in self:
                self[sec] = Namelist()
            self[sec].update(nml_patch[sec])


Instruction:
Create a Python 3 function to
return an iterator that spans values with group and variable names.

Response:
def groups(self):
        """Return an iterator that spans values with group and variable names.

        Elements of the iterator consist of a tuple containing two values.  The
        first is internal tuple containing the current namelist group and its
        variable name.  The second element of the returned tuple is the value
        associated with the current group and variable.
        """
        for key, value in self.items():
            for inner_key, inner_value in value.items():
                yield (key, inner_key), inner_value


Instruction:
Make a summary of the following Python 3 code
def _write_nmlgrp(self, grp_name, grp_vars, nml_file, sort=False):
        if self._newline:
            print(file=nml_file)
        self._newline = True

        if self.uppercase:
            grp_name = grp_name.upper()

        if sort:
            grp_vars = Namelist(sorted(grp_vars.items(), key=lambda t: t[0]))

        print('&{0}'.format(grp_name), file=nml_file)

        for v_name, v_val in grp_vars.items():

            v_start = grp_vars.start_index.get(v_name, None)

            for v_str in self._var_strings(v_name, v_val, v_start=v_start):
                nml_line = self.indent + '{0}'.format(v_str)
                print(nml_line, file=nml_file)

        print('/', file=nml_file)

Response: Write namelist group to target file.


Instruction:
Given the following Python 3 function, write the documentation
def _var_strings(self, v_name, v_values, v_idx=None, v_start=None):
        if self.uppercase:
            v_name = v_name.upper()

        var_strs = []

        # Parse a multidimensional array
        if is_nullable_list(v_values, list):
            if not v_idx:
                v_idx = []

            i_s = v_start[::-1][len(v_idx)] if v_start else None

            # FIXME: We incorrectly assume 1-based indexing if it is
            # unspecified.  This is necessary because our output method always
            # separates the outer axes to one per line.  But we cannot do this
            # if we don't know the first index (which we are no longer assuming
            # to be 1-based elsewhere).  Unfortunately, the solution needs a
            # rethink of multidimensional output.

            # NOTE: Fixing this would also clean up the output of todict(),
            # which is now incorrectly documenting unspecified indices as 1.

            # For now, we will assume 1-based indexing here, just to keep
            # things working smoothly.
            if i_s is None:
                i_s = 1

            for idx, val in enumerate(v_values, start=i_s):
                v_idx_new = v_idx + [idx]
                v_strs = self._var_strings(v_name, val, v_idx=v_idx_new,
                                           v_start=v_start)
                var_strs.extend(v_strs)

        # Parse derived type contents
        elif isinstance(v_values, Namelist):
            for f_name, f_vals in v_values.items():
                v_title = '%'.join([v_name, f_name])

                v_start_new = v_values.start_index.get(f_name, None)

                v_strs = self._var_strings(v_title, f_vals,
                                           v_start=v_start_new)
                var_strs.extend(v_strs)

        # Parse an array of derived types
        elif is_nullable_list(v_values, Namelist):
            if not v_idx:
                v_idx = []

            i_s = v_start[::-1][len(v_idx)] if v_start else 1

            for idx, val in enumerate(v_values, start=i_s):

                # Skip any empty elements in a list of derived types
                if val is None:
                    continue

                v_title = v_name + '({0})'.format(idx)

                v_strs = self._var_strings(v_title, val)
                var_strs.extend(v_strs)

        else:
            use_default_start_index = False
            if not isinstance(v_values, list):
                v_values = [v_values]
                use_default_start_index = False
            else:
                use_default_start_index = self.default_start_index is not None

            # Print the index range

            # TODO: Include a check for len(v_values) to determine if vector
            if v_idx or v_start or use_default_start_index:
                v_idx_repr = '('

                if v_start or use_default_start_index:
                    if v_start:
                        i_s = v_start[0]
                    else:
                        i_s = self.default_start_index

                    if i_s is None:
                        v_idx_repr += ':'

                    else:
                        i_e = i_s + len(v_values) - 1

                        if i_s == i_e:
                            v_idx_repr += '{0}'.format(i_s)
                        else:
                            v_idx_repr += '{0}:{1}'.format(i_s, i_e)
                else:
                    v_idx_repr += ':'

                if v_idx:
                    idx_delim = ', ' if self._index_spacing else ','
                    v_idx_repr += idx_delim
                    v_idx_repr += idx_delim.join(str(i) for i in v_idx[::-1])

                v_idx_repr += ')'

            else:
                v_idx_repr = ''

            # Split output across multiple lines (if necessary)

            val_strs = []

            val_line = ''
            for v_val in v_values:

                v_header = v_name + v_idx_repr + ' = '
                # Increase column width if the header exceeds this value
                if len(self.indent + v_header) >= self.column_width:
                    column_width = len(self.indent + v_header) + 1
                else:
                    column_width = self.column_width

                v_width = column_width - len(self.indent + v_header)

                if len(val_line) < v_width:
                    val_line += self._f90repr(v_val) + ', '

                if len(val_line) >= v_width:
                    val_strs.append(val_line.rstrip())
                    val_line = ''

            # Append any remaining values
            if val_line:
                val_strs.append(val_line.rstrip())

            if val_strs:
                if self.end_comma or v_values[-1] is None:
                    pass
                else:
                    val_strs[-1] = val_strs[-1][:-1]

            # Complete the set of values
            if val_strs:
                var_strs.append('{0}{1} = {2}'
                                ''.format(v_name, v_idx_repr,
                                          val_strs[0]).strip())

                for v_str in val_strs[1:]:
                    var_strs.append(' ' * len(v_header) + v_str)

        return var_strs

Response: Convert namelist variable to list of fixed - width strings.


Instruction:
Explain what the following Python 3 code does
def todict(self, complex_tuple=False):
        # TODO: Preserve ordering
        nmldict = OrderedDict(self)

        # Search for namelists within the namelist
        # TODO: Move repeated stuff to new functions
        for key, value in self.items():
            if isinstance(value, Namelist):
                nmldict[key] = value.todict(complex_tuple)

            elif isinstance(value, complex) and complex_tuple:
                nmldict[key] = [value.real, value.imag]
                try:
                    nmldict['_complex'].append(key)
                except KeyError:
                    nmldict['_complex'] = [key]

            elif isinstance(value, list):
                complex_list = False
                for idx, entry in enumerate(value):
                    if isinstance(entry, Namelist):
                        nmldict[key][idx] = entry.todict(complex_tuple)

                    elif isinstance(entry, complex) and complex_tuple:
                        nmldict[key][idx] = [entry.real, entry.imag]
                        complex_list = True

                if complex_list:
                    try:
                        nmldict['_complex'].append(key)
                    except KeyError:
                        nmldict['_complex'] = [key]

        # Append the start index if present
        if self.start_index:
            nmldict['_start_index'] = self.start_index

        return nmldict

Response: Return a dict equivalent to the namelist.


Instruction:
Write a Python 3 function for
converting primitive Python types to equivalent Fortran strings.

Response:
def _f90repr(self, value):
        """Convert primitive Python types to equivalent Fortran strings."""
        if isinstance(value, bool):
            return self._f90bool(value)
        elif isinstance(value, numbers.Integral):
            return self._f90int(value)
        elif isinstance(value, numbers.Real):
            return self._f90float(value)
        elif isinstance(value, numbers.Complex):
            return self._f90complex(value)
        elif isinstance(value, basestring):
            return self._f90str(value)
        elif value is None:
            return ''
        else:
            raise ValueError('Type {0} of {1} cannot be converted to a Fortran'
                             ' type.'.format(type(value), value))


Instruction:
Here you have a function in Python 3, explain what it does
def _f90complex(self, value):
        return '({0:{fmt}}, {1:{fmt}})'.format(value.real, value.imag,
                                               fmt=self.float_format)

Response: Return a Fortran 90 representation of a complex number.


Instruction:
Write a Python 3 script for
returning a Fortran 90 representation of a string.

Response:
def _f90str(self, value):
        """Return a Fortran 90 representation of a string."""
        # Replace Python quote escape sequence with Fortran
        result = repr(str(value)).replace("\\'", "''").replace('\\"', '""')

        # Un-escape the Python backslash escape sequence
        result = result.replace('\\\\', '\\')

        return result


Instruction:
How would you code a function in Python 3 to
extract the AppNexus object or list of objects from the response

Response:
def extract_data(self, page):
        """Extract the AppNexus object or list of objects from the response"""
        response_keys = set(page.keys())
        uncommon_keys = response_keys - self.common_keys

        for possible_data_key in uncommon_keys:
            element = page[possible_data_key]
            if isinstance(element, dict):
                return [self.representation(self.client, self.service_name,
                                            element)]
            if isinstance(element, list):
                return [self.representation(self.client, self.service_name, x)
                        for x in element]


Instruction:
Given the following Python 3 function, write the documentation
def first(self):
        page = self.get_page(num_elements=1)
        data = self.extract_data(page)
        if data:
            return data[0]

Response: Extract the first AppNexus object present in the response


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_page(self, start_element=0, num_elements=None):
        if num_elements is None:
            num_elements = self.batch_size
        specs = self.specs.copy()
        specs.update(start_element=start_element, num_elements=num_elements)
        return self.client.get(self.service_name, **specs)

Response: Get a page of entries from the cache.


Instruction:
Make a summary of the following Python 3 code
def size(self):
        initial_count = self.count()
        count_with_skip = max(0, initial_count - self._skip)
        size = min(count_with_skip, self._limit)
        return size

Response: Return the number of elements of the cursor with skip and limit


Instruction:
Make a summary of the following Python 3 code
def pad(s, n, p = " ", sep = "|", align = "left"):
    if align == "left":
        return (s + (p * n))[:n] + sep
    elif align == "center":
        pos = n + len(s)/2 - n/2
        return ((p * n) + s + (p * n))[pos:pos + n] + sep
    elif align == "right":
        return ((p * n) + s)[-n:] + sep

Response: Returns a padded string.


Instruction:
Write a Python 3 function for
adding two dictionaries together and merges into the first dict. Returns first dict.

Response:
def sumMerge(dict1, dict2):
    """
    Adds two dictionaries together, and merges into the first, dict1.
    Returns first dict.
    """
    for key in dict2:
        dict1[key] = list(map(lambda a,b: a + b, dict1.get(key, [0,0,0,0]), dict2[key]))
    return dict1


Instruction:
Can you generate a brief explanation for the following Python 3 code
def loadNetworkFromFile(filename, mode = 'pickle'):
    if mode == 'pickle':
        import pickle
        fp = open(filename)
        network = pickle.load(fp)
        fp.close()
        return network
    elif mode in ['plain', 'conx']:
        fp = open(filename, "r")
        line = fp.readline()
        network = None
        while line:
            if line.startswith("layer,"):
                # layer, name, size
                temp, name, sizeStr = line.split(",")
                name = name.strip()
                size = int(sizeStr)
                network.addLayer(name, size)
                line = fp.readline()
                weights = [float(f) for f in line.split()]
                for i in range(network[name].size):
                    network[name].weight[i] = weights[i]
            elif line.startswith("connection,"):
                # connection, fromLayer, toLayer
                temp, nameFrom, nameTo = line.split(",")
                nameFrom, nameTo = nameFrom.strip(), nameTo.strip()
                network.connect(nameFrom, nameTo)
                for i in range(network[nameFrom].size):
                    line = fp.readline()
                    weights = [float(f) for f in line.split()]
                    for j in range(network[nameTo].size):
                        network[nameFrom, nameTo].weight[i][j] = weights[j]
            elif line.startswith("parameter,"):
                temp, exp = line.split(",")
                exec(exp) # network is the neural network object
            elif line.startswith("network,"):
                temp, netType = line.split(",")
                netType = netType.strip().lower()
                if netType == "cascornetwork":
                    from pyrobot.brain.cascor import CascorNetwork
                    network = CascorNetwork()
                elif netType == "network":
                    network = Network()
                elif netType == "srn":
                    network = SRN()
                else:
                    raise AttributeError("unknown network type: '%s'" % netType)
            line = fp.readline()
        return network

Response: Load neural network from file.


Instruction:
Can you write a function in Python 3 where it
makes a multi - dimensional array of random floats.

Response:
def ndim(n, *args, **kwargs):
    """
    Makes a multi-dimensional array of random floats. (Replaces RandomArray).
    """
    thunk = kwargs.get("thunk", lambda: random.random())
    if not args:
        return [thunk() for i in range(n)]
    A = []
    for i in range(n):
        A.append( ndim(*args, thunk=thunk) )
    return A


Instruction:
Explain what the following Python 3 code does
def randomArray2(size, bound):
    if type(size) == type(1):
        size = (size,)
    temp = Numeric.array( ndim(*size), thunk=lambda: random.gauss(0, 1)) * (2.0 * bound)
    return temp - bound

Response:
Returns an array initialized to random values between - bound and bound distributed in a gaussian probability distribution more
    appropriate for a Tanh activation function.


Instruction:
Implement a Python 3 function for
returning an array initialized to random values between - max and max.

Response:
def randomArray(size, bound):
    """
    Returns an array initialized to random values between -max and max.
    """
    if type(size) == type(1):
        size = (size,)
    temp = Numeric.array( ndim(*size) ) * (2.0 * bound)
    return temp - bound


Instruction:
Write a Python 3 function for
printing an array to the screen.

Response:
def displayArray(name, a, width = 0):
    """
    Prints an array (any sequence of floats, really) to the screen.
    """
    print(name + ": ", end=" ")
    cnt = 0
    for i in a:
        print("%4.2f" % i, end=" ")
        if width > 0 and (cnt + 1) % width == 0:
            print('')
        cnt += 1


Instruction:
Explain what the following Python 3 code does
def toStringArray(name, a, width = 0):
    string = name + ": "
    cnt = 0
    for i in a:
        string += "%4.2f  " % i
        if width > 0 and (cnt + 1) % width == 0:
            string += '\n'
        cnt += 1
    return string

Response: Returns an array as a string.


Instruction:
Write a Python 3 function that can
write an array to file pointed to by file pointer.

Response:
def writeArray(fp, a, delim = " ", nl = 1):
    """
    Writes a sequence a of floats to file pointed to by file pointer.
    """
    for i in a:
        fp.write("%f%s" % (i, delim))
    if nl:
        fp.write("\n")