Instruction:
Write a Python 3 function that can
persist d into the db

Response:
def insert(self, schema, fields, **kwargs):
        """
        Persist d into the db

        schema -- Schema()
        fields -- dict -- the values to persist

        return -- int -- the primary key of the row just inserted
        """
        r = 0

        with self.connection(**kwargs) as connection:
            kwargs['connection'] = connection
            try:
                with self.transaction(**kwargs):
                    r = self._insert(schema, fields, **kwargs)

            except Exception as e:
                exc_info = sys.exc_info()
                if self.handle_error(schema, e, **kwargs):
                    r = self._insert(schema, fields, **kwargs)
                else:
                    self.raise_error(e, exc_info)

        return r


Instruction:
Write a Python 3 function for
updating the fields of the record that match query. fields_where

Response:
def update(self, schema, fields, query, **kwargs):
        """
        Persist the query.fields into the db that match query.fields_where

        schema -- Schema()
        fields -- dict -- the values to persist
        query -- Query() -- will be used to create the where clause

        return -- int -- how many rows where updated
        """
        with self.connection(**kwargs) as connection:
            kwargs['connection'] = connection
            try:
                with self.transaction(**kwargs):
                    r = self._update(schema, fields, query, **kwargs)

            except Exception as e:
                exc_info = sys.exc_info()
                if self.handle_error(schema, e, **kwargs):
                    r = self._update(schema, fields, query, **kwargs)
                else:
                    self.raise_error(e, exc_info)

        return r


Instruction:
Given the following Python 3 function, write the documentation
def _get_query(self, callback, schema, query=None, *args, **kwargs):
        if not query: query = Query()

        ret = None
        with self.connection(**kwargs) as connection:
            kwargs['connection'] = connection
            try:
                if connection.in_transaction():
                    # we wrap SELECT queries in a transaction if we are in a transaction because
                    # it could cause data loss if it failed by causing the db to discard
                    # anything in the current transaction if the query isn't wrapped,
                    # go ahead, ask me how I know this
                    with self.transaction(**kwargs):
                        ret = callback(schema, query, *args, **kwargs)

                else:
                    ret = callback(schema, query, *args, **kwargs)

            except Exception as e:
                exc_info = sys.exc_info()
                if self.handle_error(schema, e, **kwargs):
                    ret = callback(schema, query, *args, **kwargs)
                else:
                    self.raise_error(e, exc_info)

        return ret

Response: wrapper around all the get queries


Instruction:
Create a Python 3 function to
get one row from the db

Response:
def get_one(self, schema, query=None, **kwargs):
        """
        get one row from the db matching filters set in query

        schema -- Schema()
        query -- Query()

        return -- dict -- the matching row
        """
        ret = self._get_query(self._get_one, schema, query, **kwargs)
        if not ret: ret = {}
        return ret


Instruction:
How would you code a function in Python 3 to
get matching rows from the db

Response:
def get(self, schema, query=None, **kwargs):
        """
        get matching rows from the db matching filters set in query

        schema -- Schema()
        query -- Query()

        return -- list -- a list of matching dicts
        """
        ret = self._get_query(self._get, schema, query, **kwargs)
        if not ret: ret = []
        return ret


Instruction:
Write a Python 3 function that can
wrap around the module s logger. log

Response:
def log(self, format_str, *format_args, **log_options):
        """
        wrapper around the module's logger

        format_str -- string -- the message to log
        *format_args -- list -- if format_str is a string containing {}, then format_str.format(*format_args) is ran
        **log_options --
            level -- something like logging.DEBUG
        """
        if isinstance(format_str, Exception):
            logger.exception(format_str, *format_args)
        else:
            log_level = log_options.get('level', logging.DEBUG)
            if logger.isEnabledFor(log_level):
                if format_args:
                    logger.log(log_level, format_str.format(*format_args))
                else:
                    logger.log(log_level, format_str)


Instruction:
Can you generate the documentation for the following Python 3 function
def raise_error(self, e, exc_info=None):
        if not exc_info:
            exc_info = sys.exc_info()

        if not isinstance(e, InterfaceError):
            # allow python's built in errors to filter up through
            # https://docs.python.org/2/library/exceptions.html
            #if not hasattr(exceptions, e.__class__.__name__):
            if not hasattr(builtins, e.__class__.__name__):
                e = self._create_error(e, exc_info)

        reraise(e.__class__, e, exc_info[2])

Response: this is just a wrapper to make the passed in exception an InterfaceError


Instruction:
Make a summary of the following Python 3 code
def _query(self, query_str, query_args=None, **query_options):
        ret = True
        # http://stackoverflow.com/questions/6739355/dictcursor-doesnt-seem-to-work-under-psycopg2
        connection = query_options.get('connection', None)
        with self.connection(connection) as connection:
            cur = connection.cursor()
            ignore_result = query_options.get('ignore_result', False)
            count_result = query_options.get('count_result', False)
            one_result = query_options.get('fetchone', query_options.get('one_result', False))
            cursor_result = query_options.get('cursor_result', False)

            try:
                if query_args:
                    self.log("{}{}{}", query_str, os.linesep, query_args)
                    cur.execute(query_str, query_args)
                else:
                    self.log(query_str)
                    cur.execute(query_str)

                if cursor_result:
                    ret = cur

                elif not ignore_result:
                    if one_result:
                        ret = self._normalize_result_dict(cur.fetchone())
                    elif count_result:
                        ret = cur.rowcount
                    else:
                        ret = self._normalize_result_list(cur.fetchall())

            except Exception as e:
                self.log(e)
                raise

            return ret

Response: Query the database for the related object.


Instruction:
Given the following Python 3 function, write the documentation
def get_SQL(self, schema, query, **sql_options):
        only_where_clause = sql_options.get('only_where_clause', False)
        symbol_map = {
            'in': {'symbol': 'IN', 'list': True},
            'nin': {'symbol': 'NOT IN', 'list': True},
            'is': {'symbol': '=', 'none_symbol': 'IS'},
            'not': {'symbol': '!=', 'none_symbol': 'IS NOT'},
            'gt': {'symbol': '>'},
            'gte': {'symbol': '>='},
            'lt': {'symbol': '<'},
            'lte': {'symbol': '<='},
            # https://www.tutorialspoint.com/postgresql/postgresql_like_clause.htm
            # https://www.tutorialspoint.com/sqlite/sqlite_like_clause.htm
            'like': {'symbol': 'LIKE'},
            'nlike': {'symbol': 'NOT LIKE'},
        }

        query_args = []
        query_str = []

        if not only_where_clause:
            query_str.append('SELECT')
            select_fields = query.fields_select
            if select_fields:
                distinct = "DISTINCT " if select_fields.options.get("unique", False) else ""
                select_fields_str = distinct + ',{}'.format(os.linesep).join(
                    (self._normalize_name(f) for f in select_fields.names())
                )
            else:
                select_fields_str = "*"

            if sql_options.get('count_query', False):
                query_str.append('  count({}) as ct'.format(select_fields_str))

            else:
                query_str.append('  {}'.format(select_fields_str))

            query_str.append('FROM')
            query_str.append("  {}".format(self._normalize_table_name(schema)))

        if query.fields_where:
            query_str.append('WHERE')

            for i, field in enumerate(query.fields_where):
                if i > 0: query_str.append('AND')

                field_str = ''
                field_args = []
                sd = symbol_map[field[0]]

                # field[0], field[1], field[2], field[3]
                _, field_name, field_val, field_kwargs = field
                field_str, field_args = self._normalize_val_SQL(
                    schema,
                    sd,
                    field_name,
                    field_val,
                    field_kwargs
                )

                query_str.append('  {}'.format(field_str))
                query_args.extend(field_args)

        if query.fields_sort:
            query_sort_str = []
            query_str.append('ORDER BY')
            for field in query.fields_sort:
                sort_dir_str = 'ASC' if field[0] > 0 else 'DESC'
                if field[2]:
                    field_sort_str, field_sort_args = self._normalize_sort_SQL(field[1], field[2], sort_dir_str)
                    query_sort_str.append(field_sort_str)
                    query_args.extend(field_sort_args)

                else:
                    query_sort_str.append('  {} {}'.format(field[1], sort_dir_str))

            query_str.append(',{}'.format(os.linesep).join(query_sort_str))

        if query.bounds:
            offset = query.bounds.offset
            limit = 1 if sql_options.get('one_query', False) else query.bounds.limit
            query_str.append('LIMIT {} OFFSET {}'.format(
                limit,
                offset
            ))

        query_str = os.linesep.join(query_str)
        return query_str, query_args

Response: convert the generic Query instance into SQL


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _set_all_tables(self, schema, **kwargs):
        with self.transaction(**kwargs) as connection:
            kwargs['connection'] = connection
            # go through and make sure all foreign key referenced tables exist
            for field_name, field_val in schema.fields.items():
                s = field_val.schema
                if s:
                    self._set_all_tables(s, **kwargs)

            # now that we know all fk tables exist, create this table
            self.set_table(schema, **kwargs)

        return True

Response:
This method will go through all tables referenced by a
        and create all tables that are not referenced by any foreign key referenced by a
       .


Instruction:
Create a Python 3 function for
generating a random path for uploads.

Response:
def random_path(instance, filename):
    """ Random path generator for uploads, specify this for upload_to= argument of FileFields
    """
    # Split the uuid into two parts so that we won't run into subdirectory count limits. First part has 3 hex chars,
    #  thus 4k possible values.
    uuid_hex = get_uuid()
    return os.path.join(uuid_hex[:3], uuid_hex[3:], filename)


Instruction:
Can you write a function in Python 3 where it
initializes metrics, must be invoked at least once prior to invoking any other method.

Response:
def initialize(signal_number=DEFAULT_TIMER_SIGNAL_NUMBER,
               update_period_s=DEFAULT_UPDATE_PERIOD_S):
    """Initialize metrics, must be invoked at least once prior to invoking any
    other method."""
    global initialized
    if initialized:
        return
    initialized = True
    uwsgi.add_timer(signal_number, update_period_s)
    uwsgi.register_signal(signal_number, MULE, emit)


Instruction:
Here you have a function in Python 3, explain what it does
def emit(_):
    if not initialized:
        raise NotInitialized

    view = {
        'version': __version__,
        'counters': {},
        'gauges': {},
        'histograms': {},
        'meters': {},
        'timers': {},
    }

    for (ty, module, name), metric in six.iteritems(all_metrics):
        view[ty]['%s.%s' % (module, name)] = metric.view()

    marshalled_view = marshal.dumps(view)
    if len(marshalled_view) > MAX_MARSHALLED_VIEW_SIZE:
        log.warn(
            'Marshalled length too large, got %d, max %d. '
            'Try recording fewer metrics or increasing '
            'MAX_MARSHALLED_VIEW_SIZE'
            % (len(marshalled_view), MAX_MARSHALLED_VIEW_SIZE))
        return
    marshalled_metrics_mmap.seek(0)
    try:
        # Reading and writing to/from an mmap'ed buffer is not guaranteed
        # to be atomic, so we must serialize access to it.
        uwsgi.lock()
        marshalled_metrics_mmap.write(marshalled_view)
    finally:
        uwsgi.unlock()

Response: Serialize metrics to the memory mapped buffer.


Instruction:
Can you implement a function in Python 3 that
gets a dictionary representation of current metrics.

Response:
def view():
    """Get a dictionary representation of current metrics."""
    if not initialized:
        raise NotInitialized

    marshalled_metrics_mmap.seek(0)
    try:
        uwsgi.lock()
        marshalled_view = marshalled_metrics_mmap.read(
            MAX_MARSHALLED_VIEW_SIZE)
    finally:
        uwsgi.unlock()
    return marshal.loads(marshalled_view)


Instruction:
How would you explain what the following Python 3 function does
def timer(module, name, delta, duration_units='milliseconds'):
    timer = get_metric('timers', module, name, Timer(duration_units))
    timer.update(delta)

Response: Record a timing delta


Instruction:
Create a Python 3 function for
recording a value in a histogram

Response:
def histogram(module, name, value):
    """
    Record a value in a histogram:
    ::

        histogram(__name__, 'my_histogram', len(queue))
    """
    histogram = get_metric('histograms', module, name, Histogram())
    histogram.update(value)


Instruction:
Here you have a function in Python 3, explain what it does
def counter(module, name, count=1):
    counter = get_metric('counters', module, name, Counter())
    counter.inc(count)

Response: Record an event s occurence in a counter


Instruction:
Here you have a function in Python 3, explain what it does
def meter(module, name, count=1):
    meter = get_metric('meters', module, name, Meter())
    meter.mark(count)

Response: Record a meter event rate


Instruction:
Given the following Python 3 function, write the documentation
def exec_command_on_nodes(nodes, cmd, label, conn_params=None):

    if isinstance(nodes, BASESTRING):
        nodes = [nodes]

    if conn_params is None:
        conn_params = DEFAULT_CONN_PARAMS

    logger.debug("Running %s on %s ", label, nodes)
    remote = ex.get_remote(cmd, nodes, conn_params)
    remote.run()
    if not remote.finished_ok:
        raise Exception('An error occcured during remote execution')
    return remote

Response: Execute a command on a set of nodes.


Instruction:
Explain what the following Python 3 code does
def convert_out(self, obj):
        newobj = super(ProcessedImageProduct, self).convert_out(obj)
        if newobj:
            hdulist = newobj.open()
            hdr = hdulist[0].header
            if 'EMIRUUID' not in hdr:
                hdr['EMIRUUID'] = str(uuid.uuid1())
        return newobj

Response: Write EMIRUUID header on reduction


Instruction:
Given the following Python 3 function, write the documentation
def verify_client(endpoint_context, request, authorization_info):

    if not authorization_info:
        if 'client_id' in request and 'client_secret' in request:
            auth_info = ClientSecretPost(endpoint_context).verify(request)
            auth_info['method'] = 'client_secret_post'
        elif 'client_assertion' in request:
            auth_info = JWSAuthnMethod(endpoint_context).verify(request)
            #  If symmetric key was used
            # auth_method = 'client_secret_jwt'
            #  If asymmetric key was used
            auth_info['method'] = 'private_key_jwt'
        elif 'access_token' in request:
            auth_info = BearerBody(endpoint_context).verify(request)
            auth_info['method'] = 'bearer_body'
        else:
            raise UnknownOrNoAuthnMethod()
    else:
        if authorization_info.startswith('Basic '):
            auth_info = ClientSecretBasic(endpoint_context).verify(
                request, authorization_info)
            auth_info['method'] = 'client_secret_basic'
        elif authorization_info.startswith('Bearer '):
            auth_info = BearerHeader(endpoint_context).verify(
                request, authorization_info)
            auth_info['method'] = 'bearer_header'
        else:
            raise UnknownOrNoAuthnMethod(authorization_info)

    try:
        client_id = auth_info['client_id']
    except KeyError:
        client_id = ''
        try:
            _token = auth_info['token']
        except KeyError:
            logger.warning('Unknown client ID')
        else:
            sinfo = endpoint_context.sdb[_token]
            auth_info['client_id'] = sinfo['authn_req']['client_id']
    else:
        try:
            _cinfo = endpoint_context.cdb[client_id]
        except KeyError:
            raise ValueError('Unknown Client ID')
        else:
            if isinstance(_cinfo,str):
                try:
                    _cinfo = endpoint_context.cdb[_cinfo]
                except KeyError:
                    raise ValueError('Unknown Client ID')

            try:
                valid_client_info(_cinfo)
            except KeyError:
                logger.warning('Client registration has timed out')
                raise ValueError('Not valid client')
            else:
                # check that the expected authz method was used
                try:
                    endpoint_context.cdb[client_id]['auth_method'][
                        request.__class__.__name__] = auth_info['method']
                except KeyError:
                    try:
                        endpoint_context.cdb[client_id]['auth_method'] = {
                            request.__class__.__name__: auth_info['method']}
                    except KeyError:
                        pass

    return auth_info

Response: Verify the client ID client ID and client authentication method and access token.


Instruction:
Write a Python 3 function that can
generate a random ID of given length

Response:
def random_id(length):
    """Generates a random ID of given length"""

    def char():
        """Generate single random char"""

        return random.choice(string.ascii_letters + string.digits)

    return "".join(char() for _ in range(length))


Instruction:
Can you generate the documentation for the following Python 3 function
def delayed_close(closable):

    close = getattr(closable, "close", None)
    if close:
        # we do not want the library to close file in case we need to
        # resume, hence make close a no-op
        # pylint: disable=unused-argument
        def replacement_close(*args, **kw):
            """ No op """
            pass

        # pylint: enable=unused-argument
        setattr(closable, "close", replacement_close)
    try:
        yield closable
    finally:
        if close:
            setattr(closable, "close", close)
            closable.close()

Response: Context manager that closes the closable object.


Instruction:
Can you tell what is the following Python 3 function doing
def map_sid2uid(self, sid, uid):
        self.set('sid2uid', sid, uid)
        self.set('uid2sid', uid, sid)

Response: Store the connection between a Session ID and a User ID


Instruction:
Here you have a function in Python 3, explain what it does
def map_sid2sub(self, sid, sub):
        self.set('sid2sub', sid, sub)
        self.set('sub2sid', sub, sid)

Response: Store the connection between a Session ID and a Subject ID.


Instruction:
Create a Python 3 function to
find all subject identifiers that are connected to a User ID.

Response:
def get_subs_by_uid(self, uid):
        """
        Find all subject identifiers that is connected to a User ID.

        :param uid: A User ID
        :return: A set of subject identifiers
        """
        res = set()
        for sid in self.get('uid2sid', uid):
            res |= set(self.get('sid2sub', sid))
        return res


Instruction:
Implement a function in Python 3 to
remove the connection between a session ID and a Subject ID.

Response:
def remove_sid2sub(self, sid, sub):
        """
        Remove the connection between a session ID and a Subject

        :param sid: Session ID
        :param sub: Subject identifier
´       """
        self.remove('sub2sid', sub, sid)
        self.remove('sid2sub', sid, sub)


Instruction:
Make a summary of the following Python 3 code
def remove_sid2uid(self, sid, uid):
        self.remove('uid2sid', uid, sid)
        self.remove('sid2uid', sid, uid)

Response: Removes the connection between a session ID and a SubjectID.


Instruction:
Explain what the following Python 3 code does
def remove_session_id(self, sid):
        for uid in self.get('sid2uid', sid):
            self.remove('uid2sid', uid, sid)
        self.delete('sid2uid', sid)

        for sub in self.get('sid2sub', sid):
            self.remove('sub2sid', sub, sid)
        self.delete('sid2sub', sid)

Response: Remove all references to a specific Session ID


Instruction:
Implement a Python 3 function for
removing all references to a specific User ID

Response:
def remove_uid(self, uid):
        """
        Remove all references to a specific User ID

        :param uid: A User ID
        """
        for sid in self.get('uid2sid', uid):
            self.remove('sid2uid', sid, uid)
        self.delete('uid2sid', uid)


Instruction:
Implement a function in Python 3 to
remove all references to a specific Subject ID

Response:
def remove_sub(self, sub):
        """
        Remove all references to a specific Subject ID

        :param sub: A Subject ID
        """
        for _sid in self.get('sub2sid', sub):
            self.remove('sid2sub', _sid, sub)
        self.delete('sub2sid', sub)


Instruction:
Write a Python 3 script to
start a new experiment

Response:
def up(force=True, env=None, **kwargs):
    "Starts a new experiment"
    inventory = os.path.join(os.getcwd(), "hosts")
    conf = Configuration.from_dictionnary(provider_conf)
    provider = Enos_vagrant(conf)
    roles, networks = provider.init()
    check_networks(roles, networks)
    env["roles"] = roles
    env["networks"] = networks


Instruction:
Write a Python 3 function that can
return public attributes of class cls.

Response:
def get_public_attributes(cls, as_list=True):
    """
    Return class attributes that are neither private nor magic.

    :param cls: class
    :param as_list: [True] set to False to return generator
    :return: only public attributes of class
    """
    attrs = (a for a in dir(cls) if not a.startswith('_'))
    if as_list:
        return list(attrs)
    return attrs


Instruction:
How would you code a function in Python 3 to
register new items in the internal registry.

Response:
def register(self, newitems, *args, **kwargs):
        """
        Register newitems in registry.

        :param newitems: New items to add to registry. When registering new
            items, keys are not allowed to override existing keys in the
            registry.
        :type newitems: mapping
        :param args: Positional arguments with meta data corresponding to order
            of meta names class attributes
        :param kwargs: Maps of corresponding meta for new keys. Each set of
            meta keys must be a subset of the new item keys.
        :raises:
            :exc:`~simkit.core.exceptions.DuplicateRegItemError`,
            :exc:`~simkit.core.exceptions.MismatchRegMetaKeysError`
        """
        newkeys = newitems.viewkeys()  # set of the new item keys
        if any(self.viewkeys() & newkeys):  # duplicates
            raise DuplicateRegItemError(self.viewkeys() & newkeys)
        self.update(newitems)  # register new item
        # update meta fields
        kwargs.update(zip(self.meta_names, args))
        for k, v in kwargs.iteritems():
            meta = getattr(self, k)  # get the meta attribute
            if v:
                if not v.viewkeys() <= newkeys:
                    raise MismatchRegMetaKeysError(newkeys - v.viewkeys())
                meta.update(v)


Instruction:
How would you implement a function in Python 3 that
removes items from the registry.

Response:
def unregister(self, items):
        """
        Remove items from registry.

        :param items:
        """
        items = _listify(items)
        # get all members of Registry except private, special or class
        meta_names = (m for m in vars(self).iterkeys()
                      if (not m.startswith('_') and m not in dir(Registry)))
        # check that meta names matches
        # FIXME: this is so lame. replace this with something more robust
        for m in meta_names:
            if m not in self.meta_names:
                raise AttributeError('Meta name %s not listed.')
        # pop items from Registry and from meta
        for it in items:
            if it in self:
                self.pop(it)
            for m in (getattr(self, m_) for m_ in self.meta_names):
                if it in m:
                    m.pop(it)


Instruction:
Here you have a function in Python 3, explain what it does
def set_meta(mcs, bases, attr):
        # pop the meta class from the attributes
        meta = attr.pop(mcs._meta_cls, types.ClassType(mcs._meta_cls, (), {}))
        # get a list of the meta public class attributes
        meta_attrs = get_public_attributes(meta)
        # check all bases for meta
        for base in bases:
            base_meta = getattr(base, mcs._meta_cls, None)
            # skip if base has no meta
            if base_meta is None:
                continue
            # loop over base meta
            for a in get_public_attributes(base_meta, as_list=False):
                # skip if already in meta
                if a in meta_attrs:
                    continue
                # copy meta-option attribute from base
                setattr(meta, a, getattr(base_meta, a))
        attr[mcs._meta_attr] = meta  # set _meta combined from bases
        return attr

Response: Sets the meta attribute of the class with the meta class from bases and adds the meta - option attributes to the attributes of the class with the meta class from attributes.


Instruction:
Given the following Python 3 function, write the documentation
def set_param_file_or_parameters(mcs, attr):
        meta = attr[mcs._meta_attr]  # look for parameter file path in meta
        cls_path = getattr(meta, mcs._path_attr, None)
        cls_file = getattr(meta, mcs._file_attr, None)
        # read parameters
        attr[mcs._param_attr] = {}
        attr[mcs._param_file] = None
        # read parameters from file
        if None not in [cls_path, cls_file]:
            param_file = os.path.join(cls_path, cls_file)
            attr[mcs._param_file] = param_file
            # read and load JSON parameter map file as "parameters"
            with open(param_file, 'r') as param_file:
                file_params = json.load(param_file)
            # update meta from file
            for k, v in file_params.pop(mcs._meta_cls, {}).iteritems():
                setattr(meta, k, v)
            # dictionary of parameters for reading source file
            attr[mcs._param_attr] = {
                k: mcs._param_cls(**v) for k, v in file_params.iteritems()
            }
        # get parameters from class
        parameters = dict.fromkeys(
            k for k, v in attr.iteritems() if isinstance(v, Parameter)
        )
        # update parameters
        for k in parameters:
            attr[mcs._param_attr][k] = attr.pop(k)
        return attr

Response: Set parameters from a parameter file or from a parameter file.


Instruction:
Explain what the following Python 3 code does
def factory(cls, **kwargs):
    for name, obj in inspect.getmembers(sys.modules[__name__]):
        if inspect.isclass(obj) and issubclass(obj, UserAuthnMethod):
            try:
                if obj.__name__ == cls:
                    return obj(**kwargs)
            except AttributeError:
                pass

Response: Returns an instance of the class cls if it is known otherwise None.


Instruction:
Can you generate the documentation for the following Python 3 function
def register(self, new_formulas, *args, **kwargs):
        kwargs.update(zip(self.meta_names, args))
        # call super method, meta must be passed as kwargs!
        super(FormulaRegistry, self).register(new_formulas, **kwargs)

Response: Register new formulas and meta data.


Instruction:
How would you implement a function in Python 3 that
imports formulas specified in parameters.

Response:
def import_formulas(self):
        """
        Import formulas specified in :attr:`parameters`.

        :returns: formulas
        :rtype: dict
        """
        # TODO: unit tests!
        # TODO: move this to somewhere else and call it "importy", maybe
        # core.__init__.py since a lot of modules might use it.
        module = self.meta.module  # module read from parameters
        package = getattr(self.meta, 'package', None)  # package read from meta
        name = package + module if package else module  # concat pkg + name
        path = getattr(self.meta, 'path', None)  # path read from parameters
        # import module using module and package
        mod = None
        # SEE ALSO: http://docs.python.org/2/library/imp.html#examples
        try:
            # fast path: see if module was already imported
            mod = sys.modules[name]
        except KeyError:
            try:
                # import module specified in parameters
                mod = importlib.import_module(module, package)
            except ImportError as err:
                if not path:
                    msg = ('%s could not be imported either because it was not '
                           'on the PYTHONPATH or path was not given.')
                    LOGGER.exception(msg, name)
                    raise err
                else:
                    # import module using path
                    # expand ~, environmental variables and make path absolute
                    if not os.path.isabs(path):
                        path = os.path.expanduser(os.path.expandvars(path))
                        path = os.path.abspath(path)
                    # paths must be a list
                    paths = [path]
                    # imp does not find hierarchical module names, find and load
                    # packages recursively, then load module, see last paragraph
                    # https://docs.python.org/2/library/imp.html#imp.find_module
                    pname = ''  # full dotted name of package to load
                    # traverse namespace
                    while name:
                        # if dot in name get first package
                        if '.' in name:
                            pkg, name = name.split('.', 1)
                        else:
                            pkg, name = name, None  # pkg is the module
                        # Find package or module by name and path
                        fp, filename, desc = imp.find_module(pkg, paths)
                        # full dotted name of package to load
                        pname = pkg if not pname else '%s.%s' % (pname, pkg)
                        LOGGER.debug('package name: %s', pname)
                        # try to load the package or module
                        try:
                            mod = imp.load_module(pname, fp, filename, desc)
                        finally:
                            if fp:
                                fp.close()
                        # append package paths for imp.find_module
                        if name:
                            paths = mod.__path__
        formulas = {}  # an empty list of formulas
        formula_param = self.parameters  # formulas key
        # FYI: iterating over dictionary is equivalent to iterkeys()
        if isinstance(formula_param, (list, tuple, dict)):
            # iterate through formulas
            for f in formula_param:
                formulas[f] = getattr(mod, f)
        elif isinstance(formula_param, basestring):
            # only one formula
            # FYI: use basestring to test for str and unicode
            # SEE: http://docs.python.org/2/library/functions.html#basestring
            formulas[formula_param] = getattr(mod, formula_param)
        else:
            # autodetect formulas assuming first letter is f
            formulas = {f: getattr(mod, f) for f in dir(mod) if f[:2] == 'f_'}
        if not len(formulas):
            for f in dir(mod):
                mod_attr = getattr(mod, f)
                if inspect.isfunction(mod_attr):
                    formulas[f] = mod_attr
        return formulas


Instruction:
Can you generate the documentation for the following Python 3 function
def create_blazar_client(config, session):
    return blazar_client.Client(session=session,
                                service_type="reservation",
                                region_name=os.environ["OS_REGION_NAME"])

Response: Create a new client for the reservation if nescessary.


Instruction:
Here you have a function in Python 3, explain what it does
def reconnecting(count=None, backoff=None):
    # we get trixxy here so we can manipulate these values in the wrapped function,
    # this is one of the first times I wish we were on Python 3
    # http://stackoverflow.com/a/9264845/5006
    reconn_params = {
        "count": count,
        "backoff": backoff
    }

    def retry_decorator(func):
        @wraps(func)
        def wrapper(self, *args, **kwargs):

            count = reconn_params["count"]
            backoff = reconn_params["backoff"]

            if count is None:
                count = self.connection_config.options.get('reconnect_attempts', 3)

            if backoff is None:
                backoff = self.connection_config.options.get('reconnect_backoff', 1.0)

            count = int(count)
            backoff = float(backoff)

            for attempt in range(1, count + 1):
                try:
                    backoff_seconds = float(attempt - 1) * backoff
                    if backoff_seconds:
                        logger.debug("sleeping {} seconds before attempt {}".format(
                            backoff_seconds,
                            attempt
                        ))
                    time.sleep(backoff_seconds)

                    return func(self, *args, **kwargs)

                except InterfaceError as e:
                    e_msg = str(e.e)
                    # TODO -- this gets us by SQLite and Postgres, but might not
                    # work in the future, so this needs to be a tad more robust
                    if "closed" in e_msg.lower():
                        if attempt == count:
                            logger.debug("all {} attempts failed".format(count))
                            raise
                        else:
                            logger.debug("attempt {}/{} failed, retrying".format(
                                attempt,
                                count
                            ))

                    else:
                        raise

        return wrapper

    return retry_decorator

Response:
This is a very specific decorator that can be used to be used on Interface classes. It will attempt to reconnect if the connection is closed and run the same
    method again.


Instruction:
Given the following Python 3 function, write the documentation
def escape_header(val):

    if val is None:
        return None
    try:
        return quote(val, encoding="ascii", safe="/ ")
    except ValueError:
        return "utf-8''" + quote(val, encoding="utf-8", safe="/ ")

Response: Escapes a value so that it can be used in a mime header


Instruction:
Create a Python 3 function to
generate one or more streams for each name value pair.

Response:
def make_streams(name, value, boundary, encoding):
    """Generates one or more streams for each name, value pair"""

    filename = None
    mime = None

    # user passed in a special dict.
    if isinstance(value, collections.Mapping) and "name" in value and "value" in value:
        filename = value["name"]
        try:
            mime = value["mime"]
        except KeyError:
            pass
        value = value["value"]

    if not filename:
        filename = getattr(value, "name", None)
        if filename:
            filename = os.path.split(filename)[1]

    mime = mime or "application/octet-stream"

    name, filename, mime = [escape_header(v) for v in (name, filename, mime)]

    stream = BytesIO()
    stream.write("--{}\r\n".format(boundary).encode(encoding))
    if not filename:
        stream.write(
            'Content-Disposition: form-data; name="{}"\r\n'.format(name).encode(
                encoding
            )
        )
    else:
        stream.write(
            'Content-Disposition: form-data; name="{}"; filename="{}"\r\n'.format(
                name, filename
            ).encode(encoding)
        )
        if mime:
            stream.write("Content-Type: {}\r\n".format(mime).encode(encoding))
    stream.write(b"\r\n")

    if hasattr(value, "read"):
        stream.seek(0)
        return stream, value, BytesIO("\r\n".encode(encoding))

    # not a file-like object, encode headers and value in one go
    value = value if isinstance(value, (str, bytes)) else json.dumps(value)
    if isinstance(value, bytes):
        stream.write(value)
    else:
        stream.write(value.encode(encoding))
    stream.write(b"\r\n")
    stream.seek(0)
    return (stream,)


Instruction:
Here you have a function in Python 3, explain what it does
def len(self):
        # The len property is needed for requests.
        # requests checks __len__, then len
        # Since we cannot implement __len__ because python 32-bit uses 32-bit
        # sizes, we implement this instead.
        def stream_len(stream):
            """Stream length"""
            cur = stream.tell()
            try:
                stream.seek(0, 2)
                return stream.tell() - cur
            finally:
                stream.seek(cur)

        return sum(stream_len(s) for s in self.streams)

Response: Returns the length of the data stream.


Instruction:
Write a Python 3 script for
indexing a thing by the keyfnc to construct pools of things.

Response:
def mk_pools(things, keyfnc=lambda x: x):
    "Indexes a thing by the keyfnc to construct pools of things."
    pools = {}
    sthings = sorted(things, key=keyfnc)
    for key, thingz in groupby(sthings, key=keyfnc):
        pools.setdefault(key, []).extend(list(thingz))
    return pools


Instruction:
Write a Python 3 script for
picking a maximum of n things in a dict of indexed pool of things.

Response:
def pick_things(pools, key, n):
    "Picks a maximum of n things in a dict of indexed pool of things."
    pool = pools.get(key)
    if not pool:
        return []
    things = pool[:n]
    del pool[:n]
    return things


Instruction:
Can you generate a brief explanation for the following Python 3 code
def listen(room):
    def onmessage(m):
        """Print the new message and respond to it."""
        print(m)
        if m.admin or m.nick == r.user.name:
            return
        if "parrot" in m.msg.lower():
            r.post_chat("ayy lmao")
        elif m.msg.lower() in ("lol", "lel", "kek"):
            r.post_chat("*kok")
        else:
            r.post_chat(re.sub(r"\blain\b", "purpleadmin", m.msg, re.I))

    with Room(room) as r:
        r.user.change_nick("DumbParrot")
        r.add_listener("chat", onmessage)
        r.listen()

Response: Open a volafile room and start listening to it.


Instruction:
Can you implement a function in Python 3 that
computes valid scan range for provided y0_frontier lower and upper frontiers.

Response:
def nscan_minmax_frontiers(y0_frontier_lower, y0_frontier_upper,
                           resize=False):
    """Compute valid scan range for provided y0_frontier values.
    Parameters
    ----------
    y0_frontier_lower : float
        Ordinate of the lower frontier.
    y0_frontier_upper : float
        Ordinate of the upper frontier.
    resize : bool
        If True, when the limits are beyond the expected values
        [1,EMIR_NAXIS2], the values are truncated.
    Returns
    -------
    nscan_min : int
        Minimum useful scan for the image.
    nscan_max : int
        Maximum useful scan for the image.
    """

    fraction_pixel = y0_frontier_lower - int(y0_frontier_lower)
    if fraction_pixel > 0.0:
        nscan_min = int(y0_frontier_lower) + 1
    else:
        nscan_min = int(y0_frontier_lower)
    if nscan_min < 1:
        if resize:
            nscan_min = 1
        else:
            raise ValueError("nscan_min=" + str(nscan_min) + " is < 1")

    fraction_pixel = y0_frontier_upper - int(y0_frontier_upper)
    if fraction_pixel > 0.0:
        nscan_max = int(y0_frontier_upper)
    else:
        nscan_max = int(y0_frontier_upper) - 1
    if nscan_max > EMIR_NAXIS2:
        if resize:
            nscan_max = EMIR_NAXIS2
        else:
            raise ValueError("nscan_max=" + str(nscan_max) +
                             " is > NAXIS2_EMIR=" + str(EMIR_NAXIS2))

    return nscan_min, nscan_max


Instruction:
Write a Python 3 function for
returning the average DtuConfiguration instance with averaged values.

Response:
def average_dtu_configurations(list_of_objects):
    """Return DtuConfiguration instance with averaged values.

    Parameters
    ----------
    list_of_objects : python list
        List of DtuConfiguration instances to be averaged.

    Returns
    -------
    result : DtuConfiguration instance
        Object with averaged values.

    """

    result = DtuConfiguration()

    if len(list_of_objects) == 0:
        return result

    list_of_members = result.__dict__.keys()

    # compute average of all the members of the class
    for member in list_of_members:
        result.__dict__[member] = np.mean(
            [tmp_dtu.__dict__[member] for tmp_dtu in list_of_objects]
        )

    return result


Instruction:
Can you tell what is the following Python 3 function doing
def maxdiff_dtu_configurations(list_of_objects):

    result = DtuConfiguration()

    if len(list_of_objects) == 0:
        return result

    list_of_members = result.__dict__.keys()

    # compute maximum difference for each member
    for member in list_of_members:
        tmp_array = np.array(
            [tmp_dtu.__dict__[member] for tmp_dtu in list_of_objects]
        )
        minval = tmp_array.min()
        maxval = tmp_array.max()
        result.__dict__[member] = maxval - minval

    return result

Response: Returns a new DtuConfiguration instance with maximum differences.


Instruction:
Explain what the following Python 3 code does
def define_from_fits(cls, fitsobj, extnum=0):

        # read input FITS file
        with fits.open(fitsobj) as hdulist:
            image_header = hdulist[extnum].header
            return cls.define_from_header(image_header)

Response: Define class object from header information in FITS file.


Instruction:
Here you have a function in Python 3, explain what it does
def define_from_dictionary(cls, inputdict):

        self = DtuConfiguration()
        for item in self.__dict__:
            self.__dict__[item] = inputdict[item]
        return self

Response: Define class object from dictionary.


Instruction:
Can you implement a function in Python 3 that
defines class object from provided values.

Response:
def define_from_values(cls, xdtu, ydtu, zdtu, xdtu_0, ydtu_0, zdtu_0):
        """Define class object from from provided values.

        Parameters
        ----------
        xdtu : float
            XDTU fits keyword value.
        ydtu : float
            YDTU fits keyword value.
        zdtu : float
            ZDTU fits keyword value.
        xdtu_0 : float
            XDTU_0 fits keyword value.
        ydtu_0 : float
            YDTU_0 fits keyword value.
        zdtu_0 : float
            ZDTU_0 fits keyword value.

        """

        self = DtuConfiguration()
        # define DTU variables
        self.xdtu = xdtu
        self.ydtu = ydtu
        self.zdtu = zdtu
        self.xdtu_0 = xdtu_0
        self.ydtu_0 = ydtu_0
        self.zdtu_0 = zdtu_0
        return self


Instruction:
Write a Python 3 script for
checking that all the members are within the specified absolute error.

Response:
def closeto(self, other, abserror):
        """Check that all the members are equal within provided absolute error.

        Parameters
        ----------
        other : DtuConfiguration object
            DTU configuration instance to be compared with self.
        abserror : float
            Absolute maximum allowed error.

        Returns
        -------
        result : bool
            True is all members are within the specified maximum
            absolute error

        """

        result = \
            (abs(self.xdtu - other.xdtu) <= abserror) and \
            (abs(self.ydtu - other.ydtu) <= abserror) and \
            (abs(self.zdtu - other.zdtu) <= abserror) and \
            (abs(self.xdtu_0 - other.xdtu_0) <= abserror) and \
            (abs(self.ydtu_0 - other.ydtu_0) <= abserror) and \
            (abs(self.zdtu_0 - other.zdtu_0) <= abserror)
        return result


Instruction:
Here you have a function in Python 3, explain what it does
def outdict(self, ndigits=3):

        output = self.__dict__.copy()
        for item in output:
            output[item] = round(output[item], ndigits)
        return output

Response: Return dictionary structure rounded to a given precision.


Instruction:
Here you have a function in Python 3, explain what it does
def build_endpoints(conf, endpoint_context, client_authn_method, issuer):

    if issuer.endswith('/'):
        _url = issuer[:-1]
    else:
        _url = issuer

    endpoint = {}
    for name, spec in conf.items():
        try:
            kwargs = spec['kwargs']
        except KeyError:
            kwargs = {}

        if isinstance(spec['class'], str):
            _instance = importer(spec['class'])(
                endpoint_context=endpoint_context, **kwargs)
        else:
            _instance = spec['class'](endpoint_context=endpoint_context,
                                      **kwargs)

        _instance.endpoint_path = spec['path']
        _instance.full_path = '{}/{}'.format(_url, spec['path'])
        if 'provider_info' in spec:
            _instance.provider_info = spec['provider_info']

        try:
            _client_authn_method = kwargs['client_authn_method']
        except KeyError:
            _instance.client_auth_method = client_authn_method
        else:
            _instance.client_auth_method = _client_authn_method

        endpoint[name] = _instance

    return endpoint

Response: Builds the list of endpoints for the given configuration.


Instruction:
Can you tell what is the following Python 3 function doing
def lv_pack(*args):
    s = []
    for a in args:
        s.append('{}:{}'.format(len(a), a))
    return ''.join(s)

Response: Serializes using length : value format


Instruction:
Make a summary of the following Python 3 code
def lv_unpack(txt):
    txt = txt.strip()
    res = []
    while txt:
        l, v = txt.split(':', 1)
        res.append(v[:int(l)])
        txt = v[int(l):]
    return res

Response: Deserializes a string of the length : value format


Instruction:
Can you create a Python 3 function that
reserves and deploys the vagrant boxes.

Response:
def init(self, force_deploy=False):
        """Reserve and deploys the vagrant boxes.

        Args:
            force_deploy (bool): True iff new machines should be started
        """
        machines = self.provider_conf.machines
        networks = self.provider_conf.networks
        _networks = []
        for network in networks:
            ipnet = IPNetwork(network.cidr)
            _networks.append({
                "netpool": list(ipnet)[10:-10],
                "cidr": network.cidr,
                "roles": network.roles,
                "gateway": ipnet.ip
            })

        vagrant_machines = []
        vagrant_roles = {}
        j = 0
        for machine in machines:
            for _ in range(machine.number):
                vagrant_machine = {
                    "name": "enos-%s" % j,
                    "cpu": machine.flavour_desc["core"],
                    "mem": machine.flavour_desc["mem"],
                    "ips": [n["netpool"].pop() for n in _networks],
                }
                vagrant_machines.append(vagrant_machine)
                # Assign the machines to the right roles
                for role in machine.roles:
                    vagrant_roles.setdefault(role, []).append(vagrant_machine)
                j = j + 1

        logger.debug(vagrant_roles)

        loader = FileSystemLoader(searchpath=TEMPLATE_DIR)
        env = Environment(loader=loader, autoescape=True)
        template = env.get_template('Vagrantfile.j2')
        vagrantfile = template.render(machines=vagrant_machines,
                                      provider_conf=self.provider_conf)
        vagrantfile_path = os.path.join(os.getcwd(), "Vagrantfile")
        with open(vagrantfile_path, 'w') as f:
            f.write(vagrantfile)

        # Build env for Vagrant with a copy of env variables (needed by
        # subprocess opened by vagrant
        v_env = dict(os.environ)
        v_env['VAGRANT_DEFAULT_PROVIDER'] = self.provider_conf.backend

        v = vagrant.Vagrant(root=os.getcwd(),
                            quiet_stdout=False,
                            quiet_stderr=False,
                            env=v_env)
        if force_deploy:
            v.destroy()
        v.up()
        v.provision()
        roles = {}
        for role, machines in vagrant_roles.items():
            for machine in machines:
                keyfile = v.keyfile(vm_name=machine['name'])
                port = v.port(vm_name=machine['name'])
                address = v.hostname(vm_name=machine['name'])
                roles.setdefault(role, []).append(
                    Host(address,
                         alias=machine['name'],
                         user=self.provider_conf.user,
                         port=port,
                         keyfile=keyfile))

        networks = [{
            'cidr': str(n["cidr"]),
            'start': str(n["netpool"][0]),
            'end': str(n["netpool"][-1]),
            'dns': '8.8.8.8',
            'gateway': str(n["gateway"]),
            'roles': n["roles"]
            } for n in _networks]
        logger.debug(roles)
        logger.debug(networks)

        return (roles, networks)


Instruction:
Can you create a Python 3 function that
destroys all vagrant box involved in the deployment.

Response:
def destroy(self):
        """Destroy all vagrant box involved in the deployment."""
        v = vagrant.Vagrant(root=os.getcwd(),
                            quiet_stdout=False,
                            quiet_stderr=True)
        v.destroy()


Instruction:
Given the following Python 3 function, write the documentation
def tick(self):
        instant_rate = self.count / float(self.tick_interval_s)
        self.count = 0
        if self.initialized:
            self.rate += (self.alpha * (instant_rate - self.rate))
        else:
            self.rate = instant_rate
            self.initialized = True

Response: Mark the passage of time and decay the current rate accordingly.


Instruction:
Can you create a Python 3 function that
merges odd - and even - numbered CSU configurations into one CSU.

Response:
def merge_odd_even_csu_configurations(conf_odd, conf_even):
    """Merge CSU configuration using odd- and even-numbered values.

    The CSU returned CSU configuration include the odd-numbered values
    from 'conf_odd' and the even-numbered values from 'conf_even'.

    Parameters
    ----------
    conf_odd : CsuConfiguration instance
        CSU configuration corresponding to odd-numbered slitlets.
    conf_even : CsuConfiguration instance
        CSU configuration corresponding to even-numbered slitlets.

    Returns
    -------
    merged_conf : CsuConfiguration instance
        CSU configuration resulting from the merging process.

    """

    # initialize resulting CsuConfiguration instance using one of the
    # input configuration corresponding to the odd-numbered slitlets
    merged_conf = deepcopy(conf_odd)

    # update the resulting configuration with the values corresponding
    # to the even-numbered slitlets
    for i in range(EMIR_NBARS):
        ibar = i + 1
        if ibar % 2 == 0:
            merged_conf._csu_bar_left[i] = conf_even._csu_bar_left[i]
            merged_conf._csu_bar_right[i] = conf_even._csu_bar_right[i]
            merged_conf._csu_bar_slit_center[i] = \
                conf_even._csu_bar_slit_center[i]
            merged_conf._csu_bar_slit_width[i] = \
                conf_even._csu_bar_slit_width[i]

    # return merged configuration
    return merged_conf


Instruction:
Explain what the following Python 3 code does
def define_from_header(cls, image_header):

        self = CsuConfiguration()

        # declare lists to store configuration of CSU bars
        self._csu_bar_left = []
        self._csu_bar_right = []
        self._csu_bar_slit_center = []
        self._csu_bar_slit_width = []

        for i in range(EMIR_NBARS):
            ibar = i + 1
            keyword = 'CSUP{}'.format(ibar)
            if keyword in image_header:
                self._csu_bar_left.append(image_header[keyword])
            else:
                raise ValueError("Expected keyword " + keyword + " not found!")
            keyword = 'CSUP{}'.format(ibar + EMIR_NBARS)
            if keyword in image_header:
                # set the same origin as the one employed for _csu_bar_left
                self._csu_bar_right.append(341.5 - image_header[keyword])
            else:
                raise ValueError("Expected keyword " + keyword + " not found!")
            self._csu_bar_slit_center.append(
                (self._csu_bar_left[i] + self._csu_bar_right[i]) / 2
            )
            self._csu_bar_slit_width.append(
                self._csu_bar_right[i] - self._csu_bar_left[i]
            )

        return self

Response: Define class members directly from FITS header.


Instruction:
How would you code a function in Python 3 to
return dictionary structure rounded to a given precision.

Response:
def outdict(self, ndigits=3):
        """Return dictionary structure rounded to a given precision."""

        outdict = {}
        for i in range(EMIR_NBARS):
            ibar = i + 1
            cbar = 'slitlet' + str(ibar).zfill(2)
            outdict[cbar] = {}
            outdict[cbar]['_csu_bar_left'] = \
                round(self._csu_bar_left[i], ndigits)
            outdict[cbar]['_csu_bar_right'] = \
                round(self._csu_bar_right[i], ndigits)
            outdict[cbar]['_csu_bar_slit_center'] = \
                round(self._csu_bar_slit_center[i], ndigits)
            outdict[cbar]['_csu_bar_slit_width'] = \
                round(self._csu_bar_slit_width[i], ndigits)

        return outdict


Instruction:
Make a summary of the following Python 3 code
def widths_in_range_mm(
            self,
            minwidth=EMIR_MINIMUM_SLITLET_WIDTH_MM,
            maxwidth=EMIR_MAXIMUM_SLITLET_WIDTH_MM
    ):
        """Return list of slitlets which width is within given range

        Parameters
        ----------
        minwidth : float
            Minimum slit width (mm).
        maxwidth : float
            Maximum slit width (mm).

        Returns
        -------
        list_ok : list
            List of booleans indicating whether the corresponding
            slitlet width is within range

        """

        list_ok = []
        for i in range(EMIR_NBARS):
            slitlet_ok = minwidth <= self._csu_bar_slit_width[i] <= maxwidth
            if slitlet_ok:
                list_ok.append(i + 1)

        return list_ok

Response: Return list of slitlets which width is within given range.


Instruction:
Create a Python 3 function for
enclosing function backported from old photutils

Response:
def encloses_annulus(x_min, x_max, y_min, y_max, nx, ny, r_in, r_out):
    '''Encloses function backported from old photutils'''

    gout = circular_overlap_grid(x_min, x_max, y_min, y_max, nx, ny, r_out, 1, 1)
    gin = circular_overlap_grid(x_min, x_max, y_min, y_max, nx, ny, r_in, 1, 1)
    return gout - gin


Instruction:
How would you explain what the following Python 3 function does
def comp_back_with_annulus(img, xc, yc, r_in, r_out, frac=0.1):
    '''
    center: [x,y], center of first pixel is [0,0]
    '''

    x_min = -0.5 - xc
    x_max = img.shape[1] - 0.5 - xc
    y_min = -0.5 - yc
    y_max = img.shape[1] - 0.5 - yc
    mm = encloses_annulus(x_min, x_max, y_min, y_max,
                          img.shape[1], img.shape[0],
                          r_in, r_out)

    valid = mm > frac
    rr = img[valid]

    if rr.size == 0:
        raise ValueError("Not enough points to compute background")

    # mode?
    bck = mode_half_sample(rr)

    return bck, mm

Response: Compute background using anannulus


Instruction:
Given the following Python 3 function, write the documentation
def main(args=None):

    # parse command-line options
    parser = argparse.ArgumentParser(prog='rect_wpoly_for_mos')
    # required arguments
    parser.add_argument("input_list",
                        help="TXT file with list JSON files derived from "
                             "longslit data")
    parser.add_argument("--fitted_bound_param", required=True,
                        help="Input JSON with fitted boundary parameters",
                        type=argparse.FileType('rt'))
    parser.add_argument("--out_MOSlibrary", required=True,
                        help="Output JSON file with results",
                        type=lambda x: arg_file_is_new(parser, x))
    # optional arguments
    parser.add_argument("--debugplot",
                        help="Integer indicating plotting & debugging options"
                             " (default=0)",
                        default=0, type=int,
                        choices=DEBUGPLOT_CODES)
    parser.add_argument("--echo",
                        help="Display full command line",
                        action="store_true")
    args = parser.parse_args(args)

    if args.echo:
        print('\033[1m\033[31m% ' + ' '.join(sys.argv) + '\033[0m\n')

    # ---

    # Read input TXT file with list of JSON files
    list_json_files = list_fileinfo_from_txt(args.input_list)
    nfiles = len(list_json_files)
    if abs(args.debugplot) >= 10:
        print('>>> Number of input JSON files:', nfiles)
        for item in list_json_files:
            print(item)
    if nfiles < 2:
        raise ValueError("Insufficient number of input JSON files")

    # read fitted boundary parameters and check that all the longslit JSON
    # files have been computed using the same fitted boundary parameters
    refined_boundary_model = RefinedBoundaryModelParam._datatype_load(
        args.fitted_bound_param.name)
    for ifile in range(nfiles):
        coef_rect_wpoly = RectWaveCoeff._datatype_load(
            list_json_files[ifile].filename)
        uuid_tmp = coef_rect_wpoly.meta_info['origin']['bound_param']
        if uuid_tmp[4:] != refined_boundary_model.uuid:
            print('Expected uuid:', refined_boundary_model.uuid)
            print('uuid for ifile #' + str(ifile + 1) + ": " + uuid_tmp)
            raise ValueError("Fitted boundary parameter uuid's do not match")

    # check consistency of grism, filter, DTU configuration and list of
    # valid slitlets
    coef_rect_wpoly_first_longslit = RectWaveCoeff._datatype_load(
        list_json_files[0].filename)
    filter_name = coef_rect_wpoly_first_longslit.tags['filter']
    grism_name = coef_rect_wpoly_first_longslit.tags['grism']
    dtu_conf = DtuConfiguration.define_from_dictionary(
        coef_rect_wpoly_first_longslit.meta_info['dtu_configuration']
    )
    list_valid_islitlets = list(range(1, EMIR_NBARS + 1))
    for idel in coef_rect_wpoly_first_longslit.missing_slitlets:
        list_valid_islitlets.remove(idel)
    for ifile in range(1, nfiles):
        coef_rect_wpoly = RectWaveCoeff._datatype_load(
            list_json_files[ifile].filename)
        filter_tmp = coef_rect_wpoly.tags['filter']
        if filter_name != filter_tmp:
            print(filter_name)
            print(filter_tmp)
            raise ValueError("Unexpected different filter found")
        grism_tmp = coef_rect_wpoly.tags['grism']
        if grism_name != grism_tmp:
            print(grism_name)
            print(grism_tmp)
            raise ValueError("Unexpected different grism found")
        coef_rect_wpoly = RectWaveCoeff._datatype_load(
            list_json_files[ifile].filename)
        dtu_conf_tmp = DtuConfiguration.define_from_dictionary(
            coef_rect_wpoly.meta_info['dtu_configuration']
        )
        if dtu_conf != dtu_conf_tmp:
            print(dtu_conf)
            print(dtu_conf_tmp)
            raise ValueError("Unexpected different DTU configurations found")
        list_valid_islitlets_tmp = list(range(1, EMIR_NBARS + 1))
        for idel in coef_rect_wpoly.missing_slitlets:
            list_valid_islitlets_tmp.remove(idel)
        if list_valid_islitlets != list_valid_islitlets_tmp:
            print(list_valid_islitlets)
            print(list_valid_islitlets_tmp)
            raise ValueError("Unexpected different list of valid slitlets")

    # check consistency of horizontal bounding box limits (bb_nc1_orig and
    # bb_nc2_orig) and ymargin_bb, and store the values for each slitlet
    dict_bb_param = {}
    print("Checking horizontal bounding box limits and ymargin_bb:")
    for islitlet in list_valid_islitlets:
        islitlet_progress(islitlet, EMIR_NBARS)
        cslitlet = 'slitlet' + str(islitlet).zfill(2)
        dict_bb_param[cslitlet] = {}
        for par in ['bb_nc1_orig', 'bb_nc2_orig', 'ymargin_bb']:
            value_initial = \
                coef_rect_wpoly_first_longslit.contents[islitlet - 1][par]
            for ifile in range(1, nfiles):
                coef_rect_wpoly = RectWaveCoeff._datatype_load(
                    list_json_files[ifile].filename)
                value_tmp = coef_rect_wpoly.contents[islitlet - 1][par]
                if value_initial != value_tmp:
                    print(islitlet, value_initial, value_tmp)
                    print(value_tmp)
                    raise ValueError("Unexpected different " + par)
                dict_bb_param[cslitlet][par] = value_initial
    print('OK!')

    # ---

    # Read and store all the longslit data
    list_coef_rect_wpoly = []
    for ifile in range(nfiles):
        coef_rect_wpoly = RectWaveCoeff._datatype_load(
            list_json_files[ifile].filename)
        list_coef_rect_wpoly.append(coef_rect_wpoly)

    # ---

    # Initialize structure to save results into an ouptut JSON file
    outdict = {}
    outdict['refined_boundary_model'] = refined_boundary_model.__getstate__()
    outdict['instrument'] = 'EMIR'
    outdict['meta_info'] = {}
    outdict['meta_info']['creation_date'] = datetime.now().isoformat()
    outdict['meta_info']['description'] = \
        'rectification and wavelength calibration polynomial coefficients ' \
        'as a function of csu_bar_slit_center for MOS'
    outdict['meta_info']['recipe_name'] = 'undefined'
    outdict['meta_info']['origin'] = {}
    outdict['meta_info']['origin']['wpoly_longslits'] = {}
    for ifile in range(nfiles):
        cdum = 'longslit_' + str(ifile + 1).zfill(3) + '_uuid'
        outdict['meta_info']['origin']['wpoly_longslits'][cdum] = \
            list_coef_rect_wpoly[ifile].uuid
    outdict['tags'] = {}
    outdict['tags']['grism'] = grism_name
    outdict['tags']['filter'] = filter_name
    outdict['dtu_configuration'] = dtu_conf.outdict()
    outdict['uuid'] = str(uuid4())
    outdict['contents'] = {}

    # include bb_nc1_orig, bb_nc2_orig and ymargin_bb for each slitlet
    # (note that the values of bb_ns1_orig and bb_ns2_orig cannot be
    # computed at this stage because they depend on csu_bar_slit_center)
    for islitlet in list_valid_islitlets:
        cslitlet = 'slitlet' + str(islitlet).zfill(2)
        outdict['contents'][cslitlet] = dict_bb_param[cslitlet]

    # check that order for rectification transformations is the same for all
    # the slitlets and longslit configurations
    order_check_list = []
    for ifile in range(nfiles):
        tmpdict = list_coef_rect_wpoly[ifile].contents
        for islitlet in list_valid_islitlets:
            ttd_order = tmpdict[islitlet - 1]['ttd_order']
            if ttd_order is not None:
                order_check_list.append(ttd_order)
            ttd_order_modeled = \
                tmpdict[islitlet - 1]['ttd_order_longslit_model']
            order_check_list.append(ttd_order_modeled)
    # remove duplicates in list
    order_no_duplicates = list(set(order_check_list))
    if len(order_no_duplicates) != 1:
        print('order_no_duplicates:', order_no_duplicates)
        raise ValueError('tdd_order is not constant!')
    ttd_order = int(order_no_duplicates[0])
    ncoef_rect = ncoef_fmap(ttd_order)
    if abs(args.debugplot) >= 10:
        print('>>> ttd_order........:', ttd_order)
        print('>>> ncoef_rect.......:', ncoef_rect)

    # check that polynomial degree in frontiers and spectrails are the same
    poldeg_check_list = []
    for ifile in range(nfiles):
        tmpdict = list_coef_rect_wpoly[ifile].contents
        for islitlet in list_valid_islitlets:
            tmppoly = tmpdict[islitlet - 1]['frontier']['poly_coef_lower']
            poldeg_check_list.append(len(tmppoly) - 1)
            tmppoly = tmpdict[islitlet - 1]['frontier']['poly_coef_upper']
            poldeg_check_list.append(len(tmppoly) - 1)
            tmppoly = tmpdict[islitlet - 1]['spectrail']['poly_coef_lower']
            poldeg_check_list.append(len(tmppoly) - 1)
            tmppoly = tmpdict[islitlet - 1]['spectrail']['poly_coef_middle']
            poldeg_check_list.append(len(tmppoly) - 1)
            tmppoly = tmpdict[islitlet - 1]['spectrail']['poly_coef_upper']
            poldeg_check_list.append(len(tmppoly) - 1)
    # remove duplicates in list
    poldeg_no_duplicates = list(set(poldeg_check_list))
    if len(poldeg_no_duplicates) != 1:
        print('poldeg_no_duplicates:', poldeg_no_duplicates)
        raise ValueError('poldeg is not constant in frontiers and '
                         'spectrails!')
    poldeg_spectrails = int(poldeg_no_duplicates[0])
    if abs(args.debugplot) >= 10:
        print('>>> poldeg spectrails:', poldeg_spectrails)

    # check that polynomial degree of wavelength calibration is the same for
    # all the slitlets
    poldeg_check_list = []
    for ifile in range(nfiles):
        tmpdict = list_coef_rect_wpoly[ifile].contents
        for islitlet in list_valid_islitlets:
            tmppoly = tmpdict[islitlet - 1]['wpoly_coeff']
            poldeg_check_list.append(len(tmppoly) - 1)
            tmppoly = tmpdict[islitlet - 1]['wpoly_coeff_longslit_model']
            poldeg_check_list.append(len(tmppoly) - 1)
    # remove duplicates in list
    poldeg_no_duplicates = list(set(poldeg_check_list))
    if len(poldeg_no_duplicates) != 1:
        print('poldeg_no_duplicates:', poldeg_no_duplicates)
        raise ValueError('poldeg is not constant in wavelength calibration '
                         'polynomials!')
    poldeg_wavecal = int(poldeg_no_duplicates[0])
    if abs(args.debugplot) >= 10:
        print('>>> poldeg wavecal...:', poldeg_wavecal)

    # ---

    # csu_bar_slit_center values for each slitlet
    print("CSU_bar_slit_center values:")
    for islitlet in list_valid_islitlets:
        islitlet_progress(islitlet, EMIR_NBARS)
        cslitlet = 'slitlet' + str(islitlet).zfill(2)
        list_csu_bar_slit_center = []
        for ifile in range(nfiles):
            tmpdict = list_coef_rect_wpoly[ifile].contents[islitlet - 1]
            csu_bar_slit_center = tmpdict['csu_bar_slit_center']
            list_csu_bar_slit_center.append(csu_bar_slit_center)
        # check that list_csu_bar_slit_center is properly sorted
        if not np.all(list_csu_bar_slit_center[:-1] <=
                  list_csu_bar_slit_center[1:]):
            print('cslitlet: ', cslitlet)
            print('list_csu_bar_slit_center: ', list_csu_bar_slit_center)
            raise ValueError('Unsorted list_csu_bar_slit_center')
        outdict['contents'][cslitlet]['list_csu_bar_slit_center'] = \
            list_csu_bar_slit_center
    print('OK!')

    # ---

    # rectification polynomial coefficients

    # note: when aij and bij have not been computed, we use the modeled
    # version aij_longslit_model and bij_longslit_model
    print("Rectification polynomial coefficients:")
    for islitlet in list_valid_islitlets:
        islitlet_progress(islitlet, EMIR_NBARS)
        cslitlet = 'slitlet' + str(islitlet).zfill(2)
        outdict['contents'][cslitlet]['ttd_order'] = ttd_order
        outdict['contents'][cslitlet]['ncoef_rect'] = ncoef_rect
        for keycoef in ['ttd_aij', 'ttd_bij', 'tti_aij', 'tti_bij']:
            for icoef in range(ncoef_rect):
                ccoef = str(icoef).zfill(2)
                list_cij = []
                for ifile in range(nfiles):
                    tmpdict = \
                        list_coef_rect_wpoly[ifile].contents[islitlet - 1]
                    cij = tmpdict[keycoef]
                    if cij is not None:
                        list_cij.append(cij[icoef])
                    else:
                        cij_modeled = tmpdict[keycoef + '_longslit_model']
                        if cij_modeled is None:
                            raise ValueError("Unexpected cij_modeled=None!")
                        else:
                            list_cij.append(cij_modeled[icoef])
                        if abs(args.debugplot) >= 10:
                            print("Warning: using " + keycoef +
                                  "_longslit_model for " + cslitlet +
                                  " in file " +
                                  list_json_files[ifile].filename)
                cdum = 'list_' + keycoef + '_' + ccoef
                outdict['contents'][cslitlet][cdum] = list_cij
    print('OK!')

    # ---

    # wavelength calibration polynomial coefficients

    # note: when wpoly_coeff have not been computed, we use the
    # wpoly_coeff_longslit_model
    print("Wavelength calibration polynomial coefficients:")
    for islitlet in list_valid_islitlets:
        islitlet_progress(islitlet, EMIR_NBARS)
        cslitlet = 'slitlet' + str(islitlet).zfill(2)
        outdict['contents'][cslitlet]['wpoly_degree'] = poldeg_wavecal
        for icoef in range(poldeg_wavecal + 1):
            ccoef = str(icoef).zfill(2)
            list_cij = []
            for ifile in range(nfiles):
                tmpdict = list_coef_rect_wpoly[ifile].contents[islitlet - 1]
                cij = tmpdict['wpoly_coeff']
                if cij is not None:
                    list_cij.append(cij[icoef])
                else:
                    cij_modeled = tmpdict['wpoly_coeff_longslit_model']
                    if cij_modeled is None:
                        raise ValueError("Unexpected cij_modeled=None!")
                    else:
                        list_cij.append(cij_modeled[icoef])
                    if abs(args.debugplot) >= 10:
                        print("Warning: using wpoly_coeff_longslit_model" +
                              " for " + cslitlet +
                              " in file " +
                              list_json_files[ifile].filename)
            outdict['contents'][cslitlet]['list_wpoly_coeff_' + ccoef] = \
                list_cij
    print('OK!')

    # ---

    # OBSOLETE
    # Save resulting JSON structure
    '''
    with open(args.out_MOSlibrary.name + '_old', 'w') as fstream:
        json.dump(outdict, fstream, indent=2, sort_keys=True)
        print('>>> Saving file ' + args.out_MOSlibrary.name + '_old')
    '''

    # --

    # Create object of type MasterRectWave with library of coefficients
    # for rectification and wavelength calibration
    master_rectwv = MasterRectWave(instrument='EMIR')
    master_rectwv.quality_control = numina.types.qc.QC.GOOD
    master_rectwv.tags['grism'] = grism_name
    master_rectwv.tags['filter'] = filter_name
    master_rectwv.meta_info['dtu_configuration'] = outdict['dtu_configuration']
    master_rectwv.meta_info['refined_boundary_model'] = {
        'parmodel': refined_boundary_model.meta_info['parmodel']
    }
    master_rectwv.meta_info['refined_boundary_model'].update(
        outdict['refined_boundary_model']['contents']
    )
    master_rectwv.total_slitlets = EMIR_NBARS
    master_rectwv.meta_info['origin'] = {
        'bound_param': 'uuid' + refined_boundary_model.uuid,
        'longslit_frames': ['uuid:' + list_coef_rect_wpoly[ifile].uuid
                            for ifile in range(nfiles)]
    }
    for i in range(EMIR_NBARS):
        islitlet = i + 1
        dumdict = {'islitlet': islitlet}
        cslitlet = 'slitlet' + str(islitlet).zfill(2)
        if cslitlet in outdict['contents']:
            dumdict.update(outdict['contents'][cslitlet])
        else:
            dumdict.update({
                'bb_nc1_orig': 0,
                'bb_nc2_orig': 0,
                'ymargin_bb': 0,
                'list_csu_bar_slit_center': [],
                'ttd_order': 0,
                'ncoef_rect': 0,
                'wpolydegree': 0
            })
            master_rectwv.missing_slitlets.append(islitlet)
        master_rectwv.contents.append(dumdict)
    master_rectwv.writeto(args.out_MOSlibrary.name)
    print('>>> Saving file ' + args.out_MOSlibrary.name)

Response: Main function for the rect_wpoly_for_mos command line tool.


Instruction:
Here you have a function in Python 3, explain what it does
def display_slitlet_arrangement(fileobj,
                                grism=None,
                                spfilter=None,
                                bbox=None,
                                adjust=None,
                                geometry=None,
                                debugplot=0):
    """Display slitlet arrangment from CSUP keywords in FITS header.

    Parameters
    ----------
    fileobj : file object
        FITS or TXT file object.
    grism : str
        Grism.
    grism : str
        Filter.
    bbox : tuple of 4 floats
        If not None, values for xmin, xmax, ymin and ymax.
    adjust : bool
        Adjust X range according to minimum and maximum csu_bar_left
        and csu_bar_right (note that this option is overriden by 'bbox')
    geometry : tuple (4 integers) or None
        x, y, dx, dy values employed to set the Qt backend geometry.
    debugplot : int
        Determines whether intermediate computations and/or plots
        are displayed. The valid codes are defined in
        numina.array.display.pause_debugplot

    Returns
    -------
    csu_bar_left : list of floats
        Location (mm) of the left bar for each slitlet.
    csu_bar_right : list of floats
        Location (mm) of the right bar for each slitlet, using the
        same origin employed for csu_bar_left (which is not the
        value stored in the FITS keywords.
    csu_bar_slit_center : list of floats
        Middle point (mm) in between the two bars defining a slitlet.
    csu_bar_slit_width : list of floats
        Slitlet width (mm), computed as the distance between the two
        bars defining the slitlet.

    """

    if fileobj.name[-4:] == ".txt":
        if grism is None:
            raise ValueError("Undefined grism!")
        if spfilter is None:
            raise ValueError("Undefined filter!")
        # define CsuConfiguration object
        csu_config = CsuConfiguration()
        csu_config._csu_bar_left = []
        csu_config._csu_bar_right = []
        csu_config._csu_bar_slit_center = []
        csu_config._csu_bar_slit_width = []

        # since the input filename has been opened with argparse in binary
        # mode, it is necessary to close it and open it in text mode
        fileobj.close()
        # read TXT file
        with open(fileobj.name, mode='rt') as f:
            file_content = f.read().splitlines()
        next_id_bar = 1
        for line in file_content:
            if len(line) > 0:
                if line[0] not in ['#']:
                    line_contents = line.split()
                    id_bar = int(line_contents[0])
                    position = float(line_contents[1])
                    if id_bar == next_id_bar:
                        if id_bar <= EMIR_NBARS:
                            csu_config._csu_bar_left.append(position)
                            next_id_bar = id_bar + EMIR_NBARS
                        else:
                            csu_config._csu_bar_right.append(341.5 - position)
                            next_id_bar = id_bar - EMIR_NBARS + 1
                    else:
                        raise ValueError("Unexpected id_bar:" + str(id_bar))

        # compute slit width and center
        for i in range(EMIR_NBARS):
            csu_config._csu_bar_slit_center.append(
                (csu_config._csu_bar_left[i] + csu_config._csu_bar_right[i])/2
            )
            csu_config._csu_bar_slit_width.append(
                csu_config._csu_bar_right[i] - csu_config._csu_bar_left[i]
            )

    else:
        # read input FITS file
        hdulist = fits.open(fileobj.name)
        image_header = hdulist[0].header
        hdulist.close()

        # additional info from header
        grism = image_header['grism']
        spfilter = image_header['filter']

        # define slitlet arrangement
        csu_config = CsuConfiguration.define_from_fits(fileobj)

    # determine calibration
    if grism in ["J", "OPEN"] and spfilter == "J":
        wv_parameters = set_wv_parameters("J", "J")
    elif grism in ["H", "OPEN"] and spfilter == "H":
        wv_parameters = set_wv_parameters("H", "H")
    elif grism in ["K", "OPEN"] and spfilter == "Ksp":
        wv_parameters = set_wv_parameters("Ksp", "K")
    elif grism in ["LR", "OPEN"] and spfilter == "YJ":
        wv_parameters = set_wv_parameters("YJ", "LR")
    elif grism in ["LR", "OPEN"] and spfilter == "HK":
        wv_parameters = set_wv_parameters("HK", "LR")
    else:
        raise ValueError("Invalid grism + filter configuration")

    crval1 = wv_parameters['poly_crval1_linear']
    cdelt1 = wv_parameters['poly_cdelt1_linear']

    wvmin_useful = wv_parameters['wvmin_useful']
    wvmax_useful = wv_parameters['wvmax_useful']

    # display arrangement
    if abs(debugplot) >= 10:
        print("slit     left    right   center   width   min.wave   max.wave")
        print("====  =======  =======  =======   =====   ========   ========")
        for i in range(EMIR_NBARS):
            ibar = i + 1
            csu_crval1 = crval1(csu_config.csu_bar_slit_center(ibar))
            csu_cdelt1 = cdelt1(csu_config.csu_bar_slit_center(ibar))
            csu_crvaln = csu_crval1 + (EMIR_NAXIS1 - 1) * csu_cdelt1
            if wvmin_useful is not None:
                csu_crval1 = np.amax([csu_crval1, wvmin_useful])
            if wvmax_useful is not None:
                csu_crvaln = np.amin([csu_crvaln, wvmax_useful])
            print("{0:4d} {1:8.3f} {2:8.3f} {3:8.3f} {4:7.3f}   "
                  "{5:8.2f}   {6:8.2f}".format(
                ibar, csu_config.csu_bar_left(ibar),
                csu_config.csu_bar_right(ibar),
                csu_config.csu_bar_slit_center(ibar),
                csu_config.csu_bar_slit_width(ibar),
                csu_crval1, csu_crvaln)
            )
        print(
            "---> {0:8.3f} {1:8.3f} {2:8.3f} {3:7.3f} <- mean (all)".format(
                np.mean(csu_config._csu_bar_left),
                np.mean(csu_config._csu_bar_right),
                np.mean(csu_config._csu_bar_slit_center),
                np.mean(csu_config._csu_bar_slit_width)
            )
        )
        print(
            "---> {0:8.3f} {1:8.3f} {2:8.3f} {3:7.3f} <- mean (odd)".format(
                np.mean(csu_config._csu_bar_left[::2]),
                np.mean(csu_config._csu_bar_right[::2]),
                np.mean(csu_config._csu_bar_slit_center[::2]),
                np.mean(csu_config._csu_bar_slit_width[::2])
            )
        )
        print(
            "---> {0:8.3f} {1:8.3f} {2:8.3f} {3:7.3f} <- mean (even)".format(
                np.mean(csu_config._csu_bar_left[1::2]),
                np.mean(csu_config._csu_bar_right[1::2]),
                np.mean(csu_config._csu_bar_slit_center[1::2]),
                np.mean(csu_config._csu_bar_slit_width[1::2])
            )
        )

    # display slit arrangement
    if abs(debugplot) % 10 != 0:
        fig = plt.figure()
        set_window_geometry(geometry)
        ax = fig.add_subplot(111)
        if bbox is None:
            if adjust:
                xmin = min(csu_config._csu_bar_left)
                xmax = max(csu_config._csu_bar_right)
                dx = xmax - xmin
                if dx == 0:
                    dx = 1
                xmin -= dx/20
                xmax += dx/20
                ax.set_xlim(xmin, xmax)
            else:
                ax.set_xlim(0., 341.5)
            ax.set_ylim(0, 56)
        else:
            ax.set_xlim(bbox[0], bbox[1])
            ax.set_ylim(bbox[2], bbox[3])
        ax.set_xlabel('csu_bar_position (mm)')
        ax.set_ylabel('slit number')
        for i in range(EMIR_NBARS):
            ibar = i + 1
            ax.add_patch(patches.Rectangle(
                (csu_config.csu_bar_left(ibar), ibar-0.5),
                csu_config.csu_bar_slit_width(ibar), 1.0))
            ax.plot([0., csu_config.csu_bar_left(ibar)], [ibar, ibar],
                    '-', color='gray')
            ax.plot([csu_config.csu_bar_right(ibar), 341.5],
                    [ibar, ibar], '-', color='gray')
        plt.title("File: " + fileobj.name + "\ngrism=" + grism +
                  ", filter=" + spfilter)
        pause_debugplot(debugplot, pltshow=True)

    # return results
    return csu_config._csu_bar_left, csu_config._csu_bar_right, \
           csu_config._csu_bar_slit_center, csu_config._csu_bar_slit_width

Response: Display a slitlet arrangement from CSUP keywords in FITS header.


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

        # protections
        naxis2, naxis1 = image_2k2k.shape
        if naxis1 != EMIR_NAXIS1:
            raise ValueError('Unexpected naxis1')
        if naxis2 != EMIR_NAXIS2:
            raise ValueError('Unexpected naxis2')

        # extract slitlet region
        slitlet2d = image_2k2k[(self.bb_ns1_orig - 1):self.bb_ns2_orig,
                               (self.bb_nc1_orig - 1):self.bb_nc2_orig]

        # transform to float
        slitlet2d = slitlet2d.astype(np.float)

        # display slitlet2d with boundaries and middle spectrum trail
        if abs(self.debugplot) in [21, 22]:
            self.ximshow_unrectified(slitlet2d)

        # return slitlet image
        return slitlet2d

Response: Extract slitlet 2d image from image with original EMIR dimensions.


Instruction:
Explain what the following Python 3 code does
def rectify(self, slitlet2d, resampling, inverse=False):

        if resampling not in [1, 2]:
            raise ValueError("Unexpected resampling value=" + str(resampling))

        # check image dimension
        naxis2, naxis1 = slitlet2d.shape
        if naxis1 != self.bb_nc2_orig - self.bb_nc1_orig + 1:
            raise ValueError("Unexpected slitlet2d_rect naxis1")
        if naxis2 != self.bb_ns2_orig - self.bb_ns1_orig + 1:
            raise ValueError("Unexpected slitlet2d_rect naxis2")

        if inverse:
            aij = self.tti_aij
            bij = self.tti_bij
        else:
            aij = self.ttd_aij
            bij = self.ttd_bij

        # rectify image
        slitlet2d_rect = rectify2d(
            image2d=slitlet2d,
            aij=aij,
            bij=bij,
            resampling=resampling
        )

        if abs(self.debugplot % 10) != 0:
            if inverse:
                self.ximshow_unrectified(slitlet2d_rect)
            else:
                self.ximshow_rectified(slitlet2d_rect)

        return slitlet2d_rect

Response: Rectify a 2d slitlet image.


Instruction:
Write a Python 3 script for
displaying unrectified image with spectrails and frontiers.

Response:
def ximshow_unrectified(self, slitlet2d):
        """Display unrectified image with spectrails and frontiers.

        Parameters
        ----------
        slitlet2d : numpy array
            Array containing the unrectified slitlet image.

        """

        title = "Slitlet#" + str(self.islitlet)
        ax = ximshow(slitlet2d, title=title,
                     first_pixel=(self.bb_nc1_orig, self.bb_ns1_orig),
                     show=False)
        xdum = np.linspace(1, EMIR_NAXIS1, num=EMIR_NAXIS1)
        ylower = self.list_spectrails[0](xdum)
        ax.plot(xdum, ylower, 'b-')
        ymiddle = self.list_spectrails[1](xdum)
        ax.plot(xdum, ymiddle, 'b--')
        yupper = self.list_spectrails[2](xdum)
        ax.plot(xdum, yupper, 'b-')
        ylower_frontier = self.list_frontiers[0](xdum)
        ax.plot(xdum, ylower_frontier, 'b:')
        yupper_frontier = self.list_frontiers[1](xdum)
        ax.plot(xdum, yupper_frontier, 'b:')
        pause_debugplot(debugplot=self.debugplot, pltshow=True)


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

        title = "Slitlet#" + str(self.islitlet) + " (rectify)"
        ax = ximshow(slitlet2d_rect, title=title,
                     first_pixel=(self.bb_nc1_orig, self.bb_ns1_orig),
                     show=False)
        # grid with fitted transformation: spectrum trails
        xx = np.arange(0, self.bb_nc2_orig - self.bb_nc1_orig + 1,
                       dtype=np.float)
        for spectrail in self.list_spectrails:
            yy0 = self.corr_yrect_a + \
                  self.corr_yrect_b * spectrail(self.x0_reference)
            yy = np.tile([yy0 - self.bb_ns1_orig], xx.size)
            ax.plot(xx + self.bb_nc1_orig, yy + self.bb_ns1_orig, "b")
        for spectrail in self.list_frontiers:
            yy0 = self.corr_yrect_a +\
                  self.corr_yrect_b * spectrail(self.x0_reference)
            yy = np.tile([yy0 - self.bb_ns1_orig], xx.size)
            ax.plot(xx + self.bb_nc1_orig, yy + self.bb_ns1_orig, "b:")
        # show plot
        pause_debugplot(self.debugplot, pltshow=True)

Response: Display rectified image with spectrails and frontiers.


Instruction:
Given the following Python 3 function, write the documentation
def info(self, token):
        verifier = JWT(key_jar=self.key_jar, allowed_sign_algs=[self.alg])
        _payload = verifier.unpack(token)
        if is_expired(_payload['exp']):
            raise ToOld('Token has expired')
        return _payload['sid'], _payload['ttype']

Response: Return the type of Token and session id.


Instruction:
Explain what the following Python 3 code does
def is_expired(self, token, when=0):
        verifier = JWT(key_jar=self.key_jar, allowed_sign_algs=[self.alg])
        _payload = verifier.unpack(token)
        return is_expired(_payload['exp'], when)

Response: Evaluate whether the token has expired or not.


Instruction:
Explain what the following Python 3 code does
async def create_websocket(url: str,
                           ssl: Optional[SSLContext] = None,
                           headers: Optional[list] = None,
                           subprotocols: Optional[list] = None):
    """
    A more low-level form of open_websocket.
    You are responsible for closing this websocket.
    """
    url = yarl.URL(url)
    args = {}
    if headers:
        args["headers"] = headers

    # automatically use ssl if it's websocket secure
    if ssl is None:
        ssl = url.scheme == "wss"
    if ssl:
        if ssl is True:
            ssl = SSLContext()
        args["ssl_context"] = ssl
        args["autostart_tls"] = True
        args["tls_standard_compatible"] = False

    addr = (url.host, int(url.port))
    ws = Websocket()
    await ws.__ainit__(
        addr=addr, path=url.path_qs, subprotocols=subprotocols, **args)
    return ws

Response: Create a new websocket.


Instruction:
How would you code a function in Python 3 to
open a websocket on top of a socket.

Response:
async def open_websocket_client(sock: anyio.abc.SocketStream,
                                addr,
                                path: str,
                                headers: Optional[list] = None,
                                subprotocols: Optional[list] = None):
    """Create a websocket on top of a socket."""
    ws = await create_websocket_client(
        sock, addr=addr, path=path, headers=headers, subprotocols=subprotocols)
    try:
        yield ws
    finally:
        await ws.close()


Instruction:
Can you create a Python 3 function that
creates a new websocket client.

Response:
async def create_websocket_client(sock: anyio.abc.SocketStream,
                                  addr,
                                  path: str,
                                  headers: Optional[List] = None,
                                  subprotocols: Optional[List[str]] = None):
    """
    A more low-level form of create_websocket_client.
    You are responsible for closing this websocket.
    """
    ws = Websocket()
    await ws.start_client(
        sock, addr=addr, path=path, headers=headers, subprotocols=subprotocols)
    return ws


Instruction:
Implement a function in Python 3 to
iterate over the MAC addresses given as string.

Response:
def range_mac(mac_start, mac_end, step=1):
    """Iterate over mac addresses (given as string)."""
    start = int(EUI(mac_start))
    end = int(EUI(mac_end))
    for i_mac in range(start, end, step):
        mac = EUI(int(EUI(i_mac)) + 1)
        ip = ['10'] + [str(int(i, 2)) for i in mac.bits().split('-')[-3:]]
        yield str(mac).replace('-', ':'), '.'.join(ip)


Instruction:
Can you create a Python 3 function that
gets all rows of the query. Raises a NotFoundError if there are 0 rows.

Response:
def _all(self, query):
        """Gets all rows of the query. Raises a NotFoundError if there are 0 rows"""
        if query.count() > 0:
            results = query.all()
            for result in results:
                if result.has_expired(self._expirations):
                    raise NotFoundError
            return results
        else:
            raise NotFoundError


Instruction:
Create a Python 3 function to
put a item into the database. Updates lastUpdate column

Response:
def _put(self, item: SQLBaseObject):
        """Puts a item into the database. Updates lastUpdate column"""
        if item._dto_type in self._expirations and self._expirations[item._dto_type] == 0:
            # The expiration time has been set to 0 -> shoud not be cached
            return
        item.updated()
        self._session().merge(item)


Instruction:
Can you generate the documentation for the following Python 3 function
def _put_many(self, items: Iterable[DtoObject], cls):
        if cls._dto_type in self._expirations and self._expirations[cls._dto_type] == 0:
            # The expiration time has been set to 0 -> shoud not be cached
            return
        session = self._session
        for item in items:
            item = cls(**item)
            item.updated()
            session.merge(item)

Response: Puts many items into the database. Updates lastUpdate column for each of them


Instruction:
Create a Python 3 function for
returning only those claims that are asked for.

Response:
def filter(self, userinfo, user_info_claims=None):
        """
        Return only those claims that are asked for.
        It's a best effort task; if essential claims are not present
        no error is flagged.

        :param userinfo: A dictionary containing the available info for one user
        :param user_info_claims: A dictionary specifying the asked for claims
        :return: A dictionary of filtered claims.
        """

        if user_info_claims is None:
            return copy.copy(userinfo)
        else:
            result = {}
            missing = []
            optional = []
            for key, restr in user_info_claims.items():
                try:
                    result[key] = userinfo[key]
                except KeyError:
                    if restr == {"essential": True}:
                        missing.append(key)
                    else:
                        optional.append(key)
            return result


Instruction:
Can you generate the documentation for the following Python 3 function
def from_data(room, conn, data):
        files = list()
        rooms = dict()
        msg = str()

        for part in data["message"]:
            ptype = part["type"]
            if ptype == "text":
                val = part["value"]
                msg += val
            elif ptype == "break":
                msg += "\n"
            elif ptype == "file":
                fileid = part["id"]
                fileobj = room.filedict.get(fileid)
                if fileobj:
                    files += (fileobj,)
                fileid = f"@{fileid}"
                msg += fileid
            elif ptype == "room":
                roomid = part["id"]
                rooms[roomid] = part["name"]
                roomid = f"#{roomid}"
                msg += roomid
            elif ptype == "url":
                msg += part["text"]
            elif ptype == "raw":
                msg += html_to_text(part["value"])
            else:
                import warnings

                warnings.warn(f"unknown message type '{ptype}'", Warning)

        nick = data.get("nick") or data.get("user")
        options = data.get("options", dict())
        data = data.get("data", dict())

        message = ChatMessage(
            room,
            conn,
            nick,
            msg,
            roles=Roles.from_options(options),
            options=options,
            data=data,
            files=files,
            rooms=rooms,
        )
        return message

Response: Construct a ChatMessage instance from raw protocol data.


Instruction:
Explain what the following Python 3 code does
def create_rot2d(angle):
    ca = math.cos(angle)
    sa = math.sin(angle)
    return np.array([[ca, -sa], [sa, ca]])

Response: Create 2D rotation matrix


Instruction:
Can you tell what is the following Python 3 function doing
def comp_centroid(data, bounding_box, debug_plot=False, plot_reference=None, logger=None):

    from matplotlib.patches import Ellipse

    if logger is None:
        logger = logging.getLogger(__name__)

    region = bounding_box.slice
    ref_x = region[1].start
    ref_y = region[0].start
    logger.debug('region ofset is %s, %s', ref_x, ref_y)
    subimage = data[region].copy()
    bkg = sep.Background(subimage)
    data_sub = subimage - bkg
    objects = sep.extract(data_sub, 1.5, err=bkg.globalrms)
    # Select brightest object
    logger.debug('%d object found', len(objects))

    if len(objects) == 0:
        # print('No objects')
        return None

    iadx = objects['flux'].argmax()
    # plot background-subtracted image
    maxflux = objects[iadx]

    if debug_plot:
        fig, ax = plt.subplots()
        m, s = np.mean(data_sub), np.std(data_sub)
        ax.imshow(data_sub, interpolation='nearest', cmap='gray',
                  vmin=m - s, vmax=m + s, origin='lower',
                  extent=bounding_box.extent)
        if plot_reference:
            e = Ellipse(xy=(plot_reference[0], plot_reference[1]),
                        width=6,
                        height=6,
                        angle=0)
            e.set_facecolor('none')
            e.set_edgecolor('green')
            ax.add_artist(e)

        # plot an ellipse for each object
        for idx, obj in enumerate(objects):
            e = Ellipse(xy=(obj['x'] + ref_x, obj['y'] + ref_y),
                        width=6 * obj['a'],
                        height=6 * obj['b'],
                        angle=obj['theta'] * 180. / np.pi)
            e.set_facecolor('none')
            if idx == iadx:
                e.set_edgecolor('blue')
            else:
                e.set_edgecolor('red')
            ax.add_artist(e)
        return maxflux['x'], maxflux['y'], ax
    else:
        return maxflux['x'], maxflux['y']

Response: Detect objects in a region and return the brightest one