Instruction:
Create a Python 3 function for
serializing an instance from a metamodel.

Response:
def serialize_instance(instance):
    '''
    Serialize an *instance* from a metamodel.
    '''
    attr_count = 0
    metaclass = xtuml.get_metaclass(instance)
    s = 'INSERT INTO %s VALUES (' % metaclass.kind
    for name, ty in metaclass.attributes:
        value = getattr(instance, name)

        s += '\n    '
        s += serialize_value(value, ty)

        attr_count += 1
        if attr_count < len(metaclass.attributes):
            s += ', -- %s : %s' % (name, ty)
        else:
            s += ' -- %s : %s' % (name, ty)

    s += '\n);\n'

    return s


Instruction:
Can you tell what is the following Python 3 function doing
def serialize_instances(metamodel):
    '''
    Serialize all instances in a *metamodel*.
    '''
    s = ''
    for inst in metamodel.instances:
        s += serialize_instance(inst)

    return s

Response: Serialize all instances in a metamodel.


Instruction:
Create a Python 3 function to
serialize an xtuml metamodel association.

Response:
def serialize_association(ass):
    '''
    Serialize an xtuml metamodel association.
    '''
    s1 = '%s %s (%s)' % (ass.source_link.cardinality,
                         ass.source_link.to_metaclass.kind,
                         ', '.join(ass.source_keys))

    if ass.target_link.phrase:
        s1 += " PHRASE '%s'" % ass.target_link.phrase

    s2 = '%s %s (%s)' % (ass.target_link.cardinality,
                         ass.target_link.to_metaclass.kind,
                         ', '.join(ass.target_keys))

    if ass.source_link.phrase:
        s2 += " PHRASE '%s'" % ass.source_link.phrase

    return 'CREATE ROP REF_ID %s FROM %s TO %s;\n' % (ass.rel_id,
                                                      s1,
                                                      s2)


Instruction:
Write a Python 3 function that can
serialize an xtUML metamodel class.

Response:
def serialize_class(Cls):
    '''
    Serialize an xtUML metamodel class.
    '''
    metaclass = xtuml.get_metaclass(Cls)
    attributes = ['%s %s' % (name, ty.upper()) for name, ty in metaclass.attributes]

    s = 'CREATE TABLE %s (\n    ' % metaclass.kind
    s += ',\n    '.join(attributes)
    s += '\n);\n'

    return s


Instruction:
Create a Python 3 function for
serializing all metaclasses and associations in a given metamodel.

Response:
def serialize_schema(metamodel):
    '''
    Serialize all class and association definitions in a *metamodel*.
    '''
    s = ''
    for kind in sorted(metamodel.metaclasses.keys()):
        s += serialize_class(metamodel.metaclasses[kind].clazz)

    for ass in sorted(metamodel.associations, key=lambda x: x.rel_id):
        s += serialize_association(ass)

    return s


Instruction:
Write a Python 3 function that can
serialize the database into a string.

Response:
def serialize_database(metamodel):
    '''
    Serialize all instances, class definitions, association definitions, and
    unique identifiers  in a *metamodel*.
    '''
    schema = serialize_schema(metamodel)
    instances = serialize_instances(metamodel)
    identifiers = serialize_unique_identifiers(metamodel)

    return ''.join([schema, instances, identifiers])


Instruction:
Here you have a function in Python 3, explain what it does
def serialize(resource):
    '''
    Serialize some xtuml *resource*, e.g. an instance or a complete metamodel.
    '''
    if isinstance(resource, xtuml.MetaModel):
        return serialize_database(resource)

    elif isinstance(resource, type) and issubclass(resource, xtuml.Class):
        return serialize_class(resource)

    elif isinstance(resource, xtuml.Association):
        return serialize_association(resource)

    elif isinstance(resource, xtuml.Class):
        return serialize_instance(resource)

Response: Serialize some xtuml resource e. g. an instance or a complete metamodel.


Instruction:
Write a Python 3 function for
persisting all instances in a given metamodel to disk.

Response:
def persist_instances(metamodel, path, mode='w'):
    '''
    Persist all instances in a *metamodel* by serializing them and saving to a
    *path* on disk.
    '''
    with open(path, mode) as f:
        for inst in metamodel.instances:
            s = serialize_instance(inst)
            f.write(s)


Instruction:
Implement a Python 3 function for
persisting all class and association definitions in a given metamodel to a file on disk.

Response:
def persist_schema(metamodel, path, mode='w'):
    '''
    Persist all class and association definitions in a *metamodel* by
    serializing them and saving to a *path* on disk.
    '''
    with open(path, mode) as f:
        for kind in sorted(metamodel.metaclasses.keys()):
            s = serialize_class(metamodel.metaclasses[kind].clazz)
            f.write(s)

        for ass in sorted(metamodel.associations, key=lambda x: x.rel_id):
            s = serialize_association(ass)
            f.write(s)


Instruction:
Implement a function in Python 3 to
persist all unique identifiers in a metamodel to a file on disk.

Response:
def persist_unique_identifiers(metamodel, path, mode='w'):
    '''
    Persist all unique identifiers in a *metamodel* by serializing them and
    saving to a *path* on disk.
    '''
    with open(path, mode) as f:
        for metaclass in metamodel.metaclasses.values():
            for index_name, attribute_names in metaclass.indices.items():
                attribute_names = ', '.join(attribute_names)
                s = 'CREATE UNIQUE INDEX %s ON %s (%s);\n' % (index_name,
                                                              metaclass.kind,
                                                              attribute_names)
                f.write(s)


Instruction:
Write a Python 3 script to
persist all instances class definitions association definitions in a given metamodel to a file on disk.

Response:
def persist_database(metamodel, path, mode='w'):
    '''
    Persist all instances, class definitions and association definitions in a
    *metamodel* by serializing them and saving to a *path* on disk.
    '''
    with open(path, mode) as f:
        for kind in sorted(metamodel.metaclasses.keys()):
            metaclass = metamodel.metaclasses[kind]
            s = serialize_class(metaclass.clazz)
            f.write(s)

            for index_name, attribute_names in metaclass.indices.items():
                attribute_names = ', '.join(attribute_names)
                s = 'CREATE UNIQUE INDEX %s ON %s (%s);\n' % (index_name,
                                                              metaclass.kind,
                                                              attribute_names)
                f.write(s)

        for ass in sorted(metamodel.associations, key=lambda x: x.rel_id):
            s = serialize_association(ass)
            f.write(s)

        for inst in metamodel.instances:
            s = serialize_instance(inst)
            f.write(s)


Instruction:
Write a Python 3 script to
save a variable on given path using Pickle

Response:
def save(variable, filename):
    """Save variable on given path using Pickle

    Args:
        variable: what to save
        path (str): path of the output
    """
    fileObj = open(filename, 'wb')
    pickle.dump(variable, fileObj)
    fileObj.close()


Instruction:
Explain what the following Python 3 code does
def load(filename):
    fileObj = open(filename, 'rb')
    variable = pickle.load(fileObj)
    fileObj.close()
    return variable

Response: Load a pickle file into a dict


Instruction:
Write a Python 3 function that can
function for command line execution

Response:
def main():
    """Function for command line execution"""

    parser = ArgumentParser(description="search files using n-grams")
    parser.add_argument('--path', dest='path', help="where to search", nargs=1, action="store", default=getcwd())
    parser.add_argument('--update', dest='update', help="update the index", action='store_true', default=True)
    parser.add_argument('--filetype', dest='filetype', help="any, images, documents, code, audio, video", nargs=1, action="store", default=["any"])
    parser.add_argument('--verbose', dest='verbose', help="extended output", action='store_true', default=False)
    parser.add_argument('--results', dest='results', help="number of results to display", action="store", default=10)
    parser.add_argument('query', nargs='+', help="what to search", action="store")
    args = parser.parse_args()

    if args.verbose:
        verbose = 2
        pprint(args)
    else:
        verbose = 0

    query = args.query[0]
    for arg in args.query[1:]:
        query = query + " " + arg
    slb = min([len(w) for w in query.split(" ")])

    files = Files(path=args.path, filetype=args.filetype[0], exclude=[], update=args.update, verbose=verbose)
    index = Index(files, slb=slb, verbose=verbose)
    results = index.search(query, verbose=verbose)
    Handler(results, results_number=int(args.results))


Instruction:
Implement a Python 3 function for
searching the database for the files satisfying a query.

Response:
def search(self, query, verbose=0):
        """Searches files satisfying query

        It first decompose the query in ngrams, then score each document containing
        at least one ngram with the number. The ten document having the most ngrams
        in common with the query are selected.

        Args:
             query (str): what to search;
             results_number (int): number of results to return (default: 10)
        """
        if verbose > 0:
            print("searching " + query)
        query = query.lower()
        qgram = ng(query, self.slb)
        qocument = set()
        for q in qgram:
            if q in self.ngrams.keys():
                for i in self.ngrams[q]:
                    qocument.add(i)
        self.qocument = qocument
        results = {}
        for i in qocument:
           for j in self.D[i].keys():
                if not j in results.keys():
                    results[j] = 0
                results[j] = results[j] + self.D[i][j]
        sorted_results = sorted(results.items(), key=operator.itemgetter(1), reverse=True)
        return [self.elements[f[0]] for f in sorted_results]


Instruction:
Here you have a function in Python 3, explain what it does
def partition(condition, collection) -> Tuple[List, List]:
    succeed, fail = [], []

    for x in collection:
        if condition(x):
            succeed.append(x)
        else:
            fail.append(x)

    return succeed, fail

Response: Partitions a list into two based on a condition.


Instruction:
Explain what the following Python 3 code does
async def cli(location_strings: Tuple[str], random_postcodes_count: int, *,
              bikes: bool = False, crime: bool = False,
              nearby: bool = False, as_json: bool = False):
    """
    Runs the CLI app.
    Tries to execute as many steps as possible to give the user
    the best understanding of the errors (if there are any).

    :param location_strings: A list of desired postcodes or coordinates.
    :param random_postcodes_count: A number of random postcodes to fetch..
    :param bikes: A flag to include bikes.
    :param crime: A flag to include crime.
    :param nearby: A flag to include nearby.
    :param as_json: A flag to make json output.
    """

    def match_getter(location) -> Optional[PostcodeGetter]:
        for getter in getters:
            if getter.can_provide(location):
                return getter(location)
        else:
            return None

    async def handle_getter(exception_list, getter):
        try:
            return await getter.get_postcodes()
        except (CachingError, ApiError):
            exception_list.append(f"Could not get data for {getter}")

    async def handle_datas(exception_list, postcode):
        postcode_data, new_exceptions = await get_postcode_data(postcode, bikes, crime, nearby)
        exception_list += new_exceptions
        return postcode_data

    exception_list: List[Exception] = []

    handle_getter = partial(handle_getter, exception_list)
    handle_datas = partial(handle_datas, exception_list)

    postcode_getters = {location: match_getter(location) for location in
                        set(location_strings) | ({random_postcodes_count} if random_postcodes_count > 0 else set())}

    matched, unmatched = partition(lambda k_v: k_v[1] is not None, postcode_getters.items())

    for location, getter in unmatched:
        echo(f"Invalid input for {location}")

    postcodes_collection = [await handle_getter(getter) for location, getter in matched]

    if len(exception_list) > 0:
        for f in exception_list:
            echo(str(f))
        return 1

    postcode_datas = [await handle_datas(postcode) for entry in postcodes_collection for postcode in entry]
    serializer = (PostcodeSerializerJSON if as_json else PostcodeSerializerHuman)(postcode_datas)
    echo(serializer.serialize())

Response: Runs the CLI app.


Instruction:
Write a Python 3 function for
getting the neighbourhood from the API.

Response:
async def fetch_neighbourhood(lat: float, long: float) -> Optional[dict]:
    """
    Gets the neighbourhood from the fetch that is associated with the given postcode.
    :return: A neighbourhood object parsed from the fetch.
    :raise ApiError: When there was an error connecting to the API.
    """

    lookup_url = f"https://data.police.uk/api/locate-neighbourhood?q={lat},{long}"

    async with ClientSession() as session:
        try:
            async with session.get(lookup_url) as request:
                if request.status == 404:
                    return None
                neighbourhood = await request.json()
        except ClientConnectionError as con_err:
            logger.debug(f"Could not connect to {con_err.host}")
            raise ApiError(f"Could not connect to {con_err.host}")
        except JSONDecodeError as dec_err:
            logger.error(f"Could not decode data: {dec_err}")
            raise ApiError(f"Could not decode data: {dec_err}")

        neighbourhood_url = f"https://data.police.uk/api/{neighbourhood['force']}/{neighbourhood['neighbourhood']}"

        try:
            async with session.get(neighbourhood_url) as request:
                neighbourhood_data = await request.json()
        except ConnectionError as con_err:
            logger.debug(f"Could not connect to {con_err.args[0].pool.host}")
            raise ApiError(f"Could not connect to {con_err.args[0].pool.host}")
        except JSONDecodeError as dec_err:
            logger.error(f"Could not decode data: {dec_err}")
            raise ApiError(f"Could not decode data: {dec_err}")

        return neighbourhood_data


Instruction:
How would you explain what the following Python 3 function does
async def fetch_crime(lat: float, long: float) -> List[Dict]:
    crime_lookup = f"https://data.police.uk/api/crimes-street/all-crime?lat={lat}&lng={long}"
    async with ClientSession() as session:
        try:
            async with session.get(crime_lookup) as request:
                crime_request = await request.json()
        except ClientConnectionError as con_err:
            logger.debug(f"Could not connect to {con_err.args[0].pool.host}")
            raise ApiError(f"Could not connect to {con_err.args[0].pool.host}")
        except JSONDecodeError as dec_err:
            logger.error(f"Could not decode data: {dec_err}")
            raise ApiError(f"Could not decode data: {dec_err}")
        else:
            return crime_request

Response: Get crime for a given latitude and longitude.


Instruction:
How would you explain what the following Python 3 function does
def run(locations, random, bikes, crime, nearby, json, update_bikes, api_server, cross_origin, host, port, db_path,
        verbose):
    """
    Runs the program. Takes a list of postcodes or coordinates and
    returns various information about them. If using the cli, make
    sure to update the bikes database with the -u command.

    Locations can be either a specific postcode, or a pair of coordinates.
    Coordinates are passed in the form "55.948824,-3.196425".

    :param locations: The list of postcodes or coordinates to search.
    :param random: The number of random postcodes to include.
    :param bikes: Includes a list of stolen bikes in that area.
    :param crime: Includes a list of committed crimes in that area.
    :param nearby: Includes a list of wikipedia articles in that area.
    :param json: Returns the data in json format.
    :param update_bikes: Whether to force update bikes.
    :param api_server: If given, the program will instead run a rest api.
    :param cross_origin:
    :param host:
    :param port: Defines the port to run the rest api on.
    :param db_path: The path to the sqlite db to use.
    :param verbose: The verbosity.
    """

    log_levels = [logging.WARNING, logging.INFO, logging.DEBUG]
    logging.basicConfig(level=log_levels[min(verbose, 2)])

    initialize_database(db_path)

    loop = get_event_loop()

    if update_bikes:
        logger.info("Force updating bikes.")
        loop.run_until_complete(util.update_bikes())

    if api_server:
        if cross_origin:
            enable_cross_origin(app)

        try:
            web.run_app(app, host=host, port=port)
        except CancelledError as e:
            if e.__context__ is not None:
                click.echo(Fore.RED + (
                    f"Could not bind to address {host}:{port}" if e.__context__.errno == 48 else e.__context__))
                exit(1)
            else:
                click.echo("Goodbye!")

    elif len(locations) > 0 or random > 0:
        exit(loop.run_until_complete(cli(locations, random, bikes=bikes, crime=crime, nearby=nearby, as_json=json)))
    else:
        click.echo(Fore.RED + "Either include a post code, or the --api-server flag.")

Response:
Runs the program. Takes a list of postcodes or coordinates and
    returns various information about them. If using the cli, make
    sure to update the bikes database with the -u command.

    Locations can be either a specific postcode, or a pair of coordinates.
    Coordinates are passed in the form "55.948824,-3.196425".

    :param locations: The list of postcodes or coordinates to search.
    :param random: The number of random postcodes to include.
    :param bikes: Includes a list of stolen bikes in that area.
    :param crime: Includes a list of committed crimes in that area.
    :param nearby: Includes a list of wikipedia articles in that area.
    :param json: Returns the data in json format.
    :param update_bikes: Whether to force update bikes.
    :param api_server: If given, the program will instead run a rest api.
    :param cross_origin:
    :param host:
    :param port: Defines the port to run the rest api on.
    :param db_path: The path to the sqlite db to use.
    :param verbose: The verbosity.


Instruction:
Write a Python 3 function that can
return the BiDi context for the current language.

Response:
def bidi(request):
    """Adds to the context BiDi related variables

    LANGUAGE_DIRECTION -- Direction of current language ('ltr' or 'rtl')
    LANGUAGE_START -- Start of language layout ('right' for rtl, 'left'
                      for 'ltr')
    LANGUAGE_END -- End of language layout ('left' for rtl, 'right'
                      for 'ltr')
    LANGUAGE_MARKER -- Language marker entity ('' for rtl, '&lrm'
                       for ltr)

    """
    from django.utils import translation
    from django.utils.safestring import mark_safe

    if translation.get_language_bidi():
        extra_context = {
            'LANGUAGE_DIRECTION':'rtl',
            'LANGUAGE_START':'right',
            'LANGUAGE_END':'left',
            'LANGUAGE_MARKER': mark_safe(''),
        }
    else:
        extra_context = {
            'LANGUAGE_DIRECTION':'ltr',
            'LANGUAGE_START':'left',
            'LANGUAGE_END':'right',
            'LANGUAGE_MARKER': mark_safe(''),
        }

    return extra_context


Instruction:
How would you implement a function in Python 3 that
determines if an attribute of an instance with a specific name* is null.

Response:
def _is_null(instance, name):
    '''
    Determine if an attribute of an *instance* with a specific *name*
    is null.
    '''
    if name in instance.__dict__:
        value = instance.__dict__[name]
    else:
        value = getattr(instance, name)

    if value:
        return False

    elif value is None:
        return True

    name = name.upper()
    metaclass = get_metaclass(instance)
    for attr_name, attr_ty in metaclass.attributes:
        if attr_name.upper() != name:
            continue

        attr_ty = attr_ty.upper()
        if attr_ty == 'UNIQUE_ID':
            # UUID(int=0) is reserved for null
            return value == 0

        elif attr_ty == 'STRING':
            # empty string is reserved for null
            return len(value) == 0

        else:
            #null-values for integer, boolean and real are not supported
            return False


Instruction:
Here you have a function in Python 3, explain what it does
def apply_query_operators(iterable, ops):
    '''
    Apply a series of query operators to a sequence of instances, e.g.
    where_eq(), order_by() or filter functions.
    '''
    for op in ops:
        if isinstance(op, WhereEqual):
            iterable = op(iterable)

        elif isinstance(op, OrderBy):
            iterable = op(iterable)

        elif isinstance(op, dict):
            iterable = WhereEqual(op)(iterable)

        else:
            iterable = filter(op, iterable)

    return iterable

Response:
Apply a series of query operators to a sequence of instances e. g.
    where_eq ( where_eq ) order_by ( orderBy ) or filter functions.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def navigate_subtype(supertype, rel_id):
    '''
    Perform a navigation from *supertype* to its subtype across *rel_id*. The
    navigated association must be modeled as a subtype-supertype association.

    The return value will an instance or None.
    '''
    if not supertype:
        return

    if isinstance(rel_id, int):
        rel_id = 'R%d' % rel_id

    metaclass = get_metaclass(supertype)
    for kind, rel_id_candidate, _ in metaclass.links:
        if rel_id != rel_id_candidate:
            continue

        subtype = navigate_one(supertype).nav(kind, rel_id)()
        if subtype:
            return subtype

Response: Perform a navigation from a supertype to its subtype across a rel_id.


Instruction:
Can you write a function in Python 3 where it
sorts a set of instances in the order they appear across a conditional and reflexive association.

Response:
def sort_reflexive(set_of_instances, rel_id, phrase):
    '''
    Sort a *set of instances* in the order they appear across a conditional and
    reflexive association. The first instance in the resulting ordered set is
    **not** associated to an instance across the given *phrase*.
    '''
    if not isinstance(set_of_instances, QuerySet):
        raise MetaException('The collection to sort must be a QuerySet')

    if not set_of_instances.first:
        return QuerySet()

    if isinstance(rel_id, int):
        rel_id = 'R%d' % rel_id

    # Figure out the phrase in the other direction
    metaclass = get_metaclass(set_of_instances.first)
    for link in metaclass.links.values():
        if link.to_metaclass != metaclass:
            continue

        if link.rel_id != rel_id:
            continue

        if link.phrase == phrase:
            continue

        other_phrase = link.phrase
        break
    else:
        raise UnknownLinkException(metaclass.kind, metaclass.kind, rel_id, phrase)

    first_filt = lambda sel: not navigate_one(sel).nav(metaclass.kind, rel_id, phrase)()
    first_instances = list(filter(first_filt, set_of_instances))
    if not first_instances:
        #the instance sequence is recursive, start anywhere
        first_instances = [set_of_instances.first]

    def sequence_generator():
        for first in first_instances:
            inst = first
            while inst:
                if inst in set_of_instances:
                    yield inst
                inst = navigate_one(inst).nav(metaclass.kind, rel_id, other_phrase)()
                if inst is first:
                    break

    return QuerySet(sequence_generator())


Instruction:
Can you generate the documentation for the following Python 3 function
def _find_link(inst1, inst2, rel_id, phrase):
    '''
    Find links that correspond to the given arguments.
    '''
    metaclass1 = get_metaclass(inst1)
    metaclass2 = get_metaclass(inst2)

    if isinstance(rel_id, int):
        rel_id = 'R%d' % rel_id

    for ass in metaclass1.metamodel.associations:
        if ass.rel_id != rel_id:
            continue

        if (ass.source_link.from_metaclass.kind == metaclass1.kind and
            ass.source_link.to_metaclass.kind == metaclass2.kind and
            ass.source_link.phrase == phrase):
            return inst1, inst2, ass

        if (ass.target_link.from_metaclass.kind == metaclass1.kind and
            ass.target_link.to_metaclass.kind == metaclass2.kind and
            ass.target_link.phrase == phrase):
            return inst2, inst1, ass

    raise UnknownLinkException(metaclass1.kind, metaclass2.kind, rel_id, phrase)

Response: Find links that correspond to the given arguments.


Instruction:
Create a Python 3 function for
relating from_instance to to_instance across the relationship_id.

Response:
def relate(from_instance, to_instance, rel_id, phrase=''):
    '''
    Relate *from_instance* to *to_instance* across *rel_id*. For reflexive
    association, a *phrase* indicating the direction must also be provided.

    The two instances are related to each other by copying the identifying
    attributes from the instance on the TO side of a association to the instance
    n the FROM side. Updated values which affect existing associations are
    propagated. A set of all affected instances will be returned.
    '''
    if None in [from_instance, to_instance]:
        return False

    inst1, inst2, ass = _find_link(from_instance, to_instance, rel_id, phrase)
    if not ass.source_link.connect(inst1, inst2):
        raise RelateException(from_instance, to_instance, rel_id, phrase)

    if not ass.target_link.connect(inst2, inst1):
        raise RelateException(from_instance, to_instance, rel_id, phrase)

    return True


Instruction:
Can you generate the documentation for the following Python 3 function
def get_metaclass(class_or_instance):
    '''
    Get the metaclass for a *class_or_instance*.
    '''
    if isinstance(class_or_instance, Class):
        return class_or_instance.__metaclass__

    elif issubclass(class_or_instance, Class):
        return class_or_instance.__metaclass__

    raise MetaException("the provided argument is not an xtuml class or instance")

Response: Get the metaclass for a class or instance.


Instruction:
Explain what the following Python 3 code does
def delete(instance, disconnect=True):
    '''
    Delete an *instance* from its metaclass instance pool and optionally
    *disconnect* it from any links it might be connected to.
    '''
    if not isinstance(instance, Class):
        raise DeleteException("the provided argument is not an xtuml instance")

    return get_metaclass(instance).delete(instance, disconnect)

Response: Delete an instance from its metaclass instance pool and optionally delete it from any links it might be connected to.


Instruction:
How would you explain what the following Python 3 function does
def formalize(self):
        '''
        Formalize the association and expose referential attributes
        on instances.
        '''
        source_class = self.source_link.to_metaclass
        target_class = self.target_link.to_metaclass

        source_class.referential_attributes |= set(self.source_keys)
        target_class.identifying_attributes |= set(self.target_keys)

        def fget(inst, ref_name, alt_prop):
            other_inst = self.target_link.navigate_one(inst)
            if other_inst is None and alt_prop:
                return alt_prop.fget(inst)

            return getattr(other_inst, ref_name, None)

        def fset(inst, value, name, ref_name, alt_prop):
            kind = get_metaclass(inst).kind
            raise MetaException('%s.%s is a referential attribute '\
                                'and cannot be assigned directly'% (kind, name))

            #other_inst = self.target_link.navigate_one(inst)
            #if other_inst is None and alt_prop:
            #    return alt_prop.fset(inst, value)
            #
            #elif other_inst:
            #    return setattr(other_inst, ref_name, value)

        for ref_key, primary_key in zip(self.source_keys, self.target_keys):
            prop = getattr(source_class.clazz, ref_key, None)
            prop = property(partial(fget, ref_name=primary_key, alt_prop=prop),
                            partial(fset, name=ref_key, ref_name=primary_key, alt_prop=prop))
            setattr(source_class.clazz, ref_key, prop)

Response: Formalize the association and expose referential attributes on instances.


Instruction:
Given the following Python 3 function, write the documentation
def cardinality(self):
        '''
        Obtain the cardinality string.

        Example: '1C' for a conditional link with a single instance [0..1]
                 'MC' for a link with any number of instances [0..*]
                 'M'  for a more than one instance [1..*]
                 'M'  for a link with exactly one instance [1]
        '''
        if self.many:
            s = 'M'
        else:
            s = '1'

        if self.conditional:
            s += 'C'

        return s

Response: Obtain the cardinality string.


Instruction:
Can you write a function in Python 3 where it
connects an instance to another_instance.

Response:
def connect(self, instance, another_instance, check=True):
        '''
        Connect an *instance* to *another_instance*.

        Optionally, disable any cardinality *check* that would prevent the two
        instances from being connected.
        '''
        if instance not in self:
            self[instance] = xtuml.OrderedSet()

        if another_instance in self[instance]:
            return True

        if self[instance] and not self.many and check:
            return False

        self[instance].add(another_instance)
        return True


Instruction:
Write a Python 3 function that can
disconnect an instance from another_instance.

Response:
def disconnect(self, instance, another_instance):
        '''
        Disconnect an *instance* from *another_instance*.
        '''
        if instance not in self:
            return False

        if another_instance not in self[instance]:
            return False

        self[instance].remove(another_instance)
        return True


Instruction:
How would you implement a function in Python 3 that
computes the lookup key for an instance i. e. a foreign key that can be used to identify an instance at the end of the link.

Response:
def compute_lookup_key(self, from_instance):
        '''
        Compute the lookup key for an instance, i.e. a foreign key that
        can be used to identify an instance at the end of the link.
        '''
        kwargs = dict()
        for attr, other_attr in self.key_map.items():
            if _is_null(from_instance, attr):
                return None

            if attr in from_instance.__dict__:
                kwargs[other_attr] = from_instance.__dict__[attr]
            else:
                kwargs[other_attr] = getattr(from_instance, attr)

        return frozenset(tuple(kwargs.items()))


Instruction:
Can you tell what is the following Python 3 function doing
def compute_index_key(self, to_instance):
        '''
        Compute the index key that can be used to identify an instance
        on the link.
        '''
        kwargs = dict()
        for attr in self.key_map.values():
            if _is_null(to_instance, attr):
                return None

            if attr in to_instance.__dict__:
                kwargs[attr] = to_instance.__dict__[attr]
            else:
                kwargs[attr] = getattr(to_instance, attr)

        return frozenset(tuple(kwargs.items()))

Response:
Compute the index key that can be used to identify an instance
            on the link.


Instruction:
Write a Python 3 function that can
obtain the type of an attribute.

Response:
def attribute_type(self, attribute_name):
        '''
        Obtain the type of an attribute.
        '''
        attribute_name = attribute_name.upper()
        for name, ty in self.attributes:
            if name.upper() == attribute_name:
                return ty


Instruction:
Write a Python 3 function for
adding a new link from self to metaclass.

Response:
def add_link(self, metaclass, rel_id, phrase, conditional, many):
        '''
        Add a new link from *self* to *metaclass*.
        '''
        link = Link(self, rel_id, metaclass, phrase, conditional, many)
        key = (metaclass.kind.upper(), rel_id, phrase)
        self.links[key] = link

        return link


Instruction:
Here you have a function in Python 3, explain what it does
def append_attribute(self, name, type_name):
        '''
        Append an attribute with a given *name* and *type name* at the end of
        the list of attributes.
        '''
        attr = (name, type_name)
        self.attributes.append(attr)

Response: Append an attribute with a given name and type name to the end of the list of attributes.


Instruction:
Make a summary of the following Python 3 code
def insert_attribute(self, index, name, type_name):
        '''
        Insert an attribute with a given *name* and *type name* at some *index*
        in the list of attributes.
        '''
        attr = (name, type_name)
        self.attributes.insert(index, attr)

Response: Insert an attribute with a given name and type name at some index in the list of attributes.


Instruction:
Given the following Python 3 function, write the documentation
def delete_attribute(self, name):
        '''
        Delete an attribute with a given *name* from the list of attributes.
        '''
        for idx, attr in enumerate(self.attributes):
            attr_name, _ = attr
            if attr_name == name:
                del self.attributes[idx]
                return

Response: Delete an attribute with a given name from the list of attributes.


Instruction:
Explain what the following Python 3 code does
def default_value(self, type_name):
        '''
        Obtain the default value for some *type name*.
        '''
        uname = type_name.upper()
        if   uname == 'BOOLEAN':
            return False

        elif uname == 'INTEGER':
            return 0

        elif uname == 'REAL':
            return 0.0

        elif uname == 'STRING':
            return ''

        elif uname == 'UNIQUE_ID':
            if self.metamodel:
                return next(self.metamodel.id_generator)
            else:
                return None
        else:
            raise MetaException("Unknown type named '%s'" % type_name)

Response: Obtain the default value for some type name.


Instruction:
Explain what the following Python 3 code does
def new(self, *args, **kwargs):
        '''
        Create and return a new instance.
        '''
        inst = self.clazz()
        self.storage.append(inst)

        # set all attributes with an initial default value
        referential_attributes = dict()
        for name, ty in self.attributes:
            if name not in self.referential_attributes:
                value = self.default_value(ty)
                setattr(inst, name, value)

        # set all positional arguments
        for attr, value in zip(self.attributes, args):
            name, ty = attr
            if name not in self.referential_attributes:
                setattr(inst, name, value)
            else:
                referential_attributes[name] = value

        # set all named arguments
        for name, value in kwargs.items():
            if name not in self.referential_attributes:
                setattr(inst, name, value)
            else:
                referential_attributes[name] = value

        if not referential_attributes:
            return inst

        # batch relate referential attributes
        for link in self.links.values():
            if set(link.key_map.values()) - set(referential_attributes.keys()):
                continue

            kwargs = dict()
            for key, value in link.key_map.items():
                kwargs[key] = referential_attributes[value]

            if not kwargs:
                continue

            for other_inst in link.to_metaclass.query(kwargs):
                relate(other_inst, inst, link.rel_id, link.phrase)

        for name, value in referential_attributes.items():
            if getattr(inst, name) != value:
                logger.warning('unable to assign %s to %s', name, inst)

        return inst

Response: Create and return a new instance of the class.


Instruction:
Given the following Python 3 function, write the documentation
def clone(self, instance):
        '''
        Create a shallow clone of an *instance*.

        **Note:** the clone and the original instance **does not** have to be
        part of the same metaclass.
        '''
        args = list()
        for name, _ in get_metaclass(instance).attributes:
            value = getattr(instance, name)
            args.append(value)

        return self.new(*args)

Response: Create a shallow clone of an instance.


Instruction:
How would you code a function in Python 3 to
delete an instance from the instance pool and optionally disconnects it from any links it might be connected to.

Response:
def delete(self, instance, disconnect=True):
        '''
        Delete an *instance* from the instance pool and optionally *disconnect*
        it from any links it might be connected to. If the *instance* is not
        part of the metaclass, a *MetaException* is thrown.
        '''
        if instance in self.storage:
            self.storage.remove(instance)
        else:
            raise DeleteException("Instance not found in the instance pool")

        if not disconnect:
            return

        for link in self.links.values():
            if instance not in link:
                continue

            for other in link[instance]:
                unrelate(instance, other, link.rel_id, link.phrase)


Instruction:
Can you generate the documentation for the following Python 3 function
def select_one(self, *args):
        '''
        Select a single instance from the instance pool. Query operators such as
        where_eq(), order_by() or filter functions may be passed as optional
        arguments.
        '''
        s = apply_query_operators(self.storage, args)
        return next(iter(s), None)

Response: Select a single instance from the instance pool.


Instruction:
Make a summary of the following Python 3 code
def select_many(self, *args):
        '''
        Select several instances from the instance pool. Query operators such as
        where_eq(), order_by() or filter functions may be passed as optional
        arguments.
        '''
        s = apply_query_operators(self.storage, args)
        if isinstance(s, QuerySet):
            return s
        else:
            return QuerySet(s)

Response: Select several instances from the instance pool.


Instruction:
Can you create a Python 3 function that
navigates across a link with some rel_id and phrase that yields instances of some kind.

Response:
def navigate(self, inst, kind, rel_id, phrase=''):
        '''
        Navigate across a link with some *rel_id* and *phrase* that yields
        instances of some *kind*.
        '''
        key = (kind.upper(), rel_id, phrase)
        if key in self.links:
            link = self.links[key]
            return link.navigate(inst)

        link1, link2 = self._find_assoc_links(kind, rel_id, phrase)
        inst_set = xtuml.OrderedSet()
        for inst in link1.navigate(inst):
            inst_set |= link2.navigate(inst)

        return inst_set


Instruction:
Write a Python 3 script for
returning a sequence of all instances in the metamodel.

Response:
def instances(self):
        '''
        Obtain a sequence of all instances in the metamodel.
        '''
        for metaclass in self.metaclasses.values():
            for inst in metaclass.storage:
                yield inst


Instruction:
Can you generate the documentation for the following Python 3 function
def define_class(self, kind, attributes, doc=''):
        '''
        Define a new class in the metamodel, and return its metaclass.
        '''
        ukind = kind.upper()
        if ukind in self.metaclasses:
            raise MetaModelException('A class with the name %s is already defined' % kind)

        metaclass = MetaClass(kind, self)
        for name, ty in attributes:
            metaclass.append_attribute(name, ty)

        self.metaclasses[ukind] = metaclass

        return metaclass

Response: Define a new class in the metamodel and return its metaclass.


Instruction:
Here you have a function in Python 3, explain what it does
def find_metaclass(self, kind):
        '''
        Find a metaclass of some *kind* in the metamodel.
        '''
        ukind = kind.upper()
        if ukind in self.metaclasses:
            return self.metaclasses[ukind]
        else:
            raise UnknownClassException(kind)

Response: Find a metaclass of some kind in the metamodel.


Instruction:
Create a Python 3 function to
create and return a new instance of some kind.

Response:
def new(self, kind, *args, **kwargs):
        '''
        Create and return a new instance in the metamodel of some *kind*.

        Optionally, initial attribute values may be assigned to the new instance
        by passing them as positional or keyword arguments. Positional arguments
        are assigned in the order in which they appear in the metaclass.
        '''
        metaclass = self.find_metaclass(kind)
        return metaclass.new(*args, **kwargs)


Instruction:
Explain what the following Python 3 code does
def clone(self, instance):
        '''
        Create a shallow clone of an *instance*.

        **Note:** the clone and the original instance **does not** have to be
        part of the same metaclass.
        '''
        metaclass = get_metaclass(instance)
        metaclass = self.find_metaclass(metaclass.kind)
        return metaclass.clone(instance)

Response: Create a shallow clone of an instance.


Instruction:
Here you have a function in Python 3, explain what it does
def define_association(self, rel_id, source_kind, source_keys, source_many,
                           source_conditional, source_phrase, target_kind,
                           target_keys, target_many, target_conditional,
                           target_phrase):
        '''
        Define and return an association from one kind of class (the source
        kind) to some other kind of class (the target kind).
        '''
        if isinstance(rel_id, int):
            rel_id = 'R%d' % rel_id

        source_metaclass = self.find_metaclass(source_kind)
        target_metaclass = self.find_metaclass(target_kind)

        source_link = target_metaclass.add_link(source_metaclass, rel_id,
                                                many=source_many,
                                                phrase=target_phrase,
                                                conditional=source_conditional)

        target_link = source_metaclass.add_link(target_metaclass, rel_id,
                                                many=target_many,
                                                phrase=source_phrase,
                                                conditional=target_conditional)

        ass = Association(rel_id,
                          source_keys, source_link,
                          target_keys, target_link)

        source_link.key_map = dict(zip(source_keys, target_keys))
        target_link.key_map = dict(zip(target_keys, source_keys))

        self.associations.append(ass)

        return ass

Response: Define and return an association from one kind of class to another kind of class.


Instruction:
Create a Python 3 function for
defining a unique identifier for some kind of class based on its name and named attributes.

Response:
def define_unique_identifier(self, kind, name, *named_attributes):
        '''
        Define a unique identifier for some *kind* of class based on its
        *named attributes*.
        '''
        if not named_attributes:
            return

        if isinstance(name, int):
            name = 'I%d' % name

        metaclass = self.find_metaclass(kind)
        metaclass.indices[name] = tuple(named_attributes)
        metaclass.identifying_attributes |= set(named_attributes)


Instruction:
Create a Python 3 function to
query the metamodel for a set of instances of some kind.

Response:
def select_many(self, kind, *args):
        '''
        Query the metamodel for a set of instances of some *kind*. Query
        operators such as where_eq(), order_by() or filter functions may be
        passed as optional arguments.

        Usage example:

        >>> m = xtuml.load_metamodel('db.sql')
        >>> inst_set = m.select_many('My_Class', lambda sel: sel.number > 5)
        '''
        metaclass = self.find_metaclass(kind)
        return metaclass.select_many(*args)


Instruction:
Implement a function in Python 3 to
query the metamodel for a single instance of some kind.

Response:
def select_one(self, kind, *args):
        '''
        Query the metamodel for a single instance of some *kind*. Query
        operators such as where_eq(), order_by() or filter functions may be
        passed as optional arguments.

        Usage example:

        >>> m = xtuml.load_metamodel('db.sql')
        >>> inst = m.select_one('My_Class', lambda sel: sel.name == 'Test')
        '''
        metaclass = self.find_metaclass(kind)
        return metaclass.select_one(*args)


Instruction:
Here you have a function in Python 3, explain what it does
async def api_twitter(request):
    handle = request.match_info.get('handle', None)
    if handle is None:
        raise web.HTTPNotFound(body="Not found.")

    try:
        posts = await fetch_twitter(handle)
    except ApiError as e:
        raise web.HTTPInternalServerError(body=e.status)
    return str_json_response(posts)

Response: Get the twitter feed from a given handle.


Instruction:
Can you generate the documentation for the following Python 3 function
def send(socket, header, payload, topics=(), flags=0):
    msgs = []
    msgs.extend(topics)
    msgs.append(SEAM)
    msgs.extend(header)
    msgs.append(payload)
    return eintr_retry_zmq(socket.send_multipart, msgs, flags)

Response: Sends header payload and topics through a ZeroMQ socket.


Instruction:
Given the following Python 3 function, write the documentation
def recv(socket, flags=0, capture=(lambda msgs: None)):
    msgs = eintr_retry_zmq(socket.recv_multipart, flags)
    capture(msgs)
    return parse(msgs)

Response: Receives header payload and topics through a ZeroMQ socket.


Instruction:
How would you implement a function in Python 3 that
takes a string or list of strings and try to extract all the emails

Response:
def parse_emails(values):
    '''
    Take a string or list of strings and try to extract all the emails
    '''
    emails = []
    if isinstance(values, str):
        values = [values]
    # now we know we have a list of strings
    for value in values:
        matches = re_emails.findall(value)
        emails.extend([match[2] for match in matches])
    return emails


Instruction:
Write a Python 3 script for
marking a method as RPC.

Response:
def rpc(f=None, **kwargs):
    """Marks a method as RPC."""
    if f is not None:
        if isinstance(f, six.string_types):
            if 'name' in kwargs:
                raise ValueError('name option duplicated')
            kwargs['name'] = f
        else:
            return rpc(**kwargs)(f)
    return functools.partial(_rpc, **kwargs)


Instruction:
Explain what the following Python 3 code does
def rpc_spec_table(app):
    table = {}
    for attr, value in inspect.getmembers(app):
        rpc_spec = get_rpc_spec(value, default=None)
        if rpc_spec is None:
            continue
        table[rpc_spec.name] = (value, rpc_spec)
    return table

Response: Collects methods which are speced as RPC.


Instruction:
Here you have a function in Python 3, explain what it does
async def normalize_postcode_middleware(request, handler):
    postcode: Optional[str] = request.match_info.get('postcode', None)

    if postcode is None or postcode == "random":
        return await handler(request)
    elif not is_uk_postcode(postcode):
        raise web.HTTPNotFound(text="Invalid Postcode")

    postcode_processed = postcode.upper().replace(" ", "")
    if postcode_processed == postcode:
        return await handler(request)
    else:
        url_name = request.match_info.route.name
        url = request.app.router[url_name]
        params = dict(request.match_info)
        params['postcode'] = postcode_processed
        raise web.HTTPMovedPermanently(str(url.url_for(**params)))

Response: This is the main middleware that handles the normalizing of the postcode in the url.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def make_repr(obj, params=None, keywords=None, data=None, name=None,
              reprs=None):
    """Generates a string of object initialization code style.  It is useful
    for custom __repr__ methods::

       class Example(object):

           def __init__(self, param, keyword=None):
               self.param = param
               self.keyword = keyword

           def __repr__(self):
               return make_repr(self, ['param'], ['keyword'])

    See the representation of example object::

       >>> Example('hello', keyword='world')
       Example('hello', keyword='world')
    """
    opts = []
    if params is not None:
        opts.append(', '.join(
            _repr_attr(obj, attr, data, reprs) for attr in params))
    if keywords is not None:
        opts.append(', '.join(
            '%s=%s' % (attr, _repr_attr(obj, attr, data, reprs))
            for attr in keywords))
    if name is None:
        name = class_name(obj)
    return '%s(%s)' % (name, ', '.join(opts))

Response: Generates a string of object initialization code style.


Instruction:
Can you implement a function in Python 3 that
retries a function in a node with the given exception type.

Response:
def eintr_retry(exc_type, f, *args, **kwargs):
    """Calls a function.  If an error of the given exception type with
    interrupted system call (EINTR) occurs calls the function again.
    """
    while True:
        try:
            return f(*args, **kwargs)
        except exc_type as exc:
            if exc.errno != EINTR:
                raise
        else:
            break


Instruction:
Can you generate a brief explanation for the following Python 3 code
def eintr_retry_zmq(f, *args, **kwargs):
    return eintr_retry(zmq.ZMQError, f, *args, **kwargs)

Response: A wrapper for eintr_retry.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def next(self):
        '''
        Progress to the next identifier, and return the current one.
        '''
        val = self._current
        self._current = self.readfunc()
        return val

Response: Returns the next identifier.


Instruction:
Given the following Python 3 function, write the documentation
def enter(self, node):
        '''
        Tries to invoke a method matching the pattern *enter_<type name>*, where
        <type name> is the name of the type of the *node*.
        '''
        name = 'enter_' + node.__class__.__name__
        fn = getattr(self, name, self.default_enter)
        fn(node)

Response:
Tries to invoke a method matching the pattern enter_<type name >* where
        <type name > is the name of the node.


Instruction:
How would you explain what the following Python 3 function does
def leave(self, node):
        '''
        Tries to invoke a method matching the pattern *leave_<type name>*, where
        <type name> is the name of the type of the *node*.
        '''
        name = 'leave_' + node.__class__.__name__
        fn = getattr(self, name, self.default_leave)
        fn(node)

Response:
Tries to invoke a method matching the pattern leave_<type name >* where
        <type name > is the name of the node.


Instruction:
Write a Python 3 function that can
invoke the visitors before and after decending down the tree.

Response:
def accept(self, node, **kwargs):
        '''
        Invoke the visitors before and after decending down the tree.
        The walker will also try to invoke a method matching the pattern
        *accept_<type name>*, where <type name> is the name of the accepted
        *node*.
        '''
        if node is None:
            return

        for v in self.visitors:
            v.enter(node)

        name = 'accept_' + node.__class__.__name__
        fn = getattr(self, name, self.default_accept)
        r = fn(node, **kwargs)

        for v in self.visitors:
            v.leave(node)

        return r


Instruction:
Can you tell what is the following Python 3 function doing
def default_accept(self, node, **kwargs):
        '''
        The default accept behaviour is to decend into the iterable member
        *node.children* (if available).
        '''
        if not hasattr(node, 'children'):
            return

        for child in node.children:
            self.accept(child, **kwargs)

Response: Default accept behaviour is to decend into the iterable member


Instruction:
Write a Python 3 function for
trying to invoke a method matching the pattern render_<type name >* where <type name > is the name of the rendering node.

Response:
def render(self, node):
        '''
        Try to invoke a method matching the pattern *render_<type name>*, where
        <type name> is the name of the rendering *node*.
        '''
        name = 'render_' + type(node).__name__
        fn = getattr(self, name, self.default_render)
        return fn(node)


Instruction:
Can you generate the documentation for the following Python 3 function
def accept_S_SYS(self, inst):
        '''
        A System Model contains top-level packages
        '''
        for child in many(inst).EP_PKG[1401]():
            self.accept(child)

Response: Accept the S_SYS related objects.


Instruction:
Can you create a Python 3 function that
accepts the C component.

Response:
def accept_C_C(self, inst):
        '''
        A Component contains packageable elements
        '''
        for child in many(inst).PE_PE[8003]():
            self.accept(child)


Instruction:
Given the following Python 3 function, write the documentation
def accept_EP_PKG(self, inst):
        '''
        A Package contains packageable elements
        '''
        for child in many(inst).PE_PE[8000]():
            self.accept(child)

Response: Accept all the EEP - PKG elements in the given instance.


Instruction:
Can you tell what is the following Python 3 function doing
async def update_bikes(delta: Optional[timedelta] = None):

    async def update(delta: timedelta):
        logger.info("Fetching bike data.")
        if await should_update_bikes(delta):
            try:
                bike_data = await fetch_bikes()
            except ApiError:
                logger.debug(f"Failed to fetch bikes.")
            except CircuitBreakerError:
                logger.debug(f"Failed to fetch bikes (circuit breaker open).")
            else:
                # save only bikes that aren't in the db
                most_recent_bike = Bike.get_most_recent_bike()
                new_bikes = (
                    Bike.from_dict(bike) for index, bike in enumerate(bike_data)
                    if index > (most_recent_bike.id if most_recent_bike is not None else -1)
                )

                counter = 0
                with Bike._meta.database.atomic():
                    for bike in new_bikes:
                        bike.save()
                        counter += 1
                logger.info(f"Saved {counter} new entries.")
        else:
            logger.info("Bike data up to date.")

    if delta is None:
        await update(timedelta(days=1000))
    else:
        while True:
            await update(delta)
            await asyncio.sleep(delta.total_seconds())

Response: A background task that retrieves bike data and saves them to the database.


Instruction:
Create a Python 3 function to
check the most recently cached bike and returns true if it exists and if it is not yet updated.

Response:
async def should_update_bikes(delta: timedelta):
    """
    Checks the most recently cached bike and returns true if
    it either doesn't exist or
    :return: Whether the cache should be updated.

    todo what if there are no bikes added for a week? ... every request will be triggered.
    """
    bike = Bike.get_most_recent_bike()
    if bike is not None:
        return bike.cached_date < datetime.now() - delta
    else:
        return True


Instruction:
How would you code a function in Python 3 to
get stolen bikes from the database within certain radius of a given postcode.

Response:
async def get_bikes(postcode: PostCodeLike, kilometers=1) -> Optional[List[Bike]]:
    """
    Gets stolen bikes from the database within a
    certain radius (km) of a given postcode. Selects
    a square from the database and then filters out
    the corners of the square.
    :param postcode: The postcode to look up.
    :param kilometers: The radius (km) of the search.
    :return: The bikes in that radius or None if the postcode doesn't exist.
    """

    try:
        postcode_opt = await get_postcode(postcode)
    except CachingError as e:
        raise e

    if postcode_opt is None:
        return None
    else:
        postcode = postcode_opt

    # create point and distance
    center = Point(postcode.lat, postcode.long)
    distance = geodesic(kilometers=kilometers)

    # calculate edges of a square and retrieve
    lat_end = distance.destination(point=center, bearing=0).latitude
    lat_start = distance.destination(point=center, bearing=180).latitude
    long_start = distance.destination(point=center, bearing=270).longitude
    long_end = distance.destination(point=center, bearing=90).longitude

    bikes_in_area = Bike.select().where(
        lat_start <= Bike.latitude,
        Bike.latitude <= lat_end,
        long_start <= Bike.longitude,
        Bike.longitude <= long_end
    )

    # filter out items in square that aren't within the radius and return
    return [
        bike for bike in bikes_in_area
        if geodesic(Point(bike.latitude, bike.longitude), center).kilometers < kilometers
    ]


Instruction:
Given the following Python 3 function, write the documentation
async def get_postcode_random() -> Postcode:
    try:
        postcode = await fetch_postcode_random()
    except (ApiError, CircuitBreakerError):
        raise CachingError(f"Requested postcode is not cached, and can't be retrieved.")

    if postcode is not None:
        postcode.save()
    return postcode

Response: Gets a random postcode object.


Instruction:
Make a summary of the following Python 3 code
async def get_postcode(postcode_like: PostCodeLike) -> Optional[Postcode]:
    if isinstance(postcode_like, Postcode):
        return postcode_like

    postcode_like = postcode_like.replace(" ", "").upper()

    try:
        postcode = Postcode.get(Postcode.postcode == postcode_like)
    except DoesNotExist:
        try:
            postcode = await fetch_postcode_from_string(postcode_like)
        except (ApiError, CircuitBreakerError):
            raise CachingError(f"Requested postcode is not cached, and can't be retrieved.")
        if postcode is not None:
            postcode.save()

    return postcode

Response: Gets the postcode object for a given postcode string.


Instruction:
Can you generate the documentation for the following Python 3 function
async def get_neighbourhood(postcode_like: PostCodeLike) -> Optional[Neighbourhood]:
    try:
        postcode = await get_postcode(postcode_like)
    except CachingError as e:
        raise e
    else:
        if postcode is None:
            return None
        elif postcode.neighbourhood is not None:
            return postcode.neighbourhood

    try:
        data = await fetch_neighbourhood(postcode.lat, postcode.long)
    except ApiError as e:
        raise CachingError(f"Neighbourhood not in cache, and could not reach API: {e.status}")

    if data is not None:
        neighbourhood = Neighbourhood.from_dict(data)
        locations = [Location.from_dict(neighbourhood, postcode, location) for location in data["locations"]]
        links = [Link.from_dict(neighbourhood, link) for link in data["links"]]

        with Neighbourhood._meta.database.atomic():
            neighbourhood.save()
            postcode.neighbourhood = neighbourhood
            postcode.save()
            for location in locations:
                location.save()
            for link in links:
                link.save()
    else:
        neighbourhood = None
    return neighbourhood

Response: Gets a neighbourhood from the database.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_cdata(self, *args):
        '''
        all args-->_cffi_backend.buffer
        Returns-->cdata (if a SINGLE argument was provided)
                  LIST of cdata (if a args was a tuple or list)
        '''
        res = tuple([
            self.from_buffer(x) for x in args
        ])

        if len(res) == 0:
            return None
        elif len(res) == 1:
            return res[0]
        else:
            return res

Response: get_cdata - Returns a tuple of cdata


Instruction:
Can you write a function in Python 3 where it
gets a buffer from the list of arguments.

Response:
def get_buffer(self, *args):
        '''
        all args-->_cffi_backend.CDataOwn
        Must be a pointer or an array
        Returns-->buffer (if a SINGLE argument was provided)
                  LIST of buffer (if a args was a tuple or list)
        '''
        res = tuple([
            self.buffer(x) for x in args
        ])

        if len(res) == 0:
            return None
        elif len(res) == 1:
            return res[0]
        else:
            return res


Instruction:
How would you explain what the following Python 3 function does
def get_bytes(self, *args):
        '''
        all args-->_cffi_backend.CDataOwn
        Must be a pointer or an array
        Returns-->bytes (if a SINGLE argument was provided)
                  LIST of bytes (if a args was a tuple or list)
        '''
        res = tuple([
            bytes(self.buffer(x)) for x in args
        ])

        if len(res) == 0:
            return None
        elif len(res) == 1:
            return res[0]
        else:
            return res

Response: Get the bytes for the current cffi entry set.


Instruction:
How would you code a function in Python 3 to
get the average brightness of the image.

Response:
def get_brightness(self):
        """
        Return the average brightness of the image.
        """
        # Only download the image if it has changed
        if not self.connection.has_changed():
            return self.image_brightness

        image_path = self.connection.download_image()

        converted_image = Image.open(image_path).convert('L')
        statistics = ImageStat.Stat(converted_image)

        self.image_brightness = statistics.mean[0]
        return self.image_brightness


Instruction:
Given the following Python 3 function, write the documentation
def write_file (filename, contents):
    contents = "\n".join(contents)
    if sys.version_info >= (3,):
        contents = contents.encode("utf-8")
    f = open(filename, "wb")        # always write POSIX-style manifest
    f.write(contents)
    f.close()

Response: Write contents to a file.


Instruction:
Here you have a function in Python 3, explain what it does
def write_file(self, what, filename, data):
        log.info("writing %s to %s", what, filename)
        if sys.version_info >= (3,):
            data = data.encode("utf-8")
        if not self.dry_run:
            f = open(filename, 'wb')
            f.write(data)
            f.close()

Response: Write data to filename.


Instruction:
Write a Python 3 script for
writing the file list in self. filelist to the manifest file named by self. manifest.

Response:
def write_manifest (self):
        """Write the file list in 'self.filelist' (presumably as filled in
        by 'add_defaults()' and 'read_template()') to the manifest file
        named by 'self.manifest'.
        """
        # The manifest must be UTF-8 encodable. See #303.
        if sys.version_info >= (3,):
            files = []
            for file in self.filelist.files:
                try:
                    file.encode("utf-8")
                except UnicodeEncodeError:
                    log.warn("'%s' not UTF-8 encodable -- skipping" % file)
                else:
                    files.append(file)
            self.filelist.files = files

        files = self.filelist.files
        if os.sep!='/':
            files = [f.replace(os.sep,'/') for f in files]
        self.execute(write_file, (self.manifest, files),
                     "writing manifest file '%s'" % self.manifest)


Instruction:
Can you write a function in Python 3 where it
indicates whether or not to enter a case suite. usage: ``` py for case in switch(value): if case('A'): pass elif case(1, 3): pass # for mulit-match. else: pass # for default. ```

Response:
def match(self, *args):
        """
        Indicate whether or not to enter a case suite.

        usage:

        ``` py
        for case in switch(value):
            if case('A'):
                pass
            elif case(1, 3):
                pass # for mulit-match.
            else:
                pass # for default.
        ```
        """
        if not args:
            raise SyntaxError('cannot case empty pattern.')

        return self.match_args(self._value, args)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _find_match(self, position):
        # Decide what character to search for and what direction to search in.
        document = self._text_edit.document()
        start_char = document.characterAt(position)
        search_char = self._opening_map.get(start_char)
        if search_char:
            increment = 1
        else:
            search_char = self._closing_map.get(start_char)
            if search_char:
                increment = -1
            else:
                return -1

        # Search for the character.
        char = start_char
        depth = 0
        while position >= 0 and position < document.characterCount():
            if char == start_char:
                depth += 1
            elif char == search_char:
                depth -= 1
            if depth == 0:
                break
            position += increment
            char = document.characterAt(position)
        else:
            position = -1
        return position

Response: Given a valid position in the text document try to find the next bracket. Returns - 1 if unsuccessful.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _selection_for_character(self, position):
        selection = QtGui.QTextEdit.ExtraSelection()
        cursor = self._text_edit.textCursor()
        cursor.setPosition(position)
        cursor.movePosition(QtGui.QTextCursor.NextCharacter,
                            QtGui.QTextCursor.KeepAnchor)
        selection.cursor = cursor
        selection.format = self.format
        return selection

Response: Returns a selection object for the current character at the given position.


Instruction:
Explain what the following Python 3 code does
def _cursor_position_changed(self):
        # Clear out the old formatting.
        self._text_edit.setExtraSelections([])

        # Attempt to match a bracket for the new cursor position.
        cursor = self._text_edit.textCursor()
        if not cursor.hasSelection():
            position = cursor.position() - 1
            match_position = self._find_match(position)
            if match_position != -1:
                extra_selections = [ self._selection_for_character(pos)
                                     for pos in (position, match_position) ]
                self._text_edit.setExtraSelections(extra_selections)

Response: Updates the document formatting based on the new cursor position.


Instruction:
Explain what the following Python 3 code does
def _exc_info(self):
        e = self.exc_info()
        if sys.platform == 'cli':
            if isinstance(e[0], StringException):
                # IronPython throws these StringExceptions, but
                # traceback checks type(etype) == str. Make a real
                # string here.
                e = (str(e[0]), e[1], e[2])

        return e

Response: Bottleneck to fix up IronPython string exceptions


Instruction:
Given the following Python 3 function, write the documentation
def run(self, result):
        # proxy the result for myself
        log.debug("suite %s (%s) run called, tests: %s", id(self), self, self._tests)
        #import pdb
        #pdb.set_trace()
        if self.resultProxy:
            result, orig = self.resultProxy(result, self), result
        else:
            result, orig = result, result
        try:
            self.setUp()
        except KeyboardInterrupt:
            raise
        except:
            self.error_context = 'setup'
            result.addError(self, self._exc_info())
            return
        try:
            for test in self._tests:
                if result.shouldStop:
                    log.debug("stopping")
                    break
                # each nose.case.Test will create its own result proxy
                # so the cases need the original result, to avoid proxy
                # chains
                test(orig)
        finally:
            self.has_run = True
            try:
                self.tearDown()
            except KeyboardInterrupt:
                raise
            except:
                self.error_context = 'teardown'
                result.addError(self, self._exc_info())

Response: Run the tests in the suite inside of the suite fixtures.


Instruction:
Write a Python 3 script to
return the ancestry of the specified context.

Response:
def ancestry(self, context):
        """Return the ancestry of the context (that is, all of the
        packages and modules containing the context), in order of
        descent with the outermost ancestor last.
        This method is a generator.
        """
        log.debug("get ancestry %s", context)
        if context is None:
            return
        # Methods include reference to module they are defined in, we
        # don't want that, instead want the module the class is in now
        # (classes are re-ancestored elsewhere).
        if hasattr(context, 'im_class'):
            context = context.im_class
        elif hasattr(context, '__self__'):
            context = context.__self__.__class__
        if hasattr(context, '__module__'):
            ancestors = context.__module__.split('.')
        elif hasattr(context, '__name__'):
            ancestors = context.__name__.split('.')[:-1]
        else:
            raise TypeError("%s has no ancestors?" % context)
        while ancestors:
            log.debug(" %s ancestors %s", context, ancestors)
            yield resolve_name('.'.join(ancestors))
            ancestors.pop()


Instruction:
Can you generate the documentation for the following Python 3 function
def create_inputhook_qt4(mgr, app=None):

    if app is None:
        app = QtCore.QCoreApplication.instance()
        if app is None:
            app = QtGui.QApplication([" "])

    # Re-use previously created inputhook if any
    ip = InteractiveShell.instance()
    if hasattr(ip, '_inputhook_qt4'):
        return app, ip._inputhook_qt4

    # Otherwise create the inputhook_qt4/preprompthook_qt4 pair of
    # hooks (they both share the got_kbdint flag)

    got_kbdint = [False]

    def inputhook_qt4():
        """PyOS_InputHook python hook for Qt4.

        Process pending Qt events and if there's no pending keyboard
        input, spend a short slice of time (50ms) running the Qt event
        loop.

        As a Python ctypes callback can't raise an exception, we catch
        the KeyboardInterrupt and temporarily deactivate the hook,
        which will let a *second* CTRL+C be processed normally and go
        back to a clean prompt line.
        """
        try:
            allow_CTRL_C()
            app = QtCore.QCoreApplication.instance()
            if not app: # shouldn't happen, but safer if it happens anyway...
                return 0
            app.processEvents(QtCore.QEventLoop.AllEvents, 300)
            if not stdin_ready():
                timer = QtCore.QTimer()
                timer.timeout.connect(app.quit)
                while not stdin_ready():
                    timer.start(50)
                    app.exec_()
                    timer.stop()
        except KeyboardInterrupt:
            ignore_CTRL_C()
            got_kbdint[0] = True
            print("\nKeyboardInterrupt - Ctrl-C again for new prompt")
            mgr.clear_inputhook()
        except: # NO exceptions are allowed to escape from a ctypes callback
            ignore_CTRL_C()
            from traceback import print_exc
            print_exc()
            print("Got exception from inputhook_qt4, unregistering.")
            mgr.clear_inputhook()
        finally:
            allow_CTRL_C()
        return 0

    def preprompthook_qt4(ishell):
        """'pre_prompt_hook' used to restore the Qt4 input hook

        (in case the latter was temporarily deactivated after a
        CTRL+C)
        """
        if got_kbdint[0]:
            mgr.set_inputhook(inputhook_qt4)
        got_kbdint[0] = False

    ip._inputhook_qt4 = inputhook_qt4
    ip.set_hook('pre_prompt_hook', preprompthook_qt4)

    return app, inputhook_qt4

Response: Create an inputhook for running the Qt4 application event loop.


Instruction:
Given the following Python 3 function, write the documentation
def get(cls, name=__name__):
        if not isinstance(name, str):
            raise TypeError('A mapper name must be a string')

        if name not in cls.__instances:
            cls.__instances[name] = cls()
            cls.__instances[name]._name = name

        return cls.__instances[name]

Response: Returns a new instance of the given name.


Instruction:
Can you generate the documentation for the following Python 3 function
def url(self, pattern, method=None, type_cast=None):
        if not type_cast:
            type_cast = {}

        def decorator(function):
            self.add(pattern, function, method, type_cast)
            return function

        return decorator

Response: Decorator for registering a path pattern.


Instruction:
Make a summary of the following Python 3 code
def s_url(self, path, method=None, type_cast=None):
        if not type_cast:
            type_cast = {}

        def decorator(function):
            self.s_add(path, function, method, type_cast)
            return function

        return decorator

Response: Decorator for registering a simple url.


Instruction:
Here you have a function in Python 3, explain what it does
def add(self, pattern, function, method=None, type_cast=None):
        if not type_cast:
            type_cast = {}

        with self._lock:
            self._data_store.append({
                'pattern': pattern,
                'function': function,
                'method': method,
                'type_cast': type_cast,
            })

Response: Function for registering a path pattern.


Instruction:
Create a Python 3 function for
functioning for registering a simple path.

Response:
def s_add(self, path, function, method=None, type_cast=None):
        """Function for registering a simple path.

        Args:
            path (str): Path to be matched.
            function (function): Function to associate with this path.
            method (str, optional): Usually used to define one of GET, POST,
                PUT, DELETE. You may use whatever fits your situation though.
                Defaults to None.
            type_cast (dict, optional): Mapping between the param name and
                one of `int`, `float` or `bool`. The value reflected by the
                provided param name will than be casted to the given type.
                Defaults to None.
        """
        with self._lock:
            try:
                path = '^/{}'.format(path.lstrip('/'))
                path = '{}/$'.format(path.rstrip('/'))
                path = path.replace('<', '(?P<')
                path = path.replace('>', '>[^/]*)')

                self.add(path, function, method, type_cast)
            except Exception:
                pass


Instruction:
Write a Python 3 script to
call the first function matching the urls pattern and method.

Response:
def call(self, url, method=None, args=None):
        """Calls the first function matching the urls pattern and method.

        Args:
            url (str): Url for which to call a matching function.
            method (str, optional): The method used while registering a
                function.
                Defaults to None
            args (dict, optional): Additional args to be passed to the
                matching function.

        Returns:
            The functions return value or `None` if no function was called.
        """
        if not args:
            args = {}

        if sys.version_info.major == 3:
            data = urllib.parse.urlparse(url)
            path = data.path.rstrip('/') + '/'
            _args = dict(urllib.parse.parse_qs(data.query,
                                               keep_blank_values=True))
        elif sys.version_info.major == 2:
            data = urlparse.urlparse(url)
            path = data.path.rstrip('/') + '/'
            _args = dict(urlparse.parse_qs(data.query,
                                           keep_blank_values=True))

        for elem in self._data_store:
            pattern = elem['pattern']
            function = elem['function']
            _method = elem['method']
            type_cast = elem['type_cast']

            result = re.match(pattern, path)

            # Found matching method
            if result and _method == method:
                _args = dict(_args, **result.groupdict())

                # Unpack value lists (due to urllib.parse.parse_qs) in case
                # theres only one value available
                for key, val in _args.items():
                    if isinstance(_args[key], list) and len(_args[key]) == 1:
                        _args[key] = _args[key][0]

                # Apply typ-casting if necessary
                for key, val in type_cast.items():

                    # Not within available _args, no type-cast required
                    if key not in _args:
                        continue

                    # Is None or empty, no type-cast required
                    if not _args[key]:
                        continue

                    # Try and cast the values
                    if isinstance(_args[key], list):
                        for i, _val in enumerate(_args[key]):
                            _args[key][i] = self._cast(_val, val)
                    else:
                        _args[key] = self._cast(_args[key], val)

                requiered_args = self._get_function_args(function)
                for key, val in args.items():
                    if key in requiered_args:
                        _args[key] = val

                return function(**_args)

        return None


Instruction:
Here you have a function in Python 3, explain what it does
def execute(self, source=None, hidden=False, interactive=False):
        if not hidden:
            history = self.input_buffer if source is None else source

        executed = super(HistoryConsoleWidget, self).execute(
            source, hidden, interactive)

        if executed and not hidden:
            # Save the command unless it was an empty string or was identical
            # to the previous command.
            history = history.rstrip()
            if history and (not self._history or self._history[-1] != history):
                self._history.append(history)

            # Emulate readline: reset all history edits.
            self._history_edits = {}

            # Move the history index to the most recent item.
            self._history_index = len(self._history)

        return executed

Response: Reimplemented to the store history.


Instruction:
How would you implement a function in Python 3 that
calls when the up key is pressed. Returns True if the event should be handled otherwise False.

Response:
def _up_pressed(self, shift_modifier):
        """ Called when the up key is pressed. Returns whether to continue
            processing the event.
        """
        prompt_cursor = self._get_prompt_cursor()
        if self._get_cursor().blockNumber() == prompt_cursor.blockNumber():
            # Bail out if we're locked.
            if self._history_locked() and not shift_modifier:
                return False

            # Set a search prefix based on the cursor position.
            col = self._get_input_buffer_cursor_column()
            input_buffer = self.input_buffer
            if self._history_index == len(self._history) or \
                    (self._history_prefix and col != len(self._history_prefix)):
                self._history_index = len(self._history)
                self._history_prefix = input_buffer[:col]

            # Perform the search.
            self.history_previous(self._history_prefix,
                                    as_prefix=not shift_modifier)

            # Go to the first line of the prompt for seemless history scrolling.
            # Emulate readline: keep the cursor position fixed for a prefix
            # search.
            cursor = self._get_prompt_cursor()
            if self._history_prefix:
                cursor.movePosition(QtGui.QTextCursor.Right,
                                    n=len(self._history_prefix))
            else:
                cursor.movePosition(QtGui.QTextCursor.EndOfLine)
            self._set_cursor(cursor)

            return False

        return True


Instruction:
How would you explain what the following Python 3 function does
def _down_pressed(self, shift_modifier):
        end_cursor = self._get_end_cursor()
        if self._get_cursor().blockNumber() == end_cursor.blockNumber():
            # Bail out if we're locked.
            if self._history_locked() and not shift_modifier:
                return False

            # Perform the search.
            replaced = self.history_next(self._history_prefix,
                                            as_prefix=not shift_modifier)

            # Emulate readline: keep the cursor position fixed for a prefix
            # search. (We don't need to move the cursor to the end of the buffer
            # in the other case because this happens automatically when the
            # input buffer is set.)
            if self._history_prefix and replaced:
                cursor = self._get_prompt_cursor()
                cursor.movePosition(QtGui.QTextCursor.Right,
                                    n=len(self._history_prefix))
                self._set_cursor(cursor)

            return False

        return True

Response: Called when the down key is pressed. Returns whether to continue processing the event.


Instruction:
Implement a Python 3 function for
searching for a previous history item. If as_prefix is True then set the input buffer to the previous history item. Otherwise return False.

Response:
def history_previous(self, substring='', as_prefix=True):
        """ If possible, set the input buffer to a previous history item.

        Parameters:
        -----------
        substring : str, optional
            If specified, search for an item with this substring.
        as_prefix : bool, optional
            If True, the substring must match at the beginning (default).

        Returns:
        --------
        Whether the input buffer was changed.
        """
        index = self._history_index
        replace = False
        while index > 0:
            index -= 1
            history = self._get_edited_history(index)
            if (as_prefix and history.startswith(substring)) \
                or (not as_prefix and substring in history):
                replace = True
                break

        if replace:
            self._store_edits()
            self._history_index = index
            self.input_buffer = history

        return replace


Instruction:
Make a summary of the following Python 3 code
def history_next(self, substring='', as_prefix=True):
        index = self._history_index
        replace = False
        while self._history_index < len(self._history):
            index += 1
            history = self._get_edited_history(index)
            if (as_prefix and history.startswith(substring)) \
                or (not as_prefix and substring in history):
                replace = True
                break

        if replace:
            self._store_edits()
            self._history_index = index
            self.input_buffer = history

        return replace

Response: Search for a subsequent entry in the history.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _handle_execute_reply(self, msg):
        msg_id = msg['parent_header']['msg_id']
        info = self._request_info['execute'].pop(msg_id,None)
        if info and info.kind == 'save_magic' and not self._hidden:
            content = msg['content']
            status = content['status']
            if status == 'ok':
                self._max_session_history=(int(content['user_expressions']['hlen']))

Response: Handles replies for code execution


Instruction:
Make a summary of the following Python 3 code
def _history_locked(self):
        return (self.history_lock and
                (self._get_edited_history(self._history_index) !=
                 self.input_buffer) and
                (self._get_prompt_cursor().blockNumber() !=
                 self._get_end_cursor().blockNumber()))

Response: Returns whether history is locked.


Instruction:
How would you explain what the following Python 3 function does
def _get_edited_history(self, index):
        if index in self._history_edits:
            return self._history_edits[index]
        elif index == len(self._history):
            return unicode()
        return self._history[index]

Response: Retrieves a history item possibly with temporary edits.


Instruction:
Can you tell what is the following Python 3 function doing
def _set_history(self, history):
        self._history = list(history)
        self._history_edits = {}
        self._history_index = len(self._history)

Response: Replace the current history with a sequence of history items.


Instruction:
Can you tell what is the following Python 3 function doing
def _store_edits(self):
        current = self.input_buffer
        if self._history_index == len(self._history) or \
                self._history[self._history_index] != current:
            self._history_edits[self._history_index] = current

Response: Store the edits in the history.


Instruction:
Explain what the following Python 3 code does
def t_NAME(t):
    r'[A-Za-z_][A-Za-z0-9_]*'
    # to simplify lexing, we match identifiers and keywords as a single thing
    # if it's a keyword, we change the type to the name of that keyword
    if t.value.upper() in reserved:
        t.type = t.value.upper()
        t.value = t.value.upper()
    return t

Response: A simple name match for identifiers and keywords


Instruction:
Can you generate the documentation for the following Python 3 function
def t_STRING(t):
    r"'([^'\\]+|\\'|\\\\)*'"
    t.value = t.value.replace(r'\\', chr(92)).replace(r"\'", r"'")[1:-1]
    return t

Response: r "'([^'\\]+|\\'|\\\\ ) *'"


Instruction:
Write a Python 3 function that can
parse postpositions parameter into a single object

Response:
def p_postpositions(p):
    '''
    postpositions : LIMIT NUMBER postpositions
                  | ORDER BY colspec postpositions
                  | empty
    '''
    if len(p) > 2:
        if p[1] == "LIMIT":
            postposition = {
                "limit": p[2]
            }
            rest = p[3] if p[3] else {}
        elif p[1:3] == ["ORDER", "BY"]:
            postposition = {
                "order by": p[3]
            }
            rest = p[4] if p[4] else {}
        else:
            breakpoint()
        p[0] = {**postposition, **rest}
    else:
        p[0] = {}


Instruction:
Can you tell what is the following Python 3 function doing
def p_expression(p):
    '''
    expression : value
               | expression AND expression
               | expression OR expression
               | expression EQUALS expression
               | NOT expression
               | LPAREN expression RPAREN
    '''
    if len(p) < 3:
        p[0] = p[1]
    elif len(p) == 3:  # not
        p[0] = {
            "op": "not",
            "args": [p[2]],
        }
    elif p[1] == "(":
        p[0] = p[2]
    else:
        p[0] = {
            "op": p[2].lower(),
            "args": [p[1], p[3]],
        }

Response: A function to parse the expression part of a sequence.


Instruction:
Make a summary of the following Python 3 code
def upgrade_dir(srcdir, tgtdir):

    def pr(s):
        print s
    junk = ['.svn','ipythonrc*','*.pyc', '*.pyo', '*~', '.hg']

    def ignorable(p):
        for pat in junk:
            if p.startswith(pat) or p.fnmatch(pat):
                return True
        return False

    modded = []
    files = [path(srcdir).relpathto(p) for p in path(srcdir).walkfiles()]
    #print files
    rep = tgtdir / '.upgrade_report'
    try:
        rpt = pickle.load(rep.open())
    except:
        rpt = {}

    for f in files:
        if ignorable(f):
            continue
        src = srcdir / f
        tgt = tgtdir / f
        if not tgt.isfile():
            pr("Creating %s" % str(tgt))

            tgt.write_text(src.text())
            rpt[str(tgt)] = hashlib.md5(tgt.text()).hexdigest()
        else:
            cont = tgt.text()
            sum = rpt.get(str(tgt), None)
            #print sum
            if sum and hashlib.md5(cont).hexdigest() == sum:
                pr("%s: Unedited, installing new version" % tgt)
                tgt.write_text(src.text())
                rpt[str(tgt)] = hashlib.md5(tgt.text()).hexdigest()
            else:
                pr(' == Modified, skipping %s, diffs below == ' % tgt)
                #rpt[str(tgt)] = hashlib.md5(tgt.bytes()).hexdigest()
                real = showdiff(tgt,src)
                pr('') # empty line
                if not real:
                    pr("(Ok, it was identical, only upgrading checksum)")
                    rpt[str(tgt)] = hashlib.md5(tgt.text()).hexdigest()
                else:
                    modded.append(tgt)

        #print rpt
    pickle.dump(rpt, rep.open('w'))
    if modded:
        print "\n\nDelete the following files manually (and rerun %upgrade)\nif you need a full upgrade:"
        for m in modded:
            print m

Response: Copy all files in srcdir to tgtdir and create a new version of the target.