Instruction:
How would you implement a function in Python 3 that
calculates JD using various leap - year calculation methods.

Response:
def _to_jd_schematic(year, month, day, method):
    '''Calculate JD using various leap-year calculation methods'''

    y0, y1, y2, y3, y4, y5 = 0, 0, 0, 0, 0, 0

    intercal_cycle_yrs, over_cycle_yrs, leap_suppression_yrs = None, None, None

    # Use the every-four-years method below year 16 (madler) or below 15 (romme)
    if ((method in (100, 'romme') and year < 15) or
            (method in (128, 'madler') and year < 17)):
        method = 4

    if method in (4, 'continuous'):
        # Leap years: 15, 19, 23, ...
        y5 = -365

    elif method in (100, 'romme'):
        year = year - 13
        y5 = DAYS_IN_YEAR * 12 + 3

        leap_suppression_yrs = 100.
        leap_suppression_days = 36524  # leap_cycle_days * 25 - 1

        intercal_cycle_yrs = 400.
        intercal_cycle_days = 146097  # leap_suppression_days * 4 + 1

        over_cycle_yrs = 4000.
        over_cycle_days = 1460969  # intercal_cycle_days * 10 - 1

    elif method in (128, 'madler'):
        year = year - 17
        y5 = DAYS_IN_YEAR * 16 + 4

        leap_suppression_days = 46751  # 32 * leap_cycle_days - 1
        leap_suppression_yrs = 128

    else:
        raise ValueError("Unknown leap year method. Try: continuous, romme, madler or equinox")

    if over_cycle_yrs:
        y0 = trunc(year / over_cycle_yrs) * over_cycle_days
        year = year % over_cycle_yrs

    # count intercalary cycles in days (400 years long or None)
    if intercal_cycle_yrs:
        y1 = trunc(year / intercal_cycle_yrs) * intercal_cycle_days
        year = year % intercal_cycle_yrs

    # count leap suppresion cycles in days (100 or 128 years long)
    if leap_suppression_yrs:
        y2 = trunc(year / leap_suppression_yrs) * leap_suppression_days
        year = year % leap_suppression_yrs

    y3 = trunc(year / LEAP_CYCLE_YEARS) * LEAP_CYCLE_DAYS
    year = year % LEAP_CYCLE_YEARS

    # Adjust 'year' by one to account for lack of year 0
    y4 = year * DAYS_IN_YEAR

    yj = y0 + y1 + y2 + y3 + y4 + y5

    mj = (month - 1) * 30

    return EPOCH + yj + mj + day - 1


Instruction:
Given the following Python 3 function, write the documentation
def from_jd(jd, method=None):
    '''Calculate date in the French Revolutionary
    calendar from Julian day.  The five or six
    "sansculottides" are considered a thirteenth
    month in the results of this function.'''
    method = method or 'equinox'

    if method == 'equinox':
        return _from_jd_equinox(jd)

    else:
        return _from_jd_schematic(jd, method)

Response:
Calculate date in the French Revolutionary
    calendar from Julian day.


Instruction:
Here you have a function in Python 3, explain what it does
def _from_jd_schematic(jd, method):
    '''Convert from JD using various leap-year calculation methods'''
    if jd < EPOCH:
        raise ValueError("Can't convert days before the French Revolution")

    # days since Epoch
    J = trunc(jd) + 0.5 - EPOCH

    y0, y1, y2, y3, y4, y5 = 0, 0, 0, 0, 0, 0
    intercal_cycle_days = leap_suppression_days = over_cycle_days = None

    # Use the every-four-years method below year 17
    if (J <= DAYS_IN_YEAR * 12 + 3 and
            method in (100, 'romme')) or (J <= DAYS_IN_YEAR * 17 + 4 and method in (128, 'madler')):
        method = 4

    # set p and r in Hatcher algorithm
    if method in (4, 'continuous'):
        # Leap years: 15, 19, 23, ...
        # Reorganize so that leap day is last day of cycle
        J = J + 365
        y5 = - 1

    elif method in (100, 'romme'):
        # Year 15 is not a leap year
        # Year 16 is leap, then multiples of 4, not multiples of 100, yes multiples of 400
        y5 = 12
        J = J - DAYS_IN_YEAR * 12 - 3

        leap_suppression_yrs = 100.
        leap_suppression_days = 36524  # LEAP_CYCLE_DAYS * 25 - 1

        intercal_cycle_yrs = 400.
        intercal_cycle_days = 146097  # leap_suppression_days * 4 + 1

        over_cycle_yrs = 4000.
        over_cycle_days = 1460969  # intercal_cycle_days * 10 - 1

    elif method in (128, 'madler'):
        # Year 15 is a leap year, then year 20 and multiples of 4, not multiples of 128
        y5 = 16
        J = J - DAYS_IN_YEAR * 16 - 4

        leap_suppression_yrs = 128
        leap_suppression_days = 46751  # 32 * leap_cycle_days - 1

    else:
        raise ValueError("Unknown leap year method. Try: continuous, romme, madler or equinox")

    if over_cycle_days:
        y0 = trunc(J / over_cycle_days) * over_cycle_yrs
        J = J % over_cycle_days

    if intercal_cycle_days:
        y1 = trunc(J / intercal_cycle_days) * intercal_cycle_yrs
        J = J % intercal_cycle_days

    if leap_suppression_days:
        y2 = trunc(J / leap_suppression_days) * leap_suppression_yrs
        J = J % leap_suppression_days

    y3 = trunc(J / LEAP_CYCLE_DAYS) * LEAP_CYCLE_YEARS

    if J % LEAP_CYCLE_DAYS == LEAP_CYCLE_DAYS - 1:
        J = 1460
    else:
        J = J % LEAP_CYCLE_DAYS

    # 0 <= J <= 1460
    # J needs to be 365 here on leap days ONLY

    y4 = trunc(J / DAYS_IN_YEAR)

    if J == DAYS_IN_YEAR * 4:
        y4 = y4 - 1
        J = 365.0
    else:
        J = J % DAYS_IN_YEAR

    year = y0 + y1 + y2 + y3 + y4 + y5

    month = trunc(J / 30.)
    J = J - month * 30

    return year + 1, month + 1, trunc(J) + 1

Response: Convert from Julian day using various leap - year calculation methods


Instruction:
Can you implement a function in Python 3 that
calculates the FR day using the equinox as day 1

Response:
def _from_jd_equinox(jd):
    '''Calculate the FR day using the equinox as day 1'''
    jd = trunc(jd) + 0.5
    equinoxe = premier_da_la_annee(jd)

    an = gregorian.from_jd(equinoxe)[0] - YEAR_EPOCH
    mois = trunc((jd - equinoxe) / 30.) + 1
    jour = int((jd - equinoxe) % 30) + 1

    return (an, mois, jour)


Instruction:
Implement a function in Python 3 to
obtain Julian day for Indian Civil date

Response:
def to_jd(year, month, day):
    '''Obtain Julian day for Indian Civil date'''

    gyear = year + 78
    leap = isleap(gyear)
    # // Is this a leap year ?

    # 22 - leap = 21 if leap, 22 non-leap
    start = gregorian.to_jd(gyear, 3, 22 - leap)
    if leap:
        Caitra = 31
    else:
        Caitra = 30

    if month == 1:
        jd = start + (day - 1)
    else:
        jd = start + Caitra
        m = month - 2
        m = min(m, 5)
        jd += m * 31
        if month >= 8:
            m = month - 7
            jd += m * 30

        jd += day - 1

    return jd


Instruction:
Make a summary of the following Python 3 code
def from_jd(jd):
    '''Calculate Indian Civil date from Julian day
    Offset in years from Saka era to Gregorian epoch'''

    start = 80
    # Day offset between Saka and Gregorian

    jd = trunc(jd) + 0.5
    greg = gregorian.from_jd(jd)  # Gregorian date for Julian day
    leap = isleap(greg[0])  # Is this a leap year?
    # Tentative year in Saka era
    year = greg[0] - SAKA_EPOCH
    # JD at start of Gregorian year
    greg0 = gregorian.to_jd(greg[0], 1, 1)
    yday = jd - greg0  # Day number (0 based) in Gregorian year

    if leap:
        Caitra = 31  # Days in Caitra this year
    else:
        Caitra = 30

    if yday < start:
        # Day is at the end of the preceding Saka year
        year -= 1
        yday += Caitra + (31 * 5) + (30 * 3) + 10 + start

    yday -= start
    if yday < Caitra:
        month = 1
        day = yday + 1
    else:
        mday = yday - Caitra
        if (mday < (31 * 5)):
            month = trunc(mday / 31) + 2
            day = (mday % 31) + 1
        else:
            mday -= 31 * 5
            month = trunc(mday / 30) + 7
            day = (mday % 30) + 1

    return (year, month, int(day))

Response: Calculate Indian Civil date from Julian day in Saka era to Gregorian epoch


Instruction:
Can you tell what is the following Python 3 function doing
def format_stack(skip=0, length=6, _sep=os.path.sep):
    return ' < '.join("%s:%s:%s" % (
        '/'.join(f.f_code.co_filename.split(_sep)[-2:]),
        f.f_lineno,
        f.f_code.co_name
    ) for f in islice(frame_iterator(sys._getframe(1 + skip)), length))

Response: Returns a one - line string with the current callstack.


Instruction:
Create a Python 3 function to
decorate `func` to have logging. Args func (function): Function to decorate. If missing log returns a partial which you can use as a decorator. stacktrace (int): Number of frames to show. stacktrace_align (int): Column to align the framelist to. attributes (list): List of instance attributes to show, in case the function is a instance method. module (bool): Show the module. call (bool): If ``True``, then show calls. If ``False`` only show the call details on exceptions (if ``exception`` is enabled) (default: ``True``) call_args (bool): If ``True``, then show call arguments. (default: ``True``) call_args_repr (bool): Function to convert one argument to a string. (default: ``repr``) result (bool): If ``True``, then show result. (default: ``True``) exception (bool): If ``True``, then show exceptions. (default: ``True``) exception_repr (function): Function to convert an exception to a string. (default: ``repr``) result_repr (function): Function to convert the result object to a string. (default: ``strip_non_ascii`` - like ``str`` but nonascii characters are replaced with dots.) use_logging (string): Emit log messages with the given loglevel. (default: ``"CRITICAL"``) print_to (fileobject): File object to write to, in case you don't want to use logging module. (default: ``None`` - printing is disabled) Returns: A decorator or a wrapper. Example:: >>> @log(print_to=sys.stdout) ... def a(weird=False): ... if weird: ... raise RuntimeError('BOOM!') >>> a() a() <<< ... a => None >>> try: ... a(weird=True) ... except Exception: ... pass # naughty code! a(weird=True) <<< ... a ~ raised RuntimeError('BOOM!',) You can conveniently use this to logs just errors, or just results, example:: >>> import aspectlib >>> with aspectlib.weave(float, log(call=False, result=False, print_to=sys.stdout)): ... try: ... float('invalid') ... except Exception as e: ... pass # naughty code! float('invalid') <<< ... float ~ raised ValueError(...float...invalid...) This makes debugging naughty code easier. PS: Without the weaving it looks like this:: >>> try: ... log(call=False, result=False, print_to=sys.stdout)(float)('invalid') ... except Exception: ... pass # naughty code! float('invalid') <<< ... float ~ raised ValueError(...float...invalid...) .. versionchanged:: 0.5.0 Renamed `arguments` to `call_args`. Renamed `arguments_repr` to `call_args_repr`. Added `call` option.

Response:
def log(func=None,
        stacktrace=10,
        stacktrace_align=60,
        attributes=(),
        module=True,
        call=True,
        call_args=True,
        call_args_repr=repr,
        result=True,
        exception=True,
        exception_repr=repr,
        result_repr=strip_non_ascii,
        use_logging='CRITICAL',
        print_to=None):
    """
    Decorates `func` to have logging.

    Args
        func (function):
            Function to decorate. If missing log returns a partial which you can use as a decorator.
        stacktrace (int):
            Number of frames to show.
        stacktrace_align (int):
            Column to align the framelist to.
        attributes (list):
            List of instance attributes to show, in case the function is a instance method.
        module (bool):
            Show the module.
        call (bool):
            If ``True``, then show calls. If ``False`` only show the call details on exceptions (if ``exception`` is
            enabled) (default: ``True``)
        call_args (bool):
            If ``True``, then show call arguments. (default: ``True``)
        call_args_repr (bool):
            Function to convert one argument to a string. (default: ``repr``)
        result (bool):
            If ``True``, then show result. (default: ``True``)
        exception (bool):
            If ``True``, then show exceptions. (default: ``True``)
        exception_repr (function):
            Function to convert an exception to a string. (default: ``repr``)
        result_repr (function):
            Function to convert the result object to a string. (default: ``strip_non_ascii`` - like ``str`` but nonascii
            characters are replaced with dots.)
        use_logging (string):
            Emit log messages with the given loglevel. (default: ``"CRITICAL"``)
        print_to (fileobject):
            File object to write to, in case you don't want to use logging module. (default: ``None`` - printing is
            disabled)

    Returns:
        A decorator or a wrapper.

    Example::

        >>> @log(print_to=sys.stdout)
        ... def a(weird=False):
        ...     if weird:
        ...         raise RuntimeError('BOOM!')
        >>> a()
        a()                                                           <<< ...
        a => None
        >>> try:
        ...     a(weird=True)
        ... except Exception:
        ...     pass # naughty code!
        a(weird=True)                                                 <<< ...
        a ~ raised RuntimeError('BOOM!',)

    You can conveniently use this to logs just errors, or just results, example::

        >>> import aspectlib
        >>> with aspectlib.weave(float, log(call=False, result=False, print_to=sys.stdout)):
        ...     try:
        ...         float('invalid')
        ...     except Exception as e:
        ...         pass # naughty code!
        float('invalid')                                              <<< ...
        float ~ raised ValueError(...float...invalid...)

    This makes debugging naughty code easier.

    PS: Without the weaving it looks like this::

        >>> try:
        ...     log(call=False, result=False, print_to=sys.stdout)(float)('invalid')
        ... except Exception:
        ...     pass # naughty code!
        float('invalid')                                              <<< ...
        float ~ raised ValueError(...float...invalid...)

    .. versionchanged:: 0.5.0

        Renamed `arguments` to `call_args`.
        Renamed `arguments_repr` to `call_args_repr`.
        Added `call` option.
    """

    loglevel = use_logging and (
        logging._levelNames if hasattr(logging, '_levelNames') else logging._nameToLevel
    ).get(use_logging, logging.CRITICAL)
    _missing = object()

    def dump(buf):
        try:
            if use_logging:
                logger._log(loglevel, buf, ())
            if print_to:
                buf += '\n'
                print_to.write(buf)
        except Exception as exc:
            logger.critical('Failed to log a message: %s', exc, exc_info=True)

    class __logged__(Aspect):
        __slots__ = 'cutpoint_function', 'final_function', 'binding', '__name__', '__weakref__'

        bind = False

        def __init__(self, cutpoint_function, binding=None):
            mimic(self, cutpoint_function)
            self.cutpoint_function = cutpoint_function
            self.final_function = super(__logged__, self).__call__(cutpoint_function)
            self.binding = binding

        def __get__(self, instance, owner):
            return __logged__(self.cutpoint_function.__get__(instance, owner), instance)

        def __call__(self, *args, **kwargs):
            return self.final_function(*args, **kwargs)

        def advising_function(self, *args, **kwargs):
            name = self.cutpoint_function.__name__
            instance = self.binding
            if instance is not None:
                if isinstance(instance, InstanceType):
                    instance_type = instance.__class__
                else:
                    instance_type = type(instance)

                info = []
                for key in attributes:
                    if key.endswith('()'):
                        callarg = key = key.rstrip('()')
                    else:
                        callarg = False
                    val = getattr(instance, key, _missing)
                    if val is not _missing and key != name:
                        info.append(' %s=%s' % (
                            key, call_args_repr(val() if callarg else val)
                        ))
                sig = buf = '{%s%s%s}.%s' % (
                    instance_type.__module__ + '.' if module else '',
                    instance_type.__name__,
                    ''.join(info),
                    name
                )
            else:
                sig = buf = name
            if call_args:
                buf += '(%s%s)' % (
                    ', '.join(repr(i) for i in (args if call_args is True else args[:call_args])),
                    ((', ' if args else '') + ', '.join('%s=%r' % i for i in kwargs.items()))
                    if kwargs and call_args is True
                    else '',
                )
            if stacktrace:
                buf = ("%%-%ds  <<< %%s" % stacktrace_align) % (buf, format_stack(skip=1, length=stacktrace))
            if call:
                dump(buf)
            try:
                res = yield
            except Exception as exc:
                if exception:
                    if not call:
                        dump(buf)
                    dump('%s ~ raised %s' % (sig, exception_repr(exc)))
                raise

            if result:
                dump('%s => %s' % (sig, result_repr(res)))

    if func:
        return __logged__(func)
    else:
        return __logged__


Instruction:
Write a Python 3 script to
wire handlers to the active device.

Response:
def wireHandlers(cfg):
    """
    If the device is configured to run against a remote server, ping that device on a scheduled basis with our current
    state.
    :param cfg: the config object.
    :return:
    """
    logger = logging.getLogger('recorder')
    httpPoster = cfg.handlers.get('remote')
    csvLogger = cfg.handlers.get('local')
    activeHandler = None
    if httpPoster is None:
        if csvLogger is None:
            logger.warning("App is running with discard handler only, ALL DATA WILL BE DISCARDED!!!")
        else:
            logger.info("App is running in standalone mode, logging data to local filesystem")
            activeHandler = csvLogger
    else:
        logger.info("App is running against remote server, logging data to " + httpPoster.target)
        activeHandler = httpPoster
        heartbeater.serverURL = httpPoster.target
        heartbeater.ping()

    if activeHandler is not None:
        for device in cfg.recordingDevices.values():
            if activeHandler is httpPoster:
                httpPoster.deviceName = device.name
            copied = copy.copy(activeHandler)
            device.dataHandler = copied if not cfg.useAsyncHandlers else AsyncHandler('recorder', copied)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get(self, deviceId):
        measurementsByName = self.measurements.get(deviceId)
        if measurementsByName is None:
            return []
        else:
            return list(measurementsByName.values())

Response: Returns a list of all known active measurements for a device.


Instruction:
Can you write a function in Python 3 where it
gets the specific measurement details

Response:
def get(self, deviceId, measurementId):
        """
        details the specific measurement.
        """
        record = self.measurements.get(deviceId)
        if record is not None:
            return record.get(measurementId)
        return None


Instruction:
Write a Python 3 script for
scheduling a new measurement at the specified time.

Response:
def put(self, deviceId, measurementId):
        """
        Schedules a new measurement at the specified time.
        :param deviceId: the device to measure.
        :param measurementId: the name of the measurement.
        :return: 200 if it was scheduled, 400 if the device is busy, 500 if the device is bad.
        """
        record = self.measurements.get(deviceId)
        if record is not None:
            measurement = record.get(measurementId)
            if measurement is not None:
                if len([x.name for x in measurement.statuses if x.name is 'COMPLETE' or x.name is 'FAILED']) > 0:
                    logger.info('Overwriting existing completed measurement ' + x.name)
                    measurement = None
            if measurement is None:
                logger.info('Initiating measurement ' + measurementId)
                measurement = ScheduledMeasurement(measurementId, self.recordingDevices.get(deviceId))
                body = request.get_json()
                duration_ = body['duration']
                def _cleanup():
                    logger.info('Removing ' + measurementId + ' from ' + deviceId)
                    record.pop(measurementId)
                measurement.schedule(duration_, at=body.get('at'), delay=body.get('delay'), callback=_cleanup)
                # a quick hack to enable the measurement to be cleaned up by the ScheduledMeasurement
                record[measurementId] = measurement
                return measurement, 200
            else:
                return measurement, 400
        else:
            return 'unknown device ' + deviceId, 400


Instruction:
Can you implement a function in Python 3 that
deletes a stored measurement.

Response:
def delete(self, deviceId, measurementId):
        """
        Deletes a stored measurement.
        :param deviceId: the device to measure.
        :param measurementId: the name of the measurement.
        :return: 200 if it was deleted, 400 if no such measurement (or device).
        """
        record = self.measurements.get(deviceId)
        if record is not None:
            popped = record.pop(measurementId, None)
            return popped, 200 if popped else 400
        return None, 400


Instruction:
Can you write a function in Python 3 where it
signals a stop for the given measurement.

Response:
def get(self, deviceId, measurementId):
        """
        signals a stop for the given measurement.
        :param deviceId: the device to measure.
        :param measurementId: the name of the measurement.
        :return: 200 if stop is signalled, 400 if it doesn't exist or is not running.
        """
        record = self.measurements.get(deviceId)
        if record is not None:
            measurement = record.get(measurementId)
            if measurement.recording:
                device = self.recordingDevices.get(deviceId)
                device.signalStop()
                return measurement, 200
            else:
                return measurement, 400
        return '', 400


Instruction:
Write a Python 3 script to
schedule the measurement asynchronously.

Response:
def schedule(self, duration, at=None, delay=None, callback=None):
        """
        schedules the measurement (to execute asynchronously).
        :param duration: how long to run for.
        :param at: the time to start at.
        :param delay: the time to wait til starting (use at or delay).
        :param callback: a callback.
        :return: nothing.
        """
        delay = self.calculateDelay(at, delay)
        self.callback = callback
        logger.info('Initiating measurement ' + self.name + ' for ' + str(duration) + 's in ' + str(delay) + 's')
        self.statuses.append({'name': ScheduledMeasurementStatus.SCHEDULED.name, 'time': datetime.utcnow()})
        threading.Timer(delay, self.execute, [duration]).start()


Instruction:
Can you write a function in Python 3 where it
executes the measurement recording the event status.

Response:
def execute(self, duration):
        """
        Executes the measurement, recording the event status.
        :param duration: the time to run for.
        :return: nothing.
        """
        self.statuses.append({'name': ScheduledMeasurementStatus.RUNNING.name, 'time': datetime.utcnow()})
        try:
            self.recording = True
            self.device.start(self.name, durationInSeconds=duration)
        finally:
            self.recording = False
        if self.device.status == RecordingDeviceStatus.FAILED:
            self.statuses.append({'name': ScheduledMeasurementStatus.FAILED.name,
                                  'time': datetime.utcnow(),
                                  'reason': self.device.failureCode})
        else:
            self.statuses.append({'name': ScheduledMeasurementStatus.COMPLETE.name, 'time': datetime.utcnow()})
        # this is a bit of a hack, need to remove this at some point by refactoring the way measurements are stored
        if self.callback is not None:
            self.callback()


Instruction:
Can you generate the documentation for the following Python 3 function
def calculateDelay(self, at, delay):
        if at is not None:
            return max((datetime.strptime(at, DATETIME_FORMAT) - datetime.utcnow()).total_seconds(), 0)
        elif delay is not None:
            return delay
        else:
            return 0

Response: Calculates the delay from now til the specified start time uses at if available.


Instruction:
Implement a Python 3 function for
giving a list of multiprocessing.Process objects which have not yet been started, this function launches them and blocks until the last finishes. This makes sure that only <pool_size> processes are ever working at any one time (this number does not include the main process which called this function, since that will not tax the CPU). The idea here is roughly analogous to multiprocessing.Pool with the exceptions that: 1 - The caller will get to use the multiprocessing.Process model of using shared memory (inheritance) to pass arg data to the child, 2 - maxtasksperchild is always 1, 3 - no function return value is kept/tranferred (not yet implemented)

Response:
def launch_and_wait(mp_proc_list, pool_size):
    """ Given a list of multiprocessing.Process objects which have not yet
    been started, this function launches them and blocks until the last
    finishes.  This makes sure that only <pool_size> processes are ever
    working at any one time (this number does not include the main process
    which called this function, since that will not tax the CPU).
    The idea here is roughly analogous to multiprocessing.Pool
    with the exceptions that:
        1 - The caller will get to use the multiprocessing.Process model of
            using shared memory (inheritance) to pass arg data to the child,
        2 - maxtasksperchild is always 1,
        3 - no function return value is kept/tranferred (not yet implemented)
    """

    # Sanity check
    if len(mp_proc_list) < 1:
        return

    # Create or own list with easy state watching
    procs = []
    for p in mp_proc_list:
        procs.append(WatchedProcess(p))

    # Launch all of them, but only so pool_size are running at any time
    keep_going = True
    while (keep_going):
        # Before we start any more, find out how many are running.  First go
        # through the list of those started and see if alive.  Update state.
        for p in procs:
            if p.state == 1: # been started
                if not p.process.is_alive():
                    p.state = 2 # process has finished or been terminated
                    assert p.process.exitcode is not None, \
                           "Process is not alive but has no exitcode? "+ \
                           str(p.process)

        # now figure num_running
        num_running = len([p for p in procs if p.state == 1])

        # Start some.  Only as many as pool_size should ever be running.
        num_avail_cpus = pool_size - num_running
        num_to_start = len([p for p in procs if p.state == 0])
        if num_to_start < 1:
            # all have been started, can finally leave loop and go wait
            break
        if num_avail_cpus > 0 and num_to_start > 0:
            num_to_start_now = min(num_avail_cpus, num_to_start)
            started_now = 0
            for p in procs:
                if started_now < num_to_start_now and p.state == 0:
                    p.start_process()
                    # debug "launch_and_wait: started: "+str(p.process)
                    started_now += 1
        # else: otherwise, all cpus are in use, just wait ...

        # sleep to tame loop activity, but also must sleep a bit after each
        # start call so that the call to is_alive() woorks correctly
        time.sleep(1)

    # Out of the launching loop, can now wait on all procs left.
    for p in procs:
        p.join_process()

    # Check all exit codes before returning
    for p in procs:
        if 0 != p.process.exitcode:
            raise RuntimeError("Problem during: "+str(p.process.name)+ \
                  ', exitcode: '+str(p.process.exitcode)+'. Check log.')


Instruction:
Can you create a Python 3 function that
determines and return the best layout of tiles for a given pool_size.

Response:
def best_tile_layout(pool_size):
    """ Determine and return the best layout of "tiles" for fastest
    overall parallel processing of a rectangular image broken up into N
    smaller equally-sized rectangular tiles, given as input the number
    of processes/chunks which can be run/worked at the same time (pool_size).

    This attempts to return a layout whose total number of tiles is as
    close as possible to pool_size, without going over (and thus not
    really taking advantage of pooling).  Since we can vary the
    size of the rectangles, there is not much (any?) benefit to pooling.

    Returns a tuple of ( <num tiles in X dir>, <num in Y direction> )

    This assumes the image in question is relatively close to square, and
    so the returned tuple attempts to give a layout which is as
    squarishly-blocked as possible, except in cases where speed would be
    sacrificed.

    EXAMPLES:

    For pool_size of 4, the best result is 2x2.

    For pool_size of 6, the best result is 2x3.

    For pool_size of 5, a result of 1x5 is better than a result of
    2x2 (which would leave one core unused), and 1x5 is also better than
    a result of 2x3 (which would require one core to work twice while all
    others wait).

    For higher, odd pool_size values (say 39), it is deemed best to
    sacrifice a few unused cores to satisfy our other constraints, and thus
    the result of 6x6 is best (giving 36 tiles and 3 unused cores).
    """
    # Easy answer sanity-checks
    if pool_size < 2:
        return (1, 1)

    # Next, use a small mapping of hard-coded results.  While we agree
    # that many of these are unlikely pool_size values, they are easy
    # to accomodate.
    mapping = { 0:(1,1), 1:(1,1), 2:(1,2), 3:(1,3), 4:(2,2), 5:(1,5),
                6:(2,3), 7:(2,3), 8:(2,4), 9:(3,3), 10:(2,5), 11:(2,5),
                14:(2,7), 18:(3,6), 19:(3,6), 28:(4,7), 29:(4,7),
                32:(4,8), 33:(4,8), 34:(4,8), 40:(4,10), 41:(4,10) }
    if pool_size in mapping:
        return mapping[pool_size]

    # Next, take a guess using the square root and (for the sake of
    # simplicity), go with it.  We *could* get much fancier here...
    # Use floor-rounding (not ceil) so that the total number of resulting
    # tiles is <= pool_size.
    xnum = int(math.sqrt(pool_size))
    ynum = int((1.*pool_size)/xnum)
    return (xnum, ynum)


Instruction:
Given the following Python 3 function, write the documentation
def _accept(self):
        logger.warning("Reactor " + self._name + " is starting")
        while self.running:
            try:
                self._completeTask()
            except:
                logger.exception("Unexpected exception during request processing")
        logger.warning("Reactor " + self._name + " is terminating")

Response: This method is called by the daemon when the thread is listening and the thread is shutting down.


Instruction:
Write a Python 3 script for
offering a new request to the reactor.

Response:
def offer(self, requestType, *args):
        """
        public interface to the reactor.
        :param requestType:
        :param args:
        :return:
        """
        if self._funcsByRequest.get(requestType) is not None:
            self._workQueue.put((requestType, list(*args)))
        else:
            logger.error("Ignoring unknown request on reactor " + self._name + " " + requestType)


Instruction:
Given the following Python 3 function, write the documentation
def clicked(self):
        try:
            from . import teal
        except:
            teal = None
        try:
            # start drilling down into the tpo to get the code
            tealGui = self._mainGuiObj
            tealGui.showStatus('Clicked "'+self.getButtonLabel()+'"', keep=1)
            pscope = self.paramInfo.scope
            pname = self.paramInfo.name
            tpo = tealGui._taskParsObj
            tup = tpo.getExecuteStrings(pscope, pname)
            code = ''
            if not tup:
                if teal:
                    teal.popUpErr(tealGui.top, "No action to perform",
                                  "Action Button Error")
                return
            for exname in tup:
                if '_RULES_' in tpo and exname in tpo['_RULES_'].configspec:
                    ruleSig = tpo['_RULES_'].configspec[exname]
                    chkArgsDict = vtor_checks.sigStrToKwArgsDict(ruleSig)
                    code = chkArgsDict.get('code') # a string or None
                    # now go ahead and execute it
                    teal.execEmbCode(pscope, pname, self.getButtonLabel(),
                                     tealGui, code)
            # done
            tealGui.debug('Finished: "'+self.getButtonLabel()+'"')
        except Exception as ex:
            msg = 'Error executing: "'+self.getButtonLabel()+'"\n'+ex.message
            msgFull = msg+'\n'+''.join(traceback.format_exc())
            msgFull+= "CODE:\n"+code
            if tealGui:
                if teal: teal.popUpErr(tealGui.top, msg, "Action Button Error")
                tealGui.debug(msgFull)
            else:
                if teal: teal.popUpErr(None, msg, "Action Button Error")
                print(msgFull)

Response: Called when the button is clicked. Execute code from. cfgspc


Instruction:
Create a Python 3 function for
converting numpy array to string.

Response:
def ndarr2str(arr, encoding='ascii'):
    """ This is used to ensure that the return value of arr.tostring()
    is actually a string.  This will prevent lots of if-checks in calling
    code.  As of numpy v1.6.1 (in Python 3.2.3), the tostring() function
    still returns type 'bytes', not 'str' as it advertises. """
    # be fast, don't check - just assume 'arr' is a numpy array - the tostring
    # call will fail anyway if not
    retval = arr.tostring()
    # would rather check "if isinstance(retval, bytes)", but support 2.5.
    # could rm the if PY3K check, but it makes this faster on 2.x.
    if PY3K and not isinstance(retval, str):
        return retval.decode(encoding)
    else: # is str
        return retval


Instruction:
Write a Python 3 function for
converting numpy array to bytes.

Response:
def ndarr2bytes(arr, encoding='ascii'):
    """ This is used to ensure that the return value of arr.tostring()
    is actually a *bytes* array in PY3K.  See notes in ndarr2str above.  Even
    though we consider it a bug that numpy's tostring() function returns
    a bytes array in PY3K, there are actually many instances where that is what
    we want - bytes, not unicode.  So we use this function in those
    instances to ensure that when/if this numpy "bug" is "fixed", that
    our calling code still gets bytes where it needs/expects them. """
    # be fast, don't check - just assume 'arr' is a numpy array - the tostring
    # call will fail anyway if not
    retval = arr.tostring()
    # would rather check "if not isinstance(retval, bytes)", but support 2.5.
    if PY3K and isinstance(retval, str):
        # Take note if this ever gets used.  If this ever occurs, it
        # is likely wildly inefficient since numpy.tostring() is now
        # returning unicode and numpy surely has a tobytes() func by now.
        # If so, add a code path to call its tobytes() func at our start.
        return retval.encode(encoding)
    else: # is str==bytes in 2.x
        return retval


Instruction:
Can you write a function in Python 3 where it
converts string s to bytes type in all Pythons before 3. 0 and 2. 6 on Pythons before 3. 0.

Response:
def tobytes(s, encoding='ascii'):
    """ Convert string s to the 'bytes' type, in all Pythons, even
    back before Python 2.6.  What 'str' means varies by PY3K or not.
    In Pythons before 3.0, this is technically the same as the str type
    in terms of the character data in memory. """
    # NOTE: after we abandon 2.5, we might simply instead use "bytes(s)"
    # NOTE: after we abandon all 2.*, del this and prepend byte strings with 'b'
    if PY3K:
        if isinstance(s, bytes):
            return s
        else:
            return s.encode(encoding)
    else:
        # for py2.6 on (before 3.0), bytes is same as str;  2.5 has no bytes
        # but handle if unicode is passed
        if isinstance(s, unicode):
            return s.encode(encoding)
        else:
            return s


Instruction:
Implement a Python 3 function for
converting string - like - thing s to the str type in all Pythons before 3. 0 bytes.

Response:
def tostr(s, encoding='ascii'):
    """ Convert string-like-thing s to the 'str' type, in all Pythons, even
    back before Python 2.6.  What 'str' means varies by PY3K or not.
    In Pythons before 3.0, str and bytes are the same type.
    In Python 3+, this may require a decoding step. """
    if PY3K:
        if isinstance(s, str): # str == unicode in PY3K
            return s
        else: # s is type bytes
            return s.decode(encoding)
    else:
        # for py2.6 on (before 3.0), bytes is same as str;  2.5 has no bytes
        # but handle if unicode is passed
        if isinstance(s, unicode):
            return s.encode(encoding)
        else:
            return s


Instruction:
Given the following Python 3 function, write the documentation
def retry(func=None, retries=5, backoff=None, exceptions=(IOError, OSError, EOFError), cleanup=None, sleep=time.sleep):

    @Aspect(bind=True)
    def retry_aspect(cutpoint, *args, **kwargs):
        for count in range(retries + 1):
            try:
                if count and cleanup:
                    cleanup(*args, **kwargs)
                yield
                break
            except exceptions as exc:
                if count == retries:
                    raise
                if not backoff:
                    timeout = 0
                elif isinstance(backoff, (int, float)):
                    timeout = backoff
                else:
                    timeout = backoff(count)
                logger.exception("%s(%s, %s) raised exception %s. %s retries left. Sleeping %s secs.",
                                 cutpoint.__name__, args, kwargs, exc, retries - count, timeout)
                sleep(timeout)

    return retry_aspect if func is None else retry_aspect(func)

Response: Decorator that retries the given function if it raises exceptions.


Instruction:
Can you generate the documentation for the following Python 3 function
def loadSignal(self, name, start=None, end=None):
        entry = self._getCacheEntry(name)
        if entry is not None:
            from analyser.common.signal import loadSignalFromWav
            return loadSignalFromWav(entry['path'], start=start, end=end)
        else:
            return None

Response: Loads the named entry from the upload cache as a signal.


Instruction:
Can you create a Python 3 function that
returns the entry with the given name or None if no entry exists.

Response:
def _getCacheEntry(self, name):
        """
        :param name: the name of the cache entry.
        :return: the entry or none.
        """
        return next((x for x in self._uploadCache if x['name'] == name), None)


Instruction:
Create a Python 3 function to
convert a temporary file to the upload dir resampling it if necessary and then deleting the tmp entries.

Response:
def _convertTmp(self, tmpCacheEntry):
        """
        Moves a tmp file to the upload dir, resampling it if necessary, and then deleting the tmp entries.
        :param tmpCacheEntry: the cache entry.
        :return:
        """
        from analyser.common.signal import loadSignalFromWav
        tmpCacheEntry['status'] = 'converting'
        logger.info("Loading " + tmpCacheEntry['path'])
        signal = loadSignalFromWav(tmpCacheEntry['path'])
        logger.info("Loaded " + tmpCacheEntry['path'])
        if Path(tmpCacheEntry['path']).exists():
            logger.info('Deleting ' + tmpCacheEntry['path'])
            os.remove(tmpCacheEntry['path'])
        else:
            logger.warning('Tmp cache file does not exist: ' + tmpCacheEntry['path'])
        self._tmpCache.remove(tmpCacheEntry)
        self._conversionCache.append(tmpCacheEntry)
        srcFs = signal.fs
        completeSamples = signal.samples
        outputFileName = os.path.join(self._uploadDir, tmpCacheEntry['name'])
        if srcFs > 1024:
            self.writeOutput(outputFileName, completeSamples, srcFs, 1000)
        else:
            self.writeOutput(outputFileName, completeSamples, srcFs, srcFs)
        tmpCacheEntry['status'] = 'loaded'
        self._conversionCache.remove(tmpCacheEntry)
        self._uploadCache.append(self._extractMeta(outputFileName, 'loaded'))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def writeChunk(self, stream, filename, chunkIdx=None):
        import io
        more = True
        outputFileName = filename if chunkIdx is None else filename + '.' + str(chunkIdx)
        outputDir = self._uploadDir if chunkIdx is None else self._tmpDir
        chunkFilePath = os.path.join(outputDir, outputFileName)
        if os.path.exists(chunkFilePath) and os.path.isfile(chunkFilePath):
            logger.error('Uploaded file already exists: ' + chunkFilePath)
            return -1
        else:
            chunkFile = open(chunkFilePath, 'xb')
        count = 0
        while more:
            chunk = stream.read(io.DEFAULT_BUFFER_SIZE)
            chunkLen = len(chunk)
            count += chunkLen
            if chunkLen == 0:
                more = False
            else:
                chunkFile.write(chunk)
        return count

Response: Streams an uploaded chunk to a file.


Instruction:
Implement a function in Python 3 to
finalise the upload which means converting to a single 1kHz sample rate file output file.

Response:
def finalise(self, filename, totalChunks, status):
        """
        Completes the upload which means converting to a single 1kHz sample rate file output file.
        :param filename:
        :param totalChunks:
        :param status:
        :return:
        """

        def getChunkIdx(x):
            try:
                return int(x.suffix[1:])
            except ValueError:
                return -1

        def isChunkFile(x):
            return x.is_file() and -1 < getChunkIdx(x) <= totalChunks

        asSingleFile = os.path.join(self._tmpDir, filename)
        if status.lower() == 'true':
            chunks = [(getChunkIdx(file), str(file)) for file in
                      Path(self._tmpDir).glob(filename + '.*') if isChunkFile(file)]
            # TODO if len(chunks) != totalChunks then error
            with open(asSingleFile, 'xb') as wfd:
                for f in [x[1] for x in sorted(chunks, key=lambda tup: tup[0])]:
                    with open(f, 'rb') as fd:
                        logger.info("cat " + f + " with " + asSingleFile)
                        shutil.copyfileobj(fd, wfd, 1024 * 1024 * 10)
        self.cleanupChunks(filename, isChunkFile, status)


Instruction:
Here you have a function in Python 3, explain what it does
def writeOutput(self, filename, samples, srcFs, targetFs):
        import librosa
        inputLength = samples.shape[-1]
        if srcFs != targetFs:
            if inputLength < targetFs:
                logger.info("Input signal is too short (" + str(inputLength) +
                            " samples) for resampling to " + str(targetFs) + "Hz")
                outputSamples = samples
                targetFs = srcFs
            else:
                logger.info("Resampling " + str(inputLength) + " samples from " + str(srcFs) + "Hz to " +
                            str(targetFs) + "Hz")
                outputSamples = librosa.resample(samples, srcFs, targetFs, res_type='kaiser_fast')
        else:
            outputSamples = samples
        logger.info("Writing output to " + filename)
        maxv = np.iinfo(np.int32).max
        librosa.output.write_wav(filename, (outputSamples * maxv).astype(np.int32), targetFs)
        logger.info("Output written to " + filename)

Response: Resamples the signal to the targetFs and writes it to the filename.


Instruction:
Can you tell what is the following Python 3 function doing
def delete(self, name):
        i, entry = next(((i, x) for i, x in enumerate(self._uploadCache) if x['name'] == name), (None, None))
        if entry is not None:
            logger.info("Deleting " + name)
            os.remove(str(entry['path']))
            del self._uploadCache[i]
            return entry
        else:
            logger.info("Unable to delete " + name + ", not found")
            return None

Response: Deletes the named entry.


Instruction:
Can you create a Python 3 function that
returns an EparOption item of appropriate type for the parameter param

Response:
def eparOptionFactory(master, statusBar, param, defaultParam,
                      doScroll, fieldWidths,
                      plugIn=None, editedCallbackObj=None,
                      helpCallbackObj=None, mainGuiObj=None,
                      defaultsVerb="Default", bg=None, indent=False,
                      flagging=False, flaggedColor=None):

    """Return EparOption item of appropriate type for the parameter param"""

    # Allow passed-in overrides
    if plugIn is not None:
        eparOption = plugIn

    # If there is an enumerated list, regardless of datatype use EnumEparOption
    elif param.choice is not None:
        eparOption = EnumEparOption

    else:
        # Use String for types not in the dictionary
        eparOption = _eparOptionDict.get(param.type, StringEparOption)

    # Create it
    eo = eparOption(master, statusBar, param, defaultParam, doScroll,
                    fieldWidths, defaultsVerb, bg,
                    indent=indent, helpCallbackObj=helpCallbackObj,
                    mainGuiObj=mainGuiObj)
    eo.setEditedCallbackObj(editedCallbackObj)
    eo.setIsFlagging(flagging, False)
    if flaggedColor:
        eo.setFlaggedColor(flaggedColor)
    return eo


Instruction:
Given the following Python 3 function, write the documentation
def extraBindingsForSelectableText(self):
        # See notes in watchTextSelection
        self.entry.bind('<FocusIn>', self.watchTextSelection, "+")
        self.entry.bind('<ButtonRelease-1>', self.watchTextSelection, "+")
        self.entry.bind('<B1-Motion>', self.watchTextSelection, "+")
        self.entry.bind('<Shift_L>', self.watchTextSelection, "+")
        self.entry.bind('<Left>', self.watchTextSelection, "+")
        self.entry.bind('<Right>', self.watchTextSelection, "+")

Response: Collect in 1 place the bindings needed for the watchTextSelection method


Instruction:
Can you tell what is the following Python 3 function doing
def focusOut(self, event=None):
        # do nothing if this isn't a text-enabled widget
        if not self.isSelectable:
            return
        if self.entryCheck(event) is None:
            # Entry value is OK
            # Save the last selection so it can be restored if we
            # come right back to this widget.  Then clear the selection
            # before moving on.
            entry = self.entry
            try:
                if not entry.selection_present():
                    self.lastSelection = None
                else:
                    self.lastSelection = (entry.index(SEL_FIRST),
                                          entry.index(SEL_LAST))
            except AttributeError:
                pass
            if USING_X and sys.platform == 'darwin':
                pass # do nothing here - we need it left selected for cut/paste
            else:
                entry.selection_clear()
        else:
            return "break"

Response: Clear selection ( if text is selected in this widget


Instruction:
Write a Python 3 function for
selecting all text on taking focus

Response:
def focusIn(self, event=None):
        """Select all text (if applicable) on taking focus"""
        try:
            # doScroll returns false if the call was ignored because the
            # last call also came from this widget.  That avoids unwanted
            # scrolls and text selection when the focus moves in and out
            # of the window.
            if self.doScroll(event):
                self.entry.selection_range(0, END) # select all text in widget
            else:
                # restore selection to what it was on the last FocusOut
                if self.lastSelection:
                    self.entry.selection_range(*self.lastSelection)
        except AttributeError:
            pass


Instruction:
Can you generate the documentation for the following Python 3 function
def widgetEdited(self, event=None, val=None, action='entry', skipDups=True):

        # be as lightweight as possible if obj doesn't care about this stuff
        if not self._editedCallbackObj and not self._flagNonDefaultVals:
            return

        # get the current value
        curVal = val # take this first, if it is given
        if curVal is None:
            curVal = self.choice.get()

        # do any flagging
        self.flagThisPar(curVal, False)

        # see if this is a duplicate successive call for the same value
        if skipDups and curVal==self._lastWidgetEditedVal: return

        # pull trigger
        if not self._editedCallbackObj: return
        self._editedCallbackObj.edited(self.paramInfo.scope,
                                       self.paramInfo.name,
                                       self.previousValue, curVal,
                                       action)
        # for our duplicate checker
        self._lastWidgetEditedVal = curVal

Response: This method is called by the GUI when a value has been set. It is called by the GUI when the user has clicked on the entry.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def fileBrowser(self):
        if capable.OF_TKFD_IN_EPAR:
           fname = askopenfilename(parent=self.entry, title="Select File")
        else:
            from . import filedlg
            self.fd = filedlg.PersistLoadFileDialog(self.entry,
                              "Select File", "*")
            if self.fd.Show() != 1:
                self.fd.DialogCleanup()
                return
            fname = self.fd.GetFileName()
            self.fd.DialogCleanup()
        if not fname: return # canceled

        self.choice.set(fname)
        # don't select when we go back to widget to reduce risk of
        # accidentally typing over the filename
        self.lastSelection = None

Response: Invoke a tkinter file dialog


Instruction:
Here you have a function in Python 3, explain what it does
def dirBrowser(self):
        if capable.OF_TKFD_IN_EPAR:
            fname = askdirectory(parent=self.entry, title="Select Directory")
        else:
            raise NotImplementedError('Fix popupChoices() logic.')

        if not fname:
            return # canceled

        self.choice.set(fname)
        # don't select when we go back to widget to reduce risk of
        # accidentally typing over the filename
        self.lastSelection = None

Response: Invoke a tkinter directory dialog


Instruction:
Can you generate a brief explanation for the following Python 3 code
def forceValue(self, newVal, noteEdited=False):
        if newVal is None:
            newVal = ""
        self.choice.set(newVal)
        if noteEdited:
            self.widgetEdited(val=newVal, skipDups=False)

Response: Force - set a parameter entry to the given value


Instruction:
How would you explain what the following Python 3 function does
def unlearnValue(self):
        defaultValue = self.defaultParamInfo.get(field = "p_filename",
                            native = 0, prompt = 0)
        self.choice.set(defaultValue)

Response: Unlearn a parameter value by setting it back to its default value


Instruction:
How would you explain what the following Python 3 function does
def setActiveState(self, active):
        st = DISABLED
        if active: st = NORMAL
        self.entry.configure(state=st)
        self.inputLabel.configure(state=st)
        self.promptLabel.configure(state=st)

Response: Configure the active state of the entry input label prompt label and entry.


Instruction:
How would you explain what the following Python 3 function does
def flagThisPar(self, currentVal, force):

        # Get out ASAP if we can
        if (not force) and (not self._flagNonDefaultVals): return

        # handle simple case before comparing values (quick return)
        if force and not self._flagNonDefaultVals:
            self._flagged = False
            self.promptLabel.configure(fg="black")
            return

        # Get/format values to compare
        currentNative = self.convertToNative(currentVal)
        defaultNative = self.convertToNative(self.defaultParamInfo.value)
        # par.value is same as par.get(native=1,prompt=0)

        # flag or unflag as needed
        if currentNative != defaultNative:
            if not self._flagged or force:
                self._flagged = True
                self.promptLabel.configure(fg=self._flaggedColor) # was red
        else: # same as def
            if self._flagged or force:
                self._flagged = False
                self.promptLabel.configure(fg="black")

Response: Flag this parameter as needed.


Instruction:
Write a Python 3 script to
allow keys typed in widget to select items

Response:
def keypress(self, event):
        """Allow keys typed in widget to select items"""
        try:
            self.choice.set(self.shortcuts[event.keysym])
        except KeyError:
            # key not found (probably a bug, since we intend to catch
            # only events from shortcut keys, but ignore it anyway)
            pass


Instruction:
Given the following Python 3 function, write the documentation
def postcmd(self):
        value = self.choice.get()
        try:
            index = self.paramInfo.choice.index(value)
            self.entry.menu.activate(index)
        except ValueError:
            # initial null value may not be in list
            pass

Response: Make sure proper entry is activated when menu is posted


Instruction:
Given the following Python 3 function, write the documentation
def convertToNative(self, aVal):
        if aVal is None:
            return None
        if isinstance(aVal, bool): return aVal
        # otherwise interpret strings
        return str(aVal).lower() in ('1','on','yes','true')

Response: Convert to native bool ; interpret certain strings.


Instruction:
Create a Python 3 function to
toggle value between Yes and No

Response:
def toggle(self, event=None):
        """Toggle value between Yes and No"""
        if self.choice.get() == "yes":
            self.rbno.select()
        else:
            self.rbyes.select()
        self.widgetEdited()


Instruction:
Write a Python 3 script for
checking if an entry is uppercase and set the choice to the current value.

Response:
def entryCheck(self, event = None, repair = True):
        """ Ensure any INDEF entry is uppercase, before base class behavior """
        valupr = self.choice.get().upper()
        if valupr.strip() == 'INDEF':
            self.choice.set(valupr)
        return EparOption.entryCheck(self, event, repair = repair)


Instruction:
Can you write a function in Python 3 where it
updates the current record of the packet size per sample and the max bytes per fifo read.

Response:
def _setSampleSizeBytes(self):
        """
        updates the current record of the packet size per sample and the relationship between this and the fifo reads.
        """
        self.sampleSizeBytes = self.getPacketSize()
        if self.sampleSizeBytes > 0:
            self.maxBytesPerFifoRead = (32 // self.sampleSizeBytes)


Instruction:
Implement a Python 3 function for
getting the current packet size based on the enabled registers

Response:
def getPacketSize(self):
        """
        the current packet size.
        :return: the current packet size based on the enabled registers.
        """
        size = 0
        if self.isAccelerometerEnabled():
            size += 6
        if self.isGyroEnabled():
            size += 6
        if self.isTemperatureEnabled():
            size += 2
        return size


Instruction:
Write a Python 3 function for
initialising the device with the specified parameters

Response:
def initialiseDevice(self):
        """
        performs initialisation of the device
        :param batchSize: the no of samples that each provideData call should yield
        :return:
        """
        logger.debug("Initialising device")
        self.getInterruptStatus()
        self.setAccelerometerSensitivity(self._accelerationFactor * 32768.0)
        self.setGyroSensitivity(self._gyroFactor * 32768.0)
        self.setSampleRate(self.fs)
        for loop in self.ZeroRegister:
            self.i2c_io.write(self.MPU6050_ADDRESS, loop, 0)
        # Sets clock source to gyro reference w/ PLL
        self.i2c_io.write(self.MPU6050_ADDRESS, self.MPU6050_RA_PWR_MGMT_1, 0b00000010)
        # Controls frequency of wakeups in accel low power mode plus the sensor standby modes
        self.i2c_io.write(self.MPU6050_ADDRESS, self.MPU6050_RA_PWR_MGMT_2, 0x00)
        # Enables any I2C master interrupt source to generate an interrupt
        self.i2c_io.write(self.MPU6050_ADDRESS, self.MPU6050_RA_INT_ENABLE, 0x01)
        # enable the FIFO
        self.enableFifo()
        logger.debug("Initialised device")


Instruction:
Create a Python 3 function for
enabling the acceleration sensor for this device.

Response:
def enableAccelerometer(self):
        """
        Specifies the device should write acceleration values to the FIFO, is not applied until enableFIFO is called.
        :return:
        """
        logger.debug("Enabling acceleration sensor")
        self.fifoSensorMask |= self.enableAccelerometerMask
        self._accelEnabled = True
        self._setSampleSizeBytes()


Instruction:
Implement a Python 3 function for
disabling the acceleration sensor for the current process.

Response:
def disableAccelerometer(self):
        """
        Specifies the device should NOT write acceleration values to the FIFO, is not applied until enableFIFO is
        called.
        :return:
        """
        logger.debug("Disabling acceleration sensor")
        self.fifoSensorMask &= ~self.enableAccelerometerMask
        self._accelEnabled = False
        self._setSampleSizeBytes()


Instruction:
Create a Python 3 function to
enable the gyro sensor for this software.

Response:
def enableGyro(self):
        """
        Specifies the device should write gyro values to the FIFO, is not applied until enableFIFO is called.
        :return:
        """
        logger.debug("Enabling gyro sensor")
        self.fifoSensorMask |= self.enableGyroMask
        self._gyroEnabled = True
        self._setSampleSizeBytes()


Instruction:
Here you have a function in Python 3, explain what it does
def disableGyro(self):
        logger.debug("Disabling gyro sensor")
        self.fifoSensorMask &= ~self.enableGyroMask
        self._gyroEnabled = False
        self._setSampleSizeBytes()

Response: Disables the gyro sensor for the current process.


Instruction:
How would you implement a function in Python 3 that
enables the temperature sensor for the current process.

Response:
def enableTemperature(self):
        """
        Specifies the device should write temperature values to the FIFO, is not applied until enableFIFO is called.
        :return:
        """
        logger.debug("Enabling temperature sensor")
        self.fifoSensorMask |= self.enableTemperatureMask
        self._setSampleSizeBytes()


Instruction:
Can you generate the documentation for the following Python 3 function
def disableTemperature(self):
        logger.debug("Disabling temperature sensor")
        self.fifoSensorMask &= ~self.enableTemperatureMask
        self._setSampleSizeBytes()

Response: Disables the temperature sensor.


Instruction:
Write a Python 3 function that can
set the gyro sensitivity value for the current locale.

Response:
def setGyroSensitivity(self, value):
        """
        Sets the gyro sensitivity to 250, 500, 1000 or 2000 according to the given value (and implicitly disables the
        self
        tests)
        :param value: the target sensitivity.
        """
        try:
            self.i2c_io.write(self.MPU6050_ADDRESS, self.MPU6050_RA_GYRO_CONFIG,
                              {250: 0, 500: 8, 1000: 16, 2000: 24}[value])
            self._gyroFactor = value / 32768.0
            self.gyroSensitivity = value
            logger.debug("Set gyro sensitivity = %d", value)
        except KeyError:
            raise ArgumentError(value + " is not a valid sensitivity (250,500,1000,2000)")


Instruction:
Can you tell what is the following Python 3 function doing
def setAccelerometerSensitivity(self, value):
        # note that this implicitly disables the self tests on each axis
        # i.e. the full byte is actually 000[accel]000 where the 1st 3 are the accelerometer self tests, the next two
        # values are the actual sensitivity and the last 3 are unused
        # the 2 [accel] bits are translated by the device as follows; 00 = 2g, 01 = 4g, 10 = 8g, 11 = 16g
        # in binary we get 2 = 0, 4 = 1000, 8 = 10000, 16 = 11000
        # so the 1st 3 bits are always 0
        try:
            self.i2c_io.write(self.MPU6050_ADDRESS,
                              self.MPU6050_RA_ACCEL_CONFIG,
                              {2: 0, 4: 8, 8: 16, 16: 24}[value])
            self._accelerationFactor = value / 32768.0
            self.accelerometerSensitivity = value
            logger.debug("Set accelerometer sensitivity = %d", value)
        except KeyError:
            raise ArgumentError(value + " is not a valid sensitivity (2,4,8,18)")

Response: Sets the accelerometer sensitivity to the given value.


Instruction:
How would you explain what the following Python 3 function does
def setSampleRate(self, targetSampleRate):
        sampleRateDenominator = int((8000 / min(targetSampleRate, 1000)) - 1)
        self.i2c_io.write(self.MPU6050_ADDRESS, self.MPU6050_RA_SMPLRT_DIV, sampleRateDenominator)
        self.fs = 8000.0 / (sampleRateDenominator + 1.0)
        logger.debug("Set sample rate = %d", self.fs)

Response: Sets the internal sample rate of the MPU - 6050.


Instruction:
Given the following Python 3 function, write the documentation
def resetFifo(self):
        logger.debug("Resetting FIFO")
        self.i2c_io.write(self.MPU6050_ADDRESS, self.MPU6050_RA_USER_CTRL, 0b00000000)
        pass
        self.i2c_io.write(self.MPU6050_ADDRESS, self.MPU6050_RA_USER_CTRL, 0b00000100)
        pass
        self.i2c_io.write(self.MPU6050_ADDRESS, self.MPU6050_RA_USER_CTRL, 0b01000000)
        self.getInterruptStatus()

Response: Resets the FIFO by first disabling the FIFO then sending a FIFO_RESET and then re - enabling the FIFO.


Instruction:
Given the following Python 3 function, write the documentation
def enableFifo(self):
        logger.debug("Enabling FIFO")
        self.i2c_io.write(self.MPU6050_ADDRESS, self.MPU6050_RA_FIFO_EN, 0)
        self.resetFifo()
        self.i2c_io.write(self.MPU6050_ADDRESS, self.MPU6050_RA_FIFO_EN, self.fifoSensorMask)
        logger.debug("Enabled FIFO")

Response: Enables the FIFO and resets it and sets which values should be written to the FIFO.


Instruction:
Here you have a function in Python 3, explain what it does
def getFifoCount(self):
        bytes = self.i2c_io.readBlock(self.MPU6050_ADDRESS, self.MPU6050_RA_FIFO_COUNTH, 2)
        count = (bytes[0] << 8) + bytes[1]
        logger.debug("FIFO Count: %d", count)
        return count

Response: Gets the number of bytes available on the FIFO right now.


Instruction:
Write a Python 3 script to
read the specified number of bytes from the FIFO and returns them as a bytearray.

Response:
def getDataFromFIFO(self, bytesToRead):
        """
        reads the specified number of bytes from the FIFO, should be called after a call to getFifoCount to ensure there
        is new data available (to avoid reading duplicate data).
        :param bytesToRead: the number of bytes to read.
        :return: the bytes read.
        """
        return self.i2c_io.readBlock(self.MPU6050_ADDRESS, self.MPU6050_RA_FIFO_R_W, bytesToRead)


Instruction:
Implement a function in Python 3 to
read a batchSize batch of data from the FIFO while attempting to optimise the number of times we have to read from the device itself. :return: a list of data where each item is a single sample of data converted into real values and stored as a dict.

Response:
def provideData(self):
        """
        reads a batchSize batch of data from the FIFO while attempting to optimise the number of times we have to read
        from the device itself.
        :return: a list of data where each item is a single sample of data converted into real values and stored as a
        dict.
        """
        samples = []
        fifoBytesAvailable = 0
        fifoWasReset = False
        logger.debug(">> provideData target %d samples", self.samplesPerBatch)
        iterations = 0
        # allow 1.5x the expected duration of the batch
        breakTime = time() + ((self.samplesPerBatch / self.fs) * 1.5)
        overdue = False
        while len(samples) < self.samplesPerBatch and not overdue:
            iterations += 1
            if iterations > self.samplesPerBatch and iterations % 100 == 0:
                if time() > breakTime:
                    logger.warning("Breaking measurement after %d iterations, batch overdue", iterations)
                    overdue = True
            if fifoBytesAvailable < self.sampleSizeBytes or fifoWasReset:
                interrupt = self.getInterruptStatus()
                fifoBytesAvailable = self.getFifoCount()
                fifoWasReset = False
            logger.debug("Start sample loop [available: %d , required: %d]", fifoBytesAvailable, self.sampleSizeBytes)
            if interrupt & 0x10:
                logger.error("FIFO OVERFLOW, RESETTING [available: %d , interrupt: %d]", fifoBytesAvailable, interrupt)
                self.measurementOverflowed = True
                self.resetFifo()
                fifoWasReset = True
            elif fifoBytesAvailable == 1024:
                logger.error("FIFO FULL, RESETTING [available: %d , interrupt: %d]", fifoBytesAvailable, interrupt)
                self.measurementOverflowed = True
                self.resetFifo()
                fifoWasReset = True
            elif interrupt & 0x02 or interrupt & 0x01:
                # wait for at least 1 sample to arrive, should be a VERY short wait
                while fifoBytesAvailable < self.sampleSizeBytes:
                    logger.debug("Waiting for sample [available: %d , required: %d]", fifoBytesAvailable,
                                 self.sampleSizeBytes)
                    fifoBytesAvailable = self.getFifoCount()
                logger.debug("Processing data [available: %d , required: %d]", fifoBytesAvailable, self.sampleSizeBytes)
                fifoReadBytes = self.sampleSizeBytes
                # TODO this chunk of code is a bit messy, tidy it up
                # if we have more than 1 sample available then ensure we read as many as we can at once (albeit within
                # the limits of the max i2c read size of 32 bytes)
                if fifoBytesAvailable > self.sampleSizeBytes:
                    fifoReadBytes = min(fifoBytesAvailable // self.sampleSizeBytes,
                                        self.maxBytesPerFifoRead) * self.sampleSizeBytes
                    logger.debug("Excess bytes to read [available: %d , reading: %d]", fifoBytesAvailable,
                                 fifoReadBytes)
                # but don't read more than we need to fulfil the batch
                samplesToRead = fifoReadBytes // self.sampleSizeBytes
                excessSamples = self.samplesPerBatch - len(samples) - samplesToRead
                if excessSamples < 0:
                    samplesToRead += excessSamples
                    fifoReadBytes = int(samplesToRead * self.sampleSizeBytes)
                    logger.debug("Excess samples to read [available: %d , reading: %d]", fifoBytesAvailable,
                                 fifoReadBytes)
                else:
                    logger.debug("Reading [available: %d , reading: %d]", fifoBytesAvailable, fifoReadBytes)
                # read the bytes from the fifo, break it into sample sized chunks and convert to the actual values
                fifoBytes = self.getDataFromFIFO(fifoReadBytes)
                samples.extend([self.unpackSample(fifoBytes[i:i + self.sampleSizeBytes])
                                for i in range(0, len(fifoBytes), self.sampleSizeBytes)])
                # track the count here so we can avoid going back to the FIFO each time
                fifoBytesAvailable -= fifoReadBytes
                logger.debug("End sample loop [available: %d , required: %d]", fifoBytesAvailable, self.sampleSizeBytes)
        logger.debug("<< provideData %d samples", len(samples))
        return samples


Instruction:
How would you explain what the following Python 3 function does
def unpackSample(self, rawData):
        length = len(rawData)
        # TODO error if not multiple of 2
        # logger.debug(">> unpacking sample %d length %d", self._sampleIdx, length)
        unpacked = struct.unpack(">" + ('h' * (length // 2)), memoryview(bytearray(rawData)).tobytes())
        # store the data in a dictionary
        mpu6050 = collections.OrderedDict()
        mpu6050[SAMPLE_TIME] = self._sampleIdx / self.fs
        sensorIdx = 0
        if self.isAccelerometerEnabled():
            mpu6050[ACCEL_X] = unpacked[sensorIdx] * self._accelerationFactor
            sensorIdx += 1
            mpu6050[ACCEL_Y] = unpacked[sensorIdx] * self._accelerationFactor
            sensorIdx += 1
            mpu6050[ACCEL_Z] = unpacked[sensorIdx] * self._accelerationFactor
            sensorIdx += 1

        if self.isTemperatureEnabled():
            mpu6050[TEMP] = unpacked[sensorIdx] * self._temperatureGain + self._temperatureOffset
            sensorIdx += 1

        if self.isGyroEnabled():
            mpu6050[GYRO_X] = unpacked[sensorIdx] * self._gyroFactor
            sensorIdx += 1
            mpu6050[GYRO_Y] = unpacked[sensorIdx] * self._gyroFactor
            sensorIdx += 1
            mpu6050[GYRO_Z] = unpacked[sensorIdx] * self._gyroFactor
            sensorIdx += 1
        # TODO should we send as a dict so the keys are available?
        output = list(mpu6050.values())
        self._sampleIdx += 1
        # logger.debug("<< unpacked sample length %d into vals size %d", length, len(output))
        return output

Response: This function unpacks a single sample of data into a list of the most recent available sensors.


Instruction:
Create a Python 3 function for
wrapping text in a single line.

Response:
def wrap(text, width, *args, **kwargs):
    """
    Like :func:`textwrap.wrap` but preserves existing newlines which
    :func:`textwrap.wrap` does not otherwise handle well.

    See Also
    --------
    :func:`textwrap.wrap`
    """

    return sum([textwrap.wrap(line, width, *args, **kwargs)
                if line else [''] for line in text.splitlines()], [])


Instruction:
Can you write a function in Python 3 where it
outputs line-wrapped text wrapped in a box drawn with a repeated (usually ASCII) character. For example: >>> print(textbox('Text to wrap', width=16)) ################ # # # Text to wrap # # # ################ Parameters ---------- text : string The text to wrap width : int The width of the entire box, including the perimeter and the indentation space. Because the wrapped text is padded with an additional column of whitespace on each side, the minimum width is 5--any width less than that is is automatically increased to 5 (default: 78) boxchar : string (No pun intended.) The character to draw the box with. May also be a string of multiple characters (default: '#') indent : int Amount of space by which the box should be indented. (default: 0)

Response:
def textbox(text, width=78, boxchar='#', indent=0):
    """
    Outputs line-wrapped text wrapped in a box drawn with a repeated (usually
    ASCII) character.

    For example:

        >>> print(textbox('Text to wrap', width=16))
        ################
        #              #
        # Text to wrap #
        #              #
        ################

    Parameters
    ----------
    text : string
        The text to wrap

    width : int
        The width of the entire box, including the perimeter and
        the indentation space.  Because the
        wrapped text is padded with an additional column of whitespace on each
        side, the minimum width is 5--any width less than that is
        is automatically increased to 5 (default: 78)

    boxchar : string
        (No pun intended.) The character to draw the box with.  May also
        be a string of multiple characters (default: '#')

    indent : int
        Amount of space by which the box should be indented. (default: 0)

    """

    min_width = len(boxchar) * 2 + 3
    width = max(width-indent, min_width)
    indentspace = indent * ' '

    wrap_width = width - min_width + 1

    q, r = divmod(width, len(boxchar))
    # The top/bottom border
    top_border = indentspace + boxchar * q + boxchar[:r]
    top_padding  = indentspace + boxchar + ' ' * (width - len(boxchar) * 2) + boxchar

    lines = ['%s%s %s %s' % (indentspace, boxchar, line.ljust(wrap_width),
                             boxchar)
             for line in wrap(text, wrap_width)]
    top = [top_border, top_padding]
    bottom = [top_padding, top_border]
    return '\n'.join(top + lines + bottom)


Instruction:
How would you explain what the following Python 3 function does
def independence_day(year, observed=None):
    '''July 4th'''
    day = 4

    if observed:
        if calendar.weekday(year, JUL, 4) == SAT:
            day = 3

        if calendar.weekday(year, JUL, 4) == SUN:
            day = 5

    return (year, JUL, day)

Response: July 4th or SUN


Instruction:
How would you code a function in Python 3 to
return the columbus day of the given year.

Response:
def columbus_day(year, country='usa'):
    '''in USA: 2nd Monday in Oct
       Elsewhere: Oct 12'''
    if country == 'usa':
        return nth_day_of_month(2, MON, OCT, year)
    else:
        return (year, OCT, 12)


Instruction:
Can you implement a function in Python 3 that
returns the first thanksgiving entry for a given year.

Response:
def thanksgiving(year, country='usa'):
    '''USA: last Thurs. of November, Canada: 2nd Mon. of October'''
    if country == 'usa':
        if year in [1940, 1941]:
            return nth_day_of_month(3, THU, NOV, year)
        elif year == 1939:
            return nth_day_of_month(4, THU, NOV, year)
        else:
            return nth_day_of_month(0, THU, NOV, year)

    if country == 'canada':
        return nth_day_of_month(2, MON, OCT, year)


Instruction:
Explain what the following Python 3 code does
def linefit(x, y, weights=None):

    """
    Parameters
    ----------
    y: 1D numpy array
        The data to be fitted
    x: 1D numpy array
        The x values of the y array. x and y must
        have the same shape.
    weights:   1D numpy array, must have the same shape as x and y
        weight values

    Examples
    --------
    >>> import numpy as N
    >>> from numpy.core import around
    >>> x = N.array([-5, -4 ,-3 ,-2 ,-1, 0, 1, 2, 3, 4, 5])
    >>> y = N.array([1, 5, 4, 7, 10, 8, 9, 13, 14, 13, 18])
    >>> around(linefit(x,y), decimals=5)
    array([9.27273, 1.43636])
    >>> x = N.array([1.3,1.3,2.0,2.0,2.7,3.3,3.3,3.7,3.7,4.,4.,4.,4.7,4.7,5.,5.3,5.3,5.3,5.7,6.,6.,6.3,6.7])
    >>> y = N.array([2.3,1.8,2.8,1.5,2.2,3.8,1.8,3.7,1.7,2.8,2.8,2.2,3.2,1.9,1.8,3.5,2.8,2.1,3.4,3.2,3.,3.,5.9])
    >>> around(linefit(x,y), decimals=5)
    array([1.42564, 0.31579])
    """
    if len(x) != len(y):
        print("Error: X and Y must have equal size\n")
        return
    n = len(x)
    w = N.zeros((n,n)).astype(N.float)
    if weights is None:
        for i in N.arange(n):
            w[i,i] = 1
    else:
        if len(weights) != n:
            print("Error: Weights must have the same size as X and Y.\n")
            return
        for i in N.arange(n):
            w[i,i] = weights[i]
    x = x.astype(N.float)
    y = y.astype(N.float)
    # take the weighted avg for calculatiing the covarince
    Xavg = N.sum(N.dot(w,x)) / N.sum(w.diagonal())
    Yavg = N.sum(N.dot(w,y)) / N.sum(w.diagonal())

    xm = x - Xavg
    xmt = N.transpose(xm)
    ym = y - Yavg

    b1 = N.dot(xmt,N.dot(w,ym)) / N.dot(xmt ,N.dot(w,xm))
    b0 = Yavg - b1 * Xavg

    return b0, b1

Response: This function calculates the linefit of a single language language.


Instruction:
Implement a Python 3 function for
returning the data for the given measurementId.

Response:
def get(self, measurementId):
        """
        Analyses the measurement with the given parameters
        :param measurementId:
        :return:
        """
        logger.info('Analysing ' + measurementId)
        measurement = self._measurementController.getMeasurement(measurementId, MeasurementStatus.COMPLETE)
        if measurement is not None:
            if measurement.inflate():
                data = {
                    name: {
                        'spectrum': {
                            'x': self._jsonify(data.spectrum('x')),
                            'y': self._jsonify(data.spectrum('y')),
                            'z': self._jsonify(data.spectrum('z')),
                            'sum': self._jsonify(data.spectrum('sum'))
                        },
                        'psd': {
                            'x': self._jsonify(data.psd('x')),
                            'y': self._jsonify(data.psd('y')),
                            'z': self._jsonify(data.psd('z'))
                        },
                        'peakSpectrum': {
                            'x': self._jsonify(data.peakSpectrum('x')),
                            'y': self._jsonify(data.peakSpectrum('y')),
                            'z': self._jsonify(data.peakSpectrum('z')),
                            'sum': self._jsonify(data.peakSpectrum('sum'))
                        }
                    }
                    for name, data in measurement.data.items()
                }
                return data, 200
            else:
                return None, 404
        else:
            return None, 404


Instruction:
How would you code a function in Python 3 to
apply the target state to the current state of the current state.

Response:
def _applyTargetState(targetState, md, httpclient):
    """
    compares the current device state against the targetStateProvider and issues updates as necessary to ensure the
    device is
    at that state.
    :param md:
    :param targetState: the target state.
    :param httpclient: the http client
    :return:
    """
    anyUpdate = False
    if md['fs'] != targetState.fs:
        logger.info("Updating fs from " + str(md['fs']) + " to " + str(targetState.fs) + " for " + md['name'])
        anyUpdate = True

    if md['samplesPerBatch'] != targetState.samplesPerBatch:
        logger.info("Updating samplesPerBatch from " + str(md['samplesPerBatch']) + " to " + str(
            targetState.samplesPerBatch) + " for " + md['name'])
        anyUpdate = True

    if md['gyroEnabled'] != targetState.gyroEnabled:
        logger.info("Updating gyroEnabled from " + str(md['gyroEnabled']) + " to " + str(
            targetState.gyroEnabled) + " for " + md['name'])
        anyUpdate = True

    if md['gyroSens'] != targetState.gyroSens:
        logger.info(
            "Updating gyroSens from " + str(md['gyroSens']) + " to " + str(targetState.gyroSens) + " for " + md[
                'name'])
        anyUpdate = True

    if md['accelerometerEnabled'] != targetState.accelerometerEnabled:
        logger.info("Updating accelerometerEnabled from " + str(md['accelerometerEnabled']) + " to " + str(
            targetState.accelerometerEnabled) + " for " + md['name'])
        anyUpdate = True

    if md['accelerometerSens'] != targetState.accelerometerSens:
        logger.info("Updating accelerometerSens from " + str(md['accelerometerSens']) + " to " + str(
            targetState.accelerometerSens) + " for " + md['name'])
        anyUpdate = True

    if anyUpdate:
        payload = marshal(targetState, targetStateFields)
        logger.info("Applying target state change " + md['name'] + " - " + str(payload))
        if RecordingDeviceStatus.INITIALISED.name == md.get('status'):
            try:
                httpclient.patch(md['serviceURL'], json=payload)
            except Exception as e:
                logger.exception(e)
        else:
            logger.warning("Ignoring target state change until " + md['name'] + " is idle, currently " + md['status'])
    else:
        logger.debug("Device " + md['name'] + " is at target state, we continue")


Instruction:
Implement a function in Python 3 to
update the target state of the specified device.

Response:
def updateDeviceState(self, device):
        """
        Updates the target state on the specified device.
        :param targetState: the target state to reach.
        :param device: the device to update.
        :return:
        """
        # this is only threadsafe because the targetstate is effectively immutable, if it becomes mutable in future then
        # funkiness may result
        self._reactor.offer(REACH_TARGET_STATE, [self._targetStateProvider.state, device, self._httpclient])


Instruction:
Implement a Python 3 function for
updating the system target state and propagates that to all devices.

Response:
def updateTargetState(self, newState):
        """
        Updates the system target state and propagates that to all devices.
        :param newState:
        :return:
        """
        self._targetStateProvider.state = loadTargetState(newState, self._targetStateProvider.state)
        for device in self.deviceController.getDevices():
            self.updateDeviceState(device.payload)


Instruction:
Explain what the following Python 3 code does
def convert(input, width=132, output=None, keep=False):

    """Input ASCII trailer file "input" will be read.

    The contents will then be written out to a FITS file in the same format
    as used by 'stwfits' from IRAF.

    Parameters
    ===========
    input : str
        Filename of input ASCII trailer file

    width : int
        Number of characters wide to use for defining output FITS column
        [Default: 132]

    output : str
        Filename to use for writing out converted FITS trailer file
        If None, input filename will be converted from *.tra -> *_trl.fits
        [Default: None]

    keep : bool
        Specifies whether or not to keep any previously written FITS files
        [Default: False]

    """
    # open input trailer file
    trl = open(input)

    # process all lines
    lines = np.array([i for text in trl.readlines() for i in textwrap.wrap(text,width=width)])

    # close ASCII trailer file now that we have processed all the lines
    trl.close()

    if output is None:
        # create fits file
        rootname,suffix = os.path.splitext(input)
        s = suffix[1:].replace('ra','rl')
        fitsname = "{}_{}{}fits".format(rootname,s,os.path.extsep)
    else:
        fitsname = output
    full_name = os.path.abspath(os.path.join(os.path.curdir,fitsname))

    old_file = os.path.exists(full_name)
    if old_file:
        if keep:
            print("ERROR: Trailer file already written out as: {}".format(full_name))
            raise IOError
        else:
            os.remove(full_name)

    # Build FITS table and write it out
    line_fmt = "{}A".format(width)
    tbhdu = fits.BinTableHDU.from_columns([fits.Column(name='TEXT_FILE',format=line_fmt,array=lines)])
    tbhdu.writeto(fitsname)

    print("Created output FITS filename for trailer:{}    {}".format(os.linesep,full_name))

    os.remove(input)

Response: Convert an ASCII trailer file from IRAF to FITS.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_extra_values(conf, _prepend=()):
    out = []

    out.extend([(_prepend, name) for name in conf.extra_values])
    for name in conf.sections:
        if name not in conf.extra_values:
            out.extend(get_extra_values(conf[name], _prepend + (name,)))
    return out

Response:
Get all the extra values from a validated
   .


Instruction:
Here you have a function in Python 3, explain what it does
def popitem(self):
        sequence = (self.scalars + self.sections)
        if not sequence:
            raise KeyError(": 'popitem(): dictionary is empty'")
        key = sequence[0]
        val =  self[key]
        del self[key]
        return key, val

Response: Removes the first element from the dictionary and returns it.


Instruction:
How would you explain what the following Python 3 function does
def dict(self):
        newdict = {}
        for entry in self:
            this_entry = self[entry]
            if isinstance(this_entry, Section):
                this_entry = this_entry.dict()
            elif isinstance(this_entry, list):
                # create a copy rather than a reference
                this_entry = list(this_entry)
            elif isinstance(this_entry, tuple):
                # create a copy rather than a reference
                this_entry = tuple(this_entry)
            newdict[entry] = this_entry
        return newdict

Response: Return a deepcopy of self as a dictionary.


Instruction:
Create a Python 3 function for
renaming an entry in the dictionary with the given keyname.

Response:
def rename(self, oldkey, newkey):
        """
        Change a keyname to another, without changing position in sequence.

        Implemented so that transformations can be made on keys,
        as well as on values. (used by encode and decode)

        Also renames comments.
        """
        if oldkey in self.scalars:
            the_list = self.scalars
        elif oldkey in self.sections:
            the_list = self.sections
        else:
            raise KeyError('Key "%s" not found.' % oldkey)
        pos = the_list.index(oldkey)
        #
        val = self[oldkey]
        dict.__delitem__(self, oldkey)
        dict.__setitem__(self, newkey, val)
        the_list.remove(oldkey)
        the_list.insert(pos, newkey)
        comm = self.comments[oldkey]
        inline_comment = self.inline_comments[oldkey]
        del self.comments[oldkey]
        del self.inline_comments[oldkey]
        self.comments[newkey] = comm
        self.inline_comments[newkey] = inline_comment


Instruction:
Given the following Python 3 function, write the documentation
def walk(self, function, raise_errors=True,
            call_on_sections=False, **keywargs):
        """
        Walk every member and call a function on the keyword and value.

        Return a dictionary of the return values

        If the function raises an exception, raise the errror
        unless ``raise_errors=False``, in which case set the return value to
        ``False``.

        Any unrecognised keyword arguments you pass to walk, will be pased on
        to the function you pass in.

        Note: if ``call_on_sections`` is ``True`` then - on encountering a
        subsection, *first* the function is called for the *whole* subsection,
        and then recurses into it's members. This means your function must be
        able to handle strings, dictionaries and lists. This allows you
        to change the key of subsections as well as for ordinary members. The
        return value when called on the whole subsection has to be discarded.

        See  the encode and decode methods for examples, including functions.

        admonition:: caution

        You can use ``walk`` to transform the names of members of a section
        but you mustn't add or delete members.

        >>> config = '''[XXXXsection]
        ... XXXXkey = XXXXvalue'''.splitlines()
        >>> cfg = ConfigObj(config)
        >>> cfg
        ConfigObj({'XXXXsection': {'XXXXkey': 'XXXXvalue'}})
        >>> def transform(section, key):
        ...     val = section[key]
        ...     newkey = key.replace('XXXX', 'CLIENT1')
        ...     section.rename(key, newkey)
        ...     if isinstance(val, (tuple, list, dict)):
        ...         pass
        ...     else:
        ...         val = val.replace('XXXX', 'CLIENT1')
        ...         section[newkey] = val
        >>> cfg.walk(transform, call_on_sections=True)
        {'CLIENT1section': {'CLIENT1key': None}}
        >>> cfg
        ConfigObj({'CLIENT1section': {'CLIENT1key': 'CLIENT1value'}})
        """
        out = {}
        # scalars first
        for i in range(len(self.scalars)):
            entry = self.scalars[i]
            try:
                val = function(self, entry, **keywargs)
                # bound again in case name has changed
                entry = self.scalars[i]
                out[entry] = val
            except Exception:
                if raise_errors:
                    raise
                else:
                    entry = self.scalars[i]
                    out[entry] = False
        # then sections
        for i in range(len(self.sections)):
            entry = self.sections[i]
            if call_on_sections:
                try:
                    function(self, entry, **keywargs)
                except Exception:
                    if raise_errors:
                        raise
                    else:
                        entry = self.sections[i]
                        out[entry] = False
                # bound again in case name has changed
                entry = self.sections[i]
            # previous result is discarded
            out[entry] = self[entry].walk(
                function,
                raise_errors=raise_errors,
                call_on_sections=call_on_sections,
                **keywargs)
        return out

Response: Walks the configuration tree and calls a function on the keyword and value.


Instruction:
Can you tell what is the following Python 3 function doing
def as_bool(self, key):
        val = self[key]
        if val == True:
            return True
        elif val == False:
            return False
        else:
            try:
                if not isinstance(val, string_types):
                    # TODO: Why do we raise a KeyError here?
                    raise KeyError()
                else:
                    return self.main._bools[val.lower()]
            except KeyError:
                raise ValueError('Value "%s" is neither True nor False' % val)

Response: Returns a value from the ConfigObj corresponding to the given key as a boolean.


Instruction:
How would you explain what the following Python 3 function does
def restore_default(self, key):
        default = self.default_values[key]
        dict.__setitem__(self, key, default)
        if key not in self.defaults:
            self.defaults.append(key)
        return default

Response: Restore the default value for the specified key.


Instruction:
Can you generate the documentation for the following Python 3 function
def restore_defaults(self):
        for key in self.default_values:
            self.restore_default(key)

        for section in self.sections:
            self[section].restore_defaults()

Response:
Recursively restore default values to all members
        that have them.


Instruction:
Can you tell what is the following Python 3 function doing
def _handle_bom(self, infile):
        if ((self.encoding is not None) and
            (self.encoding.lower() not in BOM_LIST)):
            # No need to check for a BOM
            # the encoding specified doesn't have one
            # just decode
            return self._decode(infile, self.encoding)

        if isinstance(infile, (list, tuple)):
            line = infile[0]
        else:
            line = infile
        if self.encoding is not None:
            # encoding explicitly supplied
            # And it could have an associated BOM
            # TODO: if encoding is just UTF16 - we ought to check for both
            # TODO: big endian and little endian versions.
            enc = BOM_LIST[self.encoding.lower()]
            if enc == 'utf_16':
                # For UTF16 we try big endian and little endian
                for BOM, (encoding, final_encoding) in list(BOMS.items()):
                    if not final_encoding:
                        # skip UTF8
                        continue
                    if infile.startswith(BOM):
                        ### BOM discovered
                        ##self.BOM = True
                        # Don't need to remove BOM
                        return self._decode(infile, encoding)

                # If we get this far, will *probably* raise a DecodeError
                # As it doesn't appear to start with a BOM
                return self._decode(infile, self.encoding)

            # Must be UTF8
            BOM = BOM_SET[enc]
            if not line.startswith(BOM):
                return self._decode(infile, self.encoding)

            newline = line[len(BOM):]

            # BOM removed
            if isinstance(infile, (list, tuple)):
                infile[0] = newline
            else:
                infile = newline
            self.BOM = True
            return self._decode(infile, self.encoding)

        # No encoding specified - so we need to check for UTF8/UTF16
        for BOM, (encoding, final_encoding) in list(BOMS.items()):
            if not isinstance(BOM, str) or not line.startswith(BOM):
                continue
            else:
                # BOM discovered
                self.encoding = final_encoding
                if not final_encoding:
                    self.BOM = True
                    # UTF8
                    # remove BOM
                    newline = line[len(BOM):]
                    if isinstance(infile, (list, tuple)):
                        infile[0] = newline
                    else:
                        infile = newline
                    # UTF8 - don't decode
                    if isinstance(infile, string_types):
                        return infile.splitlines(True)
                    else:
                        return infile
                # UTF16 - have to decode
                return self._decode(infile, encoding)

        # No BOM discovered and no encoding specified, just return
        if isinstance(infile, string_types):
            # infile read from a file will be a single string
            return infile.splitlines(True)
        return infile

Response: Handle any BOM and decode if necessary.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _decode(self, infile, encoding):
        if isinstance(infile, string_types):
            # can't be unicode
            # NOTE: Could raise a ``UnicodeDecodeError``
            return infile.decode(encoding).splitlines(True)
        for i, line in enumerate(infile):
            # NOTE: The isinstance test here handles mixed lists of unicode/string
            # NOTE: But the decode will break on any non-string values
            # NOTE: Or could raise a ``UnicodeDecodeError``
            if PY3K:
                if not isinstance(line, str):
                    infile[i] = line.decode(encoding)
            else:
                if not isinstance(line, unicode):
                    infile[i] = line.decode(encoding)
        return infile

Response: Decode infile to unicode using the specified encoding.


Instruction:
Implement a Python 3 function for
decoding element to unicode if necessary.

Response:
def _decode_element(self, line):
        """Decode element to unicode if necessary."""
        if not self.encoding:
            return line
        if isinstance(line, str) and self.default_encoding:
            return line.decode(self.default_encoding)
        return line


Instruction:
Here you have a function in Python 3, explain what it does
def _match_depth(self, sect, depth):
        while depth < sect.depth:
            if sect is sect.parent:
                # we've reached the top level already
                raise SyntaxError()
            sect = sect.parent
        if sect.depth == depth:
            return sect
        # shouldn't get here
        raise SyntaxError()

Response: Given a section and a depth level return a reference to the right section.


Instruction:
Create a Python 3 function for
handling an error according to the error settings.

Response:
def _handle_error(self, text, ErrorClass, infile, cur_index):
        """
        Handle an error according to the error settings.

        Either raise the error or store it.
        The error will have occured at ``cur_index``
        """
        line = infile[cur_index]
        cur_index += 1
        message = text % cur_index
        error = ErrorClass(message, cur_index, line)
        if self.raise_errors:
            # raise the error - parsing stops here
            raise error
        # store the error
        # reraise when parsing has finished
        self._errors.append(error)


Instruction:
Given the following Python 3 function, write the documentation
def _unquote(self, value):
        if not value:
            # should only happen during parsing of lists
            raise SyntaxError
        if (value[0] == value[-1]) and (value[0] in ('"', "'")):
            value = value[1:-1]
        return value

Response: Return an unquoted version of a value