Instruction:
Implement a Python 3 function for
calling a bytecode function func

Response:
def _call(self, func, this, args):
        ''' Calls a bytecode function func
            NOTE:  use !ONLY! when calling functions from native methods! '''
        assert not func.is_native
        # fake call - the the runner to return to the end of the file
        old_contexts = self.contexts
        old_return_locs = self.return_locs
        old_curr_ctx = self.current_ctx

        self.contexts = [FakeCtx()]
        self.return_locs = [len(self.tape)]  # target line after return

        # prepare my ctx
        my_ctx = func._generate_my_context(this, args)
        self.current_ctx = my_ctx

        # execute dunction
        ret = self.run(my_ctx, starting_loc=self.label_locs[func.code])

        # bring back old execution
        self.current_ctx = old_curr_ctx
        self.contexts = old_contexts
        self.return_locs = old_return_locs

        return ret


Instruction:
Given the following Python 3 function, write the documentation
def pad(num, n=2, sign=False):
    '''returns n digit string representation of the num'''
    s = unicode(abs(num))
    if len(s) < n:
        s = '0' * (n - len(s)) + s
    if not sign:
        return s
    if num >= 0:
        return '+' + s
    else:
        return '-' + s

Response: returns n digit string representation of the num


Instruction:
Given the following Python 3 function, write the documentation
def replacement_template(rep, source, span, npar):
    n = 0
    res = ''
    while n < len(rep) - 1:
        char = rep[n]
        if char == '$':
            if rep[n + 1] == '$':
                res += '$'
                n += 2
                continue
            elif rep[n + 1] == '`':
                # replace with string that is BEFORE match
                res += source[:span[0]]
                n += 2
                continue
            elif rep[n + 1] == '\'':
                # replace with string that is AFTER match
                res += source[span[1]:]
                n += 2
                continue
            elif rep[n + 1] in DIGS:
                dig = rep[n + 1]
                if n + 2 < len(rep) and rep[n + 2] in DIGS:
                    dig += rep[n + 2]
                num = int(dig)
                # we will not do any replacements if we dont have this npar or dig is 0
                if not num or num > len(npar):
                    res += '$' + dig
                else:
                    # None - undefined has to be replaced with ''
                    res += npar[num - 1] if npar[num - 1] else ''
                n += 1 + len(dig)
                continue
        res += char
        n += 1
    if n < len(rep):
        res += rep[-1]
    return res

Response: Takes the replacement template and some info about the match and returns filled template


Instruction:
Make a summary of the following Python 3 code
def fix_js_args(func):
    '''Use this function when unsure whether func takes this and arguments as its last 2 args.
       It will append 2 args if it does not.'''
    fcode = six.get_function_code(func)
    fargs = fcode.co_varnames[fcode.co_argcount - 2:fcode.co_argcount]
    if fargs == ('this', 'arguments') or fargs == ('arguments', 'var'):
        return func
    code = append_arguments(six.get_function_code(func), ('this', 'arguments'))

    return types.FunctionType(
        code,
        six.get_function_globals(func),
        func.__name__,
        closure=six.get_function_closure(func))

Response: Use this function when unsure whether func takes this and arguments as its last 2 args. It will append 2 args if it does not.


Instruction:
Can you tell what is the following Python 3 function doing
def emit(self, what, *args):
        ''' what can be either name of the op, or node, or a list of statements.'''
        if isinstance(what, basestring):
            return self.exe.emit(what, *args)
        elif isinstance(what, list):
            self._emit_statement_list(what)
        else:
            return getattr(self, what['type'])(**what)

Response: emit what to the exe and return the result.


Instruction:
Create a Python 3 function to
return string representation of this object

Response:
def to_key(literal_or_identifier):
    ''' returns string representation of this object'''
    if literal_or_identifier['type'] == 'Identifier':
        return literal_or_identifier['name']
    elif literal_or_identifier['type'] == 'Literal':
        k = literal_or_identifier['value']
        if isinstance(k, float):
            return unicode(float_repr(k))
        elif 'regex' in literal_or_identifier:
            return compose_regex(k)
        elif isinstance(k, bool):
            return 'true' if k else 'false'
        elif k is None:
            return 'null'
        else:
            return unicode(k)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def trans(ele, standard=False):
    try:
        node = globals().get(ele['type'])
        if not node:
            raise NotImplementedError('%s is not supported!' % ele['type'])
        if standard:
            node = node.__dict__[
                'standard'] if 'standard' in node.__dict__ else node
        return node(**ele)
    except:
        #print ele
        raise

Response: Translates esprima syntax tree to python by delegating to appropriate translating node


Instruction:
How would you explain what the following Python 3 function does
def limited(func):
    '''Decorator limiting resulting line length in order to avoid python parser stack overflow -
      If expression longer than LINE_LEN_LIMIT characters then it will be moved to upper line
     USE ONLY ON EXPRESSIONS!!! '''

    def f(standard=False, **args):
        insert_pos = len(
            inline_stack.names
        )  # in case line is longer than limit we will have to insert the lval at current position
        # this is because calling func will change inline_stack.
        # we cant use inline_stack.require here because we dont know whether line overflows yet
        res = func(**args)
        if len(res) > LINE_LEN_LIMIT:
            name = inline_stack.require('LONG')
            inline_stack.names.pop()
            inline_stack.names.insert(insert_pos, name)
            res = 'def %s(var=var):\n    return %s\n' % (name, res)
            inline_stack.define(name, res)
            return name + '()'
        else:
            return res

    f.__dict__['standard'] = func
    return f

Response:
Decorator to limit resulting line length in order to avoid python parser stack overflow -
      If expression longer than LINE_LEN_LIMIT characters then it will be moved to upper line
     USE ONLY ON EXPRESSIONS!!!


Instruction:
Implement a Python 3 function for
doing not chceck whether t is not resticted or internal

Response:
def is_lval(t):
    """Does not chceck whether t is not resticted or internal"""
    if not t:
        return False
    i = iter(t)
    if i.next() not in IDENTIFIER_START:
        return False
    return all(e in IDENTIFIER_PART for e in i)


Instruction:
Can you generate the documentation for the following Python 3 function
def is_valid_lval(t):
    if not is_internal(t) and is_lval(t) and t not in RESERVED_NAMES:
        return True
    return False

Response: Checks whether t is a valid JS identifier name.


Instruction:
Implement a function in Python 3 to
translate a javascript source code to esprima syntax tree.

Response:
def translate_js(js, HEADER=DEFAULT_HEADER, use_compilation_plan=False):
    """js has to be a javascript source code.
       returns equivalent python code."""
    if use_compilation_plan and not '//' in js and not '/*' in js:
        return translate_js_with_compilation_plan(js, HEADER=HEADER)
    parser = pyjsparser.PyJsParser()
    parsed = parser.parse(js)  # js to esprima syntax tree
    # Another way of doing that would be with my auto esprima translation but its much slower and causes import problems:
    # parsed = esprima.parse(js).to_dict()
    translating_nodes.clean_stacks()
    return HEADER + translating_nodes.trans(
        parsed)


Instruction:
Can you tell what is the following Python 3 function doing
def translate_js_with_compilation_plan(js, HEADER=DEFAULT_HEADER):

    match_increaser_str, match_increaser_num, compilation_plan = get_compilation_plan(
        js)

    cp_hash = hashlib.md5(compilation_plan.encode('utf-8')).digest()
    try:
        python_code = cache[cp_hash]['proto_python_code']
    except:
        parser = pyjsparser.PyJsParser()
        parsed = parser.parse(compilation_plan)  # js to esprima syntax tree
        # Another way of doing that would be with my auto esprima translation but its much slower and causes import problems:
        # parsed = esprima.parse(js).to_dict()
        translating_nodes.clean_stacks()
        python_code = translating_nodes.trans(
            parsed)  # syntax tree to python code
        cache[cp_hash] = {
            'compilation_plan': compilation_plan,
            'proto_python_code': python_code,
        }

    python_code = match_increaser_str.wrap_up(python_code)
    python_code = match_increaser_num.wrap_up(python_code)

    return HEADER + python_code

Response: Translate a javascript source code with a compilation plan.


Instruction:
Implement a Python 3 function for
importing from javascript source file.

Response:
def import_js(path, lib_name, globals):
    """Imports from javascript source file.
      globals is your globals()"""
    with codecs.open(path_as_local(path), "r", "utf-8") as f:
        js = f.read()
    e = EvalJs()
    e.execute(js)
    var = e.context['var']
    globals[lib_name] = var.to_python()


Instruction:
Can you write a function in Python 3 where it
translates a JS file to python and saves it to the output path.

Response:
def translate_file(input_path, output_path):
    '''
    Translates input JS file to python and saves the it to the output path.
    It appends some convenience code at the end so that it is easy to import JS objects.

    For example we have a file 'example.js' with:   var a = function(x) {return x}
    translate_file('example.js', 'example.py')

    Now example.py can be easily importend and used:
    >>> from example import example
    >>> example.a(30)
    30
    '''
    js = get_file_contents(input_path)

    py_code = translate_js(js)
    lib_name = os.path.basename(output_path).split('.')[0]
    head = '__all__ = [%s]\n\n# Don\'t look below, you will not understand this Python code :) I don\'t.\n\n' % repr(
        lib_name)
    tail = '\n\n# Add lib to the module scope\n%s = var.to_python()' % lib_name
    out = head + py_code + tail
    write_file_contents(output_path, out)


Instruction:
Can you tell what is the following Python 3 function doing
def run_file(path_or_file, context=None):
    ''' Context must be EvalJS object. Runs given path as a JS program. Returns (eval_value, context).
    '''
    if context is None:
        context = EvalJs()
    if not isinstance(context, EvalJs):
        raise TypeError('context must be the instance of EvalJs')
    eval_value = context.eval(get_file_contents(path_or_file))
    return eval_value, context

Response: Runs given path as a JS program. Returns the eval_value and context.


Instruction:
How would you code a function in Python 3 to
execute javascript js in current context

Response:
def execute(self, js=None, use_compilation_plan=False):
        """executes javascript js in current context

        During initial execute() the converted js is cached for re-use. That means next time you
        run the same javascript snippet you save many instructions needed to parse and convert the
        js code to python code.

        This cache causes minor overhead (a cache dicts is updated) but the Js=>Py conversion process
        is typically expensive compared to actually running the generated python code.

        Note that the cache is just a dict, it has no expiration or cleanup so when running this
        in automated situations with vast amounts of snippets it might increase memory usage.
        """
        try:
            cache = self.__dict__['cache']
        except KeyError:
            cache = self.__dict__['cache'] = {}
        hashkey = hashlib.md5(js.encode('utf-8')).digest()
        try:
            compiled = cache[hashkey]
        except KeyError:
            code = translate_js(
                js, '', use_compilation_plan=use_compilation_plan)
            compiled = cache[hashkey] = compile(code, '<EvalJS snippet>',
                                                'exec')
        exec (compiled, self._context)


Instruction:
Implement a function in Python 3 to
evaluate expression in current context and returns its value

Response:
def eval(self, expression, use_compilation_plan=False):
        """evaluates expression in current context and returns its value"""
        code = 'PyJsEvalResult = eval(%s)' % json.dumps(expression)
        self.execute(code, use_compilation_plan=use_compilation_plan)
        return self['PyJsEvalResult']


Instruction:
Can you tell what is the following Python 3 function doing
def execute_debug(self, js):
        code = translate_js(js, '')
        # make sure you have a temp folder:
        filename = 'temp' + os.sep + '_' + hashlib.md5(
            code.encode("utf-8")).hexdigest() + '.py'
        try:
            with open(filename, mode='w') as f:
                f.write(code)
            with open(filename, "r") as f:
                pyCode = compile(f.read(), filename, 'exec')
                exec(pyCode, self._context)

        except Exception as err:
            raise err
        finally:
            os.remove(filename)
            try:
                os.remove(filename + 'c')
            except:
                pass

Response: execute the javascript js in current context


Instruction:
How would you explain what the following Python 3 function does
def eval_debug(self, expression):
        code = 'PyJsEvalResult = eval(%s)' % json.dumps(expression)
        self.execute_debug(code)
        return self['PyJsEvalResult']

Response:
evaluates expression in current context and returns its value
        as opposed to the execute method


Instruction:
Can you generate the documentation for the following Python 3 function
def console(self):
        while True:
            if six.PY2:
                code = raw_input('>>> ')
            else:
                code = input('>>>')
            try:
                print(self.eval(code))
            except KeyboardInterrupt:
                break
            except Exception as e:
                import traceback
                if DEBUG:
                    sys.stderr.write(traceback.format_exc())
                else:
                    sys.stderr.write('EXCEPTION: ' + str(e) + '\n')
                time.sleep(0.01)

Response: starts to interact with interactive console


Instruction:
Can you generate the documentation for the following Python 3 function
def to_key(literal_or_identifier):
    ''' returns string representation of this object'''
    if literal_or_identifier['type'] == 'Identifier':
        return literal_or_identifier['name']
    elif literal_or_identifier['type'] == 'Literal':
        k = literal_or_identifier['value']
        if isinstance(k, float):
            return unicode(float_repr(k))
        elif 'regex' in literal_or_identifier:
            return compose_regex(k)
        elif isinstance(k, bool):
            return u'true' if k else u'false'
        elif k is None:
            return u'null'
        else:
            return unicode(k)

Response: returns string representation of this object


Instruction:
Can you implement a function in Python 3 that
translates functions and all nested functions to Python code.

Response:
def translate_func(name, block, args):
    """Translates functions and all nested functions to Python code.
       name -  name of that function (global functions will be available under var while
            inline will be available directly under this name )
       block - code of the function (*with* brackets {} )
       args - arguments that this function takes"""
    inline = name.startswith('PyJsLvalInline')
    real_name = ''
    if inline:
        name, real_name = name.split('@')
    arglist = ', '.join(args) + ', ' if args else ''
    code = '@Js\ndef %s(%sthis, arguments, var=var):\n' % (name, arglist)
    # register local variables
    scope = "'this':this, 'arguments':arguments"  #it will be a simple dictionary
    for arg in args:
        scope += ', %s:%s' % (repr(arg), arg)
    if real_name:
        scope += ', %s:%s' % (repr(real_name), name)
    code += indent('var = Scope({%s}, var)\n' % scope)
    block, nested_hoisted, nested_inline = remove_functions(block)
    py_code, to_register = translate_flow(block)
    #register variables declared with var and names of hoisted functions.
    to_register += nested_hoisted.keys()
    if to_register:
        code += indent('var.registers(%s)\n' % str(to_register))
    for nested_name, info in nested_hoisted.iteritems():
        nested_block, nested_args = info
        new_code = translate_func('PyJsLvalTempHoisted', nested_block,
                                  nested_args)
        # Now put definition of hoisted function on the top
        code += indent(new_code)
        code += indent(
            'PyJsLvalTempHoisted.func_name = %s\n' % repr(nested_name))
        code += indent(
            'var.put(%s, PyJsLvalTempHoisted)\n' % repr(nested_name))
    for nested_name, info in nested_inline.iteritems():
        nested_block, nested_args = info
        new_code = translate_func(nested_name, nested_block, nested_args)
        # Inject definitions of inline functions just before usage
        # nested inline names have this format : LVAL_NAME@REAL_NAME
        py_code = inject_before_lval(py_code,
                                     nested_name.split('@')[0], new_code)
    if py_code.strip():
        code += indent(py_code)
    return code


Instruction:
Here you have a function in Python 3, explain what it does
def pass_bracket(source, start, bracket='()'):
    e = bracket_split(source[start:], [bracket], False)
    try:
        cand = e.next()
    except StopIteration:
        return None, None
    if not cand.strip():  #white space...
        try:
            res = e.next()
            return res, start + len(cand) + len(res)
        except StopIteration:
            return None, None
    elif cand[-1] == bracket[1]:
        return cand, start + len(cand)
    else:
        return None, None

Response:
Returns content of brackets with brackets and first pos after brackets
     Otherwise None


Instruction:
Can you generate the documentation for the following Python 3 function
def except_token(source, start, token, throw=True):
    start = pass_white(source, start)
    if start < len(source) and source[start] == token:
        return start + 1
    if throw:
        raise SyntaxError('Missing token. Expected %s' % token)
    return None

Response: Returns the position after token if found.


Instruction:
How would you implement a function in Python 3 that
returns the position after the keyword if found else None.

Response:
def except_keyword(source, start, keyword):
    """ Returns position after keyword if found else None
        Note: skips white space"""
    start = pass_white(source, start)
    kl = len(keyword)  #keyword len
    if kl + start > len(source):
        return None
    if source[start:start + kl] != keyword:
        return None
    if kl + start < len(source) and source[start + kl] in IDENTIFIER_PART:
        return None
    return start + kl


Instruction:
Create a Python 3 function to
parse a string from start and returns first identifier if identifier invalid and raises SyntaxError otherwise returns None

Response:
def parse_identifier(source, start, throw=True):
    """passes white space from start and returns first identifier,
       if identifier invalid and throw raises SyntaxError otherwise returns None"""
    start = pass_white(source, start)
    end = start
    if not end < len(source):
        if throw:
            raise SyntaxError('Missing identifier!')
        return None
    if source[end] not in IDENTIFIER_START:
        if throw:
            raise SyntaxError('Invalid identifier start: "%s"' % source[end])
        return None
    end += 1
    while end < len(source) and source[end] in IDENTIFIER_PART:
        end += 1
    if not is_valid_lval(source[start:end]):
        if throw:
            raise SyntaxError(
                'Invalid identifier name: "%s"' % source[start:end])
        return None
    return source[start:end], end


Instruction:
Explain what the following Python 3 code does
def argsplit(args, sep=','):
    parsed_len = 0
    last = 0
    splits = []
    for e in bracket_split(args, brackets=['()', '[]', '{}']):
        if e[0] not in {'(', '[', '{'}:
            for i, char in enumerate(e):
                if char == sep:
                    splits.append(args[last:parsed_len + i])
                    last = parsed_len + i + 1
        parsed_len += len(e)
    splits.append(args[last:])
    return splits

Response: used to split JS args into array and object elements


Instruction:
How would you code a function in Python 3 to
specialize function splitting text at add and sub operators.

Response:
def split_add_ops(text):
    """Specialized function splitting text at add/sub operators.
    Operands are *not* translated. Example result ['op1', '+', 'op2', '-', 'op3']"""
    n = 0
    text = text.replace('++', '##').replace(
        '--', '@@')  #text does not normally contain any of these
    spotted = False  # set to true if noticed anything other than +- or white space
    last = 0
    while n < len(text):
        e = text[n]
        if e == '+' or e == '-':
            if spotted:
                yield text[last:n].replace('##', '++').replace('@@', '--')
                yield e
                last = n + 1
                spotted = False
        elif e == '/' or e == '*' or e == '%':
            spotted = False
        elif e != ' ':
            spotted = True
        n += 1
    yield text[last:n].replace('##', '++').replace('@@', '--')


Instruction:
Can you create a Python 3 function that
splits at any of the specified set of items in the specified order

Response:
def split_at_any(text,
                 lis,
                 translate=False,
                 not_before=[],
                 not_after=[],
                 validitate=None):
    """ doc """
    lis.sort(key=lambda x: len(x), reverse=True)
    last = 0
    n = 0
    text_len = len(text)
    while n < text_len:
        if any(text[:n].endswith(e)
               for e in not_before):  #Cant end with end before
            n += 1
            continue
        for e in lis:
            s = len(e)
            if s + n > text_len:
                continue
            if validitate and not validitate(e, text[:n], text[n + s:]):
                continue
            if any(text[n + s:].startswith(e)
                   for e in not_after):  #Cant end with end before
                n += 1
                break
            if e == text[n:n + s]:
                yield text[last:n] if not translate else translate(
                    text[last:n])
                yield e
                n += s
                last = n
                break
        else:
            n += 1
    yield text[last:n] if not translate else translate(text[last:n])


Instruction:
Create a Python 3 function for
working like text. split but separated fragments cant end with not_before and start with not_after

Response:
def split_at_single(text, sep, not_before=[], not_after=[]):
    """Works like text.split(sep) but separated fragments
    cant end with not_before or start with not_after"""
    n = 0
    lt, s = len(text), len(sep)
    last = 0
    while n < lt:
        if not s + n > lt:
            if sep == text[n:n + s]:
                if any(text[last:n].endswith(e) for e in not_before):
                    pass
                elif any(text[n + s:].startswith(e) for e in not_after):
                    pass
                else:
                    yield text[last:n]
                    last = n + s
                    n += s - 1
        n += 1
    yield text[last:]


Instruction:
How would you implement a function in Python 3 that
returns Python array from Js array

Response:
def to_arr(this):
    """Returns Python array from Js array"""
    return [this.get(str(e)) for e in xrange(len(this))]


Instruction:
Explain what the following Python 3 code does
def transform_crap(code):  #needs some more tests
    ind = code.rfind('?')
    if ind == -1:
        return code
    sep = code.find(':', ind)
    if sep == -1:
        raise SyntaxError('Invalid ?: syntax (probably missing ":" )')
    beg = max(code.rfind(':', 0, ind), code.find('?', 0, ind)) + 1
    end = code.find(':', sep + 1)
    end = len(code) if end == -1 else end
    formula = '(' + code[ind + 1:sep] + ' if ' + code[
        beg:ind] + ' else ' + code[sep + 1:end] + ')'
    return transform_crap(code[:beg] + formula + code[end:])

Response: Transforms this? : crap into if else python syntax


Instruction:
Can you implement a function in Python 3 that
performs this operation on a list from right to left *

Response:
def rl(self, lis, op):
        """performs this operation on a list from *right to left*
        op must take 2 args
        a,b,c  => op(a, op(b, c))"""
        it = reversed(lis)
        res = trans(it.next())
        for e in it:
            e = trans(e)
            res = op(e, res)
        return res


Instruction:
How would you explain what the following Python 3 function does
def lr(self, lis, op):
        it = iter(lis)
        res = trans(it.next())
        for e in it:
            e = trans(e)
            res = op(res, e)
        return res

Response: performs this operation on a list from left to right *


Instruction:
Here you have a function in Python 3, explain what it does
def translate(self):
        if not self.code:
            return ''
        new = bracket_replace(self.code)
        #Check comma operator:
        cand = new.split(',')  #every comma in new must be an operator
        if len(cand) > 1:  #LR
            return self.lr(cand, js_comma)
        #Check = operator:
        # dont split at != or !== or == or === or <= or >=
        #note <<=, >>= or this >>> will NOT be supported
        # maybe I will change my mind later
        # Find this crappy ?:
        if '?' in new:
            cond_ind = new.find('?')
            tenary_start = 0
            for ass in re.finditer(ASSIGNMENT_MATCH, new):
                cand = ass.span()[1]
                if cand < cond_ind:
                    tenary_start = cand
                else:
                    break
            actual_tenary = new[tenary_start:]
            spl = ''.join(split_at_any(new, [':', '?'], translate=trans))
            tenary_translation = transform_crap(spl)
            assignment = new[:tenary_start] + ' PyJsConstantTENARY'
            return trans(assignment).replace('PyJsConstantTENARY',
                                             tenary_translation)
        cand = list(split_at_single(new, '=', ['!', '=', '<', '>'], ['=']))
        if len(cand) > 1:  # RL
            it = reversed(cand)
            res = trans(it.next())
            for e in it:
                e = e.strip()
                if not e:
                    raise SyntaxError('Missing left-hand in assignment!')
                op = ''
                if e[-2:] in OP_METHODS:
                    op = ',' + e[-2:].__repr__()
                    e = e[:-2]
                elif e[-1:] in OP_METHODS:
                    op = ',' + e[-1].__repr__()
                    e = e[:-1]
                e = trans(e)
                #Now replace last get method with put and change args
                c = list(bracket_split(e, ['()']))
                beg, arglist = ''.join(c[:-1]).strip(), c[-1].strip(
                )  #strips just to make sure... I will remove it later
                if beg[-4:] != '.get':
                    raise SyntaxError('Invalid left-hand side in assignment')
                beg = beg[0:-3] + 'put'
                arglist = arglist[0:-1] + ', ' + res + op + ')'
                res = beg + arglist
            return res
        #Now check remaining 2 arg operators that are not handled by python
        #They all have Left to Right (LR) associativity
        order = [OR, AND, BOR, BXOR, BAND, EQS, COMPS, BSHIFTS, ADDS, MULTS]
        # actually we dont need OR and AND because they can be handled easier. But just for fun
        dangerous = ['<', '>']
        for typ in order:
            #we have to use special method for ADDS since they can be also unary operation +/++ or -/-- FUCK
            if '+' in typ:
                cand = list(split_add_ops(new))
            else:
                #dont translate. cant start or end on dangerous op.
                cand = list(
                    split_at_any(
                        new,
                        typ.keys(),
                        False,
                        dangerous,
                        dangerous,
                        validitate=comb_validitator))
            if not len(cand) > 1:
                continue
            n = 1
            res = trans(cand[0])
            if not res:
                raise SyntaxError("Missing operand!")
            while n < len(cand):
                e = cand[n]
                if not e:
                    raise SyntaxError("Missing operand!")
                if n % 2:
                    op = typ[e]
                else:
                    res = op(res, trans(e))
                n += 1
            return res
        #Now replace unary operators - only they are left
        cand = list(
            split_at_any(
                new, UNARY.keys(), False, validitate=unary_validitator))
        if len(cand) > 1:  #contains unary operators
            if '++' in cand or '--' in cand:  #it cant contain both ++ and --
                if '--' in cand:
                    op = '--'
                    meths = js_post_dec, js_pre_dec
                else:
                    op = '++'
                    meths = js_post_inc, js_pre_inc
                pos = cand.index(op)
                if cand[pos - 1].strip():  # post increment
                    a = cand[pos - 1]
                    meth = meths[0]
                elif cand[pos + 1].strip():  #pre increment
                    a = cand[pos + 1]
                    meth = meths[1]
                else:
                    raise SyntaxError('Invalid use of ++ operator')
                if cand[pos + 2:]:
                    raise SyntaxError('Too many operands')
                operand = meth(trans(a))
                cand = cand[:pos - 1]
            # now last cand should be operand and every other odd element should be empty
            else:
                operand = trans(cand[-1])
                del cand[-1]
            for i, e in enumerate(reversed(cand)):
                if i % 2:
                    if e.strip():
                        raise SyntaxError('Too many operands')
                else:
                    operand = UNARY[e](operand)
            return operand
        #Replace brackets
        if new[0] == '@' or new[0] == '#':
            if len(
                    list(bracket_split(new, ('#{', '@}')))
            ) == 1:  # we have only one bracket, otherwise pseudobracket like @@....
                assert new in REPL
                if new[0] == '#':
                    raise SyntaxError(
                        '[] cant be used as brackets! Use () instead.')
                return '(' + trans(REPL[new][1:-1]) + ')'
        #Replace function calls and prop getters
        # 'now' must be a reference like: a or b.c.d but it can have also calls or getters ( for example a["b"](3))
        #From here @@ means a function call and ## means get operation (note they dont have to present)
        it = bracket_split(new, ('#{', '@}'))
        res = []
        for e in it:
            if e[0] != '#' and e[0] != '@':
                res += [x.strip() for x in e.split('.')]
            else:
                res += [e.strip()]
        # res[0] can be inside @@ (name)...
        res = filter(lambda x: x, res)
        if is_internal(res[0]):
            out = res[0]
        elif res[0][0] in {'#', '@'}:
            out = '(' + trans(REPL[res[0]][1:-1]) + ')'
        elif is_valid_lval(
                res[0]) or res[0] in {'this', 'false', 'true', 'null'}:
            out = 'var.get(' + res[0].__repr__() + ')'
        else:
            if is_reserved(res[0]):
                raise SyntaxError('Unexpected reserved word: "%s"' % res[0])
            raise SyntaxError('Invalid identifier: "%s"' % res[0])
        if len(res) == 1:
            return out
        n = 1
        while n < len(res):  #now every func call is a prop call
            e = res[n]
            if e[0] == '@':  # direct call
                out += trans_args(REPL[e])
                n += 1
                continue
            args = False  #assume not prop call
            if n + 1 < len(res) and res[n + 1][0] == '@':  #prop call
                args = trans_args(REPL[res[n + 1]])[1:]
                if args != ')':
                    args = ',' + args
            if e[0] == '#':
                prop = trans(REPL[e][1:-1])
            else:
                if not is_lval(e):
                    raise SyntaxError('Invalid identifier: "%s"' % e)
                prop = e.__repr__()
            if args:  # prop call
                n += 1
                out += '.callprop(' + prop + args
            else:  #prop get
                out += '.get(' + prop + ')'
            n += 1
        return out

Response: Translates outer operation and calls translate on inner operation.


Instruction:
Can you write a function in Python 3 where it
returns None if not found other functions that begin with do_

Response:
def do_statement(source, start):
    """returns none if not found other functions that begin with 'do_' raise
    also this do_ type function passes white space"""
    start = pass_white(source, start)
    # start is the fist position after initial start that is not a white space or \n
    if not start < len(source):  #if finished parsing return None
        return None, start
    if any(startswith_keyword(source[start:], e) for e in {'case', 'default'}):
        return None, start
    rest = source[start:]
    for key, meth in KEYWORD_METHODS.iteritems(
    ):  # check for statements that are uniquely defined by their keywords
        if rest.startswith(key):
            # has to startwith this keyword and the next letter after keyword must be either EOF or not in IDENTIFIER_PART
            if len(key) == len(rest) or rest[len(key)] not in IDENTIFIER_PART:
                return meth(source, start)
    if rest[0] == '{':  #Block
        return do_block(source, start)
    # Now only label and expression left
    cand = parse_identifier(source, start, False)
    if cand is not None:  # it can mean that its a label
        label, cand_start = cand
        cand_start = pass_white(source, cand_start)
        if source[cand_start] == ':':
            return do_label(source, start)
    return do_expression(source, start)


Instruction:
Write a Python 3 script for
matching a string at the given position

Response:
def match(self, string, pos):
        '''string is of course a py string'''
        return self.pat.match(string, int(pos))


Instruction:
Can you implement a function in Python 3 that
checks if n is empty object

Response:
def is_empty_object(n, last):
    """n may be the inside of block or object"""
    if n.strip():
        return False
    # seems to be but can be empty code
    last = last.strip()
    markers = {
        ')',
        ';',
    }
    if not last or last[-1] in markers:
        return False
    return True


Instruction:
Here you have a function in Python 3, explain what it does
def is_object(n, last):
    if is_empty_object(n, last):
        return True
    if not n.strip():
        return False
    #Object contains lines of code so it cant be an object
    if len(argsplit(n, ';')) > 1:
        return False
    cands = argsplit(n, ',')
    if not cands[-1].strip():
        return True  # {xxxx,} empty after last , it must be an object
    for cand in cands:
        cand = cand.strip()
        # separate each candidate element at : in dict and check whether they are correct...
        kv = argsplit(cand, ':')
        if len(
                kv
        ) > 2:  # set the len of kv to 2 because of this stupid : expression
            kv = kv[0], ':'.join(kv[1:])

        if len(kv) == 2:
            # key value pair, check whether not label or ?:
            k, v = kv
            if not is_lval(k.strip()):
                return False
            v = v.strip()
            if v.startswith('function'):
                continue
            #will fail on label... {xxx: while {}}
            if v[0] == '{':  # value cant be a code block
                return False
            for e in KEYWORD_METHODS:
                # if v starts with any statement then return false
                if v.startswith(e) and len(e) < len(v) and v[len(
                        e)] not in IDENTIFIER_PART:
                    return False
        elif not (cand.startswith('set ') or cand.startswith('get ')):
            return False
    return True

Response: check if n is an object


Instruction:
Can you generate a brief explanation for the following Python 3 code
def remove_arrays(code, count=1):
    res = ''
    last = ''
    replacements = {}
    for e in bracket_split(code, ['[]']):
        if e[0] == '[':
            if is_array(last):
                name = ARRAY_LVAL % count
                res += ' ' + name
                replacements[name] = e
                count += 1
            else:  # pseudo array. But pseudo array can contain true array. for example a[['d'][3]] has 2 pseudo and 1 true array
                cand, new_replacements, count = remove_arrays(e[1:-1], count)
                res += '[%s]' % cand
                replacements.update(new_replacements)
        else:
            res += e
        last = e
    return res, replacements, count

Response:
removes arrays and replaces them with ARRAY_LVALS
ATTRIBS


Instruction:
Make a summary of the following Python 3 code
def translate_array(array, lval, obj_count=1, arr_count=1):
    array = array[1:-1]
    array, obj_rep, obj_count = remove_objects(array, obj_count)
    array, arr_rep, arr_count = remove_arrays(array, arr_count)
    #functions can be also defined in arrays, this caused many problems since in Python
    # functions cant be defined inside literal
    # remove functions (they dont contain arrays or objects so can be translated easily)
    # hoisted functions are treated like inline
    array, hoisted, inline = functions.remove_functions(array, all_inline=True)
    assert not hoisted
    arr = []
    # separate elements in array
    for e in argsplit(array, ','):
        # translate expressions in array PyJsLvalInline will not be translated!
        e = exp_translator(e.replace('\n', ''))
        arr.append(e if e else 'None')
    arr = '%s = Js([%s])\n' % (lval, ','.join(arr))
    #But we can have more code to add to define arrays/objects/functions defined inside this array
    # translate nested objects:
    # functions:
    for nested_name, nested_info in inline.iteritems():
        nested_block, nested_args = nested_info
        new_def = FUNC_TRANSLATOR(nested_name, nested_block, nested_args)
        arr = new_def + arr
    for lval, obj in obj_rep.iteritems():
        new_def, obj_count, arr_count = translate_object(
            obj, lval, obj_count, arr_count)
        # add object definition BEFORE array definition
        arr = new_def + arr
    for lval, obj in arr_rep.iteritems():
        new_def, obj_count, arr_count = translate_array(
            obj, lval, obj_count, arr_count)
        # add object definition BEFORE array definition
        arr = new_def + arr
    return arr, obj_count, arr_count

Response: Translate a js array into a python code that can be used in the Python interpreter.


Instruction:
Here you have a function in Python 3, explain what it does
def _ensure_regexp(source, n):  #<- this function has to be improved
    '''returns True if regexp starts at n else returns False
      checks whether it is not a division '''
    markers = '(+~"\'=[%:?!*^|&-,;/\\'
    k = 0
    while True:
        k += 1
        if n - k < 0:
            return True
        char = source[n - k]
        if char in markers:
            return True
        if char != ' ' and char != '\n':
            break
    return False

Response: returns True if regexp starts at n else returns False


Instruction:
Can you generate the documentation for the following Python 3 function
def parse_num(source, start, charset):
    while start < len(source) and source[start] in charset:
        start += 1
    return start

Response: Returns a first index > = start of chat not in charset


Instruction:
Write a Python 3 script for
returning end of exponential, raises SyntaxError if failed

Response:
def parse_exponent(source, start):
    """returns end of exponential, raises SyntaxError if failed"""
    if not source[start] in {'e', 'E'}:
        if source[start] in IDENTIFIER_PART:
            raise SyntaxError('Invalid number literal!')
        return start
    start += 1
    if source[start] in {'-', '+'}:
        start += 1
    FOUND = False
    # we need at least one dig after exponent
    while source[start] in NUMS:
        FOUND = True
        start += 1
    if not FOUND or source[start] in IDENTIFIER_PART:
        raise SyntaxError('Invalid number literal!')
    return start


Instruction:
Implement a Python 3 function for
replacing Strings and Regexp literals in the source code with their corresponding constant.

Response:
def remove_constants(source):
    '''Replaces Strings and Regexp literals in the source code with
       identifiers and *removes comments*. Identifier is of the format:

       PyJsStringConst(String const number)_ - for Strings
       PyJsRegExpConst(RegExp const number)_ - for RegExps

       Returns dict which relates identifier and replaced constant.

       Removes single line and multiline comments from JavaScript source code
       Pseudo comments (inside strings) will not be removed.

       For example this line:
       var x = "/*PSEUDO COMMENT*/ TEXT //ANOTHER PSEUDO COMMENT"
       will be unaltered'''
    source = ' ' + source + '\n'
    comments = []
    inside_comment, single_comment = False, False
    inside_single, inside_double = False, False
    inside_regexp = False
    regexp_class_count = 0
    n = 0
    while n < len(source):
        char = source[n]
        if char == '"' and not (inside_comment or inside_single
                                or inside_regexp):
            if not _is_cancelled(source, n):
                if inside_double:
                    inside_double[1] = n + 1
                    comments.append(inside_double)
                    inside_double = False
                else:
                    inside_double = [n, None, 0]
        elif char == "'" and not (inside_comment or inside_double
                                  or inside_regexp):
            if not _is_cancelled(source, n):
                if inside_single:
                    inside_single[1] = n + 1
                    comments.append(inside_single)
                    inside_single = False
                else:
                    inside_single = [n, None, 0]
        elif (inside_single or inside_double):
            if char in LINE_TERMINATOR:
                if _is_cancelled(source, n):
                    if char == CR and source[n + 1] == LF:
                        n += 1
                    n += 1
                    continue
                else:
                    raise SyntaxError(
                        'Invalid string literal. Line terminators must be escaped!'
                    )
        else:
            if inside_comment:
                if single_comment:
                    if char in LINE_TERMINATOR:
                        inside_comment[1] = n
                        comments.append(inside_comment)
                        inside_comment = False
                        single_comment = False
                else:  # Multiline
                    if char == '/' and source[n - 1] == '*':
                        inside_comment[1] = n + 1
                        comments.append(inside_comment)
                        inside_comment = False
            elif inside_regexp:
                if not quiting_regexp:
                    if char in LINE_TERMINATOR:
                        raise SyntaxError(
                            'Invalid regexp literal. Line terminators cant appear!'
                        )
                    if _is_cancelled(source, n):
                        n += 1
                        continue
                    if char == '[':
                        regexp_class_count += 1
                    elif char == ']':
                        regexp_class_count = max(regexp_class_count - 1, 0)
                    elif char == '/' and not regexp_class_count:
                        quiting_regexp = True
                else:
                    if char not in IDENTIFIER_START:
                        inside_regexp[1] = n
                        comments.append(inside_regexp)
                        inside_regexp = False
            elif char == '/' and source[n - 1] == '/':
                single_comment = True
                inside_comment = [n - 1, None, 1]
            elif char == '*' and source[n - 1] == '/':
                inside_comment = [n - 1, None, 1]
            elif char == '/' and source[n + 1] not in ('/', '*'):
                if not _ensure_regexp(source, n):  #<- improve this one
                    n += 1
                    continue  #Probably just a division
                quiting_regexp = False
                inside_regexp = [n, None, 2]
            elif not (inside_comment or inside_regexp):
                if (char in NUMS and
                        source[n - 1] not in IDENTIFIER_PART) or char == '.':
                    if char == '.':
                        k = parse_num(source, n + 1, NUMS)
                        if k == n + 1:  # just a stupid dot...
                            n += 1
                            continue
                        k = parse_exponent(source, k)
                    elif char == '0' and source[n + 1] in {
                            'x', 'X'
                    }:  #Hex number probably
                        k = parse_num(source, n + 2, HEX)
                        if k == n + 2 or source[k] in IDENTIFIER_PART:
                            raise SyntaxError('Invalid hex literal!')
                    else:  #int or exp or flot or exp flot
                        k = parse_num(source, n + 1, NUMS)
                        if source[k] == '.':
                            k = parse_num(source, k + 1, NUMS)
                        k = parse_exponent(source, k)
                    comments.append((n, k, 3))
                    n = k
                    continue
        n += 1
    res = ''
    start = 0
    count = 0
    constants = {}
    for end, next_start, typ in comments:
        res += source[start:end]
        start = next_start
        if typ == 0:  # String
            name = StringName
        elif typ == 1:  # comment
            continue
        elif typ == 2:  # regexp
            name = RegExpName
        elif typ == 3:  # number
            name = NumberName
        else:
            raise RuntimeError()
        res += ' ' + name % count + ' '
        constants[name % count] = source[end:next_start]
        count += 1
    res += source[start:]
    # remove this stupid white space
    for e in WHITE:
        res = res.replace(e, ' ')
    res = res.replace(CR + LF, '\n')
    for e in LINE_TERMINATOR:
        res = res.replace(e, '\n')
    return res.strip(), constants


Instruction:
Make a summary of the following Python 3 code
def recover_constants(py_source,
                      replacements):  #now has n^2 complexity. improve to n
    '''Converts identifiers representing Js constants to the PyJs constants
    PyJsNumberConst_1_ which has the true value of 5 will be converted to PyJsNumber(5)'''
    for identifier, value in replacements.iteritems():
        if identifier.startswith('PyJsConstantRegExp'):
            py_source = py_source.replace(identifier,
                                          'JsRegExp(%s)' % repr(value))
        elif identifier.startswith('PyJsConstantString'):
            py_source = py_source.replace(
                identifier, 'Js(u%s)' % unify_string_literals(value))
        else:
            py_source = py_source.replace(identifier, 'Js(%s)' % value)
    return py_source

Response:
Converts identifiers representing Js constants to the PyJs constants
    PyJsNumberConst_1_ which has the true value of 5 will be converted to PyJsNumber ( 5 )


Instruction:
Given the following Python 3 function, write the documentation
def unify_string_literals(js_string):
    n = 0
    res = ''
    limit = len(js_string)
    while n < limit:
        char = js_string[n]
        if char == '\\':
            new, n = do_escape(js_string, n)
            res += new
        else:
            res += char
            n += 1
    return res

Response:
this function parses the string just like javascript
       for example literal \ d in JavaScript would be interpreted
       as '\\d' - backslash would be ignored


Instruction:
Given the following Python 3 function, write the documentation
def abstract_relational_comparison(self, other,
                                   self_first=True):  # todo speed up!
    ''' self<other if self_first else other<self.
       Returns the result of the question: is self smaller than other?
       in case self_first is false it returns the answer of:
                                           is other smaller than self.
       result is PyJs type: bool or undefined'''

    px = to_primitive(self, 'Number')
    py = to_primitive(other, 'Number')
    if not self_first:  # reverse order
        px, py = py, px
    if not (Type(px) == 'String' and Type(py) == 'String'):
        px, py = to_number(px), to_number(py)
        if is_nan(px) or is_nan(py):
            return None  # watch out here!
        return px < py  # same cmp algorithm
    else:
        # I am pretty sure that python has the same
        # string cmp algorithm but I have to confirm it
        return px < py

Response: abstract comparison of two objects


Instruction:
Implement a function in Python 3 to
return the result of JS == compare. js == compare. js == compare. js == compare. js == compare. js == compare. js == compare. js == compare. js == compare. js == compare. js == compare. js == compare. js == compare. js

Response:
def abstract_equality_op(self, other):
    ''' returns the result of JS == compare.
       result is PyJs type: bool'''
    tx, ty = Type(self), Type(other)
    if tx == ty:
        if tx == 'Undefined' or tx == 'Null':
            return True
        if tx == 'Number' or tx == 'String' or tx == 'Boolean':
            return self == other
        return self is other  # Object
    elif (tx == 'Undefined' and ty == 'Null') or (ty == 'Undefined'
                                                  and tx == 'Null'):
        return True
    elif tx == 'Number' and ty == 'String':
        return abstract_equality_op(self, to_number(other))
    elif tx == 'String' and ty == 'Number':
        return abstract_equality_op(to_number(self), other)
    elif tx == 'Boolean':
        return abstract_equality_op(to_number(self), other)
    elif ty == 'Boolean':
        return abstract_equality_op(self, to_number(other))
    elif (tx == 'String' or tx == 'Number') and is_object(other):
        return abstract_equality_op(self, to_primitive(other))
    elif (ty == 'String' or ty == 'Number') and is_object(self):
        return abstract_equality_op(to_primitive(self), other)
    else:
        return False


Instruction:
Can you create a Python 3 function that
checks if self is in other

Response:
def in_op(self, other):
    '''checks if self is in other'''
    if not is_object(other):
        raise MakeError(
            'TypeError',
            "You can\'t use 'in' operator to search in non-objects")
    return other.has_property(to_string(self))


Instruction:
Can you tell what is the following Python 3 function doing
def maybe_download_and_extract():
  dest_directory = '.'
  filename = DATA_URL.split('/')[-1]
  filepath = os.path.join(dest_directory, filename)
  if not os.path.exists(filepath):
    def _progress(count, block_size, total_size):
      sys.stdout.write('\r>> Downloading %s %.1f%%' % (filename,
          float(count * block_size) / float(total_size) * 100.0))
      sys.stdout.flush()
    filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath, _progress)
    print()
    statinfo = os.stat(filepath)
    print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')
  extracted_dir_path = os.path.join(dest_directory, 'trees')
  if not os.path.exists(extracted_dir_path):
    zip_ref = zipfile.ZipFile(filepath, 'r')
    zip_ref.extractall(dest_directory)
    zip_ref.close()

Response: Download and extract processed data and embeddings.


Instruction:
Explain what the following Python 3 code does
def make_network_graph(compact, expression_names, lookup_names):
  nodes = set()
#   edges = defaultdict(set) # parent -> (child, extra)

  var_name_dict = dict()
  if expression_names:
    for e in graphviz_items: # e: Expression
      if e in expression_names:
        var_name_dict[e.vindex] = expression_names[e]

  rnn_bldr_name = defaultdict(lambda: chr(len(rnn_bldr_name)+ord('A')))
  def vidx2str(vidx): return '%s%s' % ('N', vidx)

  for e in graphviz_items: # e: Expression
    vidx = e.vindex
    f_name = e.name
    args = e.args
    output_dim = e.dim
    input_dim = None # basically just RNNStates use this since everything else has input_dim==output_dim
    children = set()
    node_type = '2_regular'

    if f_name == 'vecInput':
      [_dim] = args
      arg_strs = []
    elif f_name == 'inputVector':
      [_v] = args
      arg_strs = []
    elif f_name == 'matInput':
      [_d1, _d2] = args
      arg_strs = []
    elif f_name == 'inputMatrix':
      [_v, _d] = args
      arg_strs = []
    elif f_name == 'parameters':
      [_dim] = args
      arg_strs = []
      if compact:
        if vidx in var_name_dict:
          f_name = var_name_dict[vidx]
      node_type = '1_param'
    elif f_name == 'lookup_parameters':
      [_dim] = args
      arg_strs = []
      if compact:
        if vidx in var_name_dict:
          f_name = var_name_dict[vidx]
      node_type = '1_param'
    elif f_name == 'lookup':
      [p, idx, update] = args
      [_dim] = p.args
      if vidx in var_name_dict:
        name = var_name_dict[vidx]
      else:
        name = None
      item_name = None
      if lookup_names and p in expression_names:
        param_name = expression_names[p]
        if param_name in lookup_names:
          item_name = '\\"%s\\"' % (lookup_names[param_name][idx],)
      if compact:
        if item_name is not None:
          f_name = item_name
        elif name is not None:
          f_name = '%s[%s]' % (name, idx)
        else:
          f_name = 'lookup(%s)' % (idx)
        arg_strs = []
      else:
        arg_strs = [var_name_dict.get(p.vindex, 'v%d' % (p.vindex))]
        if item_name is not None:
          arg_strs.append(item_name)
        vocab_size = _dim[0]
        arg_strs.extend(['%s' % (idx), '%s' % (vocab_size), 'update' if update else 'fixed'])
      #children.add(vidx2str(p.vindex))
      #node_type = '1_param'
    elif f_name == 'RNNState':
      [arg, input_dim, bldr_type, bldr_num, state_idx] = args # arg==input_e
      rnn_name = rnn_bldr_name[bldr_num]
      if bldr_type.endswith('Builder'):
        bldr_type[:-len('Builder')]
      f_name = '%s-%s-%s' % (bldr_type, rnn_name, state_idx)
      if not compact:
        i = arg.vindex
        s = var_name_dict.get(i, 'v%d' % (i))
        arg_strs = [s]
      else:
        arg_strs = []
      children.add(vidx2str(arg.vindex))
      node_type = '3_rnn_state'
    else:
      arg_strs = []
      for arg in args:
        if isinstance(arg, Expression):
          if not compact:
            i = arg.vindex
            s = var_name_dict.get(i, 'v%d' % (i))
            arg_strs.append(s)
          children.add(vidx2str(arg.vindex))
        elif isinstance(arg, float) and compact:
          s = re.sub('0+$', '', '%.3f' % (arg))
          if s == '0.':
            s = str(arg)
          arg_strs.append(s)
        else:
          arg_strs.append(str(arg))

#     f_name = { ,
#              }.get(f_name, f_name)

    if compact:
      f_name = { 'add': '+',
                 'sub': '-',
                 'mul': '*',
                 'div': '/',
                 'cadd': '+',
                 'cmul': '*',
                 'cdiv': '/',
                 'scalarsub': '-',
                 'concatenate': 'cat',
                 'esum': 'sum',
                 'emax': 'max',
                 'emin': 'min',
               }.get(f_name, f_name)
      if arg_strs:
        str_repr = '%s(%s)' % (f_name, ', '.join(arg_strs))
      else:
        str_repr = f_name
    elif f_name == 'add':
      [a,b] = arg_strs
      str_repr = '%s + %s' % (a,b)
    elif f_name == 'sub':
      [a,b] = arg_strs
      str_repr = '%s - %s' % (a,b)
    elif f_name == 'mul':
      [a,b] = arg_strs
      str_repr = '%s * %s' % (a,b)
    elif f_name == 'div':
      [a,b] = arg_strs
      str_repr = '%s / %s' % (a,b)
    elif f_name == 'neg':
      [a,] = arg_strs
      str_repr = '-%s' % (a)
    elif f_name == 'affine_transform':
      str_repr = arg_strs[0]
      for i in xrange(1, len(arg_strs), 2):
        str_repr += ' + %s*%s' % tuple(arg_strs[i:i+2])
    else:
      if arg_strs is not None:
        str_repr = '%s(%s)' % (f_name, ', '.join(arg_strs))
      else:
        str_repr = f_name

    name = vidx2str(vidx)
    var_name = '%s' % (var_name_dict.get(vidx, 'v%d' % (vidx))) if not compact else ''
#     if show_dims:
#       str_repr = '%s\\n%s' % (shape_str(e.dim), str_repr)
    label = str_repr
    if not compact:
      label = '%s = %s' % (var_name, label)
    features = ''
#     if output_dim.invalid():
#       features += " [color=red,style=filled,fillcolor=red]"
#     node_def_lines.append('  %s [label="%s%s"] %s;' % (vidx2str(vidx), label_prefix, str_repr, ''))
    expr_name = expression_names[e] if compact and expression_names and (e in expression_names) and (expression_names[e] != f_name) else None
    nodes.add(GVNode(name, input_dim, label, output_dim, frozenset(children), features, node_type, expr_name))

  return nodes

Response: Make a network graph from a set of expression names and a set of lookup names.


Instruction:
Can you tell what is the following Python 3 function doing
def add_inputs(self, es):
        for e in es:
            ensure_freshness(e)
        for (fb,bb) in self.builder_layers[:-1]:
            fs = fb.initial_state().transduce(es)
            bs = bb.initial_state().transduce(reversed(es))
            es = [concatenate([f,b]) for f,b in zip(fs, reversed(bs))]
        (fb,bb) = self.builder_layers[-1]
        fs = fb.initial_state().add_inputs(es)
        bs = bb.initial_state().add_inputs(reversed(es))
        return [(f,b) for f,b in zip(fs, reversed(bs))]

Response:
returns the list of state pairs obtained by adding
        inputs to both forward and backward states.


Instruction:
How would you implement a function in Python 3 that
returns the list of output Expressions obtained by adding the given inputs to the current state, one by one, to both the forward and backward RNNs, and concatenating. @param es: a list of Expression see also add_inputs(xs) .transduce(xs) is different from .add_inputs(xs) in the following way: .add_inputs(xs) returns a list of RNNState pairs. RNNState objects can be queried in various ways. In particular, they allow access to the previous state, as well as to the state-vectors (h() and s() ) .transduce(xs) returns a list of Expression. These are just the output expressions. For many cases, this suffices. transduce is much more memory efficient than add_inputs.

Response:
def transduce(self, es):
        """
        returns the list of output Expressions obtained by adding the given inputs
        to the current state, one by one, to both the forward and backward RNNs,
        and concatenating.

        @param es: a list of Expression

        see also add_inputs(xs)

        .transduce(xs) is different from .add_inputs(xs) in the following way:

            .add_inputs(xs) returns a list of RNNState pairs. RNNState objects can be
             queried in various ways. In particular, they allow access to the previous
             state, as well as to the state-vectors (h() and s() )

            .transduce(xs) returns a list of Expression. These are just the output
             expressions. For many cases, this suffices.
             transduce is much more memory efficient than add_inputs.
        """
        for e in es:
            ensure_freshness(e)
        for (fb,bb) in self.builder_layers:
            fs = fb.initial_state().transduce(es)
            bs = bb.initial_state().transduce(reversed(es))
            es = [concatenate([f,b]) for f,b in zip(fs, reversed(bs))]
        return es


Instruction:
Make a summary of the following Python 3 code
def add_inputs(self, xs):
        states = []
        cur = self
        for x in xs:
            cur = cur.add_input(x)
            states.append(cur)
        return states

Response:
Returns the list of states obtained by adding the given inputs
        to the current state one by one.


Instruction:
Explain what the following Python 3 code does
def load_mnist(dataset, path):
    if dataset is "training":
        fname_img = os.path.join(path, "train-images-idx3-ubyte")
        fname_lbl = os.path.join(path, "train-labels-idx1-ubyte")
    elif dataset is "testing":
        fname_img = os.path.join(path, "t10k-images-idx3-ubyte")
        fname_lbl = os.path.join(path, "t10k-labels-idx1-ubyte")
    else:
        raise ValueError("dataset must be 'testing' or 'training'")

    # Load everything in numpy arrays
    with open(fname_lbl, "rb") as flbl:
        magic, num = struct.unpack(">II", flbl.read(8))
        labels = np.fromfile(flbl, dtype=np.int8)

    with open(fname_img, "rb") as fimg:
        magic, num, rows, cols = struct.unpack(">IIII", fimg.read(16))
        images = np.multiply(
            np.fromfile(fimg, dtype=np.uint8).reshape(len(labels), rows*cols),
            1.0 / 255.0)

    get_instance = lambda idx: (labels[idx], images[idx].reshape(1, 28, 28))

    # Create an iterator which returns each image in turn
    # for i in range(len(labels)):
    #     yield get_instance(i)

    size_reset = lambda x: x.reshape(1, 28, 28)
    return list(map(size_reset, images))

Response: Load the mnist dataset from the given path.


Instruction:
Write a Python 3 function for
making a grid of images from a 4D mini - batch tensor.

Response:
def make_grid(tensor, nrow=8, padding=2, pad_value=0):
    """Make a grid of images, via numpy.

    Args:
        tensor (Tensor or list): 4D mini-batch Tensor of shape (B x C x H x W)
            or a list of images all of the same size.
        nrow (int, optional): Number of images displayed in each row of the grid.
            The Final grid size is (B / nrow, nrow). Default is 8.
        padding (int, optional): amount of padding. Default is 2.
        pad_value (float, optional): Value for the padded pixels.

    """
    if not (isinstance(tensor, np.ndarray) or
            (isinstance(tensor, list) and all(isinstance(t, np.ndarray) for t in tensor))):
        raise TypeError('tensor or list of tensors expected, got {}'.format(type(tensor)))

    # if list of tensors, convert to a 4D mini-batch Tensor
    if isinstance(tensor, list):
        tensor = np.stack(tensor, 0)

    if tensor.ndim == 2:  # single image H x W
        tensor = tensor.reshape((1, tensor.shape[0], tensor.shape[1]))

    if tensor.ndim == 3:
        if tensor.shape[0] == 1:  # if single-channel, single image, convert to 3-channel
            tensor = np.concatenate((tensor, tensor, tensor), 0)
        tensor = tensor.reshape((1, tensor.shape[0], tensor.shape[1], tensor.shape[2]))

    if tensor.ndim == 4 and tensor.shape[1] == 1:  # single-channel images
        tensor = np.concatenate((tensor, tensor, tensor), 1)

    if tensor.shape[0] == 1:
        return np.squeeze(tensor)

    # make the mini-batch of images into a grid
    nmaps = tensor.shape[0]
    xmaps = min(nrow, nmaps)
    ymaps = int(math.ceil(float(nmaps) / xmaps))
    height, width = int(tensor.shape[2] + padding), int(tensor.shape[3] + padding)
    grid = np.ones((3, height * ymaps + padding, width * xmaps + padding)) * pad_value
    k = 0
    for y in range(ymaps):
        for x in range(xmaps):
            if k >= nmaps:
                break
            grid[:, y * height + padding:(y+1) * height,\
                 x * width + padding:(x+1) * width] = tensor[k]
            k = k + 1
    return grid


Instruction:
How would you explain what the following Python 3 function does
def save_image(tensor, filename, nrow=8, padding=2, pad_value=0):
    from PIL import Image
    grid = make_grid(tensor, nrow=nrow, padding=padding, pad_value=pad_value)
    im = Image.fromarray(pre_pillow_float_img_process(grid))
    im.save(filename)

Response: Save a given Tensor into an image file.


Instruction:
Can you create a Python 3 function that
converts a string of arguments into a list of strings.

Response:
def pythonize_arguments(arg_str):
    """
    Remove types from function arguments in cython
    """
    out_args = []
    # If there aren't any arguments return the empty string
    if arg_str is None:
        return out_str
    args = arg_str.split(',')
    for arg in args:
        components = arg.split('=')
        name_and_type=components[0].split(' ')
        # There is probably type info
        if name_and_type[-1]=='' and len(name_and_type)>1:
            name=name_and_type[-2]
        else:
            name=name_and_type[-1]
        # if there are default parameters
        if len(components)>1:
            name+='='+components[1]

        out_args.append(name)
    return ','.join(out_args)


Instruction:
Create a Python 3 function for
communicating with the child process without closing stdin.

Response:
def correspond(text):
    """Communicate with the child process without closing stdin."""
    if text:
        subproc.stdin.write(text)
    subproc.stdin.flush()
    return get_lines()


Instruction:
Can you tell what is the following Python 3 function doing
def _normalize(c):
    if isinstance(c, int):
        return bytes([c])
    elif isinstance(c, str):
        return bytes([ord(c)])
    else:
        return c

Response: Convert a byte - like value into a canonical byte - like value.


Instruction:
Given the following Python 3 function, write the documentation
def _set_perms(self, perms):
        assert isinstance(perms, str) and len(perms) <= 3 and perms.strip() in ['', 'r', 'w', 'x', 'rw', 'r x', 'rx', 'rwx', 'wx', ]
        self._perms = perms

Response: Sets the permissions of the current object.


Instruction:
Can you create a Python 3 function that
checks if the user has enough permissions for access

Response:
def access_ok(self, access):
        """ Check if there is enough permissions for access """
        for c in access:
            if c not in self.perms:
                return False
        return True


Instruction:
Create a Python 3 function to
return True if index is in range False otherwise.

Response:
def _in_range(self, index):
        """ Returns True if index is in range """
        if isinstance(index, slice):
            in_range = index.start < index.stop and \
                index.start >= self.start and \
                index.stop <= self.end
        else:
            in_range = index >= self.start and \
                index <= self.end
        return in_range


Instruction:
Can you implement a function in Python 3 that
translates the index to the internal offsets.

Response:
def _get_offset(self, index):
        """
        Translates the index to the internal offsets.

        self.start   -> 0
        self.start+1 -> 1
        ...
        self.end     -> len(self)
        """
        if not self._in_range(index):
            raise IndexError('Map index out of range')
        if isinstance(index, slice):
            index = slice(index.start - self.start, index.stop - self.start)
        else:
            index -= self.start
        return index


Instruction:
Write a Python 3 script to
return the ceil value of the given address.

Response:
def _ceil(self, address):
        """
        Returns the smallest page boundary value not less than the address.
        :rtype: int
        :param address: the address to calculate its ceil.
        :return: the ceil of C{address}.
        """
        return (((address - 1) + self.page_size) & ~self.page_mask) & self.memory_mask


Instruction:
Implement a Python 3 function for
creating a new file mapping in the memory address space.

Response:
def mmapFile(self, addr, size, perms, filename, offset=0):
        """
        Creates a new file mapping in the memory address space.

        :param addr: the starting address (took as hint). If C{addr} is C{0} the first big enough
                     chunk of memory will be selected as starting address.
        :param size: the contents of a file mapping are initialized using C{size} bytes starting
                     at offset C{offset} in the file C{filename}.
        :param perms: the access permissions to this memory.
        :param filename: the pathname to the file to map.
        :param offset: the contents of a file mapping are initialized using C{size} bytes starting
                      at offset C{offset} in the file C{filename}.
        :return: the starting address where the file was mapped.
        :rtype: int
        :raises error:
                   - 'Address shall be concrete' if C{addr} is not an integer number.
                   - 'Address too big' if C{addr} goes beyond the limit of the memory.
                   - 'Map already used' if the piece of memory starting in C{addr} and with length C{size} isn't free.
        """
        # If addr is NULL, the system determines where to allocate the region.
        assert addr is None or isinstance(addr, int), 'Address shall be concrete'
        assert size > 0

        self.cpu._publish('will_map_memory', addr, size, perms, filename, offset)

        # address is rounded down to the nearest multiple of the allocation granularity
        if addr is not None:
            assert addr < self.memory_size, 'Address too big'
            addr = self._floor(addr)

        # size value is rounded up to the next page boundary
        size = self._ceil(size)

        # If zero search for a spot
        addr = self._search(size, addr)

        # It should not be allocated
        for i in range(self._page(addr), self._page(addr + size)):
            assert i not in self._page2map, 'Map already used'

        # Create the map
        m = FileMap(addr, size, perms, filename, offset)

        # Okay, ready to alloc
        self._add(m)

        logger.debug('New file-memory map @%x size:%x', addr, size)
        self.cpu._publish('did_map_memory', addr, size, perms, filename, offset, addr)
        return addr


Instruction:
Explain what the following Python 3 code does
def mmap(self, addr, size, perms, data_init=None, name=None):
        # If addr is NULL, the system determines where to allocate the region.
        assert addr is None or isinstance(addr, int), 'Address shall be concrete'

        self.cpu._publish('will_map_memory', addr, size, perms, None, None)

        # address is rounded down to the nearest multiple of the allocation granularity
        if addr is not None:
            assert addr < self.memory_size, 'Address too big'
            addr = self._floor(addr)

        # size value is rounded up to the next page boundary
        size = self._ceil(size)

        # If zero search for a spot
        addr = self._search(size, addr)

        # It should not be allocated
        for i in range(self._page(addr), self._page(addr + size)):
            assert i not in self._page2map, 'Map already used'

        # Create the anonymous map
        m = AnonMap(start=addr, size=size, perms=perms, data_init=data_init, name=name)

        # Okay, ready to alloc
        self._add(m)

        logger.debug('New memory map @%x size:%x', addr, size)

        self.cpu._publish('did_map_memory', addr, size, perms, None, None, addr)
        return addr

Response: Creates a new mapping in the memory address space.


Instruction:
Implement a Python 3 function for
returning the object containing the address.

Response:
def map_containing(self, address):
        """
        Returns the L{MMap} object containing the address.

        :param address: the address to obtain its mapping.
        :rtype: L{MMap}

        @todo: symbolic address
        """
        page_offset = self._page(address)
        if page_offset not in self._page2map:
            raise MemoryException("Page not mapped", address)
        return self._page2map[page_offset]


Instruction:
Write a Python 3 script for
returning a sorted list of all the mappings for this memory.

Response:
def mappings(self):
        """
        Returns a sorted list of all the mappings for this memory.

        :return: a list of mappings.
        :rtype: list
        """
        result = []
        for m in self.maps:
            if isinstance(m, AnonMap):
                result.append((m.start, m.end, m.perms, 0, ''))
            elif isinstance(m, FileMap):
                result.append((m.start, m.end, m.perms, m._offset, m._filename))
            else:
                result.append((m.start, m.end, m.perms, 0, m.name))

        return sorted(result)


Instruction:
Can you tell what is the following Python 3 function doing
def _maps_in_range(self, start, end):

        # Search for the first matching map
        addr = start
        while addr < end:
            if addr not in self:
                addr += self.page_size
            else:
                m = self._page2map[self._page(addr)]
                yield m
                addr = m.end

Response: Generate the list of maps that overlaps with the range [ start end )


Instruction:
Here you have a function in Python 3, explain what it does
def munmap(self, start, size):
        start = self._floor(start)
        end = self._ceil(start + size)

        self.cpu._publish('will_unmap_memory', start, size)

        for m in self._maps_in_range(start, end):
            self._del(m)
            head, tail = m.split(start)
            middle, tail = tail.split(end)
            assert middle is not None
            if head:
                self._add(head)
            if tail:
                self._add(tail)

        self.cpu._publish('did_unmap_memory', start, size)
        logger.debug(f'Unmap memory @{start:x} size:{size:x}')

Response: Removes mappings from the specified address range and causes further memory related references to addresses within the range to generate invalid memory.


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

        lst = self._recording_stack.pop()
        # Append the current list to a previously-started trace.
        if self._recording_stack:
            self._recording_stack[-1].extend(lst)
        return lst

Response:
Stop recording trace and return a list[tuple ] of all the writes
            that occurred where value is of type list[str ]


Instruction:
Write a Python 3 function for
deleting the mappings for the specified address range and causes further memory references to addresses within the range to generate invalid memory references.

Response:
def munmap(self, start, size):
        """
        Deletes the mappings for the specified address range and causes further
        references to addresses within the range to generate invalid memory
        references.

        :param start: the starting address to delete.
        :param size: the length of the unmapping.
        """
        for addr in range(start, start + size):
            if len(self._symbols) == 0:
                break
            if addr in self._symbols:
                del self._symbols[addr]
        super().munmap(start, size)


Instruction:
Write a Python 3 function for
reading a stream of bytes from a potentially symbolic memory.

Response:
def read(self, address, size, force=False):
        """
        Read a stream of potentially symbolic bytes from a potentially symbolic
        address

        :param address: Where to read from
        :param size: How many bytes
        :param force: Whether to ignore permissions
        :rtype: list
        """
        size = self._get_size(size)
        assert not issymbolic(size)

        if issymbolic(address):
            assert solver.check(self.constraints)
            logger.debug(f'Reading {size} bytes from symbolic address {address}')
            try:
                solutions = self._try_get_solutions(address, size, 'r', force=force)
                assert len(solutions) > 0
            except TooManySolutions as e:
                m, M = solver.minmax(self.constraints, address)
                logger.debug(f'Got TooManySolutions on a symbolic read. Range [{m:x}, {M:x}]. Not crashing!')

                # The force param shouldn't affect this, as this is checking for unmapped reads, not bad perms
                crashing_condition = True
                for start, end, perms, offset, name in self.mappings():
                    if start <= M + size and end >= m:
                        if 'r' in perms:
                            crashing_condition = Operators.AND(Operators.OR((address + size).ult(start), address.uge(end)), crashing_condition)

                if solver.can_be_true(self.constraints, crashing_condition):
                    raise InvalidSymbolicMemoryAccess(address, 'r', size, crashing_condition)

                # INCOMPLETE Result! We could also fork once for every map
                logger.info('INCOMPLETE Result! Using the sampled solutions we have as result')
                condition = False
                for base in e.solutions:
                    condition = Operators.OR(address == base, condition)
                from .state import ForkState
                raise ForkState("Forking state on incomplete result", condition)

            # So here we have all potential solutions to address

            condition = False
            for base in solutions:
                condition = Operators.OR(address == base, condition)

            result = []
            # consider size ==1 to read following code
            for offset in range(size):
                # Given ALL solutions for the symbolic address
                for base in solutions:
                    addr_value = base + offset
                    byte = Operators.ORD(self.map_containing(addr_value)[addr_value])
                    if addr_value in self._symbols:
                        for condition, value in self._symbols[addr_value]:
                            byte = Operators.ITEBV(8, condition, Operators.ORD(value), byte)
                    if len(result) > offset:
                        result[offset] = Operators.ITEBV(8, address == base, byte, result[offset])
                    else:
                        result.append(byte)
                    assert len(result) == offset + 1
            return list(map(Operators.CHR, result))
        else:
            result = list(map(Operators.ORD, super().read(address, size, force)))
            for offset in range(size):
                if address + offset in self._symbols:
                    for condition, value in self._symbols[address + offset]:
                        if condition is True:
                            result[offset] = Operators.ORD(value)
                        else:
                            result[offset] = Operators.ITEBV(8, condition, Operators.ORD(value), result[offset])
            return list(map(Operators.CHR, result))


Instruction:
Write a Python 3 script for
writing a value at the specified address.

Response:
def write(self, address, value, force=False):
        """
        Write a value at address.
        :param address: The address at which to write
        :type address: int or long or Expression
        :param value: Bytes to write
        :type value: str or list
        :param force: Whether to ignore permissions
        """
        size = len(value)
        if issymbolic(address):

            solutions = self._try_get_solutions(address, size, 'w', force=force)

            for offset in range(size):
                for base in solutions:
                    condition = base == address
                    self._symbols.setdefault(base + offset, []).append((condition, value[offset]))
        else:

            for offset in range(size):
                if issymbolic(value[offset]):
                    if not self.access_ok(address + offset, 'w', force):
                        raise InvalidMemoryAccess(address + offset, 'w')
                    self._symbols[address + offset] = [(True, value[offset])]
                else:
                    # overwrite all previous items
                    if address + offset in self._symbols:
                        del self._symbols[address + offset]
                    super().write(address + offset, [value[offset]], force)


Instruction:
Can you tell what is the following Python 3 function doing
def _try_get_solutions(self, address, size, access, max_solutions=0x1000, force=False):
        assert issymbolic(address)

        solutions = solver.get_all_values(self.constraints, address, maxcnt=max_solutions)

        crashing_condition = False
        for base in solutions:
            if not self.access_ok(slice(base, base + size), access, force):
                crashing_condition = Operators.OR(address == base, crashing_condition)

        if solver.can_be_true(self.constraints, crashing_condition):
            raise InvalidSymbolicMemoryAccess(address, access, size, crashing_condition)

        return solutions

Response: Try to find a set of possible memory entries for a given symbolic address.


Instruction:
How would you explain what the following Python 3 function does
def mmapFile(self, addr, size, perms, filename, offset=0):
        # If addr is NULL, the system determines where to allocate the region.
        assert addr is None or isinstance(addr, int), 'Address shall be concrete'
        assert addr < self.memory_size, 'Address too big'
        assert size > 0

        self.cpu._publish('will_map_memory', addr, size, perms, filename, offset)

        map = AnonMap(addr, size, perms)
        self._add(map)

        # address is rounded down to the nearest multiple of the allocation granularity
        if addr is not None:
            addr = self._floor(addr)

        # size value is rounded up to the next page boundary
        size = self._ceil(size)

        with open(filename, 'rb') as f:
            fdata = f.read()  # fdata is a bytes now

        # this worked
        fdata = fdata[offset:]
        fdata = fdata.ljust(size, b'\0')

        for i in range(size):
            Memory.write(self, addr + i, chr(fdata[i]), force=True)

        logger.debug('New file-memory map @%x size:%x', addr, size)
        self.cpu._publish('did_map_memory', addr, size, perms, filename, offset, addr)

        return addr

Response: Creates a new file mapping in the memory address space.


Instruction:
Create a Python 3 function for
importing concrete memory from from_addr to to_addr.

Response:
def _import_concrete_memory(self, from_addr, to_addr):
        """
        for each address in this range need to read from concrete and write to symbolic
        it's possible that there will be invalid/unmapped addresses in this range. need to skip to next map if so
        also need to mark all of these addresses as now in the symbolic store

        :param int from_addr:
        :param int to_addr:
        :return:
        """
        logger.debug("Importing concrete memory: {:x} - {:x} ({} bytes)".format(from_addr, to_addr, to_addr - from_addr))

        for m in self.maps:
            span = interval_intersection(m.start, m.end, from_addr, to_addr)
            if span is None:
                continue

            start, stop = span

            for addr in range(start, stop):
                if addr in self.backed_by_symbolic_store:
                    continue

                self.backing_array[addr] = Memory.read(self, addr, 1)[0]
                self.backed_by_symbolic_store.add(addr)


Instruction:
Can you write a function in Python 3 where it
scans for concrete bytes in all mapped memory. Successively yield addresses of all matches.

Response:
def scan_mem(self, data_to_find):
        """
        Scan for concrete bytes in all mapped memory. Successively yield addresses of all matches.

        :param bytes data_to_find: String to locate
        :return:
        """

        # TODO: for the moment we just treat symbolic bytes as bytes that don't match.
        # for our simple test cases right now, the bytes we're interested in scanning
        # for will all just be there concretely
        # TODO: Can probably do something smarter here like Boyer-Moore, but unnecessary
        # if we're looking for short strings.

        # Querying mem with an index returns [bytes]
        if isinstance(data_to_find, bytes):
            data_to_find = [bytes([c]) for c in data_to_find]

        for mapping in sorted(self.maps):
            for ptr in mapping:
                if ptr + len(data_to_find) >= mapping.end:
                    break

                candidate = mapping[ptr:ptr + len(data_to_find)]

                # TODO: treat symbolic bytes as bytes that don't match. for our simple tests right now, the
                # bytes will be there concretely
                if issymbolic(candidate[0]):
                    break

                if candidate == data_to_find:
                    yield ptr


Instruction:
How would you explain what the following Python 3 function does
def _reg_name(self, reg_id):
        if reg_id >= X86_REG_ENDING:
            logger.warning("Trying to get register name for a non-register")
            return None
        cs_reg_name = self.cpu.instruction.reg_name(reg_id)
        if cs_reg_name is None or cs_reg_name.lower() == '(invalid)':
            return None
        return self.cpu._regfile._alias(cs_reg_name.upper())

Response: Translate a register ID into a register name based on the alias in the register file.


Instruction:
Make a summary of the following Python 3 code
def get_argument_values(self, model, prefix_args):
        spec = inspect.getfullargspec(model)

        if spec.varargs:
            logger.warning("ABI: A vararg model must be a unary function.")

        nargs = len(spec.args) - len(prefix_args)

        # If the model is a method, we need to account for `self`
        if inspect.ismethod(model):
            nargs -= 1

        def resolve_argument(arg):
            if isinstance(arg, str):
                return self._cpu.read_register(arg)
            else:
                return self._cpu.read_int(arg)

        # Create a stream of resolved arguments from argument descriptors
        descriptors = self.get_arguments()
        argument_iter = map(resolve_argument, descriptors)

        from ..models import isvariadic  # prevent circular imports

        if isvariadic(model):
            arguments = prefix_args + (argument_iter,)
        else:
            arguments = prefix_args + tuple(islice(argument_iter, nargs))

        return arguments

Response: Extract the arguments for the given model and return as a tuple that is ready to be passed to the model.


Instruction:
Can you write a function in Python 3 where it
invokes a native function of the class.

Response:
def invoke(self, model, prefix_args=None):
        """
        Invoke a callable `model` as if it was a native function. If
        :func:`~manticore.models.isvariadic` returns true for `model`, `model` receives a single
        argument that is a generator for function arguments. Pass a tuple of
        arguments for `prefix_args` you'd like to precede the actual
        arguments.

        :param callable model: Python model of the function
        :param tuple prefix_args: Parameters to pass to model before actual ones
        :return: The result of calling `model`
        """
        prefix_args = prefix_args or ()

        arguments = self.get_argument_values(model, prefix_args)

        try:
            result = model(*arguments)
        except ConcretizeArgument as e:
            assert e.argnum >= len(prefix_args), "Can't concretize a constant arg"
            idx = e.argnum - len(prefix_args)

            # Arguments were lazily computed in case of variadic, so recompute here
            descriptors = self.get_arguments()
            src = next(islice(descriptors, idx, idx + 1))

            msg = 'Concretizing due to model invocation'
            if isinstance(src, str):
                raise ConcretizeRegister(self._cpu, src, msg)
            else:
                raise ConcretizeMemory(self._cpu.memory, src, self._cpu.address_bit_size, msg)
        else:
            if result is not None:
                self.write_result(result)

            self.ret()

        return result


Instruction:
Can you generate a brief explanation for the following Python 3 code
def write_register(self, register, value):
        self._publish('will_write_register', register, value)
        value = self._regfile.write(register, value)
        self._publish('did_write_register', register, value)
        return value

Response: Dynamic interface for writing cpu registers


Instruction:
Can you generate the documentation for the following Python 3 function
def emulate_until(self, target: int):
        self._concrete = True
        self._break_unicorn_at = target
        if self.emu:
            self.emu._stop_at = target

Response: Sets up a concrete unicorn emulator and use it to execute instructions until target is reached.


Instruction:
Given the following Python 3 function, write the documentation
def write_int(self, where, expression, size=None, force=False):
        if size is None:
            size = self.address_bit_size
        assert size in SANE_SIZES
        self._publish('will_write_memory', where, expression, size)

        data = [Operators.CHR(Operators.EXTRACT(expression, offset, 8)) for offset in range(0, size, 8)]
        self._memory.write(where, data, force)

        self._publish('did_write_memory', where, expression, size)

Response: Writes an integer to the memory.


Instruction:
Create a Python 3 function to
select bytes from memory. Attempts to do so faster than via read_bytes. :param where: address to read from :param size: number of bytes to read :return: the bytes in memory

Response:
def _raw_read(self, where: int, size=1) -> bytes:
        """
        Selects bytes from memory. Attempts to do so faster than via read_bytes.

        :param where: address to read from
        :param size: number of bytes to read
        :return: the bytes in memory
        """
        map = self.memory.map_containing(where)
        start = map._get_offset(where)
        mapType = type(map)
        if mapType is FileMap:
            end = map._get_offset(where + size)

            if end > map._mapped_size:
                logger.warning(f"Missing {end - map._mapped_size} bytes at the end of {map._filename}")

            raw_data = map._data[map._get_offset(where): min(end, map._mapped_size)]
            if len(raw_data) < end:
                raw_data += b'\x00' * (end - len(raw_data))

            data = b''
            for offset in sorted(map._overlay.keys()):
                data += raw_data[len(data):offset]
                data += map._overlay[offset]
            data += raw_data[len(data):]

        elif mapType is AnonMap:
            data = bytes(map._data[start:start + size])
        else:
            data = b''.join(self.memory[where:where + size])
        assert len(data) == size, 'Raw read resulted in wrong data read which should never happen'
        return data


Instruction:
Can you generate a brief explanation for the following Python 3 code
def read_int(self, where, size=None, force=False):
        if size is None:
            size = self.address_bit_size
        assert size in SANE_SIZES
        self._publish('will_read_memory', where, size)

        data = self._memory.read(where, size // 8, force)
        assert (8 * len(data)) == size
        value = Operators.CONCAT(size, *map(Operators.ORD, reversed(data)))

        self._publish('did_read_memory', where, value, size)
        return value

Response: Reads an integer from memory returning it as a BitVec.


Instruction:
Make a summary of the following Python 3 code
def write_bytes(self, where, data, force=False):

        mp = self.memory.map_containing(where)
        # TODO (ehennenfent) - fast write can have some yet-unstudied unintended side effects.
        # At the very least, using it in non-concrete mode will break the symbolic strcmp/strlen models. The 1024 byte
        # minimum is intended to minimize the potential effects of this by ensuring that if there _are_ any other
        # issues, they'll only crop up when we're doing very large writes, which are fairly uncommon.
        can_write_raw = type(mp) is AnonMap and \
            isinstance(data, (str, bytes)) and \
            (mp.end - mp.start + 1) >= len(data) >= 1024 and \
            not issymbolic(data) and \
            self._concrete

        if can_write_raw:
            logger.debug("Using fast write")
            offset = mp._get_offset(where)
            if isinstance(data, str):
                data = bytes(data.encode('utf-8'))
            mp._data[offset:offset + len(data)] = data
            self._publish('did_write_memory', where, data, 8 * len(data))
        else:
            for i in range(len(data)):
                self.write_int(where + i, Operators.ORD(data[i]), 8, force)

Response: Write a byte array to memory.


Instruction:
Explain what the following Python 3 code does
def read_bytes(self, where, size, force=False):
        result = []
        for i in range(size):
            result.append(Operators.CHR(self.read_int(where + i, 8, force)))
        return result

Response: Reads a number of bytes from memory.


Instruction:
Can you write a function in Python 3 where it
writes a string to memory.

Response:
def write_string(self, where, string, max_length=None, force=False):
        """
        Writes a string to memory, appending a NULL-terminator at the end.
        :param int where: Address to write the string to
        :param str string: The string to write to memory
        :param int max_length:
            The size in bytes to cap the string at, or None [default] for no
            limit. This includes the NULL terminator.
        :param force: whether to ignore memory permissions
        """

        if max_length is not None:
            string = string[:max_length - 1]

        self.write_bytes(where, string + '\x00', force)


Instruction:
Can you implement a function in Python 3 that
reads a NUL - terminated concrete buffer from memory. Stops reading at first symbolic byte.

Response:
def read_string(self, where, max_length=None, force=False):
        """
        Read a NUL-terminated concrete buffer from memory. Stops reading at first symbolic byte.

        :param int where: Address to read string from
        :param int max_length:
            The size in bytes to cap the string at, or None [default] for no
            limit.
        :param force: whether to ignore memory permissions
        :return: string read
        :rtype: str
        """
        s = io.BytesIO()
        while True:
            c = self.read_int(where, 8, force)

            if issymbolic(c) or c == 0:
                break

            if max_length is not None:
                if max_length == 0:
                    break
                max_length = max_length - 1
            s.write(Operators.CHR(c))
            where += 1
        return s.getvalue().decode()


Instruction:
Can you tell what is the following Python 3 function doing
def push_bytes(self, data, force=False):
        self.STACK -= len(data)
        self.write_bytes(self.STACK, data, force)
        return self.STACK

Response: Writes data to the stack and decrement the stack pointer accordingly.


Instruction:
Implement a function in Python 3 to
read nbytes bytes from the stack and return them.

Response:
def pop_bytes(self, nbytes, force=False):
        """
        Read `nbytes` from the stack, increment the stack pointer, and return
        data.

        :param int nbytes: How many bytes to read
        :param force: whether to ignore memory permissions
        :return: Data read from the stack
        """
        data = self.read_bytes(self.STACK, nbytes, force=force)
        self.STACK += nbytes
        return data


Instruction:
Make a summary of the following Python 3 code
def push_int(self, value, force=False):
        self.STACK -= self.address_bit_size // 8
        self.write_int(self.STACK, value, force=force)
        return self.STACK

Response: Decrement the stack pointer and write the given value to the stack.


Instruction:
Write a Python 3 script for
reading an integer from the stack and increment the stack pointer.

Response:
def pop_int(self, force=False):
        """
        Read a value from the stack and increment the stack pointer.

        :param force: whether to ignore memory permissions
        :return: Value read
        """
        value = self.read_int(self.STACK, force=force)
        self.STACK += self.address_bit_size // 8
        return value