Instruction:
Explain what the following Python 3 code does
def run_init(args):
    root = args.root
    if root is None:
        root = '.'
    root = os.path.abspath(root)

    project_data = _get_package_data()
    project_name = project_data['project_name']

    directories = [os.path.join(root, 'bin'),
                   os.path.join(root, 'docs'),
                   os.path.join(root, 'tests'),
                   os.path.join(root, project_name),
                   ]
    for dir_path in directories:
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

    script_paths = [os.path.join(root, 'README.md'),
                    os.path.join(root, 'tests/__init__.py'),
                    ]
    for script_path in script_paths:
        if not os.path.exists(script_path):
            os.mknod(script_path)

    copy_samples = [(resource_filename('mpu', 'package/templates/tox.ini.txt'),
                     os.path.join(root, 'tox.ini')),
                    (resource_filename('mpu',
                                       'package/templates/setup.cfg.txt'),
                     os.path.join(root, 'setup.cfg')),
                    (resource_filename('mpu',
                                       'package/templates/setup.py.txt'),
                     os.path.join(root, 'setup.py')),
                    (resource_filename('mpu',
                                       'package/templates/_version.py.txt'),
                     os.path.join(root, project_name + '/_version.py')),
                    (resource_filename('mpu',
                                       'package/templates/coveragerc.txt'),
                     os.path.join(root, '.coveragerc')),
                    (resource_filename('mpu', 'package/templates/init.py.txt'),
                     os.path.join(root, project_name + '/__init__.py')),
                    ]
    translate = {'[[project_name]]': project_data['project_name'],
                 '[[license]]': project_data['license'],
                 '[[author]]': project_data['author'],
                 '[[email]]': project_data['email'],
                 }
    for source, destination in copy_samples:
        if not os.path.exists(destination):
            copyfile(source, destination)
            _adjust_template(destination, translate)

Response:
Run project initialization.

    This will ask the user for input.

    Parameters
    ----------
    args : argparse named arguments


Instruction:
Implement a Python 3 function for
replacing multiple things at once in a text.

Response:
def _multiple_replace(text, search_replace_dict):
    """
    Replace multiple things at once in a text.

    Parameters
    ----------
    text : str
    search_replace_dict : dict

    Returns
    -------
    replaced_text : str

    Examples
    --------
    >>> d = {'a': 'b', 'b': 'c', 'c': 'd', 'd': 'e'}
    >>> _multiple_replace('abcdefghijklm', d)
    'bcdeefghijklm'
    """
    # Create a regular expression from all of the dictionary keys
    regex = re.compile("|".join(map(re.escape, search_replace_dict.keys())))

    # For each match, look up the corresponding value in the dictionary
    return regex.sub(lambda match: search_replace_dict[match.group(0)], text)


Instruction:
How would you explain what the following Python 3 function does
def _adjust_template(filepath, translate):
    with open(filepath, 'r') as file:
        filedata = file.read()

    filedata = _multiple_replace(filedata, translate)

    with open(filepath, 'w') as file:
        file.write(filedata)

Response: Search and replace contents of a filepath.


Instruction:
Can you tell what is the following Python 3 function doing
def get_parser(parser=None):
    from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
    if parser is None:
        parser = ArgumentParser(description=__doc__,
                                formatter_class=ArgumentDefaultsHelpFormatter)
    subparsers = parser.add_subparsers()
    pkg_init_parser = subparsers.add_parser('init')
    pkg_init_parser.add_argument("root",
                                 nargs='?',
                                 help="project root - should be empty")
    pkg_init_parser.set_defaults(func=run_init)
    return parser

Response: Get parser for mpu.


Instruction:
Here you have a function in Python 3, explain what it does
def parallel_for(loop_function, parameters, nb_threads=100):
    import multiprocessing.pool
    from contextlib import closing
    with closing(multiprocessing.pool.ThreadPool(nb_threads)) as pool:
        return pool.map(loop_function, parameters)

Response: Execute a function in parallel.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def clip(number, lowest=None, highest=None):
    if lowest is not None:
        number = max(number, lowest)
    if highest is not None:
        number = min(number, highest)
    return number

Response: Clip a number to a given lowest / highest value.


Instruction:
How would you explain what the following Python 3 function does
def consistent_shuffle(*lists):
    perm = list(range(len(lists[0])))
    random.shuffle(perm)
    lists = tuple([sublist[index] for index in perm]
                  for sublist in lists)
    return lists

Response:
Shuffle lists consistently.

    Parameters
    ----------
    *lists
        Variable length number of lists

    Returns
    -------
    shuffled_lists : tuple of lists
        All of the lists are shuffled consistently

    Examples
    --------
    >>> import mpu, random; random.seed(8)
    >>> mpu.consistent_shuffle([1,2,3], ['a', 'b', 'c'], ['A', 'B', 'C'])
    ([3, 2, 1], ['c', 'b', 'a'], ['C', 'B', 'A'])


Instruction:
Create a Python 3 function for
calculating the Haversine distance between two sets of points.

Response:
def haversine_distance(origin, destination):
    """
    Calculate the Haversine distance.

    Parameters
    ----------
    origin : tuple of float
        (lat, long)
    destination : tuple of float
        (lat, long)

    Returns
    -------
    distance_in_km : float

    Examples
    --------
    >>> munich = (48.1372, 11.5756)
    >>> berlin = (52.5186, 13.4083)
    >>> round(haversine_distance(munich, berlin), 1)
    504.2

    >>> new_york_city = (40.712777777778, -74.005833333333)  # NYC
    >>> round(haversine_distance(berlin, new_york_city), 1)
    6385.3
    """
    lat1, lon1 = origin
    lat2, lon2 = destination
    if not (-90.0 <= lat1 <= 90):
        raise ValueError('lat1={:2.2f}, but must be in [-90,+90]'.format(lat1))
    if not (-90.0 <= lat2 <= 90):
        raise ValueError('lat2={:2.2f}, but must be in [-90,+90]'.format(lat2))
    if not (-180.0 <= lon1 <= 180):
        raise ValueError('lon1={:2.2f}, but must be in [-180,+180]'
                         .format(lat1))
    if not (-180.0 <= lon2 <= 180):
        raise ValueError('lon1={:2.2f}, but must be in [-180,+180]'
                         .format(lat1))
    radius = 6371  # km

    dlat = math_stl.radians(lat2 - lat1)
    dlon = math_stl.radians(lon2 - lon1)
    a = (math_stl.sin(dlat / 2) * math_stl.sin(dlat / 2) +
         math_stl.cos(math_stl.radians(lat1)) *
         math_stl.cos(math_stl.radians(lat2)) *
         math_stl.sin(dlon / 2) * math_stl.sin(dlon / 2))
    c = 2 * math_stl.atan2(math_stl.sqrt(a), math_stl.sqrt(1 - a))
    d = radius * c

    return d


Instruction:
How would you implement a function in Python 3 that
raises an exception if value is not in an interval.

Response:
def is_in_intervall(value, min_value, max_value, name='variable'):
    """
    Raise an exception if value is not in an interval.

    Parameters
    ----------
    value : orderable
    min_value : orderable
    max_value : orderable
    name : str
        Name of the variable to print in exception.
    """
    if not (min_value <= value <= max_value):
        raise ValueError('{}={} is not in [{}, {}]'
                         .format(name, value, min_value, max_value))


Instruction:
Explain what the following Python 3 code does
def exception_logging(exctype, value, tb):
    write_val = {'exception_type': str(exctype),
                 'message': str(traceback.format_tb(tb, 10))}
    logging.exception(str(write_val))

Response: Log exception by using the root logger.


Instruction:
Given the following Python 3 function, write the documentation
def distance(self, there):
        return haversine_distance((self.latitude, self.longitude),
                                  (there.latitude, there.longitude))

Response: Calculate the distance from this location to there.


Instruction:
Can you generate the documentation for the following Python 3 function
def main():
    parser = get_parser()
    args = parser.parse_args()
    if hasattr(args, 'func') and args.func:
        args.func(args)
    else:
        parser.print_help()

Response: Command line interface of mpu.


Instruction:
Write a Python 3 script to
get parser for mpu.

Response:
def get_parser():
    """Get parser for mpu."""
    from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
    parser = ArgumentParser(description=__doc__,
                            formatter_class=ArgumentDefaultsHelpFormatter)
    parser.add_argument('--version',
                        action='version',
                        version='mpu {}'.format(mpu.__version__))
    subparsers = parser.add_subparsers(help='Python package commands')
    package_parser = subparsers.add_parser('package')
    mpu.package.cli.get_parser(package_parser)
    return parser


Instruction:
Implement a Python 3 function for
reading a single object from a file.

Response:
def read(filepath, **kwargs):
    """
    Read a file.

    Supported formats:

    * CSV
    * JSON, JSONL
    * pickle

    Parameters
    ----------
    filepath : str
        Path to the file that should be read. This methods action depends
        mainly on the file extension.
    kwargs : dict
        Any keywords for the specific file format. For CSV, this is
        'delimiter', 'quotechar', 'skiprows', 'format'

    Returns
    -------
    data : str or bytes
    """
    if filepath.lower().endswith('.csv'):
        return _read_csv(filepath, kwargs)
    elif filepath.lower().endswith('.json'):
        with open(filepath) as data_file:
            data = json.load(data_file, **kwargs)
        return data
    elif filepath.lower().endswith('.jsonl'):
        return _read_jsonl(filepath, kwargs)
    elif filepath.lower().endswith('.pickle'):
        with open(filepath, 'rb') as handle:
            data = pickle.load(handle)
        return data
    elif (filepath.lower().endswith('.yml') or
          filepath.lower().endswith('.yaml')):
        raise NotImplementedError('YAML is not supported, because you need '
                                  'PyYAML in Python3. '
                                  'See '
                                  'https://stackoverflow.com/a/42054860/562769'
                                  ' as a guide how to use it.')
    elif (filepath.lower().endswith('.h5') or
          filepath.lower().endswith('.hdf5')):
        raise NotImplementedError('HDF5 is not supported. See '
                                  'https://stackoverflow.com/a/41586571/562769'
                                  ' as a guide how to use it.')
    else:
        raise NotImplementedError('File \'{}\' is not known.'.format(filepath))


Instruction:
Can you write a function in Python 3 where it
reads a list of items from a CSV file.

Response:
def _read_csv(filepath, kwargs):
    """See documentation of mpu.io.read."""
    if 'delimiter' not in kwargs:
        kwargs['delimiter'] = ','
    if 'quotechar' not in kwargs:
        kwargs['quotechar'] = '"'
    if 'skiprows' not in kwargs:
        kwargs['skiprows'] = []
    if isinstance(kwargs['skiprows'], int):
        kwargs['skiprows'] = [i for i in range(kwargs['skiprows'])]
    if 'format' in kwargs:
        format_ = kwargs['format']
        kwargs.pop('format', None)
    else:
        format_ = 'default'
    skiprows = kwargs['skiprows']
    kwargs.pop('skiprows', None)

    kwargs_open = {'newline': ''}
    mode = 'r'
    if sys.version_info < (3, 0):
        kwargs_open.pop('newline', None)
        mode = 'rb'
    with open(filepath, mode, **kwargs_open) as fp:
        if format_ == 'default':
            reader = csv.reader(fp, **kwargs)
            data = EList([row for row in reader])
            data = data.remove_indices(skiprows)
        elif format_ == 'dicts':
            reader_list = csv.DictReader(fp, **kwargs)
            data = [row for row in reader_list]
        else:
            raise NotImplementedError('Format \'{}\' unknown'
                                      .format(format_))
    return data


Instruction:
Make a summary of the following Python 3 code
def _read_jsonl(filepath, kwargs):
    with open(filepath) as data_file:
        data = [json.loads(line, **kwargs)
                for line in data_file
                if len(line) > 0]
    return data

Response: Read a json file and return a list of dicts.


Instruction:
Can you write a function in Python 3 where it
writes a file. Supported formats: * CSV * JSON, JSONL * pickle Parameters ---------- filepath : str Path to the file that should be read. This methods action depends mainly on the file extension. data : dict or list Content that should be written kwargs : dict Any keywords for the specific file format. Returns ------- data : str or bytes

Response:
def write(filepath, data, **kwargs):
    """
    Write a file.

    Supported formats:

    * CSV
    * JSON, JSONL
    * pickle

    Parameters
    ----------
    filepath : str
        Path to the file that should be read. This methods action depends
        mainly on the file extension.
    data : dict or list
        Content that should be written
    kwargs : dict
        Any keywords for the specific file format.

    Returns
    -------
    data : str or bytes
    """
    if filepath.lower().endswith('.csv'):
        return _write_csv(filepath, data, kwargs)
    elif filepath.lower().endswith('.json'):
        return _write_json(filepath, data, kwargs)
    elif filepath.lower().endswith('.jsonl'):
        return _write_jsonl(filepath, data, kwargs)
    elif filepath.lower().endswith('.pickle'):
        return _write_pickle(filepath, data, kwargs)
    elif (filepath.lower().endswith('.yml') or
          filepath.lower().endswith('.yaml')):
        raise NotImplementedError('YAML is not supported, because you need '
                                  'PyYAML in Python3. '
                                  'See '
                                  'https://stackoverflow.com/a/42054860/562769'
                                  ' as a guide how to use it.')
    elif (filepath.lower().endswith('.h5') or
          filepath.lower().endswith('.hdf5')):
        raise NotImplementedError('YAML is not supported. See '
                                  'https://stackoverflow.com/a/41586571/562769'
                                  ' as a guide how to use it.')
    else:
        raise NotImplementedError('File \'{}\' is not known.'.format(filepath))


Instruction:
Can you write a function in Python 3 where it
writes data to a CSV file.

Response:
def _write_csv(filepath, data, kwargs):
    """See documentation of mpu.io.write."""
    kwargs_open = {'newline': ''}
    mode = 'w'
    if sys.version_info < (3, 0):
        kwargs_open.pop('newline', None)
        mode = 'wb'
    with open(filepath, mode, **kwargs_open) as fp:
        if 'delimiter' not in kwargs:
            kwargs['delimiter'] = ','
        if 'quotechar' not in kwargs:
            kwargs['quotechar'] = '"'
        with open(filepath, 'w') as fp:
            writer = csv.writer(fp, **kwargs)
            writer.writerows(data)
    return data


Instruction:
Can you tell what is the following Python 3 function doing
def _write_json(filepath, data, kwargs):
    with io_stl.open(filepath, 'w', encoding='utf8') as outfile:
        if 'indent' not in kwargs:
            kwargs['indent'] = 4
        if 'sort_keys' not in kwargs:
            kwargs['sort_keys'] = True
        if 'separators' not in kwargs:
            kwargs['separators'] = (',', ': ')
        if 'ensure_ascii' not in kwargs:
            kwargs['ensure_ascii'] = False
        str_ = json.dumps(data, **kwargs)
        outfile.write(to_unicode(str_))
    return data

Response: Write a JSON object to a file.


Instruction:
How would you explain what the following Python 3 function does
def _write_jsonl(filepath, data, kwargs):
    with io_stl.open(filepath, 'w', encoding='utf8') as outfile:
        kwargs['indent'] = None  # JSON has to be on one line!
        if 'sort_keys' not in kwargs:
            kwargs['sort_keys'] = True
        if 'separators' not in kwargs:
            kwargs['separators'] = (',', ': ')
        if 'ensure_ascii' not in kwargs:
            kwargs['ensure_ascii'] = False
        for line in data:
            str_ = json.dumps(line, **kwargs)
            outfile.write(to_unicode(str_))
            outfile.write(u'\n')
    return data

Response: Write a JSON file.


Instruction:
Can you write a function in Python 3 where it
writes data to a pickle file.

Response:
def _write_pickle(filepath, data, kwargs):
    """See documentation of mpu.io.write."""
    if 'protocol' not in kwargs:
        kwargs['protocol'] = pickle.HIGHEST_PROTOCOL
    with open(filepath, 'wb') as handle:
        pickle.dump(data, handle, **kwargs)
    return data


Instruction:
Implement a Python 3 function for
reading the content of a URL.

Response:
def urlread(url, encoding='utf8'):
    """
    Read the content of an URL.

    Parameters
    ----------
    url : str

    Returns
    -------
    content : str
    """
    try:
        from urllib.request import urlopen
    except ImportError:
        from urllib2 import urlopen
    response = urlopen(url)
    content = response.read()
    content = content.decode(encoding)
    return content


Instruction:
Can you write a function in Python 3 where it
downloads a file from a URL and store it in a local file system.

Response:
def download(source, sink=None):
    """
    Download a file.

    Parameters
    ----------
    source : str
        Where the file comes from. Some URL.
    sink : str or None (default: same filename in current directory)
        Where the file gets stored. Some filepath in the local file system.
    """
    try:
        from urllib.request import urlretrieve  # Python 3
    except ImportError:
        from urllib import urlretrieve  # Python 2
    if sink is None:
        sink = os.path.abspath(os.path.split(source)[1])
    urlretrieve(source, sink)
    return sink


Instruction:
Given the following Python 3 function, write the documentation
def hash(filepath, method='sha1', buffer_size=65536):
    if method == 'sha1':
        hash_function = hashlib.sha1()
    elif method == 'md5':
        hash_function = hashlib.md5()
    else:
        raise NotImplementedError('Only md5 and sha1 hashes are known, but '
                                  ' \'{}\' was specified.'.format(method))

    with open(filepath, 'rb') as fp:
        while True:
            data = fp.read(buffer_size)
            if not data:
                break
            hash_function.update(data)
    return hash_function.hexdigest()

Response: Calculate a hash of a local file.


Instruction:
Here you have a function in Python 3, explain what it does
def get_creation_datetime(filepath):
    if platform.system() == 'Windows':
        return datetime.fromtimestamp(os.path.getctime(filepath))
    else:
        stat = os.stat(filepath)
        try:
            return datetime.fromtimestamp(stat.st_birthtime)
        except AttributeError:
            # We're probably on Linux. No easy way to get creation dates here,
            # so we'll settle for when its content was last modified.
            return None

Response: Get the creation date of a file in a node.


Instruction:
Create a Python 3 function to
get the datetime that a file was last modified.

Response:
def get_modification_datetime(filepath):
    """
    Get the datetime that a file was last modified.

    Parameters
    ----------
    filepath : str

    Returns
    -------
    modification_datetime : datetime.datetime

    """
    import tzlocal
    timezone = tzlocal.get_localzone()
    mtime = datetime.fromtimestamp(os.path.getmtime(filepath))
    return mtime.replace(tzinfo=timezone)


Instruction:
Implement a function in Python 3 to
get the last time filepath was accessed.

Response:
def get_access_datetime(filepath):
    """
    Get the last time filepath was accessed.

    Parameters
    ----------
    filepath : str

    Returns
    -------
    access_datetime : datetime.datetime
    """
    import tzlocal
    tz = tzlocal.get_localzone()
    mtime = datetime.fromtimestamp(os.path.getatime(filepath))
    return mtime.replace(tzinfo=tz)


Instruction:
Can you tell what is the following Python 3 function doing
def get_file_meta(filepath):
    meta = {}
    meta['filepath'] = os.path.abspath(filepath)
    meta['creation_datetime'] = get_creation_datetime(filepath)
    meta['last_access_datetime'] = get_access_datetime(filepath)
    meta['modification_datetime'] = get_modification_datetime(filepath)
    try:
        import magic
        f_mime = magic.Magic(mime=True, uncompress=True)
        f_other = magic.Magic(mime=False, uncompress=True)
        meta['mime'] = f_mime.from_file(meta['filepath'])
        meta['magic-type'] = f_other.from_file(meta['filepath'])
    except ImportError:
        pass
    return meta

Response: Get meta - information about a file.


Instruction:
Can you create a Python 3 function that
creates a GZIP file from a source file.

Response:
def gzip_file(source, sink):
    """
    Create a GZIP file from a source file.

    Parameters
    ----------
    source : str
        Filepath
    sink : str
        Filepath
    """
    import gzip
    with open(source, 'rb') as f_in, gzip.open(sink, 'wb') as f_out:
        f_out.writelines(f_in)


Instruction:
How would you implement a function in Python 3 that
starts the patch for the current object.

Response:
def start(self):
        """
        Start the patch
        """
        self._patcher = mock.patch(target=self.target)
        MockClient = self._patcher.start()
        instance = MockClient.return_value
        instance.model.side_effect = mock.Mock(
            side_effect=self.model
        )


Instruction:
Create a Python 3 function for
configuring client in session

Response:
def setup(cls, client_id, client_secret):
        """Configure client in session
        """
        cls.client_id = client_id
        cls.client_secret = client_secret


Instruction:
Write a Python 3 function for
parsing a ris file and return a list of entries.

Response:
def read(filelines, mapping=None, wok=False):
    """Parse a ris lines and return a list of entries.

    Entries are codified as dictionaries whose keys are the
    different tags. For single line and singly occurring tags,
    the content is codified as a string. In the case of multiline
    or multiple key occurrences, the content is returned as a list
    of strings.

    Keyword arguments:
    bibliography_file -- ris filehandle
    mapping -- custom RIS tags mapping
    wok -- flag, Web of Knowledge format is used if True, otherwise
           Refman's RIS specifications are used.

    """

    if wok:
        if not mapping:
            mapping = WOK_TAG_KEY_MAPPING
        return Wok(filelines, mapping).parse()
    else:
        if not mapping:
            mapping = TAG_KEY_MAPPING
        return Ris(filelines, mapping).parse()


Instruction:
How would you implement a function in Python 3 that
refreshes the context of the user and save it

Response:
def refresh_context(self):
        """
        Get the default context of the user and save it
        """
        User = self.model('res.user')

        self.context = User.get_preferences(True)
        return self.context


Instruction:
Can you tell what is the following Python 3 function doing
def login(self, login, password, set_auth=False):
        rv = self.session.post(
            self.host,
            dumps({
                "method": "common.db.login",
                "params": [login, password]
            }),
        )
        rv = loads(rv.content)['result']
        if set_auth:
            self.set_auth(
                SessionAuth(login, *rv)
            )
        return rv

Response: Attempts a login to the remote server and returns the user id and session id.


Instruction:
Write a Python 3 function for
returning true if the auth is not expired else false

Response:
def is_auth_alive(self):
        "Return true if the auth is not expired, else false"
        model = self.model('ir.model')
        try:
            model.search([], None, 1, None)
        except ClientError as err:
            if err and err.message['code'] == 403:
                return False
            raise
        except Exception:
            raise
        else:
            return True


Instruction:
Make a summary of the following Python 3 code
def update(self, data=None, **kwargs):
        if data is None:
            data = {}
        data.update(kwargs)
        return self.model.write([self.id], data)

Response: Update the record right away.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def search_read_all(self, domain, order, fields, batch_size=500,
                        context=None, offset=0, limit=None):
        """
        An endless iterator that iterates over records.

        :param domain: A search domain
        :param order: The order clause for search read
        :param fields: The fields argument for search_read
        :param batch_size: The optimal batch size when sending paginated
                           requests
        """
        if context is None:
            context = {}

        if limit is None:
            # When no limit is specified, all the records
            # should be fetched.
            record_count = self.search_count(domain, context=context)
            end = record_count + offset
        else:
            end = limit + offset

        for page_offset in range(offset, end, batch_size):
            if page_offset + batch_size > end:
                batch_size = end - page_offset
            for record in self.search_read(
                    domain, page_offset, batch_size,
                    order, fields, context=context):
                yield record

Response: A generator that yields all records in a domain.


Instruction:
Create a Python 3 function to
find records that match the filter.

Response:
def find(self, filter=None, page=1, per_page=10, fields=None, context=None):
        """
        Find records that match the filter.

        Pro Tip: The fields could have nested fields names if the field is
        a relationship type. For example if you were looking up an order
        and also want to get the shipping address country then fields would be:

            `['shipment_address', 'shipment_address.country']`

        but country in this case is the ID of the country which is not very
        useful if you don't already have a map. You can fetch the country code
        by adding `'shipment_address.country.code'` to the fields.

        :param filter: A domain expression (Refer docs for domain syntax)
        :param page: The page to fetch to get paginated results
        :param per_page: The number of records to fetch per page
        :param fields: A list of field names to fetch.
        :param context: Any overrides to the context.
        """
        if filter is None:
            filter = []
        rv = self.client.session.get(
            self.path,
            params={
                'filter': dumps(filter or []),
                'page': page,
                'per_page': per_page,
                'field': fields,
                'context': dumps(context or self.client.context),
            }
        )
        response_received.send(rv)
        return rv


Instruction:
Write a Python 3 function for
adding an attachmemt to record from url

Response:
def attach(self, id, filename, url):
        """Add an attachmemt to record from url

        :param id: ID of record
        :param filename: File name of attachment
        :param url: Public url to download file from.
        """
        Attachment = self.client.model('ir.attachment')
        return Attachment.add_attachment_from_url(
            filename, url, '%s,%s' % (self.model_name, id)
        )


Instruction:
Make a summary of the following Python 3 code
def refresh_if_needed(self):
        if self.state in (self.PENDING, self.STARTED):
            try:
                response, = self._fetch_result()['tasks']
            except (KeyError, ValueError):
                raise Exception(
                    "Unable to find results for task."
                )

            if 'error' in response:
                self.state == self.FAILURE
                raise ServerError(response['error'])

            if 'state' in response:
                self.state = response['state']
                self.result = response['result']

Response: Refresh the status of the task from server if required.


Instruction:
Write a Python 3 script to
get the product inventory in each location.

Response:
def get_product_inventory(product_id, warehouse_ids):
    """
    Return the product inventory in each location. The returned response
    will look like::

        {
            12: {       // Product ID
                4: {    // Location ID
                    'quantity_on_hand': 12.0,
                    'quantity_available': 8.0
                },
                5: {    // Location ID
                    'quantity_on_hand': 8.0,
                    'quantity_available': 8.0
                },
            },
            126: {      // Product ID
                4: {    // Location ID
                    'quantity_on_hand': 16.0,
                    'quantity_available': 15.0
                },
                5: {    // Location ID
                    'quantity_on_hand': 9.0,
                    'quantity_available': 8.0
                },
            }
        }

    Read more:
    http://docs.fulfiliorestapi.apiary.io/#reference/product/product-inventory
    """
    Product = client.model('product.product')

    return Product.get_product_inventory(
        [product_id], warehouse_ids
    )[product_id]


Instruction:
Can you create a Python 3 function that
fetches a customer with the code.

Response:
def get_customer(code):
    """
    Fetch a customer with the code.
    Returns None if the customer is not found.
    """
    Party = client.model('party.party')
    results = Party.find([('code', '=', code)])
    if results:
        return results[0]['id']


Instruction:
Implement a function in Python 3 to
get the address of a customer by its id.

Response:
def get_address(customer_id, data):
    """
    Easier to fetch the addresses of customer and then check one by one.

    You can get fancy by using some validation mechanism too
    """
    Address = client.model('party.address')

    addresses = Address.find(
        [('party', '=', customer_id)],
        fields=[
            'name', 'street', 'street_bis', 'city', 'zip',
            'subdivision.code', 'country.code'
        ]
    )
    for address in addresses:
        if (
                address['name'] == data['name'] and
                address['street'] == data['street'] and
                address['street_bis'] == data['street_bis'] and
                address['city'] == data['city'] and
                address['zip'] == data['zip'] and
                address['subdivision.code'].endswith(data['state']) and
                address['country.code'] == data['country']):
            return address['id']


Instruction:
How would you explain what the following Python 3 function does
def create_address(customer_id, data):
    Address = client.model('party.address')
    Country = client.model('country.country')
    Subdivision = client.model('country.subdivision')

    country, = Country.find([('code', '=', data['country'])])
    state, = Subdivision.find([
        ('code', 'ilike', '%-' + data['state']),  # state codes are US-CA, IN-KL
        ('country', '=', country['id'])
    ])

    address, = Address.create([{
        'party': customer_id,
        'name': data['name'],
        'street': data['street'],
        'street_bis': data['street_bis'],
        'city': data['city'],
        'zip': data['zip'],
        'country': country['id'],
        'subdivision': state['id'],
    }])
    return address['id']

Response: Create an address and return the id


Instruction:
Implement a function in Python 3 to
create a customer with the name email and phone.

Response:
def create_customer(name, email, phone):
    """
    Create a customer with the name.
    Then attach the email and phone as contact methods
    """
    Party = client.model('party.party')
    ContactMechanism = client.model('party.contact_mechanism')

    party, = Party.create([{'name': name}])

    # Bulk create the email and phone
    ContactMechanism.create([
        {'type': 'email', 'value': email, 'party': party},
        {'type': 'phone', 'value': phone, 'party': party},
    ])

    return party


Instruction:
How would you code a function in Python 3 to
create an order on fulfil from order_details.

Response:
def create_order(order):
    """
    Create an order on fulfil from order_details.
    See the calling function below for an example of the order_details
    """
    SaleOrder = client.model('sale.sale')
    SaleOrderLine = client.model('sale.line')

    # Check if customer exists, if not create one
    customer_id = get_customer(order['customer']['code'])
    if not customer_id:
        customer_id = create_customer(
            order['customer']['name'],
            order['customer']['email'],
            order['customer']['phone'],
        )

    # No check if there is a matching address
    invoice_address = get_address(
        customer_id,
        order['invoice_address']
    )
    if not invoice_address:
        invoice_address = create_address(
            customer_id,
            order['invoice_address']
        )

    # See if the shipping address exists, if not create it
    shipment_address = get_address(
        customer_id,
        order['shipment_address']
    )
    if not shipment_address:
        shipment_address = create_address(
            customer_id,
            order['shipment_address']
        )

    sale_order_id, = SaleOrder.create([{
        'reference': order['number'],
        'sale_date': order['date'],
        'party': customer_id,
        'invoice_address': invoice_address,
        'shipment_address': shipment_address,
    }])

    # fetch inventory of all the products before we create lines
    warehouses = get_warehouses()
    warehouse_ids = [warehouse['id'] for warehouse in warehouses]

    lines = []
    for item in order['items']:
        # get the product. We assume ti already exists.
        product = get_product(item['product'])

        # find the first location that has inventory
        product_inventory = get_product_inventory(product, warehouse_ids)
        for location, quantities in product_inventory.items():
            if quantities['quantity_available'] >= item['quantity']:
                break

        lines.append({
            'sale': sale_order_id,
            'product': product,
            'quantity': item['quantity'],
            'unit_price': item['unit_price'],
            'warehouse': location,
        })

    SaleOrderLine.create(lines)

    SaleOrder.quote([sale_order_id])
    SaleOrder.confirm([sale_order_id])


Instruction:
Can you generate a brief explanation for the following Python 3 code
def model_base(fulfil_client, cache_backend=None, cache_expire=10 * 60):
    return type(
        'BaseModel',
        (Model,),
        {
            'fulfil_client': fulfil_client,
            'cache_backend': cache_backend,
            'cache_expire': cache_expire,
            '__abstract__': True,
            '__modelregistry__': {},
        },
    )

Response: Returns a base model class that binds to the fulfil client instance and the cache instance.


Instruction:
How would you explain what the following Python 3 function does
def all(self):
        return self.rpc_model.search_read_all(
            self.domain,
            self._order_by,
            self.fields,
            context=self.context,
            offset=self._offset or 0,
            limit=self._limit,
        )

Response: Returns the results represented by this Query as a list.


Instruction:
Explain what the following Python 3 code does
def count(self):
        "Return a count of rows this Query would return."
        return self.rpc_model.search_count(
            self.domain, context=self.context
        )

Response: Return a count of rows this Query would return.


Instruction:
Make a summary of the following Python 3 code
def exists(self):
        return self.rpc_model.search_count(
            self.domain, context=self.context
        ) > 0

Response: Returns True if a record with the given ID exists otherwise False.


Instruction:
Can you tell what is the following Python 3 function doing
def show_active_only(self, state):
        query = self._copy()
        query.active_only = state
        return query

Response: Returns a new QuerySet with active only = True or false on a copy of this QuerySet


Instruction:
Here you have a function in Python 3, explain what it does
def filter_by(self, **kwargs):
        query = self._copy()
        for field, value in kwargs.items():
            query.domain.append(
                (field, '=', value)
            )
        return query

Response: Returns a new Query object with only the specified fields and values applied to the current Query.


Instruction:
How would you explain what the following Python 3 function does
def filter_by_domain(self, domain):
        query = self._copy()
        query.domain = domain
        return query

Response: Returns a new QuerySet with the given domain applied to the current QuerySet.


Instruction:
Create a Python 3 function to
return the first result of this Query or None if the result doesn t contain any row.

Response:
def first(self):
        """
        Return the first result of this Query or None if the result
        doesn't contain any row.
        """
        results = self.rpc_model.search_read(
            self.domain, None, 1, self._order_by, self.fields,
            context=self.context
        )
        return results and results[0] or None


Instruction:
How would you code a function in Python 3 to
get an instance based on the given primary key identifier.

Response:
def get(self, id):
        """
        Return an instance based on the given primary key identifier,
        or None if not found.

        This returns a record whether active or not.
        """
        ctx = self.context.copy()
        ctx['active_test'] = False
        results = self.rpc_model.search_read(
            [('id', '=', id)],
            None, None, None, self.fields,
            context=ctx
        )
        return results and results[0] or None


Instruction:
Can you implement a function in Python 3 that
applies a LIMIT to the query and return a new Query.

Response:
def limit(self, limit):
        """
        Apply a LIMIT to the query and return the newly resulting Query.
        """
        query = self._copy()
        query._limit = limit
        return query


Instruction:
Implement a Python 3 function for
applying an OFFSET to the query and return a new Query object.

Response:
def offset(self, offset):
        """
        Apply an OFFSET to the query and return the newly resulting Query.
        """
        query = self._copy()
        query._offset = offset
        return query


Instruction:
Can you implement a function in Python 3 that
returns exactly one result or raise an exception.

Response:
def one(self):
        """
        Return exactly one result or raise an exception.

        Raises fulfil_client.exc.NoResultFound if the query selects no rows.
        Raises fulfil_client.exc.MultipleResultsFound if multiple rows are
        found.
        """
        results = self.rpc_model.search_read(
            self.domain, 2, None, self._order_by, self.fields,
            context=self.context
        )
        if not results:
            raise fulfil_client.exc.NoResultFound
        if len(results) > 1:
            raise fulfil_client.exc.MultipleResultsFound
        return results[0]


Instruction:
Create a Python 3 function to
return a new Query object with the specified ORDER BY criterion applied to the query and returns the new Query object.

Response:
def order_by(self, *criterion):
        """
        apply one or more ORDER BY criterion to the query and
        return the newly resulting Query

        All existing ORDER BY settings can be suppressed by passing None -
        this will suppress any ORDER BY configured on mappers as well.
        """
        query = self._copy()
        query._order_by = criterion
        return query


Instruction:
Implement a function in Python 3 to
delete all records matching the query.

Response:
def delete(self):
        """
        Delete all records matching the query.

        Warning: This is a desctructive operation.

        Not every model allows deletion of records and several models
        even restrict based on status. For example, deleting products
        that have been transacted is restricted. Another example is sales
        orders which can be deleted only when they are draft.

        If deletion fails, a server error is thrown.
        """
        ids = self.rpc_model.search(self.domain, context=self.context)
        if ids:
            self.rpc_model.delete(ids)


Instruction:
Can you write a function in Python 3 where it
archives all the records matching the query.

Response:
def archive(self):
        """
        Archives (soft delete) all the records matching the query.

        This assumes that the model allows archiving (not many do - especially
        transactional documents).

        Internal implementation sets the active field to False.
        """
        ids = self.rpc_model.search(self.domain, context=self.context)
        if ids:
            self.rpc_model.write(ids, {'active': False})


Instruction:
How would you explain what the following Python 3 function does
def _logged_in_successful(data):
        if re.match(r'^:(testserver\.local|tmi\.twitch\.tv)'
                    r' NOTICE \* :'
                    r'(Login unsuccessful|Error logging in)*$',
                    data.strip()):
            return False
        else:
            return True

Response:
Test the login status from the returned communication of the the
        server.


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

        # Do not use non-blocking stream, they are not reliably
        # non-blocking
        # s.setblocking(False)
        # s.settimeout(1.0)

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        connect_host = "irc.twitch.tv"
        connect_port = 6667
        try:
            s.connect((connect_host, connect_port))
        except (Exception, IOError):
            print("Unable to create a socket to %s:%s" % (
                connect_host,
                connect_port))
            raise  # unexpected, because it is a blocking socket

        # Connected to twitch
        # Sending our details to twitch...
        s.send(('PASS %s\r\n' % self.oauth).encode('utf-8'))
        s.send(('NICK %s\r\n' % self.username).encode('utf-8'))
        if self.verbose:
            print('PASS %s\r\n' % self.oauth)
            print('NICK %s\r\n' % self.username)

        received = s.recv(1024).decode()
        if self.verbose:
            print(received)
        if not TwitchChatStream._logged_in_successful(received):
            # ... and they didn't accept our details
            raise IOError("Twitch did not accept the username-oauth "
                          "combination")
        else:
            # ... and they accepted our details
            # Connected to twitch.tv!
            # now make this socket non-blocking on the OS-level
            fcntl.fcntl(s, fcntl.F_SETFL, os.O_NONBLOCK)
            if self.s is not None:
                self.s.close()  # close the previous socket
            self.s = s          # store the new socket
            self.join_channel(self.username)

            # Wait until we have switched channels
            while self.current_channel != self.username:
                self.twitch_receive_messages()

Response: Connect to Twitch and return a Twitch - level object.


Instruction:
Can you generate the documentation for the following Python 3 function
def _push_from_buffer(self):
        if len(self.buffer) > 0:
            if time.time() - self.last_sent_time > 5:
                try:
                    message = self.buffer.pop(0)
                    self.s.send(message.encode('utf-8'))
                    if self.verbose:
                        print(message)
                finally:
                    self.last_sent_time = time.time()

Response: Push a message on the stack to the IRC stream.


Instruction:
Can you implement a function in Python 3 that
joins a different chat channel on Twitch.

Response:
def join_channel(self, channel):
        """
        Join a different chat channel on Twitch.
        Note, this function returns immediately, but the switch might
        take a moment

        :param channel: name of the channel (without #)
        """
        self.s.send(('JOIN #%s\r\n' % channel).encode('utf-8'))
        if self.verbose:
            print('JOIN #%s\r\n' % channel)


Instruction:
Explain what the following Python 3 code does
def _parse_message(self, data):
        if TwitchChatStream._check_has_ping(data):
            self._send_pong()
        if TwitchChatStream._check_has_channel(data):
            self.current_channel = \
                TwitchChatStream._check_has_channel(data)[0]

        if TwitchChatStream._check_has_message(data):
            return {
                'channel': re.findall(r'^:.+![a-zA-Z0-9_]+'
                                      r'@[a-zA-Z0-9_]+'
                                      r'.+ '
                                      r'PRIVMSG (.*?) :',
                                      data)[0],
                'username': re.findall(r'^:([a-zA-Z0-9_]+)!', data)[0],
                'message': re.findall(r'PRIVMSG #[a-zA-Z0-9_]+ :(.+)',
                                      data)[0].decode('utf8')
            }
        else:
            return None

Response: Parse the bytes received from the socket and return a dict of the fields.


Instruction:
Explain what the following Python 3 code does
def twitch_receive_messages(self):
        self._push_from_buffer()
        result = []
        while True:
            # process the complete buffer, until no data is left no more
            try:
                msg = self.s.recv(4096).decode()     # NON-BLOCKING RECEIVE!
            except socket.error as e:
                err = e.args[0]
                if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
                    # There is no more data available to read
                    return result
                else:
                    # a "real" error occurred
                    # import traceback
                    # import sys
                    # print(traceback.format_exc())
                    # print("Trying to recover...")
                    self.connect()
                    return result
            else:
                if self.verbose:
                    print(msg)
                rec = [self._parse_message(line)
                       for line in filter(None, msg.split('\r\n'))]
                rec = [r for r in rec if r]     # remove Nones
                result.extend(rec)

Response: This function will process all messages received by the socket and return a list of dicts. Each dict is a dict with the keys channel username message and the values are the message fields.


Instruction:
Create a Python 3 function for
resetting the videostream by restarting ffmpeg.

Response:
def reset(self):
        """
        Reset the videostream by restarting ffmpeg
        """

        if self.ffmpeg_process is not None:
            # Close the previous stream
            try:
                self.ffmpeg_process.send_signal(signal.SIGINT)
            except OSError:
                pass

        command = []
        command.extend([
            self.ffmpeg_binary,
            '-loglevel', 'verbose',
            '-y',       # overwrite previous file/stream
            # '-re',    # native frame-rate
            '-analyzeduration', '1',
            '-f', 'rawvideo',
            '-r', '%d' % self.fps,  # set a fixed frame rate
            '-vcodec', 'rawvideo',
            # size of one frame
            '-s', '%dx%d' % (self.width, self.height),
            '-pix_fmt', 'rgb24',  # The input are raw bytes
            '-thread_queue_size', '1024',
            '-i', '/tmp/videopipe',  # The input comes from a pipe

            # Twitch needs to receive sound in their streams!
            # '-an',            # Tells FFMPEG not to expect any audio
        ])
        if self.audio_enabled:
            command.extend([
                '-ar', '%d' % AUDIORATE,
                '-ac', '2',
                '-f', 's16le',
                '-thread_queue_size', '1024',
                '-i', '/tmp/audiopipe'
            ])
        else:
            command.extend([
                '-ar', '8000',
                '-ac', '1',
                '-f', 's16le',
                '-i', '/dev/zero',  # silence alternative, works forever
                # '-i','http://stream1.radiostyle.ru:8001/tunguska',
                # '-filter_complex',
                # '[0:1][1:0]amix=inputs=2:duration=first[all_audio]'
            ])
        command.extend([
            # VIDEO CODEC PARAMETERS
            '-vcodec', 'libx264',
            '-r', '%d' % self.fps,
            '-b:v', '3000k',
            '-s', '%dx%d' % (self.width, self.height),
            '-preset', 'faster', '-tune', 'zerolatency',
            '-crf', '23',
            '-pix_fmt', 'yuv420p',
            # '-force_key_frames', r'expr:gte(t,n_forced*2)',
            '-minrate', '3000k', '-maxrate', '3000k',
            '-bufsize', '12000k',
            '-g', '60',     # key frame distance
            '-keyint_min', '1',
            # '-filter:v "setpts=0.25*PTS"'
            # '-vsync','passthrough',

            # AUDIO CODEC PARAMETERS
            '-acodec', 'libmp3lame', '-ar', '44100', '-b:a', '160k',
            # '-bufsize', '8192k',
            '-ac', '1',
            # '-acodec', 'aac', '-strict', 'experimental',
            # '-ab', '128k', '-ar', '44100', '-ac', '1',
            # '-async','44100',
            # '-filter_complex', 'asplit', #for audio sync?

            # STORE THE VIDEO PARAMETERS
            # '-vcodec', 'libx264', '-s', '%dx%d'%(width, height),
            # '-preset', 'libx264-fast',
            # 'my_output_videofile2.avi'

            # MAP THE STREAMS
            # use only video from first input and only audio from second
            '-map', '0:v', '-map', '1:a',

            # NUMBER OF THREADS
            '-threads', '2',

            # STREAM TO TWITCH
            '-f', 'flv', 'rtmp://live-ams.twitch.tv/app/%s' %
            self.twitch_stream_key
            ])

        devnullpipe = open("/dev/null", "w")     # Throw away stream
        if self.verbose:
            devnullpipe = None
        self.ffmpeg_process = subprocess.Popen(
            command,
            stdin=subprocess.PIPE,
            stderr=devnullpipe,
            stdout=devnullpipe)


Instruction:
Explain what the following Python 3 code does
def send_video_frame(self, frame):
        if self.video_pipe is None:
            if not os.path.exists('/tmp/videopipe'):
                os.mkfifo('/tmp/videopipe')
            self.video_pipe = os.open('/tmp/videopipe', os.O_WRONLY)

        assert frame.shape == (self.height, self.width, 3)

        frame = np.clip(255*frame, 0, 255).astype('uint8')
        try:
            os.write(self.video_pipe, frame.tostring())
        except OSError:
            # The pipe has been closed. Reraise and handle it further
            # downstream
            raise

Response: Send a frame to the video pipe.


Instruction:
Implement a function in Python 3 to
send the audio samples to the audio pipe.

Response:
def send_audio(self, left_channel, right_channel):
        """Add the audio samples to the stream. The left and the right
        channel should have the same shape.
        Raises an OSError when the stream is closed.

        :param left_channel: array containing the audio signal.
        :type left_channel: numpy array with shape (k, )
            containing values between -1.0 and 1.0. k can be any integer
        :param right_channel: array containing the audio signal.
        :type right_channel: numpy array with shape (k, )
            containing values between -1.0 and 1.0. k can be any integer
        """
        if self.audio_pipe is None:
            if not os.path.exists('/tmp/audiopipe'):
                os.mkfifo('/tmp/audiopipe')
            self.audio_pipe = os.open('/tmp/audiopipe', os.O_WRONLY)

        assert len(left_channel.shape) == 1
        assert left_channel.shape == right_channel.shape

        frame = np.column_stack((left_channel, right_channel)).flatten()

        frame = np.clip(32767*frame, -32767, 32767).astype('int16')
        try:
            os.write(self.audio_pipe, frame.tostring())
        except OSError:
            # The pipe has been closed. Reraise and handle it further
            # downstream
            raise


Instruction:
Can you create a Python 3 function that
adds the audio samples to the stream. The left and right channels should have the same shape. The left and right channels should have the same shape. The right and the left channels must have the same shape. The left and the right channels must have the same shape.

Response:
def send_audio(self, left_channel, right_channel):
        """Add the audio samples to the stream. The left and the right
        channel should have the same shape.

        :param left_channel: array containing the audio signal.
        :type left_channel: numpy array with shape (k, )
            containing values between -1.0 and 1.0. k can be any integer
        :param right_channel: array containing the audio signal.
        :type right_channel: numpy array with shape (k, )
            containing values between -1.0 and 1.0. k can be any integer
        """
        self.lastaudioframe_left = left_channel
        self.lastaudioframe_right = right_channel


Instruction:
Can you write a function in Python 3 where it
sends a frame to the video queue

Response:
def send_video_frame(self, frame, frame_counter=None):
        """send frame of shape (height, width, 3)
        with values between 0 and 1

        :param frame: array containing the frame.
        :type frame: numpy array with shape (height, width, 3)
            containing values between 0.0 and 1.0
        :param frame_counter: frame position number within stream.
            Provide this when multi-threading to make sure frames don't
            switch position
        :type frame_counter: int
        """
        if frame_counter is None:
            frame_counter = self.frame_counter
            self.frame_counter += 1

        self.q_video.put((frame_counter, frame))


Instruction:
How would you explain what the following Python 3 function does
def send_audio(self,
                   left_channel,
                   right_channel,
                   frame_counter=None):
        """Add the audio samples to the stream. The left and the right
        channel should have the same shape.

        :param left_channel: array containing the audio signal.
        :type left_channel: numpy array with shape (k, )
            containing values between -1.0 and 1.0. l can be any integer
        :param right_channel: array containing the audio signal.
        :type right_channel: numpy array with shape (k, )
            containing values between -1.0 and 1.0. l can be any integer
        :param frame_counter: frame position number within stream.
            Provide this when multi-threading to make sure frames don't
            switch position
        :type frame_counter: int
        """
        if frame_counter is None:
            frame_counter = self.audio_frame_counter
            self.audio_frame_counter += 1

        self.q_audio.put((frame_counter, left_channel, right_channel))

Response: Add the audio samples to the queue.


Instruction:
Write a Python 3 function that can
import an attribute from a module.

Response:
def import_attribute(self, path):
        """
        Import an attribute from a module.
        """
        module = '.'.join(path.split('.')[:-1])
        function = path.split('.')[-1]

        module = importlib.import_module(module)
        return getattr(module, function)


Instruction:
Write a Python 3 function for
returning a url to redirect to after the login or signup.

Response:
def get_next(self, request):
        """
        Returns a url to redirect to after the login / signup.
        """
        if 'next' in request.session:
            next = request.session['next']
            del request.session['next']
        elif 'next' in request.GET:
            next = request.GET.get('next')
        elif 'next' in request.POST:
            next = request.POST.get('next')
        else:
            next = getattr(settings, 'LOGIN_REDIRECT_URL', '/')

        netloc = urlparse.urlparse(next)[1]

        if netloc and netloc != request.get_host():
            next = getattr(settings, 'LOGIN_REDIRECT_URL', '/')

        return next


Instruction:
Write a Python 3 script to
return an inactive message.

Response:
def inactive_response(self, request):
        """
        Return an inactive message.
        """
        inactive_url = getattr(settings, 'LOGIN_INACTIVE_REDIRECT_URL', '')
        if inactive_url:
            return HttpResponseRedirect(inactive_url)
        else:
            return self.error_to_response(request, {'error': _("This user account is marked as inactive.")})


Instruction:
Can you write a function in Python 3 where it
creates a profile model.

Response:
def create_profile(self, user, save=False, **kwargs):
        """
        Create a profile model.

        :param user: A user object
        :param save: If this is set, the profile will
            be saved to DB straight away
        :type save: bool
        """
        profile = self.get_model()(user=user, **kwargs)

        if save:
            profile.save()

        return profile


Instruction:
Explain what the following Python 3 code does
def get_or_create_profile(self, user, save=False, **kwargs):
        try:
            profile = self.get_model().objects.get(user=user, **kwargs)
            return profile, False
        except self.get_model().DoesNotExist:
            profile = self.create_profile(user, save=save, **kwargs)
            return profile, True

Response: Get a profile from DB or create one if it doesn t exist.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_session_data(self, request):
        user = request.session['%suser' % SESSION_KEY]
        profile = request.session['%sprofile' % SESSION_KEY]
        client = request.session['%sclient' % SESSION_KEY]
        return user, profile, client

Response: Return a tuple of user profile client from the session.


Instruction:
How would you explain what the following Python 3 function does
def send_login_signal(self, request, user, profile, client):
        signals.login.send(sender=profile.__class__, user=user,
            profile=profile, client=client, request=request)

Response: Send a signal that a user logged in.


Instruction:
How would you implement a function in Python 3 that
sends a signal that a user connected a social profile to his Django account.

Response:
def send_connect_signal(self, request, user, profile, client):
        """
        Send a signal that a user connected a social profile to his Django
        account. This signal should be sent *only* when the a new social
        connection was created.
        """
        signals.connect.send(sender=profile.__class__, user=user, profile=profile,
            client=client, request=request)


Instruction:
Can you create a Python 3 function that
requests an access token for the specified set of parameters.

Response:
def request_access_token(self, params):
        """
        Foursquare does not accept POST requests to retrieve an access token,
        so we'll be doing a GET request instead.
        """
        return self.request(self.access_token_url, method="GET", params=params)


Instruction:
Here you have a function in Python 3, explain what it does
def openid_form(parser, token):

    bits = get_bits(token)

    if len(bits) > 1:
        return FormNode(bits[0], bits[1:])
    if len(bits) == 1:
        return FormNode(bits[0])
    return FormNode(None)

Response:
Render OpenID form. Allows to pre set the provider::

    	{% openid_form "https://www.google.com/accounts/o8/id" %}

    Also creates custom button URLs by concatenating all arguments
    after the provider's URL

    	{% openid_form "https://www.google.com/accounts/o8/id" STATIC_URL "image/for/google.jpg" %}


Instruction:
Explain what the following Python 3 code does
def get_initial_data(self, request, user, profile, client):
        if INITAL_DATA_FUNCTION:
            func = self.import_attribute(INITAL_DATA_FUNCTION)
            return func(request, user, profile, client)
        return {}

Response: Returns the initial data for the setup form.


Instruction:
How would you code a function in Python 3 to
return the additional context for the setup view.

Response:
def get_context(self, request, user, profile, client):
        """
        Return additional context for the setup view. The function can
        be controlled with ``SOCIALREGISTRATION_SETUP_CONTEXT_FUNCTION``.

        :param request: The current request object
        :param user: The unsaved user object
        :param profile: The unsaved profile object
        :param client: The API client
        """
        if CONTEXT_FUNCTION:
            func = self.import_attribute(CONTEXT_FUNCTION)
            return func(request, user, profile, client)
        return {}


Instruction:
How would you explain what the following Python 3 function does
def generate_username_and_redirect(self, request, user, profile, client):
        func = self.get_username_function()

        user.username = func(user, profile, client)
        user.set_unusable_password()
        user.save()

        profile.user = user
        profile.save()

        user = profile.authenticate()

        self.send_connect_signal(request, user, profile, client)

        self.login(request, user)

        self.send_login_signal(request, user, profile, client)

        self.delete_session_data(request)

        return HttpResponseRedirect(self.get_next(request))

Response: Generate a username and then redirect the user to the correct place.


Instruction:
Write a Python 3 function that can
get the social profile.

Response:
def get(self, request):
        """
        When signing a new user up - either display a setup form, or
        generate the username automatically.
        """

        if request.user.is_authenticated():
            return HttpResponseRedirect(self.get_next(request))

        try:
            user, profile, client = self.get_session_data(request)
        except KeyError:
            return self.error_to_response(request, dict(
                error=_("Social profile is missing from your session.")))

        if GENERATE_USERNAME:
            return self.generate_username_and_redirect(request, user, profile, client)

        form = self.get_form()(initial=self.get_initial_data(request, user, profile, client))

        additional_context = self.get_context(request, user, profile, client)
        return self.render_to_response(dict({'form': form}, **additional_context))


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

        if request.user.is_authenticated():
            return self.error_to_response(request, dict(
                error=_("You are already logged in.")))

        try:
            user, profile, client = self.get_session_data(request)
        except KeyError:
            return self.error_to_response(request, dict(
                error=_("A social profile is missing from your session.")))

        form = self.get_form()(request.POST, request.FILES,
            initial=self.get_initial_data(request, user, profile, client))

        if not form.is_valid():
            additional_context = self.get_context(request, user, profile, client)
            return self.render_to_response(dict({'form': form}, **additional_context))

        user, profile = form.save(request, user, profile, client)

        user = profile.authenticate()

        self.send_connect_signal(request, user, profile, client)

        self.login(request, user)

        self.send_login_signal(request, user, profile, client)

        self.delete_session_data(request)

        return HttpResponseRedirect(self.get_next(request))

Response: Save the user and profile login and send the right signals.


Instruction:
Make a summary of the following Python 3 code
def post(self, request):
        request.session['next'] = self.get_next(request)
        client = self.get_client()()
        request.session[self.get_client().get_session_key()] = client
        url = client.get_redirect_url(request=request)
        logger.debug("Redirecting to %s", url)
        try:
            return HttpResponseRedirect(url)
        except OAuthError, error:
            return self.error_to_response(request, {'error': error})
        except socket.timeout:
            return self.error_to_response(request, {'error':
                _('Could not connect to service (timed out)')})

Response: Create a client and redirect to the API provider to authorize our app and permissions.


Instruction:
Here you have a function in Python 3, explain what it does
def get(self, request):
        try:
            client = request.session[self.get_client().get_session_key()]
            logger.debug("API returned: %s", request.GET)
            client.complete(dict(request.GET.items()))
            request.session[self.get_client().get_session_key()] = client
            return HttpResponseRedirect(self.get_redirect())
        except KeyError:
            return self.error_to_response(request, {'error': "Session expired."})
        except OAuthError, error:
            return self.error_to_response(request, {'error': error})
        except socket.timeout:
            return self.error_to_response(request, {'error':
                _('Could not connect to service (timed out)')})

Response: Handles the GET request and returns a response object with the user s information.


Instruction:
Write a Python 3 function for
attempting to get a user s profile and create a profile object if it doesn t already exist.

Response:
def get(self, request):
        """
        Called after authorization was granted and the OAuth flow
        successfully completed.

        Tries to:

        - Connect the remote account if the user is logged in already
        - Log the user in if a local profile of the remote account
          exists already
        - Create a user and profile object if none of the above succeed
          and redirect the user further to either capture some data via
          form or generate a username automatically
        """

        try:
            client = request.session[self.get_client().get_session_key()]
        except KeyError:
            return self.error_to_response(request, {'error': "Session expired."})

        # Get the lookup dictionary to find the user's profile
        lookup_kwargs = self.get_lookup_kwargs(request, client)

        # Logged in user (re-)connecting an account
        if request.user.is_authenticated():
            try:
                profile = self.get_profile(**lookup_kwargs)

                # Make sure that there is only *one* account per profile.
                if not profile.user == request.user:
                    self.delete_session_data(request)
                    return self.error_to_response(request, {
                        'error': _('This profile is already connected to another user account.')
                    })

            except self.get_model().DoesNotExist:
                profile, created = self.get_or_create_profile(request.user,
                    save=True, **lookup_kwargs)

            self.send_connect_signal(request, request.user, profile, client)

            return self.redirect(request)

        # Logged out user - let's see if we've got the identity saved already.
        # If so - just log the user in. If not, create profile and redirect
        # to the setup view

        user = self.authenticate(**lookup_kwargs)

        # No user existing - create a new one and redirect to the final setup view
        if user is None:
            if not ALLOW_OPENID_SIGNUPS and self.client is OpenIDClient:
                return self.error_to_response(request, {
                    'error': _('We are not currently accepting new OpenID signups.')
                })
            user = self.create_user()
            profile = self.create_profile(user, **lookup_kwargs)

            self.store_user(request, user)
            self.store_profile(request, profile)
            self.store_client(request, client)

            return HttpResponseRedirect(reverse('socialregistration:setup'))

        # Inactive user - displaying / redirect to the appropriate place.
        if not user.is_active:
            return self.inactive_response(request)

        # Active user with existing profile: login, send signal and redirect
        self.login(request, user)

        profile = self.get_profile(user=user, **lookup_kwargs)

        self.send_login_signal(request, user, profile, client)

        return self.redirect(request)


Instruction:
Make a summary of the following Python 3 code
def client(self, verifier=None):
        # We're just starting out and don't have neither request nor access
        # token. Return the standard client
        if not self._request_token and not self._access_token:
            client = oauth.Client(self.consumer, timeout=TIMEOUT)

        # We're one step in, we've got the request token and can add that to
        # the client.
        if self._request_token and not self._access_token:
            if verifier is not None:
                self._request_token.set_verifier(verifier)
            client = oauth.Client(self.consumer, self._request_token, timeout=TIMEOUT)

        # Two steps in, we've got an access token and can now properly sign
        # our client requests with it.
        if self._access_token:
            client = oauth.Client(self.consumer, self._access_token, timeout=TIMEOUT)

        return client

Response: Return the correct client depending on which stage of the OAuth process is in.


Instruction:
Implement a function in Python 3 to
fetch a request token from self. request_token_url.

Response:
def _get_request_token(self):
        """
        Fetch a request token from `self.request_token_url`.
        """

        params = {
            'oauth_callback': self.get_callback_url()
        }

        response, content = self.client().request(self.request_token_url,
            "POST", body=urllib.urlencode(params))

        content = smart_unicode(content)

        if not response['status'] == '200':
            raise OAuthError(_(
                u"Invalid status code %s while obtaining request token from %s: %s") % (
                    response['status'], self.request_token_url, content))

        token = dict(urlparse.parse_qsl(content))

        return oauth.Token(token['oauth_token'], token['oauth_token_secret'])


Instruction:
Implement a Python 3 function for
fetching an access token from self. access_token_url.

Response:
def _get_access_token(self, verifier=None):
        """
        Fetch an access token from `self.access_token_url`.
        """

        response, content = self.client(verifier).request(
            self.access_token_url, "POST")

        content = smart_unicode(content)

        if not response['status'] == '200':
            raise OAuthError(_(
                u"Invalid status code %s while obtaining access token from %s: %s") %
                (response['status'], self.access_token_url, content))

        token = dict(urlparse.parse_qsl(content))

        return (oauth.Token(token['oauth_token'], token['oauth_token_secret']),
            token)


Instruction:
How would you code a function in Python 3 to
get the request token for this API.

Response:
def get_request_token(self):
        """
        Return the request token for this API. If we've not fetched it yet,
        go out, request and memoize it.
        """

        if self._request_token is None:
            self._request_token = self._get_request_token()
        return self._request_token


Instruction:
Can you tell what is the following Python 3 function doing
def get_access_token(self, verifier=None):

        if self._access_token is None:
            self._access_token, self._access_token_dict = self._get_access_token(verifier)
        return self._access_token

Response: Get the access token for this API.


Instruction:
Make a summary of the following Python 3 code
def get_redirect_url(self, **kwargs):
        params = {
            'oauth_token': self.get_request_token().key,
        }
        return '%s?%s' % (self.auth_url, urllib.urlencode(params))

Response:
Returns the authorization URL signed with the request
        token.


Instruction:
Create a Python 3 function for
attempting to complete the flow by requesting an access token.

Response:
def complete(self, GET):
        """
        When redirect back to our application, try to complete the flow by
        requesting an access token. If the access token request fails, it'll
        throw an `OAuthError`.

        Tries to complete the flow by validating against the `GET` paramters
        received.
        """
        token = self.get_access_token(verifier=GET.get('oauth_verifier', None))
        return token


Instruction:
How would you explain what the following Python 3 function does
def request(self, url, method="GET", params=None, headers=None):
        params = params or {}
        headers = headers or {}

        logger.debug("URL: %s", url)
        logger.debug("Method: %s", method)
        logger.debug("Headers: %s", headers)
        logger.debug("Params: %s", params)

        response, content = self.client().request(url, method, headers=headers,
            body=urllib.urlencode(params))

        content = smart_unicode(content)

        logger.debug("Status: %s", response['status'])
        logger.debug("Content: %s", content)

        if response['status'] != '200':
            raise OAuthError(_(
                u"Invalid status code %s while requesting %s: %s") % (
                    response['status'], url, content))

        return content

Response: Make signed requests against url.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_redirect_url(self, state='', **kwargs):
        params = {
            'response_type': 'code',
            'client_id': self.client_id,
            'redirect_uri': self.get_callback_url(**kwargs),
            'scope': self.scope or '',
            'state': state,
        }

        return '%s?%s' % (self.auth_url, urllib.urlencode(params))

Response: Assemble the URL to redirect the user to request the permissions.


Instruction:
How would you code a function in Python 3 to
request the access token from the API.

Response:
def request_access_token(self, params):
        """
        Request the access token from `self.access_token_url`. The default
        behaviour is to use a `POST` request, but some services use `GET`
        requests. Individual clients can override this method to use the
        correct HTTP method.
        """
        return self.request(self.access_token_url, method="POST", params=params,
            is_signed=False)


Instruction:
Can you create a Python 3 function that
fetches an access token with the provided code.

Response:
def _get_access_token(self, code, **params):
        """
        Fetch an access token with the provided `code`.
        """
        params.update({
            'code': code,
            'client_id': self.client_id,
            'client_secret': self.secret,
            'redirect_uri': self.get_callback_url(),
        })

        logger.debug("Params: %s", params)

        resp, content = self.request_access_token(params=params)

        content = smart_unicode(content)

        logger.debug("Status: %s", resp['status'])
        logger.debug("Content: %s", content)

        content = self.parse_access_token(content)

        if 'error' in content:
            raise OAuthError(_(
                u"Received error while obtaining access token from %s: %s") % (
                    self.access_token_url, content['error']))

        return content


Instruction:
Can you implement a function in Python 3 that
gets the memoized access token or go out and fetch one.

Response:
def get_access_token(self, code=None, **params):
        """
        Return the memoized access token or go out and fetch one.
        """
        if self._access_token is None:
            if code is None:
                raise ValueError(_('Invalid code.'))

            self.access_token_dict = self._get_access_token(code, **params)
            try:
                self._access_token = self.access_token_dict['access_token']
            except KeyError, e:
                raise OAuthError("Credentials could not be validated, the provider returned no access token.")

        return self._access_token


Instruction:
Can you generate the documentation for the following Python 3 function
def complete(self, GET):
        if 'error' in GET:
            raise OAuthError(
                _("Received error while obtaining access token from %s: %s") % (
                    self.access_token_url, GET['error']))
        return self.get_access_token(code=GET.get('code'))

Response: Complete the OAuth2 flow by fetching an access token with the provided code in the GET parameters.


Instruction:
Can you generate the documentation for the following Python 3 function
def request(self, url, method="GET", params=None, headers=None, is_signed=True):
        params = params or {}
        headers = headers or {}

        if is_signed:
            params.update(self.get_signing_params())

        if method.upper() == "GET":
            url = '%s?%s' % (url, urllib.urlencode(params))
            return self.client().request(url, method=method, headers=headers)
        return self.client().request(url, method, body=urllib.urlencode(params), headers=headers)

Response: Make a request against url.


Instruction:
Explain what the following Python 3 code does
def request_access_token(self, params):
        return self.client().request(self.access_token_url, method="POST", body=urllib.urlencode(params), headers={'Content-Type':'application/x-www-form-urlencoded'})

Response: Request an access token for the specified set of parameters.


Instruction:
Write a Python 3 script to
return internal temperature value in degrees celsius.

Response:
def readInternalC(self):
        """Return internal temperature value in degrees celsius."""
        v = self._read32()
        # Ignore bottom 4 bits of thermocouple data.
        v >>= 4
        # Grab bottom 11 bits as internal temperature data.
        internal = v & 0x7FF
        if v & 0x800:
            # Negative value, take 2's compliment. Compute this with subtraction
            # because python is a little odd about handling signed/unsigned.
            internal -= 4096
        # Scale by 0.0625 degrees C per bit and return value.
        return internal * 0.0625


Instruction:
How would you code a function in Python 3 to
return the thermocouple temperature value in degrees celsius.

Response:
def readTempC(self):
        """Return the thermocouple temperature value in degrees celsius."""
        v = self._read32()
        # Check for error reading value.
        if v & 0x7:
            return float('NaN')
        # Check if signed bit is set.
        if v & 0x80000000:
            # Negative value, take 2's compliment. Compute this with subtraction
            # because python is a little odd about handling signed/unsigned.
            v >>= 18
            v -= 16384
        else:
            # Positive value, just shift the bits to get the value.
            v >>= 18
        # Scale by 0.25 degrees C per bit and return value.
        return v * 0.25


Instruction:
Given the following Python 3 function, write the documentation
def readLinearizedTempC(self):
        # MAX31855 thermocouple voltage reading in mV
        thermocoupleVoltage = (self.readTempC() - self.readInternalC()) * 0.041276
        # MAX31855 cold junction voltage reading in mV
        coldJunctionTemperature = self.readInternalC()
        coldJunctionVoltage = (-0.176004136860E-01 +
            0.389212049750E-01  * coldJunctionTemperature +
            0.185587700320E-04  * math.pow(coldJunctionTemperature, 2.0) +
            -0.994575928740E-07 * math.pow(coldJunctionTemperature, 3.0) +
            0.318409457190E-09  * math.pow(coldJunctionTemperature, 4.0) +
            -0.560728448890E-12 * math.pow(coldJunctionTemperature, 5.0) +
            0.560750590590E-15  * math.pow(coldJunctionTemperature, 6.0) +
            -0.320207200030E-18 * math.pow(coldJunctionTemperature, 7.0) +
            0.971511471520E-22  * math.pow(coldJunctionTemperature, 8.0) +
            -0.121047212750E-25 * math.pow(coldJunctionTemperature, 9.0) +
            0.118597600000E+00  * math.exp(-0.118343200000E-03 * math.pow((coldJunctionTemperature-0.126968600000E+03), 2.0)))
        # cold junction voltage + thermocouple voltage
        voltageSum = thermocoupleVoltage + coldJunctionVoltage
        # calculate corrected temperature reading based on coefficients for 3 different ranges
        # float b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;
        if thermocoupleVoltage < 0:
            b0 = 0.0000000E+00
            b1 = 2.5173462E+01
            b2 = -1.1662878E+00
            b3 = -1.0833638E+00
            b4 = -8.9773540E-01
            b5 = -3.7342377E-01
            b6 = -8.6632643E-02
            b7 = -1.0450598E-02
            b8 = -5.1920577E-04
            b9 = 0.0000000E+00
        elif thermocoupleVoltage < 20.644:
            b0 = 0.000000E+00
            b1 = 2.508355E+01
            b2 = 7.860106E-02
            b3 = -2.503131E-01
            b4 = 8.315270E-02
            b5 = -1.228034E-02
            b6 = 9.804036E-04
            b7 = -4.413030E-05
            b8 = 1.057734E-06
            b9 = -1.052755E-08
        elif thermocoupleVoltage < 54.886:
            b0 = -1.318058E+02
            b1 = 4.830222E+01
            b2 = -1.646031E+00
            b3 = 5.464731E-02
            b4 = -9.650715E-04
            b5 = 8.802193E-06
            b6 = -3.110810E-08
            b7 = 0.000000E+00
            b8 = 0.000000E+00
            b9 = 0.000000E+00
        else:
            # TODO: handle error - out of range
            return 0
        return (b0 +
            b1 * voltageSum +
            b2 * pow(voltageSum, 2.0) +
            b3 * pow(voltageSum, 3.0) +
            b4 * pow(voltageSum, 4.0) +
            b5 * pow(voltageSum, 5.0) +
            b6 * pow(voltageSum, 6.0) +
            b7 * pow(voltageSum, 7.0) +
            b8 * pow(voltageSum, 8.0) +
            b9 * pow(voltageSum, 9.0))

Response: Return the NIST - linearized thermocouple temperature value in degrees celsius.


Instruction:
Explain what the following Python 3 code does
def escape_quotes(self, val):
        if self.is_string(val) and self._in_quotes(val, self.quote):
            # make sure any previously escaped quotes are not re-escaped
            middle = self.remove_quotes(val).replace("\\" + self.quote, self.quote)
            middle = middle.replace(self.quote, "\\" + self.quote)
            val = self.add_quotes(middle)

        return val

Response: Escape any quotes in a value.


Instruction:
Write a Python 3 script to
standardise the quotes used to wrap a value to the pprint default.

Response:
def standardise_quotes(self, val):
        """
        Change the quotes used to wrap a value to the pprint default
        E.g. "val" to 'val' or 'val' to "val"
        """
        if self._in_quotes(val, self.altquote):
            middle = self.remove_quotes(val)
            val = self.add_quotes(middle)

        return self.escape_quotes(val)


Instruction:
Here you have a function in Python 3, explain what it does
def process_key_dict(self, key, d, level):

        # add any composite level comments
        comments = d.get("__comments__", {})
        lines = []
        self._add_type_comment(level, comments, lines)

        lines += [self.add_start_line(key, level)]
        lines += self.process_dict(d, level, comments)
        lines.append(self.add_end_line(level, 1, key))

        return lines

Response: Process key value dicts e. g. METADATA key value


Instruction:
Explain what the following Python 3 code does
def process_dict(self, d, level, comments):
        lines = []

        for k, v in d.items():
            if not self.__is_metadata(k):
                qk = self.quoter.add_quotes(k)
                qv = self.quoter.add_quotes(v)
                line = self.__format_line(self.whitespace(level, 2), qk, qv)
                line += self.process_attribute_comment(comments, k)
                lines.append(line)

        return lines

Response: Processes keys and values within a block holding a specific level.


Instruction:
How would you explain what the following Python 3 function does
def process_config_dict(self, key, d, level):
        lines = []
        for k, v in d.items():
            k = "CONFIG {}".format(self.quoter.add_quotes(k.upper()))
            v = self.quoter.add_quotes(v)
            lines.append(self.__format_line(self.whitespace(level, 1), k, v))
        return lines

Response: Process the CONFIG block


Instruction:
Write a Python 3 function that can
process repeated keys e. g. FORMATOPTION

Response:
def process_repeated_list(self, key, lst, level):
        """
        Process blocks of repeated keys e.g. FORMATOPTION
        """
        lines = []

        for v in lst:
            k = key.upper()
            v = self.quoter.add_quotes(v)
            lines.append(self.__format_line(self.whitespace(level, 1), k, v))

        return lines


Instruction:
How would you explain what the following Python 3 function does
def format_pair_list(self, key, pair_list, level):

        lines = [self.add_start_line(key, level)]

        list_spacer = self.spacer * (level + 2)
        pairs = ["{}{} {}".format(list_spacer, p[0], p[1]) for p in pair_list]
        lines += pairs

        lines.append(self.add_end_line(level, 1, key))

        return lines

Response: Format a list of pairs.


Instruction:
Can you generate the documentation for the following Python 3 function
def format_repeated_pair_list(self, key, root_list, level):

        lines = []

        def depth(L):
            return isinstance(L, (tuple, list)) and max(map(depth, L)) + 1

        if depth(root_list) == 2:
            # single set of points only
            root_list = [root_list]

        for pair_list in root_list:
            lines += self.format_pair_list(key, pair_list, level)

        return lines

Response: Process repeated lists of pairs e. g. POINTs blocks


Instruction:
Write a Python 3 function for
returning True if the key is not a hidden container.

Response:
def is_hidden_container(self, key, val):
        """
        The key is not one of the Mapfile keywords, and its
        values are a list
        """

        if key in ("layers", "classes", "styles", "symbols", "labels",
                   "outputformats", "features", "scaletokens",
                   "composites") and isinstance(val, list):
            return True
        else:
            return False


Instruction:
How would you code a function in Python 3 to
print out a nicely indented Mapfile .

Response:
def pprint(self, composites):
        """
        Print out a nicely indented Mapfile
        """

        # if only a single composite is used then cast to list
        # and allow for multiple root composites

        if composites and not isinstance(composites, list):
            composites = [composites]

        lines = []

        for composite in composites:
            type_ = composite["__type__"]
            if type_ in ("metadata", "validation"):
                # types are being parsed directly, and not as an attr of a parent
                lines += self.process_key_dict(type_, composite, level=0)
            else:
                lines += self._format(composite)

        result = str(self.newlinechar.join(lines))
        return result


Instruction:
Can you generate the documentation for the following Python 3 function
def format_value(self, attr, attr_props, value):
        if isinstance(value, bool):
            return str(value).upper()

        if any(i in ["enum"] for i in attr_props):
            if not isinstance(value, numbers.Number):
                return value.upper()  # value is from a set list, no need for quote
            else:
                return value

        if "type" in attr_props and attr_props["type"] == "string":  # and "enum" not in attr_props
            # check schemas for expressions and handle accordingly
            if self.is_expression(attr_props) and self.quoter.in_slashes(value):
                return value
            elif self.is_expression(attr_props) and (value.endswith("'i") or value.endswith('"i')):
                # for case insensitive regex
                return value
            else:
                return self.quoter.add_quotes(value)

        # expressions can be one of a string or an expression in brackets
        if any(i in ["oneOf", "anyOf"] for i in attr_props):  # and check that type string is in list
            if "oneOf" in attr_props:
                options_list = attr_props["oneOf"]
            else:
                options_list = attr_props["anyOf"]
            if self.quoter.is_string(value):
                if self.quoter.in_parenthesis(value):
                    pass
                elif attr == "expression" and self.quoter.in_braces(value):
                    # don't add quotes to list expressions such as {val1, val2}
                    pass
                elif attr != "text" and self.quoter.in_brackets(value):
                    # TEXT expressions are often "[field1]-[field2]" so need to leave quotes for these
                    pass
                elif value.startswith("NOT ") and self.quoter.in_parenthesis(value[4:]):
                    value = "NOT {}".format(value[4:])
                else:
                    value = self.check_options_list(options_list, value)

        if isinstance(value, list):
            new_values = []

            for v in value:
                if not isinstance(v, numbers.Number):
                    v = self.quoter.add_quotes(v)
                new_values.append(v)

            value = " ".join(list(map(str, new_values)))
        else:
            value = self.quoter.escape_quotes(value)

        return value

Response: Format the value for the related attribute.


Instruction:
Write a Python 3 function that can
process one of the main composite types

Response:
def process_attribute(self, type_, attr, value, level):
        """
        Process one of the main composite types (see the type_ value)
        """

        attr_props = self.get_attribute_properties(type_, attr)
        value = self.format_value(attr, attr_props, value)
        line = self.__format_line(self.whitespace(level, 1), attr.upper(), value)

        return line


Instruction:
Can you generate the documentation for the following Python 3 function
def process_composite_comment(self, level, comments, key):
        if key not in comments:
            comment = ""
        else:
            value = comments[key]
            spacer = self.whitespace(level, 0)

            if isinstance(value, list):
                comments = [self.format_comment(spacer, v) for v in value]
                comment = self.newlinechar.join(comments)
            else:
                comment = self.format_comment(spacer, value)

        return comment

Response: Process comments for composites such as MAP LAYER etc.


Instruction:
How would you code a function in Python 3 to
create an animation from a list of image files.

Response:
def create_animation(img_files):
    """
    See http://pillow.readthedocs.io/en/4.2.x/handbook/image-file-formats.html?highlight=append_images#saving
    """

    open_images = []

    for fn in img_files:
        print(fn)
        im = Image.open(fn)
        open_images.append(im)

    im = open_images[0]
    im.save(r"C:\temp\animation.gif", save_all=True, append_images=open_images[1:], duration=120, loop=100, optimize=True)


Instruction:
Create a Python 3 function to
parse a MapServer Mapfile and returns a list of all possible Mapfiles or lists of composites

Response:
def start(self, children):
        """
        Parses a MapServer Mapfile
        Parsing of partial Mapfiles or lists of composites is also possible
        """

        composites = []

        for composite_dict in children:
            if False and self.include_position:
                key_token = composite_dict[1]
                key_name = key_token.value.lower()
                composites_position = self.get_position_dict(composite_dict)
                composites_position[key_name] = self.create_position_dict(key_token, None)

            composites.append(composite_dict)

        # only return a list when there are multiple root composites (e.g.
        # several CLASSes)
        if len(composites) == 1:
            return composites[0]
        else:
            return composites


Instruction:
Can you tell what is the following Python 3 function doing
def composite(self, t):
        if len(t) == 1:
            return t[0]  # metadata and values - already processed

        key_token = t[0][0]
        attribute_dicts = t[1]

        if not isinstance(attribute_dicts, list):
            # always handle a list of attributes
            attribute_dicts = [attribute_dicts]

        key_name = self.key_name(key_token)
        composite_dict = CaseInsensitiveOrderedDict(CaseInsensitiveOrderedDict)
        composite_dict["__type__"] = key_name

        if self.include_position:
            pd = self.create_position_dict(key_token, None)
            composite_dict["__position__"] = pd

        if self.include_comments:
            comments_dict = composite_dict["__comments__"] = OrderedDict()

        for d in attribute_dicts:
            keys = d.keys()
            if "__type__" in keys:
                k = d["__type__"]
                if k in SINGLETON_COMPOSITE_NAMES:
                    composite_dict[k] = d
                else:
                    plural_key = self.plural(k)
                    if plural_key not in composite_dict:
                        composite_dict[plural_key] = []

                    composite_dict[plural_key].append(d)
            else:
                #  simple attribute
                pos = d.pop("__position__")
                d.pop("__tokens__", None)  # tokens are no longer needed now we have the positions
                comments = d.pop("__comments__", None)

                key_name = self.get_single_key(d)

                if key_name == "config":
                    # there may be several config dicts - one for each setting
                    if key_name not in composite_dict:
                        # create an initial OrderedDict
                        composite_dict[key_name] = CaseInsensitiveOrderedDict(CaseInsensitiveOrderedDict)
                    # populate the existing config dict
                    cfg_dict = composite_dict[key_name]
                    cfg_dict.update(d[key_name])

                    if self.include_position:
                        if key_name not in pd:
                            pd[key_name] = OrderedDict()

                        subkey_name = self.get_single_key(d[key_name])
                        pd[key_name][subkey_name] = pos

                elif key_name == "points":
                    if key_name not in composite_dict:
                        composite_dict[key_name] = d[key_name]
                    else:
                        # if points are already in a feature then
                        # allow for multipart features in a nested list
                        existing_points = composite_dict[key_name]

                        def depth(L):
                            return isinstance(L, (tuple, list)) and max(map(depth, L)) + 1

                        if depth(existing_points) == 2:
                            composite_dict[key_name] = [existing_points]

                        if key_name not in composite_dict:
                            composite_dict[key_name] = []
                        composite_dict[key_name].append(d[key_name])

                    if self.include_position:
                        if key_name not in pd:
                            pd[key_name] = pos
                        else:
                            existing_pos = pd[key_name]
                            if isinstance(existing_pos, dict):
                                pd[key_name] = [existing_pos]
                            pd[key_name].append(pos)

                elif key_name in REPEATED_KEYS:
                    if key_name not in composite_dict:
                        composite_dict[key_name] = []

                    composite_dict[key_name].append(d[key_name])

                    if self.include_position:
                        if key_name not in pd:
                            pd[key_name] = []
                        pd[key_name].append(pos)

                else:
                    assert len(d.items()) == 1
                    if self.include_position:
                        # hoist position details to composite
                        pd[key_name] = pos
                    if self.include_comments and comments:
                        # hoist comments to composite
                        comments_dict[key_name] = comments

                    composite_dict[key_name] = d[key_name]

        return composite_dict

Response: Handle the composite types e. g. CLASS.. END