Instruction:
Write a Python 3 script for
reading the directives and generates source code for custom imports.

Response:
def _get_imports(self):
    """Reads the directives and generates source code for custom imports."""
    import_directives = [d for d in self.directives if d.name == "import"]
    if import_directives:
      return "\n" + "\n".join(d.args["value"] for d in import_directives)
    else:
      return ""


Instruction:
Can you generate the documentation for the following Python 3 function
def _get_token_type_enum(self):
    fmt = "class TokenType(Enum):\n" \
          "{indent}\"\"\"The token types for parse nodes generated by the Parser.\"\"\"\n" \
          "{indent}" + \
          "\n{indent}".join("{1} = {0}".format(num + 1, r.name) for num, r in enumerate(self.rules))
    return fmt.format(indent=self.indent)

Response: Builds the python source code for the Parser TokenType enum.


Instruction:
Make a summary of the following Python 3 code
def _get_class_definition(self):
    fmt = """class Parser({parser_base}):
             {indent}\"\"\"This class contains methods for reading source code and generating a parse tree.\"\"\"
             {indent}entry_point = "{entry_point}"

             {rule_definitions}
             """
    fmt = self._clean_fmt(fmt)
    return fmt.format(parser_base=self._get_parser_base(),
                      indent=self.indent,
                      entry_point=self._get_entry_point(),
                      rule_definitions="\n".join(self._get_rule_definitions()))

Response: Builds the class definition of the parser.


Instruction:
Can you generate the documentation for the following Python 3 function
def _get_entry_point(self):
    ep = self._find_directive("entry_point")
    if ep:
      return ep.args["value"]
    else:
      return self.rules[0].name

Response: Gets the entry_point value for the parser.


Instruction:
Make a summary of the following Python 3 code
def _get_rule_definition(self, rule):
    fmt = """def {rule_fxn_name}(self, text):
             {indent}\"\"\"{rule_source}\"\"\"
             {indent}self._attempting(text)
             {indent}return {rule_definition}(text){transform}
          """
    fmt = self._clean_fmt(fmt)
    source = self._indent(self._ast_to_code(rule.expression), skip_first_line=True)

    # All the primitives will accept a string x in place of terminal(x). This is terminal shorthand.
    # However, if a rule is only a wrapper around a single terminal, we have to actually make a
    # terminal call. This handles that situation.
    if self.use_terminal_shorthand and len(source) == 1 and source[0].startswith(("'", '"')):
      source = ["terminal({})".format(source[0])]

    rule_source = fmt.format(rule_fxn_name=self._get_rule_fxn_name(rule.name),
                             indent=self.indent,
                             rule_source=self._get_rule_source(rule),
                             rule_definition="\n".join(source),
                             transform=self._get_rule_transform(rule))
    return self._indent(rule_source, 1)

Response: Generates the source code for a rule.


Instruction:
Explain what the following Python 3 code does
def _get_rule_source(self, rule):
    p = len(self.input_source) + rule.position
    source = self.input_source[p:p + rule.consumed].rstrip()
    return self._indent(source, depth=self.indent + "   ", skip_first_line=True)

Response: Gets the variable part of the source code for a rule.


Instruction:
Make a summary of the following Python 3 code
def _get_rule_transform(self, rule):
    rd = self._find_directive(lambda d: d.name == "rule" and d.args.get("name") == rule.name)

    if rd:
      args = rd.args
    else:
      args = {}

    transform = args.get("transform", "retype")

    if transform == "retype":
      new_name = args.get("to_type", "TokenType.{0}".format(rule.name))
      return ".retyped({0})".format(new_name)
    elif transform == "compress":
      new_name = args.get("to_type", "TokenType.{0}".format(rule.name))
      if new_name == "identity":
        return ".compressed()"
      else:
        return ".compressed({0})".format(new_name)
    elif transform == "identity":
      return ""

Response: Returns the source code text for accomplishing the given rule.


Instruction:
Can you tell what is the following Python 3 function doing
def _expression_to_asn(self, expression):
    new_children = [self._node_to_asn(c) for c in expression.children]
    return self._remove_grouping_groups(infix_to_optree(new_children))

Response: Convert an expression to an Abstract Syntax Tree Node.


Instruction:
Can you generate the documentation for the following Python 3 function
def _node_to_asn(self, node):
    if node.is_type(TokenType.identifier):
      return Identifier(node.svalue)

    elif node.is_type(TokenType.terminal):
      return Terminal(node.svalue)

    elif node.is_type(TokenType.option_group):
      expr = node.children[0]
      return OptionGroup(self._expression_to_asn(expr))

    elif node.is_type(TokenType.repetition_group):
      expr = node.children[0]
      return RepetitionGroup(self._expression_to_asn(expr))

    elif node.is_type(TokenType.grouping_group):
      expr = node.children[0]
      return GroupingGroup(self._expression_to_asn(expr))

    elif node.is_type(TokenType.special_handling):
      ident = node.children[0]
      return SpecialHandling(ident)

    elif node.is_type(TokenType.number):
      return Number(node.svalue)

    elif node.is_type((TokenType.operator, TokenType.op_mult, TokenType.op_add)):
      return OperatorNode(OPERATOR_INDEX[node.svalue], node.position)

    else:
      raise Exception("Unhandled parse tree node: {0}".format(node))

Response: Convert a parse tree node into an absract syntax tree node.


Instruction:
Implement a function in Python 3 to
flatten a list of operands based on a pred.

Response:
def _hoist_operands(self, operands, pred):
    """Flattens a list of optree operands based on a pred.

    This is used to convert concatenation([x, concatenation[y, ...]]) (or alternation) to
    concatenation([x, y, ...]).
    """
    hopper = list(operands)
    new_operands = []
    while hopper:
      target = hopper.pop(0)
      if pred(target):
        hopper = list(target.operands) + hopper
      else:
        new_operands.append(target)
    return new_operands


Instruction:
Write a Python 3 script for
removing grouping groups from an OptreeNode.

Response:
def _remove_grouping_groups(self, optree):
    """Grouping groups are implied by optrees, this function hoists grouping group expressions up
    to their parent node.
    """
    new_operands = []
    for operand in optree.operands:
      if isinstance(operand, OptreeNode):
        new_operands.append(self._remove_grouping_groups(operand))
      elif isinstance(operand, GroupingGroup):
        new_operands.append(operand.expression)
      else:
        new_operands.append(operand)

    return OptreeNode(optree.opnode, new_operands)


Instruction:
How would you explain what the following Python 3 function does
def _ast_to_code(self, node, **kwargs):
    if isinstance(node, OptreeNode):
      return self._ast_optree_node_to_code(node, **kwargs)
    elif isinstance(node, Identifier):
      return self._ast_identifier_to_code(node, **kwargs)
    elif isinstance(node, Terminal):
      return self._ast_terminal_to_code(node, **kwargs)
    elif isinstance(node, OptionGroup):
      return self._ast_option_group_to_code(node, **kwargs)
    elif isinstance(node, RepetitionGroup):
      return self._ast_repetition_group_to_code(node, **kwargs)
    elif isinstance(node, SpecialHandling):
      return self._ast_special_handling_to_code(node, **kwargs)
    elif isinstance(node, Number):
      return self._ast_number_to_code(node, **kwargs)
    else:
      raise Exception("Unhandled ast node: {0}".format(node))

Response: Convert an abstract syntax tree to python source code.


Instruction:
Can you create a Python 3 function that
converts an abstract syntax operator tree to python source code.

Response:
def _ast_optree_node_to_code(self, node, **kwargs):
    """Convert an abstract syntax operator tree to python source code."""
    opnode = node.opnode
    if opnode is None:
      return self._ast_to_code(node.operands[0])
    else:
      operator = opnode.operator
      if operator is OP_ALTERNATE:
        return self._ast_op_alternate_to_code(node, **kwargs)
      elif operator is OP_WS_CONCAT:
        kwargs["ignore_whitespace"] = False
        return self._ast_op_concat_to_code(node, **kwargs)
      elif operator is OP_CONCAT:
        kwargs["ignore_whitespace"] = True
        return self._ast_op_concat_to_code(node, **kwargs)
      elif operator is OP_EXCLUDE:
        return self._ast_op_exclude_to_code(node, **kwargs)
      elif operator is OP_MULTIPLY:
        return self._ast_op_multiply_to_code(node, **kwargs)
      elif operator is OP_REPEAT:
        return self._ast_op_repeat_to_code(node, **kwargs)
      else:
        raise Exception("Unhandled optree node: {0}".format(node))


Instruction:
Here you have a function in Python 3, explain what it does
def _ast_terminal_to_code(self, terminal, **kwargs):
    value = _replace(terminal.value)
    if self.use_terminal_shorthand:
      return [value]
    else:
      return ["terminal({})".format(value)]

Response: Convert an AST terminal to python source code.


Instruction:
Create a Python 3 function to
convert an AST option group to python source code.

Response:
def _ast_option_group_to_code(self, option_group, **kwargs):
    """Convert an AST option group to python source code."""
    lines = ["option("]
    lines.extend(self._indent(self._ast_to_code(option_group.expression)))
    lines.append(")")
    return lines


Instruction:
Can you generate the documentation for the following Python 3 function
def _ast_repetition_group_to_code(self, repetition_group, ignore_whitespace=False, **kwargs):
    lines = ["zero_or_more("]
    lines.extend(self._indent(self._ast_to_code(repetition_group.expression)))
    lines[-1] += ","
    lines.append(self._indent("ignore_whitespace={}".format(bool(ignore_whitespace))))
    lines.append(")")
    return lines

Response: Convert an AST repetition group to python source code.


Instruction:
Make a summary of the following Python 3 code
def _ast_special_handling_to_code(self, special_handling, **kwargs):
    ident = special_handling.value.svalue
    if ident in PB_SPECIAL_HANDLING:
      return ["PB.{0}".format(ident)]
    else:
      return ["self.{0}".format(ident)]

Response: Convert an AST sepcial handling to python source code.


Instruction:
Can you create a Python 3 function that
converts an AST alternate op to python source code.

Response:
def _ast_op_alternate_to_code(self, opr, **kwargs):
    """Convert an AST alternate op to python source code."""
    hoist_target = OP_ALTERNATE
    operands = self._hoist_operands(opr.operands, lambda t: isinstance(t, OptreeNode) and t.opnode.operator is hoist_target)

    lines = ["alternation(["]
    for op in operands:
      lines.extend(self._indent(self._ast_to_code(op)))
      lines[-1] += ","
    lines.append("])")
    return lines


Instruction:
Make a summary of the following Python 3 code
def _ast_op_concat_to_code(self, opr, *, ignore_whitespace, **kwargs):
    hoist_target = OP_CONCAT if ignore_whitespace else OP_WS_CONCAT
    operands = self._hoist_operands(opr.operands, lambda t: isinstance(t, OptreeNode) and t.opnode.operator is hoist_target)

    lines = ["concatenation(["]
    for op in operands:
      lines.extend(self._indent(self._ast_to_code(op, ignore_whitespace=ignore_whitespace)))
      lines[-1] += ","
    lines.append("], ignore_whitespace={})".format(bool(ignore_whitespace)))
    return lines

Response: Convert an AST concatenate op to python source code.


Instruction:
How would you implement a function in Python 3 that
converts an AST exclude op to python source code.

Response:
def _ast_op_exclude_to_code(self, opr, **kwargs):
    """Convert an AST exclude op to python source code."""
    opl, opr = opr.operands

    lines = ["exclusion("]
    lines.extend(self._indent(self._ast_to_code(opl)))
    lines[-1] += ","
    lines.extend(self._indent(self._ast_to_code(opr)))
    lines.append(")")
    return lines


Instruction:
Can you tell what is the following Python 3 function doing
def _ast_op_multiply_to_code(self, opr, ignore_whitespace=False, **kwargs):
    opl, opr = opr.operands

    if isinstance(opl, Number):
      times = opl.value
      subject = self._ast_to_code(opr)
    else:
      times = opr.value
      subject = self._ast_to_code(opl)

    lines = ["repeated("]
    lines.extend(self._indent(subject))
    lines[-1] += ","
    lines.append("{0}times={1},".format(self.indent, times))
    lines.append("{0}ignore_whitespace={1}".format(self.indent, bool(ignore_whitespace)))
    lines.append(")")
    return lines

Response: Convert an AST multiply op to python source code.


Instruction:
Create a Python 3 function for
converting an AST repeat op to python source code.

Response:
def _ast_op_repeat_to_code(self, opr, ignore_whitespace=False, **kwargs):
    """Convert an AST repeat op to python source code."""
    lines = ["one_or_more("]
    lines.extend(self._indent(self._ast_to_code(opr.operands[0])))
    lines[-1] += ","
    lines.append(self._indent("ignore_whitespace={}".format(bool(ignore_whitespace))))
    lines.append(")")
    return lines


Instruction:
Here you have a function in Python 3, explain what it does
def _indent(self, text, depth=1, *, skip_first_line=False, suffix=""):
    as_list = isinstance(text, list)

    if as_list:
      lines = text
    else:
      lines = text.split("\n")
    new_lines = []

    if isinstance(depth, int):
      spacing = self.indent * depth
    else:
      spacing = depth

    for i, line in enumerate(lines):
      if skip_first_line and i == 0:
        new_lines.append("{0}{1}".format(line, suffix))
      else:
        new_lines.append("{0}{1}{2}".format(spacing, line, suffix))

    if as_list:
      return new_lines
    else:
      return "\n".join(new_lines)

Response: Indent text by depth and suffix.


Instruction:
Can you generate the documentation for the following Python 3 function
def _find_directives(self, pred):
    if isinstance(pred, str):
      return [d for d in self.directives if d.name == pred]
    else:
      return [d for d in self.directives if pred(d)]

Response: Finds all directives with a certain name or that passes a predicate.


Instruction:
Here you have a function in Python 3, explain what it does
def _flatten(child, parent):
    return parent.is_type(TokenType.expression) and child.node_type == parent.node_type

Response: Custom flattening method for the parse tree.


Instruction:
Here you have a function in Python 3, explain what it does
def directives_from_comment(cls, comment):
    comment_contents = comment.value[2:-2].strip()
    comment_lines = (l.strip() for l in comment_contents.split("\n"))
    directives = (l[1:].strip() for l in comment_lines if l.startswith("!"))

    for directive_def in directives:
      yield cls.parse_directive_def(directive_def)

Response: A generator that yields all directives in a comment.


Instruction:
Write a Python 3 function for
turning a directive definition string into a Directive object.

Response:
def parse_directive_def(cls, directive_def):
    """Turns a directive definition string into a directive object."""
    name, *kwargs = esc_split(directive_def, ignore_empty=True)
    return Directive(name, {key: value for key, value in (esc_split(arg, "=") for arg in kwargs)})


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_arguments(self):
        ApiCli.get_arguments(self)
        if self.args.hostGroupName is not None:
            self.url_parameters = {"name": self.args.hostGroupName}

Response: Extracts the specific arguments of this CLI


Instruction:
Implement a function in Python 3 to
extract the specific arguments of this CLI

Response:
def get_arguments(self):
        """
        Extracts the specific arguments of this CLI
        """
        ApiCli.get_arguments(self)
        if self.args.plugin_name is not None:
            self.plugin_name = self.args.plugin_name

        self.path = "v1/plugins/{0}".format(self.plugin_name)


Instruction:
Given the following Python 3 function, write the documentation
def get_arguments(self):
        ApiCli.get_arguments(self)
        self._alarm_id = self.args.alarm_id if self.args.alarm_id is not None else None

Response: Extracts the specific arguments of this CLI


Instruction:
Create a Python 3 function to
handle the results of the API call to get the current user s ID and the user s name.

Response:
def _handle_results(self):
        """
        Handle the results of the API call
        """

        # Only process if we get HTTP return code other 200.
        if self._api_result.status_code != requests.codes.ok:
            print(self.colorize_json(self._api_result.text))


Instruction:
Write a Python 3 function that can
convert a key to a string.

Response:
def key_to_str(modifiers, key, mods_table = mods, key_table = wx, key_prefix = 'WXK_'):
 """
 Returns a human-readable version of numerical modifiers and key.

 To make the key suitable for global hotkey usage, supply:
 mods_table = global_mods, key_table = win32con, key_prefix = 'VK_'
 """
 logger.debug('Converting (%s, %s) to string.', modifiers, key)
 if not key:
  key_str = 'NONE'
 else:
  key_str = None
 res = ''
 for value, name in mods_table.items():
  if (modifiers & value):
   res += name + '+'
 for x in dir(key_table):
  if x.startswith(key_prefix):
   if getattr(key_table, x) == key:
    key_str = converts.get(x, x[len(key_prefix):])
 if not key_str:
  key_str = chr(key)
 res += key_str
 logger.debug('Final result: %s.', res)
 return res


Instruction:
Can you implement a function in Python 3 that
converts a string like CTRL_ALT + K into a key.

Response:
def str_to_key(value, key_table = wx, accel_format = 'ACCEL_%s', key_format = 'WXK_%s', key_transpositions = {}):
 """
 Turns a string like "CTRL_ALT+K" into (3, 75).

 To get a global hotkey, try passing:
 key_table = win32con, accel_format = 'MOD_%s', key_format = 'VK_%s', key_transpositions = {'CTRL': 'CONTROL'}
 """
 logger.debug('Converting "%s" to integers.', value)
 modifiers = 0
 key = 0
 split = value.split('+')
 for v in split:
  v = v.upper()
  a = accel_format % key_transpositions.get(v, v)
  logger.debug('Accelerator format = %s.', a)
  k = key_format % key_transpositions.get(v, v)
  logger.debug('Key format = %s.', k)
  if hasattr(key_table, a):
   logger.debug('Found accelerator on %r.', key_table)
   modifiers = modifiers | getattr(key_table, a)
  elif hasattr(key_table, k):
   logger.debug('Found key on %r.', key_table)
   if key:
    raise ValueError('Multiple keys specified.')
   else:
    key = getattr(key_table, k)
 if not key:
  logger.debug('No key yet, falling back to ord.')
  key = ord(split[-1])
 logger.debug('modifiers = %d, key = %d.', modifiers, key)
 return (modifiers, key)


Instruction:
Implement a Python 3 function for
getting a new id if the provided one is None.

Response:
def get_id(id):
 """Get a new id if the provided one is None."""
 if id == None:
  id = wx.NewId()
  logger.debug('Generated new ID %s.', id)
 else:
  logger.debug('Using provided id %s.', id)
 return id


Instruction:
Create a Python 3 function for
adding a key to the accelerator list.

Response:
def add_accelerator(control, key, func, id = None):
 """
 Adds a key to the control.

 control: The control that the accelerator should be added to.
 key: A string like "CTRL+F", or "CMD+T" that specifies the key to use.
 func: The function that should be called when key is pressed.
 id: The id to Bind the event to. Defaults to wx.NewId().
 """
 logger.debug('Adding key "%s" to control %s to call %s.', key, control, func)
 id = get_id(id)
 control.Bind(wx.EVT_MENU, func, id = id)
 t = _tables.get(control, [])
 modifiers, key_int = str_to_key(key)
 t.append((modifiers, key_int, id))
 _tables[control] = t
 update_accelerators(control)
 return id


Instruction:
Write a Python 3 script to
remove an accelerator from a control.

Response:
def remove_accelerator(control, key):
 """
 Removes an accelerator from control.

 control: The control to affect.
 key: The key to remove.
 """
 key = str_to_key(key)
 t = _tables.get(control, [])
 for a in t:
  if a[:2] == key:
   t.remove(a)
   if t:
    _tables[control] = t
   else:
    del _tables[control]
   update_accelerators(control)
   return True
 return False


Instruction:
Given the following Python 3 function, write the documentation
def add_hotkey(control, key, func, id = None):
 if win32con is None:
  raise RuntimeError('win32con is not available.')
 logger.debug('Adding hotkey "%s" to control %s to call %s.', key, control, func)
 modifiers, keycode = str_to_key(key, key_table = win32con, accel_format = 'MOD_%s', key_format = 'VK_%s', key_transpositions = {'CTRL': 'CONTROL'})
 id = get_id(id)
 control.Bind(wx.EVT_HOTKEY, func, id = id)
 l = _hotkeys.get(control, [])
 l.append([key, id])
 _hotkeys[control] = l
 return control.RegisterHotKey(id, modifiers, keycode)

Response: Add a hotkey to a control via id.


Instruction:
Write a Python 3 function for
removing a global hotkey.

Response:
def remove_hotkey(control, key):
 """
 Remove a global hotkey.

 control - The control to affect
 key - The key to remove.
 """
 l = _hotkeys.get(control, [])
 for a in l:
  key_str, id = a
  if key_str == key:
   control.Unbind(wx.EVT_HOTKEY, id = id)
   control.UnregisterHotKey(id)
   l.remove(a)
   if l:
    _hotkeys[control] = l
   else:
    del _hotkeys[control]


Instruction:
Here you have a function in Python 3, explain what it does
def add_arguments(self):
        self.add_logging_argument()
        self.parser.add_argument('-a', '--api-host', dest='api_host', action='store', metavar="api_host",
                                 help='{0} API host endpoint'.format(self.product_name))
        self.parser.add_argument('-e', '--email', dest='email', action='store', metavar="e_mail",
                                 help='e-mail that has access to the {0} account'.format(self.product_name))
        self.parser.add_argument('-t', '--api-token', dest='api_token', required=False, action='store',
                                 metavar="api_token",
                                 help='API token for given e-mail that has access to the {0} account'.format(
                                     self.product_name))
        self.parser.add_argument('-z', '--curl', dest='curl', required=False, action='store_true', default=False,
                                 help='Output the corresponding curl command line and exit')

Response: Configure handling of command line arguments.


Instruction:
Can you implement a function in Python 3 that
configures logging based on command line options

Response:
def _configure_logging(self):
        """
        Configure logging based on command line options
        """
        if self.args.logLevel is not None:
            logging.basicConfig(level=self.levels[self.args.logLevel])
        logging.info("Set logging level to {0}".format(self.args.logLevel))


Instruction:
Make a summary of the following Python 3 code
def _validate_arguments(self):
        if self._email is None:
            self.set_error_message("E-mail for the account not provided")
            return False
        if self._api_token is None:
            self.set_error_message("API Token for the account not provided")
            return False
        return True

Response:
Validate the command line arguments passed to the CLI
            class.


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

        # Set default arguments from environment variables
        self._get_environment()

        # Call our member function to add command line arguments, child classes that override need
        # to call the ApiCli version first to add standard arguments
        self.add_arguments()

        # Parse the command line arguments
        self._parse_args()

        # Arguments are parsed call back to the instance so that it can extract the command line
        # arguments for its use
        self.get_arguments()

        self.get_api_parameters()
        if self._validate_arguments():
            if self._curl:
                self._curl_output()
            else:
                self._call_api()
                self._handle_results()
        else:
            print(self._message)

Response: Run the steps to execute the CLI


Instruction:
Can you generate a brief explanation for the following Python 3 code
def infix_to_postfix(nodes, *, recurse_types=None):
  output = []
  operators = []

  for node in nodes:
    if isinstance(node, OperatorNode):
      # Drain out all operators whose precedence is gte the node's...
      cmp_operator = node.operator
      while operators:
        current_operator = operators[-1].operator
        if current_operator.precedence > cmp_operator.precedence or \
           current_operator.precedence == cmp_operator.precedence and current_operator.association == Association.left:
          output.append(operators.pop())
        else:
          break
      operators.append(node)
    else:
      if recurse_types is not None and node.node_type in recurse_types:
        output.extend(infix_to_postfix(node.children, recurse_types=recurse_types))
      else:
        output.append(node)

  return output + list(reversed(operators))

Response: Convert a list of nodes in infix order to a list of nodes in postfix order.


Instruction:
How would you explain what the following Python 3 function does
def postfix_to_optree(nodes):
  while len(nodes) > 1:
    nodes = _reduce(nodes)

  if len(nodes) == 0:
    raise OperatorError("Empty node list")

  node = nodes[0]

  if isinstance(node, OperatorNode):
    raise OperatorError("Operator without operands")

  if isinstance(node, OptreeNode):
    return node

  return OptreeNode(None, (node, ))

Response: Convert a list of nodes in postfix order to an Optree.


Instruction:
How would you explain what the following Python 3 function does
def _reduce(nodes):
  i = 0
  while i < len(nodes):
    if isinstance(nodes[i], OperatorNode):
      break
    else:
      i += 1

  if i == len(nodes):
    raise OperatorError("No operator found")

  operator_node = nodes[i]
  operator = operator_node.operator
  operands_lbound = i - operator.cardinality

  if operands_lbound < 0:
    raise OperatorError("Insufficient operands for operator {0}".format(operator.symbol))

  return nodes[:operands_lbound] + \
         [OptreeNode(operator_node, tuple(nodes[operands_lbound:i]))] + \
         nodes[i+1:]

Response: Reduce the list of nodes by adding the operator and operands to the OptreeNode.


Instruction:
Make a summary of the following Python 3 code
def get_arguments(self):
        ApiCli.get_arguments(self)
        if self.args.pluginName is not None:
            self.pluginName = self.args.pluginName

Response: Extracts the specific arguments of this CLI


Instruction:
How would you explain what the following Python 3 function does
def _process_properties(self, properties):
        if properties is not None:
            self._properties = {}
            for p in properties:
                d = p.split('=')
                self._properties[d[0]] = d[1]

Response: Transforms the command line properties into python dictionary


Instruction:
How would you explain what the following Python 3 function does
def add_arguments(self):
        MetricCommon.add_arguments(self)
        self.parser.add_argument('-n', '--metric-name', dest='metricName', action='store',
                                 required=True, metavar='metric_name', help='Metric identifier')
        self.parser.add_argument('-d', '--display-name', dest='displayName', action='store',
                                 required=True, metavar='display_name', help='Metric display name')
        self.parser.add_argument('-s', '--display-name-short', dest='displayNameShort', action='store',
                                 required=True, metavar='display_short_name', help='Metric short display name')
        self.parser.add_argument('-i', '--description', dest='description', action='store',
                                 required=not self.update, metavar='description', help='Metric description')
        self.parser.add_argument('-g', '--aggregate', dest='aggregate', action='store',
                                 required=True, choices=['avg', 'max', 'min', 'sum'],
                                 help='Metric default aggregate')
        self.parser.add_argument('-u', '--unit', dest='unit', action='store',
                                 required=False, choices=['percent', 'number', 'bytecount', 'duration'],
                                 help='Metric unit')
        self.parser.add_argument('-r', '--resolution', dest='resolution', action='store', metavar='resolution',
                                 required=False, help='Metric default resolution')
        self.parser.add_argument('-y', '--type', dest='type', action='store', default=None,
                                 required=False, metavar='type', help='Sets the type metadata field')
        self.parser.add_argument('-x', '--is-disabled', dest='isDisabled', action='store', default=None,
                                 required=False,
                                 choices=['true', 'false'], help='Enable or disable the metric definition')

Response:
Add the specific arguments of this CLI
        to the parser


Instruction:
Create a Python 3 function to
extract the specific arguments of this CLI from the arguments object.

Response:
def get_arguments(self):
        """
        Extracts the specific arguments of this CLI
        """
        MetricCommon.get_arguments(self)

        if self.args.metricName is not None:
            self.metricName = self.args.metricName

        if self.args.displayName is not None:
            self.displayName = self.args.displayName

        if self.args.displayNameShort is not None:
            self.displayNameShort = self.args.displayNameShort

        if self.args.description is not None:
            self.description = self.args.description

        if self.args.aggregate is not None:
            self.aggregate = self.args.aggregate

        if self.args.unit is not None:
            self.unit = self.args.unit

        if self.args.resolution is not None:
            self.resolution = self.args.resolution

        if self.args.isDisabled is not None:
            self.isDisabled = self.args.isDisabled

        if self.args.type is not None:
            self.type = self.args.type

        data = {}
        if self.metricName is not None:
            data['name'] = self.metricName
        if self.displayName is not None:
            data['displayName'] = self.displayName
        if self.displayNameShort is not None:
            data['displayNameShort'] = self.displayNameShort
        if self.description is not None:
            data['description'] = self.description
        if self.aggregate is not None:
            data['defaultAggregate'] = self.aggregate
        if self.unit is not None:
            data['unit'] = self.unit
        if self.resolution is not None:
            data['defaultResolutionMS'] = self.resolution
        if self.isDisabled is not None:
            data['isDisabled'] = True if self.isDisabled == 'yes' else False
        if self.type is not None:
            data['type'] =  self.type

        self.path = "v1/metrics/{0}".format(self.metricName)
        self.data = json.dumps(data, sort_keys=True)
        self.headers = {'Content-Type': 'application/json', "Accept": "application/json"}


Instruction:
Given the following Python 3 function, write the documentation
def get_arguments(self):
        ApiCli.get_arguments(self)
        self._alarm_name = self.args.alarm_name if self.args.alarm_name is not None else None

Response: Extracts the specific arguments of this CLI


Instruction:
Explain what the following Python 3 code does
def read(self):
        f = open(self.path, "r")
        self.manifest_json = f.read()

Response: Load the metrics file from the given path


Instruction:
Write a Python 3 function that can
read the file and parse JSON into dictionary

Response:
def load(self):
        """
        Read the file and parse JSON into dictionary
        """
        manifest = PluginManifest(self.file_path)
        manifest.get()
        self.manifest = manifest.get_manifest()


Instruction:
Given the following Python 3 function, write the documentation
def getMetricDefinition(self, name):
        metric = None
        for m in self.metric_definitions:
            if m['name'] == name:
                metric = m
                break
        return metric

Response: Look up the metric definition from the definitions of the API call that matches the given name.


Instruction:
How would you explain what the following Python 3 function does
def printMetricsHeader(self, m, d):
        mstr = "Metric Name"
        dstr = "Description"

        print('|{0}{1}|{2}{3}|'.format(mstr, ' ' * (m - len(mstr)), dstr, ' ' * (d - len(dstr))))
        print('|:{0}|:{1}|'.format('-' * (m - 1), '-' * (d - 1)))

Response: Prints out the header of the metrics table based on the size of the data in columns


Instruction:
Given the following Python 3 function, write the documentation
def getFieldsColumnLengths(self):
        nameLen = 0
        descLen = 0
        for f in self.fields:
            nameLen = max(nameLen, len(f['title']))
            descLen = max(descLen, len(f['description']))
        return (nameLen, descLen)

Response: Gets the maximum length of each column in the field table


Instruction:
Can you implement a function in Python 3 that
gets the maximum length of each column in the metrics table

Response:
def getMetricsColumnLengths(self):
        """
        Gets the maximum length of each column
        """
        displayLen = 0
        descLen = 0
        for m in self.metrics:
            displayLen = max(displayLen, len(m['displayName']))
            descLen = max(descLen, len(m['description']))
        return (displayLen, descLen)


Instruction:
Write a Python 3 script to
escape underscores so that the markdown is correct

Response:
def escapeUnderscores(self):
        """
        Escape underscores so that the markdown is correct
        """
        new_metrics = []
        for m in self.metrics:
            m['name'] = m['name'].replace("_", "\_")
            new_metrics.append(m)
        self.metrics = new_metrics


Instruction:
Can you create a Python 3 function that
prints out the header of the fields of the current record set.

Response:
def printFieldsHeader(self, f, d):
        """
        Prints out table header based on the size of the data in columns
        """
        fstr = "Field Name"
        dstr = "Description"
        f = max(f, len(fstr))
        d = max(d, len(dstr))

        print('|{0}{1}|{2}{3}|'.format(fstr, ' ' * (f - len(fstr)), dstr, ' ' * (d - len(dstr))))
        print('|:{0}|:{1}|'.format('-' * (f - 1), '-' * (d - 1)))
        return (f, d)


Instruction:
Write a Python 3 script for
printing out the metrics for the current locale.

Response:
def printMetrics(self, m, d):
        """
        Prints out table rows based on the size of the data in columns
        """
        for metric in self.metrics:
            mstr = metric['displayName']
            dstr = metric['description']
            mlen = m - len(mstr)
            dlen = d - len(dstr)
            print("|{0}{1}|{2}{3}|".format(mstr, ' ' * mlen, dstr, ' ' * dlen))


Instruction:
Write a Python 3 function that can
print out the fields of the current object.

Response:
def printFields(self, f, d):
        """
        Prints out table rows based on the size of the data in columns
        """
        for field in self.fields:
            fstr = field["title"]
            dstr = field["description"]
            flen = f - len(fstr)
            dlen = d - len(dstr)
            print("|{0}{1}|{2}{3}|".format(fstr, ' ' * flen, dstr, ' ' * dlen))


Instruction:
Can you tell what is the following Python 3 function doing
def outputFieldMarkdown(self):
        f, d = self.getFieldsColumnLengths()

        fc, dc = self.printFieldsHeader(f, d)
        f = max(fc, f)
        d = max(dc, d)
        self.printFields(f, d)

Response: Send the field definitions ot standard out


Instruction:
How would you explain what the following Python 3 function does
def outputMetricMarkdown(self):
        self.escapeUnderscores()
        m, d = self.getMetricsColumnLengths()

        self.printMetricsHeader(m, d)
        self.printMetrics(m, d)

Response: Send the markdown of the metric definitions to standard out


Instruction:
Can you generate the documentation for the following Python 3 function
def generateMarkdown(self):
        self.generateMetricDefinitions()
        self.generateFieldDefinitions()
        self.generateDashboardDefinitions()
        self.outputMarkdown()

Response: Generate Markdown for all the metrics and field definitions


Instruction:
Write a Python 3 function for
attempting to parse source code.

Response:
def parse(self, text):
    """Attempt to parse source code."""
    self.original_text = text

    try:
      return getattr(self, self.entry_point)(text)
    except (DeadEnd) as exc:
      raise ParserError(self.most_consumed, "Failed to parse input") from exc
    return tree


Instruction:
Make a summary of the following Python 3 code
def _attempting(self, text):
    consumed = len(self.original_text) - len(text)
    self.most_consumed = max(consumed, self.most_consumed)

Response: Keeps track of the most consumed point in the source code the parser has reached to this point.


Instruction:
Make a summary of the following Python 3 code
def add_arguments(self):

        # Call our parent to add the default arguments
        ApiCli.add_arguments(self)

        # Command specific arguments
        self.parser.add_argument('-f', '--format', dest='format', action='store', required=False,
                                 choices=['csv', 'json', 'raw', 'xml'], help='Output format. Default is raw')
        self.parser.add_argument('-n', '--name', dest='metric_name', action='store', required=True,
                                 metavar="metric_name", help='Metric identifier')
        self.parser.add_argument('-g', '--aggregate', dest='aggregate', action='store', required=False,
                                 choices=['sum', 'avg', 'max', 'min'], help='Metric default aggregate')
        self.parser.add_argument('-r', '--sample', dest='sample', action='store', type=int, metavar="sample",
                                 help='Down sample rate sample in seconds')
        self.parser.add_argument('-s', '--source', dest='source', action='store', metavar="source", required=True,
                                 help='Source of measurement')
        self.parser.add_argument('-b', '--start', dest='start', action='store', required=True, metavar="start",
                                 help='Start of time range as ISO 8601 string or epoch seconds')
        self.parser.add_argument('-d', '--end', dest='end', action='store', metavar="end", required=False,
                                 help='End of time range as ISO 8601 string or epoch seconds')

        self.parser.add_argument('-o', '--date-format', dest='date_format', action='store', metavar="format",
                                 required=False,
                                 help='For CSV, JSON, and XML output formats dates (see Python date.strftime). ' +
                                      'Default format is %%s')

Response: Add command line arguments for this command


Instruction:
Write a Python 3 script for
extracting the specific arguments of this CLI

Response:
def get_arguments(self):
        """
        Extracts the specific arguments of this CLI
        """
        ApiCli.get_arguments(self)
        if self.args.metric_name is not None:
            self._metric_name = self.args.metric_name

        if self.args.sample is not None:
            self.sample = self.args.sample

        if self.args.source is not None:
            self.source = self.args.source
        else:
            self.source = None

        if self.args.aggregate is not None:
            self.aggregate = self.args.aggregate
        else:
            self.aggregate = "avg"

        if self.args.format is not None:
            self.format = self.args.format
        else:
            self.format = "json"

        if self.args.date_format is not None:
            self.date_format = self.args.date_format

        start_time = int(self.parse_time_date(self.args.start).strftime("%s"))

        # If the end time is not specified then
        # default to the current time
        if self.args.end is None:
            stop_time = int(self.now.strftime("%s"))
        else:
            stop_time = int(self.parse_time_date(self.args.end).strftime("%s"))

        # Convert to epoch time in milli-seconds
        start_time *= 1000
        stop_time *= 1000

        self.path = "v1/measurements/{0}".format(self._metric_name)
        url_parameters = {"start": str(start_time),
                          "end": str(stop_time),
                          "sample": str(self.sample),
                          "agg": self.aggregate}
        if self.source is not None:
            url_parameters['source'] = self.source
        self.url_parameters = url_parameters


Instruction:
Can you implement a function in Python 3 that
attempts to parse the passed in string into a valid datetime.

Response:
def parse_time_date(self, s):
        """
        Attempt to parse the passed in string into a valid datetime.
        If we get a parse error then assume the string is an epoch time
        and convert to a datetime.
        """
        try:
            ret = parser.parse(str(s))
        except ValueError:
            try:
                ret = datetime.fromtimestamp(int(s))
            except TypeError:
                ret = None
        return ret


Instruction:
Write a Python 3 script to
output results in CSV format

Response:
def output_csv(self, text):
        """
        Output results in CSV format
        """
        payload = json.loads(text)
        # Print CSV header
        print("{0},{1},{2},{3},{4}".format('timestamp', 'metric', 'aggregate', 'source', 'value'))
        metric_name = self._metric_name
        # Loop through the aggregates one row per timestamp, and 1 or more source/value pairs
        for r in payload['result']['aggregates']['key']:
            timestamp = self._format_timestamp(r[0][0])
            # timestamp = string.strip(timestamp, ' ')
            # timestamp = string.strip(timestamp, "'")
            for s in r[1]:
                print('{0},"{1}","{2}","{3}",{4}'.format(timestamp, metric_name, self.aggregate, s[0], s[1]))


Instruction:
Can you generate the documentation for the following Python 3 function
def output_json(self, text):
        payload = json.loads(text)
        data = []
        metric_name = self._metric_name
        for r in payload['result']['aggregates']['key']:
            timestamp = self._format_timestamp(r[0][0])
            for s in r[1]:
                data.append({
                    "timestamp": timestamp,
                    "metric": metric_name,
                    "aggregate": self.aggregate,
                    "source": s[0],
                    "value": s[1],
                })
        payload = {"data": data}
        out = json.dumps(payload, indent=self._indent, separators=(',', ': '))
        print(self.colorize_json(out))

Response: Output results in structured JSON format


Instruction:
Can you create a Python 3 function that
outputs results in raw JSON format

Response:
def output_raw(self, text):
        """
        Output results in raw JSON format
        """
        payload = json.loads(text)
        out = json.dumps(payload, sort_keys=True, indent=self._indent, separators=(',', ': '))
        print(self.colorize_json(out))


Instruction:
Implement a Python 3 function for
outputting the results in JSON format

Response:
def output_xml(self, text):
        """
        Output results in JSON format
        """

        # Create the main document nodes
        document = Element('results')
        comment = Comment('Generated by TrueSight Pulse measurement-get CLI')
        document.append(comment)
        aggregates = SubElement(document, 'aggregates')
        aggregate = SubElement(aggregates, 'aggregate')
        measurements = SubElement(aggregate, 'measurements')

        # Parse the JSON result so we can translate to XML
        payload = json.loads(text)

        # Current only support a single metric, if we move to the batch API then
        # we can handle multiple
        metric_name = self._metric_name

        # Loop through the aggregates one row per timestamp, and 1 or more source/value pairs
        for r in payload['result']['aggregates']['key']:
            timestamp = self._format_timestamp(r[0][0])
            for s in r[1]:
                # Each timestamp, metric, source, values is placed in a measure tag
                measure_node = SubElement(measurements, 'measure')
                source = s[0]
                value = str(s[1])
                ts_node = SubElement(measure_node, 'timestamp')
                ts_node.text = str(timestamp)
                metric_node = SubElement(measure_node, 'metric')
                metric_node.text = metric_name
                metric_node = SubElement(measure_node, 'aggregate')
                metric_node.text = self.aggregate
                source_node = SubElement(measure_node, 'source')
                source_node.text = source
                value_node = SubElement(measure_node, 'value')
                value_node.text = value

        rough_string = ElementTree.tostring(document, 'utf-8')
        reparse = minidom.parseString(rough_string)
        output = reparse.toprettyxml(indent=" ")
        print(self.colorize_xml(output))


Instruction:
Write a Python 3 script to
handle the API response from the get_iso_date API call.

Response:
def _handle_results(self):
        """
        Call back function to be implemented by the CLI.
        """

        # Only process if we get HTTP result of 200
        if self._api_result.status_code == requests.codes.ok:
            if self.format == "json":
                self.output_json(self._api_result.text)
            elif self.format == "csv":
                self.output_csv(self._api_result.text)
            elif self.format == "raw":
                self.output_raw(self._api_result.text)
            elif self.format == "xml":
                self.output_xml(self._api_result.text)
        else:
            pass


Instruction:
Can you generate the documentation for the following Python 3 function
def trimmed_pred_default(node, parent):
  return isinstance(node, ParseNode) and (node.is_empty or node.is_type(ParseNodeType.terminal))

Response: The default predicate used in Node. trimmed.


Instruction:
Make a summary of the following Python 3 code
def pprint(root, depth=0, space_unit="    ", *, source_len=0, file=None):
  spacing = space_unit * depth

  if isinstance(root, str):
    print("{0}terminal@(?): {1}".format(spacing, root), file=file)
  else:
    if root.position is None:
      position = -1
    elif root.position < 0:
      position = source_len + root.position
    else:
      position = root.position

    if root.is_value:
      print("{0}{1}@({2}:{3}):\t{4}".format(spacing, root.node_type, position, root.consumed, root.svalue), file=file)
    else:
      print("{0}{1}@({2}:{3}):".format(spacing, root.node_type, position, root.consumed), file=file)
      for child in root.children:
        pprint(child, depth + 1, source_len=source_len, file=file)

Response: Pretting print a parse tree.


Instruction:
Implement a function in Python 3 to
return a partial of _get_repetition that accepts only a text argument.

Response:
def zero_or_more(extractor, *, ignore_whitespace=False):
  """Returns a partial of _get_repetition with bounds set to (0, None) that accepts only a text
  argument.
  """
  return partial(_get_repetition, extractor, bounds=(0, None), ignore_whitespace=ignore_whitespace)


Instruction:
Can you tell what is the following Python 3 function doing
def one_or_more(extractor, *, ignore_whitespace=False):
  return partial(_get_repetition, extractor, bounds=(1, None), ignore_whitespace=ignore_whitespace)

Response:
Returns a partial of _get_repetition that accepts only a text
 argument.


Instruction:
Given the following Python 3 function, write the documentation
def repeated(extractor, times, *, ignore_whitespace=False):
  return partial(_get_repetition,
                 extractor,
                 bounds=(times, times),
                 ignore_whitespace=ignore_whitespace)

Response:
Returns a partial of _get_repetition that accepts only a text
 argument.


Instruction:
Implement a Python 3 function for
returning a partial of _get_repetition that accepts only a text argument.

Response:
def repetition(extractor, bounds, *, ignore_whitespace=False):
  """Returns a partial of _get_repetition that accepts only a text argument."""
  return partial(_get_repetition, extractor, bounds=bounds, ignore_whitespace=ignore_whitespace)


Instruction:
Here you have a function in Python 3, explain what it does
def _get_terminal(value, text):
  if text and text.startswith(value):
    return ParseNode(ParseNodeType.terminal,
                      children=[value],
                      consumed=len(value),
                      position=-len(text))
  else:
    raise DeadEnd()

Response: Checks the beginning of text for a value and returns a terminal ParseNode.


Instruction:
How would you implement a function in Python 3 that
returns a concatenation of the text.

Response:
def _get_concatenation(extractors, text, *, ignore_whitespace=True):
  """Returns a concatenation ParseNode whose children are the nodes returned by each of the
  methods in the extractors enumerable.

  If ignore_whitespace is True, whitespace will be ignored and then attached to the child it
  preceeded.
  """
  ignored_ws, use_text = _split_ignored(text, ignore_whitespace)

  extractor, *remaining = extractors

  child = _call_extractor(extractor, use_text)
  child.add_ignored(ignored_ws)

  # TODO: Should I set node.position = -len(text) for the case that ignored whitespace will cause
  #       the first child's position to not be the whitespace, and therefore the concatenation's
  #       position will be the first non-whitespace? I think not, but I'm adding this note in
  #       case that causes an issue I'm not seeing at the moment.
  node = ParseNode(ParseNodeType.concatenation, children=[child])

  if remaining:
    # child.consumed will include ignored whitespace, so we base the text we pass on on text rather
    # than use_text.
    return node.merged(_get_concatenation(remaining,
                                          text[child.consumed:],
                                          ignore_whitespace=ignore_whitespace))
  else:
    return node


Instruction:
Can you create a Python 3 function that
tries each extractor on the given text and returns the alternative.

Response:
def _get_alternation(extractors, text):
  """Tries each extractor on the given text and returns the 'best fit'.

  Best fit is defined by as the extractor whose result consumed the most text. If more than one
  extractor is the best fit, the result of the one that appeared earliest in the list is returned.

  If all extractors raise a DeadEnd, this method too will raise a DeadEnd.
  """
  candidates = []

  for extractor in extractors:
    try:
      candidates.append(_call_extractor(extractor, text))
    except DeadEnd:
      pass

  if not candidates:
    raise DeadEnd

  result, *remaining = candidates

  for candidate in remaining:
    if len(candidate) > len(result):
      result = candidate

  return result


Instruction:
Can you generate the documentation for the following Python 3 function
def _get_repetition(extractor, text, *, bounds=(0, None), ignore_whitespace=False):
  minr, maxr = bounds
  children = []

  while maxr is None or len(children) <= maxr:
    ignored_ws, use_text = _split_ignored(text, ignore_whitespace)
    try:
      child = _call_extractor(extractor, use_text)
      child.add_ignored(ignored_ws)
    except DeadEnd:
      break

    if child.is_empty:
      break

    children.append(child)
    text = text[child.consumed:]

  if len(children) >= minr:
    return ParseNode(ParseNodeType.repetition,
                      children=children)
  else:
    raise DeadEnd()

Response: Tries to pull a text node from the text until it raises DeadEnd.


Instruction:
Implement a Python 3 function for
returning extractor s result if exclusion does not match.

Response:
def _get_exclusion(extractor, exclusion, text):
  """Returns extractor's result if exclusion does not match.

  If exclusion raises DeadEnd (meaning it did not match) then the result of extractor(text) is
  returned. Otherwise, if exclusion does not raise DeadEnd it means it did match, and we then
  raise DeadEnd.
  """
  try:
    _call_extractor(exclusion, text)
    exclusion_matches = True
  except DeadEnd:
    exclusion_matches = False

  if exclusion_matches:
    raise DeadEnd()
  else:
    return _call_extractor(extractor, text)


Instruction:
How would you explain what the following Python 3 function does
def _split_ignored(text, ignore_whitespace=True):
  if ignore_whitespace:
    leading_ws_count = _count_leading_whitespace(text)
    ignored_ws = text[:leading_ws_count]
    use_text = text[leading_ws_count:]
    return (ignored_ws, use_text)
  else:
    return ("", text)

Response: Split text into leading whitespace and trailing text.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _count_leading_whitespace(text):
  idx = 0
  for idx, char in enumerate(text):
    if not char.isspace():
      return idx
  return idx + 1

Response: Returns the number of characters at the beginning of text that are whitespace.


Instruction:
Write a Python 3 function for
getting the position of the text the ParseNode processed.

Response:
def position(self):
    """Gets the position of the text the ParseNode processed. If the ParseNode does not have its
    own position, it looks to its first child for its position.

    'Value Nodes' (terminals) must have their own position, otherwise this method will throw an
    exception when it tries to get the position property of the string child.
    """
    pos = self._position
    if pos is None and self.children:
      ch1 = self.children[0]
      if isinstance(ch1, ParseNode):
        pos = ch1.position
    return pos


Instruction:
Can you generate the documentation for the following Python 3 function
def is_empty(self):
    return all(isinstance(c, ParseNode) and c.is_empty for c in self.children)

Response: Returns True if this node has no children or all of its children are empty.


Instruction:
Given the following Python 3 function, write the documentation
def add_ignored(self, ignored):
    if ignored:
      if self.ignored:
        self.ignored = ignored + self.ignored
      else:
        self.ignored = ignored

    self.consumed += len(ignored)

Response: Add ignored text to the node.


Instruction:
How would you code a function in Python 3 to
return True if node_type == value.

Response:
def is_type(self, value):
    """Returns True if node_type == value.

    If value is a tuple, node_type is checked against each member and True is returned if any of
    them match.
    """
    if isinstance(value, tuple):
      for opt in value:
        if self.node_type == opt:
          return True
      return False
    else:
      return self.node_type == value


Instruction:
Explain what the following Python 3 code does
def flattened(self, pred=flattened_pred_default):
    if self.is_value:
      return self

    new_children = []

    for child in self.children:
      if child.is_empty:
        continue

      new_child = child.flattened(pred)

      if pred(new_child, self):
        new_children.extend(new_child.children)
      else:
        new_children.append(new_child)

    return ParseNode(self.node_type,
                     children=new_children,
                     consumed=self.consumed,
                     position=self.position,
                     ignored=self.ignored)

Response: Flattens nodes by hoisting children up to ancestor nodes.


Instruction:
Can you write a function in Python 3 where it
trims a ParseTree. A node is trimmed if pred returns True.

Response:
def trimmed(self, pred=trimmed_pred_default):
    """Trim a ParseTree.

    A node is trimmed if pred(node) returns True.
    """
    new_children = []

    for child in self.children:
      if isinstance(child, ParseNode):
        new_child = child.trimmed(pred)
      else:
        new_child = child

      if not pred(new_child, self):
        new_children.append(new_child)

    return ParseNode(self.node_type,
                     children=new_children,
                     consumed=self.consumed,
                     position=self.position,
                     ignored=self.ignored)


Instruction:
Can you generate the documentation for the following Python 3 function
def merged(self, other):
    children = [c for c in itertools.chain(self.children, other.children) if len(c) > 0]
    # NOTE: Only terminals should have ignored text attached to them, and terminals shouldn't be
    #       merged (probably) so it shouldn't be necessary to copy of ignored -- it should always
    #       be None. But, we'll go ahead and copy it over anyway, recognizing that other's
    #       ignored text will be lost.
    return ParseNode(self.node_type,
                      children=children,
                      consumed=self.consumed + other.consumed,
                      ignored=self.ignored)

Response:
Returns a new ParseNode whose type is this node s type and whose children are all the the
    children from this node and the other whose length is not 0.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def retyped(self, new_type):
    return ParseNode(new_type,
                      children=list(self.children),
                      consumed=self.consumed,
                      position=self.position,
                      ignored=self.ignored)

Response: Returns a new node with the same contents as self but with a new node_type.


Instruction:
How would you explain what the following Python 3 function does
def compressed(self, new_type=None, *, include_ignored=False):
    values = []
    consumed = 0
    ignored = None

    for i, child in enumerate(self.children):
      consumed += child.consumed
      if i == 0 and not include_ignored:
        ignored = child.ignored
      if child.is_value:
        if include_ignored:
          values.append("{0}{1}".format(child.ignored or "", child.value))
        else:
          values.append(child.value)
      else:
        values.append(child.compressed(include_ignored=include_ignored).value)

    return ParseNode(new_type or self.node_type,
                      children=["".join(values)],
                      consumed=consumed,
                      ignored=ignored,
                      position=self.position)

Response:
Turns the node into a value node whose single string child is the concatenation of all its
    children.


Instruction:
Implement a function in Python 3 to
extract the specific arguments of this CLI

Response:
def get_arguments(self):
        """
        Extracts the specific arguments of this CLI
        """
        ApiCli.get_arguments(self)

        # Get the host group name
        if self.args.host_group_name is not None:
            self.host_group_name = self.args.host_group_name

        # Get the list of sources separated by commas
        if self.args.sources is not None:
            self.sources = self.args.sources

        payload = {}
        if self.host_group_name is not None:
            payload['name'] = self.host_group_name

        if self.sources is not None:
            source_list = str.split(self.sources, ',')
            if 'hostnames' not in payload:
                payload['hostnames'] = []

            for s in source_list:
                payload['hostnames'].append(s)
        self.data = json.dumps(payload, sort_keys=True)
        self.headers = {'Content-Type': 'application/json', "Accept": "application/json"}


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_scope_list(self) -> list:
        # by default only return scoped name
        lstparent = [self]
        p = self.get_parent()
        while p is not None:
            lstparent.append(p)
            p = p.get_parent()
        return lstparent

Response: Return the list of all contained scope from global to local


Instruction:
Can you implement a function in Python 3 that
returns the list of all contained scope names from global to local

Response:
def get_scope_names(self) -> list:
        """
        Return the list of all contained scope from global to local
        """
        # allow global scope to have an None string instance
        lscope = []
        for scope in reversed(self.get_scope_list()):
            if scope.name is not None:
                # handle fun/block scope decoration
                lscope.append(scope.name)
        return lscope


Instruction:
Given the following Python 3 function, write the documentation
def position(self) -> Position:
        return Position(self._index, self._lineno, self._col_offset)

Response: The current position of the cursor.


Instruction:
Here you have a function in Python 3, explain what it does
def step_next_char(self):
        self._index += 1
        self._col_offset += 1
        if self._index > self._maxindex:
            self._maxindex = self._index
            self._maxcol = self._col_offset
            self._maxline = self._lineno

Response: Puts the cursor on the next character.


Instruction:
Implement a Python 3 function for
setting cursor as beginning of next line.

Response:
def step_next_line(self):
        """Sets cursor as beginning of next line."""
        self._eol.append(self.position)
        self._lineno += 1
        self._col_offset = 0


Instruction:
How would you explain what the following Python 3 function does
def step_prev_line(self):
        #TODO(bps): raise explicit error for unregistered eol
        #assert self._eol[-1].index == self._index
        if len(self._eol) > 0:
            self.position = self._eol.pop()

Response: Sets cursor as end of previous line.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def last_readed_line(self) -> str:
        mpos = self._cursor.max_readed_position
        mindex = mpos.index
        # search last \n
        prevline = mindex - 1 if mindex == self.eos_index else mindex
        while prevline >= 0 and self._content[prevline] != '\n':
            prevline -= 1
        # search next \n
        nextline = mindex
        while nextline < self.eos_index and self._content[nextline] != '\n':
            nextline += 1
        last_line = self._content[prevline + 1:nextline]
        return last_line

Response: Usefull string to compute error message.


Instruction:
Can you generate the documentation for the following Python 3 function
def incpos(self, length: int=1) -> int:
        if length < 0:
            raise ValueError("length must be positive")
        i = 0
        while (i < length):
            if self._cursor.index < self._len:
                if self.peek_char == '\n':
                    self._cursor.step_next_line()
                self._cursor.step_next_char()
            i += 1
        return self._cursor.index

Response: Increment the cursor to the next character.


Instruction:
Can you tell what is the following Python 3 function doing
def to_fmt(self) -> fmt.indentable:
    qual = "scope"
    txt = fmt.sep(" ", [qual])
    name = self.show_name()
    if name != "":
        txt.lsdata.append(name)
    if len(self._hsig) > 0 or len(self.mapTypeTranslate) > 0:
        lsb = []
        if len(self.mapTypeTranslate) > 0:
            lsb.append("translate:\n")
            lsb.append(fmt.end("\n", self.mapTypeTranslate.to_fmt()))
        for k in sorted(self._hsig.keys()):
            s = self._hsig[k]
            lsb.append(fmt.end("\n", [s.to_fmt()]))
        block = fmt.block(":\n", "", fmt.tab(lsb))
        txt.lsdata.append(block)
    return txt

Response: Return an Fmt representation for pretty - printing.


Instruction:
Explain what the following Python 3 code does
def to_fmt(self):
    qual = "evalctx"
    lseval = []
    block = fmt.block(":\n", "", fmt.tab(lseval))
    txt = fmt.sep(" ", [qual, block])
    lseval.append(self._sig.to_fmt())
    if len(self.resolution) > 0:
        lsb = []
        for k in sorted(self.resolution.keys()):
            s = self.resolution[k]
            if s is not None:
                lsb.append(
                    fmt.end(
                        "\n",
                        ["'%s': %s (%s)" % (k, s, s().show_name())]
                    )
                )
            else:
                lsb.append(fmt.end("\n", ["'%s': Unresolved" % (k)]))
        if self._translate_to is not None:
            lsb.append("use translator:")
            lsb.append(self._translate_to.to_fmt())
        if self._variadic_types is not None:
            lsb.append("variadic types:\n")
            arity = self._sig.arity
            for t in self._variadic_types:
                lsb.append("[%d] : %s\n" % (arity, t))
                arity += 1
        lseval.append(fmt.block("\nresolution :\n", "", fmt.tab(lsb)))
    return txt

Response: Return an Fmt representation for pretty - printing the object.


Instruction:
Can you implement a function in Python 3 that
returns a Fmt representation of this object for pretty - printing.

Response:
def to_fmt(self, with_from=False) -> fmt.indentable:
    """
    Return a Fmt representation of Translator for pretty-printing
    """
    txt = fmt.sep("\n", [
        fmt.sep(
            " ",
            [
                self._type_source,
                "to",
                self._type_target,
                '=',
                self._fun.to_fmt()
            ]
        ),
        self._notify.get_content(with_from)
    ])
    return txt


Instruction:
Can you generate the documentation for the following Python 3 function
def to_fmt(self):
    params = ""
    txt = fmt.sep(" ", ['val'])
    name = self.show_name()
    if name != "":
        txt.lsdata.append(name)
    txt.lsdata.append('(%s)' % self.value)
    txt.lsdata.append(': ' + self.tret)
    return txt

Response: Returns an Fmt representation for pretty - printing


Instruction:
Make a summary of the following Python 3 code
def to_fmt(self):
    params = ""
    txt = fmt.sep(" ", ['fun'])
    name = self.show_name()
    if name != "":
        txt.lsdata.append(name)
    tparams = []
    if self.tparams is not None:
        tparams = list(self.tparams)
    if self.variadic:
        tparams.append('...')
    params = '(' + ", ".join(tparams) + ')'
    txt.lsdata.append(': ' + params)
    txt.lsdata.append('-> ' + self.tret)
    return txt

Response: Returns an Fmt representation for pretty - printing the log entry.


Instruction:
Here you have a function in Python 3, explain what it does
def walk(self, lc: state.LivingContext, user_data=None, parent=None):
    global _cacheid
    # root node autorefence
    if parent is None:
        parent = self
    ## walk attributes
    if hasattr(self, '__dict__') and not isinstance(self, node.ListNode):
        for k in sorted(vars(self).keys()):
            print("RECURS key %s ID %d" % (k, id(getattr(self, k))))
            walk(getattr(self, k), lc, user_data, self)
            # k == ?
            #print('test attr .%s' % k)
            lc.checkAttr(k, self)
            # check precond
            lc.checkEventExpr()
            # do sub Event (for unstrict mode)
            lc.doSubEvent()
    # ...as dict, walk values, match keys
    if hasattr(self, 'keys'):
        for k in sorted(self.keys()):
            #print("RECURS ID %d" % id(self[k]))
            walk(self[k], lc, user_data, self)
            # k == ?
            #print('test key [%s]' % repr(k))
            lc.checkKey(k, self)
            # check precond
            lc.checkEventExpr()
            # do sub Event (for unstrict mode)
            lc.doSubEvent()
    # ...as list, walk values, match indices
    elif not isinstance(self, str) and hasattr(self, '__iter__'):
        idx = 0
        for i in self:
            #print("RECURS ID %d" % id(i))
            walk(i, lc, user_data, self)
            # idx == ?
            #print('test indice [%s]' % str(idx))
            lc.checkIndice(idx, self)
            idx += 1
            # check precond
            lc.checkEventExpr()
            # do sub Event (for unstrict mode)
            lc.doSubEvent()
    # ...type or value
    # type(self) == ?
    #print("test type %s" % type(self))
    lc.checkType(type(self), self, parent)
    # self == ?
    #print("test value %s" % str(self))
    lc.checkValue(self)
    ## Check EVENTS
    # TODO: what if the event do something
    # but don't change current state and default change it!!!
    lc.checkEventExpr()
    #print("RESULT")
    # check Event
    lc.doResultEvent()
    # check Hook
    lc.doResultHook(self, user_data, parent)
    # no transition, fallback to default
    lc.doDefault()
    # maintain the pool of LivingState
    lc.resetLivingState()

Response: Walks the object and returns the tree of nodes.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def set_name(self, name: str):
        self.name = name
        # update internal names
        lsig = self._hsig.values()
        self._hsig = {}
        for s in lsig:
            self._hsig[s.internal_name()] = s

Response: Set the name of the current object.


Instruction:
Write a Python 3 script for
counting the number of types in the database.

Response:
def count_types(self) -> int:
        """ Count subtypes """
        n = 0
        for s in self._hsig.values():
            if type(s).__name__ == 'Type':
                n += 1
        return n


Instruction:
Can you create a Python 3 function that
counts the number of variables defined by this scope.

Response:
def count_vars(self) -> int:
        """ Count var define by this scope """
        n = 0
        for s in self._hsig.values():
            if hasattr(s, 'is_var') and s.is_var:
                n += 1
        return n


Instruction:
Write a Python 3 function for
counting the number of functions defined by this scope.

Response:
def count_funs(self) -> int:
        """ Count function define by this scope """
        n = 0
        for s in self._hsig.values():
            if hasattr(s, 'is_fun') and s.is_fun:
                n += 1
        return n


Instruction:
Create a Python 3 function to
update the set with values of another Set

Response:
def update(self, sig: list or Scope) -> Scope:
        """ Update the Set with values of another Set """
        values = sig
        if hasattr(sig, 'values'):
            values = sig.values()
        for s in values:
            if self.is_namespace:
                s.set_parent(self)
            if isinstance(s, Scope):
                s.state = StateScope.EMBEDDED
            self._hsig[s.internal_name()] = s
        self.__update_count()
        return self


Instruction:
Write a Python 3 script for
creating a new set produce by the union of two sets.

Response:
def union(self, sig: Scope) -> Scope:
        """ Create a new Set produce by the union of 2 Set """
        new = Scope(sig=self._hsig.values(), state=self.state)
        new |= sig
        return new


Instruction:
Implement a Python 3 function for
updating this set with common values of another set.

Response:
def intersection_update(self, oset: Scope) -> Scope:
        """ Update Set with common values of another Set """
        keys = list(self._hsig.keys())
        for k in keys:
            if k not in oset:
                del self._hsig[k]
            else:
                self._hsig[k] = oset.get(k)
        return self


Instruction:
Can you create a Python 3 function that
creates a new set produce by the intersection of two sets

Response:
def intersection(self, sig: Scope) -> Scope:
        """ Create a new Set produce by the intersection of 2 Set """
        new = Scope(sig=self._hsig.values(), state=self.state)
        new &= sig
        return new


Instruction:
Can you generate a brief explanation for the following Python 3 code
def difference_update(self, oset: Scope) -> Scope:
        keys = list(self._hsig.keys())
        for k in keys:
            if k in oset:
                del self._hsig[k]
        return self

Response: Remove values common with another Set.


Instruction:
Make a summary of the following Python 3 code
def difference(self, sig: Scope) -> Scope:
        new = Scope(sig=self._hsig.values(), state=self.state)
        new -= sig
        return new

Response: Create a new Set produce by a Set subtracted by another Set


Instruction:
Write a Python 3 script to
update the set with the common values from another Set and remove common values from self and update specific values from oset.

Response:
def symmetric_difference_update(self, oset: Scope) -> Scope:
        """ Remove common values
            and Update specific values from another Set
        """
        skey = set()
        keys = list(self._hsig.keys())
        for k in keys:
            if k in oset:
                skey.add(k)
        for k in oset._hsig.keys():
            if k not in skey:
                self._hsig[k] = oset.get(k)
        for k in skey:
            del self._hsig[k]
        return self


Instruction:
Can you tell what is the following Python 3 function doing
def symmetric_difference(self, sig: Scope) -> Scope:
        new = Scope(sig=self._hsig.values(), state=self.state)
        new ^= sig
        return new

Response: Create a new set with values present in only one set


Instruction:
Create a Python 3 function for
adding a signature to the set.

Response:
def add(self, it: Signature) -> bool:
        """ Add it to the Set """
        if isinstance(it, Scope):
            it.state = StateScope.EMBEDDED
        txt = it.internal_name()
        it.set_parent(self)
        if self.is_namespace:
            txt = it.internal_name()
        if txt == "":
            txt = '_' + str(len(self._hsig))
        if txt in self._hsig:
            raise KeyError("Already exists %s" % txt)
        self._hsig[txt] = it
        self.__update_count()
        return True


Instruction:
Make a summary of the following Python 3 code
def remove(self, it: Signature) -> bool:
        txt = it.internal_name()
        if txt not in self._hsig:
            raise KeyError(it.show_name() + ' not in Set')
        sig = self._hsig[txt]
        if isinstance(sig, Scope):
            sig.state = StateScope.LINKED
        del self._hsig[txt]
        return True

Response: Remove it but raise KeyError if not found


Instruction:
Write a Python 3 script for
removing it only if it is present

Response:
def discard(self, it: Signature) -> bool:
        """ Remove it only if present """
        txt = it.internal_name()
        if txt in self._hsig:
            sig = self._hsig[txt]
            if isinstance(sig, Scope):
                sig.state = StateScope.LINKED
            del self._hsig[txt]
            return True
        return False


Instruction:
How would you explain what the following Python 3 function does
def values(self) -> [Signature]:
        if self.state == StateScope.EMBEDDED and self.parent is not None:
            return list(self._hsig.values()) + list(self.parent().values())
        else:
            return self._hsig.values()

Response: Retrieve all values of this signature.


Instruction:
Create a Python 3 function to
retrieve the first Signature in the set ordered by mangling descendant

Response:
def first(self) -> Signature:
        """ Retrieve the first Signature ordered by mangling descendant """
        k = sorted(self._hsig.keys())
        return self._hsig[k[0]]


Instruction:
Write a Python 3 function that can
retrieve the last signature in the set

Response:
def last(self) -> Signature:
        """ Retrieve the last Signature ordered by mangling descendant """
        k = sorted(self._hsig.keys())
        return self._hsig[k[-1]]


Instruction:
Write a Python 3 script to
get a signature instance by its internal_name

Response:
def get(self, key: str, default=None) -> Signature:
        """ Get a signature instance by its internal_name """
        item = default
        if key in self._hsig:
            item = self._hsig[key]
        return item


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_by_symbol_name(self, name: str) -> Scope:
        lst = []
        for s in self.values():
            if s.name == name:
                # create an EvalCtx only when necessary
                lst.append(EvalCtx.from_sig(s))
        # include parent
        # TODO: see all case of local redefinition for
        #       global overloads
        # possible algos... take all with different internal_name
        if len(lst) == 0:
            p = self.get_parent()
            if p is not None:
                return p.get_by_symbol_name(name)
        rscope = Scope(sig=lst, state=StateScope.LINKED, is_namespace=False)
        # inherit type/translation from parent
        rscope.set_parent(self)
        return rscope

Response: Retrieve a Scope object by symbol name


Instruction:
Given the following Python 3 function, write the documentation
def getsig_by_symbol_name(self, name: str) -> Signature:
        subscope = self.get_by_symbol_name(name)
        if len(subscope) != 1:
            raise KeyError("%s have multiple candidates in scope" % name)
        v = list(subscope.values())
        return v[0]

Response: Retrieve the unique Signature for a symbol name. Fails if there are multiple signatures in the scope.


Instruction:
How would you code a function in Python 3 to
retrieve a Scope of all signatures by return type.

Response:
def get_by_return_type(self, tname: str) -> Scope:
        """ Retrieve a Set of all signature by (return) type """
        lst = []
        for s in self.values():
            if hasattr(s, 'tret') and s.tret == tname:
                lst.append(EvalCtx.from_sig(s))
        rscope = Scope(sig=lst, state=StateScope.LINKED, is_namespace=False)
        # inherit type/translation from parent
        rscope.set_parent(self)
        return rscope


Instruction:
Given the following Python 3 function, write the documentation
def get_all_polymorphic_return(self) -> bool:
        lst = []
        for s in self.values():
            if hasattr(s, 'tret') and s.tret.is_polymorphic:
                # encapsulate s into a EvalCtx for meta-var resolution
                lst.append(EvalCtx.from_sig(s))
        rscope = Scope(sig=lst, state=StateScope.LINKED, is_namespace=False)
        # inherit type/translation from parent
        rscope.set_parent(self)
        return rscope

Response: Returns a Scope that contains all polymorphic return types.


Instruction:
Implement a Python 3 function for
retrieving a set of all signature that match the parameter list.

Response:
def get_by_params(self, params: [Scope]) -> (Scope, [[Scope]]):
        """ Retrieve a Set of all signature that match the parameter list.
        Return a pair:

            pair[0] the overloads for the functions
            pair[1] the overloads for the parameters
                (a list of candidate list of parameters)
        """
        lst = []
        scopep = []
        # for each of our signatures
        for s in self.values():
            # for each params of this signature
            if hasattr(s, 'tparams'):
                # number of matched params
                mcnt = 0
                # temporary collect
                nbparam_sig = (0 if s.tparams is None else len(s.tparams))
                nbparam_candidates = len(params)
                # don't treat signature too short
                if nbparam_sig > nbparam_candidates:
                    continue
                # don't treat call signature too long if not variadic
                if nbparam_candidates > nbparam_sig and not s.variadic:
                    continue
                tmp = [None] * nbparam_candidates
                variadic_types = []
                for i in range(nbparam_candidates):
                    tmp[i] = Scope(state=StateScope.LINKED)
                    tmp[i].set_parent(self)
                    # match param of the expr
                    if i < nbparam_sig:
                        if params[i].state == StateScope.EMBEDDED:
                            raise ValueError(
                                ("params[%d] of get_by_params is a StateScope."
                                 + "EMBEDDED scope... "
                                 + "read the doc and try a StateScope.FREE"
                                 + " or StateScope.LINKED.") % i
                            )
                        m = params[i].get_by_return_type(s.tparams[i])
                        if len(m) > 0:
                            mcnt += 1
                            tmp[i].update(m)
                        else:
                            # co/contra-variance
                            # we just need to search a t1->t2
                            # and add it into the tree (with/without warnings)
                            t1 = params[i]
                            t2 = s.tparams[i]
                            # if exist a fun (t1) -> t2
                            (is_convertible,
                             signature,
                             translator
                             ) = t1.findTranslationTo(t2)
                            if is_convertible:
                                # add a translator in the EvalCtx
                                signature.use_translator(translator)
                                mcnt += 1
                                nscope = Scope(
                                    sig=[signature],
                                    state=StateScope.LINKED,
                                    is_namespace=False
                                    )
                                nscope.set_parent(self)
                                tmp[i].update(nscope)
                            elif s.tparams[i].is_polymorphic:
                                # handle polymorphic parameter
                                mcnt += 1
                                if not isinstance(params[i], Scope):
                                    raise Exception(
                                        "params[%d] must be a Scope" % i
                                    )
                                tmp[i].update(params[i])
                            else:
                                # handle polymorphic return type
                                m = params[i].get_all_polymorphic_return()
                                if len(m) > 0:
                                    mcnt += 1
                                    tmp[i].update(m)
                    # for variadic extra parameters
                    else:
                        mcnt += 1
                        if not isinstance(params[i], Scope):
                            raise Exception("params[%d] must be a Scope" % i)
                        variadic_types.append(params[i].first().tret)
                        tmp[i].update(params[i])
                # we have match all candidates
                if mcnt == len(params):
                    # select this signature but
                    # box it (with EvalCtx) for type resolution
                    lst.append(EvalCtx.from_sig(s))
                    lastentry = lst[-1]
                    if lastentry.variadic:
                        lastentry.use_variadic_types(variadic_types)
                    scopep.append(tmp)
        rscope = Scope(sig=lst, state=StateScope.LINKED, is_namespace=False)
        # inherit type/translation from parent
        rscope.set_parent(self)
        return (rscope, scopep)


Instruction:
How would you explain what the following Python 3 function does
def callInjector(self, old: Node, trans: Translator) -> Node:
        if self.astTranslatorInjector is None:
            if self.parent is not None:
                # TODO: think if we forward for all StateScope
                # forward to parent scope
                return self.parent().callInjector(old, trans)
            else:
                raise TypeError("Must define an Translator Injector")
        return self.astTranslatorInjector(old, trans)

Response: Call the injector from the parent if it has one.


Instruction:
Make a summary of the following Python 3 code
def findTranslationTo(self, t2: str) -> (bool, Signature, Translator):
        if not t2.is_polymorphic:
            collect = []
            for s in self.values():
                t1 = s.tret
                if t1.is_polymorphic:
                    continue
                if (s.tret in self.mapTypeTranslate):
                    if (t2 in self.mapTypeTranslate[t1]):
                        collect.append((
                            True,
                            s,
                            self.mapTypeTranslate[t1][t2]
                        ))
            # if len > 1 too many candidates
            if len(collect) == 1:
                return collect[0]
        return (False, None, None)

Response: Find an arrow that can translate something to t2


Instruction:
How would you explain what the following Python 3 function does
def normalize(ast: Node) -> Node:
    res = ast
    typemap = {DictNode, ListNode, TupleNode}
    if type(ast) is dict:
        res = DictNode(ast)
    elif type(ast) is list:
        res = ListNode(ast)
    elif type(ast) is tuple:
        res = TupleNode(ast)
    # in-depth change
    if hasattr(res, 'items'):
        for k, v in res.items():
            res[k] = normalize(v)
    elif hasattr(res, '__getitem__'):
        for idx, v in zip(range(len(res)), res):
            res[idx] = normalize(v)
    if type(res) not in typemap and hasattr(res, '__dict__'):
        subattr = vars(res)
        for k, v in subattr.items():
            setattr(res, k, normalize(v))
    return res

Response: Normalize an AST nodes.


Instruction:
Make a summary of the following Python 3 code
def check(self, ndict: dict, info="") -> bool:
        def iscycle(thing, ndict: dict, info: str) -> bool:
            # check if not already here
            idthing = id(thing)
            ndict[info] = idthing
            if idthing not in ndict:
                # add myself
                ndict[idthing] = "%s:%s no cycle" % (type(thing), info)
                return False
            else:
                ndict[idthing] += "\n%s:%s cycle" % (type(thing), info)
                return True

        def recurs(thing, ndict: dict, info: str) -> bool:
            if not iscycle(thing, ndict, info):
                res = False
                if isinstance(thing, list):
                    idx = 0
                    for i in thing:
                        res |= recurs(i, ndict, "%s[%d]" % (info, idx))
                        idx += 1
                elif isinstance(thing, Node):
                    res |= thing.check(ndict, info)
                elif isinstance(thing, dict):
                    for k, v in thing.items():
                        res |= recurs(v, ndict, "%s[%s]" % (info, k))
                return res
            return True
        # add ME FIRST
        if len(ndict) == 0:
            ndict['self'] = id(self)
            info = 'self'
        if not iscycle(self, ndict, info):
            res = False
            if len(self) > 0:
                if hasattr(self, 'keys'):
                    keys = list(self.keys())
                    for k in keys:
                        ndict["[" + repr(k) + "]"] = id(self[k])
                        res |= recurs(self[k], ndict, "%s[%s]" % (info, k))
            keys = list(vars(self).keys())
            for k in keys:
                ndict["." + k] = id(getattr(self, k))
                res |= recurs(getattr(self, k), ndict, "%s.%s" % (info, k))
            return res
        return True

Response: Debug method help detect cycle and or other incoherence in a tree of Node objects


Instruction:
Implement a Python 3 function for
allowing to completly mutate the node into any subclasses of Node

Response:
def set(self, othernode):
        """allow to completly mutate the node into any subclasses of Node"""
        self.__class__ = othernode.__class__
        self.clean()
        if len(othernode) > 0:
            for k, v in othernode.items():
                self[k] = v
        for k, v in vars(othernode).items():
            setattr(self, k, v)


Instruction:
Can you generate the documentation for the following Python 3 function
def values(self):
        tmp = self
        while tmp is not None:
            yield tmp.data
            tmp = tmp.next

Response: Yield the values in order.


Instruction:
How would you code a function in Python 3 to
get the values in the order of the keys

Response:
def rvalues(self):
        """
        in reversed order
        """
        tmp = self
        while tmp is not None:
            yield tmp.data
            tmp = tmp.prev


Instruction:
How would you explain what the following Python 3 function does
def _pixel_masked(hit, array):
    ''' Checks whether a hit (column/row) is masked or not. Array is 2D array with boolean elements corresponding to pixles indicating whether a pixel is disabled or not.
    '''
    if array.shape[0] > hit["column"] and array.shape[1] > hit["row"]:
        return array[hit["column"], hit["row"]]
    else:
        return False

Response: Checks whether a hit is masked or not.


Instruction:
Write a Python 3 script to
finish the cluster based on the hits.

Response:
def _finish_cluster(hits, clusters, cluster_size, cluster_hit_indices, cluster_index, cluster_id, charge_correction, noisy_pixels, disabled_pixels):
    ''' Set hit and cluster information of the cluster (e.g. number of hits in the cluster (cluster_size), total cluster charge (charge), ...).
    '''
    cluster_charge = 0
    max_cluster_charge = -1
    # necessary for charge weighted hit position
    total_weighted_column = 0
    total_weighted_row = 0

    for i in range(cluster_size):
        hit_index = cluster_hit_indices[i]
        if hits[hit_index]['charge'] > max_cluster_charge:
            seed_hit_index = hit_index
            max_cluster_charge = hits[hit_index]['charge']
        hits[hit_index]['is_seed'] = 0
        hits[hit_index]['cluster_size'] = cluster_size
        # include charge correction in sum
        total_weighted_column += hits[hit_index]['column'] * (hits[hit_index]['charge'] + charge_correction)
        total_weighted_row += hits[hit_index]['row'] * (hits[hit_index]['charge'] + charge_correction)
        cluster_charge += hits[hit_index]['charge']
        hits[hit_index]['cluster_ID'] = cluster_id

    hits[seed_hit_index]['is_seed'] = 1

    clusters[cluster_index]["ID"] = cluster_id
    clusters[cluster_index]["n_hits"] = cluster_size
    clusters[cluster_index]["charge"] = cluster_charge
    clusters[cluster_index]['seed_column'] = hits[seed_hit_index]['column']
    clusters[cluster_index]['seed_row'] = hits[seed_hit_index]['row']
    # correct total charge value and calculate mean column and row
    clusters[cluster_index]['mean_column'] = float(total_weighted_column) / (cluster_charge + cluster_size * charge_correction)
    clusters[cluster_index]['mean_row'] = float(total_weighted_row) / (cluster_charge + cluster_size * charge_correction)

    # Call end of cluster function hook
    _end_of_cluster_function(
        hits=hits,
        clusters=clusters,
        cluster_size=cluster_size,
        cluster_hit_indices=cluster_hit_indices,
        cluster_index=cluster_index,
        cluster_id=cluster_id,
        charge_correction=charge_correction,
        noisy_pixels=noisy_pixels,
        disabled_pixels=disabled_pixels,
        seed_hit_index=seed_hit_index)


Instruction:
Create a Python 3 function for
setting hit and cluster information of the event (e.g. number of cluster in the event (n_cluster), ...).

Response:
def _finish_event(hits, clusters, start_event_hit_index, stop_event_hit_index, start_event_cluster_index, stop_event_cluster_index):
    ''' Set hit and cluster information of the event (e.g. number of cluster in the event (n_cluster), ...).
    '''
    for hit_index in range(start_event_hit_index, stop_event_hit_index):
        hits[hit_index]['n_cluster'] = stop_event_cluster_index - start_event_cluster_index

    for cluster_index in range(start_event_cluster_index, stop_event_cluster_index):
        clusters[cluster_index]['event_number'] = hits[start_event_hit_index]['event_number']

    # Call end of event function hook
    _end_of_event_function(
        hits=hits,
        clusters=clusters,
        start_event_hit_index=start_event_hit_index,
        stop_event_hit_index=stop_event_hit_index,
        start_event_cluster_index=start_event_cluster_index,
        stop_event_cluster_index=stop_event_cluster_index)


Instruction:
How would you implement a function in Python 3 that
checks if given hit is withing the limits.

Response:
def _hit_ok(hit, min_hit_charge, max_hit_charge):
    ''' Check if given hit is withing the limits.
    '''
    # Omit hits with charge < min_hit_charge
    if hit['charge'] < min_hit_charge:
        return False

    # Omit hits with charge > max_hit_charge
    if max_hit_charge != 0 and hit['charge'] > max_hit_charge:
        return False

    return True


Instruction:
Can you generate the documentation for the following Python 3 function
def _set_1d_array(array, value, size=-1):
    ''' Set array elemets to value for given number of elements (if size is negative number set all elements to value).
    '''
    if size >= 0:
        for i in range(size):
            array[i] = value
    else:
        for i in range(array.shape[0]):
            array[i] = value

Response: Set array elemets to value for given number of elements.