Instruction:
Create a Python 3 function for
adding element_by alias and extension methods (if_exists or or_none.

Response:
def add_element_extension_method(Klass):
    """Add element_by alias and extension' methods(if_exists/or_none)."""
    def add_element_method(Klass, using):
        locator = using.name.lower()
        find_element_name = "element_by_" + locator
        find_element_if_exists_name = "element_by_" + locator + "_if_exists"
        find_element_or_none_name = "element_by_" + locator + "_or_none"
        wait_for_element_name = "wait_for_element_by_" + locator

        find_elements_name = "elements_by_" + locator
        wait_for_elements_name = "wait_for_elements_by_" + locator

        def find_element(self, value):
            return self.element(using.value, value)

        find_element.__name__ = find_element_name
        find_element.__doc__ = (
            "Set parameter 'using' to '{0}'.\n".format(using.value) +
            "See more in \'element\' method."
        )

        def find_element_if_exists(self, value):
            return self.element_if_exists(using.value, value)

        find_element_if_exists.__name__ = find_element_if_exists_name
        find_element_if_exists.__doc__ = (
            "Set parameter 'using' to '{0}'.\n".format(using.value) +
            "See more in \'element_if_exists\' method."
        )

        def find_element_or_none(self, value):
            return self.element_or_none(using.value, value)

        find_element_or_none.__name__ = find_element_or_none_name
        find_element_or_none.__doc__ = (
            "Set parameter 'using' to '{0}'.\n".format(using.value) +
            "See more in \'element_or_none\' method."
        )

        def wait_for_element_by(self, *args, **kwargs):
            return self.wait_for_element(using.value, *args, **kwargs)

        wait_for_element_by.__name__ = wait_for_element_name
        wait_for_element_by.__doc__ = (
            "Set parameter 'using' to '{0}'.\n".format(using.value) +
            "See more in \'wait_for_element\' method."
        )

        def find_elements(self, value):
            return self.elements(using.value, value)

        find_elements.__name__ = find_elements_name
        find_elements.__doc__ = (
            "Set parameter 'using' to '{0}'.\n".format(using.value) +
            "See more in \'elements\' method."
        )

        def wait_for_elements_available(self, *args, **kwargs):
            return self.wait_for_elements(using.value, *args, **kwargs)

        wait_for_elements_available.__name__ = wait_for_elements_name
        wait_for_elements_available.__doc__ = (
            "Set parameter 'using' to '{0}'.\n".format(using.value) +
            "See more in \'wait_for_elements\' method."
        )

        setattr(Klass, find_element_name, find_element)
        setattr(Klass, find_element_if_exists_name, find_element_if_exists)
        setattr(Klass, find_element_or_none_name, find_element_or_none)
        setattr(Klass, wait_for_element_name, wait_for_element_by)
        setattr(Klass, find_elements_name, find_elements)
        setattr(Klass, wait_for_elements_name, wait_for_elements_available)

    for locator in iter(Locator):
        add_element_method(Klass, locator)


Instruction:
How would you implement a function in Python 3 that
converts value to a list of key strokes

Response:
def value_to_key_strokes(value):
    """Convert value to a list of key strokes
    >>> value_to_key_strokes(123)
    ['123']
    >>> value_to_key_strokes('123')
    ['123']
    >>> value_to_key_strokes([1, 2, 3])
    ['123']
    >>> value_to_key_strokes(['1', '2', '3'])
    ['123']

    Args:
        value(int|str|list)

    Returns:
        A list of string.
    """
    result = ''
    if isinstance(value, Integral):
        value = str(value)

    for v in value:
        if isinstance(v, Keys):
            result += v.value
        elif isinstance(v, Integral):
            result += str(v)
        else:
            result += v
    return [result]


Instruction:
Given the following Python 3 function, write the documentation
def value_to_single_key_strokes(value):
    result = []
    if isinstance(value, Integral):
        value = str(value)

    for v in value:
        if isinstance(v, Keys):
            result.append(v.value)
        elif isinstance(v, Integral):
            result.append(str(v))
        else:
            result.append(v)
    return result

Response: Convert value to a list of key strokes.


Instruction:
Create a Python 3 function for
implementing the check_unused_args in superclass.

Response:
def check_unused_args(self, used_args, args, kwargs):
        """Implement the check_unused_args in superclass."""
        for k, v in kwargs.items():
            if k in used_args:
                self._used_kwargs.update({k: v})
            else:
                self._unused_kwargs.update({k: v})


Instruction:
Create a Python 3 function for
clearing used and unused dicts before each formatting.

Response:
def vformat(self, format_string, args, kwargs):
        """Clear used and unused dicts before each formatting."""
        self._used_kwargs = {}
        self._unused_kwargs = {}
        return super(MemorizeFormatter, self).vformat(format_string, args, kwargs)


Instruction:
Create a Python 3 function to
format a string by a map

Response:
def format_map(self, format_string, mapping):
        """format a string by a map

        Args:
            format_string(str): A format string
            mapping(dict): A map to format the string

        Returns:
            A formatted string.

        Raises:
            KeyError: if key is not provided by the given map.
        """
        return self.vformat(format_string, args=None, kwargs=mapping)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def find_exception_by_code(code):
    errorName = None
    for error in WebDriverError:
        if error.value.code == code:
            errorName = error
            break
    return errorName

Response: Find error name by error code.


Instruction:
Given the following Python 3 function, write the documentation
def execute(self, command, data={}):
        method, uri = command
        try:
            path = self._formatter.format_map(uri, data)
            body = self._formatter.get_unused_kwargs()
            url = "{0}{1}".format(self._url, path)
            return self._request(method, url, body)
        except KeyError as err:
            LOGGER.debug(
                'Endpoint {0} is missing argument {1}'.format(uri, err))
            raise

Response: Execute a command on the remote server and return the json body.


Instruction:
Can you generate the documentation for the following Python 3 function
def _execute(self, command, data=None, unpack=True):
        if not data:
            data = {}
        data.setdefault('element_id', self.element_id)
        return self._driver._execute(command, data, unpack)

Response: Private method to execute command with data.


Instruction:
Here you have a function in Python 3, explain what it does
def element(self, using, value):
        return self._execute(Command.FIND_CHILD_ELEMENT, {
            'using': using,
            'value': value
        })

Response: Find an element in the current element.


Instruction:
Can you tell what is the following Python 3 function doing
def element_or_none(self, using, value):
        try:
            return self._execute(Command.FIND_CHILD_ELEMENT, {
                'using': using,
                'value': value
            })
        except:
            return None

Response: Check if an element in the current element or return None if it does not exist.


Instruction:
Given the following Python 3 function, write the documentation
def elements(self, using, value):
        return self._execute(Command.FIND_CHILD_ELEMENTS, {
            'using': using,
            'value': value
        })

Response: Find elements in the current element.


Instruction:
Write a Python 3 function that can
deprecate use element. touch move to

Response:
def move_to(self, x=0, y=0):
        """Deprecated use element.touch('drag', { toX, toY, duration(s) }) instead.
            Move the mouse by an offset of the specificed element.

        Support:
            Android

        Args:
            x(float): X offset to move to, relative to the
                      top-left corner of the element.
            y(float): Y offset to move to, relative to the
                      top-left corner of the element.

        Returns:
            WebElement object.
        """
        self._driver.move_to(self, x, y)


Instruction:
Can you implement a function in Python 3 that
flicks on the touch screen using finger motion events.

Response:
def flick(self, x, y, speed):
        """Deprecated use touch('drag', { fromX, fromY, toX, toY, duration(s) }) instead.
            Flick on the touch screen using finger motion events.
            This flickcommand starts at a particulat screen location.

        Support:
            iOS

        Args:
            x(float}: The x offset in pixels to flick by.
            y(float): The y offset in pixels to flick by.
            speed(float) The speed in pixels per seconds.

        Returns:
            WebElement object.
        """
        self._driver.flick(self, x, y, speed)


Instruction:
Can you tell what is the following Python 3 function doing
def touch(self, name, args=None):
        if isinstance(name, list) and not isinstance(name, str):
            for obj in name:
                obj['element'] = self.element_id
            actions = name
        elif isinstance(name, str):
            if not args:
                args = {}
            args['type'] = name
            args['element'] = self.element_id
            actions = [args]
        else:
            raise TypeError('Invalid parameters.')
        self._driver._execute(Command.PERFORM_ACTIONS, {
            'actions': actions
        })

Response: Applies touch actions on devices. Such as tap doubleTap press pinch rotate drag.


Instruction:
Write a Python 3 function that can
assert whether the target is displayed.

Response:
def is_displayed(target):
    """Assert whether the target is displayed

    Args:
        target(WebElement): WebElement Object.

    Returns:
        Return True if the element is displayed or return False otherwise.
    """
    is_displayed = getattr(target, 'is_displayed', None)
    if not is_displayed or not callable(is_displayed):
        raise TypeError('Target has no attribute \'is_displayed\' or not callable')
    if not is_displayed():
        raise WebDriverException('element not visible')


Instruction:
Can you implement a function in Python 3 that
plugs in to Virtual USB Bus

Response:
def PlugIn(self):
        """Take next available controller id and plug in to Virtual USB Bus"""
        ids = self.available_ids()
        if len(ids) == 0:
            raise MaxInputsReachedError('Max Inputs Reached')

        self.id = ids[0]

        _xinput.PlugIn(self.id)
        while self.id in self.available_ids():
            pass


Instruction:
Write a Python 3 script to
unplug the controller from Virtual USB Bus and free up ID.

Response:
def UnPlug(self, force=False):
        """Unplug controller from Virtual USB Bus and free up ID"""
        if force:
            _xinput.UnPlugForce(c_uint(self.id))
        else:
            _xinput.UnPlug(c_uint(self.id))
        while self.id not in self.available_ids():
            if self.id == 0:
                break


Instruction:
Implement a function in Python 3 to
set a value on the controller.

Response:
def set_value(self, control, value=None):
        """Set a value on the controller
    If percent is True all controls will accept a value between -1.0 and 1.0

    If not then:
        Triggers are 0 to 255
        Axis are -32768 to 32767

    Control List:
        AxisLx          , Left Stick X-Axis
        AxisLy          , Left Stick Y-Axis
        AxisRx          , Right Stick X-Axis
        AxisRy          , Right Stick Y-Axis
        BtnBack         , Menu/Back Button
        BtnStart        , Start Button
        BtnA            , A Button
        BtnB            , B Button
        BtnX            , X Button
        BtnY            , Y Button
        BtnThumbL       , Left Thumbstick Click
        BtnThumbR       , Right Thumbstick Click
        BtnShoulderL    , Left Shoulder Button
        BtnShoulderR    , Right Shoulder Button
        Dpad            , Set Dpad Value (0 = Off, Use DPAD_### Constants)
        TriggerL        , Left Trigger
        TriggerR        , Right Trigger

    """
        func = getattr(_xinput, 'Set' + control)

        if 'Axis' in control:
            target_type = c_short

            if self.percent:
                target_value = int(32767 * value)
            else:
                target_value = value
        elif 'Btn' in control:
            target_type = c_bool
            target_value = bool(value)
        elif 'Trigger' in control:
            target_type = c_byte

            if self.percent:
                target_value = int(255 * value)
            else:
                target_value = value
        elif 'Dpad' in control:
            target_type = c_int
            target_value = int(value)

        func(c_uint(self.id), target_type(target_value))


Instruction:
Implement a function in Python 3 to
test the functionality of the rController object

Response:
def main():
    """Test the functionality of the rController object"""
    import time

    print('Testing controller in position 1:')
    print('Running 3 x 3 seconds tests')

    # Initialise Controller
    con = rController(1)

    # Loop printing controller state and buttons held
    for i in range(3):
        print('Waiting...')
        time.sleep(2.5)
        print('State: ', con.gamepad)
        print('Buttons: ', con.buttons)
        time.sleep(0.5)

    print('Done!')


Instruction:
Explain what the following Python 3 code does
def gamepad(self):
        state = _xinput_state()
        _xinput.XInputGetState(self.ControllerID - 1, pointer(state))
        self.dwPacketNumber = state.dwPacketNumber

        return state.XINPUT_GAMEPAD

Response: Returns the current gamepad state.


Instruction:
Implement a function in Python 3 to
return a list of buttons currently pressed

Response:
def buttons(self):
        """Returns a list of buttons currently pressed"""
        return [name for name, value in rController._buttons.items()
                if self.gamepad.wButtons & value == value]


Instruction:
Write a Python 3 function for
decoding a 7 - bit ASCII header value into its actual value.

Response:
def maybe_decode_header(header):
    """
    Decodes an encoded 7-bit ASCII header value into it's actual value.
    """
    value, encoding = decode_header(header)[0]
    if encoding:
        return value.decode(encoding)
    else:
        return value


Instruction:
Write a Python 3 script for
importing all available previews classes.

Response:
def autodiscover():
    """
    Imports all available previews classes.
    """
    from django.conf import settings
    for application in settings.INSTALLED_APPS:
        module = import_module(application)

        if module_has_submodule(module, 'emails'):
            emails = import_module('%s.emails' % application)
            try:
                import_module('%s.emails.previews' % application)
            except ImportError:
                # Only raise the exception if this module contains previews and
                # there was a problem importing them. (An emails module that
                # does not contain previews is not an error.)
                if module_has_submodule(emails, 'previews'):
                    raise


Instruction:
Can you write a function in Python 3 where it
registers a new preview with the index.

Response:
def register(self, cls):
        """
        Adds a preview to the index.
        """
        preview = cls(site=self)
        logger.debug('Registering %r with %r', preview, self)
        index = self.__previews.setdefault(preview.module, {})
        index[cls.__name__] = preview


Instruction:
How would you implement a function in Python 3 that
returns a detail view for the given module and preview.

Response:
def detail_view(self, request, module, preview):
        """
        Looks up a preview in the index, returning a detail view response.
        """
        try:
            preview = self.__previews[module][preview]
        except KeyError:
            raise Http404  # The provided module/preview does not exist in the index.
        return preview.detail_view(request)


Instruction:
Can you write a function in Python 3 where it
returns the URL to access this preview.

Response:
def url(self):
        """
        The URL to access this preview.
        """
        return reverse('%s:detail' % URL_NAMESPACE, kwargs={
            'module': self.module,
            'preview': type(self).__name__,
        })


Instruction:
Write a Python 3 script to
render the message view to a response.

Response:
def detail_view(self, request):
        """
        Renders the message view to a response.
        """
        context = {
            'preview': self,
        }

        kwargs = {}
        if self.form_class:
            if request.GET:
                form = self.form_class(data=request.GET)
            else:
                form = self.form_class()

            context['form'] = form
            if not form.is_bound or not form.is_valid():
                return render(request, 'mailviews/previews/detail.html', context)

            kwargs.update(form.get_message_view_kwargs())

        message_view = self.get_message_view(request, **kwargs)

        message = message_view.render_to_message()
        raw = message.message()
        headers = OrderedDict((header, maybe_decode_header(raw[header])) for header in self.headers)

        context.update({
            'message': message,
            'subject': message.subject,
            'body': message.body,
            'headers': headers,
            'raw': raw.as_string(),
        })

        alternatives = getattr(message, 'alternatives', [])
        try:
            html = next(alternative[0] for alternative in alternatives
                if alternative[1] == 'text/html')
            context.update({
                'html': html,
                'escaped_html': b64encode(html.encode('utf-8')),
            })
        except StopIteration:
            pass

        return render(request, self.template_name, context)


Instruction:
Can you generate the documentation for the following Python 3 function
def split_docstring(value):
    docstring = textwrap.dedent(getattr(value, '__doc__', ''))
    if not docstring:
        return None

    pieces = docstring.strip().split('\n\n', 1)
    try:
        body = pieces[1]
    except IndexError:
        body = None

    return Docstring(pieces[0], body)

Response: Splits the docstring of the given value into its summary and body.


Instruction:
Write a Python 3 script for
rendering and returns an unsent message with the provided context.

Response:
def render_to_message(self, extra_context=None, **kwargs):
        """
        Renders and returns an unsent message with the provided context.

        Any extra keyword arguments passed will be passed through as keyword
        arguments to the message constructor.

        :param extra_context: Any additional context to use when rendering the
            templated content.
        :type extra_context: :class:`dict`
        :returns: A message instance.
        :rtype: :attr:`.message_class`
        """
        if extra_context is None:
            extra_context = {}

        # Ensure our custom headers are added to the underlying message class.
        kwargs.setdefault('headers', {}).update(self.headers)

        context = self.get_context_data(**extra_context)
        return self.message_class(
            subject=self.render_subject(context),
            body=self.render_body(context),
            **kwargs)


Instruction:
Write a Python 3 function that can
render and sends an email message.

Response:
def send(self, extra_context=None, **kwargs):
        """
        Renders and sends an email message.

        All keyword arguments other than ``extra_context`` are passed through
        as keyword arguments when constructing a new :attr:`message_class`
        instance for this message.

        This method exists primarily for convenience, and the proper
        rendering of your message should not depend on the behavior of this
        method. To alter how a message is created, override
        :meth:``render_to_message`` instead, since that should always be
        called, even if a message is not sent.

        :param extra_context: Any additional context data that will be used
            when rendering this message.
        :type extra_context: :class:`dict`
        """
        message = self.render_to_message(extra_context=extra_context, **kwargs)
        return message.send()


Instruction:
Make a summary of the following Python 3 code
def render_subject(self, context):
        rendered = self.subject_template.render(unescape(context))
        return rendered.strip()

Response: Renders the subject for the given context.


Instruction:
How would you code a function in Python 3 to
render and returns an unsent message with the given context.

Response:
def render_to_message(self, extra_context=None, *args, **kwargs):
        """
        Renders and returns an unsent message with the given context.

        Any extra keyword arguments passed will be passed through as keyword
        arguments to the message constructor.

        :param extra_context: Any additional context to use when rendering
            templated content.
        :type extra_context: :class:`dict`
        :returns: A message instance.
        :rtype: :attr:`.message_class`
        """
        message = super(TemplatedHTMLEmailMessageView, self)\
            .render_to_message(extra_context, *args, **kwargs)

        if extra_context is None:
            extra_context = {}

        context = self.get_context_data(**extra_context)
        content = self.render_html_body(context)
        message.attach_alternative(content, mimetype='text/html')
        return message


Instruction:
Here you have a function in Python 3, explain what it does
def timestamp(_, dt):
    'get microseconds since 2000-01-01 00:00'
    # see http://stackoverflow.com/questions/2956886/
    dt = util.to_utc(dt)
    unix_timestamp = calendar.timegm(dt.timetuple())
    # timetuple doesn't maintain microseconds
    # see http://stackoverflow.com/a/14369386/519015
    val = ((unix_timestamp - psql_epoch) * 1000000) + dt.microsecond
    return ('iq', (8, val))

Response: get microseconds since 2000 - 01 - 01 00 : 00


Instruction:
How would you implement a function in Python 3 that
returns a string that represents a numeric value in the ISO - 8601 format.

Response:
def numeric(_, n):
    """
    NBASE = 1000
    ndigits = total number of base-NBASE digits
    weight = base-NBASE weight of first digit
    sign = 0x0000 if positive, 0x4000 if negative, 0xC000 if nan
    dscale = decimal digits after decimal place
    """
    try:
        nt = n.as_tuple()
    except AttributeError:
        raise TypeError('numeric field requires Decimal value (got %r)' % n)
    digits = []
    if isinstance(nt.exponent, str):
        # NaN, Inf, -Inf
        ndigits = 0
        weight = 0
        sign = 0xC000
        dscale = 0
    else:
        decdigits = list(reversed(nt.digits + (nt.exponent % 4) * (0,)))
        weight = 0
        while decdigits:
            if any(decdigits[:4]):
                break
            weight += 1
            del decdigits[:4]
        while decdigits:
            digits.insert(0, ndig(decdigits[:4]))
            del decdigits[:4]
        ndigits = len(digits)
        weight += nt.exponent // 4 + ndigits - 1
        sign = nt.sign * 0x4000
        dscale = -min(0, nt.exponent)
    data = [ndigits, weight, sign, dscale] + digits
    return ('ihhHH%dH' % ndigits, [2 * len(data)] + data)


Instruction:
Explain what the following Python 3 code does
def set_default_subparser(self, name, args=None):
    subparser_found = False
    for arg in sys.argv[1:]:
        if arg in ['-h', '--help']:  # global help if no subparser
            break
    else:
        for x in self._subparsers._actions:
            if not isinstance(x, argparse._SubParsersAction):
                continue
            for sp_name in x._name_parser_map.keys():
                if sp_name in sys.argv[1:]:
                    subparser_found = True
        if not subparser_found:
            # insert default in first position, this implies no
            # global options without a sub_parsers specified
            if args is None:
                sys.argv.insert(1, name)
            else:
                args.insert(0, name)

Response: set the default subparser for this command line


Instruction:
Create a Python 3 function to
convert a record to JSON - encode through redis.

Response:
def format(self, record):
        """
        JSON-encode a record for serializing through redis.

        Convert date to iso format, and stringify any exceptions.
        """
        data = record._raw.copy()

        # serialize the datetime date as utc string
        data['time'] = data['time'].isoformat()

        # stringify exception data
        if data.get('traceback'):
            data['traceback'] = self.formatException(data['traceback'])

        return json.dumps(data)


Instruction:
Implement a function in Python 3 to
publish a record to redis logging channel

Response:
def emit(self, record):
        """
        Publish record to redis logging channel
        """
        try:
            self.redis_client.publish(self.channel, self.format(record))
        except redis.RedisError:
            pass


Instruction:
Can you generate the documentation for the following Python 3 function
def emit(self, record):
        try:
            if self.max_messages:
                p = self.redis_client.pipeline()
                p.rpush(self.key, self.format(record))
                p.ltrim(self.key, -self.max_messages, -1)
                p.execute()
            else:
                self.redis_client.rpush(self.key, self.format(record))
        except redis.RedisError:
            pass

Response: Publish a record to the redis logging list


Instruction:
Can you generate a brief explanation for the following Python 3 code
def require_template_debug(f):
    def _(*args, **kwargs):
        TEMPLATE_DEBUG = getattr(settings, 'TEMPLATE_DEBUG', False)
        return f(*args, **kwargs) if TEMPLATE_DEBUG else ''
    return _

Response: Decorator that ensures that the TEMPLATE_DEBUG setting is set to True.


Instruction:
Can you implement a function in Python 3 that
displays the details of the variable attributes in the terminal.

Response:
def _display_details(var_data):
    """
    Given a dictionary of variable attribute data from get_details display the
    data in the terminal.
    """
    meta_keys = (key for key in list(var_data.keys())
                 if key.startswith('META_'))
    for key in meta_keys:
        display_key = key[5:].capitalize()
        pprint('{0}: {1}'.format(display_key, var_data.pop(key)))
    pprint(var_data)


Instruction:
Can you generate the documentation for the following Python 3 function
def set_trace(context):
    try:
        import ipdb as pdb
    except ImportError:
        import pdb
        print("For best results, pip install ipdb.")
    print("Variables that are available in the current context:")
    render = lambda s: template.Template(s).render(context)
    availables = get_variables(context)
    pprint(availables)
    print('Type `availables` to show this list.')
    print('Type <variable_name> to access one.')
    print('Use render("template string") to test template rendering')
    # Cram context variables into the local scope
    for var in availables:
        locals()[var] = context[var]
    pdb.set_trace()
    return ''

Response: Start a pdb set_trace inside of the template with the given context. Uses ipdb if available.


Instruction:
How would you implement a function in Python 3 that
starts a pydevd settrace on the next page

Response:
def pydevd(context):
    """
    Start a pydev settrace
    """
    global pdevd_not_available
    if pdevd_not_available:
        return ''
    try:
        import pydevd
    except ImportError:
        pdevd_not_available = True
        return ''
    render = lambda s: template.Template(s).render(context)
    availables = get_variables(context)
    for var in availables:
        locals()[var] = context[var]
    #catch the case where no client is listening
    try:
        pydevd.settrace()
    except socket.error:
        pdevd_not_available = True
    return ''


Instruction:
Can you tell what is the following Python 3 function doing
def _flatten(iterable):
    for i in iterable:
        if isinstance(i, Iterable) and not isinstance(i, string_types):
            for sub_i in _flatten(i):
                yield sub_i
        else:
            yield i

Response: Given an iterable with nested iterables generate a flat iterable


Instruction:
How would you implement a function in Python 3 that
gives a variable in the context obtain the attributes and callables and their values where possible and add them in the var_data dictionary.

Response:
def get_details(var):
    """
    Given a variable inside the context, obtain the attributes/callables,
    their values where possible, and the module name and class name if possible
    """
    var_data = {}
    # Obtain module and class details if available and add them in
    module = getattr(var, '__module__', '')
    kls = getattr(getattr(var, '__class__', ''), '__name__', '')
    if module:
        var_data['META_module_name'] = module
    if kls:
        var_data['META_class_name'] = kls
    for attr in get_attributes(var):
        value = _get_detail_value(var, attr)
        if value is not None:
            var_data[attr] = value
    return var_data


Instruction:
Here you have a function in Python 3, explain what it does
def _get_detail_value(var, attr):
    value = getattr(var, attr)
    # Rename common Django class names
    kls = getattr(getattr(value, '__class__', ''), '__name__', '')
    if kls in ('ManyRelatedManager', 'RelatedManager', 'EmptyManager'):
        return kls
    if callable(value):
        return 'routine'
    return value

Response:
Returns the value of a variable and one of its attributes that are available inside of
   .


Instruction:
Write a Python 3 function that can
give a varaible return the list of attributes that are available inside the template

Response:
def get_attributes(var):
    """
    Given a varaible, return the list of attributes that are available inside
    of a template
    """
    is_valid = partial(is_valid_in_template, var)
    return list(filter(is_valid, dir(var)))


Instruction:
Write a Python 3 function for
giving a variable and an attribute returns True or False accordingly

Response:
def is_valid_in_template(var, attr):
    """
    Given a variable and one of its attributes, determine if the attribute is
    accessible inside of a Django template and return True or False accordingly
    """
    # Remove private variables or methods
    if attr.startswith('_'):
        return False
    # Remove any attributes that raise an acception when read
    try:
        value = getattr(var, attr)
    except:
        return False
    if isroutine(value):
        # Remove any routines that are flagged with 'alters_data'
        if getattr(value, 'alters_data', False):
            return False
        else:
            # Remove any routines that require arguments
            try:
                argspec = getargspec(value)
                num_args = len(argspec.args) if argspec.args else 0
                num_defaults = len(argspec.defaults) if argspec.defaults else 0
                if num_args - num_defaults > 1:
                    return False
            except TypeError:
                # C extension callables are routines, but getargspec fails with
                # a TypeError when these are passed.
                pass
    return True


Instruction:
Create a Python 3 function for
incrementing version number string version.

Response:
def version_bump(self, version, type="bug"):
        """
        Increment version number string 'version'.

        Type can be one of: major, minor, or bug
        """
        parsed_version = LooseVersion(version).version
        total_components = max(3, len(parsed_version))

        bits = []
        for bit in parsed_version:
            try:
                bit = int(bit)
            except ValueError:
                continue

            bits.append(bit)

        indexes = {
            "major": 0,
            "minor": 1,
            "bug": 2,
        }

        bits += [0] * (3 - len(bits)) # pad to 3 digits

        # Increment the version
        bits[indexes[type]] += 1

        # Set the subsequent digits to 0
        for i in range(indexes[type] + 1, 3):
            bits[i] = 0

        return ".".join(map(str, bits))


Instruction:
Can you generate the documentation for the following Python 3 function
def determine_paths(self, package_name=None, create_package_dir=False, dry_run=False):

        # Give preference to the environment variable here as it will not
        # derefrence sym links
        self.project_dir = Path(os.getenv('PWD') or os.getcwd())

        # Try and work out the project name
        distribution = self.get_distribution()
        if distribution:
            # Get name from setup.py
            self.project_name = distribution.get_name()
        else:
            # ...failing that, use the current directory name
            self.project_name = self.project_dir.name

        # Descend into the 'src' directory to find the package
        # if necessary
        if os.path.isdir(self.project_dir / "src"):
            package_search_dir = self.project_dir / "src"
        else:
            package_search_dir = self.project_dir

        created_package_dir = False
        if not package_name:
            # Lets try and work out the package_name from the project_name
            package_name = self.project_name.replace("-", "_")

            # Now do some fuzzy matching
            def get_matches(name):
                possibles = [n for n in os.listdir(package_search_dir) if os.path.isdir(package_search_dir / n)]
                return difflib.get_close_matches(name, possibles, n=1, cutoff=0.8)

            close = get_matches(package_name)

            # If no matches, try removing the first part of the package name
            # (e.g. django-guardian becomes guardian)
            if not close and "_" in package_name:
                short_package_name = "_".join(package_name.split("_")[1:])
                close = get_matches(short_package_name)

            if not close:
                if create_package_dir:
                    package_dir = package_search_dir / package_name
                    # Gets set to true even during dry run
                    created_package_dir = True
                    if not dry_run:
                        print("Creating package directory at %s" % package_dir)
                        os.mkdir(package_dir)
                    else:
                        print("Would have created package directory at %s" % package_dir)
                else:
                    raise CommandError("Could not guess the package name. Specify it using --name.")
            else:
                package_name = close[0]

        self.package_name = package_name
        self.package_dir = package_search_dir / package_name

        if not os.path.exists(self.package_dir) and not created_package_dir:
            raise CommandError("Package directory did not exist at %s. Perhaps specify it using --name" % self.package_dir)

Response: Determine paths automatically and a little intelligently.


Instruction:
Given the following Python 3 function, write the documentation
def check_integrity(sakefile, settings):
    sprint = settings["sprint"]
    error = settings["error"]
    sprint("Call to check_integrity issued", level="verbose")
    if not sakefile:
        error("Sakefile is empty")
        return False
    # checking for duplicate targets
    if len(sakefile.keys()) != len(set(sakefile.keys())):
        error("Sakefile contains duplicate targets")
        return False
    for target in sakefile:
        if target == "all":
            if not check_target_integrity(target, sakefile["all"], all=True):
                error("Failed to accept target 'all'")
                return False
            continue
        if "formula" not in sakefile[target]:
            if not check_target_integrity(target, sakefile[target],
                                          meta=True):
                errmes = "Failed to accept meta-target '{}'".format(target)
                error(errmes)
                return False
            for atom_target in sakefile[target]:
                if atom_target == "help":
                    continue
                if not check_target_integrity(atom_target,
                                              sakefile[target][atom_target],
                                              parent=target):
                    errmes = "Failed to accept target '{}'\n".format(
                                                                atom_target)
                    error(errmes)
                    return False
            continue
        if not check_target_integrity(target, sakefile[target]):
            errmes = "Failed to accept target '{}'\n".format(target)
            error(errmes)
            return False
    return True

Response: Checks the format of the Sakefile to ensure it conforms to the specification of the current language.


Instruction:
Can you create a Python 3 function that
checks the integrity of a specific target.

Response:
def check_target_integrity(key, values, meta=False, all=False, parent=None):
    """
    Checks the integrity of a specific target. Gets called
    multiple times from check_integrity()

    Args:
        The target name
        The dictionary values of that target
        A boolean representing whether it is a meta-target
        A boolean representing whether it is the "all" target
        A string representing name of parent (default None)

    Returns:
        True is the target is conformant
        False if not
    """

    # logic to audit "all" target
    if all:
        if not values:
            print("Warning: target 'all' is empty")
        # will check if it has unrecognized target later
        return True

    errmes = "target '{}' is not allowed to be missing a help message\n"

    # logic to audit a meta-target
    if meta:
        # check if help is missing
        if "help" not in values:
            sys.stderr.write(errmes.format(key))
            return False
        # checking if empty
        if len(values.keys()) == 1:
            sys.stderr.write("Meta-target '{}' is empty\n".format(key))
            return False
        return True

    # logic to audit any other target
    expected_fields = ["dependencies", "help", "output", "formula"]
    expected_fields = set(expected_fields)
    try:
        our_keys_set = set(values.keys())
    except:
        sys.stderr.write("Error processing target '{}'\n".format(key))
        sys.stderr.write("Are you sure '{}' is a meta-target?\n".format(
                                                                     parent))
        sys.stderr.write("If it's not, it's missing a formula\n")
        return False
    ignored_fields = set([field for field in our_keys_set\
                          if field.strip().startswith("(ignore)")])
    difference = our_keys_set - expected_fields - ignored_fields
    if difference:
        print("The following fields were not recognized and will be ignored")
        for item in difference:
            print("  - " + item)
    if "help" not in values:
        sys.stderr.write(errmes.format(key))
        return False
    # can't be missing formula either
    if "formula" not in values:
        sys.stderr.write("Target '{}' is missing formula\n".format(key))
        return False
    return True


Instruction:
How would you code a function in Python 3 to
check if the current version of sake is in the from_store.

Response:
def check_shastore_version(from_store, settings):
    """
    This function gives us the option to emit errors or warnings
    after sake upgrades
    """
    sprint = settings["sprint"]
    error = settings["error"]

    sprint("checking .shastore version for potential incompatibilities",
           level="verbose")
    if not from_store or 'sake version' not in from_store:
        errmes = ["Since you've used this project last, a new version of ",
                  "sake was installed that introduced backwards incompatible",
                  " changes. Run 'sake clean', and rebuild before continuing\n"]
        errmes = " ".join(errmes)
        error(errmes)
        sys.exit(1)


Instruction:
Implement a function in Python 3 to
return sha1 hash of the file supplied as an argument

Response:
def get_sha(a_file, settings=None):
    """
    Returns sha1 hash of the file supplied as an argument
    """
    if settings:
        error = settings["error"]
    else:
        error = ERROR_FN
    try:
        BLOCKSIZE = 65536
        hasher = hashlib.sha1()
        with io.open(a_file, "rb") as fh:
            buf = fh.read(BLOCKSIZE)
            while len(buf) > 0:
                hasher.update(buf)
                buf = fh.read(BLOCKSIZE)
        the_hash = hasher.hexdigest()
    except IOError:
        errmes = "File '{}' could not be read! Exiting!".format(a_file)
        error(errmes)
        sys.exit(1)
    except:
        errmes = "Unspecified error returning sha1 hash. Exiting!"
        error(errmes)
        sys.exit(1)
    return the_hash


Instruction:
How would you explain what the following Python 3 function does
def write_shas_to_shastore(sha_dict):
    if sys.version_info[0] < 3:
        fn_open = open
    else:
        fn_open = io.open
    with fn_open(".shastore", "w") as fh:
        fh.write("---\n")
        fh.write('sake version: {}\n'.format(constants.VERSION))
        if sha_dict:
            fh.write(yaml.dump(sha_dict))
        fh.write("...")

Response:
Writes a sha1 dictionary stored in memory to
AttributeNames the. shastore file


Instruction:
Can you generate the documentation for the following Python 3 function
def take_shas_of_all_files(G, settings):
    global ERROR_FN
    sprint = settings["sprint"]
    error = settings["error"]
    ERROR_FN = error
    sha_dict = {}
    all_files = []
    for target in G.nodes(data=True):
        sprint("About to take shas of files in target '{}'".format(target[0]),
               level="verbose")
        if 'dependencies' in target[1]:
            sprint("It has dependencies", level="verbose")
            deplist = []
            for dep in target[1]['dependencies']:
                glist = glob.glob(dep)
                if glist:
                    for oneglob in glist:
                        deplist.append(oneglob)
                else:
                    deplist.append(dep)
            target[1]['dependencies'] = list(deplist)
            for dep in target[1]['dependencies']:
                sprint("  - {}".format(dep), level="verbose")
                all_files.append(dep)
        if 'output' in target[1]:
            sprint("It has outputs", level="verbose")
            for out in acts.get_all_outputs(target[1]):
                sprint("  - {}".format(out), level="verbose")
                all_files.append(out)
    if len(all_files):
        sha_dict['files'] = {}
        # check if files exist and de-dupe
        extant_files = []
        for item in all_files:
            if item not in extant_files and os.path.isfile(item):
                extant_files.append(item)
        pool = Pool()
        results = pool.map(get_sha, extant_files)
        pool.close()
        pool.join()
        for fn, sha in zip(extant_files, results):
            sha_dict['files'][fn] = {'sha': sha}
        return sha_dict
    sprint("No dependencies", level="verbose")

Response: Takes sha1 hash of all dependencies and outputs of all targets and returns a dictionary where the keys are the filenames and the values are the sha1 hash of the files and the values are the sha1 hash of the files and the values are the sha1 hash of the files and the sha1 hash of the files.


Instruction:
Explain what the following Python 3 code does
def needs_to_run(G, target, in_mem_shas, from_store, settings):
    force = settings["force"]
    sprint = settings["sprint"]

    if(force):
        sprint("Target rebuild is being forced so {} needs to run".format(target),
               level="verbose")
        return True
    node_dict = get_the_node_dict(G, target)
    if 'output' in node_dict:
        for output in acts.get_all_outputs(node_dict):
            if not os.path.isfile(output):
                outstr = "Output file '{}' is missing so it needs to run"
                sprint(outstr.format(output), level="verbose")
                return True
    if 'dependencies' not in node_dict:
        # if it has no dependencies, it always needs to run
        sprint("Target {} has no dependencies and needs to run".format(target),
               level="verbose")
        return True
    for dep in node_dict['dependencies']:
        # because the shas are updated after all targets build,
        # its possible that the dependency's sha doesn't exist
        # in the current "in_mem" dictionary. If this is the case,
        # then the target needs to run
        if ('files' in in_mem_shas and dep not in in_mem_shas['files'] or
            'files' not in in_mem_shas):
            outstr = "Dep '{}' doesn't exist in memory so it needs to run"
            sprint(outstr.format(dep), level="verbose")
            return True
        now_sha = in_mem_shas['files'][dep]['sha']
        if ('files' in from_store and dep not in from_store['files'] or
            'files' not in from_store):
            outst = "Dep '{}' doesn't exist in shastore so it needs to run"
            sprint(outst.format(dep), level="verbose")
            return True
        old_sha = from_store['files'][dep]['sha']
        if now_sha != old_sha:
            outstr = "There's a mismatch for dep {} so it needs to run"
            sprint(outstr.format(dep), level="verbose")
            return True
    sprint("Target '{}' doesn't need to run".format(target), level="verbose")
    return False

Response: Determines if a target needs to be run based on the current state of the target.


Instruction:
Explain what the following Python 3 code does
def run_commands(commands, settings):
    sprint = settings["sprint"]
    quiet = settings["quiet"]
    error = settings["error"]
    enhanced_errors = True
    the_shell = None
    if settings["no_enhanced_errors"]:
        enhanced_errors = False
    if "shell" in settings:
        the_shell = settings["shell"]
    windows_p = sys.platform == "win32"

    STDOUT = None
    STDERR = None
    if quiet:
        STDOUT = PIPE
        STDERR = PIPE

    commands = commands.rstrip()
    sprint("About to run commands '{}'".format(commands), level="verbose")
    if not quiet:
        sprint(commands)

    if the_shell:
        tmp = shlex.split(the_shell)
        the_shell = tmp[0]
        tmp = tmp[1:]
        if enhanced_errors and not windows_p:
            tmp.append("-e")
        tmp.append(commands)
        commands = tmp
    else:
        if enhanced_errors and not windows_p:
            commands = ["-e", commands]

    p = Popen(commands, shell=True, stdout=STDOUT, stderr=STDERR,
              executable=the_shell)
    out, err = p.communicate()
    if p.returncode:
        if quiet:
            error(err.decode(locale.getpreferredencoding()))
        error("Command failed to run")
        sys.exit(1)

Response: Runs the commands supplied as an argument


Instruction:
How would you code a function in Python 3 to
wrap function that sends to commands in a target s formula to run_commands

Response:
def run_the_target(G, target, settings):
    """
    Wrapper function that sends to commands in a target's 'formula'
    to run_commands()

    Args:
        The graph we are going to build
        The target to run
        The settings dictionary
    """
    sprint = settings["sprint"]
    sprint("Running target {}".format(target))
    the_formula = get_the_node_dict(G, target)["formula"]
    run_commands(the_formula, settings)


Instruction:
How would you code a function in Python 3 to
return a list of nodes that are the direct ancestors of all of the nodes given as an argument.

Response:
def get_direct_ancestors(G, list_of_nodes):
    """
    Returns a list of nodes that are the parents
    from all of the nodes given as an argument.
    This is for use in the parallel topo sort
    """
    parents = []
    for item in list_of_nodes:
        anc = G.predecessors(item)
        for one in anc:
            parents.append(one)
    return parents


Instruction:
Implement a function in Python 3 to
return a list of all sinks in a topo - sort graph.

Response:
def get_sinks(G):
    """
    A sink is a node with no children.
    This means that this is the end of the line,
    and it should be run last in topo sort. This
    returns a list of all sinks in a graph
    """
    sinks = []
    for node in G:
        if not len(list(G.successors(node))):
            sinks.append(node)
    return sinks


Instruction:
Write a Python 3 script for
returning a list of lists representing all the nodes that have no dependency relationship between any nodes in a layer.

Response:
def get_levels(G):
    """
    For the parallel topo sort to work, the targets have
    to be executed in layers such that there is no
    dependency relationship between any nodes in a layer.
    What is returned is a list of lists representing all
    the layers, or levels
    """
    levels = []
    ends = get_sinks(G)
    levels.append(ends)
    while get_direct_ancestors(G, ends):
        ends = get_direct_ancestors(G, ends)
        levels.append(ends)
    levels.reverse()
    return levels


Instruction:
Implement a Python 3 function for
removing redundant entries from the list of levels.

Response:
def remove_redundancies(levels):
    """
    There are repeats in the output from get_levels(). We
    want only the earliest occurrence (after it's reversed)
    """
    seen = []
    final = []
    for line in levels:
        new_line = []
        for item in line:
            if item not in seen:
                seen.append(item)
                new_line.append(item)
        final.append(new_line)
    return final


Instruction:
How would you explain what the following Python 3 function does
def parallel_run_these(G, list_of_targets, in_mem_shas, from_store,
                       settings, dont_update_shas_of):
    """
    The parallel equivalent of "run_this_target()"
    It receives a list of targets to execute in parallel.
    Unlike "run_this_target()" it has to update the shas
    (in memory and in the store) within the function.
    This is because one of the targets may fail but many can
    succeed, and those outputs need to be updated

    Args:
        G
        A graph
        A list of targets that we need to build in parallel
        The dictionary containing the in-memory sha store
        The dictionary containing the contents of the .shastore file
        The settings dictionary
        A list of outputs to not update shas of
    """
    verbose = settings["verbose"]
    quiet = settings["quiet"]
    error = settings["error"]
    sprint = settings["sprint"]

    if len(list_of_targets) == 1:
        target = list_of_targets[0]
        sprint("Going to run target '{}' serially".format(target),
               level="verbose")
        run_the_target(G, target, settings)
        node_dict = get_the_node_dict(G, target)
        if "output" in node_dict:
            for output in acts.get_all_outputs(node_dict):
                if output not in dont_update_shas_of:
                    in_mem_shas['files'][output] = {"sha": get_sha(output,
                                                                   settings)}
                    in_mem_shas[output] = get_sha(output, settings)
                    write_shas_to_shastore(in_mem_shas)
        if "dependencies" in node_dict:
            for dep in acts.get_all_dependencies(node_dict):
                if dep not in dont_update_shas_of:
                    in_mem_shas['files'][dep] = {"sha": get_sha(dep, settings)}
                    write_shas_to_shastore(in_mem_shas)
        return True
    a_failure_occurred = False
    out = "Going to run these targets '{}' in parallel"
    sprint(out.format(", ".join(list_of_targets)))
    info = [(target, get_the_node_dict(G, target))
              for target in list_of_targets]
    commands = [item[1]['formula'].rstrip() for item in info]
    if not quiet:
        procs = [Popen(command, shell=True) for command in commands]
    else:
        procs = [Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
                   for command in commands]
    for index, process in enumerate(procs):
        if process.wait():
            error("Target '{}' failed!".format(info[index][0]))
            a_failure_occurred = True
        else:
            if "output" in info[index][1]:
                for output in acts.get_all_outputs(info[index][1]):
                    if output not in dont_update_shas_of:
                        in_mem_shas['files'][output] = {"sha": get_sha(output,
                                                                       settings)}
                        write_shas_to_shastore(in_mem_shas)
            if "dependencies" in info[index][1]:
                for dep in acts.get_all_dependencies(info[index][1]):
                    if dep not in dont_update_shas_of:
                        in_mem_shas['files'][dep] = {"sha": get_sha(dep, settings)}
                        write_shas_to_shastore(in_mem_shas)
    if a_failure_occurred:
        error("A command failed to run")
        sys.exit(1)
    return True

Response: This function runs the target in parallel and updates the in - memory shas of the nodes in the store.


Instruction:
Write a Python 3 function for
merging the files in from_store and in_mem_shas with the shas in in_mem_shas.

Response:
def merge_from_store_and_in_mems(from_store, in_mem_shas, dont_update_shas_of):
    """
    If we don't merge the shas from the sha store and if we build a
    subgraph, the .shastore will only contain the shas of the files
    from the subgraph and the rest of the graph will have to be
    rebuilt
    """
    if not from_store:
        for item in dont_update_shas_of:
            if item in in_mem_shas['files']:
                del in_mem_shas['files'][item]
        return in_mem_shas
    for key in from_store['files']:
        if key not in in_mem_shas['files'] and key not in dont_update_shas_of:
            in_mem_shas['files'][key] = from_store['files'][key]
    for item in dont_update_shas_of:
        if item in in_mem_shas['files']:
            del in_mem_shas['files'][item]
    return in_mem_shas


Instruction:
Can you generate the documentation for the following Python 3 function
def build_this_graph(G, settings, dont_update_shas_of=None):
    verbose = settings["verbose"]
    quiet = settings["quiet"]
    force = settings["force"]
    recon = settings["recon"]
    parallel = settings["parallel"]
    error = settings["error"]
    sprint = settings["sprint"]

    if not dont_update_shas_of:
        dont_update_shas_of = []
    sprint("Checking that graph is directed acyclic", level="verbose")
    if not nx.is_directed_acyclic_graph(G):
        errmes = "Dependency resolution is impossible; "
        errmes += "graph is not directed and acyclic"
        errmes += "\nCheck the Sakefile\n"
        error(errmes)
        sys.exit(1)
    sprint("Dependency resolution is possible", level="verbose")
    in_mem_shas = take_shas_of_all_files(G, settings)
    from_store = {}
    if not os.path.isfile(".shastore"):
        write_shas_to_shastore(in_mem_shas)
        in_mem_shas = {}
        in_mem_shas['files'] = {}
    with io.open(".shastore", "r") as fh:
        shas_on_disk = fh.read()
    from_store = yaml.load(shas_on_disk)
    check_shastore_version(from_store, settings)
    if not from_store:
        write_shas_to_shastore(in_mem_shas)
        in_mem_shas = {}
        in_mem_shas['files'] = {}
        with io.open(".shastore", "r") as fh:
            shas_on_disk = fh.read()
        from_store = yaml.load(shas_on_disk)
    # parallel
    if parallel:
        for line in parallel_sort(G):
            line = sorted(line)
            out = "Checking if targets '{}' need to be run"
            sprint(out.format(", ".join(line)), level="verbose")
            to_build = []
            for item in line:
                if needs_to_run(G, item, in_mem_shas, from_store, settings):
                    to_build.append(item)
            if to_build:
                if recon:
                    if len(to_build) == 1:
                        out = "Would run target '{}'"
                        sprint(out.format(to_build[0]))
                    else:
                        out = "Would run targets '{}' in parallel"
                        sprint(out.format(", ".join(to_build)))
                    continue
                parallel_run_these(G, to_build, in_mem_shas, from_store,
                                   settings, dont_update_shas_of)
    # not parallel
    else:
        # still have to use parallel_sort to make
        # build order deterministic (by sorting targets)
        targets = []
        for line in parallel_sort(G):
            for item in sorted(line):
                targets.append(item)
        for target in targets:
            outstr = "Checking if target '{}' needs to be run"
            sprint(outstr.format(target), level="verbose")
            if needs_to_run(G, target, in_mem_shas, from_store, settings):
                if recon:
                    sprint("Would run target: {}".format(target))
                    continue
                run_the_target(G, target, settings)
                node_dict = get_the_node_dict(G, target)
                if "output" in node_dict:
                    for output in acts.get_all_outputs(node_dict):
                        if output not in dont_update_shas_of:
                            in_mem_shas['files'][output] = {"sha": get_sha(output,
                                                                           settings)}
                            write_shas_to_shastore(in_mem_shas)
                if "dependencies" in node_dict:
                    for dep in acts.get_all_dependencies(node_dict):
                        if dep not in dont_update_shas_of:
                            in_mem_shas['files'][dep] = {"sha": get_sha(dep,
                                                                        settings)}
                            write_shas_to_shastore(in_mem_shas)

    if recon:
        return 0
    in_mem_shas = take_shas_of_all_files(G, settings)
    if in_mem_shas:
        in_mem_shas = merge_from_store_and_in_mems(from_store, in_mem_shas,
                                                   dont_update_shas_of)
        write_shas_to_shastore(in_mem_shas)
    sprint("Done", color=True)
    return 0

Response: This function builds the tree from a graph G.


Instruction:
Write a Python 3 script to
return the print functions that can be used to print the current language.

Response:
def get_print_functions(settings):
    """
    This returns the appropriate print functions
    in a tuple
    The print function are:
        - sprint - for standard printing
        - warn - for warnings
        - error - for errors
    This will all be the same if color is False.

    The returned print functions will contain an optional parameter
    that specifies the output level (verbose or not). If not verbose,
    the print function will ignore the message.
    """
    verbose = settings["verbose"]
    # the regular print doesn't use color by default
    # (even if color is True)
    def sprint(message, level=None, color=False):
        if level=="verbose" and not verbose:
            return
        # for colors
        prepend = ""
        postfix = ""
        if settings["color"] and color:
            prepend = "\033[92m"
            postfix = "\033[0m"
        print("{}{}{}".format(prepend, message, postfix))
        sys.stdout.flush()
    def warn(message, level=None, color=True):
        if level=="verbose" and not verbose:
            return
        # for colors
        prepend = ""
        postfix = ""
        if settings["color"] and color:
            prepend = "\033[93m"
            postfix = "\033[0m"
        print("{}{}{}".format(prepend, message, postfix))
        sys.stdout.flush()
    def error(message, level=None, color=True):
        # this condition does really make any sense but w/e
        if level=="verbose" and not verbose:
            return
        # for colors
        prepend = ""
        postfix = ""
        if settings["color"] and color:
            prepend = "\033[91m"
            postfix = "\033[0m"
        print("{}{}{}".format(prepend, message, postfix), file=sys.stderr)
        sys.stderr.flush()
    return sprint, warn, error


Instruction:
How would you explain what the following Python 3 function does
def find_standard_sakefile(settings):
    error = settings["error"]
    if settings["customsake"]:
        custom = settings["customsake"]
        if not os.path.isfile(custom):
            error("Specified sakefile '{}' doesn't exist", custom)
            sys.exit(1)
        return custom
    # no custom specified, going over defaults in order
    for name in ["Sakefile", "Sakefile.yaml", "Sakefile.yml"]:
        if os.path.isfile(name):
            return name
    error("Error: there is no Sakefile to read")
    sys.exit(1)

Response: Returns the filename of the appropriate sakefile


Instruction:
Make a summary of the following Python 3 code
def clean_path(a_path, force_os=None, force_start=None):
    if not force_start:
        force_start = os.curdir
    if force_os == "windows":
        import ntpath
        return ntpath.relpath(ntpath.normpath(a_path),
                             start=force_start)
    if force_os == "posix":
        import posixpath
        return posixpath.relpath(posixpath.normpath(a_path),
                                start=force_start)
    return os.path.relpath(os.path.normpath(a_path),
                          start=force_start)

Response:
This function is used to normalize the path of an output or
    dependency and provide the path in relative form.


Instruction:
Implement a Python 3 function for
returning a prettily formatted help string for the current language of the current language.

Response:
def get_help(sakefile):
    """
    Returns the prettily formatted help strings (for printing)

    Args:
        A dictionary that is the parsed Sakefile (from sake.py)

    NOTE:
        the list sorting in this function is required for this
        function to be deterministic
    """
    full_string = "You can 'sake' one of the following...\n\n"
    errmes = "target '{}' is not allowed to not have help message\n"
    outerlines = []
    for target in sakefile:
        if target == "all":
            # this doesn't have a help message
            continue
        middle_lines = []
        if "formula" not in sakefile[target]:
            # this means it's a meta-target
            innerstr = "{}:\n  - {}\n\n".format(escp(target),
                                                sakefile[target]["help"])
            inner = []
            for atom_target in sakefile[target]:
                if atom_target == "help":
                    continue
                inner.append("    {}:\n      -  {}\n\n".format(escp(atom_target),
                                                               sakefile[target][atom_target]["help"]))
            if inner:
                innerstr += '\n'.join(sorted(inner))
            middle_lines.append(innerstr)
        else:
            middle_lines.append("{}:\n  - {}\n\n".format(escp(target),
                                                         sakefile[target]["help"]))
        if middle_lines:
            outerlines.append('\n'.join(sorted(middle_lines)))

    if outerlines:
        full_string += '\n'.join(sorted(outerlines))
    what_clean_does = "remove all targets' outputs and start from scratch"
    full_string += "\nclean:\n  -  {}\n\n".format(what_clean_does)
    what_visual_does = "output visual representation of project's dependencies"
    full_string += "visual:\n  -  {}\n".format(what_visual_does)
    full_string = re.sub("\n{3,}", "\n\n", full_string)
    return full_string


Instruction:
Can you tell what is the following Python 3 function doing
def check_for_dep_in_outputs(dep, verbose, G):
    if verbose:
        print("checking dep {}".format(dep))
    ret_list = []
    for node in G.nodes(data=True):
        if "output" not in node[1]:
            continue
        for out in node[1]['output']:
            if fnmatch.fnmatch(out, dep):
                ret_list.append(node[0])
                break
    return ret_list

Response: Function to help construct_graph() identify dependencies in outputs


Instruction:
How would you explain what the following Python 3 function does
def get_ties(G):
    # we are going to make a dictionary whose keys are every dependency
    # and whose values are a list of all targets that use that dependency.
    # after making the dictionary, values whose length is above one will
    # be called "ties"
    ties = []
    dep_dict = {}
    for node in G.nodes(data=True):
        if 'dependencies' in node[1]:
            for item in node[1]['dependencies']:
                if item not in dep_dict:
                    dep_dict[item] = []
                dep_dict[item].append(node[0])
    for item in dep_dict:
        if len(list(set(dep_dict[item]))) > 1:
            ties.append(list(set(dep_dict[item])))
    return ties

Response: Returns a list of all ties that are used by the dependency tree.


Instruction:
Can you implement a function in Python 3 that
constructs a NetworkX graph from a dictionary of sakefile.

Response:
def construct_graph(sakefile, settings):
    """
    Takes the sakefile dictionary and builds a NetworkX graph

    Args:
        A dictionary that is the parsed Sakefile (from sake.py)
        The settings dictionary

    Returns:
        A NetworkX graph
    """
    verbose = settings["verbose"]
    sprint = settings["sprint"]
    G = nx.DiGraph()
    sprint("Going to construct Graph", level="verbose")
    for target in sakefile:
        if target == "all":
            # we don't want this node
            continue
        if "formula" not in sakefile[target]:
            # that means this is a meta target
            for atomtarget in sakefile[target]:
                if atomtarget == "help":
                    continue
                sprint("Adding '{}'".format(atomtarget), level="verbose")
                data_dict = sakefile[target][atomtarget]
                data_dict["parent"] = target
                G.add_node(atomtarget, **data_dict)
        else:
            sprint("Adding '{}'".format(target), level="verbose")
            G.add_node(target, **sakefile[target])
    sprint("Nodes are built\nBuilding connections", level="verbose")
    for node in G.nodes(data=True):
        sprint("checking node {} for dependencies".format(node[0]),
               level="verbose")
        # normalize all paths in output
        for k, v in node[1].items():
            if v is None: node[1][k] = []
        if "output" in node[1]:
            for index, out in enumerate(node[1]['output']):
                node[1]['output'][index] = clean_path(node[1]['output'][index])
        if "dependencies" not in node[1]:
            continue
        sprint("it has dependencies", level="verbose")
        connects = []
        # normalize all paths in dependencies
        for index, dep in enumerate(node[1]['dependencies']):
            dep = os.path.normpath(dep)
            shrt = "dependencies"
            node[1]['dependencies'][index] = clean_path(node[1][shrt][index])
    for node in G.nodes(data=True):
        connects = []
        if "dependencies" not in node[1]:
            continue
        for dep in node[1]['dependencies']:
            matches = check_for_dep_in_outputs(dep, verbose, G)
            if not matches:
                continue
            for match in matches:
                sprint("Appending {} to matches".format(match), level="verbose")
                connects.append(match)
        if connects:
            for connect in connects:
                G.add_edge(connect, node[0])
    return G


Instruction:
Can you tell what is the following Python 3 function doing
def get_all_outputs(node_dict):
    outlist = []
    for item in node_dict['output']:
        glist = glob.glob(item)
        if glist:
            for oneglob in glist:
                outlist.append(oneglob)
        else:
            outlist.append(item)
    return outlist

Response: This function takes a node dictionary and returns a list of all outputs.


Instruction:
Write a Python 3 script for
returning a list of all the dependencies of a node.

Response:
def get_all_dependencies(node_dict):
    """
    ...............................
    """
    deplist = []
    for item in node_dict['dependencies']:
        glist = glob.glob(item)
        if glist:
            for oneglob in glist:
                deplist.append(oneglob)
        else:
            deplist.append(item)
    return deplist


Instruction:
Here you have a function in Python 3, explain what it does
def clean_all(G, settings):
    quiet = settings["quiet"]
    recon = settings["recon"]
    sprint = settings["sprint"]
    error = settings["error"]
    all_outputs = []
    for node in G.nodes(data=True):
        if "output" in node[1]:
            for item in get_all_outputs(node[1]):
                all_outputs.append(item)
    all_outputs.append(".shastore")
    retcode = 0
    for item in sorted(all_outputs):
        if os.path.isfile(item):
            if recon:
                sprint("Would remove file: {}".format(item))
                continue
            sprint("Attempting to remove file '{}'", level="verbose")
            try:
                os.remove(item)
                sprint("Removed file", level="verbose")
            except:
                errmes = "Error: file '{}' failed to be removed"
                error(errmes.format(item))
                retcode = 1
    if not retcode and not recon:
        sprint("All clean", color=True)
    return retcode

Response: Removes all the output files from all targets in the graph.


Instruction:
Given the following Python 3 function, write the documentation
def write_dot_file(G, filename):
    with io.open(filename, "w") as fh:
        fh.write("strict digraph DependencyDiagram {\n")
        edge_list = G.edges()
        node_list = set(G.nodes())
        if edge_list:
            for edge in sorted(edge_list):
                source, targ = edge
                node_list = node_list - set(source)
                node_list = node_list - set(targ)
                line = '"{}" -> "{}";\n'
                fh.write(line.format(source, targ))
        # draw nodes with no links
        if node_list:
            for node in sorted(node_list):
                line = '"{}"\n'.format(node)
                fh.write(line)
        fh.write("}")

Response: Writes the graph G in dot file format for graphviz visualization.


Instruction:
Here you have a function in Python 3, explain what it does
def visualize(G, settings, filename="dependencies", no_graphviz=False):
    error = settings["error"]
    if no_graphviz:
        write_dot_file(G, filename)
        return 0
    write_dot_file(G, "tempdot")
    renderer = "svg"
    if re.search("\.jpg$", filename, re.IGNORECASE):
        renderer = "jpg"
    elif re.search("\.jpeg$", filename, re.IGNORECASE):
        renderer = "jpg"
    elif re.search("\.svg$", filename, re.IGNORECASE):
        renderer = "svg"
    elif re.search("\.png$", filename, re.IGNORECASE):
        renderer = "png"
    elif re.search("\.gif$", filename, re.IGNORECASE):
        renderer = "gif"
    elif re.search("\.ps$", filename, re.IGNORECASE):
        renderer = "ps"
    elif re.search("\.pdf$", filename, re.IGNORECASE):
        renderer = "pdf"
    else:
        renderer = "svg"
        filename += ".svg"
    command = "dot -T{} tempdot -o {}".format(renderer, filename)
    p = Popen(command, shell=True)
    p.communicate()
    if p.returncode:
        errmes = "Either graphviz is not installed, or its not on PATH"
        os.remove("tempdot")
        error(errmes)
        sys.exit(1)
    os.remove("tempdot")
    return 0

Response: Visualize a NetworkX DiGraph into a graphviz dot file.


Instruction:
Write a Python 3 function for
preparing transcriptiondata from the transcription sources.

Response:
def _make_package(args):  # pragma: no cover
    """Prepare transcriptiondata from the transcription sources."""
    from lingpy.sequence.sound_classes import token2class
    from lingpy.data import Model

    columns = ['LATEX', 'FEATURES', 'SOUND', 'IMAGE', 'COUNT', 'NOTE']
    bipa = TranscriptionSystem('bipa')
    for src, rows in args.repos.iter_sources(type='td'):
        args.log.info('TranscriptionData {0} ...'.format(src['NAME']))
        uritemplate = URITemplate(src['URITEMPLATE']) if src['URITEMPLATE'] else None
        out = [['BIPA_GRAPHEME', 'CLTS_NAME', 'GENERATED', 'EXPLICIT',
                'GRAPHEME', 'URL'] + columns]
        graphemes = set()
        for row in rows:
            if row['GRAPHEME'] in graphemes:
                args.log.warn('skipping duplicate grapheme: {0}'.format(row['GRAPHEME']))
                continue
            graphemes.add(row['GRAPHEME'])
            if not row['BIPA']:
                bipa_sound = bipa[row['GRAPHEME']]
                explicit = ''
            else:
                bipa_sound = bipa[row['BIPA']]
                explicit = '+'
            generated = '+' if bipa_sound.generated else ''
            if is_valid_sound(bipa_sound, bipa):
                bipa_grapheme = bipa_sound.s
                bipa_name = bipa_sound.name
            else:
                bipa_grapheme, bipa_name = '<NA>', '<NA>'
            url = uritemplate.expand(**row) if uritemplate else row.get('URL', '')
            out.append(
                [bipa_grapheme, bipa_name, generated, explicit, row['GRAPHEME'],
                 url] + [
                    row.get(c, '') for c in columns])
        found = len([o for o in out if o[0] != '<NA>'])
        args.log.info('... {0} of {1} graphemes found ({2:.0f}%)'.format(
            found, len(out), found / len(out) * 100))
        with UnicodeWriter(
            pkg_path('transcriptiondata', '{0}.tsv'.format(src['NAME'])), delimiter='\t'
        ) as writer:
            writer.writerows(out)

    count = 0
    with UnicodeWriter(pkg_path('soundclasses', 'lingpy.tsv'), delimiter='\t') as writer:
        writer.writerow(['CLTS_NAME', 'BIPA_GRAPHEME'] + SOUNDCLASS_SYSTEMS)
        for grapheme, sound in sorted(bipa.sounds.items()):
            if not sound.alias:
                writer.writerow(
                    [sound.name, grapheme] + [token2class(
                        grapheme, Model(cls)) for cls in SOUNDCLASS_SYSTEMS])
                count += 1
    args.log.info('SoundClasses: {0} written to file.'.format(count))


Instruction:
How would you implement a function in Python 3 that
checks the consistency of a given transcription system conversino

Response:
def is_valid_sound(sound, ts):
    """Check the consistency of a given transcription system conversino"""
    if isinstance(sound, (Marker, UnknownSound)):
        return False
    s1 = ts[sound.name]
    s2 = ts[sound.s]
    return s1.name == s2.name and s1.s == s2.s


Instruction:
Given the following Python 3 function, write the documentation
def resolve_sound(self, sound):
        sound = sound if isinstance(sound, Sound) else self.system[sound]
        if sound.name in self.data:
            return '//'.join([x['grapheme'] for x in self.data[sound.name]])
        raise KeyError(":td:resolve_sound: No sound could be found.")

Response: Function tries to identify a sound in the data and returns the grapheme of that sound.


Instruction:
Can you tell what is the following Python 3 function doing
def _norm(self, string):
        nstring = norm(string)
        if "/" in string:
            s, t = string.split('/')
            nstring = t
        return self.normalize(nstring)

Response:
Extended normalization : normalize by list of norm - characers split
            by character "/".


Instruction:
Write a Python 3 function for
normalizing the string according to normalization list

Response:
def normalize(self, string):
        """Normalize the string according to normalization list"""
        return ''.join([self._normalize.get(x, x) for x in nfd(string)])


Instruction:
Write a Python 3 function that can
parse a sound from its name

Response:
def _from_name(self, string):
        """Parse a sound from its name"""
        components = string.split(' ')
        if frozenset(components) in self.features:
            return self.features[frozenset(components)]
        rest, sound_class = components[:-1], components[-1]
        if sound_class in ['diphthong', 'cluster']:
            if string.startswith('from ') and 'to ' in string:
                extension = {'diphthong': 'vowel', 'cluster': 'consonant'}[sound_class]
                string_ = ' '.join(string.split(' ')[1:-1])
                from_, to_ = string_.split(' to ')
                v1, v2 = frozenset(from_.split(' ') + [extension]), frozenset(
                    to_.split(' ') + [extension])
                if v1 in self.features and v2 in self.features:
                    s1, s2 = (self.features[v1], self.features[v2])
                    if sound_class == 'diphthong':
                        return Diphthong.from_sounds(s1 + s2, s1, s2, self)  # noqa: F405
                    else:
                        return Cluster.from_sounds(s1 + s2, s1, s2, self)  # noqa: F405
                else:
                    # try to generate the sounds if they are not there
                    s1, s2 = self._from_name(from_ + ' ' + extension), self._from_name(
                        to_ + ' ' + extension)
                    if not (isinstance(
                        s1, UnknownSound) or isinstance(s2, UnknownSound)):  # noqa: F405
                        if sound_class == 'diphthong':
                            return Diphthong.from_sounds(  # noqa: F405
                                s1 + s2, s1, s2, self)
                        return Cluster.from_sounds(s1 + s2, s1, s2, self)  # noqa: F405
                    raise ValueError('components could not be found in system')
            else:
                raise ValueError('name string is erroneously encoded')

        if sound_class not in self.sound_classes:
            raise ValueError('no sound class specified')

        args = {self._feature_values.get(comp, '?'): comp for comp in rest}
        if '?' in args:
            raise ValueError('string contains unknown features')
        args['grapheme'] = ''
        args['ts'] = self
        sound = self.sound_classes[sound_class](**args)
        if sound.featureset not in self.features:
            sound.generated = True
            return sound
        return self.features[sound.featureset]


Instruction:
How would you code a function in Python 3 to
parse a string and return its features.

Response:
def _parse(self, string):
        """Parse a string and return its features.

        :param string: A one-symbol string in NFD

        Notes
        -----
        Strategy is rather simple: we determine the base part of a string and
        then search left and right of this part for the additional features as
        expressed by the diacritics. Fails if a segment has more than one basic
        part.
        """
        nstring = self._norm(string)

        # check whether sound is in self.sounds
        if nstring in self.sounds:
            sound = self.sounds[nstring]
            sound.normalized = nstring != string
            sound.source = string
            return sound

        match = list(self._regex.finditer(nstring))
        # if the match has length 2, we assume that we have two sounds, so we split
        # the sound and pass it on for separate evaluation (recursive function)
        if len(match) == 2:
            sound1 = self._parse(nstring[:match[1].start()])
            sound2 = self._parse(nstring[match[1].start():])
            # if we have ANY unknown sound, we mark the whole sound as unknown, if
            # we have two known sounds of the same type (vowel or consonant), we
            # either construct a diphthong or a cluster
            if 'unknownsound' not in (sound1.type, sound2.type) and \
                    sound1.type == sound2.type:
                # diphthong creation
                if sound1.type == 'vowel':
                    return Diphthong.from_sounds(  # noqa: F405
                        string, sound1, sound2, self)
                elif sound1.type == 'consonant' and \
                        sound1.manner in ('stop', 'implosive', 'click', 'nasal') and \
                        sound2.manner in ('stop', 'implosive', 'affricate', 'fricative'):
                    return Cluster.from_sounds(  # noqa: F405
                        string, sound1, sound2, self)
            return UnknownSound(grapheme=nstring, source=string, ts=self)  # noqa: F405

        if len(match) != 1:
            # Either no match or more than one; both is considered an error.
            return UnknownSound(grapheme=nstring, source=string, ts=self)  # noqa: F405

        pre, mid, post = nstring.partition(nstring[match[0].start():match[0].end()])
        base_sound = self.sounds[mid]
        if isinstance(base_sound, Marker):  # noqa: F405
            assert pre or post
            return UnknownSound(grapheme=nstring, source=string, ts=self)  # noqa: F405

        # A base sound with diacritics or a custom symbol.
        features = attr.asdict(base_sound)
        features.update(
            source=string,
            generated=True,
            normalized=nstring != string,
            base=base_sound.grapheme)

        # we construct two versions: the "normal" version and the version where
        # we search for aliases and normalize them (as our features system for
        # diacritics may well define aliases
        grapheme, sound = '', ''
        for dia in [p + EMPTY for p in pre]:
            feature = self.diacritics[base_sound.type].get(dia, {})
            if not feature:
                return UnknownSound(  # noqa: F405
                    grapheme=nstring, source=string, ts=self)
            features[self._feature_values[feature]] = feature
            # we add the unaliased version to the grapheme
            grapheme += dia[0]
            # we add the corrected version (if this is needed) to the sound
            sound += self.features[base_sound.type][feature][0]
        # add the base sound
        grapheme += base_sound.grapheme
        sound += base_sound.s
        for dia in [EMPTY + p for p in post]:
            feature = self.diacritics[base_sound.type].get(dia, {})
            # we are strict: if we don't know the feature, it's an unknown
            # sound
            if not feature:
                return UnknownSound(  # noqa: F405
                    grapheme=nstring, source=string, ts=self)
            features[self._feature_values[feature]] = feature
            grapheme += dia[1]
            sound += self.features[base_sound.type][feature][1]

        features['grapheme'] = sound
        new_sound = self.sound_classes[base_sound.type](**features)
        # check whether grapheme differs from re-generated sound
        if text_type(new_sound) != sound:
            new_sound.alias = True
        if grapheme != sound:
            new_sound.alias = True
            new_sound.grapheme = grapheme
        return new_sound


Instruction:
How would you code a function in Python 3 to
function tries to identify a sound in the data and returns the next approximate sound class.

Response:
def resolve_sound(self, sound):
        """Function tries to identify a sound in the data.

        Notes
        -----
        The function tries to resolve sounds to take a sound with less complex
        features in order to yield the next approximate sound class, if the
        transcription data are sound classes.
        """
        sound = sound if isinstance(sound, Symbol) else self.system[sound]
        if sound.name in self.data:
            return self.data[sound.name]['grapheme']
        if not sound.type == 'unknownsound':
            if sound.type in ['diphthong', 'cluster']:
                return self.resolve_sound(sound.from_sound)
            name = [
                s for s in sound.name.split(' ') if
                self.system._feature_values.get(s, '') not in
                ['laminality', 'ejection', 'tone']]
            while len(name) >= 4:
                sound = self.system.get(' '.join(name))
                if sound and sound.name in self.data:
                    return self.resolve_sound(sound)
                name.pop(0)
        raise KeyError(":sc:resolve_sound: No sound could be found.")


Instruction:
Can you generate the documentation for the following Python 3 function
def ipfn_np(self, m, aggregates, dimensions, weight_col='total'):
        steps = len(aggregates)
        dim = len(m.shape)
        product_elem = []
        tables = [m]
        # TODO: do we need to persist all these dataframe? Or maybe we just need to persist the table_update and table_current
        # and then update the table_current to the table_update to the latest we have. And create an empty zero dataframe for table_update (Evelyn)
        for inc in range(steps - 1):
            tables.append(np.array(np.zeros(m.shape)))
        original = copy.copy(m)

        # Calculate the new weights for each dimension
        for inc in range(steps):
            if inc == (steps - 1):
                table_update = m
                table_current = tables[inc]
            else:
                table_update = tables[inc + 1]
                table_current = tables[inc]
            for dimension in dimensions[inc]:
                product_elem.append(range(m.shape[dimension]))
            for item in product(*product_elem):
                idx = self.index_axis_elem(dim, dimensions[inc], item)
                table_current_slice = table_current[idx]
                mijk = table_current_slice.sum()
                # TODO: Directly put it as xijk = aggregates[inc][item] (Evelyn)
                xijk = aggregates[inc]
                xijk = xijk[item]
                if mijk == 0:
                    # table_current_slice += 1e-5
                    # TODO: Basically, this part would remain 0 as always right? Cause if the sum of the slice is zero, then we only have zeros in this slice.
                    # TODO: you could put it as table_update[idx] = table_current_slice (since multiplication on zero is still zero)
                    table_update[idx] = table_current_slice
                else:
                    # TODO: when inc == steps - 1, this part is also directly updating the dataframe m (Evelyn)
                    # If we are not going to persist every table generated, we could still keep this part to directly update dataframe m
                    table_update[idx] = table_current_slice * 1.0 * xijk / mijk
                # For debug purposes
                # if np.isnan(table_update).any():
                #     print(idx)
                #     sys.exit(0)
            product_elem = []

        # Check the convergence rate for each dimension
        max_conv = 0
        for inc in range(steps):
            # TODO: this part already generated before, we could somehow persist it. But it's not important (Evelyn)
            for dimension in dimensions[inc]:
                product_elem.append(range(m.shape[dimension]))
            for item in product(*product_elem):
                idx = self.index_axis_elem(dim, dimensions[inc], item)
                ori_ijk = aggregates[inc][item]
                m_slice = m[idx]
                m_ijk = m_slice.sum()
                # print('Current vs original', abs(m_ijk/ori_ijk - 1))
                if abs(m_ijk / ori_ijk - 1) > max_conv:
                    max_conv = abs(m_ijk / ori_ijk - 1)

            product_elem = []

        return m, max_conv

Response: Runs the ipfn method from a matrix m and returns a numpy array with the result of the ipfn method.


Instruction:
Can you write a function in Python 3 where it
runs the ipfn method from a dataframe df and returns a dictionary of the unique entries.

Response:
def ipfn_df(self, df, aggregates, dimensions, weight_col='total'):
        """
        Runs the ipfn method from a dataframe df, aggregates/marginals and the dimension(s) preserved.
        For example:
        from ipfn import ipfn
        import pandas as pd
        age = [30, 30, 30, 30, 40, 40, 40, 40, 50, 50, 50, 50]
        distance = [10,20,30,40,10,20,30,40,10,20,30,40]
        m = [8., 4., 6., 7., 3., 6., 5., 2., 9., 11., 3., 1.]
        df = pd.DataFrame()
        df['age'] = age
        df['distance'] = distance
        df['total'] = m

        xip = df.groupby('age')['total'].sum()
        xip.loc[30] = 20
        xip.loc[40] = 18
        xip.loc[50] = 22
        xpj = df.groupby('distance')['total'].sum()
        xpj.loc[10] = 18
        xpj.loc[20] = 16
        xpj.loc[30] = 12
        xpj.loc[40] = 14
        dimensions = [['age'], ['distance']]
        aggregates = [xip, xpj]

        IPF = ipfn(df, aggregates, dimensions)
        df = IPF.iteration()

        print(df)
        print(df.groupby('age')['total'].sum(), xip)"""

        steps = len(aggregates)
        tables = [df]
        for inc in range(steps - 1):
            tables.append(df.copy())
        original = df.copy()

        # Calculate the new weights for each dimension
        inc = 0
        for features in dimensions:
            if inc == (steps - 1):
                table_update = df
                table_current = tables[inc]
            else:
                table_update = tables[inc + 1]
                table_current = tables[inc]

            tmp = table_current.groupby(features)[weight_col].sum()
            xijk = aggregates[inc]

            feat_l = []
            for feature in features:
                feat_l.append(np.unique(table_current[feature]))
            table_update.set_index(features, inplace=True)
            table_current.set_index(features, inplace=True)

            for feature in product(*feat_l):
                den = tmp.loc[feature]
                # calculate new weight for this iteration
                if den == 0:
                    table_update.loc[feature, weight_col] =\
                        table_current.loc[feature, weight_col] *\
                        xijk.loc[feature]
                else:
                    table_update.loc[feature, weight_col] = \
                        table_current.loc[feature, weight_col].astype(float) * \
                        xijk.loc[feature] / den

            table_update.reset_index(inplace=True)
            table_current.reset_index(inplace=True)
            inc += 1
            feat_l = []

        # Calculate the max convergence rate
        max_conv = 0
        inc = 0
        for features in dimensions:
            tmp = df.groupby(features)[weight_col].sum()
            ori_ijk = aggregates[inc]
            temp_conv = max(abs(tmp / ori_ijk - 1))
            if temp_conv > max_conv:
                max_conv = temp_conv
            inc += 1

        return df, max_conv


Instruction:
Can you write a function in Python 3 where it
renders link as HTML output tag <a.

Response:
def render(self, obj):
        """ Render link as HTML output tag <a>.
        """
        self.obj = obj
        attrs = ' '.join([
            '%s="%s"' % (attr_name, attr.resolve(obj))
            if isinstance(attr, Accessor)
            else '%s="%s"' % (attr_name, attr)
            for attr_name, attr in self.attrs.items()
        ])
        return mark_safe(u'<a %s>%s</a>' % (attrs, self.text))


Instruction:
Create a Python 3 function for
resolving the object described by the accessor by traversing the attributes of the given context.

Response:
def resolve(self, context, quiet=True):
        """
        Return an object described by the accessor by traversing the attributes
        of context.

        """
        try:
            obj = context
            for level in self.levels:
                if isinstance(obj, dict):
                    obj = obj[level]
                elif isinstance(obj, list) or isinstance(obj, tuple):
                    obj = obj[int(level)]
                else:
                    if callable(getattr(obj, level)):
                        try:
                            obj = getattr(obj, level)()
                        except KeyError:
                            obj = getattr(obj, level)
                    else:
                        # for model field that has choice set
                        # use get_xxx_display to access
                        display = 'get_%s_display' % level
                        obj = getattr(obj, display)() if hasattr(obj, display) else getattr(obj, level)
                if not obj:
                    break
            return obj
        except Exception as e:
            if quiet:
                return ''
            else:
                raise e


Instruction:
Create a Python 3 function for
returning a list of all the header rows in order of the entries in the table.

Response:
def header_rows(self):
        """
        [ [header1], [header3, header4] ]
        """
        # TO BE FIX: refactor
        header_rows = []
        headers = [col.header for col in self.columns]
        for header in headers:
            if len(header_rows) <= header.row_order:
                header_rows.append([])
            header_rows[header.row_order].append(header)
        return header_rows


Instruction:
Write a Python 3 function for
getting context data for datatable server - side response.

Response:
def get_context_data(self, **kwargs):
        """
        Get context data for datatable server-side response.
        See http://www.datatables.net/usage/server-side
        """
        sEcho = self.query_data["sEcho"]

        context = super(BaseListView, self).get_context_data(**kwargs)
        queryset = context["object_list"]
        if queryset is not None:
            total_length = self.get_queryset_length(queryset)
            queryset = self.filter_queryset(queryset)
            display_length = self.get_queryset_length(queryset)

            queryset = self.sort_queryset(queryset)
            queryset = self.paging_queryset(queryset)
            values_list = self.convert_queryset_to_values_list(queryset)
            context = {
                "sEcho": sEcho,
                "iTotalRecords": total_length,
                "iTotalDisplayRecords": display_length,
                "aaData": values_list,
            }
        else:
            context = {
                "sEcho": sEcho,
                "iTotalRecords": 0,
                "iTotalDisplayRecords": 0,
                "aaData": [],
            }
        return context


Instruction:
Here you have a function in Python 3, explain what it does
def get_days_span(self, month_index):
        is_first_month = month_index == 0
        is_last_month = month_index == self.__len__() - 1

        y = int(self.start_date.year + (self.start_date.month + month_index) / 13)
        m = int((self.start_date.month + month_index) % 12 or 12)
        total = calendar.monthrange(y, m)[1]

        if is_first_month and is_last_month:
            return (self.end_date - self.start_date).days + 1
        else:
            if is_first_month:
                return total - self.start_date.day + 1
            elif is_last_month:
                return self.end_date.day
            else:
                return total

Response: Calculate how many days the month spans.


Instruction:
Can you write a function in Python 3 where it
calculates an IEEE 754 float from an array of 4 bytes

Response:
def _calculate_float(self, byte_array):
        """Returns an IEEE 754 float from an array of 4 bytes

        :param byte_array: Expects an array of 4 bytes

        :type byte_array: array

        :rtype: float
        """
        if len(byte_array) != 4:
            return None

        return struct.unpack('f', struct.pack('4B', *byte_array))[0]


Instruction:
Explain what the following Python 3 code does
def _calculate_period(self, vals):
        ''' calculate the sampling period in seconds '''
        if len(vals) < 4:
            return None

        if self.firmware['major'] < 16:
            return ((vals[3] << 24) | (vals[2] << 16) | (vals[1] << 8) | vals[0]) / 12e6
        else:
            return self._calculate_float(vals)

Response: calculate the sampling period in seconds


Instruction:
Can you generate a brief explanation for the following Python 3 code
def wait(self, **kwargs):

        if not callable(self.on):
            raise UserWarning('Your device does not support the self.on function, try without wait')

        if not callable(self.histogram):
            raise UserWarning('Your device does not support the self.histogram function, try without wait')

        self.on()
        while True:
            try:
                if self.histogram() is None:
                    raise UserWarning('Could not load histogram, perhaps the device is not yet connected')

            except UserWarning as e:
                sleep(kwargs.get('check', 200) / 1000.)

        return self

Response: Wait for the OPC to prepare itself for data transmission.


Instruction:
How would you explain what the following Python 3 function does
def calculate_bin_boundary(self, bb):

        return min(enumerate(OPC_LOOKUP), key = lambda x: abs(x[1] - bb))[0]

Response: Calculates the adc value that corresponds to a specific bin boundary diameter in microns.