Instruction:
Can you create a Python 3 function that
deploys Cloned VM From VM Command, will deploy vm from another vm :param cancellation_context: :param reservation_id: :param si: :param logger: :type data_holder: :type vcenter_data_model: :rtype DeployAppResult: :return:

Response:
def deploy_clone_from_vm(self, si, logger, data_holder, vcenter_data_model, reservation_id, cancellation_context):
        """
        deploy Cloned VM From VM Command, will deploy vm from another vm

        :param cancellation_context:
        :param reservation_id:
        :param si:
        :param logger:
        :type data_holder:
        :type vcenter_data_model:
        :rtype DeployAppResult:
        :return:
        """
        template_resource_model = data_holder.template_resource_model
        return self._deploy_a_clone(si,
                                    logger,
                                    data_holder.app_name,
                                    template_resource_model.vcenter_vm,
                                    template_resource_model,
                                    vcenter_data_model,
                                    reservation_id,
                                    cancellation_context)


Instruction:
Given the following Python 3 function, write the documentation
def _get_deploy_image_params(data_holder, host_info, vm_name):
        image_params = OvfImageParams()
        if hasattr(data_holder, 'vcenter_image_arguments') and data_holder.vcenter_image_arguments:
            image_params.user_arguments = data_holder.vcenter_image_arguments
        if hasattr(data_holder, 'vm_location') and data_holder.vm_location:
            image_params.vm_folder = data_holder.vm_location.replace(data_holder.default_datacenter + '/', '')
        image_params.cluster = data_holder.vm_cluster
        image_params.resource_pool = data_holder.vm_resource_pool
        image_params.connectivity = host_info
        image_params.vm_name = vm_name
        image_params.datastore = data_holder.vm_storage
        image_params.datacenter = data_holder.default_datacenter
        image_params.image_url = data_holder.vcenter_image
        image_params.power_on = False
        image_params.vcenter_name = data_holder.vcenter_name
        return image_params

Response: Returns the deploy image parameters for the specified image resource model.


Instruction:
Write a Python 3 function for
returning details of the current VM

Response:
def get_details(self):
        """
        :rtype list[VmDataField]
        """
        data = []
        if isinstance(self.model, vCenterCloneVMFromVMResourceModel):
            data.append(VmDetailsProperty(key='Cloned VM Name', value=self.model.vcenter_vm))

        if isinstance(self.model, VCenterDeployVMFromLinkedCloneResourceModel):
            template = self.model.vcenter_vm
            snapshot = self.model.vcenter_vm_snapshot
            data.append(VmDetailsProperty(key='Cloned VM Name',value= '{0} (snapshot: {1})'.format(template, snapshot)))

        if isinstance(self.model, vCenterVMFromImageResourceModel):
            data.append(VmDetailsProperty(key='Base Image Name', value=self.model.vcenter_image.split('/')[-1]))

        if isinstance(self.model, vCenterVMFromTemplateResourceModel):
            data.append(VmDetailsProperty(key='Template Name', value=self.model.vcenter_template))

        return data


Instruction:
Write a Python 3 script for
converting an instance of resource with dictionary of attributes to a class instance according to family and assigns its properties

Response:
def convert_to_resource_model(self, attributes, resource_model_type):
        """
        Converts an instance of resource with dictionary of attributes
        to a class instance according to family and assigns its properties
        :type attributes: dict
        :param resource_model_type: Resource Model type to create
        :return:
        """
        if resource_model_type:
            if not callable(resource_model_type):
                raise ValueError('resource_model_type {0} cannot be instantiated'.format(resource_model_type))
            instance = resource_model_type()
        else:
            raise ValueError('resource_model_type must have a value')

        props = ResourceModelParser.get_public_properties(instance)
        for attrib in attributes:
            property_name = ResourceModelParser.get_property_name_from_attribute_name(attrib)
            property_name_for_attribute_name = ResourceModelParser.get_property_name_with_attribute_name_postfix(attrib)
            if props.__contains__(property_name):
                value = attributes[attrib]
                setattr(instance, property_name, value)
                if hasattr(instance, property_name_for_attribute_name):
                    setattr(instance, property_name_for_attribute_name, attrib)
                    props.remove(property_name_for_attribute_name)
                props.remove(property_name)

        if props:
            raise ValueError('Property(ies) {0} not found on resource with attributes {1}'
                             .format(','.join(props),
                                     ','.join(attributes)))

        if hasattr(instance, 'vcenter_vm'):
            instance.vcenter_vm = back_slash_to_front_converter(instance.vcenter_vm)

        if hasattr(instance, 'vcenter_vm_snapshot'):
            instance.vcenter_vm_snapshot = back_slash_to_front_converter(instance.vcenter_vm_snapshot)

        return instance


Instruction:
Can you tell what is the following Python 3 function doing
def create_resource_model_instance(resource_instance):
        resource_model = ResourceModelParser.get_resource_model(resource_instance)
        resource_class_name = ResourceModelParser.get_resource_model_class_name(
            resource_model)
        # print 'Family name is ' + resource_class_name
        instance = ResourceModelParser.get_class('cloudshell.cp.vcenter.models.' + resource_class_name)
        return instance

Response:
Create an instance of class named *ResourceModel
        from models folder according to ResourceModelName of a resource


Instruction:
Make a summary of the following Python 3 code
def get_class(class_path):
        module_path, class_name = class_path.rsplit(".", 1)

        try:
            module = __import__(class_path, fromlist=[class_name])
        except ImportError:
            raise ValueError('Class {0} could not be imported'.format(class_path))

        try:
            cls = getattr(module, class_name)
        except AttributeError:
            raise ValueError("Module '%s' has no class '%s'" % (module_path, class_name,))

        try:
            instance = cls()
        except TypeError as type_error:
            raise ValueError('Failed to instantiate class {0}. Error: {1}'.format(class_name, type_error.message))

        return instance

Response: Returns an instance of a class by its class_path.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_property_name_from_attribute_name(attribute):
        if isinstance(attribute, str) or isinstance(attribute, unicode):
            attribute_name = attribute
        elif hasattr(attribute, 'Name'):
            attribute_name = attribute.Name
        else:
            raise Exception('Attribute type {0} is not supported'.format(str(type(attribute))))

        return attribute_name.lower().replace(' ', '_')

Response: Returns property name from attribute name


Instruction:
Can you generate the documentation for the following Python 3 function
def crc32File(filename, skip=0):
    with open(filename, 'rb') as stream:
        discard = stream.read(skip)
        return zlib.crc32(stream.read()) & 0xffffffff

Response: Computes the CRC - 32 of the contents of filename optionally skipping a certain number of bytes at the beginning of the file.


Instruction:
How would you code a function in Python 3 to
swap pairs of bytes in the given bytearray.

Response:
def endianSwapU16(bytes):
    """Swaps pairs of bytes (16-bit words) in the given bytearray."""
    for b in range(0, len(bytes), 2):
        bytes[b], bytes[b + 1] = bytes[b + 1], bytes[b]
    return bytes


Instruction:
Make a summary of the following Python 3 code
def setDictDefaults (d, defaults):
  for key, val in defaults.items():
    d.setdefault(key, val)

  return d

Response:
Sets all defaults for the given dictionary to those contained in a
 athermotto.


Instruction:
Can you create a Python 3 function that
returns the default AIT dictonary for modname

Response:
def getDefaultDict(modname, config_key, loader, reload=False, filename=None):
    """Returns default AIT dictonary for modname

    This helper function encapulates the core logic necessary to
    (re)load, cache (via util.ObjectCache), and return the default
    dictionary.  For example, in ait.core.cmd:

    def getDefaultDict(reload=False):
        return ait.util.getDefaultDict(__name__, 'cmddict', CmdDict, reload)
    """
    module   = sys.modules[modname]
    default  = getattr(module, 'DefaultDict', None)

    if filename is None:
        filename = ait.config.get('%s.filename' % config_key, None)

    if filename is not None and (default is None or reload is True):
        try:
            default = ObjectCache(filename, loader).load()
            setattr(module, 'DefaultDict', default)
        except IOError, e:
            msg = 'Could not load default %s "%s": %s'
            log.error(msg, config_key, filename, str(e))

    return default or loader()


Instruction:
Create a Python 3 function to
convert the number n into a Binary Coded Decimal.

Response:
def toBCD (n):

    """Converts the number n into Binary Coded Decimal."""
    bcd  = 0
    bits = 0

    while True:
        n, r  = divmod(n, 10)
        bcd  |= (r << bits)
        if n is 0:
            break
        bits += 4

    return bcd


Instruction:
Given the following Python 3 function, write the documentation
def toFloat (str, default=None):
    value = default

    try:
        value = float(str)
    except ValueError:
        pass

    return value

Response: Converts the given string to a floating - point value.


Instruction:
Given the following Python 3 function, write the documentation
def toNumber (str, default=None):
    value = default

    try:
        if str.startswith("0x"):
            value = int(str, 16)
        else:
            try:
                value = int(str)
            except ValueError:
                value = float(str)
    except ValueError:
        pass

    return value

Response: Convert a string to a numeric value.


Instruction:
Given the following Python 3 function, write the documentation
def toRepr (obj):
    args  = [ ]
    names = [ ]

    if hasattr(obj, "__dict__"):
        names += getattr(obj, "__dict__").keys()
    if hasattr(obj, "__slots__"):
        names += getattr(obj, "__slots__")

    for name in names:
        value = getattr(obj, name)
        if value is not None:
            if type(value) is str:
                if len(value) > 32:
                    value = value[0:32] + "..."
                value = "'" + value + "'"
        args.append("%s=%s" % (name, str(value)))

    return "%s(%s)" % (obj.__class__.__name__, ", ".join(args))

Response:
Convert the Python object to a string representation of the kind
    often returned by a class __repr__ method.


Instruction:
Can you implement a function in Python 3 that
returns a description of the given duration in the most appropriate units.

Response:
def toStringDuration (duration):
    """Returns a description of the given duration in the most appropriate
    units (e.g. seconds, ms, us, or ns).
    """

    table = (
        ('%dms'      , 1e-3, 1e3),
        (u'%d\u03BCs', 1e-6, 1e6),
        ('%dns'      , 1e-9, 1e9)
    )

    if duration > 1:
        return '%fs' % duration

    for format, threshold, factor in table:
        if duration > threshold:
            return format % int(duration * factor)

    return '%fs' % duration


Instruction:
Given the following Python 3 function, write the documentation
def expandPath (pathname, prefix=None):
    if prefix is None:
        prefix = ''

    expanded = pathname

    if pathname[0] == '~':
        expanded = os.path.expanduser(pathname)
    elif pathname[0] != '/':
        expanded = os.path.join(prefix, pathname)

    return os.path.abspath(expanded)

Response:
Return pathname as an absolute path either expanded by the users
    home directory or with prefix prepended.


Instruction:
How would you implement a function in Python 3 that
returns the list of all files within the input directory and all subdirectories.

Response:
def listAllFiles (directory, suffix=None, abspath=False):
    """Returns the list of all files within the input directory and
    all subdirectories.
    """
    files = []

    directory = expandPath(directory)

    for dirpath, dirnames, filenames in os.walk(directory, followlinks=True):
        if suffix:
            filenames = [f for f in filenames if f.endswith(suffix)]

        for filename in filenames:
            filepath = os.path.join(dirpath, filename)
            if not abspath:
                filepath = os.path.relpath(filepath, start=directory)

                # os.path.join(path, filename)

            files.append(filepath)

    return files


Instruction:
Write a Python 3 function that can
true if the cache needs to be updated False otherwise.

Response:
def dirty(self):
      """True if the cache needs to be updated, False otherwise"""
      return not os.path.exists(self.cachename) or \
            (os.path.getmtime(self.filename) >
             os.path.getmtime(self.cachename))


Instruction:
Create a Python 3 function for
caching the result of loader ( filename to cachename.

Response:
def cache(self):
      """Caches the result of loader(filename) to cachename."""
      msg = 'Saving updates from more recent "%s" to "%s"'
      log.info(msg, self.filename, self.cachename)
      with open(self.cachename, 'wb') as output:
          cPickle.dump(self._dict, output, -1)


Instruction:
Implement a Python 3 function for
loading the Python object from the cache file.

Response:
def load(self):
        """Loads the Python object

        Loads the Python object, either via loader(filename) or the
        pickled cache file, whichever was modified most recently.
        """
        if self._dict is None:
            if self.dirty:
                self._dict = self._loader(self.filename)
                self.cache()
            else:
                with open(self.cachename, 'rb') as stream:
                    self._dict = cPickle.load(stream)

        return self._dict


Instruction:
Can you generate the documentation for the following Python 3 function
def process(self, input_data, topic=None, **kwargs):
        try:
            split = input_data[1:-1].split(',', 1)
            uid, pkt = int(split[0]), split[1]
            defn = self.packet_dict[uid]
            decoded = tlm.Packet(defn, data=bytearray(pkt))
            self.dbconn.insert(decoded, **kwargs)
        except Exception as e:
            log.error('Data archival failed with error: {}.'.format(e))

Response: Process a message received from PacketHandler into a tuple and inserts it into the database backend.


Instruction:
Can you implement a function in Python 3 that
returns the current UTC time in seconds and microseconds.

Response:
def getTimestampUTC():
    """getTimestampUTC() -> (ts_sec, ts_usec)

    Returns the current UTC time in seconds and microseconds.
    """
    utc     = datetime.datetime.utcnow()
    ts_sec  = calendar.timegm( utc.timetuple() )
    ts_usec = utc.microsecond
    return ts_sec, ts_usec


Instruction:
How would you explain what the following Python 3 function does
def getUTCDatetimeDOY(days=0, hours=0, minutes=0, seconds=0):
    return (datetime.datetime.utcnow() +
        datetime.timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds)).strftime(DOY_Format)

Response: getUTCDatetimeDOY - Returns the UTC current datetime with the input timedelta arguments days hours minutes seconds


Instruction:
Here you have a function in Python 3, explain what it does
def toc():
    end = datetime.datetime.now()
    return totalSeconds( end - TICs.pop() ) if len(TICs) else None

Response: Returns the total elapsed seconds since the most recent tic call or None if tic was not called.


Instruction:
Write a Python 3 function that can
convert the given UTC timestamp to a two - tuple that is a two - tuple with the GPS week number and GPS seconds within the week.

Response:
def toGPSWeekAndSecs(timestamp=None):
    """Converts the given UTC timestamp (defaults to the current time) to
    a two-tuple, (GPS week number, GPS seconds within the week).
    """
    if timestamp is None:
        timestamp = datetime.datetime.utcnow()

    leap = LeapSeconds.get_GPS_offset_for_date(timestamp)

    secsInWeek = 604800
    delta      = totalSeconds(timestamp - GPS_Epoch) + leap
    seconds    = delta % secsInWeek
    week       = int( math.floor(delta / secsInWeek) )

    return (week, seconds)


Instruction:
How would you code a function in Python 3 to
convert the given Python datetime or Julian date to Greenwich Mean Sidereal Time in radians

Response:
def toGMST(dt=None):
    """Converts the given Python datetime or Julian date (float) to
    Greenwich Mean Sidereal Time (GMST) (in radians) using the formula
    from D.A. Vallado (2004).

    See:

        D.A. Vallado, Fundamentals of Astrodynamics and Applications, p. 192
        http://books.google.com/books?id=PJLlWzMBKjkC&lpg=PA956&vq=192&pg=PA192
    """
    if dt is None or type(dt) is datetime.datetime:
        jd = toJulian(dt)
    else:
        jd = dt

    tUT1  = (jd - 2451545.0) / 36525.0
    gmst  = 67310.54841 + (876600 * 3600 + 8640184.812866) * tUT1
    gmst += 0.093104 * tUT1**2
    gmst -= 6.2e-6   * tUT1**3

    # Convert from seconds to degrees, i.e.
    # 86400 seconds / 360 degrees = 240 seconds / degree
    gmst /= 240.

    # Convert to radians
    gmst  = math.radians(gmst) % TwoPi

    if gmst < 0:
        gmst += TwoPi

    return gmst


Instruction:
Can you generate the documentation for the following Python 3 function
def toJulian(dt=None):
    if dt is None:
        dt = datetime.datetime.utcnow()

    if dt.month < 3:
        year  = dt.year  -  1
        month = dt.month + 12
    else:
        year  = dt.year
        month = dt.month

    A   = int(year / 100.0)
    B   = 2 - A + int(A / 4.0)
    C   = ( (dt.second / 60.0 + dt.minute) / 60.0 + dt.hour ) / 24.0
    jd  = int(365.25  * (year + 4716))
    jd += int(30.6001 * (month + 1)) + dt.day + B - 1524.5 + C

    return jd

Response:
Converts a Python datetime to a Julian date using the formula from
    Meesus ( 2004.


Instruction:
Write a Python 3 function that can
convert the given number of seconds since the GPS Epoch to this computer s local time.

Response:
def toLocalTime(seconds, microseconds=0):
    """toLocalTime(seconds, microseconds=0) -> datetime

    Converts the given number of seconds since the GPS Epoch (midnight
    on January 6th, 1980) to this computer's local time.  Returns a
    Python datetime object.

    Examples:

    >>> toLocalTime(0)
    datetime.datetime(1980, 1, 6, 0, 0)

    >>> toLocalTime(25 * 86400)
    datetime.datetime(1980, 1, 31, 0, 0)
    """
    delta = datetime.timedelta(seconds=seconds, microseconds=microseconds)
    return GPS_Epoch + delta


Instruction:
Can you tell what is the following Python 3 function doing
def totalSeconds(td):
    if hasattr(td, "total_seconds"):
        ts = td.total_seconds()
    else:
        ts = (td.microseconds + (td.seconds + td.days * 24 * 3600.0) * 1e6) / 1e6

    return ts

Response:
Returns the total number of seconds contained in the given Python
    datetime. timedelta object.


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

        log.info('Attempting to acquire latest leapsecond data')

        ls_file = ait.config.get(
            'leapseconds.filename',
            os.path.join(ait.config._directory, _DEFAULT_FILE_NAME)
        )

        url = 'https://www.ietf.org/timezones/data/leap-seconds.list'
        r = requests.get(url)

        if r.status_code != 200:
            msg = 'Unable to locate latest timezone data. Connection to IETF failed'
            log.error(msg)
            raise ValueError(msg)

        text = r.text.split('\n')
        lines = [l for l in text if l.startswith('#@') or not l.startswith('#')]

        data = {'valid': None, 'leapseconds': []}
        data['valid'] = datetime.datetime(1900, 1, 1) + datetime.timedelta(seconds=int(lines[0].split('\t')[1]))

        leap = 1
        for l in lines[1:-1]:
            t = datetime.datetime(1900, 1, 1) + datetime.timedelta(seconds=int(l.split('\t')[0]))
            if t < GPS_Epoch:
                continue

            data['leapseconds'].append((t, leap))
            leap += 1

        self._data = data
        with open(ls_file, 'w') as outfile:
            pickle.dump(data, outfile)

Response: Updates the system leap second information from the system leap second list file and updates the leap second config file.


Instruction:
Can you generate the documentation for the following Python 3 function
def wait(self):
        for greenlet in (self.greenlets + self.servers):
            log.info("Starting {} greenlet...".format(greenlet))
            greenlet.start()

        gevent.joinall(self.greenlets)

Response: Starts all greenlets and joins all servers.


Instruction:
Given the following Python 3 function, write the documentation
def _load_streams(self):
        common_err_msg = 'No valid {} stream configurations found. '
        specific_err_msg = {'inbound': 'No data will be received (or displayed).',
                            'outbound': 'No data will be published.'}
        err_msgs = {}

        for stream_type in ['inbound', 'outbound']:
            err_msgs[stream_type] = common_err_msg.format(stream_type) + specific_err_msg[stream_type]
            streams = ait.config.get('server.{}-streams'.format(stream_type))

            if streams is None:
                log.warn(err_msgs[stream_type])
            else:
                for index, s in enumerate(streams):
                    try:
                        if stream_type == 'inbound':
                            strm = self._create_inbound_stream(s['stream'])
                            if type(strm) == PortInputStream:
                                self.servers.append(strm)
                            else:
                                self.inbound_streams.append(strm)
                        elif stream_type == 'outbound':
                            strm = self._create_outbound_stream(s['stream'])
                            self.outbound_streams.append(strm)
                        log.info('Added {} stream {}'.format(stream_type, strm))
                    except Exception:
                        exc_type, value, tb = sys.exc_info()
                        log.error('{} creating {} stream {}: {}'.format(exc_type,
                                                                        stream_type,
                                                                        index,
                                                                        value))
        if not self.inbound_streams and not self.servers:
            log.warn(err_msgs['inbound'])

        if not self.outbound_streams:
            log.warn(err_msgs['outbound'])

Response: Reads and creates streams specified in config. yaml.


Instruction:
Write a Python 3 script to
create an inbound stream from its config.

Response:
def _create_inbound_stream(self, config=None):
        """
        Creates an inbound stream from its config.

        Params:
            config:       stream configuration as read by ait.config
        Returns:
            stream:       a Stream
        Raises:
            ValueError:   if any of the required config values are missing
        """
        if config is None:
            raise ValueError('No stream config to create stream from.')

        name = self._get_stream_name(config)
        stream_handlers = self._get_stream_handlers(config, name)
        stream_input = config.get('input', None)
        if stream_input is None:
            raise(cfg.AitConfigMissing('inbound stream {}\'s input'.format(name)))

        if type(stream_input[0]) is int:
            return PortInputStream(name,
                                   stream_input,
                                   stream_handlers,
                                   zmq_args={'zmq_context': self.broker.context,
                                             'zmq_proxy_xsub_url': self.broker.XSUB_URL,
                                             'zmq_proxy_xpub_url': self.broker.XPUB_URL})
        else:
            return ZMQStream(name,
                             stream_input,
                             stream_handlers,
                             zmq_args={'zmq_context': self.broker.context,
                                       'zmq_proxy_xsub_url': self.broker.XSUB_URL,
                                       'zmq_proxy_xpub_url': self.broker.XPUB_URL})


Instruction:
Write a Python 3 function for
creating an outbound stream from its config.

Response:
def _create_outbound_stream(self, config=None):
        """
        Creates an outbound stream from its config.

        Params:
            config:       stream configuration as read by ait.config
        Returns:
            stream:       a Stream
        Raises:
            ValueError:   if any of the required config values are missing
        """
        if config is None:
            raise ValueError('No stream config to create stream from.')

        name = self._get_stream_name(config)
        stream_handlers = self._get_stream_handlers(config, name)
        stream_input = config.get('input', None)
        stream_output = config.get('output', None)

        if type(stream_output) is int:
            return PortOutputStream(name,
                                    stream_input,
                                    stream_output,
                                    stream_handlers,
                                    zmq_args={'zmq_context': self.broker.context,
                                              'zmq_proxy_xsub_url': self.broker.XSUB_URL,
                                              'zmq_proxy_xpub_url': self.broker.XPUB_URL})
        else:
            if stream_output is not None:
                log.warn("Output of stream {} is not an integer port. "
                         "Stream outputs can only be ports.".format(name))
            return ZMQStream(name,
                             stream_input,
                             stream_handlers,
                             zmq_args={'zmq_context': self.broker.context,
                                       'zmq_proxy_xsub_url': self.broker.XSUB_URL,
                                       'zmq_proxy_xpub_url': self.broker.XPUB_URL})


Instruction:
Implement a function in Python 3 to
create a handler from its config.

Response:
def _create_handler(self, config):
        """
        Creates a handler from its config.

        Params:
            config:      handler config
        Returns:
            handler instance
        """
        if config is None:
            raise ValueError('No handler config to create handler from.')

        if 'name' not in config:
            raise ValueError('Handler name is required.')

        handler_name = config['name']
        # try to create handler
        module_name = handler_name.rsplit('.', 1)[0]
        class_name = handler_name.rsplit('.', 1)[-1]
        module = import_module(module_name)
        handler_class = getattr(module, class_name)
        instance = handler_class(**config)

        return instance


Instruction:
Here you have a function in Python 3, explain what it does
def _load_plugins(self):
        plugins = ait.config.get('server.plugins')

        if plugins is None:
            log.warn('No plugins specified in config.')
        else:
            for index, p in enumerate(plugins):
                try:
                    plugin = self._create_plugin(p['plugin'])
                    self.plugins.append(plugin)
                    log.info('Added plugin {}'.format(plugin))

                except Exception:
                    exc_type, value, tb = sys.exc_info()
                    log.error('{} creating plugin {}: {}'.format(exc_type,
                                                                 index,
                                                                 value))
            if not self.plugins:
                log.warn('No valid plugin configurations found. No plugins will be added.')

Response: Reads parses and creates plugins specified in config. yaml.


Instruction:
Given the following Python 3 function, write the documentation
def _create_plugin(self, config):
        if config is None:
            raise ValueError('No plugin config to create plugin from.')

        name = config.pop('name', None)
        if name is None:
            raise(cfg.AitConfigMissing('plugin name'))

        # TODO I don't think we actually care about this being unique? Left over from
        # previous conversations about stuff?
        module_name = name.rsplit('.', 1)[0]
        class_name = name.rsplit('.', 1)[-1]
        if class_name in [x.name for x in (self.outbound_streams +
                                           self.inbound_streams +
                                           self.servers +
                                           self.plugins)]:
            raise ValueError(
                'Plugin "{}" already loaded. Only one plugin of a given name is allowed'.
                format(class_name)
            )

        plugin_inputs = config.pop('inputs', None)
        if plugin_inputs is None:
            log.warn('No plugin inputs specified for {}'.format(name))
            plugin_inputs = [ ]

        subscribers = config.pop('outputs', None)
        if subscribers is None:
            log.warn('No plugin outputs specified for {}'.format(name))
            subscribers = [ ]

        # try to create plugin
        module = import_module(module_name)
        plugin_class = getattr(module, class_name)
        instance = plugin_class(plugin_inputs,
                                subscribers,
                                zmq_args={'zmq_context': self.broker.context,
                                          'zmq_proxy_xsub_url': self.broker.XSUB_URL,
                                          'zmq_proxy_xpub_url': self.broker.XPUB_URL},
                                **config
        )

        return instance

Response: Create a new plugin from its config.


Instruction:
Explain what the following Python 3 code does
def slotsToJSON(obj, slots=None):
    if slots is None:
        slots = list(obj.__slots__) if hasattr(obj, '__slots__') else [ ]
        for base in obj.__class__.__bases__:
            if hasattr(base, '__slots__'):
                slots.extend(base.__slots__)

    testOmit = hasattr(obj, '__jsonOmit__') and callable(obj.__jsonOmit__)
    result   = { }

    for slot in slots:
        key = slot[1:] if slot.startswith('_') else slot
        val = getattr(obj, slot, None)

        if testOmit is False or obj.__jsonOmit__(key, val) is False:
            result[key] = toJSON(val)

    return result

Response:
Converts the given Python object to one suitable for Javascript
    Object Notation ( JSON serialization via json. dumps.


Instruction:
Explain what the following Python 3 code does
def toJSON (obj):
    if hasattr(obj, 'toJSON') and callable(obj.toJSON):
        result = obj.toJSON()
    elif isinstance(obj, (int, long, float, str, unicode)) or obj is None:
        result = obj
    elif isinstance(obj, collections.Mapping):
        result = { toJSON(key): toJSON(obj[key]) for key in obj }
    elif isinstance(obj, collections.Sequence):
        result = [ toJSON(item) for item in obj ]
    else:
        result = str(obj)

    return result

Response:
Converts a Python object to one suitable for Javascript
    Object Notation ( JSON serialization via JSON.


Instruction:
Create a Python 3 function to
loop ait. config. _datapaths from AIT_CONFIG and creates a directory structure.

Response:
def createDirStruct(paths, verbose=True):
    '''Loops ait.config._datapaths from AIT_CONFIG and creates a directory.

    Replaces year and doy with the respective year and day-of-year.
    If neither are given as arguments, current UTC day and year are used.

    Args:
        paths:
            [optional] list of directory paths you would like to create.
            doy and year will be replaced by the datetime day and year, respectively.

        datetime:
            UTC Datetime string in ISO 8601 Format YYYY-MM-DDTHH:mm:ssZ

    '''
    for k, path in paths.items():
        p = None
        try:
            pathlist = path if type(path) is list else [ path ]
            for p in pathlist:
                os.makedirs(p)
                if verbose:
                    log.info('Creating directory: ' + p)
        except OSError, e:
            #print path
            if e.errno == errno.EEXIST and os.path.isdir(p):
                pass
            else:
                raise

    return True


Instruction:
Can you write a function in Python 3 where it
writes packet data to the logger s log file.

Response:
def capture_packet(self):
        ''' Write packet data to the logger's log file. '''
        data = self.socket.recv(self._buffer_size)

        for h in self.capture_handlers:
            h['reads'] += 1
            h['data_read'] += len(data)

            d = data
            if 'pre_write_transforms' in h:
                for data_transform in h['pre_write_transforms']:
                    d = data_transform(d)
            h['logger'].write(d)


Instruction:
Given the following Python 3 function, write the documentation
def clean_up(self):
        ''' Clean up the socket and log file handles. '''
        self.socket.close()
        for h in self.capture_handlers:
            h['logger'].close()

Response: Clean up the socket and log file handles.


Instruction:
Write a Python 3 function that can
monitor the socket and log captured data.

Response:
def socket_monitor_loop(self):
        ''' Monitor the socket and log captured data. '''
        try:
            while True:
                gevent.socket.wait_read(self.socket.fileno())

                self._handle_log_rotations()
                self.capture_packet()
        finally:
            self.clean_up()


Instruction:
Write a Python 3 function for
adding an additional handler for the

Response:
def add_handler(self, handler):
        ''' Add an additional handler

        Args:
            handler:
                A dictionary of handler configuration for the handler
                that should be added. See :func:`__init__` for details
                on valid parameters.
        '''
        handler['logger'] = self._get_logger(handler)
        handler['reads'] = 0
        handler['data_read'] = 0

        self.capture_handlers.append(handler)


Instruction:
Create a Python 3 function for
removing a handler given a name.

Response:
def remove_handler(self, name):
        ''' Remove a handler given a name

        Note, if multiple handlers have the same name the last matching
        instance in the handler list will be removed.

        Args:
            name:
                The name of the handler to remove
        '''
        index = None
        for i, h in enumerate(self.capture_handlers):
            if h['name'] == name:
                index = i

        if index is not None:
            self.capture_handlers[index]['logger'].close()
            del self.capture_handlers[index]


Instruction:
Create a Python 3 function for
returning a dictionary of capture handler configuration data.

Response:
def dump_handler_config_data(self):
        ''' Return capture handler configuration data.

        Return a dictionary of capture handler configuration data of the form:

        .. code-block:: none

            [{
                'handler': <handler configuration dictionary>,

                'log_file_path': <Path to the current log file that the logger
                    is writing. Note that if rotation is used it\'s possible
                    this data will be stale eventually.>,

                'conn_type': <The string defining the connection type of the
                    logger.>,

                'address': <The list containing address info that the logger is
                    using for its connection.>
            }, ...]

        '''
        ignored_keys = ['logger', 'log_rot_time', 'reads', 'data_read']
        config_data = []
        for h in self.capture_handlers:
            config_data.append({
                'handler': {
                    k:v for k, v in h.iteritems()
                    if k not in ignored_keys
                },
                'log_file_path': h['logger']._stream.name,
                'conn_type': self.conn_type,
                'address': self.address,
            })
        return config_data


Instruction:
Make a summary of the following Python 3 code
def dump_all_handler_stats(self):
        ''' Return handler capture statistics

        Return a dictionary of capture handler statistics of the form:

        .. code-block:: none

            [{
                'name': The handler's name,

                'reads': The number of packet reads this handler has received

                'data_read_length': The total length of the data received

                'approx_data_rate': The approximate data rate for this handler
            }, ...]

        '''
        stats = []
        for h in self.capture_handlers:
            now = calendar.timegm(time.gmtime())
            rot_time = calendar.timegm(h['log_rot_time'])
            time_delta = now - rot_time
            approx_data_rate = '{} bytes/second'.format(h['data_read'] / float(time_delta))

            stats.append({
                'name': h['name'],
                'reads': h['reads'],
                'data_read_length': '{} bytes'.format(h['data_read']),
                'approx_data_rate': approx_data_rate
            })

        return stats

Response: Dump the capture statistics for all handler in the system.


Instruction:
How would you implement a function in Python 3 that
rotates each handler s log file if necessary

Response:
def _handle_log_rotations(self):
        ''' Rotate each handler's log file if necessary '''
        for h in self.capture_handlers:
            if self._should_rotate_log(h):
                self._rotate_log(h)


Instruction:
Explain what the following Python 3 code does
def _should_rotate_log(self, handler):
        ''' Determine if a log file rotation is necessary '''
        if handler['rotate_log']:
            rotate_time_index = handler.get('rotate_log_index', 'day')
            try:
                rotate_time_index = self._decode_time_rotation_index(rotate_time_index)
            except ValueError:
                rotate_time_index = 2

            rotate_time_delta = handler.get('rotate_log_delta', 1)

            cur_t = time.gmtime()
            first_different_index = 9
            for i in range(9):
                if cur_t[i] != handler['log_rot_time'][i]:
                    first_different_index = i
                    break

            if first_different_index < rotate_time_index:
                # If the time deltas differ by a time step greater than what we
                # have set for the rotation (I.e., months instead of days) we will
                # automatically rotate.
                return True
            else:
                time_delta = cur_t[rotate_time_index] - handler['log_rot_time'][rotate_time_index]
                return time_delta >= rotate_time_delta

        return False

Response: Determine if a log file rotation is necessary


Instruction:
Create a Python 3 function to
return the time struct index to use for log rotation checks

Response:
def _decode_time_rotation_index(self, time_rot_index):
        ''' Return the time struct index to use for log rotation checks '''
        time_index_decode_table = {
            'year': 0,    'years': 0,    'tm_year': 0,
            'month': 1,   'months': 1,   'tm_mon': 1,
            'day': 2,     'days': 2,     'tm_mday': 2,
            'hour': 3,    'hours': 3,    'tm_hour': 3,
            'minute': 4,  'minutes': 4,  'tm_min': 4,
            'second': 5,  'seconds': 5,  'tm_sec': 5,
        }

        if time_rot_index not in time_index_decode_table.keys():
            raise ValueError('Invalid time option specified for log rotation')

        return time_index_decode_table[time_rot_index]


Instruction:
How would you implement a function in Python 3 that
generates a log file path for a given handler.

Response:
def _get_log_file(self, handler):
        ''' Generate log file path for a given handler

        Args:
            handler:
                The handler configuration dictionary for which a log file
                path should be generated.
        '''
        if 'file_name_pattern' not in handler:
            filename = '%Y-%m-%d-%H-%M-%S-{name}.pcap'
        else:
            filename = handler['file_name_pattern']

        log_file = handler['log_dir']
        if 'path' in handler:
            log_file = os.path.join(log_file, handler['path'], filename)
        else:
            log_file = os.path.join(log_file, filename)

        log_file = time.strftime(log_file, time.gmtime())
        log_file = log_file.format(**handler)

        return log_file


Instruction:
Can you implement a function in Python 3 that
initializes a PCAP stream for logging data

Response:
def _get_logger(self, handler):
        ''' Initialize a PCAP stream for logging data '''
        log_file = self._get_log_file(handler)

        if not os.path.isdir(os.path.dirname(log_file)):
            os.makedirs(os.path.dirname(log_file))

        handler['log_rot_time'] = time.gmtime()
        return pcap.open(log_file, mode='a')


Instruction:
Can you create a Python 3 function that
adds a new capturer to the manager.

Response:
def add_logger(self, name, address, conn_type, log_dir_path=None, **kwargs):
        ''' Add a new stream capturer to the manager.

        Add a new stream capturer to the manager with the provided configuration
        details. If an existing capturer is monitoring the same address the
        new handler will be added to it.

        Args:
            name:
                A string defining the new capturer's name.

            address:
                A tuple containing address data for the capturer. Check the
                :class:`SocketStreamCapturer` documentation for what is
                required.

            conn_type:
                A string defining the connection type. Check the
                :class:`SocketStreamCapturer` documentation for a list of valid
                options.

            log_dir_path:
                An optional path defining the directory where the
                capturer should write its files. If this isn't provided the root
                log directory from the manager configuration is used.

        '''
        capture_handler_conf = kwargs

        if not log_dir_path:
            log_dir_path = self._mngr_conf['root_log_directory']

        log_dir_path = os.path.normpath(os.path.expanduser(log_dir_path))

        capture_handler_conf['log_dir'] = log_dir_path
        capture_handler_conf['name'] = name
        if 'rotate_log' not in capture_handler_conf:
            capture_handler_conf['rotate_log'] = True

        transforms = []
        if 'pre_write_transforms' in capture_handler_conf:
            for transform in capture_handler_conf['pre_write_transforms']:
                if isinstance(transform, str):
                    if globals().has_key(transform):
                        transforms.append(globals().get(transform))
                    else:
                        msg = (
                            'Unable to load data transformation '
                            '"{}" for handler "{}"'
                        ).format(
                            transform,
                            capture_handler_conf['name']
                        )
                        log.warn(msg)
                elif hasattr(transform, '__call__'):
                    transforms.append(transform)
                else:
                    msg = (
                        'Unable to determine how to load data transform "{}"'
                    ).format(transform)
                    log.warn(msg)
        capture_handler_conf['pre_write_transforms'] = transforms

        address_key = str(address)
        if address_key in self._stream_capturers:
            capturer = self._stream_capturers[address_key][0]
            capturer.add_handler(capture_handler_conf)
            return

        socket_logger = SocketStreamCapturer(capture_handler_conf,
                                             address,
                                             conn_type)
        greenlet = gevent.spawn(socket_logger.socket_monitor_loop)

        self._stream_capturers[address_key] = (
            socket_logger,
            greenlet
        )
        self._pool.add(greenlet)


Instruction:
Can you create a Python 3 function that
stops capture handler with a given name.

Response:
def stop_capture_handler(self, name):
        ''' Remove all handlers with a given name

        Args:
            name:
                The name of the handler(s) to remove.
        '''
        empty_capturers_indeces = []
        for k, sc in self._stream_capturers.iteritems():
            stream_capturer = sc[0]
            stream_capturer.remove_handler(name)

            if stream_capturer.handler_count == 0:
                self._pool.killone(sc[1])
                empty_capturers_indeces.append(k)

        for i in empty_capturers_indeces:
            del self._stream_capturers[i]


Instruction:
How would you code a function in Python 3 to
stop a capturer that the manager controls.

Response:
def stop_stream_capturer(self, address):
        ''' Stop a capturer that the manager controls.

        Args:
            address:
                An address array of the form ['host', 'port'] or similar
                depending on the connection type of the stream capturer being
                terminated. The capturer for the address will be terminated
                along with all handlers for that capturer if the address is
                that of a managed capturer.

        Raises:
            ValueError:
                The provided address doesn't match a capturer that is
                currently managed.
        '''
        address = str(address)
        if address not in self._stream_capturers:
            raise ValueError('Capturer address does not match a managed capturer')

        stream_cap = self._stream_capturers[address]
        self._pool.killone(stream_cap[1])
        del self._stream_capturers[address]


Instruction:
Write a Python 3 function that can
force a rotation of a handler s log file

Response:
def rotate_capture_handler_log(self, name):
        ''' Force a rotation of a handler's log file

        Args:
            name:
                The name of the handler who's log file should be rotated.
        '''
        for sc_key, sc in self._stream_capturers.iteritems():
            for h in sc[0].capture_handlers:
                if h['name'] == name:
                    sc[0]._rotate_log(h)


Instruction:
Explain what the following Python 3 code does
def get_logger_data(self):
        ''' Return data on managed loggers.

        Returns a dictionary of managed logger configuration data. The format
        is primarily controlled by the
        :func:`SocketStreamCapturer.dump_handler_config_data` function::

            {
                <capture address>: <list of handler config for data capturers>
            }

        '''
        return {
            address : stream_capturer[0].dump_handler_config_data()
            for address, stream_capturer in self._stream_capturers.iteritems()
        }

Response:
Return data on managed loggers.
        Returns a dictionary of managed loggers.


Instruction:
Write a Python 3 script for
returning handler read statistics Returns a dictionary of managed handler read statistics. The format is primarily controlled by the SocketStreamCapturer. dump_all_handler_stats

Response:
def get_handler_stats(self):
        ''' Return handler read statistics

        Returns a dictionary of managed handler data read statistics. The
        format is primarily controlled by the
        :func:`SocketStreamCapturer.dump_all_handler_stats` function::

            {
                <capture address>: <list of handler capture statistics>
            }

        '''
        return {
            address : stream_capturer[0].dump_all_handler_stats()
            for address, stream_capturer in self._stream_capturers.iteritems()
        }


Instruction:
Explain what the following Python 3 code does
def get_capture_handler_config_by_name(self, name):
        ''' Return data for handlers of a given name.

        Args:
            name:
                Name of the capture handler(s) to return config data for.

        Returns:
            Dictionary dump from the named capture handler as given by
            the :func:`SocketStreamCapturer.dump_handler_config_data` method.
        '''
        handler_confs = []
        for address, stream_capturer in self._stream_capturers.iteritems():
            handler_data = stream_capturer[0].dump_handler_config_data()
            for h in handler_data:
                if h['handler']['name'] == name:
                    handler_confs.append(h)

        return handler_confs

Response: Return data for a given name.


Instruction:
How would you explain what the following Python 3 function does
def run_socket_event_loop(self):
        ''' Start monitoring managed loggers. '''
        try:
            while True:
                self._pool.join()

                # If we have no loggers we'll sleep briefly to ensure that we
                # allow other processes (I.e., the webserver) to do their work.
                if len(self._logger_data.keys()) == 0:
                    time.sleep(0.5)

        except KeyboardInterrupt:
            pass
        finally:
            self._pool.kill()

Response: Start monitoring managed loggers.


Instruction:
Can you tell what is the following Python 3 function doing
def _route(self):
        ''' Handles server route instantiation. '''
        self._app.route('/',
                        method='GET',
                        callback=self._get_logger_list)
        self._app.route('/stats',
                        method='GET',
                        callback=self._fetch_handler_stats)
        self._app.route('/<name>/start',
                        method='POST',
                        callback=self._add_logger_by_name)
        self._app.route('/<name>/stop',
                        method='DELETE',
                        callback=self._stop_logger_by_name)
        self._app.route('/<name>/config',
                        method='GET',
                        callback=self._get_logger_conf)
        self._app.route('/<name>/rotate',
                        method='POST',
                        callback=self._rotate_capturer_log)

Response: Handles server route instantiation.


Instruction:
How would you code a function in Python 3 to
handle POST requests for adding a new logger.

Response:
def _add_logger_by_name(self, name):
        ''' Handles POST requests for adding a new logger.

        Expects logger configuration to be passed in the request's query string.
        The logger name is included in the URL and the address components and
        connection type should be included as well. The loc attribute is
        defaulted to "localhost" when making the socket connection if not
        defined.

        loc = IP / interface
        port = port / protocol
        conn_type = udp or ethernet

        Raises:
            ValueError:
                if the port or connection type are not supplied.
        '''
        data = dict(request.forms)
        loc = data.pop('loc', '')
        port = data.pop('port', None)
        conn_type = data.pop('conn_type', None)

        if not port or not conn_type:
            e = 'Port and/or conn_type not set'
            raise ValueError(e)
        address = [loc, int(port)]

        if 'rotate_log' in data:
            data['rotate_log'] = True if data == 'true' else False

        if 'rotate_log_delta' in data:
            data['rotate_log_delta'] = int(data['rotate_log_delta'])

        self._logger_manager.add_logger(name, address, conn_type, **data)


Instruction:
How would you explain what the following Python 3 function does
def handle_includes(defns):
    '''Recursive handling of includes for any input list of defns.
    The assumption here is that when an include is handled by the
    pyyaml reader, it adds them as a list, which is stands apart from the rest
    of the expected YAML definitions.
    '''
    newdefns = []
    for d in defns:
        if isinstance(d,list):
            newdefns.extend(handle_includes(d))
        else:
            newdefns.append(d)

    return newdefns

Response: Recursive handling of includes for any input list of defns.


Instruction:
Write a Python 3 function that can
return the result of evaluating this DNToEUConversion in the context of the given Packet.

Response:
def eval(self, packet):
        """Returns the result of evaluating this DNToEUConversion in the
        context of the given Packet.
        """
        result = None
        terms  = None

        if self._when is None or self._when.eval(packet):
            result = self._equation.eval(packet)

        return result


Instruction:
Write a Python 3 script to
return True if the given field value is valid False otherwise.

Response:
def validate(self, value, messages=None):
        """Returns True if the given field value is valid, False otherwise.
        Validation error messages are appended to an optional messages
        array.
        """
        valid     = True
        primitive = value

        def log(msg):
            if messages is not None:
                messages.append(msg)

        if self.enum:
            if value not in self.enum.values():
                valid = False
                flds = (self.name, str(value))
                log("%s value '%s' not in allowed enumerated values." % flds)
            else:
                primitive = int(self.enum.keys()[self.enum.values().index(value)])

        if self.type:
            if self.type.validate(primitive, messages, self.name) is False:
                valid = False

        return valid


Instruction:
Write a Python 3 function that can
decode the given bytes according to this Field Definition.

Response:
def decode(self, bytes, raw=False, index=None):
        """Decodes the given bytes according to this Field Definition.

        If raw is True, no enumeration substitutions will be applied
        to the data returned.

        If index is an integer or slice (and the type of this
        FieldDefinition is an ArrayType), then only the element(s) at
        the specified position(s) will be decoded.
        """
        if index is not None and isinstance(self.type, dtype.ArrayType):
            value = self.type.decode( bytes[self.slice()], index, raw )
        else:
            value = self.type.decode( bytes[self.slice()], raw )

        # Apply bit mask if needed
        if self.mask is not None:
            value &= self.mask

        if self.shift > 0:
            value >>= self.shift

        if not raw and self.enum is not None:
            value = self.enum.get(value, value)

        return value


Instruction:
Given the following Python 3 function, write the documentation
def encode(self, value):
        if type(value) == str and self.enum and value in self.enum:
            value = self.enum[value]

        if type(value) == int:
            if self.shift > 0:
                value <<= self.shift
            if self.mask is not None:
                value &= self.mask

        return self.type.encode(value) if self.type else bytearray()

Response: Encodes the given value according to this FieldDefinition.


Instruction:
Explain what the following Python 3 code does
def slice(self, offset=0):
        if self.bytes is None:
            start = 0
            stop  = start + self.nbytes
        elif type(self.bytes) is int:
            start = self.bytes
            stop  = start + self.nbytes
        else:
            start = self.bytes[0]
            stop  = self.bytes[1] + 1

        return slice(start + offset, stop + offset)

Response: Returns a Python slice object that is used to retrieve the start and stop byte positions of this Telemetry field.


Instruction:
Can you tell what is the following Python 3 function doing
def _assertField(self, fieldname):
        if not self._hasattr(fieldname):
            values = self._defn.name, fieldname
            raise AttributeError("Packet '%s' has no field '%s'" % values)

Response: Raise AttributeError when Packet has no field with the given fieldname.


Instruction:
Write a Python 3 script for
returning the value of the given field name.

Response:
def _getattr (self, fieldname, raw=False, index=None):
        """Returns the value of the given packet field name.

        If raw is True, the field value is only decoded.  That is no
        enumeration substituions or DN to EU conversions are applied.
        """
        self._assertField(fieldname)
        value = None

        if fieldname == 'raw':
            value = createRawPacket(self)
        elif fieldname == 'history':
            value = self._defn.history
        else:
            if fieldname in self._defn.derivationmap:
                defn = self._defn.derivationmap[fieldname]
            else:
                defn = self._defn.fieldmap[fieldname]

            if isinstance(defn.type, dtype.ArrayType) and index is None:
                return createFieldList(self, defn, raw)

            if defn.when is None or defn.when.eval(self):
                if isinstance(defn, DerivationDefinition):
                    value = defn.equation.eval(self)
                elif raw or (defn.dntoeu is None and defn.expr is None):
                    value = defn.decode(self._data, raw, index)
                elif defn.dntoeu is not None:
                    value = defn.dntoeu.eval(self)
                elif defn.expr is not None:
                    value = defn.expr.eval(self)

        return value


Instruction:
Can you write a function in Python 3 where it
returns True if this packet contains fieldname False otherwise.

Response:
def _hasattr(self, fieldname):
        """Returns True if this packet contains fieldname, False otherwise."""
        special = 'history', 'raw'
        return (fieldname in special or
                fieldname in self._defn.fieldmap or
                fieldname in self._defn.derivationmap)


Instruction:
Make a summary of the following Python 3 code
def nbytes(self):
        max_byte = -1

        for defn in self.fields:
            byte = defn.bytes if type(defn.bytes) is int else max(defn.bytes)
            max_byte = max(max_byte, byte)

        return max_byte + 1

Response: The number of bytes for this telemetry packet


Instruction:
Write a Python 3 script for
returning True if the given Packet is valid False otherwise.

Response:
def validate(self, pkt, messages=None):
        """Returns True if the given Packet is valid, False otherwise.
        Validation error messages are appended to an optional messages
        array.
        """
        valid = True

        for f in self.fields:
            try:
                value = getattr(pkt, f.name)
            except AttributeError:
                valid = False
                if messages is not None:
                    msg = "Telemetry field mismatch for packet '%s'.  "
                    msg += "Unable to retrieve value for %s in Packet."
                    values = self.name, f.name
                    messages.append(msg % values)
                break

            if f.validate(value, messages) is False:
                valid = False

        return valid


Instruction:
Can you tell what is the following Python 3 function doing
def eval(self, packet):
        try:
            context = createPacketContext(packet)
            result  = eval(self._code, packet._defn.globals, context)
        except ZeroDivisionError:
            result = None

        return result

Response:
Returns the result of evaluating this PacketExpression in the
            context of the given Packet.


Instruction:
Can you implement a function in Python 3 that
raises AttributeError when PacketHistory has no field with the given name.

Response:
def _assertField(self, name):
        """Raise AttributeError when PacketHistory has no field with the given
        name.
        """
        if name not in self._names:
            msg    = 'PacketHistory "%s" has no field "%s"'
            values = self._defn.name, name
            raise AttributeError(msg % values)


Instruction:
How would you implement a function in Python 3 that
adds the given Packet to this PacketHistory.

Response:
def add(self, packet):
        """Add the given Packet to this PacketHistory."""
        for name in self._names:
            value = getattr(packet, name)
            if value is not None:
                self._dict[name] = value


Instruction:
How would you explain what the following Python 3 function does
def add(self, defn):
        if defn.name not in self:
            self[defn.name] = defn
        else:
            msg = "Duplicate packet name '%s'" % defn.name
            log.error(msg)
            raise util.YAMLError(msg)

Response: Adds the given Packet Definition to this Telemetry Dictionary.


Instruction:
Can you tell what is the following Python 3 function doing
def create(self, name, data=None):
        return createPacket(self[name], data) if name in self else None

Response: Creates a new packet with the given definition and raw data.


Instruction:
Create a Python 3 function to
load Packet Definitions from the given YAML content into this Telemetry Dictionary.

Response:
def load(self, content):
        """Loads Packet Definitions from the given YAML content into this
        Telemetry Dictionary.  Content may be either a filename
        containing YAML content or a YAML string.

        Load has no effect if this Command Dictionary was already
        instantiated with a filename or YAML content.
        """
        if self.filename is None:
            if os.path.isfile(content):
                self.filename = content
                stream        = open(self.filename, 'rb')
            else:
                stream        = content

            pkts = yaml.load(stream)
            pkts = handle_includes(pkts)
            for pkt in pkts:
                self.add(pkt)

            if type(stream) is file:
                stream.close()


Instruction:
How would you explain what the following Python 3 function does
def writeToCSV(self, output_path=None):
        '''writeToCSV - write the telemetry dictionary to csv
        '''
        header = ['Name', 'First Byte', 'Last Byte', 'Bit Mask', 'Endian',
                  'Type', 'Description', 'Values']

        if output_path is None:
            output_path = ait.config._directory

        for pkt_name in self.tlmdict:
            filename = os.path.join(output_path, pkt_name + '.csv')

            with open(filename, 'wb') as output:
                csvwriter = csv.writer(output, quoting=csv.QUOTE_ALL)
                csvwriter.writerow(header)

                for fld in self.tlmdict[pkt_name].fields:
                    # Pre-process some fields

                    # Description
                    desc = fld.desc.replace('\n', ' ') if fld.desc is not None else ""

                    # Mask
                    mask = hex(fld.mask) if fld.mask is not None else ""

                    # Enumerations
                    enums = '\n'.join("%s: %s" % (k, fld.enum[k])
                            for k in fld.enum) if fld.enum is not None else ""

                    # Set row
                    row = [fld.name, fld.slice().start, fld.slice().stop,
                           mask, fld.type.endian, fld.type.name, desc, enums]

                    csvwriter.writerow(row)

Response: writeToCSV - write the telemetry dictionary to CSV


Instruction:
Can you generate a brief explanation for the following Python 3 code
def decode(self, bytes):
        value = self.type.decode(bytes)
        if self._enum is not None:
            for name, val in self._enum.items():
                if value == val:
                    value = name
                    break
        return value

Response:
Decodes the given bytes according to this AIT Argument
        Definition.


Instruction:
How would you explain what the following Python 3 function does
def encode(self, value):
        if type(value) == str and self.enum and value in self.enum:
            value = self.enum[value]
        return self.type.encode(value) if self.type else bytearray()

Response:
Encodes the given value according to this AIT Argument
        Definition.


Instruction:
Write a Python 3 function that can
return True if the given Argument value is valid False otherwise.

Response:
def validate(self, value, messages=None):
        """Returns True if the given Argument value is valid, False otherwise.
        Validation error messages are appended to an optional messages
        array.
        """
        valid     = True
        primitive = value

        def log(msg):
            if messages is not None:
                messages.append(msg)

        if self.enum:
            if value not in self.enum.keys():
                valid = False
                args  = (self.name, str(value))
                log("%s value '%s' not in allowed enumerated values." % args)
            else:
                primitive = int(self.enum[value])

        if self.type:
            if self.type.validate(primitive, messages, self.name) is False:
                valid = False

        if self.range:
            if primitive < self.range[0] or primitive > self.range[1]:
                valid = False
                args  = (self.name, str(primitive), self.range[0], self.range[1])
                log("%s value '%s' out of range [%d, %d]." % args)

        return valid


Instruction:
How would you code a function in Python 3 to
encode this AIT command to binary.

Response:
def encode(self, pad=106):
        """Encodes this AIT command to binary.

        If pad is specified, it indicates the maximum size of the encoded
        command in bytes.  If the encoded command is less than pad, the
        remaining bytes are set to zero.

        Commands sent to ISS payloads over 1553 are limited to 64 words
        (128 bytes) with 11 words (22 bytes) of CCSDS overhead (SSP
        52050J, Section 3.2.3.4).  This leaves 53 words (106 bytes) for
        the command itself.
        """
        opcode  = struct.pack('>H', self.defn.opcode)
        offset  = len(opcode)
        size    = max(offset + self.defn.argsize, pad)
        encoded = bytearray(size)

        encoded[0:offset] = opcode
        encoded[offset]   = self.defn.argsize
        offset           += 1
        index             = 0

        for defn in self.defn.argdefns:
            if defn.fixed:
                value = defn.value
            else:
                value  = self.args[index]
                index += 1
            encoded[defn.slice(offset)] = defn.encode(value)

        return encoded


Instruction:
Can you generate a brief explanation for the following Python 3 code
def nbytes(self):
        return len(self.opcode) + 1 + sum(arg.nbytes for arg in self.argdefns)

Response: The number of bytes required to encode this command.


Instruction:
Can you generate the documentation for the following Python 3 function
def argsize(self):
        argsize = sum(arg.nbytes for arg in self.argdefns)
        return argsize if len(self.argdefns) > 0 else 0

Response: The total size in bytes of all the command arguments.


Instruction:
Explain what the following Python 3 code does
def validate(self, cmd, messages=None):
        valid = True
        args  = [ arg for arg in cmd.args if arg is not None ]

        if self.nargs != len(args):
            valid = False
            if messages is not None:
                msg  = 'Expected %d arguments, but received %d.'
                messages.append(msg % (self.nargs, len(args)))

        for defn, value in zip(self.args, cmd.args):
            if value is None:
                valid = False
                if messages is not None:
                    messages.append('Argument "%s" is missing.' % defn.name)
            elif defn.validate(value, messages) is False:
                valid = False

        if len(cmd._unrecognized) > 0:
            valid = False
            if messages is not None:
                for name in cmd.unrecognized:
                    messages.append('Argument "%s" is unrecognized.' % name)

        return valid

Response: Returns True if the given Command is valid False otherwise. Messages are appended to an optional messages array. Messages are appended to an optional messages array. Messages are appended to an optional messages array.


Instruction:
How would you code a function in Python 3 to
add the given Command Definition to this Command Dictionary.

Response:
def add(self, defn):
        """Adds the given Command Definition to this Command Dictionary."""
        self[defn.name]            = defn
        self.opcodes[defn._opcode] = defn


Instruction:
Here you have a function in Python 3, explain what it does
def create(self, name, *args, **kwargs):
        tokens = name.split()

        if len(tokens) > 1 and (len(args) > 0 or len(kwargs) > 0):
            msg  = 'A Cmd may be created with either positional arguments '
            msg += '(passed as a string or a Python list) or keyword '
            msg += 'arguments, but not both.'
            raise TypeError(msg)

        if len(tokens) > 1:
            name = tokens[0]
            args = [ util.toNumber(t, t) for t in tokens[1:] ]

        defn = self.get(name, None)

        if defn is None:
            raise TypeError('Unrecognized command: %s' % name)

        return createCmd(defn, *args, **kwargs)

Response: Creates a new AIT command with the given arguments.


Instruction:
Can you tell what is the following Python 3 function doing
def decode(self, bytes):
        opcode  = struct.unpack(">H", bytes[0:2])[0]
        nbytes  = struct.unpack("B",  bytes[2:3])[0]
        name   = None
        args   = []

        if opcode in self.opcodes:
            defn = self.opcodes[opcode]
            name = defn.name
            stop = 3

            for arg in defn.argdefns:
                start = stop
                stop  = start + arg.nbytes
                if arg.fixed:
                    pass  # FIXME: Confirm fixed bytes are as expected?
                else:
                    args.append(arg.decode(bytes[start:stop]))

        return self.create(name, *args)

Response:
Decodes the given bytes according to this AIT Command
        Definition.


Instruction:
How would you code a function in Python 3 to
load Command Definitions from the given YAML content into this Command Dictionary.

Response:
def load(self, content):
        """Loads Command Definitions from the given YAML content into
        into this Command Dictionary.  Content may be either a
        filename containing YAML content or a YAML string.

        Load has no effect if this Command Dictionary was already
        instantiated with a filename or YAML content.
        """
        if self.filename is None:
            if os.path.isfile(content):
                self.filename = content
                stream        = open(self.filename, 'rb')
            else:
                stream        = content

            for cmd in yaml.load(stream):
                self.add(cmd)

            if type(stream) is file:
                stream.close()


Instruction:
Write a Python 3 script for
returning the cube root of x.

Response:
def cbrt (x):
  """Returns the cube root of x."""
  if x >= 0:
    return   math.pow(x     , 1.0 / 3.0)
  else:
    return - math.pow(abs(x), 1.0 / 3.0)


Instruction:
Implement a function in Python 3 to
convert the given ECI coordinates to ECEF at the given Greenwich Mean Sidereal Time.

Response:
def eci2ecef (x, y, z, gmst=None):
  """Converts the given ECI coordinates to ECEF at the given Greenwich
  Mean Sidereal Time (GMST) (defaults to now).

  This code was adapted from
  `shashwatak/satellite-js <https://github.com/shashwatak/satellite-js/blob/master/src/coordinate-transforms.js>`_
  and http://ccar.colorado.edu/ASEN5070/handouts/coordsys.doc

  """
  if gmst is None:
    gmst = dmc.toGMST()

  X = (x * math.cos(gmst))    + (y * math.sin(gmst))
  Y = (x * (-math.sin(gmst))) + (y * math.cos(gmst))
  Z = z

  return X, Y, Z


Instruction:
Can you generate the documentation for the following Python 3 function
def eci2geodetic (x, y, z, gmst=None, ellipsoid=None):
  if gmst is None:
    gmst = dmc.toGMST()

  if ellipsoid is None:
    ellipsoid = WGS84

  a    = WGS84.a
  b    = WGS84.b
  f    = WGS84.f
  r    = math.sqrt((x * x) + (y * y))
  e2   = (2 * f) - (f * f)
  lon  = math.atan2(y, x) - gmst
  k    = 0
  kmax = 20
  lat  = math.atan2(z, r)

  while (k < kmax):
    slat = math.sin(lat)
    C    = 1 / math.sqrt( 1 - e2 * (slat * slat) )
    lat  = math.atan2(z + (a * C * e2 * slat), r)
    k   += 1

  z = (r / math.cos(lat)) - (a * C)

  return lat, lon, z

Response:
Converts the given ECI coordinates to Geodetic coordinates at the given GMST and with
.


Instruction:
Implement a Python 3 function for
processing the input data and publishes the final output data.

Response:
def process(self, input_data, topic=None):
        """
        Invokes each handler in sequence.
        Publishes final output data.

        Params:
            input_data:  message received by stream
            topic:       name of plugin or stream message received from,
                         if applicable
        """
        for handler in self.handlers:
            output = handler.handle(input_data)
            input_data = output

        self.publish(input_data)


Instruction:
Make a summary of the following Python 3 code
def valid_workflow(self):
        for ix, handler in enumerate(self.handlers[:-1]):
            next_input_type = self.handlers[ix + 1].input_type

            if (handler.output_type is not None and
                    next_input_type is not None):
                if handler.output_type != next_input_type:
                    return False

        return True

Response: Returns True if the workflow is valid.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def compress (input_filename, output_filename=None, verbose=False):
  input_size  = 0
  output_size = 0

  if output_filename is None:
    output_filename = input_fillename + '.ait-zlib'

  try:
    stream     = open(input_filename , 'rb')
    output     = open(output_filename, 'wb')
    bytes      = stream.read()
    input_size = len(bytes)

    if verbose:
      log.info("Compressing %s (%d bytes).", input_filename, input_size)

    compressed  = zlib.compress(bytes, 3)
    output_size = len(compressed)
    output.write(compressed)

    stream.close()
    output.close()

    percent = (1.0 - (output_size / float(input_size) )) * 100

    if verbose:
      log.info("Wrote %s (%d bytes).", output_filename, output_size)
      log.info("Compressed %6.2f percent", percent)

  except (IOError, OSError), e:
    log.error(str(e) + ".")

  return output_size

Response: Compresses the input file and stores the result in output_filename.


Instruction:
Write a Python 3 script for
dumping bytes in hexdump format.

Response:
def hexdump (bytes, addr=None, preamble=None, printfunc=None, stepsize=16):
  """hexdump(bytes[, addr[, preamble[, printfunc[, stepsize=16]]]])

  Outputs bytes in hexdump format lines similar to the following (here
  preamble='Bank1', stepsize=8, and len(bytes) == 15)::

    Bank1: 0xFD020000: 7f45  4c46  0102  0100  *.ELF....*
    Bank1: 0xFD020008: 0000  0000  0000  00    *....... *

  Where stepsize controls the number of bytes per line.  If addr is
  omitted, the address portion of the hexdump will not be output.
  Lines will be passed to printfunc for output, or Python's builtin
  print, if printfunc is omitted.

  If a byte is not in the range [32, 127), a period will rendered for
  the character portion of the output.
  """
  if preamble is None:
    preamble = ""

  bytes = bytearray(bytes)
  size  = len(bytes)

  for n in xrange(0, size, stepsize):
    if addr is not None:
      dump = preamble + "0x%04X: " % (addr + n)
    else:
      dump = preamble
    end   = min(size, n + stepsize)
    dump += hexdumpLine(bytes[n:end], stepsize)

    if printfunc is None:
      print dump
    else:
      printfunc(dump)


Instruction:
Create a Python 3 function for
returning a single hexdump formatted line for the given bytes.

Response:
def hexdumpLine (bytes, length=None):
  """hexdumpLine(bytes[, length])

  Returns a single hexdump formatted line for bytes.  If length is
  greater than len(bytes), the line will be padded with ASCII space
  characters to indicate no byte data is present.

  Used by hexdump().
  """
  line = ""

  if length is None:
    length = len(bytes)

  for n in xrange(0, length, 2):
    if n < len(bytes) - 1:
      line += "%02x%02x  " % (bytes[n], bytes[n + 1])
    elif n < len(bytes):
      line += "%02x    "   % bytes[n]
    else:
      line += "      "

  line += "*"

  for n in xrange(length):
    if n < len(bytes):
      if bytes[n] in xrange(32, 127):
        line += "%c" % bytes[n]
      else:
        line += "."
    else:
      line += " "

  line += "*"
  return line


Instruction:
Create a Python 3 function for
parsing command - line arguments according to the given defaults.

Response:
def parseArgs (argv, defaults):
  """parseArgs(argv, defaults) -> (dict, list)

  Parses command-line arguments according to the given defaults.  For
  every key in defaults, an argument of the form --key=value will be
  parsed.  Numeric arguments are converted from strings with errors
  reported via ait.core.log.error() and default values used instead.

  Returns a copy of defaults with parsed option values and a list of
  any non-flag arguments.
  """
  options = dict(defaults)
  numeric = \
    [ k for k, v in options.items() if type(v) is float or type(v) is int ]

  try:
    longopts   = [ "%s=" % key for key in options.keys() ]
    opts, args = getopt.getopt(argv, "", longopts)

    for key, value in opts:
      if key.startswith("--"):
        key = key[2:]
      options[key] = value
  except getopt.GetoptError, err:
    log.error( str(err)  )
    usage( exit=True )

  for key in numeric:
    value = options[key]
    if type(value) is str:
      options[key] = util.toNumber(value)

    if options[key] is None:
      msg = "Option '%s': '%s' is not a number, using default '%s' instead."
      log.error(msg, key, value, defaults[key])
      options[key] = defaults[key]

  return options, args


Instruction:
Here you have a function in Python 3, explain what it does
def usage (exit=False):
  stream = open(sys.argv[0])
  for line in stream.readlines():
    if line.startswith("##"): print line.replace("##", ""),
  stream.close()

  if exit:
    sys.exit(2)

Response: Print the usage statement at the top of a Python program.


Instruction:
How would you implement a function in Python 3 that
gets the IP address of the computer.

Response:
def getip():
  """
  getip()

  Returns the IP address of the computer. Helpful for those hosts that might
  sit behind gateways and report a hostname that is a little strange (I'm
  looking at you oco3-sim1).
  """
  return [(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]


Instruction:
Write a Python 3 script for
parsing the arguments using argparse and returns a Namespace object.

Response:
def arg_parse(arguments, description=None):
  """
  arg_parse()

  Parses the arguments using argparse. Returns a Namespace object. The
  arguments dictionary should match the argparse expected data structure:

  .. code-block::python

    arguments = {
      '--port': {
        'type'    : int,
        'default' : 3075,
        'help'    : 'Port on which to send data'
      },
      '--verbose': {
        'action'  : 'store_true',
        'default' : False,
        'help'    : 'Hexdump of the raw command being sent.'
      }
    }

  For positional arguments, be sure to pass in an OrderedDict:

  .. code-block::python

    arguments = {
      '--port': {
        'type'    : int,
        'default' : 3075,
        'help'    : 'Port on which to send data'
      },
      '--verbose': {
        'action'  : 'store_true',
        'default' : False,
        'help'    : 'Hexdump of the raw command being sent.'
      }
    }

    arguments['command'] = {
      'type' : str,
      'help' : 'Name of the command to send.'
    }

    arguments['arguments'] = {
      'type'      : util.toNumberOrStr,
      'metavar'   : 'argument',
      'nargs'     : '*',
      'help'      : 'Command arguments.'
    }

  """
  if not description:
    description = ""

  ap  = argparse.ArgumentParser(
    description = description,
    formatter_class = argparse.ArgumentDefaultsHelpFormatter
  )

  for name, params in arguments.items():
    ap.add_argument(name, **params)

  args = ap.parse_args()
  return args


Instruction:
Can you write a function in Python 3 where it
types for argparse - checks that file exists but does not open.

Response:
def extant_file(file):
    """
    'Type' for argparse - checks that file exists but does not open.
    """
    if not os.path.exists(file):
        # Argparse uses the ArgumentTypeError to give a rejection message like:
        # error: argument input: file does not exist
        raise argparse.ArgumentTypeError("{0} does not exist".format(file))
    return file


Instruction:
Given the following Python 3 function, write the documentation
def dot (self, other):
    if self.z:
      return (self.x * other.x) + (self.y * other.y) + (self.z * other.z)
    else:
      return (self.x * other.x) + (self.y * other.y)

Response: Returns the dot product of this Point with another.


Instruction:
How would you explain what the following Python 3 function does
def slope (self):
    return (self.p.y - self.q.y) / (self.p.x - self.q.x)

Response: Returns the slope of the current node.


Instruction:
How would you code a function in Python 3 to
intersect two line segments.

Response:
def intersect (self, line):
    """intersect (line) -> Point | None

    Returns the intersection point of this line segment with another.
    If this line segment and the other line segment are conincident,
    the first point on this line segment is returned.  If the line
    segments do not intersect, None is returned.

    See http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/

    Examples:

    >>> A = Line( Point(0.0, 0.0), Point(5.0, 5.0) )
    >>> B = Line( Point(5.0, 0.0), Point(0.0, 5.0) )
    >>> C = Line( Point(1.0, 3.0), Point(9.0, 3.0) )
    >>> D = Line( Point(0.5, 3.0), Point(6.0, 4.0) )
    >>> E = Line( Point(1.0, 1.0), Point(3.0, 8.0) )
    >>> F = Line( Point(0.5, 2.0), Point(4.0, 7.0) )
    >>> G = Line( Point(1.0, 2.0), Point(3.0, 6.0) )
    >>> H = Line( Point(2.0, 4.0), Point(4.0, 8.0) )
    >>> I = Line( Point(3.5, 9.0), Point(3.5, 0.5) )
    >>> J = Line( Point(3.0, 1.0), Point(9.0, 1.0) )
    >>> K = Line( Point(2.0, 3.0), Point(7.0, 9.0) )
    >>> L = Line( Point(1.0, 2.0), Point(5.0, 7.0) )

    >>> A.intersect(B)
    Point(2.5, 2.5)

    >>> C.intersect(D) == None
    True

    >>> E.intersect(F)
    Point(1.8275862069, 3.89655172414)

    >>> G.intersect(H)
    Point(1.0, 2.0)

    >>> I.intersect(J)
    Point(3.5, 1.0)

    >>> K.intersect(L) == None
    True
    """
    (x1, y1)  = (self.p.x, self.p.y)
    (x2, y2)  = (self.q.x, self.q.y)
    (x3, y3)  = (line.p.x, line.p.y)
    (x4, y4)  = (line.q.x, line.q.y)
    denom     = ((y4 - y3) * (x2 - x1)) - ((x4 - x3) * (y2 - y1))
    num1      = ((x4 - x3) * (y1 - y3)) - ((y4 - y3) * (x1 - x3))
    num2      = ((x2 - x1) * (y1 - y3)) - ((y2 - y1) * (x1 - x3))
    intersect = None

    if num1 == 0 and num2 == 0 and denom == 0:    # Coincident lines
      intersect = self.p
    elif denom != 0:                              # Parallel lines (denom == 0)
      ua = float(num1) / denom
      ub = float(num2) / denom
      if ua >= 0.0 and ua <= 1.0 and ub >= 0.0 and ub <= 1.0:
        x = x1 + (ua * (x2 - x1))
        y = y1 + (ua * (y2 - y1))
        intersect = Point(x, y)

    return intersect


Instruction:
Here you have a function in Python 3, explain what it does
def intersect (self, line):
     eps   = 1e-8
     d     = (line.q - line.p)
     dn    = d.dot(self.n)
     point = None

     if abs(dn) >= eps:
       mu = self.n.dot(self.p - line.p) / dn
       if mu >= 0 and mu <= 1:
         point = line.p + mu * d

     return point

Response:
intersect - Returns the point at which the line segment and Plane intersect
     or None if the line segment and Plane intersect
     does not intersect.


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

    for segment in self.segments():
      area += ((segment.p.x * segment.q.y) - (segment.q.x * segment.p.y))/2

    return area

Response: area - Returns the area of this Polygon.


Instruction:
Here you have a function in Python 3, explain what it does
def bounds (self):
    if self._dirty:
      min = self.vertices[0].copy()
      max = self.vertices[0].copy()
      for point in self.vertices[1:]:
        if point.x < min.x: min.x = point.x
        if point.y < min.y: min.y = point.y
        if point.x > max.x: max.x = point.x
        if point.y > max.y: max.y = point.y

      self._bounds = Rect(min, max)
    self._dirty  = False

    return self._bounds

Response: Returns the bounding rectangle for this Polygon.


Instruction:
Given the following Python 3 function, write the documentation
def center (self):
    Cx    = 0.0
    Cy    = 0.0
    denom = 6.0 * self.area()

    for segment in self.segments():
      x   = (segment.p.x + segment.q.x)
      y   = (segment.p.y + segment.q.y)
      xy  = (segment.p.x * segment.q.y) - (segment.q.x * segment.p.y)
      Cx += (x * xy)
      Cy += (y * xy)

    Cx /= denom
    Cy /= denom

    return Point(Cx, Cy)

Response: Returns the center point of this Polygon.


Instruction:
Create a Python 3 function to
return True if point is contained inside this Polygon False otherwise.

Response:
def contains (self, p):
    """Returns True if point is contained inside this Polygon, False
    otherwise.

    This method uses the Ray Casting algorithm.

    Examples:

    >>> p = Polygon()
    >>> p.vertices = [Point(1, 1), Point(1, -1), Point(-1, -1), Point(-1, 1)]

    >>> p.contains( Point(0, 0) )
    True

    >>> p.contains( Point(2, 3) )
    False

    """
    inside = False

    if p in self.bounds():
      for s in self.segments():
        if ((s.p.y > p.y) != (s.q.y > p.y) and
            (p.x < (s.q.x - s.p.x) * (p.y - s.p.y) / (s.q.y - s.p.y) + s.p.x)):
          inside = not inside

    return inside


Instruction:
Implement a Python 3 function for
returning the Line segments that comprise this Polygon.

Response:
def segments (self):
    """Return the Line segments that comprise this Polygon."""
    for n in xrange(len(self.vertices) - 1):
      yield Line(self.vertices[n], self.vertices[n + 1])

    yield Line(self.vertices[-1], self.vertices[0])


Instruction:
Implement a function in Python 3 to
return True if point is contained inside this Rectangle False otherwise.

Response:
def contains (self, point):
    """contains(point) -> True | False

    Returns True if point is contained inside this Rectangle, False otherwise.

    Examples:

    >>> r = Rect( Point(-1, -1), Point(1, 1) )
    >>> r.contains( Point(0, 0) )
    True

    >>> r.contains( Point(2, 3) )
    False
    """
    return (point.x >= self.ul.x and point.x <= self.lr.x) and \
           (point.y >= self.ul.y and point.y <= self.lr.y)


Instruction:
Write a Python 3 script for
returning a list of Line segments that comprise this Rectangle.

Response:
def segments (self):
    """segments () -> [ Line, Line, Line, Line ]

    Return a list of Line segments that comprise this Rectangle.
    """
    ul = self.ul
    lr = self.lr
    ur = Point(lr.x, ul.y)
    ll = Point(ul.x, lr.y)
    return [ Line(ul, ur), Line(ur, lr), Line(lr, ll), Line(ll, ul) ]


Instruction:
How would you code a function in Python 3 to
format an EVR message with the given byte array of EVR data.

Response:
def format_message(self, evr_hist_data):
        ''' Format EVR message with EVR data

        Given a byte array of EVR data, format the EVR's message attribute
        printf format strings and split the byte array into appropriately
        sized chunks. Supports most format strings containing length and type
        fields.

        Args:
            evr_hist_data: A bytearray of EVR data. Bytes are expected to be in
             MSB ordering.

            Example formatting::

                # This is the character '!', string 'Foo', and int '4279317316'
                bytearray([0x21, 0x46, 0x6f, 0x6f, 0x00, 0xff, 0x11, 0x33, 0x44])

        Returns:
            The EVR's message string formatted with the EVR data or the
            unformatted EVR message string if there are no valid format
            strings present in it.

        Raises:
            ValueError: When the bytearray cannot be fully processed with the
                specified format strings. This is usually a result of the
                expected data length and the byte array length not matching.
        '''
        size_formatter_info = {
            's' : -1,
            'c' : 1,
            'i' : 4,
            'd' : 4,
            'u' : 4,
            'x' : 4,
            'hh': 1,
            'h' : 2,
            'l' : 4,
            'll': 8,
            'f' : 8,
            'g' : 8,
            'e' : 8,
        }
        type_formatter_info = {
            'c' : 'U{}',
            'i' : 'MSB_I{}',
            'd' : 'MSB_I{}',
            'u' : 'MSB_U{}',
            'f' : 'MSB_D{}',
            'e' : 'MSB_D{}',
            'g' : 'MSB_D{}',
            'x' : 'MSB_U{}',
        }

        formatters = re.findall("%(?:\d+\$)?([cdieEfgGosuxXhlL]+)", self._message)

        cur_byte_index = 0
        data_chunks = []

        for f in formatters:
            # If the format string we found is > 1 character we know that a length
            # field is included and we need to adjust our sizing accordingly.
            f_size_char = f_type = f[-1]
            if len(f) > 1:
                f_size_char = f[:-1]

            fsize = size_formatter_info[f_size_char.lower()]

            try:
                if f_type != 's':
                    end_index = cur_byte_index + fsize
                    fstr = type_formatter_info[f_type.lower()].format(fsize*8)

                    # Type formatting can give us incorrect format strings when
                    # a size formatter promotes a smaller data type. For instnace,
                    # 'hhu' says we'll promote a char (1 byte) to an unsigned
                    # int for display. Here, the type format string would be
                    # incorrectly set to 'MSB_U8' if we didn't correct.
                    if fsize == 1 and 'MSB_' in fstr:
                        fstr = fstr[4:]

                    d = dtype.PrimitiveType(fstr).decode(
                        evr_hist_data[cur_byte_index:end_index]
                    )

                # Some formatters have an undefined data size (such as strings)
                # and require additional processing to determine the length of
                # the data and decode data.
                else:
                    end_index = str(evr_hist_data).index('\x00', cur_byte_index)
                    d = str(evr_hist_data[cur_byte_index:end_index])

                data_chunks.append(d)
            except:
                msg = "Unable to format EVR Message with data {}".format(evr_hist_data)
                log.error(msg)
                raise ValueError(msg)

            cur_byte_index = end_index

            # If we were formatting a string we need to add another index offset
            # to exclude the null terminator.
            if f == 's':
                cur_byte_index += 1

        # Format and return the EVR message if formatters were present, otherwise
        # just return the EVR message as is.
        if len(formatters) == 0:
            return self._message
        else:
            # Python format strings cannot handle size formatter information. So something
            # such as %llu needs to be adjusted to be a valid identifier in python by
            # removing the size formatter.
            msg = self._message
            for f in formatters:
                if len(f) > 1:
                    msg = msg.replace('%{}'.format(f), '%{}'.format(f[-1]))

            return msg % tuple(data_chunks)


Instruction:
Can you generate the documentation for the following Python 3 function
def _parseHeader (self, line, lineno, log):
    if line.startswith('#') and line.find(':') > 0:
      tokens = [ t.strip().lower() for t in line[1:].split(":", 1) ]
      name   = tokens[0]
      pos    = SeqPos(line, lineno)

      if name in self.header:
        msg = 'Ignoring duplicate header parameter: %s'
        log.warning(msg % name, pos)
      else:
        for expected in ['seqid', 'version']:
          if name == expected:
            value = util.toNumber(tokens[1], None)
            if value is None:
              msg = 'Parameter "%s" value "%s" is not a number.'
              log.error(msg % (name, tokens[1]), poss)
            else:
              self.header[name] = value

Response: Parses a sequence header line containing name value pairs.


Instruction:
Implement a Python 3 function for
adding a new command to this sequence.

Response:
def append (self, cmd, delay=0.000, attrs=None):
    """Adds a new command with a relative time delay to this sequence."""
    self.lines.append( SeqCmd(cmd, delay, attrs) )


Instruction:
Given the following Python 3 function, write the documentation
def printText (self, stream=None):
    if stream is None:
      stream = sys.stdout

    stream.write('# seqid   : %u\n'     % self.seqid         )
    stream.write('# version : %u\n'     % self.version       )
    stream.write('# crc32   : 0x%04x\n' % self.crc32         )
    stream.write('# ncmds   : %u\n'     % len(self.commands) )
    stream.write('# duration: %.3fs\n'  % self.duration      )
    stream.write('\n')

    for line in self.lines:
      stream.write( str(line) )
      stream.write('\n')

Response: Prints a text representation of this sequence to the given stream.


Instruction:
Write a Python 3 function that can
read a command sequence from the given filename.

Response:
def read (self, filename=None):
    """Reads a command sequence from the given filename (defaults to
    self.pathname).
    """
    if filename is None:
      filename = self.pathname

    stream = open(filename, 'rb')
    magic  = struct.unpack('>H', stream.read(2))[0]
    stream.close()

    if magic == Seq.Magic:
      self.readBinary(filename)
    else:
      self.readText(filename)


Instruction:
How would you explain what the following Python 3 function does
def readBinary (self, filename=None):
    if filename is None:
      filename = self.pathname

    stream       = open(filename, 'rb')
    magic        = struct.unpack('>H', stream.read(2))[0]
    self.crc32   = struct.unpack('>I', stream.read(4))[0]
    self.seqid   = struct.unpack('>H', stream.read(2))[0]
    self.version = struct.unpack('>H', stream.read(2))[0]
    ncmds        = struct.unpack('>H', stream.read(2))[0]
    reserved     = stream.read(20)

    for n in range(ncmds):
      bytes = stream.read(110)
      self.lines.append( SeqCmd.decode(bytes, self.cmddict) )

Response: Reads a binary command sequence from the given file.


Instruction:
Can you write a function in Python 3 where it
reads a text command sequence from the given file.

Response:
def readText (self, filename=None):
    """Reads a text command sequence from the given filename (defaults to
    self.pathname).
    """
    if filename is None:
      filename = self.pathname

    self.header = { }
    inBody      = False

    with open(filename, 'rt') as stream:
      for (lineno, line) in enumerate(stream.readlines()):
        stripped = line.strip()
        if stripped == '':
          continue
        elif stripped.startswith('#'):
          if not inBody:
            self._parseHeader(line, lineno, self.log)
        else:
          inBody = True
          self.lines.append( SeqCmd.parse(line, lineno, self.log, self.cmddict) )

    if 'seqid' in self.header:
      self.seqid = self.header['seqid']
    elif self.seqid is None:
      self.log.error('No sequence id present in header.')

    if 'version' in self.header:
      self.version = self.header['version']
    elif self.version is None:
      self.log.warning('No version present in header.  Defaulting to zero (0).')
      self.version = 0


Instruction:
Explain what the following Python 3 code does
def validate (self):
    if not os.path.isfile(self.pathname):
      self.message.append('Filename "%s" does not exist.')
    else:
      try:
        with open(self.pathname, 'r') as stream:
          pass
      except IOError:
        self.messages.append('Could not open "%s" for reading.' % self.pathname)

    for line in self.commands:
      messages = [ ]
      if line.cmd and not line.cmd.validate(messages):
        msg = 'error: %s: %s' % (line.cmd.name, " ".join(messages))
        self.log.messages.append(msg)

    return len(self.log.messages) == 0

Response: Returns True if this Sequence is valid False otherwise.