Instruction:
Implement a Python 3 function for
parsing the date or time string into a naive datetime object.

Response:
def parse(self, timestr, default=None, ignoretz=False, tzinfos=None, **kwargs):
        """
        Parse the date/time string into a :class:`datetime.datetime` object.

        :param timestr:
            Any date/time string using the supported formats.

        :param default:
            The default datetime object, if this is a datetime object and not
            ``None``, elements specified in ``timestr`` replace elements in the
            default object.

        :param ignoretz:
            If set ``True``, time zones in parsed strings are ignored and a
            naive :class:`datetime.datetime` object is returned.

        :param tzinfos:
            Additional time zone names / aliases which may be present in the
            string. This argument maps time zone names (and optionally offsets
            from those time zones) to time zones. This parameter can be a
            dictionary with timezone aliases mapping time zone names to time
            zones or a function taking two parameters (``tzname`` and
            ``tzoffset``) and returning a time zone.

            The timezones to which the names are mapped can be an integer
            offset from UTC in minutes or a :class:`tzinfo` object.

            .. doctest::
               :options: +NORMALIZE_WHITESPACE

                >>> from dateutil.parser import parse
                >>> from dateutil.tz import gettz
                >>> tzinfos = {"BRST": -10800, "CST": gettz("America/Chicago")}
                >>> parse("2012-01-19 17:21:00 BRST", tzinfos=tzinfos)
                datetime.datetime(2012, 1, 19, 17, 21, tzinfo=tzoffset(u'BRST', -10800))
                >>> parse("2012-01-19 17:21:00 CST", tzinfos=tzinfos)
                datetime.datetime(2012, 1, 19, 17, 21,
                                  tzinfo=tzfile('/usr/share/zoneinfo/America/Chicago'))

            This parameter is ignored if ``ignoretz`` is set.

        :param **kwargs:
            Keyword arguments as passed to ``_parse()``.

        :return:
            Returns a :class:`datetime.datetime` object or, if the
            ``fuzzy_with_tokens`` option is ``True``, returns a tuple, the
            first element being a :class:`datetime.datetime` object, the second
            a tuple containing the fuzzy tokens.

        :raises ValueError:
            Raised for invalid or unknown string format, if the provided
            :class:`tzinfo` is not in a valid format, or if an invalid date
            would be created.

        :raises TypeError:
            Raised for non-string or character stream input.

        :raises OverflowError:
            Raised if the parsed date exceeds the largest valid C integer on
            your system.
        """

        if default is None:
            default = datetime.datetime.now().replace(hour=0, minute=0,
                                                      second=0, microsecond=0)

        res, skipped_tokens = self._parse(timestr, **kwargs)

        if res is None:
            raise ValueError("Unknown string format")

        if len(res) == 0:
            raise ValueError("String does not contain a date.")

        repl = {}
        for attr in ("year", "month", "day", "hour",
                     "minute", "second", "microsecond"):
            value = getattr(res, attr)
            if value is not None:
                repl[attr] = value

        if 'day' not in repl:
            # If the default day exceeds the last day of the month, fall back to
            # the end of the month.
            cyear = default.year if res.year is None else res.year
            cmonth = default.month if res.month is None else res.month
            cday = default.day if res.day is None else res.day

            if cday > monthrange(cyear, cmonth)[1]:
                repl['day'] = monthrange(cyear, cmonth)[1]

        ret = default.replace(**repl)

        if res.weekday is not None and not res.day:
            ret = ret + relativedelta.relativedelta(weekday=res.weekday)

        if not ignoretz:
            if (isinstance(tzinfos, collections.Callable) or
                    tzinfos and res.tzname in tzinfos):

                if isinstance(tzinfos, collections.Callable):
                    tzdata = tzinfos(res.tzname, res.tzoffset)
                else:
                    tzdata = tzinfos.get(res.tzname)

                if isinstance(tzdata, datetime.tzinfo):
                    tzinfo = tzdata
                elif isinstance(tzdata, text_type):
                    tzinfo = tz.tzstr(tzdata)
                elif isinstance(tzdata, integer_types):
                    tzinfo = tz.tzoffset(res.tzname, tzdata)
                else:
                    raise ValueError("Offset must be tzinfo subclass, "
                                     "tz string, or int offset.")
                ret = ret.replace(tzinfo=tzinfo)
            elif res.tzname and res.tzname in time.tzname:
                ret = ret.replace(tzinfo=tz.tzlocal())
            elif res.tzoffset == 0:
                ret = ret.replace(tzinfo=tz.tzutc())
            elif res.tzoffset:
                ret = ret.replace(tzinfo=tz.tzoffset(res.tzname, res.tzoffset))

        if kwargs.get('fuzzy_with_tokens', False):
            return ret, skipped_tokens
        else:
            return ret


Instruction:
Given the following Python 3 function, write the documentation
def tzname_in_python2(namefunc):
    def adjust_encoding(*args, **kwargs):
        name = namefunc(*args, **kwargs)
        if name is not None and not PY3:
            name = name.encode()

        return name

    return adjust_encoding

Response: Change unicode output into bytestrings in Python 2 and Python 3 changed in Python 3.


Instruction:
Can you create a Python 3 function that
returns True if the wall time of a given datetime is ambiguous in thisCOOKIE zone.

Response:
def is_ambiguous(self, dt):
        """
        Whether or not the "wall time" of a given datetime is ambiguous in this
        zone.

        :param dt:
            A :py:class:`datetime.datetime`, naive or time zone aware.

        :return:
            Returns ``True`` if ambiguous, ``False`` otherwise.

        .. versionadded:: 2.6.0
        """

        dt = dt.replace(tzinfo=self)

        wall_0 = enfold(dt, fold=0)
        wall_1 = enfold(dt, fold=1)

        same_offset = wall_0.utcoffset() == wall_1.utcoffset()
        same_dt = wall_0.replace(tzinfo=None) == wall_1.replace(tzinfo=None)

        return same_dt and not same_offset


Instruction:
Here you have a function in Python 3, explain what it does
def _fold_status(self, dt_utc, dt_wall):
        if self.is_ambiguous(dt_wall):
            delta_wall = dt_wall - dt_utc
            _fold = int(delta_wall == (dt_utc.utcoffset() - dt_utc.dst()))
        else:
            _fold = 0

        return _fold

Response: Determines the fold status of a given UTC datetime.


Instruction:
Can you write a function in Python 3 where it
gives a timezone - aware datetime in a given timezone calculates a new object containing the unambiguous entry - set entry for that datetime.

Response:
def _fromutc(self, dt):
        """
        Given a timezone-aware datetime in a given timezone, calculates a
        timezone-aware datetime in a new timezone.

        Since this is the one time that we *know* we have an unambiguous
        datetime object, we take this opportunity to determine whether the
        datetime is ambiguous and in a "fold" state (e.g. if it's the first
        occurence, chronologically, of the ambiguous datetime).

        :param dt:
            A timezone-aware :class:`datetime.datetime` object.
        """

        # Re-implement the algorithm from Python's datetime.py
        dtoff = dt.utcoffset()
        if dtoff is None:
            raise ValueError("fromutc() requires a non-None utcoffset() "
                             "result")

        # The original datetime.py code assumes that `dst()` defaults to
        # zero during ambiguous times. PEP 495 inverts this presumption, so
        # for pre-PEP 495 versions of python, we need to tweak the algorithm.
        dtdst = dt.dst()
        if dtdst is None:
            raise ValueError("fromutc() requires a non-None dst() result")
        delta = dtoff - dtdst

        dt += delta
        # Set fold=1 so we can default to being in the fold for
        # ambiguous dates.
        dtdst = enfold(dt, fold=1).dst()
        if dtdst is None:
            raise ValueError("fromutc(): dt.dst gave inconsistent "
                             "results; cannot convert")
        return dt + dtdst


Instruction:
Implement a Python 3 function for
calculating a from a timezone - aware datetime in a given timezone.

Response:
def fromutc(self, dt):
        """
        Given a timezone-aware datetime in a given timezone, calculates a
        timezone-aware datetime in a new timezone.

        Since this is the one time that we *know* we have an unambiguous
        datetime object, we take this opportunity to determine whether the
        datetime is ambiguous and in a "fold" state (e.g. if it's the first
        occurance, chronologically, of the ambiguous datetime).

        :param dt:
            A timezone-aware :class:`datetime.datetime` object.
        """
        dt_wall = self._fromutc(dt)

        # Calculate the fold status given the two datetimes.
        _fold = self._fold_status(dt, dt_wall)

        # Set the default fold value for ambiguous dates
        return enfold(dt_wall, fold=_fold)


Instruction:
Make a summary of the following Python 3 code
def fromutc(self, dt):
        if not isinstance(dt, datetime):
            raise TypeError("fromutc() requires a datetime argument")

        if dt.tzinfo is not self:
            raise ValueError("dt.tzinfo is not self")

        # Get transitions - if there are none, fixed offset
        transitions = self.transitions(dt.year)
        if transitions is None:
            return dt + self.utcoffset(dt)

        # Get the transition times in UTC
        dston, dstoff = transitions

        dston -= self._std_offset
        dstoff -= self._std_offset

        utc_transitions = (dston, dstoff)
        dt_utc = dt.replace(tzinfo=None)

        isdst = self._naive_isdst(dt_utc, utc_transitions)

        if isdst:
            dt_wall = dt + self._dst_offset
        else:
            dt_wall = dt + self._std_offset

        _fold = int(not isdst and self.is_ambiguous(dt_wall))

        return enfold(dt_wall, fold=_fold)

Response: Given a datetime in UTC return a new time object.


Instruction:
Create a Python 3 function to
return True if the wall time of a given datetime is ambiguous in thisCOOKIE zone.

Response:
def is_ambiguous(self, dt):
        """
        Whether or not the "wall time" of a given datetime is ambiguous in this
        zone.

        :param dt:
            A :py:class:`datetime.datetime`, naive or time zone aware.

        :return:
            Returns ``True`` if ambiguous, ``False`` otherwise.

        .. versionadded:: 2.6.0
        """
        if not self.hasdst:
            return False

        start, end = self.transitions(dt.year)

        dt = dt.replace(tzinfo=None)
        return (end <= dt < end + self._dst_base_offset)


Instruction:
Explain what the following Python 3 code does
def strip_comment_line_with_symbol(line, start):
    parts = line.split(start)
    counts = [len(findall(r'(?:^|[^"\\]|(?:\\\\|\\")+)(")', part))
              for part in parts]
    total = 0
    for nr, count in enumerate(counts):
        total += count
        if total % 2 == 0:
            return start.join(parts[:nr + 1]).rstrip()
    else:  # pragma: no cover
        return line.rstrip()

Response: Strip comments from line string.


Instruction:
Write a Python 3 function that can
strip comments from json string.

Response:
def strip_comments(string, comment_symbols=frozenset(('#', '//'))):
    """Strip comments from json string.

    :param string: A string containing json with comments started by comment_symbols.
    :param comment_symbols: Iterable of symbols that start a line comment (default # or //).
    :return: The string with the comments removed.
    """
    lines = string.splitlines()
    for k in range(len(lines)):
        for symbol in comment_symbols:
            lines[k] = strip_comment_line_with_symbol(lines[k], start=symbol)
    return '\n'.join(lines)


Instruction:
Create a Python 3 function for
rebuilding the internal timezone info in dateutil / zoneinfo * tar * file

Response:
def rebuild(filename, tag=None, format="gz", zonegroups=[], metadata=None):
    """Rebuild the internal timezone info in dateutil/zoneinfo/zoneinfo*tar*

    filename is the timezone tarball from ftp.iana.org/tz.

    """
    tmpdir = tempfile.mkdtemp()
    zonedir = os.path.join(tmpdir, "zoneinfo")
    moduledir = os.path.dirname(__file__)
    try:
        with tar_open(filename) as tf:
            for name in zonegroups:
                tf.extract(name, tmpdir)
            filepaths = [os.path.join(tmpdir, n) for n in zonegroups]
            try:
                check_call(["zic", "-d", zonedir] + filepaths)
            except OSError as e:
                _print_on_nosuchfile(e)
                raise
        # write metadata file
        with open(os.path.join(zonedir, METADATA_FN), 'w') as f:
            json.dump(metadata, f, indent=4, sort_keys=True)
        target = os.path.join(moduledir, ZONEFILENAME)
        with tar_open(target, "w:%s" % format) as tf:
            for entry in os.listdir(zonedir):
                entrypath = os.path.join(zonedir, entry)
                tf.add(entrypath, entry)
    finally:
        shutil.rmtree(tmpdir)


Instruction:
Write a Python 3 script for
picking the nth weekday of the given dayofweek.

Response:
def picknthweekday(year, month, dayofweek, hour, minute, whichweek):
    """ dayofweek == 0 means Sunday, whichweek 5 means last instance """
    first = datetime.datetime(year, month, 1, hour, minute)

    # This will work if dayofweek is ISO weekday (1-7) or Microsoft-style (0-6),
    # Because 7 % 7 = 0
    weekdayone = first.replace(day=((dayofweek - first.isoweekday()) % 7) + 1)
    wd = weekdayone + ((whichweek - 1) * ONEWEEK)
    if (wd.month != month):
        wd -= ONEWEEK

    return wd


Instruction:
Here you have a function in Python 3, explain what it does
def valuestodict(key):
    dout = {}
    size = winreg.QueryInfoKey(key)[1]
    tz_res = None

    for i in range(size):
        key_name, value, dtype = winreg.EnumValue(key, i)
        if dtype == winreg.REG_DWORD or dtype == winreg.REG_DWORD_LITTLE_ENDIAN:
            # If it's a DWORD (32-bit integer), it's stored as unsigned - convert
            # that to a proper signed integer
            if value & (1 << 31):
                value = value - (1 << 32)
        elif dtype == winreg.REG_SZ:
            # If it's a reference to the tzres DLL, load the actual string
            if value.startswith('@tzres'):
                tz_res = tz_res or tzres()
                value = tz_res.name_from_string(value)

            value = value.rstrip('\x00')    # Remove trailing nulls

        dout[key_name] = value

    return dout

Response: Convert a registry key s values to a dictionary.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def load_name(self, offset):
        resource = self.p_wchar()
        lpBuffer = ctypes.cast(ctypes.byref(resource), wintypes.LPWSTR)
        nchar = self.LoadStringW(self._tzres._handle, offset, lpBuffer, 0)
        return resource[:nchar]

Response: Load a timezone name from a DLL offset.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def name_from_string(self, tzname_str):
        if not tzname_str.startswith('@'):
            return tzname_str

        name_splt = tzname_str.split(',-')
        try:
            offset = int(name_splt[1])
        except:
            raise ValueError("Malformed timezone string.")

        return self.load_name(offset)

Response: Parse a string into a localized time zone name.


Instruction:
Make a summary of the following Python 3 code
def transitions(self, year):

        if not self.hasdst:
            return None

        dston = picknthweekday(year, self._dstmonth, self._dstdayofweek,
                               self._dsthour, self._dstminute,
                               self._dstweeknumber)

        dstoff = picknthweekday(year, self._stdmonth, self._stddayofweek,
                                self._stdhour, self._stdminute,
                                self._stdweeknumber)

        # Ambiguous dates default to the STD side
        dstoff -= self._dst_base_offset

        return dston, dstoff

Response:
Returns the DST on and off transition times expressed
        always on the standard time side.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_zonefile_instance(new_instance=False):
    if new_instance:
        zif = None
    else:
        zif = getattr(get_zonefile_instance, '_cached_instance', None)

    if zif is None:
        zif = ZoneInfoFile(getzoneinfofile_stream())

        get_zonefile_instance._cached_instance = zif

    return zif

Response: Returns a new instance of the ZoneInfoFile class.


Instruction:
Can you create a Python 3 function that
returns a time zone object for the given name.

Response:
def gettz(name):
    """
    This retrieves a time zone from the local zoneinfo tarball that is packaged
    with dateutil.

    :param name:
        An IANA-style time zone name, as found in the zoneinfo file.

    :return:
        Returns a :class:`dateutil.tz.tzfile` time zone object.

    .. warning::
        It is generally inadvisable to use this function, and it is only
        provided for API compatibility with earlier versions. This is *not*
        equivalent to ``dateutil.tz.gettz()``, which selects an appropriate
        time zone based on the inputs, favoring system zoneinfo. This is ONLY
        for accessing the dateutil-specific zoneinfo (which may be out of
        date compared to the system zoneinfo).

    .. deprecated:: 2.6
        If you need to use a specific zoneinfofile over the system zoneinfo,
        instantiate a :class:`dateutil.zoneinfo.ZoneInfoFile` object and call
        :func:`dateutil.zoneinfo.ZoneInfoFile.get(name)` instead.

        Use :func:`get_zonefile_instance` to retrieve an instance of the
        dateutil-provided zoneinfo.
    """
    warnings.warn("zoneinfo.gettz() will be removed in future versions, "
                  "to use the dateutil-provided zoneinfo files, instantiate a "
                  "ZoneInfoFile object and use ZoneInfoFile.zones.get() "
                  "instead. See the documentation for details.",
                  DeprecationWarning)

    if len(_CLASS_ZONE_INSTANCE) == 0:
        _CLASS_ZONE_INSTANCE.append(ZoneInfoFile(getzoneinfofile_stream()))
    return _CLASS_ZONE_INSTANCE[0].zones.get(name)


Instruction:
Make a summary of the following Python 3 code
def gettz_db_metadata():
    warnings.warn("zoneinfo.gettz_db_metadata() will be removed in future "
                  "versions, to use the dateutil-provided zoneinfo files, "
                  "ZoneInfoFile object and query the 'metadata' attribute "
                  "instead. See the documentation for details.",
                  DeprecationWarning)

    if len(_CLASS_ZONE_INSTANCE) == 0:
        _CLASS_ZONE_INSTANCE.append(ZoneInfoFile(getzoneinfofile_stream()))
    return _CLASS_ZONE_INSTANCE[0].metadata

Response: Get the zonefile metadata from the database.


Instruction:
Create a Python 3 function for
getting the configuration for the given JID based on XMPP_HTTP_UPLOAD_ACCESS.

Response:
def get_config(jid):
    """Get the configuration for the given JID based on XMPP_HTTP_UPLOAD_ACCESS.

    If the JID does not match any rule, ``False`` is returned.
    """

    acls = getattr(settings, 'XMPP_HTTP_UPLOAD_ACCESS', (('.*', False), ))

    for regex, config in acls:
        if isinstance(regex, six.string_types):
            regex = [regex]

        for subex in regex:
            if re.search(subex, jid):
                return config

    return False


Instruction:
Write a Python 3 script for
giving a datetime and a time zone determine whether or not a given datetime is in a gap.

Response:
def datetime_exists(dt, tz=None):
    """
    Given a datetime and a time zone, determine whether or not a given datetime
    would fall in a gap.

    :param dt:
        A :class:`datetime.datetime` (whose time zone will be ignored if ``tz``
        is provided.)

    :param tz:
        A :class:`datetime.tzinfo` with support for the ``fold`` attribute. If
        ``None`` or not provided, the datetime's own time zone will be used.

    :return:
        Returns a boolean value whether or not the "wall time" exists in ``tz``.
    """
    if tz is None:
        if dt.tzinfo is None:
            raise ValueError('Datetime is naive and no time zone provided.')
        tz = dt.tzinfo

    dt = dt.replace(tzinfo=None)

    # This is essentially a test of whether or not the datetime can survive
    # a round trip to UTC.
    dt_rt = dt.replace(tzinfo=tz).astimezone(tzutc()).astimezone(tz)
    dt_rt = dt_rt.replace(tzinfo=None)

    return dt == dt_rt


Instruction:
Can you generate a brief explanation for the following Python 3 code
def datetime_ambiguous(dt, tz=None):
    if tz is None:
        if dt.tzinfo is None:
            raise ValueError('Datetime is naive and no time zone provided.')

        tz = dt.tzinfo

    # If a time zone defines its own "is_ambiguous" function, we'll use that.
    is_ambiguous_fn = getattr(tz, 'is_ambiguous', None)
    if is_ambiguous_fn is not None:
        try:
            return tz.is_ambiguous(dt)
        except:
            pass

    # If it doesn't come out and tell us it's ambiguous, we'll just check if
    # the fold attribute has any effect on this particular date and time.
    dt = dt.replace(tzinfo=tz)
    wall_0 = enfold(dt, fold=0)
    wall_1 = enfold(dt, fold=1)

    same_offset = wall_0.utcoffset() == wall_1.utcoffset()
    same_dst = wall_0.dst() == wall_1.dst()

    return not (same_offset and same_dst)

Response: Given a datetime and a time zone determine whether or not the wall time of the given datetime is ambiguous.


Instruction:
Can you tell what is the following Python 3 function doing
def is_ambiguous(self, dt):
        naive_dst = self._naive_is_dst(dt)
        return (not naive_dst and
                (naive_dst != self._naive_is_dst(dt - self._dst_saved)))

Response: Returns True if the wall time of a given datetime is ambiguous in thisCOOKIE zone.


Instruction:
Given the following Python 3 function, write the documentation
def _set_tzdata(self, tzobj):
        # Copy the relevant attributes over as private attributes
        for attr in _tzfile.attrs:
            setattr(self, '_' + attr, getattr(tzobj, attr))

Response: Set the time zone data of this object from a _tzfile object


Instruction:
Make a summary of the following Python 3 code
def fromutc(self, dt):
        # These isinstance checks are in datetime.tzinfo, so we'll preserve
        # them, even if we don't care about duck typing.
        if not isinstance(dt, datetime.datetime):
            raise TypeError("fromutc() requires a datetime argument")

        if dt.tzinfo is not self:
            raise ValueError("dt.tzinfo is not self")

        # First treat UTC as wall time and get the transition we're in.
        idx = self._find_last_transition(dt, in_utc=True)
        tti = self._get_ttinfo(idx)

        dt_out = dt + datetime.timedelta(seconds=tti.offset)

        fold = self.is_ambiguous(dt_out, idx=idx)

        return enfold(dt_out, fold=int(fold))

Response: Returns a new object representing the object in the time zone from the given datetime.


Instruction:
How would you explain what the following Python 3 function does
def is_ambiguous(self, dt, idx=None):
        if idx is None:
            idx = self._find_last_transition(dt)

        # Calculate the difference in offsets from current to previous
        timestamp = _datetime_to_timestamp(dt)
        tti = self._get_ttinfo(idx)

        if idx is None or idx <= 0:
            return False

        od = self._get_ttinfo(idx - 1).offset - tti.offset
        tt = self._trans_list[idx]          # Transition time

        return timestamp < tt + od

Response: Returns True if the wall time of a given datetime is ambiguous in thisCOOKIE zone.


Instruction:
Make a summary of the following Python 3 code
def transitions(self, year):
        if not self.hasdst:
            return None

        base_year = datetime.datetime(year, 1, 1)

        start = base_year + self._start_delta
        end = base_year + self._end_delta

        return (start, end)

Response:
Returns the DST on and off transition times expressed
        always on the standard time side.


Instruction:
Make a summary of the following Python 3 code
def get(self, tzid=None):
        if tzid is None:
            if len(self._vtz) == 0:
                raise ValueError("no timezones defined")
            elif len(self._vtz) > 1:
                raise ValueError("more than one timezone available")
            tzid = next(iter(self._vtz))

        return self._vtz.get(tzid)

Response: Retrieves a time zone object by its tzid.


Instruction:
Here you have a function in Python 3, explain what it does
def normalized(self):
        # Cascade remainders down (rounding each to roughly nearest
        # microsecond)
        days = int(self.days)

        hours_f = round(self.hours + 24 * (self.days - days), 11)
        hours = int(hours_f)

        minutes_f = round(self.minutes + 60 * (hours_f - hours), 10)
        minutes = int(minutes_f)

        seconds_f = round(self.seconds + 60 * (minutes_f - minutes), 8)
        seconds = int(seconds_f)

        microseconds = round(self.microseconds + 1e6 * (seconds_f - seconds))

        # Constructor carries overflow back up with call to _fix()
        return self.__class__(years=self.years, months=self.months,
                              days=days, hours=hours, minutes=minutes,
                              seconds=seconds, microseconds=microseconds,
                              leapdays=self.leapdays, year=self.year,
                              month=self.month, day=self.day,
                              weekday=self.weekday, hour=self.hour,
                              minute=self.minute, second=self.second,
                              microsecond=self.microsecond)

Response:
Return a version of this object represented entirely using integer
        values for the relative attributes.


Instruction:
Explain what the following Python 3 code does
def get_algorithm(alg: str) -> Callable:
    if alg not in algorithms:
        raise ValueError('Invalid algorithm: {:s}'.format(alg))
    return algorithms[alg]

Response: Returns a function that returns the requested algorithm.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _hash(secret: bytes, data: bytes, alg: str) -> bytes:
    algorithm = get_algorithm(alg)
    return hmac \
        .new(secret, msg=data, digestmod=algorithm) \
        .digest()

Response: Create a new HMAC hash.


Instruction:
Write a Python 3 function that can
encode the payload and header in a new token.

Response:
def encode(secret: Union[str, bytes], payload: dict = None,
           alg: str = default_alg, header: dict = None) -> str:
    """
    :param secret: The secret used to encode the token.
    :type secret: Union[str, bytes]
    :param payload: The payload to be encoded in the token.
    :type payload: dict
    :param alg: The algorithm used to hash the token.
    :type alg: str
    :param header: The header to be encoded in the token.
    :type header: dict
    :return: A new token
    :rtype: str
    """
    secret = util.to_bytes(secret)

    payload = payload or {}
    header = header or {}

    header_json = util.to_bytes(json.dumps(header))
    header_b64 = util.b64_encode(header_json)
    payload_json = util.to_bytes(json.dumps(payload))
    payload_b64 = util.b64_encode(payload_json)

    pre_signature = util.join(header_b64, payload_b64)
    signature = _hash(secret, pre_signature, alg)
    signature_b64 = util.b64_encode(signature)

    token = util.join(pre_signature, signature_b64)
    return util.from_bytes(token)


Instruction:
Implement a Python 3 function for
decoding the given token and validates the signature.

Response:
def decode(secret: Union[str, bytes], token: Union[str, bytes],
           alg: str = default_alg) -> Tuple[dict, dict]:
    """
    Decodes the given token's header and payload and validates the signature.

    :param secret: The secret used to decode the token. Must match the
        secret used when creating the token.
    :type secret: Union[str, bytes]
    :param token: The token to decode.
    :type token: Union[str, bytes]
    :param alg: The algorithm used to decode the token. Must match the
        algorithm used when creating the token.
    :type alg: str
    :return: The decoded header and payload.
    :rtype: Tuple[dict, dict]
    """
    secret = util.to_bytes(secret)
    token = util.to_bytes(token)
    pre_signature, signature_segment = token.rsplit(b'.', 1)
    header_b64, payload_b64 = pre_signature.split(b'.')
    try:
        header_json = util.b64_decode(header_b64)
        header = json.loads(util.from_bytes(header_json))
    except (json.decoder.JSONDecodeError, UnicodeDecodeError, ValueError):
        raise InvalidHeaderError('Invalid header')
    try:
        payload_json = util.b64_decode(payload_b64)
        payload = json.loads(util.from_bytes(payload_json))
    except (json.decoder.JSONDecodeError, UnicodeDecodeError, ValueError):
        raise InvalidPayloadError('Invalid payload')

    if not isinstance(header, dict):
        raise InvalidHeaderError('Invalid header: {}'.format(header))
    if not isinstance(payload, dict):
        raise InvalidPayloadError('Invalid payload: {}'.format(payload))

    signature = util.b64_decode(signature_segment)
    calculated_signature = _hash(secret, pre_signature, alg)

    if not compare_signature(signature, calculated_signature):
        raise InvalidSignatureError('Invalid signature')
    return header, payload


Instruction:
Can you implement a function in Python 3 that
compares the given signature and returns True if the signature matches.

Response:
def compare_signature(expected: Union[str, bytes],
                      actual: Union[str, bytes]) -> bool:
    """
    Compares the given signatures.

    :param expected: The expected signature.
    :type expected: Union[str, bytes]
    :param actual: The actual signature.
    :type actual: Union[str, bytes]
    :return: Do the signatures match?
    :rtype: bool
    """
    expected = util.to_bytes(expected)
    actual = util.to_bytes(actual)
    return hmac.compare_digest(expected, actual)


Instruction:
Implement a function in Python 3 to
compare the given tokens. :param expected: The expected token. :type expected: Union[str, bytes] :param actual: The actual token. :type actual: Union[str, bytes] :return: Do the tokens match? :rtype: bool

Response:
def compare_token(expected: Union[str, bytes],
                  actual: Union[str, bytes]) -> bool:
    """
    Compares the given tokens.

    :param expected: The expected token.
    :type expected: Union[str, bytes]
    :param actual: The actual token.
    :type actual: Union[str, bytes]
    :return: Do the tokens match?
    :rtype: bool
    """
    expected = util.to_bytes(expected)
    actual = util.to_bytes(actual)
    _, expected_sig_seg = expected.rsplit(b'.', 1)
    _, actual_sig_seg = actual.rsplit(b'.', 1)
    expected_sig = util.b64_decode(expected_sig_seg)
    actual_sig = util.b64_decode(actual_sig_seg)
    return compare_signature(expected_sig, actual_sig)


Instruction:
Create a Python 3 function to
return the token header.

Response:
def header(self) -> dict:
        """
        :return: Token header.
        :rtype: dict
        """
        header = {}
        if isinstance(self._header, dict):
            header = self._header.copy()
            header.update(self._header)
        header.update({
            'type': 'JWT',
            'alg': self.alg
        })
        return header


Instruction:
Implement a function in Python 3 to
return True if the token is valid.

Response:
def valid(self, time: int = None) -> bool:
        """
        Is the token valid? This method only checks the timestamps within the
        token and compares them against the current time if none is provided.

        :param time: The timestamp to validate against
        :type time: Union[int, None]
        :return: The validity of the token.
        :rtype: bool
        """
        if time is None:
            epoch = datetime(1970, 1, 1, 0, 0, 0)
            now = datetime.utcnow()
            time = int((now - epoch).total_seconds())
        if isinstance(self.valid_from, int) and time < self.valid_from:
            return False
        if isinstance(self.valid_to, int) and time > self.valid_to:
            return False
        return True


Instruction:
How would you explain what the following Python 3 function does
def _pop_claims_from_payload(self):
        claims_in_payload = [k for k in self.payload.keys() if
                             k in registered_claims.values()]
        for name in claims_in_payload:
            self.registered_claims[name] = self.payload.pop(name)

Response: Pop claims from the payload and update the self. registered_claims property.


Instruction:
Make a summary of the following Python 3 code
def encode(self) -> str:
        payload = {}
        payload.update(self.registered_claims)
        payload.update(self.payload)
        return encode(self.secret, payload, self.alg, self.header)

Response: Encode the data held in the class into a token.


Instruction:
Can you tell what is the following Python 3 function doing
def decode(secret: Union[str, bytes], token: Union[str, bytes],
               alg: str = default_alg) -> 'Jwt':
        """
        Decodes the given token into an instance of `Jwt`.

        :param secret: The secret used to decode the token. Must match the
            secret used when creating the token.
        :type secret: Union[str, bytes]
        :param token: The token to decode.
        :type token: Union[str, bytes]
        :param alg: The algorithm used to decode the token. Must match the
            algorithm used when creating the token.
        :type alg: str
        :return: The decoded token.
        :rtype: `Jwt`
        """
        header, payload = decode(secret, token, alg)
        return Jwt(secret, payload, alg, header)

Response: Decodes the given token into an instance of Jwt.


Instruction:
Write a Python 3 function that can
compare this Jwt with another Jwt.

Response:
def compare(self, jwt: 'Jwt', compare_dates: bool = False) -> bool:
        """
        Compare against another `Jwt`.

        :param jwt: The token to compare against.
        :type jwt: Jwt
        :param compare_dates: Should the comparision take dates into account?
        :type compare_dates: bool
        :return: Are the two Jwt's the same?
        :rtype: bool
        """
        if self.secret != jwt.secret:
            return False
        if self.payload != jwt.payload:
            return False
        if self.alg != jwt.alg:
            return False
        if self.header != jwt.header:
            return False
        expected_claims = self.registered_claims
        actual_claims = jwt.registered_claims
        if not compare_dates:
            strip = ['exp', 'nbf', 'iat']
            expected_claims = {k: {v if k not in strip else None} for k, v in
                               expected_claims.items()}
            actual_claims = {k: {v if k not in strip else None} for k, v in
                             actual_claims.items()}
        if expected_claims != actual_claims:
            return False
        return True


Instruction:
Create a Python 3 function to
base 64 encode data.

Response:
def b64_encode(data: bytes) -> bytes:
    """
    :param data: Data the encode.
    :type data: bytes
    :return: Base 64 encoded data with padding removed.
    :rtype: bytes
    """
    encoded = urlsafe_b64encode(data)
    return encoded.replace(b'=', b'')


Instruction:
Can you tell what is the following Python 3 function doing
def b64_decode(data: bytes) -> bytes:
    missing_padding = len(data) % 4
    if missing_padding != 0:
        data += b'=' * (4 - missing_padding)
    return urlsafe_b64decode(data)

Response: Decode base 64 encoded data.


Instruction:
Write a Python 3 function that can
convert data to bytes.

Response:
def to_bytes(data: Union[str, bytes]) -> bytes:
    """
    :param data: Data to convert to bytes.
    :type data: Union[str, bytes]
    :return: `data` encoded to UTF8.
    :rtype: bytes
    """
    if isinstance(data, bytes):
        return data
    return data.encode('utf-8')


Instruction:
Write a Python 3 script for
decoding a UTF8 byte string into a base64 - encoded version of the object.

Response:
def from_bytes(data: Union[str, bytes]) -> str:
    """
    :param data: A UTF8 byte string.
    :type data: Union[str, bytes]
    :return: `data` decoded from UTF8.
    :rtype: str
    """
    if isinstance(data, str):
        return data
    return str(data, 'utf-8')


Instruction:
Can you generate the documentation for the following Python 3 function
def camelize_classname(base, tablename, table):
    "Produce a 'camelized' class name, e.g. "
    "'words_and_underscores' -> 'WordsAndUnderscores'"
    return str(tablename[0].upper() +
               re.sub(r'_([a-z])', lambda m: m.group(1).upper(),
                      tablename[1:]))

Response: Produce a camelized class name e. g. words_and_underscores -> words_and_underscores


Instruction:
Write a Python 3 script to
produce an uncamelized pluralized class name e. g.

Response:
def pluralize_collection(base, local_cls, referred_cls, constraint):
    "Produce an 'uncamelized', 'pluralized' class name, e.g. "
    "'SomeTerm' -> 'some_terms'"
    referred_name = referred_cls.__name__
    uncamelized = re.sub(r'[A-Z]',
                         lambda m: "_%s" % m.group(0).lower(),
                         referred_name)[1:]
    pluralized = _pluralizer.plural(uncamelized)
    return pluralized


Instruction:
Here you have a function in Python 3, explain what it does
def is_compressed_json_file(abspath):
    abspath = abspath.lower()
    fname, ext = os.path.splitext(abspath)
    if ext in [".json", ".js"]:
        is_compressed = False
    elif ext == ".gz":
        is_compressed = True
    else:
        raise ValueError(
            "'%s' is not a valid json file. "
            "extension has to be '.json' or '.js' for uncompressed, '.gz' "
            "for compressed." % abspath)
    return is_compressed

Response: Test a file is a valid json file.


Instruction:
Can you implement a function in Python 3 that
dumps a set into a dictionary.

Response:
def dump_set(self, obj, class_name=set_class_name):
        """
        ``set`` dumper.
        """
        return {"$" + class_name: [self._json_convert(item) for item in obj]}


Instruction:
Can you generate a brief explanation for the following Python 3 code
def dump_deque(self, obj, class_name="collections.deque"):
        return {"$" + class_name: [self._json_convert(item) for item in obj]}

Response: Dump a deque into a JSON - compatible dict.


Instruction:
Create a Python 3 function for
dumping an OrderedDict into a dictionary.

Response:
def dump_OrderedDict(self, obj, class_name="collections.OrderedDict"):
        """
        ``collections.OrderedDict`` dumper.
        """
        return {
            "$" + class_name: [
                (key, self._json_convert(value)) for key, value in iteritems(obj)
            ]
        }


Instruction:
Can you generate a brief explanation for the following Python 3 code
def dump_nparray(self, obj, class_name=numpy_ndarray_class_name):
        return {"$" + class_name: self._json_convert(obj.tolist())}

Response: Dump a numpy. ndarray into a dictionary.


Instruction:
Here you have a function in Python 3, explain what it does
def _invalidates_cache(f):

    def inner_func(self, *args, **kwargs):
        rv = f(self, *args, **kwargs)
        self._invalidate_cache()
        return rv

    return inner_func

Response: Decorator for rruleset methods which may invalidate the cached length.


Instruction:
How would you explain what the following Python 3 function does
def before(self, dt, inc=False):
        if self._cache_complete:
            gen = self._cache
        else:
            gen = self
        last = None
        if inc:
            for i in gen:
                if i > dt:
                    break
                last = i
        else:
            for i in gen:
                if i >= dt:
                    break
                last = i
        return last

Response: Returns the last recurrence before the given datetime instance.


Instruction:
Can you generate the documentation for the following Python 3 function
def after(self, dt, inc=False):
        if self._cache_complete:
            gen = self._cache
        else:
            gen = self
        if inc:
            for i in gen:
                if i >= dt:
                    return i
        else:
            for i in gen:
                if i > dt:
                    return i
        return None

Response: Returns the first recurrence after the given datetime instance.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def xafter(self, dt, count=None, inc=False):

        if self._cache_complete:
            gen = self._cache
        else:
            gen = self

        # Select the comparison function
        if inc:
            def comp(dc, dtc): return dc >= dtc
        else:
            def comp(dc, dtc): return dc > dtc

        # Generate dates
        n = 0
        for d in gen:
            if comp(d, dt):
                if count is not None:
                    n += 1
                    if n > count:
                        break

                yield d

Response: Generator which yields up to count recurrences after the given datetime instance.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def replace(self, **kwargs):
        new_kwargs = {"interval": self._interval,
                      "count": self._count,
                      "dtstart": self._dtstart,
                      "freq": self._freq,
                      "until": self._until,
                      "wkst": self._wkst,
                      "cache": False if self._cache is None else True}
        new_kwargs.update(self._original_rule)
        new_kwargs.update(kwargs)
        return rrule(**new_kwargs)

Response:
Return a new rrule with same attributes except for those attributes given new
           values by whichever keyword arguments are specified.


Instruction:
Can you implement a function in Python 3 that
constructs a set of all possible values for a given frequency range.

Response:
def __construct_byset(self, start, byxxx, base):
        """
        If a `BYXXX` sequence is passed to the constructor at the same level as
        `FREQ` (e.g. `FREQ=HOURLY,BYHOUR={2,4,7},INTERVAL=3`), there are some
        specifications which cannot be reached given some starting conditions.

        This occurs whenever the interval is not coprime with the base of a
        given unit and the difference between the starting position and the
        ending position is not coprime with the greatest common denominator
        between the interval and the base. For example, with a FREQ of hourly
        starting at 17:00 and an interval of 4, the only valid values for
        BYHOUR would be {21, 1, 5, 9, 13, 17}, because 4 and 24 are not
        coprime.

        :param start:
            Specifies the starting position.
        :param byxxx:
            An iterable containing the list of allowed values.
        :param base:
            The largest allowable value for the specified frequency (e.g.
            24 hours, 60 minutes).

        This does not preserve the type of the iterable, returning a set, since
        the values should be unique and the order is irrelevant, this will
        speed up later lookups.

        In the event of an empty set, raises a :exception:`ValueError`, as this
        results in an empty rrule.
        """

        cset = set()

        # Support a single byxxx value.
        if isinstance(byxxx, integer_types):
            byxxx = (byxxx, )

        for num in byxxx:
            i_gcd = gcd(self._interval, base)
            # Use divmod rather than % because we need to wrap negative nums.
            if i_gcd == 1 or divmod(num - start, i_gcd)[1] == 0:
                cset.add(num)

        if len(cset) == 0:
            raise ValueError("Invalid rrule byxxx generates an empty set.")

        return cset


Instruction:
How would you explain what the following Python 3 function does
def __mod_distance(self, value, byxxx, base):
        accumulator = 0
        for ii in range(1, base + 1):
            # Using divmod() over % to account for negative intervals
            div, value = divmod(value + self._interval, base)
            accumulator += div
            if value in byxxx:
                return (accumulator, value)

Response: Calculates the distance between the value and the next specified value in byxxx.


Instruction:
Can you generate the documentation for the following Python 3 function
def _handle_BYWEEKDAY(self, rrkwargs, name, value, **kwargs):
        l = []
        for wday in value.split(','):
            if '(' in wday:
                # If it's of the form TH(+1), etc.
                splt = wday.split('(')
                w = splt[0]
                n = int(splt[1][:-1])
            elif len(wday):
                # If it's of the form +1MO
                for i in range(len(wday)):
                    if wday[i] not in '+-0123456789':
                        break
                n = wday[:i] or None
                w = wday[i:]
                if n:
                    n = int(n)
            else:
                raise ValueError("Invalid (empty) BYDAY specification.")

            l.append(weekdays[self._weekday_map[w]](n))
        rrkwargs["byweekday"] = l

Response: Handle BYDAY keyword arguments.


Instruction:
Implement a Python 3 function for
calculating what percentage a given number is of another, e.g. 50 is 50% of 100.

Response:
def run_get_percentage():
    """
    Calculate what percentage a given number is of another,
    e.g. 50 is 50% of 100.
    """
    description = run_get_percentage.__doc__
    parser = argparse.ArgumentParser(
        prog='get_percentage',
        description=description,
        epilog="Example use: get_percentage 25 100",
    )
    parser.add_argument(
        'a', help='Integer or floating point number that is a percent of another number'
    )
    parser.add_argument(
        'b',
        help='Integer or floating point number of which the first number is a percent',
    )
    args = parser.parse_args()
    print(sm.get_percentage(float(args.a), float(args.b)))


Instruction:
Create a Python 3 function to
run the excel_to_html function from the command - line.

Response:
def run_excel_to_html():
    """
    Run the excel_to_html function from the
    command-line.

    Args:
        -p path to file
        -s name of the sheet to convert
        -css classes to apply
        -m attempt to combine merged cells
        -c caption for accessibility
        -su summary for accessibility
        -d details for accessibility

    Example use:

        excel_to_html -p myfile.xlsx -s SheetName -css diablo-python -m true
    """
    # Capture commandline arguments. prog='' argument must
    # match the command name in setup.py entry_points
    parser = argparse.ArgumentParser(prog='excel_to_html')
    parser.add_argument('-p', nargs='?', help='Path to an excel file for conversion.')
    parser.add_argument(
        '-s',
        nargs='?',
        help='The name of a sheet in our excel file. Defaults to "Sheet1".',
    )
    parser.add_argument(
        '-css', nargs='?', help='Space separated css classes to append to the table.'
    )
    parser.add_argument(
        '-m', action='store_true', help='Merge, attempt to combine merged cells.'
    )
    parser.add_argument(
        '-c', nargs='?', help='Caption for creating an accessible table.'
    )
    parser.add_argument(
        '-d',
        nargs='?',
        help='Two strings separated by a | character. The first string \
        is for the html "summary" attribute and the second string is for the html "details" attribute. \
        both values must be provided and nothing more.',
    )
    parser.add_argument(
        '-r', action='store_true', help='Row headers. Does the table have row headers?'
    )

    args = parser.parse_args()
    inputs = {
        'p': args.p,
        's': args.s,
        'css': args.css,
        'm': args.m,
        'c': args.c,
        'd': args.d,
        'r': args.r,
    }

    p = inputs['p']
    s = inputs['s'] if inputs['s'] else 'Sheet1'
    css = inputs['css'] if inputs['css'] else ''
    m = inputs['m'] if inputs['m'] else False
    c = inputs['c'] if inputs['c'] else ''
    d = inputs['d'].split('|') if inputs['d'] else []
    r = inputs['r'] if inputs['r'] else False

    html = fp.excel_to_html(
        p, sheetname=s, css_classes=css, caption=c, details=d, row_headers=r, merge=m
    )

    print(html)


Instruction:
Implement a Python 3 function for
getting the return string for a language that s supported by python.

Response:
def get_built_in(self, language, level, data):
        """
        Gets the return string for a language that's supported by python.
        Used in cases when python provides support for the conversion.

        Args:
            language: string the langage to return for.

            level: integer, the indentation level.

            data: python data structure being converted (list of tuples)

        Returns:
            None, updates self.data_structure
        """
        # Language is python
        pp = pprint.PrettyPrinter(indent=level)

        lookup = {'python' : pp.pformat(data),
                  'json' : str(json.dumps(data, sort_keys=True, indent=level, separators=(',', ': ')))}

        self.data_structure = lookup[language]


Instruction:
Can you implement a function in Python 3 that
returns the inner template for the given language and type.

Response:
def get_inner_template(self, language, template_type, indentation, key, val):
        """
        Gets the requested template for the given language.

        Args:
            language: string, the language of the template to look for.

            template_type: string, 'iterable' or 'singular'.
            An iterable template is needed when the value is an iterable
            and needs more unpacking, e.g. list, tuple. A singular template
            is needed when unpacking is complete and the value is singular,
            e.g. string, int, float.

            indentation: int, the indentation level.

            key: multiple types, the array key.

            val: multiple types, the array values

        Returns:
            string, template formatting for arrays by language.
        """
        #Language specific inner templates
        inner_templates = {'php' : {
                                'iterable' : '%s%s => array \n%s( \n%s%s),\n' % (indentation, key, indentation, val, indentation),
                                'singular' : '%s%s => %s, \n' % (indentation, key, val) },
                           'javascript' : {
                                'iterable' : '%s%s : {\n%s\n%s},\n' % (indentation, key, val, indentation),
                                'singular' : '%s%s: %s,\n' % (indentation, key, val)},
                           'ocaml' : {
                                'iterable' : '%s[| (%s, (\n%s\n%s))|] ;;\n' % (indentation, key, val, indentation),
                                'singular' : '%s(%s, %s);\n' % (indentation, key, val)}}

        return inner_templates[language][template_type]


Instruction:
Write a Python 3 function for
translating a string representing a php array into a data structure in the specified language.

Response:
def translate_array(self, string, language, level=3, retdata=False):
        """Unserializes a serialized php array and prints it to
        the console as a data structure in the specified language.
        Used to translate or convert a php array into a data structure
        in another language. Currently supports, PHP, Python, Javascript,
        and JSON.

        Args:
            string: a string of serialized php

            language: a string representing the desired output
            format for the array.

            level: integer, indentation level in spaces.
            Defaults to 3.

            retdata: boolean, the method will return the string
            in addition to printing it if set to True. Defaults
            to false.

        Returns:
            None but prints a string to the console if retdata is
            False, otherwise returns a string.
            """
        language = language.lower()
        assert self.is_built_in(language) or language in self.outer_templates, \
            "Sorry, " + language + " is not a supported language."

        # Serialized data converted to a python data structure (list of tuples)
        data = phpserialize.loads(bytes(string, 'utf-8'), array_hook=list, decode_strings=True)

        # If language conversion is supported by python avoid recursion entirely
        # and use a built in library
        if self.is_built_in(language):
            self.get_built_in(language, level, data)
            print(self)
            return self.data_structure if retdata else None

        # The language is not supported. Use recursion to build a data structure.
        def loop_print(iterable, level=3):
            """
            Loops over a python representation of a php array
            (list of tuples) and constructs a representation in another language.
            Translates a php array into another structure.

            Args:
                iterable: list or tuple to unpack.

                level: integer, number of spaces to use for indentation
            """
            retval = ''
            indentation = ' ' * level

            # Base case - variable is not an iterable
            if not self.is_iterable(iterable) or isinstance(iterable, str):
                non_iterable = str(iterable)
                return str(non_iterable)

            # Recursive case
            for item in iterable:
                # If item is a tuple it should be a key, value pair
                if isinstance(item, tuple) and len(item) == 2:
                    # Get the key value pair
                    key = item[0]
                    val = loop_print(item[1], level=level+3)

                    # Translate special values
                    val = self.translate_val(language, val) if language in self.lang_specific_values \
                          and val in self.lang_specific_values[language] else val

                    # Convert keys to their properly formatted strings
                    # Integers are not quoted as array keys
                    key = str(key) if isinstance(key, int) else '\'' + str(key) + '\''

                    # The first item is a key and the second item is an iterable, boolean
                    needs_unpacking = hasattr(item[0],'__iter__') == False \
                                      and hasattr(item[1],'__iter__') == True

                    # The second item is an iterable
                    if needs_unpacking:
                        retval += self.get_inner_template(language, 'iterable', indentation, key, val)
                    # The second item is not an iterable
                    else:
                        # Convert values to their properly formatted strings
                        # Integers and booleans are not quoted as array values
                        val = str(val) if val.isdigit() or val in self.lang_specific_values[language].values() else '\'' + str(val) + '\''

                        retval += self.get_inner_template(language, 'singular', indentation, key, val)

            return retval

        # Execute the recursive call in language specific wrapper template
        self.data_structure = self.outer_templates[language] % (loop_print(data))
        print(self)
        return self.data_structure if retdata else None


Instruction:
Can you tell what is the following Python 3 function doing
def _validate(config):
    for mandatory_key in _mandatory_keys:
        if mandatory_key not in config:
            raise KeyError(mandatory_key)
    for key in config.keys():
        if key not in _mandatory_keys and key not in _optional_keys:
            raise SyntaxError(key)
        if not isinstance(config[key], _default_config[key].__class__):
            raise ValueError(key)

Response: Validate the config dictionary for the key sequence.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _create_default_config():
    config_path = _get_config_path()
    with open(config_path, 'w+') as f:
        yaml.dump(_default_config, f, default_flow_style=False)

Response: Writes the default configuration to the appropriate place.


Instruction:
Can you generate the documentation for the following Python 3 function
def reusable(func):
    sig = signature(func)
    origin = func
    while hasattr(origin, '__wrapped__'):
        origin = origin.__wrapped__
    return type(
        origin.__name__,
        (ReusableGenerator, ),
        dict([
            ('__doc__',       origin.__doc__),
            ('__module__',    origin.__module__),
            ('__signature__', sig),
            ('__wrapped__',   staticmethod(func)),
        ] + [
            (name, property(compose(itemgetter(name),
                                    attrgetter('_bound_args.arguments'))))
            for name in sig.parameters
        ] + ([
            ('__qualname__',  origin.__qualname__),
        ] if sys.version_info > (3, ) else [])))

Response: Create a reusable class from a generator function.


Instruction:
Implement a Python 3 function for
sending an item into a generator expecting a final return value

Response:
def sendreturn(gen, value):
    """Send an item into a generator expecting a final return value

    Parameters
    ----------
    gen: ~typing.Generator[T_yield, T_send, T_return]
        the generator to send the value to
    value: T_send
        the value to send

    Raises
    ------
    RuntimeError
        if the generator did not return as expected

    Returns
    -------
    T_return
        the generator's return value
    """
    try:
        gen.send(value)
    except StopIteration as e:
        return stopiter_value(e)
    else:
        raise RuntimeError('generator did not return as expected')


Instruction:
Here you have a function in Python 3, explain what it does
def imap_send(func, gen):
    gen = iter(gen)
    assert _is_just_started(gen)
    yielder = yield_from(gen)
    for item in yielder:
        with yielder:
            yielder.send(func((yield item)))
    return_(yielder.result)

Response: Apply a function to all send values of a generator.


Instruction:
Write a Python 3 function for
creating a new generator by relaying yield and send interactions through another generator.

Response:
def irelay(gen, thru):
    """Create a new generator by relaying yield/send interactions
    through another generator

    Parameters
    ----------
    gen: Generable[T_yield, T_send, T_return]
        the original generator
    thru: ~typing.Callable[[T_yield], ~typing.Generator]
        the generator callable through which each interaction is relayed

    Returns
    -------
    ~typing.Generator
        the relayed generator
    """
    gen = iter(gen)
    assert _is_just_started(gen)

    yielder = yield_from(gen)
    for item in yielder:
        with yielder:

            subgen = thru(item)
            subyielder = yield_from(subgen)
            for subitem in subyielder:
                with subyielder:
                    subyielder.send((yield subitem))

            yielder.send(subyielder.result)

    return_(yielder.result)


Instruction:
How would you code a function in Python 3 to
check if all command dependencies refers to and existing command. If not raises an error. ProjectfileError will be raised with the problematic dependency and it s command.

Response:
def _data_integrity_check(data):
    """Checks if all command dependencies refers to and existing command. If not, a ProjectfileError
    will be raised with the problematic dependency and it's command.

    :param data: parsed raw data set.
    :return: None
    """
    deps = []
    for command in data['commands']:
        if 'dependencies' in data['commands'][command]:
            for d in data['commands'][command]['dependencies']:
                deps.append({
                    'd': d,
                    'c': command
                })
    for d in deps:
        if d['d'] not in data['commands']:
            raise error.ProjectfileError({
                'error': error.PROJECTFILE_INVALID_DEPENDENCY.format(d['d'], d['c'])
            })


Instruction:
Implement a function in Python 3 to
populate any database related fields that have _getters to populate them with

Response:
def _link_rels(obj, fields=None, save=False, overwrite=False):
    """Populate any database related fields (ForeignKeyField, OneToOneField) that have `_get`ters to populate them with"""
    if not fields:
        meta = obj._meta
        fields = [f.name for f in meta.fields if hasattr(f, 'do_related_class') and not f.primary_key and hasattr(meta, '_get_' + f.name) and hasattr(meta, '_' + f.name)]
    for field in fields:
        # skip fields if they contain non-null data and `overwrite` option wasn't set
        if not overwrite and not isinstance(getattr(obj, field, None), NoneType):
            # print 'skipping %s which already has a value of %s' % (field, getattr(obj, field, None))
            continue
        if hasattr(obj, field):
            setattr(obj, field, getattr(obj, '_' + field, None))
    if save:
        obj.save()
    return obj


Instruction:
Can you write a function in Python 3 where it
updates any database fields that have _getters to populate them with

Response:
def _update(obj, fields=None, save=False, overwrite=False):
    """Update/populate any database fields that have `_get`ters to populate them with, regardless of whether they are data fields or related fields"""
    if not fields:
        meta = obj._meta
        fields = [f.name for f in meta.fields if not f.primary_key and hasattr(meta, '_get_' + f.name) and hasattr(meta, '_' + f.name)]
    # print fields
    fields_updated = []
    for field in fields:
        # skip fields if they contain non-null data and `overwrite` option wasn't set
        if not overwrite and not getattr(obj, field, None) == None:
            # print 'skipping %s which already has a value of %s' % (field, getattr(obj, field, None))
            continue
        # print field
        if hasattr(obj, field):
            # print field, getattr(obj, '_' + field, None)
            setattr(obj, field, getattr(obj, '_' + field, None))
            if getattr(obj, field, None) != None:
                fields_updated += [field]
    if save:
        obj.save()
    return fields_updated


Instruction:
Write a Python 3 function that can
print the traceback and invokes the ipython debugger on any exception in the order they were passed.

Response:
def bug_info(exc_type, exc_value, exc_trace):
    """Prints the traceback and invokes the ipython debugger on any exception

    Only invokes ipydb if you are outside ipython or python interactive session.
    So scripts must be called from OS shell in order for exceptions to ipy-shell-out.

    Dependencies:
      Needs `pip install ipdb`

    Arguments:
      exc_type (type): The exception type/class (e.g. RuntimeError)
      exc_value (Exception): The exception instance (e.g. the error message passed to the Exception constructor)
      exc_trace (Traceback): The traceback instance

    References:
      http://stackoverflow.com/a/242531/623735

    Example Usage:
      $  python -c 'from pug import debug;x=[];x[0]'
      Traceback (most recent call last):
        File "<string>", line 1, in <module>
      IndexError: list index out of range

      > <string>(1)<module>()

      ipdb> x
      []
      ipdb> locals()
      {'__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 'x': [], 'debug': <module 'pug.debug' from 'pug/debug.py'>, '__name__': '__main__', '__doc__': None}
      ipdb>
    """
    if hasattr(sys, 'ps1') or not sys.stderr.isatty():
        # We are in interactive mode or don't have a tty-like device, so we call the default hook
        sys.__excepthook__(exc_type, exc_value, exc_trace)
    else:
        # Need to import non-built-ins here, so if dependencies haven't been installed, both tracebacks will print
        # (e.g. the ImportError and the Exception that got you here)
        import ipdb
        # We are NOT in interactive mode, print the exception
        traceback.print_exception(exc_type, exc_value, exc_trace)
        print
        # Start the debugger in post-mortem mode.
        ipdb.post_mortem(exc_trace)


Instruction:
Implement a Python 3 function for
copying a file from its location on the web to a designated place on the local machine.

Response:
def copy_web_file_to_local(file_path, target_path):
    """Copies a file from its location on the web to a designated
    place on the local machine.

    Args:
        file_path: Complete url of the file to copy, string (e.g. http://fool.com/input.css).

        target_path: Path and name of file on the local machine, string. (e.g. /directory/output.css)

    Returns:
        None.

    """
    response = urllib.request.urlopen(file_path)
    f = open(target_path, 'w')
    f.write(response.read())
    f.close()


Instruction:
Can you generate the documentation for the following Python 3 function
def get_line_count(fname):
    i = 0
    with open(fname) as f:
        for i, l in enumerate(f):
            pass
    return i + 1

Response: Counts the number of lines in a file.


Instruction:
Can you generate the documentation for the following Python 3 function
def indent_css(f, output):
    line_count = get_line_count(f)
    f = open(f, 'r+')
    output = open(output, 'r+')
    for line in range(line_count):
        string = f.readline().rstrip()
        if len(string) > 0:
            if string[-1] == ";":
                output.write("    " + string + "\n")
            else:
                output.write(string + "\n")
    output.close()
    f.close()

Response: Indentes css that has not been indented and saves it to a new file.


Instruction:
Can you tell what is the following Python 3 function doing
def add_newlines(f, output, char):
    line_count = get_line_count(f)
    f = open(f, 'r+')
    output = open(output, 'r+')
    for line in range(line_count):
        string = f.readline()
        string = re.sub(char, char + '\n', string)
        output.write(string)

Response: Adds line breaks after every occurance of a given character in a file.


Instruction:
Can you write a function in Python 3 where it
adds a space before a character.

Response:
def add_whitespace_before(char, input_file, output_file):
    """Adds a space before a character if there's isn't one already.

    Args:
        char: string, character that needs a space before it.

        input_file: string, path to file to parse.

        output_file: string, path to destination file.

    Returns:
        None.
    """
    line_count = get_line_count(input_file)
    input_file = open(input_file, 'r')
    output_file = open(output_file, 'r+')
    for line in range(line_count):
        string = input_file.readline()
        # If there's not already a space before the character, add one
        if re.search(r'[a-zA-Z0-9]' + char, string) != None:
            string = re.sub(char, ' ' + char, string)
        output_file.write(string)
    input_file.close()


Instruction:
Can you write a function in Python 3 where it
reformats poorly written css.

Response:
def reformat_css(input_file, output_file):
    """Reformats poorly written css. This function does not validate or fix errors in the code.
    It only gives code the proper indentation.

    Args:
        input_file: string, path to the input file.

        output_file: string, path to where the reformatted css should be saved. If the target file
        doesn't exist, a new file is created.

    Returns:
        None.
    """
    # Number of lines in the file.
    line_count = get_line_count(input_file)

    # Open source and target files.
    f = open(input_file, 'r+')
    output = open(output_file, 'w')

    # Loop over every line in the file.
    for line in range(line_count):
        # Eliminate whitespace at the beginning and end of lines.
        string = f.readline().strip()
        # New lines after {
        string = re.sub('\{', '{\n', string)
        # New lines after ;
        string = re.sub('; ', ';', string)
        string = re.sub(';', ';\n', string)
        # Eliminate whitespace before comments
        string = re.sub('} /*', '}/*', string)
        # New lines after }
        string = re.sub('\}', '}\n', string)
        # New lines at the end of comments
        string = re.sub('\*/', '*/\n', string)
        # Write to the output file.
        output.write(string)

    # Close the files.
    output.close()
    f.close()

    # Indent the css.
    indent_css(output_file, output_file)

    # Make sure there's a space before every {
    add_whitespace_before("{", output_file, output_file)


Instruction:
Can you tell what is the following Python 3 function doing
def is_int(string):
    try:
        a = float(string)
        b = int(a)
    except ValueError:
        return False
    else:
        return a == b

Response: Checks if a string is an integer.


Instruction:
Here you have a function in Python 3, explain what it does
def total_hours(input_files):
    hours = 0
    # Look for singular and plural forms of the word
    # and allow typos.
    allow = set(['hours', 'hour', 'huors', 'huor'])
    for input_file in input_files:
        doc = open(input_file, 'r')
        for line in doc:
            line = line.rstrip()
            data = line.split(' ')
            if (len(data) == 2) and (is_numeric(data[0])) and (data[1].lower() in allow):
                hours += float(data[0])
        doc.close()
    return hours

Response: Returns the total number of hours spent on a given project.


Instruction:
Make a summary of the following Python 3 code
def clean_strings(iterable):
    retval = []
    for val in iterable:
        try:
            retval.append(val.strip())
        except(AttributeError):
            retval.append(val)
    return retval

Response:
Takes a list of strings and clear whitespace
    on each one.


Instruction:
Implement a function in Python 3 to
convert an Excel spreadsheet to an html table.

Response:
def excel_to_html(path, sheetname='Sheet1', css_classes='', \
    caption='', details=[], row_headers=False, merge=False):
    """
    Convert an excel spreadsheet to an html table.
    This function supports the conversion of merged
    cells. It can be used in code or run from the
    command-line. If passed the correct arguments
    it can generate fully accessible html.

    Args:
        path: string, path to the spreadsheet.

        sheetname: string, name of the sheet
        to convert.

        css_classes: string, space separated
        classnames to append to the table.

        caption: string, a short heading-like
        description of the table.

        details: list of strings, where the first
        item in the list is a string for the html
        summary element and the second item is
        a string for the details element. The
        summary should be very short, e.g. "Help",
        where as the details element should be a
        long description regarding the purpose or
        how to navigate the table.

        row_headers: boolean, defaults to False.
        Does the table have row headers? If set
        to True, the first element in each row
        will be a  element
        instead of a  element.

        merge: boolean, whether or not to
        combine cells that were merged in the
        spreadsheet.

    Returns:
        string, html table
    """

    def get_data_on_merged_cells():
        """
        Build a datastructure with data
        on merged cells.
        """
        # Use this to build support for merged columns and rows????
        merged_cells = xls.book.sheet_by_name(sheetname).merged_cells
        ds = {}
        for crange in merged_cells:
            rlo, rhi, clo, chi = crange
            for rowx in range(rlo, rhi):
                for colx in range(clo, chi):
                    # Cell (rlo, clo) (the top left one) will carry the data and
                    # formatting info. The remainder will be recorded as blank cells,
                    # but a renderer will apply the formatting info for the top left
                    # cell (e.g. border, pattern) to all cells in the range.
                    #print(str(rlo) + ' ' + str(clo))
                    #print(str(rowx) + ' ' + str(colx))
                    parent_cell = (rlo,clo)
                    child_cell = (rowx,colx)
                    if not parent_cell in ds:
                        # Return data structure is a dictionary with numeric tuples
                        # as keys. Each tuple holds the x, y coordinates of the cell.
                        # The dictionary holds two values:
                        # 1. A list with two numbers which represent the x/y count
                        #    starting at 1 for the current cell.
                        # 2. A set describing which direction the cells are merged.
                        ds[parent_cell] = [[1,1], set([])]
                    else:
                        if parent_cell != child_cell and child_cell[0] == parent_cell[0]:
                            ds[parent_cell][0][0] += 1
                            ds[parent_cell][1].add('right')
                        elif parent_cell != child_cell and child_cell[0] > parent_cell[0]:
                            if child_cell[1] == parent_cell[1]:
                                ds[parent_cell][0][1] += 1
                            ds[parent_cell][1].add('down')
                        else:
                            raise RuntimeError('Something went wrong')
        return ds

    def mark_cells_going_right(cell, curr_cell, merged_cells):
        """
        Add a "colspan" attribute and mark empty table
        columns for deletion if they are part of a
        merged cell going right.

        Args:
            cell: BeautifulSoup element tag object
            representation of the current cell.

            curr_cell: tuple, numeric representation
            of the current cell.

            merged_cells: dictionary of of data about
            merged cells.
        """
        #if curr_cell in merged_cells and merged_cells[curr_cell][1] == set(['right']):
        try:
            xcount = merged_cells[curr_cell][0][0]
            if xcount > 1: # No colspans on 1
                cell['colspan'] = xcount
            col_count = xcount - 1
            while col_count > 0:
                cell = cell.find_next_sibling()
                cell['class'] = 'delete'
                col_count -= 1
        except:
            pass

    def mark_cells_going_down(cell, curr_cell, merged_cells):
        """
        Add a "rowspan" attribute and mark empty table
        columns for deletion if they are part of a
        merged cell going down.

        Args:
            cell: BeautifulSoup element tag object
            representation of the current cell.

            curr_cell: tuple, numeric representation
            of the current cell.

            merged_cells: dictionary of of data about
            merged cells.
        """
        if curr_cell in merged_cells and merged_cells[curr_cell][1] == set(['down']):
            ycount = merged_cells[curr_cell][0][1]
            cell['rowspan'] = ycount
            row_count = ycount
            for child_row in cell.parent.find_next_siblings(limit=row_count - 1):
                i = 0
                for child in child_row.find_all('td'):
                    if i == curr_cell[1]:
                        child['class'] = 'delete'
                    i += 1

    def mark_cells_going_down_and_right(cell, curr_cell, merged_cells):
        """
        Add "rowspan" and "colspan" attributes and mark
        empty columns for deletion if they are part of a
        merged cell going down and to the right diagonally.

        Args:
            cell: BeautifulSoup element tag object
            representation of the current cell.

            curr_cell: tuple, numeric representation
            of the current cell.

            merged_cells: dictionary of of data about
            merged cells.
        """
        if curr_cell in merged_cells and \
            ('down' in merged_cells[curr_cell][1] and \
             'right' in merged_cells[curr_cell][1]):
            xcount = merged_cells[curr_cell][0][0]
            ycount = merged_cells[curr_cell][0][1]
            row_count = ycount
            col_count = xcount
            mark_cells_going_right(cell, curr_cell, merged_cells)

            flag = False
            for child_row in [cell.parent] + cell.parent.find_all_next('tr', limit=row_count - 1):
                i = 0
                for child in child_row.find_all('td'):
                    if i == curr_cell[1]:
                        mark_cells_going_right(child, curr_cell, merged_cells)
                        if not flag:
                            child['colspan'] = col_count
                            child['rowspan'] = row_count
                            flag = True
                        else:
                            child['class'] = 'delete'
                    i += 1

    def is_empty_th(string):
        """
        Detects if a table cell is left
        empty (is a merged cell).

        Args:
            string: string
        """
        if string[:8] == 'Unnamed:':
            data = string.split(' ')
            if is_numeric(data[1]):
                return True
        return False

    def mark_header_cells(html):
        """
        Mark header cells for deletion if they
        need to be merged. Also, add colspan
        and scope attributes.

        Args:
            html: string
        """
        th = html.find_all('th')
        for header in th:
            txt = header.string
            if not is_empty_th(txt):
                header['scope'] = 'col'
                count = 1
                for sibling in header.find_next_siblings():
                    if is_empty_th(sibling.string):
                        count += 1
                        sibling['class'] = 'delete'
                    else:
                        break
                if count > 1:
                    header['colspan'] = count
                    header['scope'] = 'colgroup'

    def create_caption(html, caption):
        """
        Create a caption element for an
        accessible table and append it
        to the right part of the tree.

        Args:
            html: string

            caption: string
        """
        ctag = html.new_tag('caption')
        ctag.insert(0, caption)
        html.table.insert(0, ctag)

    def create_summary_and_details(html, details):
        """
        Create a summary and details element
        for an accessible table and insert
        it into the right part of the tree.

        Args:
            html: string

            details: string
        """
        if len(details) != 2:
            msg = 'The "details" argument should be a list with two items. ' \
                + 'The first item should be a string for the html summary ' \
                + 'and the second should be a long description for the details ' \
                + 'element. Both of those must be included and nothing else.'
            raise RuntimeError(msg)

        summary = details[0]
        details = details[1]

        if not caption:
            create_caption(html, caption)

        dtag = html.new_tag('details')
        stag = html.new_tag('summary')
        ptag = html.new_tag('p')
        stag.insert(0, summary)
        ptag.insert(0, details)
        dtag.insert(0, stag)
        dtag.append(ptag)
        html.table.caption.insert(1, dtag)

    def format_properly(html):
        """
        Fix bad formatting from beautifulsoup.

        Args:
            html: string of html representing
            a table.
        """
        return html.replace('\n    ', '').replace('\n   	', \
            '	').replace('\n   	', '	').replace('\n   ', \
            '').replace('\n
', '
')

    def add_row_headers(html):
        """
        Convert s to s if row_headers
        is set to True.

        Args:
            html: string, table.
        """
        for row in html.tbody.find_all('tr'):
            spans_rows = 'rowspan' in row.td.attrs
            spans_columns = 'colspan' in row.td.attrs
            new_tag = html.new_tag('th')
            new_tag['scope'] = 'row'
            new_tag.string = row.td.string
            if spans_rows:
                new_tag['rowspan'] = row.td.attrs['rowspan']
                new_tag['scope'] = 'rowgroup'
            if spans_columns:
                new_tag['colspan'] = row.td.attrs['colspan']
            row.td.replace_with(new_tag)

    def beautify(html):
        """
        Beautify the html from pandas.

        Args:
            html: table markup from pandas.
        """
        table = html.find('table')
        first_tr = table.find('tr')
        del table['border']
        del first_tr['style']

        return format_properly(html.prettify(formatter='minimal'))

    def parse_html(html, caption, details):
        """
        Use BeautifulSoup to correct the
        html for merged columns and rows.
        What could possibly go wrong?

        Args:
            html: string

            caption: string

            details: list of strings lenght of two

        Returns:
            string, modified html
        """
        new_html = BeautifulSoup(html, 'html.parser')
        if merge:
            row_num = 1
            # e.g. {(4, 3): [1, 'right'], (2, 1): [1, 'down']}
            merged_cells = get_data_on_merged_cells()
            rows = new_html.find('table').find('tbody').find_all('tr')
            for row in rows:
                cell_num = 0 # Why are we off by 1? Maybe because we set index to False in to_html?
                cells = row.find_all('td')
                for cell in cells:
                    #cell['class'] = str(row_num) + ' ' + str(cell_num) # DEBUG
                    curr_cell = (row_num, cell_num)

                    # Mark merged cells for deletion
                    mark_cells_going_right(cell, curr_cell, merged_cells)
                    mark_cells_going_down(cell, curr_cell, merged_cells)
                    mark_cells_going_down_and_right(cell, curr_cell, merged_cells)

                    cell_num += 1
                row_num += 1

            # Mark header cells for deletion
            mark_header_cells(new_html)

            # Delete all the renegade cells at once
            destroy = new_html.find_all(attrs={'class' : 'delete' })
            for item in destroy:
                item.extract()

        # Convert s to s if needed.
        if row_headers:
            add_row_headers(new_html)

        # Add caption if applicable
        if caption:
            create_caption(new_html, caption)

        # Add summary and details if possible
        if details:
            create_summary_and_details(new_html, details)

        return beautify(new_html)

    # Set options for pandas and load the excel file
    pd.options.display.max_colwidth = -1
    xls = pd.ExcelFile(path)

    # Parse the sheet you're interested in, results in a Dataframe
    df = xls.parse(sheetname)

    # Convert the dataframe to html
    panda_html = df.to_html(classes=css_classes, index=False, na_rep='')

    # Parse the panda html to merge cells and beautify the markup
    return parse_html(panda_html, caption, details)


Instruction:
Can you write a function in Python 3 where it
calculates the future value of the current value of the aracartor for a given number of years.

Response:
def future_value(present_value, annual_rate, periods_per_year, years):
    """
    Calculates the future value of money invested at an anual interest rate,
    x times per year, for a given number of years.

    Args:
        present_value: int or float, the current value of the money (principal).

        annual_rate: float 0 to 1 e.g., .5 = 50%), the interest rate paid out.

        periods_per_year: int, the number of times money is invested per year.

        years: int, the number of years invested.

    Returns:
        Float, the future value of the money invested with compound interest.
    """

    # The nominal interest rate per period (rate) is how much interest you earn during a
    # particular length of time, before accounting for compounding. This is typically
    # expressed as a percentage.
    rate_per_period = annual_rate / float(periods_per_year)

    # How many periods in the future the calculation is for.
    periods = periods_per_year * years

    return present_value * (1 + rate_per_period) ** periods


Instruction:
Given the following Python 3 function, write the documentation
def triangle_area(point1, point2, point3):

    """Lengths of the three sides of the triangle"""
    a = point_distance(point1, point2)
    b = point_distance(point1, point3)
    c = point_distance(point2, point3)

    """Where s is the semiperimeter"""
    s = (a + b + c) / 2.0

    """Return the area of the triangle (using Heron's formula)"""
    return math.sqrt(s * (s - a) * (s - b) * (s - c))

Response: Calculates the area of a triangle.


Instruction:
Can you tell what is the following Python 3 function doing
def regular_polygon_area(number_of_sides, length_of_sides):
    return (0.25 * number_of_sides * length_of_sides ** 2) / math.tan(
        math.pi / number_of_sides
    )

Response: Calculates the area of a regular polygon


Instruction:
Write a Python 3 function that can
calculate the median of a list of integers or floating point numbers.

Response:
def median(data):
    """
    Calculates  the median of a list of integers or floating point numbers.

    Args:
        data: A list of integers or floating point numbers

    Returns:
        Sorts the list numerically and returns the middle number if the list has an odd number
        of items. If the list contains an even number of items the mean of the two middle numbers
        is returned.
    """
    ordered = sorted(data)
    length = len(ordered)
    if length % 2 == 0:
        return (
            ordered[math.floor(length / 2) - 1] + ordered[math.floor(length / 2)]
        ) / 2.0

    elif length % 2 != 0:
        return ordered[math.floor(length / 2)]


Instruction:
Create a Python 3 function to
calculate the average or mean of a list of numbers or a Decimal object.

Response:
def average(numbers, numtype='float'):
    """
    Calculates the average or mean of a list of numbers

    Args:
        numbers: a list of integers or floating point numbers.

        numtype: string, 'decimal' or 'float'; the type of number to return.

    Returns:
        The average (mean) of the numbers as a floating point number
        or a Decimal object.

    Requires:
        The math module
    """
    if type == 'decimal':
        return Decimal(sum(numbers)) / len(numbers)
    else:
        return float(sum(numbers)) / len(numbers)


Instruction:
Can you tell what is the following Python 3 function doing
def variance(numbers, type='population'):
    mean = average(numbers)
    variance = 0
    for number in numbers:
        variance += (mean - number) ** 2

    if type == 'population':
        return variance / len(numbers)
    else:
        return variance / (len(numbers) - 1)

Response: Calculates the population or sample variance of a list of numbers.


Instruction:
Here you have a function in Python 3, explain what it does
def get_percentage(a, b, i=False, r=False):
    # Round to the second decimal
    if i is False and r is True:
        percentage = round(100.0 * (float(a) / b), 2)

    # Round to the nearest whole number
    elif (i is True and r is True) or (i is True and r is False):
        percentage = int(round(100 * (float(a) / b)))

        # A rounded number and an integer were requested
        if r is False:
            warnings.warn(
                "If integer is set to True and Round is set to False, you will still get a rounded number if you pass floating point numbers as arguments."
            )

    # A precise unrounded decimal
    else:
        percentage = 100.0 * (float(a) / b)

    return percentage

Response: Returns the percentage of one number over another.


Instruction:
Write a Python 3 function that can
calculate the net take - home pay including employer retirement savings

Response:
def take_home_pay(gross_pay, employer_match, taxes_and_fees, numtype='float'):
    """
    Calculate net take-home pay including employer retirement savings match
    using the formula laid out by Mr. Money Mustache:
    http://www.mrmoneymustache.com/2015/01/26/calculating-net-worth/

    Args:
        gross_pay: float or int, gross monthly pay.

        employer_match: float or int, the 401(k) match from your employer.

        taxes_and_fees: list, taxes and fees that are deducted from your paycheck.

        numtype: string, 'decimal' or 'float'; the type of number to return.

    Returns:
        your monthly take-home pay.
    """
    if numtype == 'decimal':
        return (Decimal(gross_pay) + Decimal(employer_match)) - Decimal(
            sum(taxes_and_fees)
        )
    else:
        return (float(gross_pay) + float(employer_match)) - sum(taxes_and_fees)


Instruction:
How would you explain what the following Python 3 function does
def savings_rate(take_home_pay, spending, numtype='float'):

    if numtype == 'decimal':
        try:
            return (
                (Decimal(take_home_pay) - Decimal(spending)) / (Decimal(take_home_pay))
            ) * Decimal(100.0)
        # Leave InvalidOperation for backwards compatibility
        except (InvalidOperation, DivisionByZero):
            return Decimal(0.0)
    else:
        try:
            return (
                (float(take_home_pay) - float(spending)) / (float(take_home_pay))
            ) * 100.0
        except (ZeroDivisionError):
            return 0.0

Response: Calculate monthly savings rate for a given monthly take - home pay and spending.


Instruction:
Make a summary of the following Python 3 code
def get_variable(relpath, keyword='__version__'):
    for line in open(os.path.join(os.path.dirname(__file__), relpath), encoding='cp437'):
        if keyword in line:
            if '"' in line:
                return line.split('"')[1]
            elif "'" in line:
                return line.split("'")[1]

Response:
Read the variable from a python file without importing it
 from gist. github. com.


Instruction:
Can you create a Python 3 function that
converts a datetime string to a unix timestamp

Response:
def datetime_str_to_timestamp(datetime_str):
        '''
        '2018-01-01 00:00:00' (str) --> 1514736000

        :param str datetime_str: datetime string
        :return: unix timestamp (int) or None
        :rtype: int or None
        '''

        try:
            dtf = DTFormat()
            struct_time = time.strptime(datetime_str, dtf.datetime_format)
            return time.mktime(struct_time)
        except:
            return None


Instruction:
Can you implement a function in Python 3 that
gets datetime string from datetime object

Response:
def get_datetime_string(datetime_obj):
        '''
        Get datetime string from datetime object

        :param datetime datetime_obj: datetime object
        :return: datetime string
        :rtype: str
        '''

        if isinstance(datetime_obj, datetime):
            dft = DTFormat()
            return datetime_obj.strftime(dft.datetime_format)

        return None


Instruction:
Can you generate a brief explanation for the following Python 3 code
def timestamp_to_datetime(timestamp):
        '''
        1514736000 --> datetime object

        :param int timestamp: unix timestamp (int)
        :return: datetime object or None
        :rtype: datetime or None
        '''

        if isinstance(timestamp, (int, float, str)):
            try:
                timestamp = float(timestamp)
                if timestamp.is_integer():
                    timestamp = int(timestamp)
            except:
                return None

            temp = str(timestamp).split('.')[0]
            if len(temp) == 13:
                timestamp = timestamp / 1000.0

            if len(temp) < 10:
                return None

        else:
            return None

        return datetime.fromtimestamp(timestamp)

Response: converts unix timestamp to datetime object


Instruction:
Make a summary of the following Python 3 code
def attrdict(prev, attr_names):
    if isinstance(attr_names, dict):
        for obj in prev:
            attr_values = dict()
            for name in attr_names.keys():
                if hasattr(obj, name):
                    attr_values[name] = getattr(obj, name)
                else:
                    attr_values[name] = attr_names[name]
            yield attr_values
    else:
        for obj in prev:
            attr_values = dict()
            for name in attr_names:
                if hasattr(obj, name):
                    attr_values[name] = getattr(obj, name)
            yield attr_values

Response: This function returns a generator that yields the attribute values of the object in the object that is in the pipe.


Instruction:
Explain what the following Python 3 code does
def flatten(prev, depth=sys.maxsize):
    def inner_flatten(iterable, curr_level, max_levels):
        for i in iterable:
            if hasattr(i, '__iter__') and curr_level < max_levels:
                for j in inner_flatten(i, curr_level + 1, max_levels):
                    yield j
            else:
                yield i

    for d in prev:
        if hasattr(d, '__iter__') and depth > 0:
            for inner_d in inner_flatten(d, 1, depth):
                yield inner_d
        else:
            yield d

Response: This function flattens the given pipe into a single nested item.


Instruction:
Implement a Python 3 function for
valuing pipe extract value from previous pipe.

Response:
def values(prev, *keys, **kw):
    """values pipe extract value from previous pipe.

    If previous pipe send a dictionary to values pipe, keys should contains
    the key of dictionary which you want to get. If previous pipe send list or
    tuple,

    :param prev: The previous iterator of pipe.
    :type prev: Pipe
    :returns: generator
    """
    d = next(prev)
    if isinstance(d, dict):
        yield [d[k] for k in keys if k in d]
        for d in prev:
            yield [d[k] for k in keys if k in d]
    else:
        yield [d[i] for i in keys if 0 <= i < len(d)]
        for d in prev:
            yield [d[i] for i in keys if 0 <= i < len(d)]


Instruction:
How would you code a function in Python 3 to
pack pipe takes n elements from previous generator and yield one list to next. :param prev: The previous iterator of pipe. :type prev: Pipe :param rest: Set True to allow to output the rest part of last elements. :type prev: boolean :param padding: Specify the padding element for the rest part of last elements. :type prev: boolean :returns: generator :Example: >>> result([1,2,3,4,5,6,7] | pack(3)) [[1, 2, 3], [4, 5, 6]] >>> result([1,2,3,4,5,6,7] | pack(3, rest=True)) [[1, 2, 3], [4, 5, 6], [7,]] >>> result([1,2,3,4,5,6,7] | pack(3, padding=None)) [[1, 2, 3], [4, 5, 6], [7, None, None]]

Response:
def pack(prev, n, rest=False, **kw):
    """pack pipe takes n elements from previous generator and yield one
    list to next.

    :param prev: The previous iterator of pipe.
    :type prev: Pipe
    :param rest: Set True to allow to output the rest part of last elements.
    :type prev: boolean
    :param padding: Specify the padding element for the rest part of last elements.
    :type prev: boolean
    :returns: generator

    :Example:
    >>> result([1,2,3,4,5,6,7] | pack(3))
    [[1, 2, 3], [4, 5, 6]]

    >>> result([1,2,3,4,5,6,7] | pack(3, rest=True))
    [[1, 2, 3], [4, 5, 6], [7,]]

    >>> result([1,2,3,4,5,6,7] | pack(3, padding=None))
    [[1, 2, 3], [4, 5, 6], [7, None, None]]
    """

    if 'padding' in kw:
        use_padding = True
        padding = kw['padding']
    else:
        use_padding = False
        padding = None

    items = []
    for i, data in enumerate(prev, 1):
        items.append(data)
        if (i % n) == 0:
            yield items
            items = []
    if len(items) != 0 and rest:
        if use_padding:
            items.extend([padding, ] * (n - (i % n)))
        yield items


Instruction:
How would you explain what the following Python 3 function does
def grep(prev, pattern, *args, **kw):
    inv = False if 'inv' not in kw else kw.pop('inv')
    pattern_obj = re.compile(pattern, *args, **kw)

    for data in prev:
        if bool(inv) ^ bool(pattern_obj.match(data)):
            yield data

Response: The grep function for the given pattern in the given pipe.


Instruction:
How would you explain what the following Python 3 function does
def match(prev, pattern, *args, **kw):
    to = 'to' in kw and kw.pop('to')
    pattern_obj = re.compile(pattern, *args, **kw)

    if to is dict:
        for data in prev:
            match = pattern_obj.match(data)
            if match is not None:
                yield match.groupdict()
    elif to is tuple:
        for data in prev:
            match = pattern_obj.match(data)
            if match is not None:
                yield match.groups()
    elif to is list:
        for data in prev:
            match = pattern_obj.match(data)
            if match is not None:
                yield list(match.groups())
    else:
        for data in prev:
            match = pattern_obj.match(data)
            if match is not None:
                yield match

Response: The pattern is a regular expression which can be used to filter the data.