{"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn an instruction that evaluates to the absolute value of top of the stack.", "response": "def _abs16(ins):\n ''' Absolute value of top of the stack (16 bits in HL)\n '''\n output = _16bit_oper(ins.quad[2])\n output.append('call __ABS16')\n output.append('push hl')\n REQUIRES.add('abs16.asm')\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _shru16(ins):\n ''' Logical right shift 16bit unsigned integer.\n The result is pushed onto the stack.\n\n Optimizations:\n * If 2nd op is 0 then\n do nothing\n\n * If 2nd op is 1\n Shift Right Arithmetic\n '''\n op1, op2 = tuple(ins.quad[2:])\n if is_int(op2):\n op = int16(op2)\n if op == 0:\n return []\n\n output = _16bit_oper(op1)\n if op == 1:\n output.append('srl h')\n output.append('rr l')\n output.append('push hl')\n return output\n\n output.append('ld b, %i' % op)\n else:\n output = _8bit_oper(op2)\n output.append('ld b, a')\n output.extend(_16bit_oper(op1))\n\n label = tmp_label()\n output.append('%s:' % label)\n output.append('srl h')\n output.append('rr l')\n output.append('djnz %s' % label)\n output.append('push hl')\n return output", "response": "Logical right shift 16bit unsigned integer."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nread a txt file and returns the content as a unicode string.", "response": "def read_txt_file(fname):\n \"\"\"Reads a txt file, regardless of its encoding\n \"\"\"\n encodings = ['utf-8-sig', 'cp1252']\n with open(fname, 'rb') as f:\n content = bytes(f.read())\n\n for i in encodings:\n try:\n result = content.decode(i)\n if six.PY2:\n result = result.encode('utf-8')\n return result\n except UnicodeDecodeError:\n pass\n\n global_.FILENAME = fname\n errmsg.syntax_error(1, 'Invalid file encoding. Use one of: %s' % ', '.join(encodings))\n return ''"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef open_file(fname, mode='rb', encoding='utf-8'):\n if six.PY2 or 't' not in mode:\n kwargs = {}\n else:\n kwargs = {'encoding': encoding}\n\n return open(fname, mode, **kwargs)", "response": "A wrapper for open that supports both Python 2 and Python 3."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef parse_int(str_num):\n str_num = (str_num or \"\").strip().upper()\n if not str_num:\n return None\n\n base = 10\n if str_num.startswith('0X'):\n base = 16\n str_num = str_num[2:]\n if str_num.endswith('H'):\n base = 16\n str_num = str_num[:-1]\n if str_num.startswith('$'):\n base = 16\n str_num = str_num[1:]\n\n try:\n return int(str_num, base)\n except ValueError:\n return None", "response": "Given an integer number return its value or None if it could not be parsed."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef out(self, l):\n if not isinstance(l, list):\n l = [l]\n\n self.output.extend([int(i) & 0xFF for i in l])", "response": "Adds a list of bytes to the output string"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef standard_block(self, _bytes):\n self.out(self.BLOCK_STANDARD) # Standard block ID\n self.out(self.LH(1000)) # 1000 ms standard pause\n self.out(self.LH(len(_bytes) + 1)) # + 1 for CHECKSUM byte\n\n checksum = 0\n for i in _bytes:\n checksum ^= (int(i) & 0xFF)\n self.out(i)\n\n self.out(checksum)", "response": "Adds a standard block of bytes to the internal buffer."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsaves the TZX file to fname", "response": "def dump(self, fname):\n \"\"\" Saves TZX file to fname\n \"\"\"\n with open(fname, 'wb') as f:\n f.write(self.output)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nsaving a generic standard header for the current object.", "response": "def save_header(self, _type, title, length, param1, param2):\n \"\"\" Saves a generic standard header:\n type: 00 -- Program\n 01 -- Number Array\n 02 -- Char Array\n 03 -- Code\n\n title: Name title.\n Will be truncated to 10 chars and padded\n with spaces if necessary.\n\n length: Data length (in bytes) of the next block.\n\n param1: For CODE -> Start address.\n For PROGRAM -> Autostart line (>=32768 for no autostart)\n For DATA (02 & 03) high byte of param1 have the variable name.\n\n param2: For CODE -> 32768\n For PROGRAM -> Start of the Variable area relative to program Start (Length of basic in bytes)\n For DATA (02 & 03) NOT USED\n\n Info taken from: http://www.worldofspectrum.org/faq/reference/48kreference.htm#TapeDataStructure\n \"\"\"\n title = (title + 10 * ' ')[:10] # Padd it with spaces\n title_bytes = [ord(i) for i in title] # Convert it to bytes\n\n _bytes = [self.BLOCK_TYPE_HEADER, _type] + title_bytes + self.LH(length) + self.LH(param1) + self.LH(param2)\n self.standard_block(_bytes)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef standard_bytes_header(self, title, addr, length):\n self.save_header(self.HEADER_TYPE_CODE, title, length, param1=addr, param2=32768)", "response": "Generates a standard header block of CODE type"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef standard_program_header(self, title, length, line=32768):\n self.save_header(self.HEADER_TYPE_BASIC, title, length, param1=line, param2=length)", "response": "Generates a standard header block of PROGRAM type\n "} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsaves the given bytes as code.", "response": "def save_code(self, title, addr, _bytes):\n \"\"\" Saves the given bytes as code. If bytes are strings,\n its chars will be converted to bytes\n \"\"\"\n self.standard_bytes_header(title, addr, len(_bytes))\n _bytes = [self.BLOCK_TYPE_DATA] + [(int(x) & 0xFF) for x in _bytes] # & 0xFF truncates to bytes\n self.standard_block(_bytes)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef save_program(self, title, bytes, line=32768):\n self.standard_program_header(title, len(bytes), line)\n bytes = [self.BLOCK_TYPE_DATA] + [(int(x) & 0xFF) for x in bytes] # & 0xFF truncates to bytes\n self.standard_block(bytes)", "response": "Saves the given bytes as a BASIC program."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a node for a string slice.", "response": "def make_node(cls, lineno, s, lower, upper):\n \"\"\" Creates a node for a string slice. S is the string expression Tree.\n Lower and upper are the bounds, if lower & upper are constants, and\n s is also constant, then a string constant is returned.\n\n If lower > upper, an empty string is returned.\n \"\"\"\n if lower is None or upper is None or s is None:\n return None\n\n if not check_type(lineno, Type.string, s):\n return None\n\n lo = up = None\n base = NUMBER(api.config.OPTIONS.string_base.value, lineno=lineno)\n lower = TYPECAST.make_node(gl.SYMBOL_TABLE.basic_types[gl.STR_INDEX_TYPE],\n BINARY.make_node('MINUS', lower, base, lineno=lineno,\n func=lambda x, y: x - y), lineno)\n upper = TYPECAST.make_node(gl.SYMBOL_TABLE.basic_types[gl.STR_INDEX_TYPE],\n BINARY.make_node('MINUS', upper, base, lineno=lineno,\n func=lambda x, y: x - y), lineno)\n\n if lower is None or upper is None:\n return None\n\n if is_number(lower):\n lo = lower.value\n if lo < gl.MIN_STRSLICE_IDX:\n lower.value = lo = gl.MIN_STRSLICE_IDX\n\n if is_number(upper):\n up = upper.value\n if up > gl.MAX_STRSLICE_IDX:\n upper.value = up = gl.MAX_STRSLICE_IDX\n\n if is_number(lower, upper):\n if lo > up:\n return STRING('', lineno)\n\n if s.token == 'STRING': # A constant string? Recalculate it now\n up += 1\n st = s.value.ljust(up) # Procrustean filled (right)\n return STRING(st[lo:up], lineno)\n\n # a$(0 TO INF.) = a$\n if lo == gl.MIN_STRSLICE_IDX and up == gl.MAX_STRSLICE_IDX:\n return s\n\n return cls(s, lower, upper, lineno)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the instruction sequence for converting from the given type to byte.", "response": "def to_byte(stype):\n \"\"\" Returns the instruction sequence for converting from\n the given type to byte.\n \"\"\"\n output = []\n\n if stype in ('i8', 'u8'):\n return []\n\n if is_int_type(stype):\n output.append('ld a, l')\n elif stype == 'f16':\n output.append('ld a, e')\n elif stype == 'f': # Converts C ED LH to byte\n output.append('call __FTOU32REG')\n output.append('ld a, l')\n REQUIRES.add('ftou32reg.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef to_word(stype):\n output = [] # List of instructions\n\n if stype == 'u8': # Byte to word\n output.append('ld l, a')\n output.append('ld h, 0')\n\n elif stype == 'i8': # Signed byte to word\n output.append('ld l, a')\n output.append('add a, a')\n output.append('sbc a, a')\n output.append('ld h, a')\n\n elif stype == 'f16': # Must MOVE HL into DE\n output.append('ex de, hl')\n\n elif stype == 'f':\n output.append('call __FTOU32REG')\n REQUIRES.add('ftou32reg.asm')\n\n return output", "response": "Returns the instruction sequence for converting the given type stored in DE HL to word ( unsigned ) HL."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef to_fixed(stype):\n output = [] # List of instructions\n\n if is_int_type(stype):\n output = to_word(stype)\n output.append('ex de, hl')\n output.append('ld hl, 0') # 'Truncate' the fixed point\n elif stype == 'f':\n output.append('call __FTOF16REG')\n REQUIRES.add('ftof16reg.asm')\n\n return output", "response": "Returns the instruction sequence for converting the given\n type stored in DE HL to fixed DE HL."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the instruction sequence for converting the given type stored in DE HL to fixed DE HL.", "response": "def to_float(stype):\n \"\"\" Returns the instruction sequence for converting the given\n type stored in DE,HL to fixed DE,HL.\n \"\"\"\n output = [] # List of instructions\n\n if stype == 'f':\n return [] # Nothing to do\n\n if stype == 'f16':\n output.append('call __F16TOFREG')\n REQUIRES.add('f16tofreg.asm')\n return output\n\n # If we reach this point, it's an integer type\n if stype == 'u8':\n output.append('call __U8TOFREG')\n elif stype == 'i8':\n output.append('call __I8TOFREG')\n else:\n output = to_long(stype)\n if stype in ('i16', 'i32'):\n output.append('call __I32TOFREG')\n else:\n output.append('call __U32TOFREG')\n\n REQUIRES.add('u32tofreg.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\noutputting the ending sequence", "response": "def _end(ins):\n \"\"\" Outputs the ending sequence\n \"\"\"\n global FLAG_end_emitted\n output = _16bit_oper(ins.quad[1])\n output.append('ld b, h')\n output.append('ld c, l')\n\n if FLAG_end_emitted:\n return output + ['jp %s' % END_LABEL]\n\n FLAG_end_emitted = True\n\n output.append('%s:' % END_LABEL)\n if OPTIONS.headerless.value:\n return output + ['ret']\n\n output.append('di')\n output.append('ld hl, (%s)' % CALL_BACK)\n output.append('ld sp, hl')\n output.append('exx')\n output.append('pop hl')\n output.append('exx')\n output.append('pop iy')\n output.append('pop ix')\n output.append('ei')\n output.append('ret')\n output.append('%s:' % CALL_BACK)\n output.append('DEFW 0')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _data(ins):\n output = []\n t = ins.quad[1]\n q = eval(ins.quad[2])\n\n if t in ('i8', 'u8'):\n size = 'B'\n elif t in ('i16', 'u16'):\n size = 'W'\n elif t in ('i32', 'u32'):\n size = 'W'\n z = list()\n for expr in ins.quad[2]:\n z.extend(['(%s) & 0xFFFF' % expr, '(%s) >> 16' % expr])\n q = z\n elif t == 'str':\n size = \"B\"\n q = ['\"%s\"' % x.replace('\"', '\"\"') for x in q]\n elif t == 'f':\n dat_ = [api.fp.immediate_float(float(x)) for x in q]\n for x in dat_:\n output.extend(['DEFB %s' % x[0], 'DEFW %s, %s' % (x[1], x[2])])\n return output\n else:\n raise InvalidIC(ins.quad, 'Unimplemented data size %s for %s' % (t, q))\n\n for x in q:\n output.append('DEF%s %s' % (size, x))\n\n return output", "response": "Defines a data item for the current object."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _var(ins):\n output = []\n output.append('%s:' % ins.quad[1])\n output.append('DEFB %s' % ((int(ins.quad[2]) - 1) * '00, ' + '00'))\n\n return output", "response": "Defines a memory variable.\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndefine a memory space with a default CONSTANT expression", "response": "def _varx(ins):\n \"\"\" Defines a memory space with a default CONSTANT expression\n 1st parameter is the var name\n 2nd parameter is the type-size (u8 or i8 for byte, u16 or i16 for word, etc)\n 3rd parameter is the list of expressions. All of them will be converted to the\n type required.\n \"\"\"\n output = []\n output.append('%s:' % ins.quad[1])\n q = eval(ins.quad[3])\n\n if ins.quad[2] in ('i8', 'u8'):\n size = 'B'\n elif ins.quad[2] in ('i16', 'u16'):\n size = 'W'\n elif ins.quad[2] in ('i32', 'u32'):\n size = 'W'\n z = list()\n for expr in q:\n z.extend(['(%s) & 0xFFFF' % expr, '(%s) >> 16' % expr])\n q = z\n else:\n raise InvalidIC(ins.quad, 'Unimplemented vard size: %s' % ins.quad[2])\n\n for x in q:\n output.append('DEF%s %s' % (size, x))\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _vard(ins):\n output = []\n output.append('%s:' % ins.quad[1])\n\n q = eval(ins.quad[2])\n\n for x in q:\n if x[0] == '#': # literal?\n size_t = 'W' if x[1] == '#' else 'B'\n output.append('DEF{0} {1}'.format(size_t, x.lstrip('#')))\n continue\n\n # must be an hex number\n x = x.upper()\n assert RE_HEXA.match(x), 'expected an hex number, got \"%s\"' % x\n size_t = 'B' if len(x) <= 2 else 'W'\n if x[0] > '9': # Not a number?\n x = '0' + x\n output.append('DEF{0} {1}h'.format(size_t, x))\n\n return output", "response": "Defines a memory space with a default set of bytes or words in hexadecimal."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _lvarx(ins):\n output = []\n\n l = eval(ins.quad[3]) # List of bytes to push\n label = tmp_label()\n offset = int(ins.quad[1])\n tmp = list(ins.quad)\n tmp[1] = label\n ins.quad = tmp\n AT_END.extend(_varx(ins))\n\n output.append('push ix')\n output.append('pop hl')\n output.append('ld bc, %i' % -offset)\n output.append('add hl, bc')\n output.append('ex de, hl')\n output.append('ld hl, %s' % label)\n output.append('ld bc, %i' % (len(l) * YY_TYPES[ins.quad[2]]))\n output.append('ldir')\n\n return output", "response": "Defines a local variable."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndefine a local variable. 1st param is offset of the local variable. 2nd param is a list of bytes in hexadecimal.", "response": "def _lvard(ins):\n \"\"\" Defines a local variable. 1st param is offset of the local variable.\n 2nd param is a list of bytes in hexadecimal.\n \"\"\"\n output = []\n\n l = eval(ins.quad[2]) # List of bytes to push\n label = tmp_label()\n offset = int(ins.quad[1])\n tmp = list(ins.quad)\n tmp[1] = label\n ins.quad = tmp\n AT_END.extend(_vard(ins))\n\n output.append('push ix')\n output.append('pop hl')\n output.append('ld bc, %i' % -offset)\n output.append('add hl, bc')\n output.append('ex de, hl')\n output.append('ld hl, %s' % label)\n output.append('ld bc, %i' % len(l))\n output.append('ldir')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ntranslate OUT to asm.", "response": "def _out(ins):\n \"\"\" Translates OUT to asm.\n \"\"\"\n output = _8bit_oper(ins.quad[2])\n output.extend(_16bit_oper(ins.quad[1]))\n output.append('ld b, h')\n output.append('ld c, l')\n output.append('out (c), a')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ntranslates IN to asm.", "response": "def _in(ins):\n \"\"\" Translates IN to asm.\n \"\"\"\n output = _16bit_oper(ins.quad[1])\n output.append('ld b, h')\n output.append('ld c, l')\n output.append('in a, (c)')\n output.append('push af')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nloading a 32 bit value from a memory address.", "response": "def _load32(ins):\n \"\"\" Load a 32 bit value from a memory address\n If 2nd arg. start with '*', it is always treated as\n an indirect value.\n \"\"\"\n output = _32bit_oper(ins.quad[2])\n output.append('push de')\n output.append('push hl')\n return output"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nloading a 32 bit fixed point value from a memory address.", "response": "def _loadf16(ins):\n \"\"\" Load a 32 bit (16.16) fixed point value from a memory address\n If 2nd arg. start with '*', it is always treated as\n an indirect value.\n \"\"\"\n output = _f16_oper(ins.quad[2])\n output.append('push de')\n output.append('push hl')\n return output"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nload a floating point value from a memory address.", "response": "def _loadf(ins):\n \"\"\" Loads a floating point value from a memory address.\n If 2nd arg. start with '*', it is always treated as\n an indirect value.\n \"\"\"\n output = _float_oper(ins.quad[2])\n output.extend(_fpush())\n return output"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _loadstr(ins):\n temporal, output = _str_oper(ins.quad[2], no_exaf=True)\n\n if not temporal:\n output.append('call __LOADSTR')\n REQUIRES.add('loadstr.asm')\n\n output.append('push hl')\n return output", "response": "Loads a string value from a memory address."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _store8(ins):\n output = _8bit_oper(ins.quad[2])\n\n op = ins.quad[1]\n\n indirect = op[0] == '*'\n if indirect:\n op = op[1:]\n\n immediate = op[0] == '#'\n if immediate:\n op = op[1:]\n\n if is_int(op) or op[0] == '_':\n if is_int(op):\n op = str(int(op) & 0xFFFF)\n\n if immediate:\n if indirect:\n output.append('ld (%s), a' % op)\n else: # ???\n output.append('ld (%s), a' % op)\n elif indirect:\n output.append('ld hl, (%s)' % op)\n output.append('ld (hl), a')\n else:\n output.append('ld (%s), a' % op)\n else:\n if immediate:\n if indirect: # A label not starting with _\n output.append('ld hl, (%s)' % op)\n output.append('ld (hl), a')\n else:\n output.append('ld (%s), a' % op)\n return output\n else:\n output.append('pop hl')\n\n if indirect:\n output.append('ld e, (hl)')\n output.append('inc hl')\n output.append('ld d, (hl)')\n output.append('ld (de), a')\n else:\n output.append('ld (hl), a')\n\n return output", "response": "Stores 2nd operand content into address of 1st operand."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nstores 2nd operand content into address of 1st operand.", "response": "def _store16(ins):\n \"\"\" Stores 2nd operand content into address of 1st operand.\n store16 a, x => *(&a) = x\n Use '*' for indirect store on 1st operand.\n \"\"\"\n output = []\n output = _16bit_oper(ins.quad[2])\n\n try:\n value = ins.quad[1]\n indirect = False\n if value[0] == '*':\n indirect = True\n value = value[1:]\n\n value = int(value) & 0xFFFF\n if indirect:\n output.append('ex de, hl')\n output.append('ld hl, (%s)' % str(value))\n output.append('ld (hl), e')\n output.append('inc hl')\n output.append('ld (hl), d')\n else:\n output.append('ld (%s), hl' % str(value))\n except ValueError:\n if value[0] == '_':\n if indirect:\n output.append('ex de, hl')\n output.append('ld hl, (%s)' % str(value))\n output.append('ld (hl), e')\n output.append('inc hl')\n output.append('ld (hl), d')\n else:\n output.append('ld (%s), hl' % str(value))\n elif value[0] == '#':\n value = value[1:]\n if indirect:\n output.append('ex de, hl')\n output.append('ld hl, (%s)' % str(value))\n output.append('ld (hl), e')\n output.append('inc hl')\n output.append('ld (hl), d')\n else:\n output.append('ld (%s), hl' % str(value))\n else:\n output.append('ex de, hl')\n if indirect:\n output.append('pop hl')\n output.append('ld a, (hl)')\n output.append('inc hl')\n output.append('ld h, (hl)')\n output.append('ld l, a')\n else:\n output.append('pop hl')\n\n output.append('ld (hl), e')\n output.append('inc hl')\n output.append('ld (hl), d')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nstoring 2nd operand content into address of 1st operand.", "response": "def _store32(ins):\n \"\"\" Stores 2nd operand content into address of 1st operand.\n store16 a, x => *(&a) = x\n \"\"\"\n op = ins.quad[1]\n\n indirect = op[0] == '*'\n if indirect:\n op = op[1:]\n\n immediate = op[0] == '#' # Might make no sense here?\n if immediate:\n op = op[1:]\n\n if is_int(op) or op[0] == '_' or immediate:\n output = _32bit_oper(ins.quad[2], preserveHL=indirect)\n\n if is_int(op):\n op = str(int(op) & 0xFFFF)\n\n if indirect:\n output.append('ld hl, (%s)' % op)\n output.append('call __STORE32')\n REQUIRES.add('store32.asm')\n\n return output\n\n output.append('ld (%s), hl' % op)\n output.append('ld (%s + 2), de' % op)\n\n return output\n\n output = _32bit_oper(ins.quad[2], preserveHL=True)\n output.append('pop hl')\n\n if indirect:\n output.append('call __ISTORE32')\n REQUIRES.add('store32.asm')\n\n return output\n\n output.append('call __STORE32')\n REQUIRES.add('store32.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nstoring 2\u00ba operand content into address of 1st operand.", "response": "def _storef16(ins):\n \"\"\" Stores 2\u00ba operand content into address of 1st operand.\n store16 a, x => *(&a) = x\n \"\"\"\n value = ins.quad[2]\n if is_float(value):\n val = float(ins.quad[2]) # Immediate?\n (de, hl) = f16(val)\n q = list(ins.quad)\n q[2] = (de << 16) | hl\n ins.quad = tuple(q)\n\n return _store32(ins)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nstore a floating point value into a memory address.", "response": "def _storef(ins):\n \"\"\" Stores a floating point value into a memory address.\n \"\"\"\n output = _float_oper(ins.quad[2])\n\n op = ins.quad[1]\n\n indirect = op[0] == '*'\n if indirect:\n op = op[1:]\n\n immediate = op[0] == '#' # Might make no sense here?\n if immediate:\n op = op[1:]\n\n if is_int(op) or op[0] == '_':\n if is_int(op):\n op = str(int(op) & 0xFFFF)\n\n if indirect:\n output.append('ld hl, (%s)' % op)\n else:\n output.append('ld hl, %s' % op)\n else:\n output.append('pop hl')\n if indirect:\n output.append('call __ISTOREF')\n REQUIRES.add('storef.asm')\n\n return output\n\n output.append('call __STOREF')\n REQUIRES.add('storef.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _storestr(ins):\n op1 = ins.quad[1]\n indirect = op1[0] == '*'\n if indirect:\n op1 = op1[1:]\n\n immediate = op1[0] == '#'\n if immediate and not indirect:\n raise InvalidIC('storestr does not allow immediate destination', ins.quad)\n\n if not indirect:\n op1 = '#' + op1\n\n tmp1, tmp2, output = _str_oper(op1, ins.quad[2], no_exaf=True)\n\n if not tmp2:\n output.append('call __STORE_STR')\n REQUIRES.add('storestr.asm')\n else:\n output.append('call __STORE_STR2')\n REQUIRES.add('storestr2.asm')\n\n return output", "response": "Stores a string value into a memory address."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nconverts data from typeA to typeB.", "response": "def _cast(ins):\n \"\"\" Convert data from typeA to typeB (only numeric data types)\n \"\"\"\n # Signed and unsigned types are the same in the Z80\n tA = ins.quad[2] # From TypeA\n tB = ins.quad[3] # To TypeB\n\n YY_TYPES[tA] # Type sizes\n xsB = sB = YY_TYPES[tB] # Type sizes\n\n output = []\n if tA in ('u8', 'i8'):\n output.extend(_8bit_oper(ins.quad[4]))\n elif tA in ('u16', 'i16'):\n output.extend(_16bit_oper(ins.quad[4]))\n elif tA in ('u32', 'i32'):\n output.extend(_32bit_oper(ins.quad[4]))\n elif tA == 'f16':\n output.extend(_f16_oper(ins.quad[4]))\n elif tA == 'f':\n output.extend(_float_oper(ins.quad[4]))\n else:\n raise errors.GenericError(\n 'Internal error: invalid typecast from %s to %s' % (tA, tB))\n\n if tB in ('u8', 'i8'): # It was a byte\n output.extend(to_byte(tA))\n elif tB in ('u16', 'i16'):\n output.extend(to_word(tA))\n elif tB in ('u32', 'i32'):\n output.extend(to_long(tA))\n elif tB == 'f16':\n output.extend(to_fixed(tA))\n elif tB == 'f':\n output.extend(to_float(tA))\n\n xsB += sB % 2 # make it even (round up)\n\n if xsB > 4:\n output.extend(_fpush())\n else:\n if xsB > 2:\n output.append('push de') # Fixed or 32 bit Integer\n\n if sB > 1:\n output.append('push hl') # 16 bit Integer\n else:\n output.append('push af') # 8 bit Integer\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\njumps if the top of the stack is 0 to arg ( 1 )", "response": "def _jzerof(ins):\n \"\"\" Jumps if top of the stack (40bit, float) is 0 to arg(1)\n \"\"\"\n value = ins.quad[1]\n if is_float(value):\n if float(value) == 0:\n return ['jp %s' % str(ins.quad[2])] # Always true\n else:\n return []\n\n output = _float_oper(value)\n output.append('ld a, c')\n output.append('or l')\n output.append('or h')\n output.append('or e')\n output.append('or d')\n output.append('jp z, %s' % str(ins.quad[2]))\n return output"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _jzerostr(ins):\n output = []\n disposable = False # True if string must be freed from memory\n\n if ins.quad[1][0] == '_': # Variable?\n output.append('ld hl, (%s)' % ins.quad[1][0])\n else:\n output.append('pop hl')\n output.append('push hl') # Saves it for later\n disposable = True\n\n output.append('call __STRLEN')\n\n if disposable:\n output.append('ex (sp), hl')\n output.append('call __MEM_FREE')\n output.append('pop hl')\n REQUIRES.add('alloc.asm')\n\n output.append('ld a, h')\n output.append('or l')\n output.append('jp z, %s' % str(ins.quad[2]))\n REQUIRES.add('strlen.asm')\n return output", "response": "Jumps if the top of the stack contains a NULL pointer\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\njumping if top of the stack is not 0 to arg ( 1 )", "response": "def _jnzero16(ins):\n \"\"\" Jumps if top of the stack (16bit) is != 0 to arg(1)\n \"\"\"\n value = ins.quad[1]\n if is_int(value):\n if int(value) != 0:\n return ['jp %s' % str(ins.quad[2])] # Always true\n else:\n return []\n\n output = _16bit_oper(value)\n output.append('ld a, h')\n output.append('or l')\n output.append('jp nz, %s' % str(ins.quad[2]))\n return output"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\njumps if top of the stack is!0 to arg ( 1 )", "response": "def _jnzero32(ins):\n \"\"\" Jumps if top of the stack (32bit) is !=0 to arg(1)\n \"\"\"\n value = ins.quad[1]\n if is_int(value):\n if int(value) != 0:\n return ['jp %s' % str(ins.quad[2])] # Always true\n else:\n return []\n\n output = _32bit_oper(value)\n output.append('ld a, h')\n output.append('or l')\n output.append('or e')\n output.append('or d')\n output.append('jp nz, %s' % str(ins.quad[2]))\n return output"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\njumping if top of the stack is!0 to arg ( 1 )", "response": "def _jnzerof16(ins):\n \"\"\" Jumps if top of the stack (32bit) is !=0 to arg(1)\n Fixed Point (16.16 bit) values.\n \"\"\"\n value = ins.quad[1]\n if is_float(value):\n if float(value) != 0:\n return ['jp %s' % str(ins.quad[2])] # Always true\n else:\n return []\n\n output = _f16_oper(value)\n output.append('ld a, h')\n output.append('or l')\n output.append('or e')\n output.append('or d')\n output.append('jp nz, %s' % str(ins.quad[2]))\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _jgezerou8(ins):\n output = []\n value = ins.quad[1]\n if not is_int(value):\n output = _8bit_oper(value)\n\n output.append('jp %s' % str(ins.quad[2]))\n return output", "response": "Jumps if top of the stack is > = 0 to arg ( 1 ) Always TRUE for unsigned\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _jgezeroi8(ins):\n value = ins.quad[1]\n if is_int(value):\n if int(value) >= 0:\n return ['jp %s' % str(ins.quad[2])] # Always true\n else:\n return []\n\n output = _8bit_oper(value)\n output.append('add a, a') # Puts sign into carry\n output.append('jp nc, %s' % str(ins.quad[2]))\n return output", "response": "Jumps if top of the stack is >= 0 to arg1"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\njump if top of the stack is > = 0 to arg ( 1 ) Always TRUE for unsigned", "response": "def _jgezerou16(ins):\n \"\"\" Jumps if top of the stack (16bit) is >= 0 to arg(1)\n Always TRUE for unsigned\n \"\"\"\n output = []\n value = ins.quad[1]\n if not is_int(value):\n output = _16bit_oper(value)\n\n output.append('jp %s' % str(ins.quad[2]))\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _jgezerou32(ins):\n output = []\n value = ins.quad[1]\n if not is_int(value):\n output = _32bit_oper(value)\n\n output.append('jp %s' % str(ins.quad[2]))\n return output", "response": "Jumps if top of the stack is >= 0 to arg ( 1 ) Always TRUE for unsigned\n "} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns from a procedure or function an 8bits value", "response": "def _ret8(ins):\n \"\"\" Returns from a procedure / function an 8bits value\n \"\"\"\n output = _8bit_oper(ins.quad[1])\n output.append('#pragma opt require a')\n output.append('jp %s' % str(ins.quad[2]))\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns from a procedure or function a 16bits value", "response": "def _ret16(ins):\n \"\"\" Returns from a procedure / function a 16bits value\n \"\"\"\n output = _16bit_oper(ins.quad[1])\n output.append('#pragma opt require hl')\n output.append('jp %s' % str(ins.quad[2]))\n return output"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _ret32(ins):\n output = _32bit_oper(ins.quad[1])\n output.append('#pragma opt require hl,de')\n output.append('jp %s' % str(ins.quad[2]))\n return output", "response": "Returns from a procedure or function a 32bits value"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn from a procedure a function a Fixed Point ( 32bits ) value", "response": "def _retf16(ins):\n \"\"\" Returns from a procedure / function a Fixed Point (32bits) value\n \"\"\"\n output = _f16_oper(ins.quad[1])\n output.append('#pragma opt require hl,de')\n output.append('jp %s' % str(ins.quad[2]))\n return output"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _retf(ins):\n output = _float_oper(ins.quad[1])\n output.append('#pragma opt require a,bc,de')\n output.append('jp %s' % str(ins.quad[2]))\n return output", "response": "Returns from a procedure a Floating Point ( 40bits ) value\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn from a procedure or function a string pointer ( 16bits ) value", "response": "def _retstr(ins):\n \"\"\" Returns from a procedure / function a string pointer (16bits) value\n \"\"\"\n tmp, output = _str_oper(ins.quad[1], no_exaf=True)\n\n if not tmp:\n output.append('call __LOADSTR')\n REQUIRES.add('loadstr.asm')\n\n output.append('#pragma opt require hl')\n output.append('jp %s' % str(ins.quad[2]))\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _call(ins):\n output = []\n output.append('call %s' % str(ins.quad[1]))\n\n try:\n val = int(ins.quad[2])\n if val == 1:\n output.append('push af') # Byte\n else:\n if val > 4:\n output.extend(_fpush())\n else:\n if val > 2:\n output.append('push de')\n if val > 1:\n output.append('push hl')\n\n except ValueError:\n pass\n\n return output", "response": "Returns a list of the return values of a function."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _leave(ins):\n global FLAG_use_function_exit\n\n output = []\n\n if ins.quad[1] == '__fastcall__':\n output.append('ret')\n return output\n\n nbytes = int(ins.quad[1]) # Number of bytes to pop (params size)\n\n if nbytes == 0:\n output.append('ld sp, ix')\n output.append('pop ix')\n output.append('ret')\n\n return output\n\n if nbytes == 1:\n output.append('ld sp, ix')\n output.append('pop ix')\n output.append('inc sp') # \"Pops\" 1 byte\n output.append('ret')\n\n return output\n\n if nbytes <= 11: # Number of bytes it worth the hassle to \"pop\" off the stack\n output.append('ld sp, ix')\n output.append('pop ix')\n output.append('exx')\n output.append('pop hl')\n for i in range((nbytes >> 1) - 1):\n output.append('pop bc') # Removes (n * 2 - 2) bytes form the stack\n\n if nbytes & 1: # Odd?\n output.append('inc sp') # \"Pops\" 1 byte (This should never happens, since params are always even-sized)\n\n output.append('ex (sp), hl') # Place back return address\n output.append('exx')\n output.append('ret')\n\n return output\n\n if not FLAG_use_function_exit:\n FLAG_use_function_exit = True # Use standard exit\n output.append('exx')\n output.append('ld hl, %i' % nbytes)\n output.append('__EXIT_FUNCTION:')\n output.append('ld sp, ix')\n output.append('pop ix')\n output.append('pop de')\n output.append('add hl, sp')\n output.append('ld sp, hl')\n output.append('push de')\n output.append('exx')\n output.append('ret')\n else:\n output.append('exx')\n output.append('ld hl, %i' % nbytes)\n output.append('jp __EXIT_FUNCTION')\n\n return output", "response": "Return from a function popping N bytes from the stack and leaving the stack."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nenters a function sequence for doing a function.", "response": "def _enter(ins):\n \"\"\" Enter function sequence for doing a function start\n ins.quad[1] contains size (in bytes) of local variables\n Use '__fastcall__' as 1st parameter to prepare a fastcall\n function (no local variables).\n \"\"\"\n output = []\n\n if ins.quad[1] == '__fastcall__':\n return output\n\n output.append('push ix')\n output.append('ld ix, 0')\n output.append('add ix, sp')\n\n size_bytes = int(ins.quad[1])\n\n if size_bytes != 0:\n if size_bytes < 7:\n output.append('ld hl, 0')\n output.extend(['push hl'] * (size_bytes >> 1))\n\n if size_bytes % 2: # odd?\n output.append('push hl')\n output.append('inc sp')\n else:\n output.append('ld hl, -%i' % size_bytes) # \"Pushes nn bytes\"\n output.append('add hl, sp')\n output.append('ld sp, hl')\n output.append('ld (hl), 0')\n output.append('ld bc, %i' % (size_bytes - 1))\n output.append('ld d, h')\n output.append('ld e, l')\n output.append('inc de')\n output.append('ldir') # Clear with ZEROs\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\npush a 32bit param into the stack", "response": "def _param32(ins):\n \"\"\" Pushes 32bit param into the stack\n \"\"\"\n output = _32bit_oper(ins.quad[1])\n output.append('push de')\n output.append('push hl')\n return output"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\npush 32bit fixed point param into the stack", "response": "def _paramf16(ins):\n \"\"\" Pushes 32bit fixed point param into the stack\n \"\"\"\n output = _f16_oper(ins.quad[1])\n output.append('push de')\n output.append('push hl')\n return output"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\npushing 40bit float param into the stack", "response": "def _paramf(ins):\n \"\"\" Pushes 40bit (float) param into the stack\n \"\"\"\n output = _float_oper(ins.quad[1])\n output.extend(_fpush())\n return output"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncopying a block of memory from param 2 addrto param 1 addr.", "response": "def _memcopy(ins):\n \"\"\" Copies a block of memory from param 2 addr\n to param 1 addr.\n \"\"\"\n output = _16bit_oper(ins.quad[3])\n output.append('ld b, h')\n output.append('ld c, l')\n output.extend(_16bit_oper(ins.quad[1], ins.quad[2], reversed=True))\n output.append('ldir') # ***\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef convertToBool():\n if not OPTIONS.strictBool.value:\n return []\n\n REQUIRES.add('strictbool.asm')\n\n result = []\n result.append('pop af')\n result.append('call __NORMALIZE_BOOLEAN')\n result.append('push af')\n\n return result", "response": "Convert a byte value to boolean"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef emit_end(MEMORY=None):\n output = []\n output.extend(AT_END)\n\n if REQUIRES.intersection(MEMINITS) or '__MEM_INIT' in INITS:\n output.append(OPTIONS.heap_start_label.value + ':')\n output.append('; Defines DATA END\\n' + 'ZXBASIC_USER_DATA_END EQU ZXBASIC_MEM_HEAP + ZXBASIC_HEAP_SIZE')\n else:\n output.append('; Defines DATA END --> HEAP size is 0\\n' + 'ZXBASIC_USER_DATA_END EQU ZXBASIC_MEM_HEAP')\n\n output.append('; Defines USER DATA Length in bytes\\n' +\n 'ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA')\n\n if OPTIONS.autorun.value:\n output.append('END %s' % START_LABEL)\n else:\n output.append('END')\n\n return output", "response": "This function is used to emit the end of the current memory area."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nbegin converting each quad instruction to asm by iterating over the \"mem\" array, and called its associated function. Each function returns an array of ASM instructions which will be appended to the 'output' array", "response": "def emit(mem):\n \"\"\" Begin converting each quad instruction to asm\n by iterating over the \"mem\" array, and called its\n associated function. Each function returns an array of\n ASM instructions which will be appended to the\n 'output' array\n \"\"\"\n\n def output_join(output, new_chunk):\n \"\"\" Extends output instruction list\n performing a little peep-hole optimization\n \"\"\"\n changed = True and OPTIONS.optimization.value > 0 # Only enter here if -O0 was not set\n\n while changed and new_chunk:\n if output:\n a1 = output[-1] # Last output instruction\n i1 = inst(a1)\n o1 = oper(a1)\n else:\n a1 = i1 = o1 = None\n\n if len(output) > 1:\n a0 = output[-2]\n i0 = inst(a0)\n o0 = oper(a0)\n else:\n a0 = i0 = o0 = None\n\n a2 = new_chunk[0] # Fist new output instruction\n i2 = inst(a2)\n o2 = oper(a2)\n\n if OPT00 and i2[-1] == ':':\n # Ok, a2 is a label\n # check if the above starts with jr / jp\n if i1 in ('jp', 'jr') and o1[0] == i2[:-1]:\n # Ok remove the above instruction\n output.pop()\n changed = True\n continue\n\n if OPT01 and i1 == 'push' and i2 == 'pop' and o1[0] == o2[0]:\n # Ok, we have a push/pop sequence which refers to\n # the same register pairs\n # Ok, remove these instructions (both)\n output.pop()\n new_chunk = new_chunk[1:]\n changed = True\n continue\n\n if (\n OPT03 and\n (i1 == 'sbc' and\n o1[0] == o1[1] == 'a' and\n i2 == 'or' and\n o2[0] == 'a' and\n len(new_chunk) > 1)\n ):\n a3 = new_chunk[1]\n i3 = inst(a3)\n o3 = oper(a3)\n c = condition(a3)\n if i3 in ('jp', 'jr') and c in ('z', 'nz'):\n c = 'nc' if c == 'z' else 'c'\n changed = True\n output.pop()\n new_chunk.pop(0)\n new_chunk[0] = '%s %s, %s' % (i3, c, o3[0])\n continue\n\n if OPT04 and i1 == 'push' and i2 == 'pop':\n if 'af' in (o1[0], o2[0]):\n output.pop()\n new_chunk[0] = 'ld %s, %s' % (o2[0][0], o1[0][0])\n changed = True\n continue\n\n if o1[0] in ('hl', 'de') and o2[0] in ('hl', 'de'):\n # push hl; push de; pop hl; pop de || push de; push hl; pop de; pop hl => ex de, hl\n if len(new_chunk) > 1 and len(output) > 1 and oper(new_chunk[1])[0] == o1[0] and \\\n o2[0] == oper(output[-2])[0] and \\\n inst(output[-2]) == 'push' and inst(new_chunk[1]) == 'pop':\n output.pop()\n new_chunk.pop(0)\n new_chunk.pop(0)\n output[-1] = 'ex de, hl'\n changed = True\n continue\n\n # push hl; pop de || push de ; pop hl\n if len(new_chunk) > 1 and inst(new_chunk[1]) in ('pop', 'ld') and oper(new_chunk[1])[0] == o1[0]:\n output.pop()\n new_chunk[0] = 'ex de, hl'\n changed = True\n continue\n\n if o1[0] not in ('ix', 'iy') and o2[0] not in ('ix', 'iy'):\n # Change push XX, pop YY sequence with ld Yh, Xl; ld Yl, Xl\n output.pop()\n new_chunk = ['ld %s, %s' % (o2[0][0], o1[0][0])] + new_chunk\n new_chunk[1] = 'ld %s, %s' % (o2[0][1], o1[0][1])\n changed = True\n continue\n\n # ex af, af'; ex af, af' => \n # ex de, hl ; ex de, hl => \n if OPT16 and i1 == i2 == 'ex' and o1 == o2:\n output.pop()\n new_chunk.pop(0)\n changed = True\n continue\n\n # Tries to optimize:\n # jp , LABEL\n # jp OTHER\n # LABEL:\n # into\n # JP !, OTHER\n # LABEL:\n if OPT17 and len(output) > 1:\n if i0 == i1 == 'jp' \\\n and i2[-1] == ':' \\\n and condition(a0) in {'c', 'nc', 'z', 'nz'} \\\n and condition(a1) is None \\\n and a2[:-1] in o0:\n output.pop()\n output.pop()\n new_chunk = ['jp %s, %s' % ({'c': 'nc',\n 'z': 'nz',\n 'nc': 'c',\n 'nz': 'z'}[condition(a0)], o1[0])] + new_chunk\n changed = True\n continue\n\n # Tries to optimize a == b for U/Integers\n # call __EQ16\n # or a\n # jp nz, ...\n # into:\n # or a\n # sbc hl, de\n # jp z, ...\n if OPT18 and i1 == 'call' and o1[0] == '__EQ16' \\\n and i2 in {'or', 'and'} and o2[0] == 'a' \\\n and len(new_chunk) > 1:\n a3 = new_chunk[1]\n i3 = inst(a3)\n c3 = condition(a3)\n if i3 == 'jp' and c3 in {'z', 'nz'}:\n cond = 'z' if c3 == 'nz' else 'nz'\n new_chunk[1] = 'jp %s, %s' % (cond, oper(a3)[0])\n output.pop()\n new_chunk.insert(1, 'sbc hl, de')\n changed = True\n continue\n\n # Tries to optimize a == b for U/Bytes\n # sub N\n # sub 1\n # jp nc, __LABEL\n # into:\n # sub N\n # or a\n # jp nz, __LABEL\n if OPT19 and i1 == 'sub' and '1' in o1 and i2 == 'jp' and len(output) > 1:\n c2 = condition(new_chunk[0])\n if c2 in {'c', 'nc'}:\n cond = 'z' if c2 == 'c' else 'nz'\n new_chunk[0] = 'jp %s, %s' % (cond, o2[0])\n if i0 in ('sub', 'dec'):\n output.pop()\n else:\n output[-1] = 'or a'\n changed = True\n continue\n\n # Removes useless or a from sequence:\n # sub X\n # or a\n # jp z/nz ...\n if OPT21 and i1 == 'sub' and i2 in {'or', 'and'} and o2[0] == 'a':\n new_chunk.pop(0)\n changed = True\n continue\n\n # Converts:\n # ld a, (ix +/- n)\n # ld r, a\n # pop af\n # Into:\n # pop af\n # ld r, (ix +/- n)\n if OPT22 and len(output) > 1 and i1 == 'ld' and o1[0] in 'bcdehl' and o1[1] == 'a' and \\\n (i2, o2) == ('pop', ['af', 'sp']):\n if (i0, o0[:1]) == ('ld', ['a']) and RE_IX_IDX.match(o0[1]):\n output.pop() # Removes ld r, a\n output.pop() # Removes ld a, (ix + n)\n new_chunk.insert(1, 'ld %s, %s' % (o1[0], o0[1])) # Inserts 'ld r, (ix + n)' after 'pop af'\n changed = True\n continue\n\n # Converts:\n # ld hl, (NN) | ld hl, NN | pop hl\n # ld b, h\n # ld c, l\n # in a, (c)\n # Into:\n # ld bc, (NN) | ld bc, NN | pop bc\n # in a, (c)\n if OPT23 and len(new_chunk) > 3 and inst(new_chunk[3]) == 'in':\n ia = inst(new_chunk[1])\n oa = oper(new_chunk[1])\n ib = inst(new_chunk[2])\n ob = oper(new_chunk[2])\n if (ia, oa[0], oa[1], ib, ob[0], ob[1]) == ('ld', 'b', 'h', 'ld', 'c', 'l'):\n ii = inst(new_chunk[0])\n oi = oper(new_chunk[0])\n if ii in ('pop', 'ld') and oi[0] == 'hl':\n new_chunk[0] = ii + ' ' + 'bc' + (', %s' % oi[1] if ii == 'ld' else '')\n new_chunk.pop(1)\n new_chunk.pop(1)\n changed = True\n continue\n\n # Converts:\n # ld hl, (NN) | ld hl, NN | pop hl\n # ld b, h\n # ld c, l\n # out (c), a\n # Into:\n # ld bc, (NN) | ld bc, NN | pop bc\n # out (c), a\n if OPT23 and len(new_chunk) > 3 and inst(new_chunk[-1]) == 'out':\n ia = inst(new_chunk[-3])\n oa = oper(new_chunk[-3])\n ib = inst(new_chunk[-2])\n ob = oper(new_chunk[-2])\n if (ia, oa[0], oa[1], ib, ob[0], ob[1]) == ('ld', 'b', 'h', 'ld', 'c', 'l'):\n ii = inst(new_chunk[-4])\n oi = oper(new_chunk[-4])\n if ii in ('pop', 'ld') and oi[0] == 'hl':\n new_chunk[-4] = ii + ' ' + 'bc' + (', %s' % oi[1] if ii == 'ld' else '')\n new_chunk.pop(-2)\n new_chunk.pop(-2)\n changed = True\n continue\n\n # Converts:\n # or X | and X\n # or a | and a\n # Into:\n # or X | and X\n if OPT24 and i1 in ('and', 'or') and new_chunk[0] in ('or a', 'and a'):\n new_chunk.pop(0)\n changed = True\n continue\n\n # Converts:\n # ld h, X (X != A)\n # ld a, Y\n # or/and/cp/add/sub h\n # Into:\n # ld a, Y\n # or/and/cp X\n if OPT25 and \\\n (i1 in ('cp', 'or', 'and') and o1[0] == 'h' or\n i1 in ('sub', 'add', 'sbc', 'adc') and o1[1] == 'h') \\\n and i0 == 'ld' and o0[0] == 'a' and len(output) > 2:\n ii = inst(output[-3])\n oo = oper(output[-3])\n if i1 in ('add', 'adc', 'sbc'):\n i1 = i1 + ' a,'\n if ii == 'ld' and oo[0] == 'h' and oo[1] != 'a':\n output[-1] = '{0} {1}'.format(i1, oo[1])\n output.pop(-3)\n changed = True\n continue\n\n # Converts:\n # ld a, (nn) | ld a, (ix+N)\n # inc/dec a\n # ld (nn), a | ld (ix+N), a\n # Into:\n # ld hl, _n | \n # inc/dec (hl) | inc/dec (ix+N)\n if OPT26 and i1 in ('inc', 'dec') and o1[0] == 'a' and i0 == i2 == 'ld' and \\\n (o0[0], o0[1]) == (o2[1], o2[0]) and o0[1][0] == '(':\n new_chunk.pop(0)\n if RE_IX_IDX.match(o0[1]):\n output[-1] = '{0} {1}'.format(i1, o0[1])\n output.pop(-2)\n else:\n output[-1] = '{0} (hl)'.format(i1)\n output[-2] = 'ld hl, {0}'.format(o0[1][1:-1])\n changed = True\n continue\n\n # Converts:\n # ld X, Y\n # ld Y, X\n # Into:\n # ld X, Y\n if OPT02 and i1 == i2 == 'ld' and o1[0] == o2[1] and o2[0] == o1[1]:\n # This and previous instruction are LD X, Y\n # Ok, previous instruction is LD A, B and current is LD B, A. Remove this one.\n new_chunk = new_chunk[1:]\n changed = True\n continue\n\n # Converts:\n # ld h, X\n # or/and h\n # Into:\n # or/and X\n if OPT27 and i1 == 'ld' and o1[0] == 'h' and i2 in ('and', 'or') and o2[0] == 'h':\n output.pop()\n new_chunk[0] = '{0} {1}'.format(i2, o1[1])\n changed = True\n continue\n\n # Converts\n # ld a, r|(ix+/-N)|(hl)\n # ld h, a\n # ld a, XXX | pop af\n # Into:\n # ld h, r|(ix+/-N)|(hl)\n # ld a, XXX | pop af\n if OPT28 and i1 == i0 == 'ld' and o0[0] == 'a' and \\\n (o0[1] in ('a', 'b', 'c', 'd', 'e', 'h', 'l', '(hl)') or RE_IX_IDX.match(o0[1])) and \\\n (o1[0], o1[1]) == ('h', 'a') and new_chunk and (new_chunk[0] == 'pop af' or\n i2 == 'ld' and o2[0] == 'a'):\n\n output.pop()\n output[-1] = 'ld h, {0}'.format(o0[1])\n changed = True\n continue\n\n # Converts:\n # cp 0\n # Into:\n # or a\n if OPT29 and i1 == 'cp' and o1[0] == '0':\n output[-1] = 'or a'\n changed = True\n continue\n\n # Converts:\n # or/and X\n # jp c/nc XXX\n # Into:\n # /jp XXX\n if OPT30 and i1 in ('and', 'or') and i2 == 'jp':\n c = condition(new_chunk[0])\n if c in ('c', 'nc'):\n output.pop()\n if c == 'nc':\n new_chunk[0] = 'jp {0}'.format(o2[0])\n else:\n new_chunk.pop(0)\n changed = True\n continue\n\n # Converts\n # jp XXX\n # \n # Into:\n # jp XXX\n if OPT31 and i1 == 'jp' and not condition(output[-1]) and i2 is not None and \\\n i2[-1] != ':' and new_chunk[0] not in ASMS:\n new_chunk.pop(0)\n changed = True\n continue\n\n # Converts:\n # call __LOADSTR\n # ld a, 1\n # call __PRINSTR\n # Into:\n # xor a\n # call __PRINTSTR\n if OPT32 and i0 == 'call' and o0[0] == '__LOADSTR' and i1 == 'ld' and tuple(o1) == ('a', '1') and \\\n i2 == 'call' and o2[0] == '__PRINTSTR':\n output.pop(-2)\n output[-1] = 'xor a'\n changed = True\n continue\n\n changed, new_chunk = optiblock(new_chunk)\n\n output.extend(new_chunk)\n\n output = []\n for i in mem:\n output_join(output, QUADS[i.quad[0]][1](i))\n if RE_BOOL.match(i.quad[0]): # If it is a boolean operation convert it to 0/1 if the STRICT_BOOL flag is True\n output_join(output, convertToBool())\n\n changed = OPTIONS.optimization.value > 1\n while changed:\n to_remove = []\n\n for i, ins in enumerate(output):\n ins = ins[:-1]\n if ins not in TMP_LABELS:\n continue\n\n for j, ins2 in enumerate(output):\n if j == i:\n continue\n if ins in oper(ins2):\n break\n else:\n to_remove.append(i)\n\n changed = len(to_remove) > 0\n to_remove.reverse()\n for i in to_remove:\n output.pop(i)\n\n tmp = output\n output = []\n for i in tmp:\n output_join(output, [i])\n\n for i in sorted(REQUIRES):\n output.append('#include once <%s>' % i)\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef t_defexpr_CONTINUE(self, t):\n r'[\\\\_]\\r?\\n'\n t.lexer.lineno += 1\n t.value = t.value[1:]\n return t", "response": "r \\ _ \\ r?\\n"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef t_prepro_ID(self, t):\n r'[_a-zA-Z][_a-zA-Z0-9]*' # preprocessor directives\n t.type = reserved_directives.get(t.value.lower(), 'ID')\n states_ = {\n 'DEFINE': 'define',\n 'PRAGMA': 'pragma',\n 'IF': 'if',\n 'ERROR': 'msg',\n 'WARNING': 'msg'\n }\n\n if t.type in states_:\n t.lexer.begin(states_[t.type])\n elif t.type == 'ID' and self.expectingDirective:\n self.error(\"invalid directive #%s\" % t.value)\n\n self.expectingDirective = False\n return t", "response": "Look for reserved ID directives and parse them as ID."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef t_INITIAL_asm_sharp(self, t):\n r'[ \\t]*\\#' # Only matches if at beginning of line and \"#\"\n if self.find_column(t) == 1:\n t.lexer.push_state('prepro') # Start preprocessor\n self.expectingDirective = True", "response": "Tokenize the initial asm."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef put_current_line(self, prefix='', suffix=''):\n return '%s#line %i \"%s\"%s' % (prefix, self.lex.lineno, self.filestack[-1][0], suffix)", "response": "Returns line and file for include sequences."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nperform and end of include.", "response": "def include_end(self):\n \"\"\" Performs and end of include.\n \"\"\"\n self.lex = self.filestack[-1][2]\n self.input_data = self.filestack[-1][3]\n self.filestack.pop()\n\n if not self.filestack: # End of input?\n return\n\n self.filestack[-1][1] += 1 # Increment line counter of previous file\n\n result = lex.LexToken()\n result.value = self.put_current_line(suffix='\\n')\n result.type = '_ENDFILE_'\n result.lineno = self.lex.lineno\n result.lexpos = self.lex.lexpos\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndefining input string removing current lexer.", "response": "def input(self, str, filename=''):\n \"\"\" Defines input string, removing current lexer.\n \"\"\"\n self.filestack.append([filename, 1, self.lex, self.input_data])\n\n self.input_data = str\n self.lex = lex.lex(object=self)\n self.lex.input(self.input_data)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a token from the current input.", "response": "def token(self):\n \"\"\" Returns a token from the current input. If tok is None\n from the current input, it means we are at end of current input\n (e.g. at end of include file). If so, closes the current input\n and discards it; then pops the previous input and lexer from\n the input stack, and gets another token.\n\n If new token is again None, repeat the process described above\n until the token is either not None, or self.lex is None, wich\n means we must effectively return None, because parsing has\n ended.\n \"\"\"\n tok = None\n if self.next_token is not None:\n tok = lex.LexToken()\n tok.value = ''\n tok.lineno = self.lex.lineno\n tok.lexpos = self.lex.lexpos\n tok.type = self.next_token\n self.next_token = None\n\n while self.lex is not None and tok is None:\n tok = self.lex.token()\n if tok is not None:\n break\n\n tok = self.include_end()\n\n return tok"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nfinds the column in the input file that contains the given token.", "response": "def find_column(self, token):\n \"\"\" Compute column:\n - token is a token instance\n \"\"\"\n i = token.lexpos\n while i > 0:\n if self.input_data[i - 1] == '\\n':\n break\n i -= 1\n\n column = token.lexpos - i + 1\n return column"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nconverts duration pitch to a pair of unsigned 16 bit integers and to be loaded in DE HL following the ROM listing.", "response": "def getDEHL(duration, pitch):\n \"\"\"Converts duration,pitch to a pair of unsigned 16 bit integers,\n to be loaded in DE,HL, following the ROM listing.\n Returns a t-uple with the DE, HL values.\n \"\"\"\n intPitch = int(pitch)\n fractPitch = pitch - intPitch # Gets fractional part\n tmp = 1 + 0.0577622606 * fractPitch\n if not -60 <= intPitch <= 127:\n raise BeepError('Pitch out of range: must be between [-60, 127]')\n\n if duration < 0 or duration > 10:\n raise BeepError('Invalid duration: must be between [0, 10]')\n\n A = intPitch + 60\n B = -5 + int(A / 12) # -5 <= B <= 10\n A %= 0xC # Semitones above C\n\n frec = TABLE[A]\n tmp2 = tmp * frec\n f = tmp2 * 2.0 ** B\n\n DE = int(0.5 + f * duration - 1)\n HL = int(0.5 + 437500.0 / f - 30.125)\n return DE, HL"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef make_node(cls, *args):\n new_args = []\n\n args = [x for x in args if not is_null(x)]\n for x in args:\n assert isinstance(x, Symbol)\n if x.token == 'BLOCK':\n new_args.extend(SymbolBLOCK.make_node(*x.children).children)\n else:\n new_args.append(x)\n\n result = SymbolBLOCK(*new_args)\n return result", "response": "Creates a chain of code blocks."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef t_MACROS(t):\n r'__[a-zA-Z]+__'\n\n if t.value in macros:\n t.type = t.value\n return t\n\n syntax_error(t.lexer.lineno, \"unknown macro '%s'\" % t.value)", "response": "r A macro can only be used in the tree"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef t_string_NGRAPH(t):\n r\"\\\\[ '.:][ '.:]\"\n global __STRING\n\n P = {' ': 0, \"'\": 2, '.': 8, ':': 10}\n N = {' ': 0, \"'\": 1, '.': 4, ':': 5}\n\n __STRING += chr(128 + P[t.value[1]] + N[t.value[2]])", "response": "r A string with NGram"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef t_asm(t):\n r'\\b[aA][sS][mM]\\b'\n\n global ASM, ASMLINENO, IN_STATE\n t.lexer.begin('asm')\n\n ASM = ''\n ASMLINENO = t.lexer.lineno\n IN_STATE = True", "response": "r \\ b [ aA][sS][mM]\\b"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef t_ID(t):\n r'[a-zA-Z][a-zA-Z0-9]*[$%]?'\n t.type = reserved.get(t.value.lower(), 'ID')\n callables = {\n api.constants.CLASS.array: 'ARRAY_ID',\n }\n\n if t.type != 'ID':\n t.value = t.type\n else:\n entry = api.global_.SYMBOL_TABLE.get_entry(t.value) if api.global_.SYMBOL_TABLE is not None else None\n if entry:\n t.type = callables.get(entry.class_, t.type)\n\n if t.type == 'BIN':\n t.lexer.begin('bin')\n return None\n\n return t", "response": "t_ID is a token that is used to identify the type of the object"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef t_bin_ZERO(t):\n r'[^01]'\n t.lexer.begin('INITIAL')\n t.type = 'NUMBER'\n t.value = 0\n t.lexer.lexpos -= 1\n return t", "response": "r A binary number."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncomputes the column of the next non - blank line in the token instance.", "response": "def find_column(token):\n \"\"\" Compute column:\n input is the input text string\n token is a token instance\n \"\"\"\n i = token.lexpos\n input = token.lexer.lexdata\n\n while i > 0:\n if input[i - 1] == '\\n':\n break\n i -= 1\n\n column = token.lexpos - i + 1\n\n return column"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns whether the token is a label.", "response": "def is_label(token):\n \"\"\" Return whether the token is a label (an integer number or id\n at the beginning of a line.\n\n To do so, we compute find_column() and moves back to the beginning\n of the line if previous chars are spaces or tabs. If column 0 is\n reached, it's a label.\n \"\"\"\n if not LABELS_ALLOWED:\n return False\n\n c = i = token.lexpos\n input = token.lexer.lexdata\n c -= 1\n while c > 0 and input[c] in (' ', '\\t'):\n c -= 1\n\n while i > 0:\n if input[i] == '\\n':\n break\n i -= 1\n\n column = c - i\n\n if column == 0:\n column += 1\n\n return column == 1"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _get_val(other):\n assert isinstance(other, (numbers.Number, SymbolNUMBER, SymbolCONST))\n if isinstance(other, SymbolNUMBER):\n return other.value\n\n if isinstance(other, SymbolCONST):\n return other.expr.value\n\n return other", "response": "Given a Number a Numeric Constant or a python number return its value\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndumping a table on screen for debugging purposes", "response": "def __dumptable(self, table):\n \"\"\" Dumps table on screen\n for debugging purposes\n \"\"\"\n for x in table.table.keys():\n sys.stdout.write(\"{0}\\t<--- {1} {2}\".format(x, table[x], type(table[x])))\n if isinstance(table[x], ID):\n sys.stdout(\" {0}\".format(table[x].value)),\n sys.stdout.write(\"\\n\")"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nvisits the Return node.", "response": "def visit_RETURN(self, node):\n \"\"\" Visits only children[1], since children[0] points to\n the current function being returned from (if any), and\n might cause infinite recursion.\n \"\"\"\n if len(node.children) == 2:\n node.children[1] = (yield ToVisit(node.children[1]))\n yield node"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef count(self):\n return functools.reduce(lambda x, y: x * y, (x.count for x in self.bounds))", "response": "Count the number of cells in the array."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the size of the array cell + indexes", "response": "def memsize(self):\n \"\"\" Total array cell + indexes size\n \"\"\"\n return self.size + 1 + TYPE.size(gl.BOUND_TYPE) * len(self.bounds)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a node containing the type cast of the given one.", "response": "def make_node(cls, new_type, node, lineno):\n \"\"\" Creates a node containing the type cast of\n the given one. If new_type == node.type, then\n nothing is done, and the same node is\n returned.\n\n Returns None on failure (and calls syntax_error)\n \"\"\"\n assert isinstance(new_type, SymbolTYPE)\n\n # None (null) means the given AST node is empty (usually an error)\n if node is None:\n return None # Do nothing. Return None\n\n assert isinstance(node, Symbol), '<%s> is not a Symbol' % node\n # The source and dest types are the same\n if new_type == node.type_:\n return node # Do nothing. Return as is\n\n STRTYPE = TYPE.string\n # Typecasting, at the moment, only for number\n if node.type_ == STRTYPE:\n syntax_error(lineno, 'Cannot convert string to a value. '\n 'Use VAL() function')\n return None\n\n # Converting from string to number is done by STR\n if new_type == STRTYPE:\n syntax_error(lineno, 'Cannot convert value to string. '\n 'Use STR() function')\n return None\n\n # If the given operand is a constant, perform a static typecast\n if is_CONST(node):\n node.expr = cls(new_type, node.expr, lineno)\n return node\n\n if not is_number(node) and not is_const(node):\n return cls(new_type, node, lineno)\n\n # It's a number. So let's convert it directly\n if is_const(node):\n node = SymbolNUMBER(node.value, node.lineno, node.type_)\n\n if new_type.is_basic and not TYPE.is_integral(new_type): # not an integer\n node.value = float(node.value)\n else: # It's an integer\n new_val = (int(node.value) & ((1 << (8 * new_type.size)) - 1)) # Mask it\n\n if node.value >= 0 and node.value != new_val:\n errmsg.warning_conversion_lose_digits(node.lineno)\n node.value = new_val\n elif node.value < 0 and (1 << (new_type.size * 8)) + \\\n node.value != new_val: # Test for positive to negative coercion\n errmsg.warning_conversion_lose_digits(node.lineno)\n node.value = new_val - (1 << (new_type.size * 8))\n\n node.type_ = new_type\n return node"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nnormalizes a namespace to be a single dot - separated list of dots.", "response": "def normalize_namespace(namespace):\n \"\"\" Given a namespace (e.g. '.' or 'mynamespace'),\n returns it in normalized form. That is:\n - always prefixed with a dot\n - no trailing dots\n - any double dots are converted to single dot (..my..namespace => .my.namespace)\n - one or more dots (e.g. '.', '..', '...') are converted to '.' (Global namespace)\n \"\"\"\n namespace = (DOT + DOT.join(RE_DOTS.split(namespace))).rstrip(DOT) + DOT\n return namespace"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef p_program(p):\n if p[1] is not None:\n [MEMORY.add_instruction(x) for x in p[1] if isinstance(x, Asm)]", "response": "Program is a list of Asm objects."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nprograming line is a list of instruction objects", "response": "def p_program_line(p):\n \"\"\" program : program line\n \"\"\"\n if p[2] is not None:\n [MEMORY.add_instruction(x) for x in p[2] if isinstance(x, Asm)]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_def_label(p):\n p[0] = None\n __DEBUG__(\"Declaring '%s%s' in %i\" % (NAMESPACE, p[1], p.lineno(1)))\n MEMORY.declare_label(p[1], p.lineno(1), p[3])", "response": "A label declaration is a special case."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nlines - label asms NEWLINE", "response": "def p_line_label_asm(p):\n \"\"\" line : LABEL asms NEWLINE\n \"\"\"\n p[0] = p[2]\n __DEBUG__(\"Declaring '%s%s' (value %04Xh) in %i\" % (NAMESPACE, p[1], MEMORY.org, p.lineno(1)))\n MEMORY.declare_label(p[1], p.lineno(1))"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_asm_ld8(p):\n if p[2] in ('H', 'L') and p[4] in ('IXH', 'IXL', 'IYH', 'IYL'):\n p[0] = None\n error(p.lineno(0), \"Unexpected token '%s'\" % p[4])\n else:\n p[0] = Asm(p.lineno(1), 'LD %s,%s' % (p[2], p[4]))", "response": "Parse an ASM L8 sequence."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef p_LOCAL(p):\n p[0] = None\n for label, line in p[2]:\n __DEBUG__(\"Setting label '%s' as local at line %i\" % (label, line))\n\n MEMORY.set_label(label, line, local=True)", "response": "A macro that sets the first element of the tree to be local."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef p_DEFS(p): # Define bytes\n if len(p[2]) > 2:\n error(p.lineno(1), \"too many arguments for DEFS\")\n\n if len(p[2]) < 2:\n num = Expr.makenode(Container(0, p.lineno(1))) # Defaults to 0\n p[2] = p[2] + (num,)\n\n p[0] = Asm(p.lineno(1), 'DEFS', p[2])", "response": "asm : DEFS number_list"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef p_namespace(p):\n global NAMESPACE\n\n NAMESPACE = normalize_namespace(p[2])\n __DEBUG__('Setting namespace to ' + (NAMESPACE.rstrip(DOT) or DOT), level=1)", "response": "Set the NAMESPACE property of the asm."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef p_incbin(p):\n try:\n fname = zxbpp.search_filename(p[2], p.lineno(2), local_first=True)\n if not fname:\n p[0] = None\n return\n with api.utils.open_file(fname, 'rb') as f:\n filecontent = f.read()\n except IOError:\n error(p.lineno(2), \"cannot read file '%s'\" % p[2])\n p[0] = None\n return\n\n p[0] = Asm(p.lineno(1), 'DEFB', filecontent)", "response": "A function to parse INCBIN STRINGs."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_JP_hl(p):\n s = 'JP '\n if p[2] == '(HL)':\n s += p[2]\n else:\n s += '(%s)' % p[3]\n\n p[0] = Asm(p.lineno(1), s)", "response": "Asm parser for JP hl."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef p_BIT(p):\n bit = p[2].eval()\n if bit < 0 or bit > 7:\n error(p.lineno(3), 'Invalid bit position %i. Must be in [0..7]' % bit)\n p[0] = None\n return\n\n p[0] = Asm(p.lineno(3), '%s %i,%s' % (p[1], bit, p[4]))", "response": "asm : bitop expr COMMA A\n | bitop pexpr COMMA A\n | bitop expr COMMA reg8\n | bitop pexpr COMMA reg8\n | bitop expr COMMA reg8_hl\n | bitop pexpr COMMA reg8_hl"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef p_jrjp(p):\n if p[1] in ('JR', 'DJNZ'):\n op = 'N'\n p[2] = Expr.makenode(Container('-', p.lineno(1)), p[2], Expr.makenode(Container(MEMORY.org + 2, p.lineno(1))))\n else:\n op = 'NN'\n\n p[0] = Asm(p.lineno(1), p[1] + ' ' + op, p[2])", "response": "A macro - line parser for the next statement in the JVM."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef p_im(p):\n val = p[2].eval()\n if val not in (0, 1, 2):\n error(p.lineno(1), 'Invalid IM number %i' % val)\n p[0] = None\n return\n\n p[0] = Asm(p.lineno(1), 'IM %i' % val)", "response": "A function to parse the IM expression."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nparsing expr DIV expr", "response": "def p_expr_div_expr(p):\n \"\"\" expr : expr BAND expr\n | expr BOR expr\n | expr BXOR expr\n | expr PLUS expr\n | expr MINUS expr\n | expr MUL expr\n | expr DIV expr\n | expr MOD expr\n | expr POW expr\n | expr LSHIFT expr\n | expr RSHIFT expr\n | pexpr BAND expr\n | pexpr BOR expr\n | pexpr BXOR expr\n | pexpr PLUS expr\n | pexpr MINUS expr\n | pexpr MUL expr\n | pexpr DIV expr\n | pexpr MOD expr\n | pexpr POW expr\n | pexpr LSHIFT expr\n | pexpr RSHIFT expr\n | expr BAND pexpr\n | expr BOR pexpr\n | expr BXOR pexpr\n | expr PLUS pexpr\n | expr MINUS pexpr\n | expr MUL pexpr\n | expr DIV pexpr\n | expr MOD pexpr\n | expr POW pexpr\n | expr LSHIFT pexpr\n | expr RSHIFT pexpr\n | pexpr BAND pexpr\n | pexpr BOR pexpr\n | pexpr BXOR pexpr\n | pexpr PLUS pexpr\n | pexpr MINUS pexpr\n | pexpr MUL pexpr\n | pexpr DIV pexpr\n | pexpr MOD pexpr\n | pexpr POW pexpr\n | pexpr LSHIFT pexpr\n | pexpr RSHIFT pexpr\n \"\"\"\n p[0] = Expr.makenode(Container(p[2], p.lineno(2)), p[1], p[3])"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef p_expr_label(p):\n p[0] = Expr.makenode(Container(MEMORY.get_label(p[1], p.lineno(1)), p.lineno(1)))", "response": "P expr_label - > expr"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef p_preprocessor_line_line_file(p):\n p.lexer.lineno = int(p[2]) + p.lexer.lineno - p.lineno(3) - 1\n gl.FILENAME = p[3]", "response": "preproc_line : _LINE INTEGER STRING"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nassembles input string and leave the result in the", "response": "def assemble(input_):\n \"\"\" Assembles input string, and leave the result in the\n MEMORY global object\n \"\"\"\n global MEMORY\n\n if MEMORY is None:\n MEMORY = Memory()\n\n parser.parse(input_, lexer=LEXER, debug=OPTIONS.Debug.value > 2)\n if len(MEMORY.scopes):\n error(MEMORY.scopes[-1], 'Missing ENDP to close this scope')\n\n return gl.has_errors"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef generate_binary(outputfname, format_, progname='', binary_files=None, headless_binary_files=None):\n global AUTORUN_ADDR\n\n org, binary = MEMORY.dump()\n if gl.has_errors:\n return\n\n if binary_files is None:\n binary_files = []\n\n if headless_binary_files is None:\n headless_binary_files = []\n\n bin_blocks = []\n for fname in binary_files:\n with api.utils.open_file(fname) as f:\n bin_blocks.append((os.path.basename(fname), f.read()))\n\n headless_bin_blocks = []\n for fname in headless_binary_files:\n with api.utils.open_file(fname) as f:\n headless_bin_blocks.append(f.read())\n\n if AUTORUN_ADDR is None:\n AUTORUN_ADDR = org\n\n if not progname:\n progname = os.path.basename(outputfname)[:10]\n\n if OPTIONS.use_loader.value:\n import basic # Minimalist basic tokenizer\n\n program = basic.Basic()\n if org > 16383: # Only for zx48k: CLEAR if below 16383\n program.add_line([['CLEAR', org - 1]])\n program.add_line([['LOAD', '\"\"', program.token('CODE')]])\n\n if OPTIONS.autorun.value:\n program.add_line([['RANDOMIZE', program.token('USR'), AUTORUN_ADDR]])\n else:\n program.add_line([['REM'], ['RANDOMIZE', program.token('USR'), AUTORUN_ADDR]])\n\n if format_ in ('tap', 'tzx'):\n t = {'tap': outfmt.TAP, 'tzx': outfmt.TZX}[format_]()\n\n if OPTIONS.use_loader.value:\n t.save_program('loader', program.bytes, line=1) # Put line 0 to protect against MERGE\n\n t.save_code(progname, org, binary)\n for name, block in bin_blocks:\n t.save_code(name, 0, block)\n for block in headless_bin_blocks:\n t.standard_block(block)\n\n t.dump(outputfname)\n\n else:\n with open(outputfname, 'wb') as f:\n f.write(bytearray(binary))", "response": "Generates the memory binary file for the given format"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the bytes representation of the current asm.", "response": "def bytes(self):\n \"\"\" Returns opcodes\n \"\"\"\n if self.asm not in ('DEFB', 'DEFS', 'DEFW'):\n if self.pending:\n tmp = self.arg # Saves current arg temporarily\n self.arg = tuple([0] * self.arg_num)\n result = super(Asm, self).bytes()\n self.arg = tmp # And recovers it\n\n return result\n\n return super(Asm, self).bytes()\n\n if self.asm == 'DEFB':\n if self.pending:\n return tuple([0] * self.arg_num)\n\n return tuple([x & 0xFF for x in self.argval()])\n\n if self.asm == 'DEFS':\n if self.pending:\n N = self.arg[0]\n if isinstance(N, Expr):\n N = N.eval()\n return tuple([0] * N) # ??\n\n args = self.argval()\n num = args[1] & 0xFF\n return tuple([num] * args[0])\n\n if self.pending: # DEFW\n return tuple([0] * 2 * self.arg_num)\n\n result = ()\n for i in self.argval():\n x = i & 0xFFFF\n result += (x & 0xFF, x >> 8)\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef argval(self):\n if gl.has_errors:\n return [None]\n\n if self.asm in ('DEFB', 'DEFS', 'DEFW'):\n return tuple([x.eval() if isinstance(x, Expr) else x for x in self.arg])\n\n self.arg = tuple([x if not isinstance(x, Expr) else x.eval() for x in self.arg])\n if gl.has_errors:\n return [None]\n\n if self.asm.split(' ')[0] in ('JR', 'DJNZ'): # A relative jump?\n if self.arg[0] < -128 or self.arg[0] > 127:\n error(self.lineno, 'Relative jump out of range')\n return [None]\n\n return super(Asm, self).argval()", "response": "Solve args values or raise errors if not defined yet"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef try_eval(self):\n item = self.symbol.item\n\n if isinstance(item, int):\n return item\n\n if isinstance(item, Label):\n if item.defined:\n if isinstance(item.value, Expr):\n return item.value.try_eval()\n else:\n return item.value\n else:\n if Expr.ignore:\n return None\n\n # Try to resolve into the global namespace\n error(self.symbol.lineno, \"Undefined label '%s'\" % item.name)\n return None\n\n try:\n if isinstance(item, tuple):\n return tuple([x.try_eval() for x in item])\n\n if isinstance(item, list):\n return [x.try_eval() for x in item]\n\n if item == '-' and len(self.children) == 1:\n return -self.left.try_eval()\n\n try:\n return self.funct[item](self.left.try_eval(), self.right.try_eval())\n except ZeroDivisionError:\n error(self.symbol.lineno, 'Division by 0')\n except KeyError:\n pass\n\n except TypeError:\n pass\n\n return None", "response": "Recursively evaluates the node. Returns None if it is still unresolved."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndefine a label value. It can be anything. Even an AST", "response": "def define(self, value, lineno, namespace=None):\n \"\"\" Defines label value. It can be anything. Even an AST\n \"\"\"\n if self.defined:\n error(lineno, \"label '%s' already defined at line %i\" % (self.name, self.lineno))\n\n self.value = value\n self.lineno = lineno\n self.namespace = NAMESPACE if namespace is None else namespace"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef resolve(self, lineno):\n if not self.defined:\n error(lineno, \"Undeclared label '%s'\" % self.name)\n\n if isinstance(self.value, Expr):\n return self.value.eval()\n\n return self.value", "response": "Resolves the label value. Exits with error if value is not defined."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef enter_proc(self, lineno):\n self.local_labels.append({}) # Add a new context\n self.scopes.append(lineno)\n __DEBUG__('Entering scope level %i at line %i' % (len(self.scopes), lineno))", "response": "Adds a new context\n to the list of scope level names and adds a new context\n to the list of scope level names and the current line number."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef set_org(self, value, lineno):\n if value < 0 or value > MAX_MEM:\n error(lineno, \"Memory ORG out of range [0 .. 65535]. Current value: %i\" % value)\n\n self.index = self.ORG = value", "response": "Sets a new ORG value"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nresolves a name and a namespace returns the name as namespace +. + name.", "response": "def id_name(label, namespace=None):\n \"\"\" Given a name and a namespace, resolves\n returns the name as namespace + '.' + name. If namespace\n is none, the current NAMESPACE is used\n \"\"\"\n if not label.startswith(DOT):\n if namespace is None:\n namespace = NAMESPACE\n ex_label = namespace + label # The mangled namespace.labelname label\n else:\n if namespace is None:\n namespace = GLOBAL_NAMESPACE # Global namespace\n ex_label = label\n\n return ex_label, namespace"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef __set_byte(self, byte, lineno):\n if byte < 0 or byte > 255:\n error(lineno, 'Invalid byte value %i' % byte)\n\n self.memory_bytes[self.org] = byte\n self.index += 1", "response": "Sets a byte at the current location and increments org in one."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nexits current procedure. Local labels are transferred to global scope unless they have been marked as local ones. Raises an error if no current local context (stack underflow)", "response": "def exit_proc(self, lineno):\n \"\"\" Exits current procedure. Local labels are transferred to global\n scope unless they have been marked as local ones.\n\n Raises an error if no current local context (stack underflow)\n \"\"\"\n __DEBUG__('Exiting current scope from lineno %i' % lineno)\n\n if len(self.local_labels) <= 1:\n error(lineno, 'ENDP in global scope (with no PROC)')\n return\n\n for label in self.local_labels[-1].values():\n if label.local:\n if not label.defined:\n error(lineno, \"Undefined LOCAL label '%s'\" % label.name)\n return\n continue\n\n name = label.name\n _lineno = label.lineno\n value = label.value\n\n if name not in self.global_labels.keys():\n self.global_labels[name] = label\n else:\n self.global_labels[name].define(value, _lineno)\n\n self.local_labels.pop() # Removes current context\n self.scopes.pop()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_instruction(self, instr):\n if gl.has_errors:\n return\n\n __DEBUG__('%04Xh [%04Xh] ASM: %s' % (self.org, self.org - self.ORG, instr.asm))\n self.set_memory_slot()\n self.orgs[self.org] += (instr,)\n\n for byte in instr.bytes():\n self.__set_byte(byte, instr.lineno)", "response": "This method adds an instruction to the current ASM memory slot and adds the opcode to the memory bytes at the current memory position."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a tuple containing code ORG and a list of OUTPUT ArcGIS memory regions.", "response": "def dump(self):\n \"\"\" Returns a tuple containing code ORG, and a list of OUTPUT\n \"\"\"\n org = min(self.memory_bytes.keys()) # Org is the lowest one\n OUTPUT = []\n align = []\n\n for i in range(org, max(self.memory_bytes.keys()) + 1):\n if gl.has_errors:\n return org, OUTPUT\n\n try:\n try:\n a = [x for x in self.orgs[i] if isinstance(x, Asm)] # search for asm instructions\n\n if not a:\n align.append(0) # Fill with ZEROes not used memory regions\n continue\n\n OUTPUT += align\n align = []\n a = a[0]\n if a.pending:\n a.arg = a.argval()\n a.pending = False\n tmp = a.bytes()\n\n for r in range(len(tmp)):\n self.memory_bytes[i + r] = tmp[r]\n except KeyError:\n pass\n\n OUTPUT.append(self.memory_bytes[i])\n\n except KeyError:\n OUTPUT.append(0) # Fill with ZEROes not used memory regions\n\n return org, OUTPUT"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef declare_label(self, label, lineno, value=None, local=False, namespace=None):\n ex_label, namespace = Memory.id_name(label, namespace)\n\n is_address = value is None\n if value is None:\n value = self.org\n\n if ex_label in self.local_labels[-1].keys():\n self.local_labels[-1][ex_label].define(value, lineno)\n self.local_labels[-1][ex_label].is_address = is_address\n else:\n self.local_labels[-1][ex_label] = Label(ex_label, lineno, value, local, namespace, is_address)\n\n self.set_memory_slot()\n\n return self.local_labels[-1][ex_label]", "response": "Sets a label with the given value or with the current address. Returns the label object."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_label(self, label, lineno):\n global NAMESPACE\n\n ex_label, namespace = Memory.id_name(label)\n\n for i in range(len(self.local_labels) - 1, -1, -1): # Downstep\n result = self.local_labels[i].get(ex_label, None)\n if result is not None:\n return result\n\n result = Label(ex_label, lineno, namespace=namespace)\n self.local_labels[-1][ex_label] = result # HINT: no namespace\n\n return result", "response": "Returns a label in the current context or in the global one."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsets a label lineno and local flag in the current scope .", "response": "def set_label(self, label, lineno, local=False):\n \"\"\" Sets a label, lineno and local flag in the current scope\n (even if it exist in previous scopes). If the label exist in\n the current scope, changes it flags.\n\n The resulting label is returned.\n \"\"\"\n ex_label, namespace = Memory.id_name(label)\n\n if ex_label in self.local_labels[-1].keys():\n result = self.local_labels[-1][ex_label]\n result.lineno = lineno\n else:\n result = self.local_labels[-1][ex_label] = Label(ex_label, lineno, namespace=NAMESPACE)\n\n if result.local == local:\n warning(lineno, \"label '%s' already declared as LOCAL\" % label)\n\n result.local = local\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef memory_map(self):\n return '\\n'.join(sorted(\"%04X: %s\" % (x.value, x.name) for x in self.global_labels.values() if x.is_address))", "response": "Returns a string containing a memory map containing a memory map containing a hex address."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _aload8(ins):\n ''' Loads an 8 bit value from a memory address\n If 2nd arg. start with '*', it is always treated as\n an indirect value.\n '''\n output = _addr(ins.quad[2])\n output.append('ld a, (hl)')\n output.append('push af')\n\n return output", "response": "Load an 8 bit value from a memory address\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nloading a 16 bit value from a memory address", "response": "def _aload16(ins):\n ''' Loads a 16 bit value from a memory address\n If 2nd arg. start with '*', it is always treated as\n an indirect value.\n '''\n output = _addr(ins.quad[2])\n\n output.append('ld e, (hl)')\n output.append('inc hl')\n output.append('ld d, (hl)')\n output.append('ex de, hl')\n output.append('push hl')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nloads a 32 bit value from a memory address.", "response": "def _aload32(ins):\n ''' Load a 32 bit value from a memory address\n If 2nd arg. start with '*', it is always treated as\n an indirect value.\n '''\n output = _addr(ins.quad[2])\n\n output.append('call __ILOAD32')\n output.append('push de')\n output.append('push hl')\n\n REQUIRES.add('iload32.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nloading a floating point value from a memory address.", "response": "def _aloadf(ins):\n ''' Loads a floating point value from a memory address.\n If 2nd arg. start with '*', it is always treated as\n an indirect value.\n '''\n output = _addr(ins.quad[2])\n output.append('call __LOADF')\n output.extend(_fpush())\n\n REQUIRES.add('iloadf.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nload a string value from a memory address.", "response": "def _aloadstr(ins):\n ''' Loads a string value from a memory address.\n '''\n output = _addr(ins.quad[2])\n\n output.append('call __ILOADSTR')\n output.append('push hl')\n REQUIRES.add('loadstr.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nstores 2\u00ba operand content into address of 1st operand.", "response": "def _astore8(ins):\n ''' Stores 2\u00ba operand content into address of 1st operand.\n 1st operand is an array element. Dimensions are pushed into the\n stack.\n Use '*' for indirect store on 1st operand (A pointer to an array)\n '''\n output = _addr(ins.quad[1])\n op = ins.quad[2]\n\n indirect = op[0] == '*'\n if indirect:\n op = op[1:]\n\n immediate = op[0] == '#'\n if immediate:\n op = op[1:]\n\n if is_int(op):\n if indirect:\n if immediate:\n op = str(int(op) & 0xFFFF) # Truncate to 16bit pointer\n output.append('ld a, (%s)' % op)\n else:\n output.append('ld de, (%s)' % op)\n output.append('ld a, (de)')\n else:\n op = str(int(op) & 0xFF) # Truncate to byte\n output.append('ld (hl), %s' % op)\n return output\n\n elif op[0] == '_':\n if indirect:\n if immediate:\n output.append('ld a, (%s)' % op) # Redundant: *#_id == _id\n else:\n output.append('ld de, (%s)' % op) # *_id\n output.append('ld a, (de)')\n else:\n if immediate:\n output.append('ld a, %s' % op) # #_id\n else:\n output.append('ld a, (%s)' % op) # _id\n else:\n output.append('pop af') # tn\n\n output.append('ld (hl), a')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _astore16(ins):\n ''' Stores 2\u00ba operand content into address of 1st operand.\n store16 a, x => *(&a) = x\n Use '*' for indirect store on 1st operand.\n '''\n output = _addr(ins.quad[1])\n op = ins.quad[2]\n\n indirect = op[0] == '*'\n if indirect:\n op = op[1:]\n\n immediate = op[0] == '#'\n if immediate:\n op = op[1:]\n\n if is_int(op):\n op = str(int(op) & 0xFFFF) # Truncate to 16bit pointer\n\n if indirect:\n if immediate:\n output.append('ld de, (%s)' % op)\n else:\n output.append('ld de, (%s)' % op)\n output.append('call __LOAD_DE_DE')\n REQUIRES.add('lddede.asm')\n else:\n H = int(op) >> 8\n L = int(op) & 0xFF\n output.append('ld (hl), %i' % L)\n output.append('inc hl')\n output.append('ld (hl), %i' % H)\n return output\n\n elif op[0] == '_':\n if indirect:\n if immediate:\n output.append('ld de, (%s)' % op) # redundant: *#_id == _id\n else:\n output.append('ld de, (%s)' % op) # *_id\n output.append('call __LOAD_DE_DE')\n REQUIRES.add('lddede.asm')\n else:\n if immediate:\n output.append('ld de, %s' % op)\n else:\n output.append('ld de, (%s)' % op)\n else:\n output.append('pop de')\n\n output.append('ld (hl), e')\n output.append('inc hl')\n output.append('ld (hl), d')\n\n return output", "response": "Stores 2\u00ba operand content into address of 1st operand."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nstore 2\u00ba operand content into address of 1st operand.", "response": "def _astore32(ins):\n ''' Stores 2\u00ba operand content into address of 1st operand.\n store16 a, x => *(&a) = x\n '''\n output = _addr(ins.quad[1])\n\n value = ins.quad[2]\n if value[0] == '*':\n value = value[1:]\n indirect = True\n else:\n indirect = False\n\n try:\n value = int(ins.quad[2]) & 0xFFFFFFFF # Immediate?\n if indirect:\n output.append('push hl')\n output.append('ld hl, %i' % (value & 0xFFFF))\n output.append('call __ILOAD32')\n output.append('ld b, h')\n output.append('ld c, l') # BC = Lower 16 bits\n output.append('pop hl')\n REQUIRES.add('iload32.asm')\n else:\n output.append('ld de, %i' % (value >> 16))\n output.append('ld bc, %i' % (value & 0xFFFF))\n except ValueError:\n output.append('pop bc')\n output.append('pop de')\n\n output.append('call __STORE32')\n REQUIRES.add('store32.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _astoref16(ins):\n ''' Stores 2\u00ba operand content into address of 1st operand.\n storef16 a, x => *(&a) = x\n '''\n output = _addr(ins.quad[1])\n\n value = ins.quad[2]\n if value[0] == '*':\n value = value[1:]\n indirect = True\n else:\n indirect = False\n\n if indirect:\n output.append('push hl')\n output.extend(_f16_oper(ins.quad[2], useBC=True))\n output.append('pop hl')\n REQUIRES.add('iload32.asm')\n else:\n output.extend(_f16_oper(ins.quad[2], useBC=True))\n\n output.append('call __STORE32')\n REQUIRES.add('store32.asm')\n\n return output", "response": "Stores 2\u00ba operand content into address of 1st operand."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _astoref(ins):\n ''' Stores a floating point value into a memory address.\n '''\n output = _addr(ins.quad[1])\n\n value = ins.quad[2]\n if value[0] == '*':\n value = value[1:]\n indirect = True\n else:\n indirect = False\n\n if indirect:\n output.append('push hl')\n output.extend(_float_oper(ins.quad[2]))\n output.append('pop hl')\n else:\n output.extend(_float_oper(ins.quad[2]))\n\n output.append('call __STOREF')\n REQUIRES.add('storef.asm')\n\n return output", "response": "Stores a floating point value into a memory address."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _astorestr(ins):\n ''' Stores a string value into a memory address.\n It copies content of 2nd operand (string), into 1st, reallocating\n dynamic memory for the 1st str. These instruction DOES ALLOW\n immediate strings for the 2nd parameter, starting with '#'.\n '''\n output = _addr(ins.quad[1])\n op = ins.quad[2]\n\n indirect = op[0] == '*'\n if indirect:\n op = op[1:]\n\n immediate = op[0] == '#'\n if immediate:\n op = op[1:]\n\n temporal = op[0] != '$'\n if not temporal:\n op = op[1:]\n\n if is_int(op):\n op = str(int(op) & 0xFFFF)\n if indirect:\n if immediate: # *# = ld hl, (number)\n output.append('ld de, (%s)' % op)\n else:\n output.append('ld de, (%s)' % op)\n output.append('call __LOAD_DE_DE')\n REQUIRES.add('lddede.asm')\n else:\n # Integer does not make sense here (unless it's a ptr)\n raise InvalidICError(str(ins))\n\n output.append('ld de, (%s)' % op)\n elif op[0] == '_': # an identifier\n temporal = False # Global var is not a temporary string\n\n if indirect:\n if immediate: # *#_id = _id\n output.append('ld de, (%s)' % op)\n else: # *_id\n output.append('ld de, (%s)' % op)\n output.append('call __LOAD_DE_DE')\n REQUIRES.add('lddede.asm')\n else:\n if immediate:\n output.append('ld de, %s' % op)\n else:\n output.append('ld de, (%s)' % op)\n else: # tn\n output.append('pop de')\n\n if indirect:\n output.append('call __LOAD_DE_DE')\n REQUIRES.add('lddede.asm')\n\n if not temporal:\n output.append('call __STORE_STR')\n REQUIRES.add('storestr.asm')\n else: # A value already on dynamic memory\n output.append('call __STORE_STR2')\n REQUIRES.add('storestr2.asm')\n\n return output", "response": "Stores a string value into a memory address."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a pop sequence for 32 bit operands.", "response": "def _32bit_oper(op1, op2=None, reversed=False, preserveHL=False):\n \"\"\" Returns pop sequence for 32 bits operands\n 1st operand in HLDE, 2nd operand remains in the stack\n\n Now it does support operands inversion calling __SWAP32.\n\n However, if 1st operand is integer (immediate) or indirect, the stack\n will be rearranged, so it contains a 32 bit pushed parameter value for the\n subroutine to be called.\n\n If preserveHL is True, then BC will be used instead of HL for lower part\n for the 1st operand.\n \"\"\"\n output = []\n\n if op1 is not None:\n op1 = str(op1)\n\n if op2 is not None:\n op2 = str(op2)\n\n op = op2 if op2 is not None else op1\n\n int1 = False # whether op1 (2nd operand) is integer\n\n indirect = (op[0] == '*')\n if indirect:\n op = op[1:]\n\n immediate = (op[0] == '#')\n if immediate:\n op = op[1:]\n\n hl = 'hl' if not preserveHL and not indirect else 'bc'\n\n if is_int(op):\n int1 = True\n op = int(op)\n\n if indirect:\n if immediate:\n output.append('ld hl, %i' % op)\n else:\n output.append('ld hl, (%i)' % op)\n\n output.append('call __ILOAD32')\n REQUIRES.add('iload32.asm')\n\n if preserveHL:\n output.append('ld b, h')\n output.append('ld c, l')\n else:\n DE, HL = int32(op)\n output.append('ld de, %i' % DE)\n output.append('ld %s, %i' % (hl, HL))\n else:\n if op[0] == '_':\n if immediate:\n output.append('ld %s, %s' % (hl, op))\n else:\n output.append('ld %s, (%s)' % (hl, op))\n else:\n if immediate:\n output.append('ld %s, (%s) & 0xFFFF' % (hl, op))\n else:\n output.append('pop %s' % hl)\n\n if indirect:\n output.append('call __ILOAD32')\n REQUIRES.add('iload32.asm')\n\n if preserveHL:\n output.append('ld b, h')\n output.append('ld c, l')\n else:\n if op[0] == '_':\n output.append('ld de, (%s + 2)' % op)\n else:\n if immediate:\n output.append('ld de, (%s) >> 16' % op)\n else:\n output.append('pop de')\n\n if op2 is not None:\n op = op1\n\n indirect = (op[0] == '*')\n if indirect:\n op = op[1:]\n\n immediate = (op[0] == '#')\n if immediate:\n op = op[1:]\n\n if is_int(op):\n op = int(op)\n\n if indirect:\n output.append('exx')\n if immediate:\n output.append('ld hl, %i' % (op & 0xFFFF))\n else:\n output.append('ld hl, (%i)' % (op & 0xFFFF))\n\n output.append('call __ILOAD32')\n output.append('push de')\n output.append('push hl')\n output.append('exx')\n REQUIRES.add('iload32.asm')\n else:\n DE, HL = int32(op)\n output.append('ld bc, %i' % DE)\n output.append('push bc')\n output.append('ld bc, %i' % HL)\n output.append('push bc')\n else:\n if indirect:\n output.append('exx') # uses alternate set to put it on the stack\n if op[0] == '_':\n if immediate:\n output.append('ld hl, %s' % op)\n else:\n output.append('ld hl, (%s)' % op)\n else:\n output.append('pop hl') # Pointers are only 16 bits ***\n\n output.append('call __ILOAD32')\n output.append('push de')\n output.append('push hl')\n output.append('exx')\n REQUIRES.add('iload32.asm')\n elif immediate:\n output.append('ld bc, (%s) >> 16' % op)\n output.append('push bc')\n output.append('ld bc, (%s) & 0xFFFF' % op)\n output.append('push bc')\n elif op[0] == '_': # an address\n if int1 or op1[0] == '_': # If previous op was integer, we can use hl in advance\n tmp = output\n output = []\n output.append('ld hl, (%s + 2)' % op)\n output.append('push hl')\n output.append('ld hl, (%s)' % op)\n output.append('push hl')\n output.extend(tmp)\n else:\n output.append('ld bc, (%s + 2)' % op)\n output.append('push bc')\n output.append('ld bc, (%s)' % op)\n output.append('push bc')\n else:\n pass # 2nd operand remains in the stack\n\n if op2 is not None and reversed:\n output.append('call __SWAP32')\n REQUIRES.add('swap32.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nadd a 32 - bit integer to the stack and returns the result.", "response": "def _add32(ins):\n \"\"\" Pops last 2 bytes from the stack and adds them.\n Then push the result onto the stack.\n\n Optimizations:\n * If any of the operands is ZERO,\n then do NOTHING: A + 0 = 0 + A = A\n \"\"\"\n op1, op2 = tuple(ins.quad[2:])\n\n if _int_ops(op1, op2) is not None:\n o1, o2 = _int_ops(op1, op2)\n\n if int(o2) == 0: # A + 0 = 0 + A = A => Do Nothing\n output = _32bit_oper(o1)\n output.append('push de')\n output.append('push hl')\n return output\n\n if op1[0] == '_' and op2[0] != '_':\n op1, op2 = op2, op1 # swap them\n\n if op2[0] == '_':\n output = _32bit_oper(op1)\n output.append('ld bc, (%s)' % op2)\n output.append('add hl, bc')\n output.append('ex de, hl')\n output.append('ld bc, (%s + 2)' % op2)\n output.append('adc hl, bc')\n output.append('push hl')\n output.append('push de')\n return output\n\n output = _32bit_oper(op1, op2)\n output.append('pop bc')\n output.append('add hl, bc')\n output.append('ex de, hl')\n output.append('pop bc')\n output.append('adc hl, bc')\n output.append('push hl') # High and low parts are reversed\n output.append('push de')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _sub32(ins):\n op1, op2 = tuple(ins.quad[2:])\n\n if is_int(op2):\n if int(op2) == 0: # A - 0 = A => Do Nothing\n output = _32bit_oper(op1)\n output.append('push de')\n output.append('push hl')\n return output\n\n rev = op1[0] != 't' and not is_int(op1) and op2[0] == 't'\n\n output = _32bit_oper(op1, op2, rev)\n output.append('call __SUB32')\n output.append('push de')\n output.append('push hl')\n REQUIRES.add('sub32.asm')\n return output", "response": "Sub32 implementation of the 32 - bit sub - function."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nmultiplying two last 32bit values on top of the stack and returns the value on top of the stack and .", "response": "def _mul32(ins):\n \"\"\" Multiplies two last 32bit values on top of the stack and\n and returns the value on top of the stack\n\n Optimizations done:\n\n * If any operand is 1, do nothing\n * If any operand is 0, push 0\n \"\"\"\n op1, op2 = tuple(ins.quad[2:])\n if _int_ops(op1, op2):\n op1, op2 = _int_ops(op1, op2)\n output = _32bit_oper(op1)\n\n if op2 == 1:\n output.append('push de')\n output.append('push hl')\n return output # A * 1 = Nothing\n\n if op2 == 0:\n output.append('ld hl, 0')\n output.append('push hl')\n output.append('push hl')\n return output\n\n output = _32bit_oper(op1, op2)\n output.append('call __MUL32') # Inmmediate\n output.append('push de')\n output.append('push hl')\n REQUIRES.add('mul32.asm')\n return output"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncompare top 2 operands and returns a 32 bit unsigned version of the top 2 operands.", "response": "def _ltu32(ins):\n \"\"\" Compares & pops top 2 operands out of the stack, and checks\n if the 1st operand < 2nd operand (top of the stack).\n Pushes 0 if False, 1 if True.\n\n 32 bit unsigned version\n \"\"\"\n op1, op2 = tuple(ins.quad[2:])\n rev = op1[0] != 't' and not is_int(op1) and op2[0] == 't'\n output = _32bit_oper(op1, op2, rev)\n output.append('call __SUB32')\n output.append('sbc a, a')\n output.append('push af')\n REQUIRES.add('sub32.asm')\n return output"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncompare top 2 operands and returns a 32 bit unsigned version of the tree.", "response": "def _gtu32(ins):\n \"\"\" Compares & pops top 2 operands out of the stack, and checks\n if the 1st operand > 2nd operand (top of the stack).\n Pushes 0 if False, 1 if True.\n\n 32 bit unsigned version\n \"\"\"\n op1, op2 = tuple(ins.quad[2:])\n rev = op1[0] != 't' and not is_int(op1) and op2[0] == 't'\n output = _32bit_oper(op1, op2, rev)\n output.append('pop bc')\n output.append('or a')\n output.append('sbc hl, bc')\n output.append('ex de, hl')\n output.append('pop de')\n output.append('sbc hl, de')\n output.append('sbc a, a')\n output.append('push af')\n return output"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncompare top 2 operands and returns a 32 bit un - signed version of the stack.", "response": "def _eq32(ins):\n \"\"\" Compares & pops top 2 operands out of the stack, and checks\n if the 1st operand == 2nd operand (top of the stack).\n Pushes 0 if False, 1 if True.\n\n 32 bit un/signed version\n \"\"\"\n op1, op2 = tuple(ins.quad[2:])\n output = _32bit_oper(op1, op2)\n output.append('call __EQ32')\n output.append('push af')\n REQUIRES.add('eq32.asm')\n return output"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _and32(ins):\n op1, op2 = tuple(ins.quad[2:])\n\n if _int_ops(op1, op2):\n op1, op2 = _int_ops(op1, op2)\n\n if op2 == 0: # X and False = False\n if str(op1)[0] == 't': # a temporary term (stack)\n output = _32bit_oper(op1) # Remove op1 from the stack\n else:\n output = []\n output.append('xor a')\n output.append('push af')\n return output\n\n # For X and TRUE = X we do nothing as we have to convert it to boolean\n # which is a rather expensive instruction\n\n output = _32bit_oper(op1, op2)\n output.append('call __AND32')\n output.append('push af')\n REQUIRES.add('and32.asm')\n return output", "response": "Compares and pops top 2 operands out of the stack and checks\n ."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _not32(ins):\n output = _32bit_oper(ins.quad[2])\n output.append('call __NOT32')\n output.append('push af')\n REQUIRES.add('not32.asm')\n return output", "response": "Negates top ( Logical NOT ) of the stack"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _bnot32(ins):\n output = _32bit_oper(ins.quad[2])\n output.append('call __BNOT32')\n output.append('push de')\n output.append('push hl')\n REQUIRES.add('bnot32.asm')\n return output", "response": "Negates top ( 32 bits in DEHL and push hl"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nnegate top of the stack.", "response": "def _neg32(ins):\n \"\"\" Negates top of the stack (32 bits in DEHL)\n \"\"\"\n output = _32bit_oper(ins.quad[2])\n output.append('call __NEG32')\n output.append('push de')\n output.append('push hl')\n REQUIRES.add('neg32.asm')\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _abs32(ins):\n output = _32bit_oper(ins.quad[2])\n output.append('call __ABS32')\n output.append('push de')\n output.append('push hl')\n REQUIRES.add('abs32.asm')\n return output", "response": "Returns the absolute value of top of the stack."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _shl32(ins):\n op1, op2 = tuple(ins.quad[2:])\n\n if is_int(op2):\n output = _32bit_oper(op1)\n\n if int(op2) == 0:\n output.append('push de')\n output.append('push hl')\n return output\n\n if int(op2) > 1:\n label = tmp_label()\n output.append('ld b, %s' % op2)\n output.append('%s:' % label)\n output.append('call __SHL32')\n output.append('djnz %s' % label)\n else:\n output.append('call __SHL32')\n\n output.append('push de')\n output.append('push hl')\n REQUIRES.add('shl32.asm')\n return output\n\n output = _8bit_oper(op2)\n output.append('ld b, a')\n output.extend(_32bit_oper(op1))\n label = tmp_label()\n output.append('%s:' % label)\n output.append('call __SHL32')\n output.append('djnz %s' % label)\n output.append('push de')\n output.append('push hl')\n REQUIRES.add('shl32.asm')\n return output", "response": "Logical Left shift 32bit unsigned integers."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _float_oper(op1, op2=None):\n ''' Returns pop sequence for floating point operands\n 1st operand in A DE BC, 2nd operand remains in the stack\n\n Unlike 8bit and 16bit version, this does not supports\n operands inversion. Since many of the instructions are implemented\n as functions, they must support this.\n\n However, if 1st operand is a number (immediate) or indirect, the stack\n will be rearranged, so it contains a 48 bit pushed parameter value for the\n subroutine to be called.\n '''\n output = []\n op = op2 if op2 is not None else op1\n\n indirect = (op[0] == '*')\n if indirect:\n op = op[1:]\n\n if is_float(op):\n op = float(op)\n\n if indirect:\n op = int(op) & 0xFFFF\n output.append('ld hl, (%i)' % op)\n output.append('call __ILOADF')\n REQUIRES.add('iloadf.asm')\n else:\n A, DE, BC = _float(op)\n output.append('ld a, %s' % A)\n output.append('ld de, %s' % DE)\n output.append('ld bc, %s' % BC)\n else:\n if indirect:\n if op[0] == '_':\n output.append('ld hl, (%s)' % op)\n else:\n output.append('pop hl')\n\n output.append('call __ILOADF')\n REQUIRES.add('iloadf.asm')\n else:\n if op[0] == '_':\n output.append('ld a, (%s)' % op)\n output.append('ld de, (%s + 1)' % op)\n output.append('ld bc, (%s + 3)' % op)\n else:\n output.extend(_fpop())\n\n if op2 is not None:\n op = op1\n if is_float(op): # An float must be in the stack. Let's pushit\n A, DE, BC = _float(op)\n output.append('ld hl, %s' % BC)\n output.append('push hl')\n output.append('ld hl, %s' % DE)\n output.append('push hl')\n output.append('ld h, %s' % A)\n output.append('push hl')\n elif op[0] == '*': # Indirect\n op = op[1:]\n output.append('exx') # uses alternate set to put it on the stack\n output.append(\"ex af, af'\")\n if is_int(op): # noqa TODO: it will fail\n op = int(op)\n output.append('ld hl, %i' % op)\n elif op[0] == '_':\n output.append('ld hl, (%s)' % op)\n else:\n output.append('pop hl')\n\n output.append('call __ILOADF')\n output.extend(_fpush())\n output.append(\"ex af, af'\")\n output.append('exx')\n REQUIRES.add('iloadf.asm')\n elif op[0] == '_':\n if is_float(op2):\n tmp = output\n output = []\n output.append('ld hl, %s + 4' % op)\n '''\n output.append('ld hl, (%s + 3)' % op)\n output.append('push hl')\n output.append('ld hl, (%s + 1)' % op)\n output.append('push hl')\n output.append('ld a, (%s)' % op)\n output.append('push af')\n '''\n output.append('call __FP_PUSH_REV')\n output.extend(tmp)\n REQUIRES.add('pushf.asm')\n else:\n '''\n output.append('ld hl, (%s + 3)' % op)\n output.append('push hl')\n output.append('ld hl, (%s + 1)' % op)\n output.append('push hl')\n output.append('ld hl, (%s - 1)' % op)\n output.append('push hl')\n '''\n output.append('ld hl, %s + 4' % op)\n output.append('call __FP_PUSH_REV')\n REQUIRES.add('pushf.asm')\n else:\n pass # Else do nothing, and leave the op onto the stack\n\n return output", "response": "Returns pop sequence for floating point operands in A DE BC 2nd operand remains in the stack."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nadds 2 float values. The result is pushed onto the stack.", "response": "def _addf(ins):\n ''' Adds 2 float values. The result is pushed onto the stack.\n '''\n op1, op2 = tuple(ins.quad[2:])\n\n if _f_ops(op1, op2) is not None:\n opa, opb = _f_ops(op1, op2)\n if opb == 0: # A + 0 => A\n output = _float_oper(opa)\n output.extend(_fpush())\n return output\n\n output = _float_oper(op1, op2)\n output.append('call __ADDF')\n output.extend(_fpush())\n REQUIRES.add('addf.asm')\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _divf(ins):\n ''' Divides 2 float values. The result is pushed onto the stack.\n '''\n op1, op2 = tuple(ins.quad[2:])\n\n if is_float(op2) and float(op2) == 1: # Nothing to do. A / 1 = A\n output = _float_oper(op1)\n output.extend(_fpush())\n return output\n\n output = _float_oper(op1, op2)\n output.append('call __DIVF')\n output.extend(_fpush())\n REQUIRES.add('divf.asm')\n return output", "response": "Divides 2 float values. The result is pushed onto the stack."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _modf(ins):\n ''' Reminder of div. 2 float values. The result is pushed onto the stack.\n '''\n op1, op2 = tuple(ins.quad[2:])\n output = _float_oper(op1, op2)\n output.append('call __MODF')\n output.extend(_fpush())\n REQUIRES.add('modf.asm')\n return output", "response": "Reminder of div. 2 float values. The result is pushed onto the stack."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _ltf(ins):\n ''' Compares & pops top 2 operands out of the stack, and checks\n if the 1st operand < 2nd operand (top of the stack).\n Pushes 0 if False, 1 if True.\n\n Floating Point version\n '''\n op1, op2 = tuple(ins.quad[2:])\n output = _float_oper(op1, op2)\n output.append('call __LTF')\n output.append('push af')\n REQUIRES.add('ltf.asm')\n return output", "response": "Compares top 2 operands out of the stack and checks\n if the 1st operand < 2nd operand."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nnegate top of the stack ( 48 bits )", "response": "def _notf(ins):\n ''' Negates top of the stack (48 bits)\n '''\n output = _float_oper(ins.quad[2])\n output.append('call __NOTF')\n output.append('push af')\n REQUIRES.add('notf.asm')\n return output"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nchange sign of top of the stack ( 48 bits )", "response": "def _negf(ins):\n ''' Changes sign of top of the stack (48 bits)\n '''\n output = _float_oper(ins.quad[2])\n output.append('call __NEGF')\n output.extend(_fpush())\n REQUIRES.add('negf.asm')\n return output"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the absolute value of the top of the stack.", "response": "def _absf(ins):\n ''' Absolute value of top of the stack (48 bits)\n '''\n output = _float_oper(ins.quad[2])\n output.append('res 7, e') # Just resets the sign bit!\n output.extend(_fpush())\n return output"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_uniques(l):\n result = []\n\n for i in l:\n if i not in result:\n result.append(i)\n\n return result", "response": "Returns a list with no repeated elements."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef t_CHAR(self, t):\n r\"'.'\" # A single char\n\n t.value = ord(t.value[1])\n t.type = 'INTEGER'\n return t", "response": "r \"'.. \"' r'."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef t_BIN(self, t):\n r'(%[01]+)|([01]+[bB])' # A Binary integer\n # Note 00B is a 0 binary, but\n # 00Bh is a 12 in hex. So this pattern must come\n # after HEXA\n\n if t.value[0] == '%':\n t.value = t.value[1:] # Remove initial %\n else:\n t.value = t.value[:-1] # Remove last 'b'\n\n t.value = int(t.value, 2) # Convert to decimal\n t.type = 'INTEGER'\n return t", "response": "A Binary integer t"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nprocesses preprocessor ID directives.", "response": "def t_preproc_ID(self, t):\n r'[_a-zA-Z][_a-zA-Z0-9]*' # preprocessor directives\n t.type = preprocessor.get(t.value.lower(), 'ID')\n return t"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef t_INITIAL_SHARP(self, t):\n r'\\#'\n\n if self.find_column(t) == 1:\n t.lexer.begin('preproc')\n else:\n self.t_INITIAL_preproc_error(t)", "response": "r \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S + \\ S +"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndefining input string removing current lexer.", "response": "def input(self, str):\n \"\"\" Defines input string, removing current lexer.\n \"\"\"\n self.input_data = str\n self.lex = lex.lex(object=self)\n self.lex.input(self.input_data)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef typecast(self, type_):\n self.value = SymbolTYPECAST.make_node(type_, self.value, self.lineno)\n return self.value is not None", "response": "Test typecast to the argument expression."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _paddr(offset):\n ''' Generic array address parameter loading.\n Emmits output code for setting IX at the right location.\n bytes = Number of bytes to load:\n 1 => 8 bit value\n 2 => 16 bit value / string\n 4 => 32 bit value / f16 value\n 5 => 40 bit value\n '''\n output = []\n\n indirect = offset[0] == '*'\n if indirect:\n offset = offset[1:]\n\n I = int(offset)\n if I >= 0:\n I += 4 # Return Address + \"push IX\"\n\n output.append('push ix')\n output.append('pop hl')\n output.append('ld de, %i' % I)\n output.append('add hl, de')\n\n if indirect:\n output.append('ld c, (hl)')\n output.append('inc hl')\n output.append('ld h, (hl)')\n output.append('ld l, c')\n\n output.append('call __ARRAY')\n REQUIRES.add('array.asm')\n return output", "response": "Generic array address parameter loading."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nload an 8 bit value from a memory address.", "response": "def _paload8(ins):\n ''' Loads an 8 bit value from a memory address\n If 2nd arg. start with '*', it is always treated as\n an indirect value.\n '''\n output = _paddr(ins.quad[2])\n output.append('ld a, (hl)')\n output.append('push af')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _paload16(ins):\n ''' Loads a 16 bit value from a memory address\n If 2nd arg. start with '*', it is always treated as\n an indirect value.\n '''\n output = _paddr(ins.quad[2])\n\n output.append('ld e, (hl)')\n output.append('inc hl')\n output.append('ld d, (hl)')\n output.append('ex de, hl')\n output.append('push hl')\n\n return output", "response": "Load a 16 bit value from a memory address\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _paload32(ins):\n ''' Load a 32 bit value from a memory address\n If 2nd arg. start with '*', it is always treated as\n an indirect value.\n '''\n output = _paddr(ins.quad[2])\n\n output.append('call __ILOAD32')\n output.append('push de')\n output.append('push hl')\n\n REQUIRES.add('iload32.asm')\n\n return output", "response": "Load a 32 bit value from a memory address."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _paloadf(ins):\n ''' Loads a floating point value from a memory address.\n If 2nd arg. start with '*', it is always treated as\n an indirect value.\n '''\n output = _paddr(ins.quad[2])\n output.append('call __ILOADF')\n output.extend(_fpush())\n\n REQUIRES.add('iloadf.asm')\n\n return output", "response": "Load a floating point value from a memory address."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nload a string value from a memory address.", "response": "def _paloadstr(ins):\n ''' Loads a string value from a memory address.\n '''\n output = _paddr(ins.quad[2])\n\n output.append('call __ILOADSTR')\n output.append('push hl')\n REQUIRES.add('loadstr.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _pastore8(ins):\n ''' Stores 2\u00ba operand content into address of 1st operand.\n 1st operand is an array element. Dimensions are pushed into the\n stack.\n Use '*' for indirect store on 1st operand (A pointer to an array)\n '''\n output = _paddr(ins.quad[1])\n\n value = ins.quad[2]\n if value[0] == '*':\n value = value[1:]\n indirect = True\n else:\n indirect = False\n\n try:\n value = int(ins.quad[2]) & 0xFFFF\n if indirect:\n output.append('ld a, (%i)' % value)\n output.append('ld (hl), a')\n else:\n value &= 0xFF\n output.append('ld (hl), %i' % value)\n except ValueError:\n output.append('pop af')\n output.append('ld (hl), a')\n\n return output", "response": "Stores 2\u00ba operand content into address of 1st operand."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nstore 2\u00ba operand content into address of 1st operand.", "response": "def _pastore16(ins):\n ''' Stores 2\u00ba operand content into address of 1st operand.\n store16 a, x => *(&a) = x\n Use '*' for indirect store on 1st operand.\n '''\n output = _paddr(ins.quad[1])\n\n value = ins.quad[2]\n if value[0] == '*':\n value = value[1:]\n indirect = True\n else:\n indirect = False\n\n try:\n value = int(ins.quad[2]) & 0xFFFF\n output.append('ld de, %i' % value)\n if indirect:\n output.append('call __LOAD_DE_DE')\n REQUIRES.add('lddede.asm')\n\n except ValueError:\n output.append('pop de')\n\n output.append('ld (hl), e')\n output.append('inc hl')\n output.append('ld (hl), d')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _pastore32(ins):\n ''' Stores 2\u00ba operand content into address of 1st operand.\n store16 a, x => *(&a) = x\n '''\n output = _paddr(ins.quad[1])\n\n value = ins.quad[2]\n if value[0] == '*':\n value = value[1:]\n indirect = True\n else:\n indirect = False\n\n try:\n value = int(ins.quad[2]) & 0xFFFFFFFF # Immediate?\n if indirect:\n output.append('push hl')\n output.append('ld hl, %i' % (value & 0xFFFF))\n output.append('call __ILOAD32')\n output.append('ld b, h')\n output.append('ld c, l') # BC = Lower 16 bits\n output.append('pop hl')\n REQUIRES.add('iload32.asm')\n else:\n output.append('ld de, %i' % (value >> 16))\n output.append('ld bc, %i' % (value & 0xFFFF))\n except ValueError:\n output.append('pop bc')\n output.append('pop de')\n\n output.append('call __STORE32')\n REQUIRES.add('store32.asm')\n\n return output", "response": "Stores 2\u00ba operand content into address of 1st operand."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nstore 2\u00ba operand content into address of 1st operand.", "response": "def _pastoref16(ins):\n ''' Stores 2\u00ba operand content into address of 1st operand.\n storef16 a, x => *(&a) = x\n '''\n output = _paddr(ins.quad[1])\n\n value = ins.quad[2]\n if value[0] == '*':\n value = value[1:]\n indirect = True\n else:\n indirect = False\n\n try:\n if indirect:\n value = int(ins.quad[2])\n output.append('push hl')\n output.append('ld hl, %i' % (value & 0xFFFF))\n output.append('call __ILOAD32')\n output.append('ld b, h')\n output.append('ld c, l') # BC = Lower 16 bits\n output.append('pop hl')\n REQUIRES.add('iload32.asm')\n else:\n de, hl = f16(value)\n output.append('ld de, %i' % de)\n output.append('ld bc, %i' % hl)\n except ValueError:\n output.append('pop bc')\n output.append('pop de')\n\n output.append('call __STORE32')\n REQUIRES.add('store32.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _pastoref(ins):\n ''' Stores a floating point value into a memory address.\n '''\n output = _paddr(ins.quad[1])\n\n value = ins.quad[2]\n if value[0] == '*':\n value = value[1:]\n indirect = True\n else:\n indirect = False\n\n try:\n if indirect:\n value = int(value) & 0xFFFF # Inmediate?\n output.append('push hl')\n output.append('ld hl, %i' % value)\n output.append('call __ILOADF')\n output.append('ld a, c')\n output.append('ld b, h')\n output.append('ld c, l') # BC = Lower 16 bits, A = Exp\n output.append('pop hl') # Recovers pointer\n REQUIRES.add('iloadf.asm')\n else:\n value = float(value) # Inmediate?\n C, DE, HL = fp.immediate_float(value) # noqa TODO: it will fail\n output.append('ld a, %s' % C)\n output.append('ld de, %s' % DE)\n output.append('ld bc, %s' % HL)\n except ValueError:\n output.append('pop bc')\n output.append('pop de')\n output.append('ex (sp), hl') # Preserve HL for STOREF\n output.append('ld a, l')\n output.append('pop hl')\n\n output.append('call __STOREF')\n REQUIRES.add('storef.asm')\n\n return output", "response": "Stores a floating point value into a memory address."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nstoring a string value into a memory address.", "response": "def _pastorestr(ins):\n ''' Stores a string value into a memory address.\n It copies content of 2nd operand (string), into 1st, reallocating\n dynamic memory for the 1st str. These instruction DOES ALLOW\n inmediate strings for the 2nd parameter, starting with '#'.\n '''\n output = _paddr(ins.quad[1])\n temporal = False\n value = ins.quad[2]\n\n indirect = value[0] == '*'\n if indirect:\n value = value[1:]\n\n immediate = value[0]\n if immediate:\n value = value[1:]\n\n if value[0] == '_':\n if indirect:\n if immediate:\n output.append('ld de, (%s)' % value)\n else:\n output.append('ld de, (%s)' % value)\n output.append('call __LOAD_DE_DE')\n REQUIRES.add('lddede.asm')\n else:\n if immediate:\n output.append('ld de, %s' % value)\n else:\n output.append('ld de, (%s)' % value)\n else:\n output.append('pop de')\n temporal = True\n\n if indirect:\n output.append('call __LOAD_DE_DE')\n REQUIRES.add('lddede.asm')\n\n if not temporal:\n output.append('call __STORE_STR')\n REQUIRES.add('storestr.asm')\n else: # A value already on dynamic memory\n output.append('call __STORE_STR2')\n REQUIRES.add('storestr2.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nconvert an integer to a little - endian t - tuple of bytes.", "response": "def num2bytes(x, bytes):\n \"\"\" Returns x converted to a little-endian t-uple of bytes.\n E.g. num2bytes(255, 4) = (255, 0, 0, 0)\n \"\"\"\n if not isinstance(x, int): # If it is another \"thing\", just return ZEROs\n return tuple([0] * bytes)\n\n x = x & ((2 << (bytes * 8)) - 1) # mask the initial value\n result = ()\n\n for i in range(bytes):\n result += (x & 0xFF,)\n x >>= 8\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef argval(self):\n if self.arg is None or any(x is None for x in self.arg):\n return None\n\n for x in self.arg:\n if not isinstance(x, int):\n raise InvalidArgError(self.arg)\n\n return self.arg", "response": "Returns the value of the arg."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef bytes(self):\n result = []\n op = self.opcode.split(' ')\n argi = 0\n\n while op:\n q = op.pop(0)\n\n if q == 'XX':\n for k in range(self.argbytes[argi] - 1):\n op.pop(0)\n\n result.extend(num2bytes(self.argval()[argi], self.argbytes[argi]))\n argi += 1\n else:\n result.append(int(q, 16)) # Add opcode\n\n if len(result) != self.size:\n raise InternalMismatchSizeError(len(result), self)\n\n return result", "response": "Returns a t - tuple with instruction bytes"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nwarning for implicit type x for id_.", "response": "def warning_implicit_type(lineno, id_, type_=None):\n \"\"\" Warning: Using default implicit type 'x'\n \"\"\"\n if OPTIONS.strict.value:\n syntax_error_undeclared_type(lineno, id_)\n return\n\n if type_ is None:\n type_ = global_.DEFAULT_TYPE\n\n warning(lineno, \"Using default implicit type '%s' for '%s'\" % (type_, id_))"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn operands of an ASM instruction.", "response": "def oper(inst):\n \"\"\" Returns operands of an ASM instruction.\n Even \"indirect\" operands, like SP if RET or CALL is used.\n \"\"\"\n i = inst.strip(' \\t\\n').split(' ')\n I = i[0].lower() # Instruction\n i = ''.join(i[1:])\n\n op = i.split(',')\n if I in {'call', 'jp', 'jr'} and len(op) > 1:\n op = op[1:] + ['f']\n\n elif I == 'djnz':\n op.append('b')\n\n elif I in {'push', 'pop', 'call'}:\n op.append('sp') # Sp is also affected by push, pop and call\n\n elif I in {'or', 'and', 'xor', 'neg', 'cpl', 'rrca', 'rlca'}:\n op.append('a')\n\n elif I in {'rra', 'rla'}:\n op.extend(['a', 'f'])\n\n elif I in ('rr', 'rl'):\n op.append('f')\n\n elif I in {'adc', 'sbc'}:\n if len(op) == 1:\n op = ['a', 'f'] + op\n\n elif I in {'add', 'sub'}:\n if len(op) == 1:\n op = ['a'] + op\n\n elif I in {'ldd', 'ldi', 'lddr', 'ldir'}:\n op = ['hl', 'de', 'bc']\n\n elif I in {'cpd', 'cpi', 'cpdr', 'cpir'}:\n op = ['a', 'hl', 'bc']\n\n elif I == 'exx':\n op = ['*', 'bc', 'de', 'hl', 'b', 'c', 'd', 'e', 'h', 'l']\n\n elif I in {'ret', 'reti', 'retn'}:\n op += ['sp']\n\n elif I == 'out':\n if len(op) and RE_OUTC.match(op[0]):\n op[0] = 'c'\n else:\n op.pop(0)\n\n elif I == 'in':\n if len(op) > 1 and RE_OUTC.match(op[1]):\n op[1] = 'c'\n else:\n op.pop(1)\n\n for i in range(len(op)):\n tmp = RE_INDIR16.match(op[i])\n if tmp is not None:\n op[i] = '(' + op[i].strip()[1:-1].strip().lower() + ')' # ' ( dE ) ' => '(de)'\n\n return op"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef condition(i):\n I = inst(i)\n\n if I not in {'call', 'jp', 'jr', 'ret'}:\n return None # This instruction always execute\n\n if I == 'ret':\n i = [x.lower() for x in i.split(' ') if x != '']\n return i[1] if len(i) > 1 else None\n\n i = [x.strip() for x in i.split(',')]\n i = [x.lower() for x in i[0].split(' ') if x != '']\n if len(i) > 1 and i[1] in {'c', 'nc', 'z', 'nz', 'po', 'pe', 'p', 'm'}:\n return i[1]\n\n return None", "response": "Returns the flag this instruction uses\n or None. E. g. c z p m is not - zero etc."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef single_registers(op):\n result = set()\n if isinstance(op, str):\n op = [op]\n\n for x in op:\n if is_8bit_register(x):\n result = result.union([x])\n elif x == 'sp':\n result.add(x)\n elif x == 'af':\n result = result.union(['a', 'f'])\n elif x == \"af'\":\n result = result.union([\"a'\", \"f'\"])\n elif is_16bit_register(x): # Must be a 16bit reg or we have an internal error!\n result = result.union([LO16(x), HI16(x)])\n\n return list(result)", "response": "Given a list of single registers returns a list of single registers."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the list of 8 - bit registers that can be used by an asm instruction to return a result.", "response": "def result(i):\n \"\"\" Returns which 8-bit registers are used by an asm\n instruction to return a result.\n \"\"\"\n ins = inst(i)\n op = oper(i)\n\n if ins in ('or', 'and') and op == ['a']:\n return ['f']\n\n if ins in {'xor', 'or', 'and', 'neg', 'cpl', 'daa', 'rld', 'rrd', 'rra', 'rla', 'rrca', 'rlca'}:\n return ['a', 'f']\n\n if ins in {'bit', 'cp', 'scf', 'ccf'}:\n return ['f']\n\n if ins in {'sub', 'add', 'sbc', 'adc'}:\n if len(op) == 1:\n return ['a', 'f']\n else:\n return single_registers(op[0]) + ['f']\n\n if ins == 'djnz':\n return ['b', 'f']\n\n if ins in {'ldir', 'ldi', 'lddr', 'ldd'}:\n return ['f', 'b', 'c', 'd', 'e', 'h', 'l']\n\n if ins in {'cpi', 'cpir', 'cpd', 'cpdr'}:\n return ['f', 'b', 'c', 'h', 'l']\n\n if ins in ('pop', 'ld'):\n return single_registers(op[0])\n\n if ins in {'inc', 'dec', 'sbc', 'rr', 'rl', 'rrc', 'rlc'}:\n return ['f'] + single_registers(op[0])\n\n if ins in ('set', 'res'):\n return single_registers(op[1])\n\n return []"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef block_partition(block, i):\n i += 1\n new_block = BasicBlock(block.asm[i:])\n block.mem = block.mem[:i]\n block.asm = block.asm[:i]\n block.update_labels()\n new_block.update_labels()\n\n new_block.goes_to = block.goes_to\n block.goes_to = IdentitySet()\n\n new_block.label_goes = block.label_goes\n block.label_goes = []\n\n new_block.next = new_block.original_next = block.original_next\n new_block.prev = block\n new_block.add_comes_from(block)\n\n if new_block.next is not None:\n new_block.next.prev = new_block\n new_block.next.add_comes_from(new_block)\n new_block.next.delete_from(block)\n\n block.next = block.original_next = new_block\n block.update_next_block()\n block.add_goes_to(new_block)\n\n return block, new_block", "response": "Partition a basic block into two blocks."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef partition_block(block):\n result = [block]\n\n if not block.is_partitionable:\n return result\n\n EDP = END_PROGRAM_LABEL + ':'\n\n for i in range(len(block) - 1):\n if i and block.asm[i] == EDP: # END_PROGRAM label always starts a basic block\n block, new_block = block_partition(block, i - 1)\n LABELS[END_PROGRAM_LABEL].basic_block = new_block\n result.extend(partition_block(new_block))\n return result\n\n if block.mem[i].is_ender:\n block, new_block = block_partition(block, i)\n result.extend(partition_block(new_block))\n op = block.mem[i].opers\n\n for l in op:\n if l in LABELS.keys():\n JUMP_LABELS.add(l)\n block.label_goes += [l]\n return result\n\n if block.asm[i] in arch.zx48k.backend.ASMS:\n if i > 0:\n block, new_block = block_partition(block, i - 1)\n result.extend(partition_block(new_block))\n return result\n\n block, new_block = block_partition(block, i)\n result.extend(partition_block(new_block))\n return result\n\n for label in JUMP_LABELS:\n must_partition = False\n if LABELS[label].basic_block is block:\n for i in range(len(block)):\n cell = block.mem[i]\n if cell.inst == label:\n break\n\n if cell.is_label:\n continue\n\n if cell.is_ender:\n continue\n\n must_partition = True\n\n if must_partition:\n block, new_block = block_partition(block, i - 1)\n LABELS[label].basic_block = new_block\n result.extend(partition_block(new_block))\n return result\n\n return result", "response": "Partition a block into a list of blocks with the same block."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget all the labels in the global label table.", "response": "def get_labels(MEMORY, basic_block):\n \"\"\" Traverses memory, to annotate all the labels in the global\n LABELS table\n \"\"\"\n for cell in MEMORY:\n if cell.is_label:\n label = cell.inst\n LABELS[label] = LabelInfo(label, cell.addr, basic_block)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ninitialize the global memory array with the given one", "response": "def initialize_memory(basic_block):\n \"\"\" Initializes global memory array with the given one\n \"\"\"\n global MEMORY\n\n MEMORY = basic_block.mem\n get_labels(MEMORY, basic_block)\n basic_block.mem = MEMORY"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nclean up initial memory.", "response": "def cleanupmem(initial_memory):\n \"\"\" Cleans up initial memory. Each label must be\n ALONE. Each instruction must have an space, etc...\n \"\"\"\n i = 0\n while i < len(initial_memory):\n tmp = initial_memory[i]\n match = RE_LABEL.match(tmp)\n if not match:\n i += 1\n continue\n\n if tmp.rstrip() == match.group():\n i += 1\n continue\n\n initial_memory[i] = tmp[match.end():]\n initial_memory.insert(i, match.group())\n i += 1"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef cleanup_local_labels(block):\n global PROC_COUNTER\n\n stack = [[]]\n hashes = [{}]\n stackprc = [PROC_COUNTER]\n used = [{}] # List of hashes of unresolved labels per scope\n\n MEMORY = block.mem\n\n for cell in MEMORY:\n if cell.inst.upper() == 'PROC':\n stack += [[]]\n hashes += [{}]\n stackprc += [PROC_COUNTER]\n used += [{}]\n PROC_COUNTER += 1\n continue\n\n if cell.inst.upper() == 'ENDP':\n if len(stack) > 1: # There might be unbalanced stack due to syntax errors\n for label in used[-1].keys():\n if label in stack[-1]:\n newlabel = hashes[-1][label]\n for cell in used[-1][label]:\n cell.replace_label(label, newlabel)\n\n stack.pop()\n hashes.pop()\n stackprc.pop()\n used.pop()\n continue\n\n tmp = cell.asm.strip()\n if tmp.upper()[:5] == 'LOCAL':\n tmp = tmp[5:].split(',')\n for lbl in tmp:\n lbl = lbl.strip()\n if lbl in stack[-1]:\n continue\n stack[-1] += [lbl]\n hashes[-1][lbl] = 'PROC%i.' % stackprc[-1] + lbl\n if used[-1].get(lbl, None) is None:\n used[-1][lbl] = []\n\n cell.asm = ';' + cell.asm # Remove it\n continue\n\n if cell.is_label:\n label = cell.inst\n for i in range(len(stack) - 1, -1, -1):\n if label in stack[i]:\n label = hashes[i][label]\n cell.asm = label + ':'\n break\n continue\n\n for label in cell.used_labels:\n labelUsed = False\n for i in range(len(stack) - 1, -1, -1):\n if label in stack[i]:\n newlabel = hashes[i][label]\n cell.replace_label(label, newlabel)\n labelUsed = True\n break\n\n if not labelUsed:\n if used[-1].get(label, None) is None:\n used[-1][label] = []\n\n used[-1][label] += [cell]\n\n for i in range(len(MEMORY) - 1, -1, -1):\n if MEMORY[i].asm[0] == ';':\n MEMORY.pop(i)\n\n block.mem = MEMORY\n block.asm = [x.asm for x in MEMORY if len(x.asm.strip())]", "response": "Removes any local labels from the block and returns a unique version of the block."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef optimize(initial_memory):\n global BLOCKS\n global PROC_COUNTER\n\n LABELS.clear()\n JUMP_LABELS.clear()\n del MEMORY[:]\n PROC_COUNTER = 0\n\n cleanupmem(initial_memory)\n if OPTIONS.optimization.value <= 2:\n return '\\n'.join(x for x in initial_memory if not RE_PRAGMA.match(x))\n\n optimize_init()\n bb = BasicBlock(initial_memory)\n cleanup_local_labels(bb)\n initialize_memory(bb)\n\n BLOCKS = basic_blocks = get_basic_blocks(bb) # 1st partition the Basic Blocks\n\n for x in basic_blocks:\n x.clean_up_comes_from()\n x.clean_up_goes_to()\n\n for x in basic_blocks:\n x.update_goes_and_comes()\n\n LABELS['*START*'].basic_block.add_goes_to(basic_blocks[0])\n LABELS['*START*'].basic_block.next = basic_blocks[0]\n\n basic_blocks[0].prev = LABELS['*START*'].basic_block\n LABELS[END_PROGRAM_LABEL].basic_block.add_goes_to(LABELS['*__END_PROGRAM*'].basic_block)\n\n for x in basic_blocks:\n x.optimize()\n\n for x in basic_blocks:\n if x.comes_from == [] and len([y for y in JUMP_LABELS if x is LABELS[y].basic_block]):\n x.ignored = True\n\n return '\\n'.join([y for y in flatten_list([x.asm for x in basic_blocks if not x.ignored])\n if not RE_PRAGMA.match(y)])", "response": "This will remove useless instructions from the memory and return a string of the resulting basic blocks."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nresets the flags to an unknown state", "response": "def reset_flags(self):\n \"\"\" Resets flags to an \"unknown state\"\n \"\"\"\n self.C = None\n self.Z = None\n self.P = None\n self.S = None"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get(self, r):\n if r is None:\n return None\n\n if r.lower() == '(sp)' and self.stack:\n return self.stack[-1]\n\n if r[:1] == '(':\n return self.mem[r[1:-1]]\n\n r = r.lower()\n if is_number(r):\n return str(valnum(r))\n\n if not is_register(r):\n return None\n\n return self.regs[r]", "response": "Returns the precomputed value of the given expression\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nliking get but returns the value.", "response": "def getv(self, r):\n \"\"\" Like the above, but returns the value.\n \"\"\"\n v = self.get(r)\n if not is_unknown(v):\n try:\n v = int(v)\n except ValueError:\n v = None\n else:\n v = None\n return v"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef eq(self, r1, r2):\n if not is_register(r1) or not is_register(r2):\n return False\n\n if self.regs[r1] is None or self.regs[r2] is None: # HINT: This's been never USED??\n return False\n\n return self.regs[r1] == self.regs[r2]", "response": "True if values of r1 and r2 registers are equal"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef inc(self, r):\n self.set_flag(None)\n\n if not is_register(r):\n if r[0] == '(': # a memory position, basically: inc(hl)\n r_ = r[1:-1].strip()\n v_ = self.getv(self.mem.get(r_, None))\n if v_ is not None:\n v_ = (v_ + 1) & 0xFF\n self.mem[r_] = str(v_)\n self.Z = int(v_ == 0) # HINT: This might be improved\n else:\n self.mem[r_] = new_tmp_val()\n return\n\n if self.getv(r) is not None:\n self.set(r, self.getv(r) + 1)\n else:\n self.set(r, None)", "response": "Does inc on the register and precomputes flags\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndoing a ROTATION to the RIGHT |>>", "response": "def rrc(self, r):\n \"\"\" Does a ROTATION to the RIGHT |>>\n \"\"\"\n if not is_number(self.regs[r]):\n self.set(r, None)\n self.set_flag(None)\n return\n\n v_ = self.getv(self.regs[r]) & 0xFF\n self.regs[r] = str((v_ >> 1) | ((v_ & 1) << 7))"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef rr(self, r):\n if self.C is None or not is_number(self.regs[r]):\n self.set(r, None)\n self.set_flag(None)\n return\n\n self.rrc(r)\n tmp = self.C\n v_ = self.getv(self.regs[r])\n self.C = v_ >> 7\n self.regs[r] = str((v_ & 0x7F) | (tmp << 7))", "response": "Like the above bus uses carry\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndoing a ROTATION to the LEFT <<|", "response": "def rlc(self, r):\n \"\"\" Does a ROTATION to the LEFT <<|\n \"\"\"\n if not is_number(self.regs[r]):\n self.set(r, None)\n self.set_flag(None)\n return\n\n v_ = self.getv(self.regs[r]) & 0xFF\n self.set(r, ((v_ << 1) & 0xFF) | (v_ >> 7))"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nliking the above bus uses carry", "response": "def rl(self, r):\n \"\"\" Like the above, bus uses carry\n \"\"\"\n if self.C is None or not is_number(self.regs[r]):\n self.set(r, None)\n self.set_flag(None)\n return\n\n self.rlc(r)\n tmp = self.C\n v_ = self.getv(self.regs[r])\n self.C = v_ & 1\n self.regs[r] = str((v_ & 0xFE) | tmp)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ntrue if value of r is val.", "response": "def _is(self, r, val):\n \"\"\" True if value of r is val.\n \"\"\"\n if not is_register(r) or val is None:\n return False\n\n r = r.lower()\n if is_register(val):\n return self.eq(r, val)\n\n if is_number(val):\n val = str(valnum(val))\n else:\n val = str(val)\n\n if val[0] == '(':\n val = self.mem[val[1:-1]]\n\n return self.regs[r] == val"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nupdating the registers values with the given instruction.", "response": "def op(self, i, o):\n \"\"\" Tries to update the registers values with the given\n instruction.\n \"\"\"\n for ii in range(len(o)):\n if is_register(o[ii]):\n o[ii] = o[ii].lower()\n\n if i == 'ld':\n self.set(o[0], o[1])\n return\n\n if i == 'push':\n if valnum(self.regs['sp']):\n self.set('sp', (self.getv(self.regs['sp']) - 2) % 0xFFFF)\n else:\n self.set('sp', None)\n self.stack.append(self.regs[o[0]])\n return\n\n if i == 'pop':\n self.set(o[0], self.stack and self.stack.pop() or None)\n if valnum(self.regs['sp']):\n self.set('sp', (self.getv(self.regs['sp']) + 2) % 0xFFFF)\n else:\n self.set('sp', None)\n return\n\n if i == 'inc':\n self.inc(o[0])\n return\n\n if i == 'dec':\n self.dec(o[0])\n return\n\n if i == 'rra':\n self.rr('a')\n return\n if i == 'rla':\n self.rl('a')\n return\n if i == 'rlca':\n self.rlc('a')\n return\n if i == 'rrca':\n self.rrc('a')\n return\n if i == 'rr':\n self.rr(o[0])\n return\n if i == 'rl':\n self.rl(o[0])\n return\n\n if i == 'exx':\n tmp = self.regs['bc']\n self.set('bc', \"bc'\")\n self.set(\"bc'\", tmp)\n tmp = self.regs['de']\n self.set('de', \"de'\")\n self.set(\"de'\", tmp)\n tmp = self.regs['hl']\n self.set('hl', \"hl'\")\n self.set(\"hl'\", tmp)\n return\n\n if i == 'ex':\n tmp = self.get(o[1])\n self.set(o[1], o[0])\n self.set(o[0], tmp)\n return\n\n if i == 'xor':\n self.C = 0\n\n if o[0] == 'a':\n self.set('a', 0)\n self.Z = 1\n return\n\n if self.getv('a') is None or self.getv(o[0]) is None:\n self.Z = None\n self.set('a', None)\n return\n\n self.set('a', self.getv('a') ^ self.getv(o[0]))\n self.Z = int(self.get('a') == 0)\n return\n\n if i in ('or', 'and'):\n self.C = 0\n\n if self.getv('a') is None or self.getv(o[0]) is None:\n self.Z = None\n self.set('a', None)\n return\n\n if i == 'or':\n self.set('a', self.getv('a') | self.getv(o[0]))\n else:\n self.set('a', self.getv('a') & self.getv(o[0]))\n\n self.Z = int(self.get('a') == 0)\n return\n\n if i in ('adc', 'sbc'):\n if len(o) == 1:\n o = ['a', o[0]]\n\n if self.C is None:\n self.set(o[0], 'None')\n self.Z = None\n self.set(o[0], None)\n return\n\n if i == 'sbc' and o[0] == o[1]:\n self.Z = int(not self.C)\n self.set(o[0], -self.C)\n return\n\n if self.getv(o[0]) is None or self.getv(o[1]) is None:\n self.set_flag(None)\n self.set(o[0], None)\n return\n\n if i == 'adc':\n val = self.getv(o[0]) + self.getv(o[1]) + self.C\n if is_8bit_register(o[0]):\n self.C = int(val > 0xFF)\n else:\n self.C = int(val > 0xFFFF)\n self.set(o[0], val)\n return\n\n val = self.getv(o[0]) - self.getv(o[1]) - self.C\n self.C = int(val < 0)\n self.Z = int(val == 0)\n self.set(o[0], val)\n return\n\n if i in ('add', 'sub'):\n if len(o) == 1:\n o = ['a', o[0]]\n\n if i == 'sub' and o[0] == o[1]:\n self.Z = 1\n self.C = 0\n self.set(o[0], 0)\n return\n\n if not is_number(self.get(o[0])) or not is_number(self.get(o[1])) is None:\n self.set_flag(None)\n self.set(o[0], None)\n return\n\n if i == 'add':\n val = self.getv(o[0]) + self.getv(o[1])\n if is_8bit_register(o[0]):\n self.C = int(val > 0xFF)\n val &= 0xFF\n self.Z = int(val == 0)\n self.S = val >> 7\n else:\n self.C = int(val > 0xFFFF)\n val &= 0xFFFF\n\n self.set(o[0], val)\n return\n\n val = self.getv(o[0]) - self.getv(o[1])\n if is_8bit_register(o[0]):\n self.C = int(val < 0)\n val &= 0xFF\n self.Z = int(val == 0)\n self.S = val >> 7\n else:\n self.C = int(val < 0)\n val &= 0xFFFF\n\n self.set(o[0], val)\n return\n\n if i == 'neg':\n if self.getv('a') is None:\n self.set_flag(None)\n return\n\n val = -self.getv('a')\n self.set('a', val)\n self.Z = int(not val)\n val &= 0xFF\n self.S = val >> 7\n return\n\n if i == 'scf':\n self.C = 1\n return\n\n if i == 'ccf':\n if self.C is not None:\n self.C = int(not self.C)\n return\n\n if i == 'cpl':\n if self.getv('a') is None:\n return\n\n self.set('a', 0xFF ^ self.getv('a'))\n return\n\n # Unknown. Resets ALL\n self.reset()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef opers(self):\n i = [x for x in self.asm.strip(' \\t\\n').split(' ') if x != '']\n\n if len(i) == 1:\n return []\n\n i = ''.join(i[1:]).split(',')\n if self.condition_flag is not None:\n i = i[1:]\n else:\n i = i[0:]\n\n op = [x.lower() if is_register(x) else x for x in i]\n return op", "response": "Returns a list of operators this mnemonic uses\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef destroys(self):\n\n if self.asm in arch.zx48k.backend.ASMS:\n return ALL_REGS\n\n res = set([])\n i = self.inst\n o = self.opers\n\n if i in {'push', 'ret', 'call', 'rst', 'reti', 'retn'}:\n return ['sp']\n\n if i == 'pop':\n res.update('sp', single_registers(o[:1]))\n elif i in {'ldi', 'ldir', 'ldd', 'lddr'}:\n res.update('a', 'b', 'c', 'd', 'e', 'f')\n elif i in {'otir', 'otdr', 'oti', 'otd', 'inir', 'indr', 'ini', 'ind'}:\n res.update('h', 'l', 'b')\n elif i in {'cpir', 'cpi', 'cpdr', 'cpd'}:\n res.update('h', 'l', 'b', 'c', 'f')\n elif i in ('ld', 'in'):\n res.update(single_registers(o[:1]))\n elif i in ('inc', 'dec'):\n res.update('f', single_registers(o[:1]))\n elif i == 'exx':\n res.update('b', 'c', 'd', 'e', 'h', 'l')\n elif i == 'ex':\n res.update(single_registers(o[0]))\n res.update(single_registers(o[1]))\n elif i in {'ccf', 'scf', 'bit', 'cp'}:\n res.add('f')\n elif i in {'or', 'and', 'xor', 'add', 'adc', 'sub', 'sbc'}:\n if len(o) > 1:\n res.update(single_registers(o[0]))\n else:\n res.add('a')\n res.add('f')\n elif i in {'neg', 'cpl', 'daa', 'rra', 'rla', 'rrca', 'rlca', 'rrd', 'rld'}:\n res.update('a', 'f')\n elif i == 'djnz':\n res.update('b', 'f')\n elif i in {'rr', 'rl', 'rrc', 'rlc', 'srl', 'sra', 'sll', 'sla'}:\n res.update(single_registers(o[0]))\n res.add('f')\n elif i in ('set', 'res'):\n res.update(single_registers(o[1]))\n\n return list(res)", "response": "Returns a list of all the destroys for the current entry."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the set of registers operands etc. required by an instruction.", "response": "def requires(self):\n \"\"\" Returns the registers, operands, etc. required by an instruction.\n \"\"\"\n if self.asm in arch.zx48k.backend.ASMS:\n return ALL_REGS\n\n if self.inst == '#pragma':\n tmp = self.__instr.split(' ')[1:]\n if tmp[0] != 'opt':\n return\n if tmp[1] == 'require':\n return set(flatten_list([single_registers(x.strip(', \\t\\r')) for x in tmp[2:]]))\n\n return set([])\n\n result = set([])\n i = self.inst\n o = [x.lower() for x in self.opers]\n\n if i in ['ret', 'pop', 'push']:\n result.add('sp')\n\n if self.condition_flag is not None or i in ['sbc', 'adc']:\n result.add('f')\n\n for O in o:\n if '(hl)' in O:\n result.add('h')\n result.add('l')\n\n if '(de)' in O:\n result.add('d')\n result.add('e')\n\n if '(bc)' in O:\n result.add('b')\n result.add('c')\n\n if '(sp)' in O:\n result.add('sp')\n\n if '(ix' in O:\n result.add('ixh')\n result.add('ixl')\n\n if '(iy' in O:\n result.add('iyh')\n result.add('iyl')\n\n if i in ['ccf']:\n result.add('f')\n\n elif i in {'rra', 'rla', 'rrca', 'rlca'}:\n result.add('a')\n result.add('f')\n\n elif i in ['xor', 'cp']:\n # XOR A, and CP A don't need the a register\n if o[0] != 'a':\n result.add('a')\n\n if o[0][0] != '(' and not is_number(o[0]):\n result = result.union(single_registers(o))\n\n elif i in ['or', 'and']:\n # AND A, and OR A do need the a register\n result.add('a')\n\n if o[0][0] != '(' and not is_number(o[0]):\n result = result.union(single_registers(o))\n\n elif i in {'adc', 'sbc', 'add', 'sub'}:\n if len(o) == 1:\n if i not in ('sub', 'sbc') or o[0] != 'a':\n # sbc a and sub a dont' need the a register\n result.add('a')\n\n if o[0][0] != '(' and not is_number(o[0]):\n result = result.union(single_registers(o))\n else:\n if o[0] != o[1] or i in ('add', 'adc'):\n # sub HL, HL or sub X, X don't need the X register(s)\n result = result.union(single_registers(o))\n\n if i in ['adc', 'sbc']:\n result.add('f')\n\n elif i in {'daa', 'rld', 'rrd', 'neg', 'cpl'}:\n result.add('a')\n\n elif i in {'rl', 'rr', 'rlc', 'rrc'}:\n result = result.union(single_registers(o) + ['f'])\n\n elif i in {'sla', 'sll', 'sra', 'srl', 'inc', 'dec'}:\n result = result.union(single_registers(o))\n\n elif i == 'djnz':\n result.add('b')\n\n elif i in {'ldir', 'lddr', 'ldi', 'ldd'}:\n result = result.union(['b', 'c', 'd', 'e', 'h', 'l'])\n\n elif i in {'cpi', 'cpd', 'cpir', 'cpdr'}:\n result = result.union(['a', 'b', 'c', 'h', 'l'])\n\n elif i == 'ld' and not is_number(o[1]):\n result = result.union(single_registers(o[1]))\n\n elif i == 'ex':\n if o[0] == 'de':\n result = result.union(['d', 'e', 'h', 'l'])\n elif o[1] == '(sp)':\n result = result.union(['h', 'l']) # sp already included\n else:\n result = result.union(['a', 'f', \"a'\", \"f'\"])\n\n elif i == 'exx':\n result = result.union(['b', 'c', 'd', 'e', 'h', 'l'])\n\n elif i == 'push':\n result = result.union(single_registers(o))\n\n elif i in {'bit', 'set', 'res'}:\n result = result.union(single_registers(o[1]))\n\n elif i == 'out':\n result.add(o[1])\n if o[0] == '(c)':\n result.add('c')\n\n elif i == 'in':\n if o[1] == '(c)':\n result.add('c')\n\n elif i == 'im':\n result.add('i')\n\n result = list(result)\n return result"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning if this instruction affects any of the registers in reglist.", "response": "def affects(self, reglist):\n \"\"\" Returns if this instruction affects any of the registers\n in reglist.\n \"\"\"\n if isinstance(reglist, str):\n reglist = [reglist]\n\n reglist = single_registers(reglist)\n\n return len([x for x in self.destroys if x in reglist]) > 0"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn if this instruction needs any of the registers in reglist.", "response": "def needs(self, reglist):\n \"\"\" Returns if this instruction need any of the registers\n in reglist.\n \"\"\"\n if isinstance(reglist, str):\n reglist = [reglist]\n\n reglist = single_registers(reglist)\n\n return len([x for x in self.requires if x in reglist]) > 0"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of required labels for this instruction", "response": "def used_labels(self):\n \"\"\" Returns a list of required labels for this instruction\n \"\"\"\n result = []\n\n tmp = self.asm.strip(' \\n\\r\\t')\n if not len(tmp) or tmp[0] in ('#', ';'):\n return result\n\n try:\n tmpLexer = asmlex.lex.lex(object=asmlex.Lexer(), lextab='zxbasmlextab')\n tmpLexer.input(tmp)\n\n while True:\n token = tmpLexer.token()\n if not token:\n break\n\n if token.type == 'ID':\n result += [token.value]\n except:\n pass\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef replace_label(self, oldLabel, newLabel):\n if oldLabel == newLabel:\n return\n\n tmp = re.compile(r'\\b' + oldLabel + r'\\b')\n last = 0\n l = len(newLabel)\n while True:\n match = tmp.search(self.asm[last:])\n if not match:\n break\n\n txt = self.asm\n self.asm = txt[:last + match.start()] + newLabel + txt[last + match.end():]\n last += match.start() + l", "response": "Replaces old label with a new one."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef is_partitionable(self):\n if len(self.mem) < 2:\n return False # An atomic block\n\n if any(x.is_ender or x.asm in arch.zx48k.backend.ASMS for x in self.mem):\n return True\n\n for label in JUMP_LABELS:\n if LABELS[label].basic_block == self and (not self.mem[0].is_label or self.mem[0].inst != label):\n return True\n\n return False", "response": "Returns if this block can be partitioned in 2 or more blocks."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nremove the basic_block from the list for the entry in the entry list.", "response": "def delete_from(self, basic_block):\n \"\"\" Removes the basic_block ptr from the list for \"comes_from\"\n if it exists. It also sets self.prev to None if it is basic_block.\n \"\"\"\n if basic_block is None:\n return\n\n if self.lock:\n return\n\n self.lock = True\n\n if self.prev is basic_block:\n if self.prev.next is self:\n self.prev.next = None\n self.prev = None\n\n for i in range(len(self.comes_from)):\n if self.comes_from[i] is basic_block:\n self.comes_from.pop(i)\n break\n\n self.lock = False"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nadd the basic_block to the comes_from list.", "response": "def add_comes_from(self, basic_block):\n \"\"\" This simulates a set. Adds the basic_block to the comes_from\n list if not done already.\n \"\"\"\n if basic_block is None:\n return\n\n if self.lock:\n return\n\n # Return if already added\n if basic_block in self.comes_from:\n return\n\n self.lock = True\n self.comes_from.add(basic_block)\n basic_block.add_goes_to(self)\n self.lock = False"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nupdating the next basic block and the next sets.", "response": "def update_next_block(self):\n \"\"\" If the last instruction of this block is a JP, JR or RET (with no\n conditions) then the next and goes_to sets just contains a\n single block\n \"\"\"\n last = self.mem[-1]\n if last.inst not in ('ret', 'jp', 'jr') or last.condition_flag is not None:\n return\n\n if last.inst == 'ret':\n if self.next is not None:\n self.next.delete_from(self)\n self.delete_goes(self.next)\n return\n\n if last.opers[0] not in LABELS.keys():\n __DEBUG__(\"INFO: %s is not defined. No optimization is done.\" % last.opers[0], 2)\n LABELS[last.opers[0]] = LabelInfo(last.opers[0], 0, DummyBasicBlock(ALL_REGS, ALL_REGS))\n\n n_block = LABELS[last.opers[0]].basic_block\n if self.next is n_block:\n return\n\n if self.next.prev == self:\n # The next basic block is not this one since it ends with a jump\n self.next.delete_from(self)\n self.delete_goes(self.next)\n\n self.next = n_block\n self.next.add_comes_from(self)\n self.add_goes_to(self.next)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nupdates the goes_to and comes_from sets of the receivers.", "response": "def update_goes_and_comes(self):\n \"\"\" Once the block is a Basic one, check the last instruction and updates\n goes_to and comes_from set of the receivers.\n Note: jp, jr and ret are already done in update_next_block()\n \"\"\"\n # Remove any block from the comes_from and goes_to list except the PREVIOUS and NEXT\n if not len(self):\n return\n\n if self.mem[-1].inst == 'ret':\n return # subroutine returns are updated from CALLer blocks\n\n self.update_used_by_list()\n\n if not self.mem[-1].is_ender:\n return\n\n last = self.mem[-1]\n inst = last.inst\n oper = last.opers\n cond = last.condition_flag\n\n if oper and oper[0] not in LABELS.keys():\n __DEBUG__(\"INFO: %s is not defined. No optimization is done.\" % oper[0], 1)\n LABELS[oper[0]] = LabelInfo(oper[0], 0, DummyBasicBlock(ALL_REGS, ALL_REGS))\n\n if inst == 'djnz' or inst in ('jp', 'jr') and cond is not None:\n if oper[0] in LABELS.keys():\n self.add_goes_to(LABELS[oper[0]].basic_block)\n\n elif inst in ('jp', 'jr') and cond is None:\n if oper[0] in LABELS.keys():\n self.delete_goes(self.next)\n self.next = LABELS[oper[0]].basic_block\n self.add_goes_to(self.next)\n\n elif inst == 'call':\n LABELS[oper[0]].basic_block.add_comes_from(self)\n stack = [LABELS[oper[0]].basic_block]\n bbset = IdentitySet()\n\n while stack:\n bb = stack.pop(0)\n\n while bb is not None:\n if bb in bbset:\n break\n\n bbset.add(bb)\n if len(bb):\n bb1 = bb[-1]\n if bb1.inst == 'ret':\n bb.add_goes_to(self.next)\n if bb1.condition_flag is None: # 'ret'\n break\n\n if bb1.inst in ('jp', 'jr') and bb1.condition_flag is not None: # jp/jr nc/nz/.. LABEL\n if bb1.opers[0] in LABELS: # some labels does not exist (e.g. immediate numeric addresses)\n stack += [LABELS[bb1.opers[0]].basic_block]\n\n bb = bb.next # next contiguous block\n\n if cond is None:\n self.calls.add(LABELS[oper[0]].basic_block)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncheck whether any of the given regs are required from the given point to the end or not.", "response": "def is_used(self, regs, i, top=None):\n \"\"\" Checks whether any of the given regs are required from the given point\n to the end or not.\n \"\"\"\n if i < 0:\n i = 0\n\n if self.lock:\n return True\n\n regs = list(regs) # make a copy\n if top is None:\n top = len(self)\n else:\n top -= 1\n\n for ii in range(i, top):\n for r in self.mem[ii].requires:\n if r in regs:\n return True\n\n for r in self.mem[ii].destroys:\n if r in regs:\n regs.remove(r)\n\n if not regs:\n return False\n\n self.lock = True\n result = self.goes_requires(regs)\n self.lock = False\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngiving a list of registers returns a list of them that are safe to write from the given index until the given end_.", "response": "def safe_to_write(self, regs, i=0, end_=0):\n \"\"\" Given a list of registers (8 or 16 bits) returns a list of them\n that are safe to modify from the given index until the position given\n which, if omitted, defaults to the end of the block.\n :param regs: register or iterable of registers (8 or 16 bit one)\n :param i: initial position of the block to examine\n :param end_: final position to examine\n :returns: registers safe to write\n \"\"\"\n if is_register(regs):\n regs = set(single_registers(regs))\n else:\n regs = set(single_registers(x) for x in regs)\n return not regs.intersection(self.requires(i, end_))"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a list of registers and variables that this block requires. By default checks from the beginning.", "response": "def requires(self, i=0, end_=None):\n \"\"\" Returns a list of registers and variables this block requires.\n By default checks from the beginning (i = 0).\n :param i: initial position of the block to examine\n :param end_: final position to examine\n :returns: registers safe to write\n \"\"\"\n if i < 0:\n i = 0\n end_ = len(self) if end_ is None or end_ > len(self) else end_\n regs = {'a', 'b', 'c', 'd', 'e', 'f', 'h', 'l', 'i', 'ixh', 'ixl', 'iyh', 'iyl', 'sp'}\n result = []\n\n for ii in range(i, end_):\n for r in self.mem[ii].requires:\n r = r.lower()\n if r in regs:\n result.append(r)\n regs.remove(r)\n\n for r in self.mem[ii].destroys:\n r = r.lower()\n if r in regs:\n regs.remove(r)\n\n if not regs:\n break\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef destroys(self, i=0):\n regs = {'a', 'b', 'c', 'd', 'e', 'f', 'h', 'l', 'i', 'ixh', 'ixl', 'iyh', 'iyl', 'sp'}\n top = len(self)\n result = []\n\n for ii in range(i, top):\n for r in self.mem[ii].destroys:\n if r in regs:\n result.append(r)\n regs.remove(r)\n\n if not regs:\n break\n\n return result", "response": "Returns a list of registers this block destroys by default checks from the beginning."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef swap(self, a, b):\n self.mem[a], self.mem[b] = self.mem[b], self.mem[a]\n self.asm[a], self.asm[b] = self.asm[b], self.asm[a]", "response": "Swaps the memory positions a and b."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef goes_requires(self, regs):\n if len(self) and self.mem[-1].inst == 'call' and self.mem[-1].condition_flag is None:\n for block in self.calls:\n if block.is_used(regs, 0):\n return True\n\n d = block.destroys()\n if not len([x for x in regs if x not in d]):\n return False # If all registers are destroyed then they're not used\n\n for block in self.goes_to:\n if block.is_used(regs, 0):\n return True\n\n return False", "response": "Returns True if any of the goes_to blocks requires any of the given registers."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_label_idx(self, label):\n for i in range(len(self)):\n if self.mem[i].is_label and self.mem[i].inst == label:\n return i\n\n return None", "response": "Returns the index of a label. Returns None if the label is not found."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_first_non_label_instruction(self):\n for i in range(len(self)):\n if not self.mem[i].is_label:\n return self.mem[i]\n\n return None", "response": "Returns the first non - label instruction in the block."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ntrying to detect peep - hole patterns in this basic block and remove them.", "response": "def optimize(self):\n \"\"\" Tries to detect peep-hole patterns in this basic block\n and remove them.\n \"\"\"\n changed = OPTIONS.optimization.value > 2 # only with -O3 will enter here\n\n while changed:\n changed = False\n regs = Registers()\n\n if len(self) and self[-1].inst in ('jp', 'jr') and \\\n self.original_next is LABELS[self[-1].opers[0]].basic_block:\n # { jp Label ; Label: ; ... } => { Label: ; ... }\n LABELS[self[-1].opers[0]].used_by.remove(self)\n self.pop(len(self) - 1)\n changed = True\n continue\n\n for i in range(len(self)):\n try:\n if self.mem[i].is_label:\n # ignore labels\n continue\n except IndexError:\n print(i)\n print('\\n'.join(str(x) for x in self.mem))\n raise\n\n i1 = self.mem[i].inst\n o1 = self.mem[i].opers\n\n if i > 0:\n i0 = self.mem[i - 1].inst\n o0 = self.mem[i - 1].opers\n else:\n i0 = o0 = None\n\n if i < len(self) - 1:\n i2 = self.mem[i + 1].inst\n o2 = self.mem[i + 1].opers\n else:\n i2 = o2 = None\n\n if i < len(self) - 2:\n i3 = self.mem[i + 2].inst\n o3 = self.mem[i + 2].opers\n else:\n i3 = o3 = None\n\n if i1 == 'ld':\n if OPT00 and o1[0] == o1[1]:\n # { LD X, X } => {}\n self.pop(i)\n changed = True\n break\n\n if OPT01 and o0 == 'ld' and o0[0] == o1[1] and o1[0] == o0[1]:\n # { LD A, X; LD X, A} => {LD A, X}\n self.pop(i)\n changed = True\n break\n\n if OPT02 and i0 == i1 == 'ld' and o0[1] == o1[1] and \\\n is_register(o0[0]) and is_register(o1[0]) and not is_16bit_idx_register(o1[0]):\n if is_8bit_register(o1[0]):\n if not is_8bit_register(o1[1]):\n # { LD r1, N; LD r2, N} => {LD r1, N; LD r2, r1}\n changed = True\n self[i] = 'ld %s, %s' % (o1[0], o0[0])\n break\n else:\n changed = True\n # {LD r1, NN; LD r2, NN} => { LD r1, NN; LD r2H, r1H; LD r2L, r1L}\n self[i] = 'ld %s, %s' % (HI16(o1[0]), HI16(o0[0]))\n self.insert(i + 1, 'ld %s, %s' % (LO16(o1[0]), LO16(o0[0])))\n break\n\n if OPT03 and is_register(o1[0]) and o1[0] != 'sp' and \\\n not self.is_used(single_registers(o1[0]), i + 1):\n # LD X, nnn ; X not used later => Remove instruction\n tmp = str(self.asm)\n self.pop(i)\n changed = True\n __DEBUG__('Changed %s ==> %s' % (tmp, self.asm), 2)\n break\n\n if OPT04 and o1 == ['h', 'a'] and i2 == 'ld' and o2[0] == 'a' \\\n and i3 == 'sub' and o3[0] == 'h' and not self.is_used('h', i + 3):\n if is_number(o2[1]):\n self[i] = 'neg'\n self[i + 1] = 'add a, %s' % o2[1]\n self[i + 2] = 'ccf'\n changed = True\n break\n\n if OPT05 and regs._is(o1[0], o1[1]): # and regs.get(o1[0])[0:3] != '(ix':\n tmp = str(self.asm)\n self.pop(i)\n changed = True\n __DEBUG__('Changed %s ==> %s' % (tmp, self.asm), 2)\n break\n\n if OPT06 and o1[0] in ('hl', 'de') and \\\n i2 == 'ex' and o2[0] == 'de' and o2[1] == 'hl' and \\\n not self.is_used(single_registers(o1[0]), i + 2):\n # { LD HL, XX ; EX DE, HL; POP HL } ::= { LD DE, XX ; POP HL }\n reg = 'de' if o1[0] == 'hl' else 'hl'\n self.pop(i + 1)\n self[i] = 'ld %s, %s' % (reg, o1[1])\n changed = True\n break\n\n if OPT07 and i0 == 'ld' and i2 == 'ld' and o2[1] == 'hl' and not self.is_used(['h', 'l'], i + 2) \\\n and (o0[0] == 'h' and o0[1] == 'b' and o1[0] == 'l' and o1[1] == 'c' or\n o0[0] == 'l' and o0[1] == 'c' and o1[0] == 'h' and o1[1] == 'b' or\n o0[0] == 'h' and o0[1] == 'd' and o1[0] == 'l' and o1[1] == 'e' or\n o0[0] == 'l' and o0[1] == 'e' and o1[0] == 'h' and o1[1] == 'd'):\n # { LD h, rH ; LD l, rl ; LD (XX), HL } ::= { LD (XX), R }\n tmp = str(self.asm)\n r2 = 'de' if o0[1] in ('d', 'e') else 'bc'\n self[i + 1] = 'ld %s, %s' % (o2[0], r2)\n self.pop(i)\n self.pop(i - 1)\n changed = True\n __DEBUG__('Changed %s ==> %s' % (tmp, self.asm), 2)\n break\n\n if OPT08 and i1 == i2 == 'ld' and i > 0 and \\\n (o1[1] == 'h' and o1[0] == 'b' and o2[1] == 'l' and o2[0] == 'c' or\n o1[1] == 'l' and o1[0] == 'c' and o2[1] == 'h' and o2[0] == 'b' or\n o1[1] == 'h' and o1[0] == 'd' and o2[1] == 'l' and o2[0] == 'e' or\n o1[1] == 'l' and o1[0] == 'e' and o2[1] == 'h' and o2[\n 0] == 'd') and \\\n regs.get('hl') is not None and not self.is_used(['h', 'l'], i + 2) and \\\n not self[i - 1].needs(['h', 'l']) and not self[i - 1].affects(['h', 'l']):\n # { LD HL, XXX ; ; LD rH, H; LD rL, L } ::= { LD HL, XXX ; LD rH, H; LD rL, L; }\n changed = True\n tmp = str(self.asm)\n self.swap(i - 1, i + 1)\n __DEBUG__('Changed %s ==> %s' % (tmp, self.asm), 2)\n break\n\n if OPT09 and i > 0 and i0 == i1 == i2 == 'ld' and \\\n o0[0] == 'hl' and \\\n (o1[1] == 'h' and o1[0] == 'b' and o2[1] == 'l' and o2[0] == 'c' or\n o1[1] == 'l' and o1[0] == 'c' and o2[1] == 'h' and o2[0] == 'b' or\n o1[1] == 'h' and o1[0] == 'd' and o2[1] == 'l' and o2[0] == 'e' or\n o1[1] == 'l' and o1[0] == 'e' and o2[1] == 'h' and o2[0] == 'd') and \\\n not self.is_used(['h', 'l'], i + 2):\n # { LD HL, XXX ; LD rH, H; LD rL, L } ::= { LD rr, XXX }\n changed = True\n r1 = 'de' if o1[0] in ('d', 'e') else 'bc'\n tmp = str(self.asm)\n self[i - 1] = 'ld %s, %s' % (r1, o0[1])\n self.pop(i + 1)\n self.pop(i)\n __DEBUG__('Changed %s ==> %s' % (tmp, self.asm), 2)\n break\n\n if OPT10 and i1 in ('inc', 'dec') and o1[0] == 'a':\n if i2 == i0 == 'ld' and o2[0] == o0[1] and 'a' == o0[0] == o2[1] and o0[1][0] == '(':\n if not RE_INDIR.match(o2[0]):\n if not self.is_used(['a', 'h', 'l'], i + 2):\n # { LD A, (X); [ DEC A | INC A ]; LD (X), A} ::= {LD HL, X; [ DEC (HL) | INC (HL) ]}\n tmp = str(self.asm)\n self.pop(i + 1)\n self[i - 1] = 'ld hl, %s' % (o0[1][1:-1])\n self[i] = '%s (hl)' % i1\n changed = True\n __DEBUG__('Changed %s ==> %s' % (tmp, self.asm), 2)\n break\n else:\n if not self.is_used(['a'], i + 2):\n # { LD A, (IX + n); [ DEC A | INC A ]; LD (X), A} ::=\n # { [ DEC (IX + n) | INC (IX + n) ] }\n tmp = str(self.asm)\n self.pop(i + 1)\n self.pop(i)\n self[i - 1] = '%s %s' % (i1, o0[1])\n changed = True\n __DEBUG__('Changed %s ==> %s' % (tmp, self.asm), 2)\n break\n\n if OPT11 and i0 == 'push' and i3 == 'pop' and o0[0] != o3[0] \\\n and o0[0] in ('hl', 'de') and o3[0] in ('hl', 'de') \\\n and i1 == i2 == 'ld' and (\n o1[0] == HI16(o0[0]) and o2[0] == LO16(o0[0]) and o1[1] == HI16(o3[0]) and\n o2[1] == LO16(o3[0]) or\n o2[0] == HI16(o0[0]) and o1[0] == LO16(o0[0]) and o2[1] == HI16(\n o3[0]) and o1[1] == LO16(o3[0])):\n # { PUSH HL; LD H, D; LD L, E; POP HL } ::= {EX DE, HL}\n self.pop(i + 2)\n self.pop(i + 1)\n self.pop(i)\n self[i - 1] = 'ex de, hl'\n changed = True\n break\n\n if i0 == 'push' and i1 == 'pop':\n if OPT12 and o0[0] == o1[0]:\n # { PUSH X ; POP X } ::= { }\n self.pop(i)\n self.pop(i - 1)\n changed = True\n break\n\n if OPT13 and o0[0] in ('de', 'hl') and o1[0] in ('de', 'hl') and not self.is_used(\n single_registers(o0[0]), i + 1):\n # { PUSH DE ; POP HL } ::= { EX DE, HL }\n self.pop(i)\n self[i - 1] = 'ex de, hl'\n changed = True\n break\n\n if OPT14 and 'af' in (o0[0], o1[0]):\n # { push Xx ; pop af } => { ld a, X }\n if not self.is_used(o1[0][1], i + 1):\n self[i - 1] = 'ld %s, %s' % (HI16(o1[0]), HI16(o0[0]))\n self.pop(i)\n changed = True\n break\n elif OPT15 and not is_16bit_idx_register(o0[0]) and not is_16bit_idx_register(\n o1[0]) and 'af' not in (o0[0], o1[0]):\n # { push Xx ; pop Yy } => { ld Y, X ; ld y, x }\n self[i - 1] = 'ld %s, %s' % (HI16(o1[0]), HI16(o0[0]))\n self[i] = 'ld %s, %s' % (LO16(o1[0]), LO16(o0[0]))\n changed = True\n break\n\n if OPT16 and i > 0 and not self.mem[i - 1].is_label and i1 == 'pop' and \\\n (not self.mem[i - 1].affects([o1[0], 'sp']) or\n self.safe_to_write(o1[0], i + 1)) and \\\n not self.mem[i - 1].needs([o1[0], 'sp']):\n # { ; POP X } => { POP X; } ; if inst does not uses X\n tmp = str(self.asm)\n self.swap(i - 1, i)\n changed = True\n __DEBUG__('Changed %s ==> %s' % (tmp, self.asm), 2)\n break\n\n if OPT17 and i1 == 'xor' and o1[0] == 'a' and regs._is('a', 0) and regs.Z and not regs.C:\n tmp = str(self.asm)\n self.pop(i)\n __DEBUG__('Changed %s ==> %s' % (tmp, self.asm), 2)\n changed = True\n break\n\n if OPT18 and i3 is not None and \\\n (i0 == i1 == 'ld' and i2 == i3 == 'push') and \\\n (o0[0] == o3[0] == 'de' and o1[0] == o2[0] == 'bc'): # and \\\n if not self.is_used(['h', 'l', 'd', 'e', 'b', 'c'], i + 3):\n # { LD DE, (X2) ; LD BC, (X1); PUSH DE; PUSH BC } ::=\n # { LD HL, (X2); PUSH HL; LD HL, (X1); PUSH HL }\n self[i - 1] = 'ld hl, %s' % o1[1]\n self[i] = 'push hl'\n self[i + 1] = 'ld hl, %s' % o0[1]\n self[i + 2] = 'push hl'\n changed = True\n break\n\n if i1 in ('jp', 'jr', 'call') and o1[0] in JUMP_LABELS:\n c = self.mem[i].condition_flag\n if OPT19 and c is not None:\n if c == 'c' and regs.C == 1 or \\\n c == 'z' and regs.Z == 1 or \\\n c == 'nc' and regs.C == 0 or \\\n c == 'nz' and regs.Z == 0:\n # If the condition is always satisfied, replace with a simple jump / call\n changed = True\n tmp = str(self.asm)\n self[i] = '%s %s' % (i1, o1[0])\n self.update_goes_and_comes()\n __DEBUG__('Changed %s ==> %s' % (tmp, self.asm), 2)\n break\n\n ii = LABELS[o1[0]].basic_block.get_first_non_label_instruction()\n ii1 = None if ii is None else ii.inst\n cc = None if ii is None else ii.condition_flag\n # Are we calling / jumping into another jump?\n if OPT20 and ii1 in ('jp', 'jr') and (\n cc is None or\n cc == c or\n cc == 'c' and regs.C == 1 or\n cc == 'z' and regs.Z == 1 or\n cc == 'nc' and regs.C == 0 or\n cc == 'nz' and regs.Z == 0):\n if c is None:\n c = ''\n else:\n c = c + ', '\n\n changed = True\n tmp = str(self.asm)\n LABELS[o1[0]].used_by.remove(self) # This block no longer uses this label\n self[i] = '%s %s%s' % (i1, c, ii.opers[0])\n self.update_goes_and_comes()\n __DEBUG__('Changed %s ==> %s' % (tmp, self.asm), 2)\n break\n\n if OPT22 and i0 == 'sbc' and o0[0] == o0[1] == 'a' and \\\n i1 == 'or' and o1[0] == 'a' and \\\n i2 == 'jp' and \\\n self[i + 1].condition_flag is not None and \\\n not self.is_used(['a'], i + 2):\n c = self.mem[i + 1].condition_flag\n if c in ('z', 'nz'):\n c = 'c' if c == 'nz' else 'nc'\n changed = True\n self[i + 1] = 'jp %s, %s' % (c, o2[0])\n self.pop(i)\n self.pop(i - 1)\n break\n\n if OPT23 and i0 == 'ld' and is_16bit_register(o0[0]) and o0[1][0] == '(' and \\\n i1 == 'ld' and o1[0] == 'a' and o1[1] == LO16(o0[0]) and not self.is_used(\n single_registers(o0[0]), i + 1):\n # { LD HL, (X) ; LD A, L } ::= { LD A, (X) }\n self.pop(i)\n self[i - 1] = 'ld a, %s' % o0[1]\n changed = True\n break\n\n if OPT24 and i1 == i2 == 'ccf': # { ccf ; ccf }\u00a0::= {\u00a0}\n self.pop(i)\n self.pop(i)\n changed = True\n break\n\n if OPT25 and i1 == 'ld' and is_register(o1[0]) and o1[0] != 'sp':\n is8 = is_8bit_register(o1[0])\n ss = [x for x, y in regs.regs.items() if x != o1[0] and y is not None and y == regs.get(o1[1]) and\n not is_8bit_register(o1[1])]\n for r_ in ss:\n if is8 != is_8bit_register(r_):\n continue\n changed = True\n if is8: # ld A, n; ld B, n => ld A, n; ld B, A\n self[i] = 'ld %s, %s' % (o1[0], r_)\n else: # ld HL, n; ld DE, n => ld HL, n; ld d, h; ld e, l\n # 16 bit register\n self[i] = 'ld %s, %s' % (HI16(o1[0]), HI16(r_))\n self.insert(i + 1, 'ld %s, %s' % (LO16(o1[0]), LO16(r_)))\n break\n\n if changed:\n break\n\n if OPT26 and i1 == i2 == 'ld' and (o1[0], o1[1], o2[0], o2[1]) == ('d', 'h', 'e', 'l') and \\\n not self.is_used(['h', 'l'], i + 2):\n self[i] = 'ex de, hl'\n self.pop(i + 1)\n changed = True\n break\n\n if OPT27 and i1 in ('cp', 'or', 'and', 'add', 'adc', 'sub', 'sbc') and o1[-1] != 'a' and \\\n not self.is_used(o1[-1], i + 1) and i0 == 'ld' and o0[0] == o1[-1] and \\\n (o0[1] == '(hl)' or RE_IXIND.match(o0[1])):\n template = '{0} %s{1}' % ('a, ' if i1 in ('add', 'adc', 'sbc') else '')\n self[i] = template.format(i1, o0[1])\n self.pop(i - 1)\n changed = True\n break\n\n regs.op(i1, o1)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a string representation of the operands in the tree.", "response": "def _str_oper(op1, op2=None, reversed=False, no_exaf=False):\n ''' Returns pop sequence for 16 bits operands\n 1st operand in HL, 2nd operand in DE\n\n You can swap operators extraction order\n by setting reversed to True.\n\n If no_exaf = True => No bits flags in A' will be used.\n This saves two bytes.\n '''\n output = []\n\n if op2 is not None and reversed:\n op1, op2 = op2, op1\n\n tmp2 = False\n if op2 is not None:\n val = op2\n if val[0] == '*':\n indirect = True\n val = val[1:]\n else:\n indirect = False\n\n if val[0] == '_': # Direct\n output.append('ld de, (%s)' % val)\n elif val[0] == '#': # Direct\n output.append('ld de, %s' % val[1:])\n elif val[0] == '$': # Direct in the stack\n output.append('pop de')\n else:\n output.append('pop de')\n tmp2 = True\n\n if indirect:\n output.append('call __LOAD_DE_DE')\n REQUIRES.add('lddede.asm') # TODO: This is never used??\n\n if reversed:\n tmp = output\n output = []\n\n val = op1\n tmp1 = False\n if val[0] == '*':\n indirect = True\n val = val[1:]\n else:\n indirect = False\n\n if val[0] == '_': # Direct\n output.append('ld hl, (%s)' % val)\n elif val[0] == '#': # Inmmediate\n output.append('ld hl, %s' % val[1:])\n elif val[0] == '$': # Direct in the stack\n output.append('pop hl')\n else:\n output.append('pop hl')\n tmp1 = True\n\n if indirect:\n output.append('ld hl, %s' % val[1:])\n output.append('ld c, (hl)')\n output.append('inc hl')\n output.append('ld h, (hl)')\n output.append('ld l, c')\n\n if reversed:\n output.extend(tmp)\n\n if not no_exaf:\n if tmp1 and tmp2:\n output.append('ld a, 3') # Marks both strings to be freed\n elif tmp1:\n output.append('ld a, 1') # Marks str1 to be freed\n elif tmp2:\n output.append('ld a, 2') # Marks str2 to be freed\n else:\n output.append('xor a') # Marks no string to be freed\n\n if op2 is not None:\n return (tmp1, tmp2, output)\n\n return (tmp1, output)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\noutputs a FREEMEM sequence for 1 or 2 ops", "response": "def _free_sequence(tmp1, tmp2=False):\n ''' Outputs a FREEMEM sequence for 1 or 2 ops\n '''\n if not tmp1 and not tmp2:\n return []\n\n output = []\n if tmp1 and tmp2:\n output.append('pop de')\n output.append('ex (sp), hl')\n output.append('push de')\n output.append('call __MEM_FREE')\n output.append('pop hl')\n output.append('call __MEM_FREE')\n else:\n output.append('ex (sp), hl')\n output.append('call __MEM_FREE')\n\n output.append('pop hl')\n REQUIRES.add('alloc.asm')\n return output"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncomparing & pops top 2 strings out of the stack.", "response": "def _nestr(ins):\n ''' Compares & pops top 2 strings out of the stack.\n Temporal values are freed from memory. (a$ != b$)\n '''\n (tmp1, tmp2, output) = _str_oper(ins.quad[2], ins.quad[3])\n output.append('call __STRNE')\n output.append('push af')\n REQUIRES.add('string.asm')\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef make_node(cls, lineno, fname, func=None, type_=None, *operands):\n if func is not None and len(operands) == 1: # Try constant-folding\n if is_number(operands[0]) or is_string(operands[0]): # e.g. ABS(-5)\n return SymbolNUMBER(func(operands[0].value), type_=type_, lineno=lineno)\n\n return cls(lineno, fname, type_, *operands)", "response": "Creates a node for a unary operation."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _8bit_oper(op1, op2=None, reversed_=False):\n output = []\n\n if op2 is not None and reversed_:\n tmp = op1\n op1 = op2\n op2 = tmp\n\n op = op1\n indirect = (op[0] == '*')\n if indirect:\n op = op[1:]\n\n immediate = (op[0] == '#')\n if immediate:\n op = op[1:]\n\n if is_int(op):\n op = int(op)\n\n if indirect:\n output.append('ld a, (%i)' % op)\n else:\n if op == 0:\n output.append('xor a')\n else:\n output.append('ld a, %i' % int8(op))\n else:\n if immediate:\n if indirect:\n output.append('ld a, (%s)' % op)\n else:\n output.append('ld a, %s' % op)\n elif op[0] == '_':\n if indirect:\n idx = 'bc' if reversed_ else 'hl'\n output.append('ld %s, (%s)' % (idx, op)) # can't use HL\n output.append('ld a, (%s)' % idx)\n else:\n output.append('ld a, (%s)' % op)\n else:\n if immediate:\n output.append('ld a, %s' % op)\n elif indirect:\n idx = 'bc' if reversed_ else 'hl'\n output.append('pop %s' % idx)\n output.append('ld a, (%s)' % idx)\n else:\n output.append('pop af')\n\n if op2 is None:\n return output\n\n if not reversed_:\n tmp = output\n output = []\n\n op = op2\n indirect = (op[0] == '*')\n if indirect:\n op = op[1:]\n\n immediate = (op[0] == '#')\n if immediate:\n op = op[1:]\n\n if is_int(op):\n op = int(op)\n\n if indirect:\n output.append('ld hl, (%i - 1)' % op)\n else:\n output.append('ld h, %i' % int8(op))\n else:\n if immediate:\n if indirect:\n output.append('ld hl, %s' % op)\n output.append('ld h, (hl)')\n else:\n output.append('ld h, %s' % op)\n elif op[0] == '_':\n if indirect:\n output.append('ld hl, (%s)' % op)\n output.append('ld h, (hl)' % op)\n else:\n output.append('ld hl, (%s - 1)' % op)\n else:\n output.append('pop hl')\n\n if indirect:\n output.append('ld h, (hl)')\n\n if not reversed_:\n output.extend(tmp)\n\n return output", "response": "Returns a pop sequence for 8 bits operands op1 and op2."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nadding an 8 - bit integer to the stack and returns the result.", "response": "def _add8(ins):\n \"\"\" Pops last 2 bytes from the stack and adds them.\n Then push the result onto the stack.\n\n Optimizations:\n * If any of the operands is ZERO,\n then do NOTHING: A + 0 = 0 + A = A\n\n * If any of the operands is 1, then\n INC is used\n\n * If any of the operands is -1 (255), then\n DEC is used\n \"\"\"\n op1, op2 = tuple(ins.quad[2:])\n if _int_ops(op1, op2) is not None:\n op1, op2 = _int_ops(op1, op2)\n\n output = _8bit_oper(op1)\n if op2 == 0: # Nothing to add: A + 0 = A\n output.append('push af')\n return output\n\n op2 = int8(op2)\n\n if op2 == 1: # Adding 1 is just an inc\n output.append('inc a')\n output.append('push af')\n return output\n\n if op2 == 0xFF: # Adding 255 is just a dec\n output.append('dec a')\n output.append('push af')\n return output\n\n output.append('add a, %i' % int8(op2))\n output.append('push af')\n return output\n\n if op2[0] == '_': # stack optimization\n op1, op2 = op2, op1\n\n output = _8bit_oper(op1, op2)\n output.append('add a, h')\n output.append('push af')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsubs function for internal use only.", "response": "def _sub8(ins):\n \"\"\" Pops last 2 bytes from the stack and subtract them.\n Then push the result onto the stack. Top-1 of the stack is\n subtracted Top\n _sub8 t1, a, b === t1 <-- a - b\n\n Optimizations:\n * If 2nd op is ZERO,\n then do NOTHING: A - 0 = A\n\n * If 1st operand is 0, then\n just do a NEG\n\n * If any of the operands is 1, then\n DEC is used\n\n * If any of the operands is -1 (255), then\n INC is used\n \"\"\"\n\n op1, op2 = tuple(ins.quad[2:])\n if is_int(op2): # 2nd operand\n op2 = int8(op2)\n output = _8bit_oper(op1)\n\n if op2 == 0:\n output.append('push af')\n return output # A - 0 = A\n\n op2 = int8(op2)\n\n if op2 == 1: # A - 1 == DEC A\n output.append('dec a')\n output.append('push af')\n return output\n\n if op2 == 0xFF: # A - (-1) == INC A\n output.append('inc a')\n output.append('push af')\n return output\n\n output.append('sub %i' % op2)\n output.append('push af')\n return output\n\n if is_int(op1): # 1st operand is numeric?\n if int8(op1) == 0: # 0 - A = -A ==> NEG A\n output = _8bit_oper(op2)\n output.append('neg')\n output.append('push af')\n return output\n\n # At this point, even if 1st operand is numeric, proceed\n # normally\n\n if op2[0] == '_': # Optimization when 2nd operand is an id\n rev = True\n op1, op2 = op2, op1\n else:\n rev = False\n\n output = _8bit_oper(op1, op2, rev)\n output.append('sub h')\n output.append('push af')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _mul8(ins):\n\n op1, op2 = tuple(ins.quad[2:])\n if _int_ops(op1, op2) is not None:\n op1, op2 = _int_ops(op1, op2)\n\n output = _8bit_oper(op1)\n if op2 == 1: # A * 1 = 1 * A = A\n output.append('push af')\n return output\n\n if op2 == 0:\n output.append('xor a')\n output.append('push af')\n return output\n\n if op2 == 2: # A * 2 == A SLA 1\n output.append('add a, a')\n output.append('push af')\n return output\n\n if op2 == 4: # A * 4 == A SLA 2\n output.append('add a, a')\n output.append('add a, a')\n output.append('push af')\n return output\n\n output.append('ld h, %i' % int8(op2))\n else:\n if op2[0] == '_': # stack optimization\n op1, op2 = op2, op1\n\n output = _8bit_oper(op1, op2)\n\n output.append('call __MUL8_FAST') # Inmmediate\n output.append('push af')\n REQUIRES.add('mul8.asm')\n return output", "response": "Multiplies 2 las values from the stack."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _divu8(ins):\n op1, op2 = tuple(ins.quad[2:])\n if is_int(op2):\n op2 = int8(op2)\n\n output = _8bit_oper(op1)\n if op2 == 1:\n output.append('push af')\n return output\n\n if op2 == 2:\n output.append('srl a')\n output.append('push af')\n return output\n\n output.append('ld h, %i' % int8(op2))\n else:\n if op2[0] == '_': # Optimization when 2nd operand is an id\n if is_int(op1) and int(op1) == 0:\n output = list() # Optimization: Discard previous op if not from the stack\n output.append('xor a')\n output.append('push af')\n return output\n\n rev = True\n op1, op2 = op2, op1\n else:\n rev = False\n\n output = _8bit_oper(op1, op2, rev)\n\n output.append('call __DIVU8_FAST')\n output.append('push af')\n REQUIRES.add('div8.asm')\n return output", "response": "Divides 2 8bit unsigned integers."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _ltu8(ins):\n output = _8bit_oper(ins.quad[2], ins.quad[3])\n output.append('cp h')\n output.append('sbc a, a')\n output.append('push af')\n\n return output", "response": "Compares top 2 operands and pushes them out of the stack and checks\n if the 1st operand < 2nd operand."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncompare top 2 operands out of the stack and checks if the 1st operand < 2nd operand. Returns a list of 8 bit signed version of the tree.", "response": "def _lti8(ins):\n \"\"\" Compares & pops top 2 operands out of the stack, and checks\n if the 1st operand < 2nd operand (top of the stack).\n Pushes 0 if False, 1 if True.\n\n 8 bit signed version\n \"\"\"\n output = []\n output.extend(_8bit_oper(ins.quad[2], ins.quad[3]))\n output.append('call __LTI8')\n output.append('push af')\n REQUIRES.add('lti8.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _gtu8(ins):\n output = _8bit_oper(ins.quad[2], ins.quad[3], reversed_=True)\n output.append('cp h')\n output.append('sbc a, a')\n output.append('push af')\n\n return output", "response": "Compares top 2 operands and pushes them out of the stack and checks\n if the 1st operand > 2nd operand."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncompare top 2 operands out of the stack and checks if they are equal.", "response": "def _eq8(ins):\n \"\"\" Compares & pops top 2 operands out of the stack, and checks\n if the 1st operand == 2nd operand (top of the stack).\n Pushes 0 if False, 1 if True.\n\n 8 bit un/signed version\n \"\"\"\n if is_int(ins.quad[3]):\n output = _8bit_oper(ins.quad[2])\n n = int8(ins.quad[3])\n if n:\n if n == 1:\n output.append('dec a')\n else:\n output.append('sub %i' % n)\n else:\n output = _8bit_oper(ins.quad[2], ins.quad[3])\n output.append('sub h')\n\n output.append('sub 1') # Sets Carry only if 0\n output.append('sbc a, a')\n output.append('push af')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _leu8(ins):\n output = _8bit_oper(ins.quad[2], ins.quad[3], reversed_=True)\n output.append('sub h') # Carry if H > A\n output.append('ccf') # Negates => Carry if H <= A\n output.append('sbc a, a')\n output.append('push af')\n\n return output", "response": "Compares top 2 operands and pops top 3 operands out of the stack and checks\n if the 1st operand < 2nd operand."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _lei8(ins):\n output = _8bit_oper(ins.quad[2], ins.quad[3])\n output.append('call __LEI8')\n output.append('push af')\n REQUIRES.add('lei8.asm')\n\n return output", "response": "Compares top 2 operands and pushes them out of the stack and checks\n if the 1st operand < 2nd operand. Returns the new stack."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _gei8(ins):\n output = _8bit_oper(ins.quad[2], ins.quad[3], reversed_=True)\n output.append('call __LEI8')\n output.append('push af')\n REQUIRES.add('lei8.asm')\n\n return output", "response": "Compares top 2 operands and pushes them out of the stack and checks\n if the 1st operand > 2nd operand."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _and8(ins):\n op1, op2 = tuple(ins.quad[2:])\n if _int_ops(op1, op2) is not None:\n op1, op2 = _int_ops(op1, op2)\n\n output = _8bit_oper(op1) # Pops the stack (if applicable)\n if op2 != 0: # X and True = X\n output.append('push af')\n return output\n\n # False and X = False\n output.append('xor a')\n output.append('push af')\n return output\n\n output = _8bit_oper(op1, op2)\n # output.append('call __AND8')\n lbl = tmp_label()\n output.append('or a')\n output.append('jr z, %s' % lbl)\n output.append('ld a, h')\n output.append('%s:' % lbl)\n output.append('push af')\n # REQUIRES.add('and8.asm')\n\n return output", "response": "Internal function to produce an AND instruction."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\npop top 2 operands out of the stack and pushes the result.", "response": "def _band8(ins):\n \"\"\" Pops top 2 operands out of the stack, and does\n 1st AND (bitwise) 2nd operand (top of the stack),\n pushes the result.\n\n 8 bit un/signed version\n \"\"\"\n op1, op2 = tuple(ins.quad[2:])\n if _int_ops(op1, op2) is not None:\n op1, op2 = _int_ops(op1, op2)\n\n output = _8bit_oper(op1)\n if op2 == 0xFF: # X & 0xFF = X\n output.append('push af')\n return output\n\n if op2 == 0: # X and 0 = 0\n output.append('xor a')\n output.append('push af')\n return output\n\n op1, op2 = tuple(ins.quad[2:])\n\n output = _8bit_oper(op1, op2)\n output.append('and h')\n output.append('push af')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\npops top 2 operands out of the stack, and checks if 1st operand XOR (logical) 2nd operand (top of the stack), pushes 0 if False, 1 if True. 8 bit un/signed version", "response": "def _xor8(ins):\n \"\"\" Pops top 2 operands out of the stack, and checks\n if 1st operand XOR (logical) 2nd operand (top of the stack),\n pushes 0 if False, 1 if True.\n\n 8 bit un/signed version\n \"\"\"\n op1, op2 = tuple(ins.quad[2:])\n if _int_ops(op1, op2) is not None:\n op1, op2 = _int_ops(op1, op2)\n\n output = _8bit_oper(op1) # True or X = not X\n if op2 == 0: # False xor X = X\n output.append('push af')\n return output\n\n output.append('sub 1')\n output.append('sbc a, a')\n output.append('push af')\n return output\n\n output = _8bit_oper(op1, op2)\n output.append('call __XOR8')\n output.append('push af')\n REQUIRES.add('xor8.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _not8(ins):\n output = _8bit_oper(ins.quad[2])\n output.append('sub 1') # Gives carry only if A = 0\n output.append('sbc a, a') # Gives FF only if Carry else 0\n output.append('push af')\n\n return output", "response": "Negates top of the stack"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nnegates (BITWISE NOT) top of the stack (8 bits in AF)", "response": "def _bnot8(ins):\n \"\"\" Negates (BITWISE NOT) top of the stack (8 bits in AF)\n \"\"\"\n output = _8bit_oper(ins.quad[2])\n output.append('cpl') # Gives carry only if A = 0\n output.append('push af')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nnegate top of the stack.", "response": "def _neg8(ins):\n \"\"\" Negates top of the stack (8 bits in AF)\n \"\"\"\n output = _8bit_oper(ins.quad[2])\n output.append('neg')\n output.append('push af')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _abs8(ins):\n output = _8bit_oper(ins.quad[2])\n output.append('call __ABS8')\n output.append('push af')\n REQUIRES.add('abs8.asm')\n return output", "response": "Returns the absolute value of top of the stack."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nshifts 8bit unsigned integer to the right.", "response": "def _shru8(ins):\n \"\"\" Shift 8bit unsigned integer to the right. The result is pushed onto the stack.\n\n Optimizations:\n * If 1nd or 2nd op is 0 then\n do nothing\n\n * If 2nd op is < 4 then\n unroll loop\n \"\"\"\n op1, op2 = tuple(ins.quad[2:])\n\n if is_int(op2):\n op2 = int8(op2)\n\n output = _8bit_oper(op1)\n if op2 == 0:\n output.append('push af')\n return output\n\n if op2 < 4:\n output.extend(['srl a'] * op2)\n output.append('push af')\n return output\n\n label = tmp_label()\n output.append('ld b, %i' % int8(op2))\n output.append('%s:' % label)\n output.append('srl a')\n output.append('djnz %s' % label)\n output.append('push af')\n return output\n\n if is_int(op1) and int(op1) == 0:\n output = _8bit_oper(op2)\n output.append('xor a')\n output.append('push af')\n return output\n\n output = _8bit_oper(op1, op2, True)\n label = tmp_label()\n label2 = tmp_label()\n output.append('or a')\n output.append('ld b, a')\n output.append('ld a, h')\n output.append('jr z, %s' % label2)\n output.append('%s:' % label)\n output.append('srl a')\n output.append('djnz %s' % label)\n output.append('%s:' % label2)\n output.append('push af')\n return output"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ntrue if this macro is defined in the given symbol", "response": "def is_defined(self, symbolTable=None):\n \"\"\" True if this macro has been defined\n \"\"\"\n if symbolTable is None:\n symbolTable = self.table\n\n return symbolTable.defined(self.id_)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate an array BOUND LIST. node. children are appended to the node.", "response": "def make_node(cls, node, *args):\n ''' Creates an array BOUND LIST.\n '''\n if node is None:\n return cls.make_node(SymbolBOUNDLIST(), *args)\n\n if node.token != 'BOUNDLIST':\n return cls.make_node(None, node, *args)\n\n for arg in args:\n if arg is None:\n continue\n node.appendChild(arg)\n\n return node"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ninitializes the parser state", "response": "def init():\n \"\"\" Initializes parser state\n \"\"\"\n global LABELS\n global LET_ASSIGNMENT\n global PRINT_IS_USED\n global SYMBOL_TABLE\n\n global ast\n global data_ast\n global optemps\n global OPTIONS\n global last_brk_linenum\n\n LABELS = {}\n LET_ASSIGNMENT = False\n PRINT_IS_USED = False\n last_brk_linenum = 0\n\n ast = None\n data_ast = None # Global Variables AST\n optemps = OpcodesTemps()\n\n gl.INITS.clear()\n del gl.FUNCTION_CALLS[:]\n del gl.FUNCTION_LEVEL[:]\n del gl.FUNCTIONS[:]\n SYMBOL_TABLE = gl.SYMBOL_TABLE = api.symboltable.SymbolTable()\n OPTIONS = api.config.OPTIONS\n\n # DATAs info\n gl.DATA_LABELS.clear()\n gl.DATA_IS_USED = False\n del gl.DATAS[:]\n gl.DATA_PTR_CURRENT = api.utils.current_data_label()\n gl.DATA_FUNCTIONS = []\n gl.error_msg_cache.clear()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef make_number(value, lineno, type_=None):\n return symbols.NUMBER(value, type_=type_, lineno=lineno)", "response": "Creates a number node."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef make_typecast(type_, node, lineno):\n assert isinstance(type_, symbols.TYPE)\n return symbols.TYPECAST.make_node(type_, node, lineno)", "response": "Wrapper for a Typecast node"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef make_binary(lineno, operator, left, right, func=None, type_=None):\n return symbols.BINARY.make_node(operator, left, right, lineno, func, type_)", "response": "Wrapper for creating a Binary node"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef make_unary(lineno, operator, operand, func=None, type_=None):\n return symbols.UNARY.make_node(lineno, operator, operand, func, type_)", "response": "Wrapper for making a Unary node"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef make_builtin(lineno, fname, operands, func=None, type_=None):\n if operands is None:\n operands = []\n assert isinstance(operands, Symbol) or isinstance(operands, tuple) or isinstance(operands, list)\n # TODO: In the future, builtin functions will be implemented in an external library, like POINT or ATTR\n __DEBUG__('Creating BUILTIN \"{}\"'.format(fname), 1)\n if not isinstance(operands, collections.Iterable):\n operands = [operands]\n return symbols.BUILTIN.make_node(lineno, fname, func, type_, *operands)", "response": "Wrapper for creating a Builtin function node."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nwrapping for creating a String Slice node", "response": "def make_strslice(lineno, s, lower, upper):\n \"\"\" Wrapper: returns String Slice node\n \"\"\"\n return symbols.STRSLICE.make_node(lineno, s, lower, upper)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef make_sentence(sentence, *args, **kwargs):\n return symbols.SENTENCE(*([sentence] + list(args)), **kwargs)", "response": "Wrapper for creating a Sentence node"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef make_argument(expr, lineno, byref=None):\n if expr is None:\n return # There were a syntax / semantic error\n\n if byref is None:\n byref = OPTIONS.byref.value\n return symbols.ARGUMENT(expr, lineno=lineno, byref=byref)", "response": "Wrapper for creating an ARGUMENT node"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef make_array_access(id_, lineno, arglist):\n return symbols.ARRAYACCESS.make_node(id_, arglist, lineno)", "response": "Creates an array access node."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef make_call(id_, lineno, args):\n assert isinstance(args, symbols.ARGLIST)\n\n entry = SYMBOL_TABLE.access_call(id_, lineno)\n if entry is None:\n return None\n\n if entry.class_ is CLASS.unknown and entry.type_ == TYPE.string and len(args) == 1 and is_numeric(args[0]):\n entry.class_ = CLASS.var # A scalar variable. e.g a$(expr)\n\n if entry.class_ == CLASS.array: # An already declared array\n arr = symbols.ARRAYLOAD.make_node(id_, args, lineno)\n if arr is None:\n return None\n\n if arr.offset is not None:\n offset = make_typecast(TYPE.uinteger,\n make_number(arr.offset, lineno=lineno),\n lineno)\n arr.appendChild(offset)\n return arr\n\n if entry.class_ == CLASS.var: # An already declared/used string var\n if len(args) > 1:\n api.errmsg.syntax_error_not_array_nor_func(lineno, id_)\n return None\n\n entry = SYMBOL_TABLE.access_var(id_, lineno)\n if entry is None:\n return None\n\n if len(args) == 1:\n return symbols.STRSLICE.make_node(lineno, entry, args[0].value, args[0].value)\n\n entry.accessed = True\n return entry\n\n return make_func_call(id_, lineno, args)", "response": "This function will return an AST node for a function call or array access."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef make_type(typename, lineno, implicit=False):\n assert isinstance(typename, str)\n if not SYMBOL_TABLE.check_is_declared(typename, lineno, 'type'):\n return None\n\n type_ = symbols.TYPEREF(SYMBOL_TABLE.get_entry(typename), lineno, implicit)\n return type_", "response": "Converts a typename identifier to a type usage symbol"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef make_bound(lower, upper, lineno):\n return symbols.BOUND.make_node(lower, upper, lineno)", "response": "Wrapper for creating an array bound node"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef make_label(id_, lineno):\n entry = SYMBOL_TABLE.declare_label(id_, lineno)\n if entry:\n gl.DATA_LABELS[id_] = gl.DATA_PTR_CURRENT # This label points to the current DATA block index\n return entry", "response": "Creates a label entry. Returns None on error."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef make_break(lineno, p):\n global last_brk_linenum\n\n if not OPTIONS.enableBreak.value or lineno == last_brk_linenum or is_null(p):\n return None\n\n last_brk_linenum = lineno\n return make_sentence('CHKBREAK', make_number(lineno, lineno, TYPE.uinteger))", "response": "Make a BREAK instruction."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nprogram that starts the program.", "response": "def p_start(p):\n \"\"\" start : program\n \"\"\"\n global ast, data_ast\n\n user_data = make_label('.ZXBASIC_USER_DATA', 0)\n make_label('.ZXBASIC_USER_DATA_LEN', 0)\n\n if PRINT_IS_USED:\n zxbpp.ID_TABLE.define('___PRINT_IS_USED___', 1)\n # zxbasmpp.ID_TABLE.define('___PRINT_IS_USED___', 1)\n\n if zxblex.IN_STATE:\n p.type = 'NEWLINE'\n p_error(p)\n sys.exit(1)\n\n ast = p[0] = p[1]\n __end = make_sentence('END', make_number(0, lineno=p.lexer.lineno))\n\n if not is_null(ast):\n ast.appendChild(__end)\n else:\n ast = __end\n\n SYMBOL_TABLE.check_labels()\n SYMBOL_TABLE.check_classes()\n\n if gl.has_errors:\n return\n\n __DEBUG__('Checking pending labels', 1)\n if not api.check.check_pending_labels(ast):\n return\n\n __DEBUG__('Checking pending calls', 1)\n if not api.check.check_pending_calls():\n return\n\n data_ast = make_sentence('BLOCK', user_data)\n\n # Appends variable declarations at the end.\n for var in SYMBOL_TABLE.vars_:\n data_ast.appendChild(make_var_declaration(var))\n\n # Appends arrays declarations at the end.\n for var in SYMBOL_TABLE.arrays:\n data_ast.appendChild(make_array_declaration(var))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nprogram : program program_line", "response": "def p_program(p):\n \"\"\" program : program program_line\n \"\"\"\n p[0] = make_block(p[1], p[2], make_break(p.lineno(2), p[2]))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef p_program_line_label(p):\n lbl = make_label(p[1], p.lineno(1))\n p[0] = make_block(lbl, p[2]) if len(p) == 3 else lbl", "response": "Label line with block if it is not already in the program line."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_var_decl(p):\n for vardata in p[2]:\n SYMBOL_TABLE.declare_variable(vardata[0], vardata[1], p[3])\n\n p[0] = None", "response": "declare variable in sequence"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef p_bound(p):\n p[0] = make_bound(make_number(OPTIONS.array_base.value,\n lineno=p.lineno(1)), p[1], p.lexer.lineno)", "response": "parse an array element bound"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef p_const_vector_elem_list(p):\n if p[1] is None:\n return\n\n if not is_static(p[1]):\n if isinstance(p[1], symbols.UNARY):\n tmp = make_constexpr(p.lineno(1), p[1])\n else:\n api.errmsg.syntax_error_not_constant(p.lexer.lineno)\n p[0] = None\n return\n else:\n tmp = p[1]\n\n p[0] = [tmp]", "response": "p = parser. parse_const_vector_elem_list"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef p_const_vector_elem_list_list(p):\n if p[1] is None or p[3] is None:\n return\n\n if not is_static(p[3]):\n if isinstance(p[3], symbols.UNARY):\n tmp = make_constexpr(p.lineno(2), p[3])\n else:\n api.errmsg.syntax_error_not_constant(p.lineno(2))\n p[0] = None\n return\n else:\n tmp = p[3]\n\n if p[1] is not None:\n p[1].append(tmp)\n p[0] = p[1]", "response": "parse a const vector elem list list"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_const_vector_vector_list(p):\n if len(p[3]) != len(p[1][0]):\n syntax_error(p.lineno(2), 'All rows must have the same number of elements')\n p[0] = None\n return\n\n p[0] = p[1] + [p[3]]", "response": "A function to parse a const_vector_list."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef p_statement_border(p):\n p[0] = make_sentence('BORDER',\n make_typecast(TYPE.ubyte, p[2], p.lineno(1)))", "response": "statement : BORDER expr"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_statement_plot(p):\n p[0] = make_sentence('PLOT',\n make_typecast(TYPE.ubyte, p[2], p.lineno(3)),\n make_typecast(TYPE.ubyte, p[4], p.lineno(3)))", "response": "statement : PLOT expr COMMA expr"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef p_statement_plot_attr(p):\n p[0] = make_sentence('PLOT',\n make_typecast(TYPE.ubyte, p[3], p.lineno(4)),\n make_typecast(TYPE.ubyte, p[5], p.lineno(4)), p[2])", "response": "statement : PLOT attr_list expr COMMA expr"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef p_statement_draw(p):\n p[0] = make_sentence('DRAW',\n make_typecast(TYPE.integer, p[2], p.lineno(3)),\n make_typecast(TYPE.integer, p[4], p.lineno(3)))", "response": "statement : DRAW expr COMMA expr"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef p_statement_draw_attr(p):\n p[0] = make_sentence('DRAW',\n make_typecast(TYPE.integer, p[3], p.lineno(4)),\n make_typecast(TYPE.integer, p[5], p.lineno(4)), p[2])", "response": "Statement draw attr list expr COMMA expr"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef p_statement_circle(p):\n p[0] = make_sentence('CIRCLE',\n make_typecast(TYPE.byte_, p[2], p.lineno(3)),\n make_typecast(TYPE.byte_, p[4], p.lineno(5)),\n make_typecast(TYPE.byte_, p[6], p.lineno(5)))", "response": "statement : CIRCLE expr COMMA expr COMMA expr"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef p_statement_beep(p):\n p[0] = make_sentence('BEEP', make_typecast(TYPE.float_, p[2], p.lineno(1)),\n make_typecast(TYPE.float_, p[4], p.lineno(3)))", "response": "statement : BEEP expr COMMA expr"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nparsing an assignment statement.", "response": "def p_assignment(p):\n \"\"\" statement : lexpr expr\n \"\"\"\n global LET_ASSIGNMENT\n\n LET_ASSIGNMENT = False # Mark we're no longer using LET\n p[0] = None\n q = p[1:]\n i = 1\n\n if q[1] is None:\n return\n\n if isinstance(q[1], symbols.VAR) and q[1].class_ == CLASS.unknown:\n q[1] = SYMBOL_TABLE.access_var(q[1].name, p.lineno(i))\n\n q1class_ = q[1].class_ if isinstance(q[1], symbols.VAR) else CLASS.unknown\n variable = SYMBOL_TABLE.access_id(q[0], p.lineno(i), default_type=q[1].type_, default_class=q1class_)\n if variable is None:\n return # HINT: This only happens if variable was not declared with DIM and --strict flag is in use\n\n if variable.class_ == CLASS.unknown: # The variable is implicit\n variable.class_ = CLASS.var\n\n if variable.class_ not in (CLASS.var, CLASS.array):\n api.errmsg.syntax_error_cannot_assing_not_a_var(p.lineno(i), variable.name)\n return\n\n if variable.class_ == CLASS.var and q1class_ == CLASS.array:\n syntax_error(p.lineno(i), 'Cannot assign an array to an scalar variable')\n return\n\n if variable.class_ == CLASS.array:\n if q1class_ != variable.class_:\n syntax_error(p.lineno(i), 'Cannot assign an scalar to an array variable')\n return\n\n if q[1].type_ != variable.type_:\n syntax_error(p.lineno(i), 'Arrays must have the same element type')\n return\n\n if variable.memsize != q[1].memsize:\n syntax_error(p.lineno(i), \"Arrays '%s' and '%s' must have the same size\" %\n (variable.name, q[1].name))\n return\n\n if variable.count != q[1].count:\n warning(p.lineno(i), \"Arrays '%s' and '%s' don't have the same number of dimensions\" %\n (variable.name, q[1].name))\n else:\n for b1, b2 in zip(variable.bounds, q[1].bounds):\n if b1.count != b2.count:\n warning(p.lineno(i), \"Arrays '%s' and '%s' don't have the same dimensions\" %\n (variable.name, q[1].name))\n break\n # Array copy\n p[0] = make_sentence('ARRAYCOPY', variable, q[1])\n return\n\n expr = make_typecast(variable.type_, q[1], p.lineno(i))\n p[0] = make_sentence('LET', variable, expr)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_lexpr(p):\n global LET_ASSIGNMENT\n\n LET_ASSIGNMENT = True # Mark we're about to start a LET sentence\n\n if p[1] == 'LET':\n p[0] = p[2]\n i = 2\n else:\n p[0] = p[1]\n i = 1\n\n SYMBOL_TABLE.access_id(p[i], p.lineno(i))", "response": "lexpr : ID EQ\n | LET ID EQ\n | ARRAY_ID EQ\n | LET ARRAY_ID EQ"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef p_arr_assignment(p):\n i = 2 if p[1].upper() == 'LET' else 1\n id_ = p[i]\n arg_list = p[i + 1]\n expr = p[i + 3]\n\n p[0] = None\n if arg_list is None or expr is None:\n return # There were errors\n\n entry = SYMBOL_TABLE.access_call(id_, p.lineno(i))\n if entry is None:\n return\n\n if entry.type_ == TYPE.string:\n variable = gl.SYMBOL_TABLE.access_array(id_, p.lineno(i))\n if len(variable.bounds) + 1 == len(arg_list):\n ss = arg_list.children.pop().value\n p[0] = make_array_substr_assign(p.lineno(i), id_, arg_list, (ss, ss), expr)\n return\n\n arr = make_array_access(id_, p.lineno(i), arg_list)\n if arr is None:\n return\n\n expr = make_typecast(arr.type_, expr, p.lineno(i))\n if entry is None:\n return\n\n p[0] = make_sentence('LETARRAY', arr, expr)", "response": "A statement for assignment of ARRAY_ID arg_list EQ expr\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef p_substr_assignment_no_let(p):\n # This can be only a substr assignment like a$(i + 3) = \".\", since arrays\n # have ARRAY_ID already\n entry = SYMBOL_TABLE.access_call(p[1], p.lineno(1))\n if entry is None:\n return\n\n if entry.class_ == CLASS.unknown:\n entry.class_ = CLASS.var\n\n if p[6].type_ != TYPE.string:\n api.errmsg.syntax_error_expected_string(p.lineno(5), p[6].type_)\n\n lineno = p.lineno(2)\n base = make_number(OPTIONS.string_base.value, lineno, _TYPE(gl.STR_INDEX_TYPE))\n substr = make_typecast(_TYPE(gl.STR_INDEX_TYPE), p[3], lineno)\n p[0] = make_sentence('LETSUBSTR', entry,\n make_binary(lineno, 'MINUS', substr, base, func=lambda x, y: x - y),\n make_binary(lineno, 'MINUS', substr, base, func=lambda x, y: x - y),\n p[6])", "response": "A substr assignment statement."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef p_substr_assignment(p):\n if p[3] is None or p[5] is None:\n return # There were errors\n\n p[0] = None\n entry = SYMBOL_TABLE.access_call(p[2], p.lineno(2))\n if entry is None:\n return\n\n if entry.class_ == CLASS.unknown:\n entry.class_ = CLASS.var\n\n assert entry.class_ == CLASS.var and entry.type_ == TYPE.string\n\n if p[5].type_ != TYPE.string:\n api.errmsg.syntax_error_expected_string(p.lineno(4), p[5].type_)\n\n if len(p[3]) > 1:\n syntax_error(p.lineno(2), \"Accessing string with too many indexes. Expected only one.\")\n return\n\n if len(p[3]) == 1:\n substr = (\n make_typecast(_TYPE(gl.STR_INDEX_TYPE), p[3][0].value, p.lineno(2)),\n make_typecast(_TYPE(gl.STR_INDEX_TYPE), p[3][0].value, p.lineno(2)))\n else:\n substr = (make_typecast(_TYPE(gl.STR_INDEX_TYPE),\n make_number(gl.MIN_STRSLICE_IDX,\n lineno=p.lineno(2)),\n p.lineno(2)),\n make_typecast(_TYPE(gl.STR_INDEX_TYPE),\n make_number(gl.MAX_STRSLICE_IDX,\n lineno=p.lineno(2)),\n p.lineno(2)))\n\n lineno = p.lineno(2)\n base = make_number(OPTIONS.string_base.value, lineno, _TYPE(gl.STR_INDEX_TYPE))\n p[0] = make_sentence('LETSUBSTR', entry,\n make_binary(lineno, 'MINUS', substr[0], base, func=lambda x, y: x - y),\n make_binary(lineno, 'MINUS', substr[1], base, func=lambda x, y: x - y),\n p[5])", "response": "Statement for substr assignment."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef p_str_assign(p):\n if p[1].upper() != 'LET':\n q = p[1]\n r = p[4]\n s = p[2]\n lineno = p.lineno(3)\n else:\n q = p[2]\n r = p[5]\n s = p[3]\n lineno = p.lineno(4)\n\n if q is None or s is None:\n p[0] = None\n return\n\n if r.type_ != TYPE.string:\n api.errmsg.syntax_error_expected_string(lineno, r.type_)\n\n entry = SYMBOL_TABLE.access_var(q, lineno, default_type=TYPE.string)\n if entry is None:\n p[0] = None\n return\n\n p[0] = make_sentence('LETSUBSTR', entry, s[0], s[1], r)", "response": "A string assignment statement."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_if_sentence(p):\n cond_ = p[1]\n if len(p) == 6:\n lbl = make_label(p[3], p.lineno(3))\n stat_ = make_block(lbl, p[4])\n endif_ = p[5]\n elif len(p) == 5:\n stat_ = p[3]\n endif_ = p[4]\n else:\n stat_ = make_nop()\n endif_ = p[3]\n\n p[0] = make_sentence('IF', cond_, make_block(stat_, endif_), lineno=p.lineno(2))", "response": "P an if statement."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nprocesses single - line IF statements.", "response": "def p_single_line_if(p):\n \"\"\" if_inline : if_then_part statements %prec ID\n | if_then_part co_statements_co %prec NEWLINE\n | if_then_part statements_co %prec NEWLINE\n | if_then_part co_statements %prec ID\n \"\"\"\n cond_ = p[1]\n stat_ = p[2]\n p[0] = make_sentence('IF', cond_, stat_, lineno=p.lineno(1))"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nprocesses the else - if - part of a statement.", "response": "def p_elseif_part(p):\n \"\"\" elseif_expr : ELSEIF expr then\n | LABEL ELSEIF expr then\n \"\"\"\n if p[1] == 'ELSEIF':\n label_ = make_nop() # No label\n cond_ = p[2]\n else:\n label_ = make_label(p[1], p.lineno(1))\n cond_ = p[3]\n\n p[0] = label_, cond_"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef p_elseif_list(p):\n label_, cond_ = p[1]\n then_ = p[2]\n else_ = p[3]\n\n if isinstance(else_, list): # it's an else part\n else_ = make_block(*else_)\n else:\n then_ = make_block(then_, else_)\n else_ = None\n\n p[0] = make_block(label_, make_sentence('IF', cond_, then_, else_, lineno=p.lineno(1)))", "response": "P 3. 1. 3. elseiflist"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef p_elseif_elseiflist(p):\n label_, cond_ = p[1]\n then_ = p[2]\n else_ = p[3]\n p[0] = make_block(label_, make_sentence('IF', cond_, then_, else_, lineno=p.lineno(1)))", "response": "P 3. 1. 6. If list"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef p_else_part_endif(p):\n if p[2] == '\\n':\n if len(p) == 4:\n p[0] = [make_nop(), p[3]]\n elif len(p) == 6:\n p[0] = [make_label(p[3], p.lineno(3)), p[4], p[5]]\n else:\n p[0] = [p[3], p[4]]\n else:\n p[0] = [p[2], p[3]]", "response": "P [ 1 ] else_part_inline"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef p_else_part_label(p):\n lbl = make_label(p[1], p.lineno(1))\n p[0] = [make_block(lbl, p[3]), p[4]]", "response": "Label the else part of a block."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef p_if_then_part(p):\n if is_number(p[2]):\n api.errmsg.warning_condition_is_always(p.lineno(1), bool(p[2].value))\n\n p[0] = p[2]", "response": "If expr then part"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_next1(p):\n if p[1] == 'NEXT':\n p1 = make_nop()\n p3 = p[2]\n else:\n p1 = make_label(p[1], p.lineno(1))\n p3 = p[3]\n\n if p3 != gl.LOOPS[-1][1]:\n api.errmsg.syntax_error_wrong_for_var(p.lineno(2), gl.LOOPS[-1][1], p3)\n p[0] = make_nop()\n return\n\n p[0] = p1", "response": "A function to be used in the loop loop."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nhandles FOR start and FOR statements.", "response": "def p_for_sentence_start(p):\n \"\"\" for_start : FOR ID EQ expr TO expr step\n \"\"\"\n gl.LOOPS.append(('FOR', p[2]))\n p[0] = None\n\n if p[4] is None or p[6] is None or p[7] is None:\n return\n\n if is_number(p[4], p[6], p[7]):\n if p[4].value != p[6].value and p[7].value == 0:\n warning(p.lineno(5), 'STEP value is 0 and FOR might loop forever')\n\n if p[4].value > p[6].value and p[7].value > 0:\n warning(p.lineno(5), 'FOR start value is greater than end. This FOR loop is useless')\n\n if p[4].value < p[6].value and p[7].value < 0:\n warning(p.lineno(2), 'FOR start value is lower than end. This FOR loop is useless')\n\n id_type = common_type(common_type(p[4], p[6]), p[7])\n variable = SYMBOL_TABLE.access_var(p[2], p.lineno(2), default_type=id_type)\n if variable is None:\n return\n\n variable.accessed = True\n expr1 = make_typecast(variable.type_, p[4], p.lineno(3))\n expr2 = make_typecast(variable.type_, p[6], p.lineno(5))\n expr3 = make_typecast(variable.type_, p[7], p.lexer.lineno)\n\n p[0] = make_sentence('FOR', variable, expr1, expr2, expr3)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef p_error_raise(p):\n q = make_number(1, lineno=p.lineno(2))\n r = make_binary(p.lineno(1), 'MINUS',\n make_typecast(TYPE.ubyte, p[2], p.lineno(1)), q,\n lambda x, y: x - y)\n p[0] = make_sentence('ERROR', r)", "response": "A statement that can be used to raise an error."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef p_stop_raise(p):\n q = make_number(9, lineno=p.lineno(1)) if len(p) == 2 else p[2]\n z = make_number(1, lineno=p.lineno(1))\n r = make_binary(p.lineno(1), 'MINUS',\n make_typecast(TYPE.ubyte, q, p.lineno(1)), z,\n lambda x, y: x - y)\n p[0] = make_sentence('STOP', r)", "response": "A stop raise statement."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef p_do_loop(p):\n if len(p) == 4:\n q = make_block(p[2], p[3])\n else:\n q = p[2]\n\n if p[1] == 'DO':\n gl.LOOPS.append(('DO',))\n\n if q is None:\n warning(p.lineno(1), 'Infinite empty loop')\n\n # An infinite loop and no warnings\n p[0] = make_sentence('DO_LOOP', q)\n gl.LOOPS.pop()", "response": "A do loop statement."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef p_read(p):\n gl.DATA_IS_USED = True\n reads = []\n\n if p[2] is None:\n return\n\n for arg in p[2]:\n entry = arg.value\n if entry is None:\n p[0] = None\n return\n\n if isinstance(entry, symbols.VARARRAY):\n api.errmsg.syntax_error(p.lineno(1), \"Cannot read '%s'. It's an array\" % entry.name)\n p[0] = None\n return\n\n if isinstance(entry, symbols.VAR):\n if entry.class_ != CLASS.var:\n api.errmsg.syntax_error_cannot_assing_not_a_var(p.lineno(2), entry.name)\n p[0] = None\n return\n\n entry.accessed = True\n if entry.type_ == TYPE.auto:\n entry.type_ = _TYPE(gl.DEFAULT_TYPE)\n api.errmsg.warning_implicit_type(p.lineno(2), p[2], entry.type_)\n\n reads.append(make_sentence('READ', entry))\n continue\n\n if isinstance(entry, symbols.ARRAYLOAD):\n reads.append(make_sentence('READ', symbols.ARRAYACCESS(entry.entry, entry.args, entry.lineno)))\n continue\n\n api.errmsg.syntax_error(p.lineno(1), \"Syntax error. Can only read a variable or an array element\")\n p[0] = None\n return\n\n p[0] = make_block(*reads)", "response": "parse a READ statement"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_do_loop_while(p):\n if len(p) == 6:\n q = make_block(p[2], p[3])\n r = p[5]\n else:\n q = p[2]\n r = p[4]\n\n if p[1] == 'DO':\n gl.LOOPS.append(('DO',))\n\n p[0] = make_sentence('DO_WHILE', r, q)\n gl.LOOPS.pop()\n\n if is_number(r):\n api.errmsg.warning_condition_is_always(p.lineno(3), bool(r.value))\n if q is None:\n api.errmsg.warning_empty_loop(p.lineno(3))", "response": "parse a DO loop while statement"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_do_while_loop(p):\n r = p[1]\n q = p[2]\n if q == 'LOOP':\n q = None\n\n p[0] = make_sentence('WHILE_DO', r, q)\n gl.LOOPS.pop()\n\n if is_number(r):\n api.errmsg.warning_condition_is_always(p.lineno(2), bool(r.value))", "response": "Process a while loop statement."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef p_while_sentence(p):\n gl.LOOPS.pop()\n q = make_block(p[2], p[3])\n\n if is_number(p[1]) and p[1].value:\n if q is None:\n warning(p[1].lineno, \"Condition is always true and leads to an infinite loop.\")\n else:\n warning(p[1].lineno, \"Condition is always true and might lead to an infinite loop.\")\n\n p[0] = make_sentence('WHILE', p[1], q)", "response": "Process a WHILE statement."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef p_exit(p):\n q = p[2]\n p[0] = make_sentence('EXIT_%s' % q)\n\n for i in gl.LOOPS:\n if q == i[0]:\n return\n\n syntax_error(p.lineno(1), 'Syntax Error: EXIT %s out of loop' % q)", "response": "EXIT WHILE DOCOME WHILE DOCOME FOR FOREXIT"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nparse an ATTR_LIST statement.", "response": "def p_attr(p):\n \"\"\" attr : OVER expr\n | INVERSE expr\n | INK expr\n | PAPER expr\n | BRIGHT expr\n | FLASH expr\n \"\"\"\n # ATTR_LIST are used by drawing commands: PLOT, DRAW, CIRCLE\n # BOLD and ITALIC are ignored by them, so we put them out of the\n # attr definition so something like DRAW BOLD 1; .... will raise\n # a syntax error\n p[0] = make_sentence(p[1] + '_TMP',\n make_typecast(TYPE.ubyte, p[2], p.lineno(1)))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef p_print_list(p):\n p[0] = p[1]\n p[0].eol = (p[3] is not None)\n\n if p[3] is not None:\n p[0].appendChild(p[3])", "response": "print_list is a list of lists of lists of dicts"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef p_print_list_comma(p):\n p[0] = p[1]\n p[0].eol = (p[3] is not None)\n p[0].appendChild(make_sentence('PRINT_COMMA'))\n\n if p[3] is not None:\n p[0].appendChild(p[3])", "response": "A function to parse the print_list_comma."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef p_on_goto(p):\n expr = make_typecast(TYPE.ubyte, p[2], p.lineno(1))\n p[0] = make_sentence('ON_' + p[3], expr, *p[4])", "response": "statement : ON expr goto label_list"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nparse a RETURN statement.", "response": "def p_return(p):\n \"\"\" statement : RETURN\n \"\"\"\n if not FUNCTION_LEVEL: # At less one level, otherwise, this return is from a GOSUB\n p[0] = make_sentence('RETURN')\n return\n\n if FUNCTION_LEVEL[-1].kind != KIND.sub:\n syntax_error(p.lineno(1), 'Syntax Error: Functions must RETURN a value, or use EXIT FUNCTION instead.')\n p[0] = None\n return\n\n p[0] = make_sentence('RETURN', FUNCTION_LEVEL[-1])"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef p_return_expr(p):\n if not FUNCTION_LEVEL: # At less one level\n syntax_error(p.lineno(1), 'Syntax Error: Returning value out of FUNCTION')\n p[0] = None\n return\n\n if FUNCTION_LEVEL[-1].kind is None: # This function was not correctly declared.\n p[0] = None\n return\n\n if FUNCTION_LEVEL[-1].kind != KIND.function:\n syntax_error(p.lineno(1), 'Syntax Error: SUBs cannot return a value')\n p[0] = None\n return\n\n if is_numeric(p[2]) and FUNCTION_LEVEL[-1].type_ == TYPE.string:\n syntax_error(p.lineno(2), 'Type Error: Function must return a string, not a numeric value')\n p[0] = None\n return\n\n if not is_numeric(p[2]) and FUNCTION_LEVEL[-1].type_ != TYPE.string:\n syntax_error(p.lineno(2), 'Type Error: Function must return a numeric value, not a string')\n p[0] = None\n return\n\n p[0] = make_sentence('RETURN', FUNCTION_LEVEL[-1],\n make_typecast(FUNCTION_LEVEL[-1].type_, p[2],\n p.lineno(1)))", "response": "Parse a RETURN statement."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_poke2(p):\n i = 2 if isinstance(p[2], Symbol) or p[2] is None else 3\n if p[i + 1] is None or p[i + 3] is None:\n p[0] = None\n return\n p[0] = make_sentence('POKE',\n make_typecast(TYPE.uinteger, p[i + 1], p.lineno(i + 2)),\n make_typecast(p[i], p[i + 3], p.lineno(i + 3))\n )", "response": "statement : POKE numbertype expr COMMA expr\n | POKE LP numbertype expr COMMA expr RP"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nparses a simple instruction.", "response": "def p_simple_instruction(p):\n \"\"\" statement : ITALIC expr\n | BOLD expr\n | INK expr\n | PAPER expr\n | BRIGHT expr\n | FLASH expr\n | OVER expr\n | INVERSE expr\n \"\"\"\n p[0] = make_sentence(p[1], make_typecast(TYPE.ubyte, p[2], p.lineno(1)))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nparsing load code statement", "response": "def p_load_code(p):\n \"\"\" statement : load_or_verify expr ID\n | load_or_verify expr CODE\n | load_or_verify expr CODE expr\n | load_or_verify expr CODE expr COMMA expr\n \"\"\"\n if p[2].type_ != TYPE.string:\n api.errmsg.syntax_error_expected_string(p.lineno(3), p[2].type_)\n\n if len(p) == 4:\n if p[3].upper() not in ('SCREEN', 'SCREEN$', 'CODE'):\n syntax_error(p.lineno(3), 'Unexpected \"%s\" ID. Expected \"SCREEN$\" instead' % p[3])\n return None\n else:\n if p[3].upper() == 'CODE': # LOAD \"...\" CODE\n start = make_number(0, lineno=p.lineno(3))\n length = make_number(0, lineno=p.lineno(3))\n else: # SCREEN$\n start = make_number(16384, lineno=p.lineno(3))\n length = make_number(6912, lineno=p.lineno(3))\n else:\n start = make_typecast(TYPE.uinteger, p[4], p.lineno(3))\n\n if len(p) == 5:\n length = make_number(0, lineno=p.lineno(3))\n else:\n length = make_typecast(TYPE.uinteger, p[6], p.lineno(5))\n\n p[0] = make_sentence(p[1], p[2], start, length)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef p_expr_div_expr(p):\n p[0] = make_binary(p.lineno(2), 'DIV', p[1], p[3], lambda x, y: x / y)", "response": "expr : expr DIV expr"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef p_expr_mod_expr(p):\n p[0] = make_binary(p.lineno(2), 'MOD', p[1], p[3], lambda x, y: x % y)", "response": "expr : expr MOD expr"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_expr_pow_expr(p):\n p[0] = make_binary(p.lineno(2), 'POW',\n make_typecast(TYPE.float_, p[1], p.lineno(2)),\n make_typecast(TYPE.float_, p[3], p.lexer.lineno),\n lambda x, y: x ** y)", "response": "expr = expr POW expr"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef p_expr_shl_expr(p):\n if p[1] is None or p[3] is None:\n p[0] = None\n return\n\n if p[1].type_ in (TYPE.float_, TYPE.fixed):\n p[1] = make_typecast(TYPE.ulong, p[1], p.lineno(2))\n\n p[0] = make_binary(p.lineno(2), 'SHL', p[1],\n make_typecast(TYPE.ubyte, p[3], p.lineno(2)),\n lambda x, y: x << y)", "response": "P expr SHL expr"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_expr_LT_expr(p):\n p[0] = make_binary(p.lineno(2), 'LT', p[1], p[3], lambda x, y: x < y)", "response": "expr : expr LT expr"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef p_expr_GT_expr(p):\n p[0] = make_binary(p.lineno(2), 'GT', p[1], p[3], lambda x, y: x > y)", "response": "expr : expr GT expr"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef p_expr_GE_expr(p):\n p[0] = make_binary(p.lineno(2), 'GE', p[1], p[3], lambda x, y: x >= y)", "response": "expr : expr GE expr"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef p_expr_NE_expr(p):\n p[0] = make_binary(p.lineno(2), 'NE', p[1], p[3], lambda x, y: x != y)", "response": "expr : expr NE expr"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_expr_OR_expr(p):\n p[0] = make_binary(p.lineno(2), 'OR', p[1], p[3], lambda x, y: x or y)", "response": "expr : expr OR expr"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef p_expr_BOR_expr(p):\n p[0] = make_binary(p.lineno(2), 'BOR', p[1], p[3], lambda x, y: x | y)", "response": "expr : expr BOR expr"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef p_expr_XOR_expr(p):\n p[0] = make_binary(p.lineno(2), 'XOR', p[1], p[3], lambda x, y: (x and not y) or (not x and y))", "response": "expr : expr XOR expr"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_expr_AND_expr(p):\n p[0] = make_binary(p.lineno(2), 'AND', p[1], p[3], lambda x, y: x and y)", "response": "expr : expr AND expr"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef p_expr_BAND_expr(p):\n p[0] = make_binary(p.lineno(2), 'BAND', p[1], p[3], lambda x, y: x & y)", "response": "expr : expr BAND expr"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef p_expr_PI(p):\n p[0] = make_number(PI, lineno=p.lineno(1), type_=TYPE.float_)", "response": "A function to generate the PI expression."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_expr_id_substr(p):\n entry = SYMBOL_TABLE.access_var(p[1], p.lineno(1), default_type=TYPE.string)\n p[0] = None\n if entry is None:\n return\n\n entry.accessed = True\n p[0] = make_strslice(p.lineno(1), entry, p[2][0], p[2][1])", "response": "string : ID substr"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nstring : LP expr RP substr", "response": "def p_string_expr_lp(p):\n \"\"\" string : LP expr RP substr\n \"\"\"\n if p[2].type_ != TYPE.string:\n syntax_error(p.lexer.lineno,\n \"Expected a string type expression. \"\n \"Got %s type instead\" % TYPE.to_string(p[2].type_))\n p[0] = None\n else:\n p[0] = make_strslice(p.lexer.lineno, p[2], p[4][0], p[4][1])"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef p_subind_str(p):\n p[0] = (make_typecast(TYPE.uinteger, p[2], p.lineno(1)),\n make_typecast(TYPE.uinteger, p[4], p.lineno(3)))", "response": "substr : LP expr TO expr RP"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_subind_TO(p):\n p[0] = (make_typecast(TYPE.uinteger,\n make_number(0, lineno=p.lineno(2)),\n p.lineno(1)),\n make_typecast(TYPE.uinteger,\n make_number(gl.MAX_STRSLICE_IDX, lineno=p.lineno(3)),\n p.lineno(2)))", "response": "Subind to LP TO RP"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef p_addr_of_id(p):\n entry = SYMBOL_TABLE.access_id(p[2], p.lineno(2))\n if entry is None:\n p[0] = None\n return\n\n entry.accessed = True\n result = make_unary(p.lineno(1), 'ADDRESS', entry, type_=_TYPE(gl.PTR_TYPE))\n\n if is_dynamic(entry):\n p[0] = result\n else:\n p[0] = make_constexpr(p.lineno(1), result)", "response": "bexpr : ADDRESSOF ID\n | ADDRESSOF ARRAY_ID\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nhandle the ID call expression.", "response": "def p_idcall_expr(p):\n \"\"\" func_call : ID arg_list %prec UMINUS\n \"\"\" # This can be a function call or a string index\n p[0] = make_call(p[1], p.lineno(1), p[2])\n if p[0] is None:\n return\n\n if p[0].token in ('STRSLICE', 'VAR', 'STRING'):\n entry = SYMBOL_TABLE.access_call(p[1], p.lineno(1))\n entry.accessed = True\n return\n\n # TODO: Check that arrays really needs kind=function to be set\n # Both array accesses and functions are tagged as functions\n # functions also has the class_ attribute set to 'function'\n p[0].entry.set_kind(KIND.function, p.lineno(1))\n p[0].entry.accessed = True"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef p_let_arr_substr(p):\n i = 2 if p[1].upper() == 'LET' else 1\n\n id_ = p[i]\n arg_list = p[i + 1]\n substr = p[i + 2]\n expr_ = p[i + 4]\n p[0] = make_array_substr_assign(p.lineno(i), id_, arg_list, substr, expr_)", "response": "P_let_arr_substr is a helper function for inserting a substr statement in a LET ARRAY_ID arg_list substr EQ expr\n | ARRAY_ID substr EQ expr\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_let_arr_substr_in_args4(p):\n i = 2 if p[1].upper() == 'LET' else 1\n\n id_ = p[i]\n arg_list = p[i + 2]\n substr = (arg_list.children.pop().value, p[i + 4])\n expr_ = p[i + 7]\n p[0] = make_array_substr_assign(p.lineno(i), id_, arg_list, substr, expr_)", "response": "A let arr substr in args 4."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_addr_of_array_element(p):\n p[0] = None\n\n if p[3] is None:\n return\n\n result = make_array_access(p[2], p.lineno(2), p[3])\n if result is None:\n return\n\n result.entry.accessed = True\n p[0] = make_unary(p.lineno(1), 'ADDRESS', result, type_=_TYPE(gl.PTR_TYPE))", "response": "A function to create an array access."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef p_bexpr_func(p):\n args = make_arg_list(make_argument(p[2], p.lineno(2)))\n p[0] = make_call(p[1], p.lineno(1), args)\n if p[0] is None:\n return\n\n if p[0].token in ('STRSLICE', 'VAR', 'STRING'):\n entry = SYMBOL_TABLE.access_call(p[1], p.lineno(1))\n entry.accessed = True\n return\n\n # TODO: Check that arrays really needs kind=function to be set\n # Both array accesses and functions are tagged as functions\n # functions also has the class_ attribute set to 'function'\n p[0].entry.set_kind(KIND.function, p.lineno(1))\n p[0].entry.accessed = True", "response": "A function is a function call."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef p_funcdecl(p):\n if p[1] is None:\n p[0] = None\n return\n\n p[0] = p[1]\n p[0].local_symbol_table = SYMBOL_TABLE.table[SYMBOL_TABLE.current_scope]\n p[0].locals_size = SYMBOL_TABLE.leave_scope()\n FUNCTION_LEVEL.pop()\n p[0].entry.body = p[2]\n\n entry = p[0].entry\n entry.forwarded = False", "response": "function_declaration : function_header function_body"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef p_funcdeclforward(p):\n if p[2] is None:\n if FUNCTION_LEVEL:\n FUNCTION_LEVEL.pop()\n return\n\n if p[2].entry.forwarded:\n syntax_error(p.lineno(1), \"duplicated declaration for function '%s'\" % p[2].name)\n\n p[2].entry.forwarded = True\n SYMBOL_TABLE.leave_scope()\n FUNCTION_LEVEL.pop()", "response": "function declaration is forward"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_param_definition(p):\n p[0] = p[1]\n if p[0] is not None:\n p[0].byref = OPTIONS.byref.value", "response": "param_definition is a param_definition object that is passed to the API."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef p_function_body(p):\n if not FUNCTION_LEVEL:\n syntax_error(p.lineno(3), \"Unexpected token 'END %s'. No Function or Sub has been defined.\" % p[2])\n p[0] = None\n return\n\n a = FUNCTION_LEVEL[-1].kind\n if a not in (KIND.sub, KIND.function): # This function/sub was not correctly declared, so exit now\n p[0] = None\n return\n\n i = 2 if p[1] == 'END' else 3\n b = p[i].lower()\n\n if a != b:\n syntax_error(p.lineno(i), \"Unexpected token 'END %s'. Should be 'END %s'\" % (b.upper(), a.upper()))\n p[0] = None\n else:\n p[0] = None if p[1] == 'END' else p[1]", "response": "Parse the function body."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_expr_usr(p):\n if p[2].type_ == TYPE.string:\n p[0] = make_builtin(p.lineno(1), 'USR_STR', p[2], type_=TYPE.uinteger)\n else:\n p[0] = make_builtin(p.lineno(1), 'USR',\n make_typecast(TYPE.uinteger, p[2], p.lineno(1)),\n type_=TYPE.uinteger)", "response": "bexpr : USR bexpr %prec UMINUS"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef p_expr_peek(p):\n p[0] = make_builtin(p.lineno(1), 'PEEK',\n make_typecast(TYPE.uinteger, p[2], p.lineno(1)),\n type_=TYPE.ubyte)", "response": "bexpr : PEEK bexpr %prec UMINUS"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_expr_lbound(p):\n entry = SYMBOL_TABLE.access_array(p[3], p.lineno(3))\n if entry is None:\n p[0] = None\n return\n\n entry.accessed = True\n\n if p[1] == 'LBOUND':\n p[0] = make_number(entry.bounds[OPTIONS.array_base.value].lower,\n p.lineno(3), TYPE.uinteger)\n else:\n p[0] = make_number(entry.bounds[OPTIONS.array_base.value].upper,\n p.lineno(3), TYPE.uinteger)", "response": "A function to handle LBOUND and UBOUND expressions."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_expr_lbound_expr(p):\n entry = SYMBOL_TABLE.access_array(p[3], p.lineno(3))\n if entry is None:\n p[0] = None\n return\n\n entry.accessed = True\n num = make_typecast(TYPE.uinteger, p[5], p.lineno(6))\n\n if is_number(num):\n if num.value == 0: # 0 => Number of dims\n p[0] = make_number(len(entry.bounds), p.lineno(3), TYPE.uinteger)\n return\n\n val = num.value - 1\n if val < 0 or val >= len(entry.bounds):\n syntax_error(p.lineno(6), \"Dimension out of range\")\n p[0] = None\n return\n\n if p[1] == 'LBOUND':\n p[0] = make_number(entry.bounds[val].lower, p.lineno(3), TYPE.uinteger)\n else:\n p[0] = make_number(entry.bounds[val].upper, p.lineno(3), TYPE.uinteger)\n return\n\n if p[1] == 'LBOUND':\n entry.lbound_used = True\n else:\n entry.ubound_used = True\n\n p[0] = make_builtin(p.lineno(1), p[1], [entry, num], type_=TYPE.uinteger)", "response": "p = parse_expr_lbound_expr ( p )"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_len(p):\n arg = p[2]\n if arg is None:\n p[0] = None\n elif isinstance(arg, symbols.VAR) and arg.class_ == CLASS.array:\n p[0] = make_number(len(arg.bounds), lineno=p.lineno(1)) # Do constant folding\n elif arg.type_ != TYPE.string:\n api.errmsg.syntax_error_expected_string(p.lineno(1), TYPE.to_string(arg.type_))\n p[0] = None\n elif is_string(arg): # Constant string?\n p[0] = make_number(len(arg.value), lineno=p.lineno(1)) # Do constant folding\n else:\n p[0] = make_builtin(p.lineno(1), 'LEN', arg, type_=TYPE.uinteger)", "response": "Parse the LEN expression."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef p_str(p):\n if is_number(p[2]): # A constant is converted to string directly\n p[0] = symbols.STRING(str(p[2].value), p.lineno(1))\n else:\n p[0] = make_builtin(p.lineno(1), 'STR',\n make_typecast(TYPE.float_, p[2], p.lineno(1)),\n type_=TYPE.string)", "response": "%prec UMINUS\n string expr %prec UMINUS\n string"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_chr_one(p):\n arg_list = make_arg_list(make_argument(p[2], p.lineno(1)))\n arg_list[0].value = make_typecast(TYPE.ubyte, arg_list[0].value, p.lineno(1))\n p[0] = make_builtin(p.lineno(1), 'CHR', arg_list, type_=TYPE.string)", "response": "\\ x1b [ 1mNAME \\ x1b [ 0m chr - 1 - > CHR"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nstrings : CHR arg_list", "response": "def p_chr(p):\n \"\"\" string : CHR arg_list\n \"\"\"\n if len(p[2]) < 1:\n syntax_error(p.lineno(1), \"CHR$ function need at less 1 parameter\")\n p[0] = None\n return\n\n for i in range(len(p[2])): # Convert every argument to 8bit unsigned\n p[2][i].value = make_typecast(TYPE.ubyte, p[2][i].value, p.lineno(1))\n\n p[0] = make_builtin(p.lineno(1), 'CHR', p[2], type_=TYPE.string)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef p_code(p):\n\n def asc(x):\n if len(x):\n return ord(x[0])\n\n return 0\n\n if p[2] is None:\n p[0] = None\n return\n\n if p[2].type_ != TYPE.string:\n api.errmsg.syntax_error_expected_string(p.lineno(1), TYPE.to_string(p[2].type_))\n p[0] = None\n else:\n p[0] = make_builtin(p.lineno(1), 'CODE', p[2], lambda x: asc(x), type_=TYPE.ubyte)", "response": "Code parser for internal use."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef make_node(lower, upper, lineno):\n if not is_static(lower, upper):\n syntax_error(lineno, 'Array bounds must be constants')\n return None\n\n if isinstance(lower, SymbolVAR):\n lower = lower.value\n if isinstance(upper, SymbolVAR):\n upper = upper.value\n\n lower.value = int(lower.value)\n upper.value = int(upper.value)\n\n if lower.value < 0:\n syntax_error(lineno, 'Array bounds must be greater than 0')\n return None\n\n if lower.value > upper.value:\n syntax_error(lineno, 'Lower array bound must be less or equal to upper one')\n return None\n\n return SymbolBOUND(lower.value, upper.value)", "response": "Creates a node that is a range of the array."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a number for a BASIC program. That is the number of the BASIC log file.", "response": "def number(self, number):\n \"\"\" Returns a floating point (or integer) number for a BASIC\n program. That is: It's ASCII representation followed by 5 bytes\n in floating point or integer format (if number in (-65535 + 65535)\n \"\"\"\n s = [ord(x) for x in str(number)] + [14] # Bytes of string representation in bytes\n\n if number == int(number) and abs(number) < 65536: # integer form?\n sign = 0xFF if number < 0 else 0\n b = [0, sign] + self.numberLH(number) + [0]\n else: # Float form\n (C, ED, LH) = fp.immediate_float(number)\n C = C[:2] # Remove 'h'\n ED = ED[:4] # Remove 'h'\n LH = LH[:4] # Remove 'h'\n\n b = [int(C, 16)] # Convert to BASE 10\n b += [int(ED[:2], 16), int(ED[2:], 16)]\n b += [int(LH[:2], 16), int(LH[2:], 16)]\n\n return s + b"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nparses a given string into a list of BASIC commands and the remaining strings.", "response": "def parse_sentence(self, string):\n \"\"\" Parses the given sentence. BASIC commands must be\n types UPPERCASE and as SEEN in ZX BASIC. e.g. GO SUB for gosub, etc...\n \"\"\"\n\n result = []\n\n def shift(string_):\n \"\"\" Returns first word of a string, and remaining\n \"\"\"\n string_ = string_.strip() # Remove spaces and tabs\n\n if not string_: # Avoid empty strings\n return '', ''\n\n i = string_.find(' ')\n if i == -1:\n command_ = string_\n string_ = ''\n else:\n command_ = string_[:i]\n string_ = string_[i:]\n\n return command_, string_\n\n command, string = shift(string)\n while command != '':\n result += self.token(command)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the bytes of a sentence.", "response": "def sentence_bytes(self, sentence):\n \"\"\" Return bytes of a sentence.\n This is a very simple parser. Sentence is a list of strings and numbers.\n 1st element of sentence MUST match a token.\n \"\"\"\n result = [TOKENS[sentence[0]]]\n\n for i in sentence[1:]: # Remaining bytes\n if isinstance(i, str):\n result.extend(self.literal(i))\n elif isinstance(i, float) or isinstance(i, int): # A number?\n result.extend(self.number(i))\n else:\n result.extend(i) # Must be another thing\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the bytes for a basic line.", "response": "def line(self, sentences, line_number=None):\n \"\"\" Return the bytes for a basic line.\n If no line number is given, current one + 10 will be used\n Sentences if a list of sentences\n \"\"\"\n if line_number is None:\n line_number = self.current_line + 10\n self.current_line = line_number\n\n sep = []\n result = []\n for sentence in sentences:\n result.extend(sep)\n result.extend(self.sentence_bytes(sentence))\n sep = [ord(':')]\n\n result.extend([ENTER])\n result = self.line_number(line_number) + self.numberLH(len(result)) + result\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nadding a line to the output.", "response": "def add_line(self, sentences, line_number=None):\n \"\"\" Add current line to the output.\n See self.line() for more info\n \"\"\"\n self.bytes += self.line(sentences, line_number)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the offset in bytes of the base memory of the current object.", "response": "def offset(self):\n \"\"\" If this is a constant access (e.g. A(1))\n return the offset in bytes from the beginning of the\n variable in memory.\n\n Otherwise, if it's not constant (e.g. A(i))\n returns None\n \"\"\"\n offset = 0\n # Now we must typecast each argument to a u16 (POINTER) type\n # i is the dimension ith index, b is the bound\n for i, b in zip(self.arglist, self.entry.bounds):\n tmp = i.children[0]\n if is_number(tmp) or is_const(tmp):\n if offset is not None:\n offset = offset * b.count + tmp.value\n else:\n offset = None\n break\n\n if offset is not None:\n offset = TYPE.size(gl.SIZE_TYPE) + TYPE.size(gl.BOUND_TYPE) * len(self.arglist) + offset * self.type_.size\n\n return offset"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncreates an array access. A node.", "response": "def make_node(cls, id_, arglist, lineno):\n \"\"\" Creates an array access. A(x1, x2, ..., xn)\n \"\"\"\n assert isinstance(arglist, SymbolARGLIST)\n variable = gl.SYMBOL_TABLE.access_array(id_, lineno)\n if variable is None:\n return None\n\n if len(variable.bounds) != len(arglist):\n syntax_error(lineno, \"Array '%s' has %i dimensions, not %i\" %\n (variable.name, len(variable.bounds), len(arglist)))\n return None\n\n # Checks for array subscript range if the subscript is constant\n # e.g. A(1) is a constant subscript access\n for i, b in zip(arglist, variable.bounds):\n btype = gl.SYMBOL_TABLE.basic_types[gl.BOUND_TYPE]\n lower_bound = NUMBER(b.lower, type_=btype, lineno=lineno)\n i.value = BINARY.make_node('MINUS',\n TYPECAST.make_node(btype, i.value, lineno),\n lower_bound, lineno, func=lambda x, y: x - y,\n type_=btype)\n if is_number(i.value) or is_const(i.value):\n val = i.value.value\n if val < 0 or val > b.count:\n warning(lineno, \"Array '%s' subscript out of range\" % id_)\n\n # Returns the variable entry and the node\n return cls(variable, arglist, lineno)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef t_INITIAL_COMMENT(self, t):\n r';'\n t.lexer.push_state('asmcomment')\n t.type = 'TOKEN'\n t.value = ';'\n return t", "response": "r This is the initial comment token."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nlook for preprocessor ID directives and set the type to ID", "response": "def t_prepro_ID(self, t):\n r'[_a-zA-Z][_a-zA-Z0-9]*' # preprocessor directives\n t.type = reserved_directives.get(t.value.lower(), 'ID')\n if t.type == 'DEFINE':\n t.lexer.begin('define')\n elif t.type == 'PRAGMA':\n t.lexer.begin('pragma')\n return t"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef put_current_line(self, prefix=''):\n return '%s#line %i \"%s\"\\n' % (prefix, self.lex.lineno, os.path.basename(self.filestack[-1][0]))", "response": "Returns line and file for include sequences."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nincluding a file into the internal state.", "response": "def include(self, filename):\n \"\"\" Changes FILENAME and line count\n \"\"\"\n if filename != STDIN and filename in [x[0] for x in self.filestack]: # Already included?\n self.warning(' Recursive inclusion')\n\n self.filestack.append([filename, 1, self.lex, self.input_data])\n self.lex = lex.lex(object=self)\n result = self.put_current_line() # First #line start with \\n (EOL)\n\n try:\n if filename == STDIN:\n self.input_data = sys.stdin.read()\n else:\n self.input_data = api.utils.read_txt_file(filename)\n\n if len(self.input_data) and self.input_data[-1] != EOL:\n self.input_data += EOL\n except IOError:\n self.input_data = EOL\n\n self.lex.input(self.input_data)\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_alias(self, entry):\n assert isinstance(entry, SymbolVAR)\n self.aliased_by.append(entry)", "response": "Adds an alias to the current list aliased_by"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef make_alias(self, entry):\n entry.add_alias(self)\n self.alias = entry\n self.scope = entry.scope # Local aliases can be \"global\" (static)\n self.byref = entry.byref\n self.offset = entry.offset\n self.addr = entry.addr", "response": "Make this variable an alias of another one."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nconvert a var_instance to a label one", "response": "def to_label(var_instance):\n \"\"\" Converts a var_instance to a label one\n \"\"\"\n # This can be done 'cause LABEL is just a dummy descent of VAR\n assert isinstance(var_instance, SymbolVAR)\n from symbols import LABEL\n var_instance.__class__ = LABEL\n var_instance.class_ = CLASS.label\n var_instance._scope_owner = []\n return var_instance"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nconvert a var_instance to a function one", "response": "def to_function(var_instance, lineno=None):\n \"\"\" Converts a var_instance to a function one\n \"\"\"\n assert isinstance(var_instance, SymbolVAR)\n from symbols import FUNCTION\n var_instance.__class__ = FUNCTION\n var_instance.class_ = CLASS.function\n var_instance.reset(lineno=lineno)\n return var_instance"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef to_vararray(var_instance, bounds):\n assert isinstance(var_instance, SymbolVAR)\n from symbols import BOUNDLIST\n from symbols import VARARRAY\n assert isinstance(bounds, BOUNDLIST)\n var_instance.__class__ = VARARRAY\n var_instance.class_ = CLASS.array\n var_instance.bounds = bounds\n return var_instance", "response": "Converts a var_instance to a var array one\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _paddr(ins):\n output = []\n\n oper = ins.quad[1]\n indirect = (oper[0] == '*')\n if indirect:\n oper = oper[1:]\n\n I = int(oper)\n if I >= 0:\n I += 4 # Return Address + \"push IX\"\n\n output.append('push ix')\n output.append('pop hl')\n output.append('ld de, %i' % I)\n output.append('add hl, de')\n\n if indirect:\n output.append('ld e, (hl)')\n output.append('inc hl')\n output.append('ld h, (hl)')\n output.append('ld l, e')\n\n output.append('push hl')\n return output", "response": "Returns code sequence which points to\n local variable or parameter HL"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _pload(offset, size):\n output = []\n\n indirect = offset[0] == '*'\n if indirect:\n offset = offset[1:]\n\n I = int(offset)\n if I >= 0: # If it is a parameter, round up to even bytes\n I += 4 + (size % 2 if not indirect else 0) # Return Address + \"push IX\"\n\n ix_changed = (indirect or size < 5) and (abs(I) + size) > 127 # Offset > 127 bytes. Need to change IX\n if ix_changed: # more than 1 byte\n output.append('push ix')\n output.append('ld de, %i' % I)\n output.append('add ix, de')\n I = 0\n elif size == 5: # For floating point numbers we always use DE as IX offset\n output.append('push ix')\n output.append('pop hl')\n output.append('ld de, %i' % I)\n output.append('add hl, de')\n I = 0\n\n if indirect:\n output.append('ld h, (ix%+i)' % (I + 1))\n output.append('ld l, (ix%+i)' % I)\n\n if size == 1:\n output.append('ld a, (hl)')\n elif size == 2:\n output.append('ld c, (hl)')\n output.append('inc hl')\n output.append('ld h, (hl)')\n output.append('ld l, c')\n elif size == 4:\n output.append('call __ILOAD32')\n REQUIRES.add('iload32.asm')\n else: # Floating point\n output.append('call __ILOADF')\n REQUIRES.add('iloadf.asm')\n else:\n if size == 1:\n output.append('ld a, (ix%+i)' % I)\n else:\n if size <= 4: # 16/32bit integer, low part\n output.append('ld l, (ix%+i)' % I)\n output.append('ld h, (ix%+i)' % (I + 1))\n\n if size > 2: # 32 bit integer, high part\n output.append('ld e, (ix%+i)' % (I + 2))\n output.append('ld d, (ix%+i)' % (I + 3))\n\n else: # Floating point\n output.append('call __PLOADF')\n REQUIRES.add('ploadf.asm')\n\n if ix_changed:\n output.append('pop ix')\n\n return output", "response": "Generic parameter loading.\n Emmits output code for setting IX at the right location.\n size = Number of bytes to load:\n 1 => 8 bit value\n 2 => 16 bit value / string\n 4 => 32 bit value / f16 value\n 5 => 40 bit value"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _pload32(ins):\n output = _pload(ins.quad[2], 4)\n output.append('push de')\n output.append('push hl')\n return output", "response": "Loads from stack pointer X 2nd parameter."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _ploadf(ins):\n output = _pload(ins.quad[2], 5)\n output.extend(_fpush())\n return output", "response": "Loads from stack pointer SP + X 2nd parameter."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _ploadstr(ins):\n output = _pload(ins.quad[2], 2)\n if ins.quad[1][0] != '$':\n output.append('call __LOADSTR')\n REQUIRES.add('loadstr.asm')\n\n output.append('push hl')\n return output", "response": "Loads from stack pointer X 2nd parameter X being\n X 2nd operand X being\n X 2nd parameter X being\n X 2nd operand X 2nd operand X"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nloading from stack pointer SP + X 2nd parameter.", "response": "def _fploadstr(ins):\n \"\"\" Loads from stack pointer (SP) + X, being\n X 2st parameter.\n\n 1st operand must be a SIGNED integer.\n Unlike ploadstr, this version does not push the result\n back into the stack.\n \"\"\"\n output = _pload(ins.quad[2], 2)\n if ins.quad[1][0] != '$':\n output.append('call __LOADSTR')\n REQUIRES.add('loadstr.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nstore 2nd parameter at stack pointer X 1st parameter.", "response": "def _pstore8(ins):\n \"\"\" Stores 2nd parameter at stack pointer (SP) + X, being\n X 1st parameter.\n\n 1st operand must be a SIGNED integer.\n \"\"\"\n value = ins.quad[2]\n offset = ins.quad[1]\n indirect = offset[0] == '*'\n size = 0\n if indirect:\n offset = offset[1:]\n size = 1\n\n I = int(offset)\n if I >= 0:\n I += 4 # Return Address + \"push IX\"\n if not indirect:\n I += 1 # F flag ignored\n\n if is_int(value):\n output = []\n else:\n output = _8bit_oper(value)\n\n ix_changed = not (-128 + size <= I <= 127 - size) # Offset > 127 bytes. Need to change IX\n if ix_changed: # more than 1 byte\n output.append('push ix')\n output.append('pop hl')\n output.append('ld de, %i' % I)\n output.append('add hl, de')\n\n if indirect:\n if ix_changed:\n output.append('ld c, (hl)')\n output.append('inc hl')\n output.append('ld h, (hl)')\n output.append('ld l, c')\n else:\n output.append('ld h, (ix%+i)' % (I + 1))\n output.append('ld l, (ix%+i)' % I)\n\n if is_int(value):\n output.append('ld (hl), %i' % int8(value))\n else:\n output.append('ld (hl), a')\n\n return output\n\n # direct store\n if ix_changed:\n if is_int(value):\n output.append('ld (hl), %i' % int8(value))\n else:\n output.append('ld (hl), a')\n\n return output\n\n if is_int(value):\n output.append('ld (ix%+i), %i' % (I, int8(value)))\n else:\n output.append('ld (ix%+i), a' % I)\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _pstore16(ins):\n value = ins.quad[2]\n offset = ins.quad[1]\n indirect = offset[0] == '*'\n size = 1\n if indirect:\n offset = offset[1:]\n\n I = int(offset)\n if I >= 0:\n I += 4 # Return Address + \"push IX\"\n\n if is_int(value):\n output = []\n else:\n output = _16bit_oper(value)\n\n ix_changed = not (-128 + size <= I <= 127 - size) # Offset > 127 bytes. Need to change IX\n\n if indirect:\n if is_int(value):\n output.append('ld hl, %i' % int16(value))\n\n output.append('ld bc, %i' % I)\n output.append('call __PISTORE16')\n REQUIRES.add('istore16.asm')\n return output\n\n # direct store\n if ix_changed: # more than 1 byte\n if not is_int(value):\n output.append('ex de, hl')\n\n output.append('push ix')\n output.append('pop hl')\n output.append('ld bc, %i' % I)\n output.append('add hl, bc')\n\n if is_int(value):\n v = int16(value)\n output.append('ld (hl), %i' % (v & 0xFF))\n output.append('inc hl')\n output.append('ld (hl), %i' % (v >> 8))\n return output\n else:\n output.append('ld (hl), e')\n output.append('inc hl')\n output.append('ld (hl), d')\n return output\n\n if is_int(value):\n v = int16(value)\n output.append('ld (ix%+i), %i' % (I, v & 0xFF))\n output.append('ld (ix%+i), %i' % (I + 1, v >> 8))\n else:\n output.append('ld (ix%+i), l' % I)\n output.append('ld (ix%+i), h' % (I + 1))\n\n return output", "response": "Stores 2nd parameter at stack pointer X 1st parameter."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nstore 2nd parameter at stack pointer X 1st parameter being X 1st operand must be a SIGNED integer. Returns Address + push IX", "response": "def _pstore32(ins):\n \"\"\" Stores 2nd parameter at stack pointer (SP) + X, being\n X 1st parameter.\n\n 1st operand must be a SIGNED integer.\n \"\"\"\n value = ins.quad[2]\n offset = ins.quad[1]\n indirect = offset[0] == '*'\n if indirect:\n offset = offset[1:]\n\n I = int(offset)\n if I >= 0:\n I += 4 # Return Address + \"push IX\"\n\n output = _32bit_oper(value)\n\n if indirect:\n output.append('ld bc, %i' % I)\n output.append('call __PISTORE32')\n REQUIRES.add('pistore32.asm')\n return output\n\n # direct store\n output.append('ld bc, %i' % I)\n output.append('call __PSTORE32')\n REQUIRES.add('pstore32.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nstoring 2nd parameter at stack pointer X 1st parameter being X 1st operand must be a SIGNED integer.", "response": "def _pstoref16(ins):\n \"\"\" Stores 2nd parameter at stack pointer (SP) + X, being\n X 1st parameter.\n\n 1st operand must be a SIGNED integer.\n \"\"\"\n value = ins.quad[2]\n offset = ins.quad[1]\n indirect = offset[0] == '*'\n if indirect:\n offset = offset[1:]\n\n I = int(offset)\n if I >= 0:\n I += 4 # Return Address + \"push IX\"\n\n output = _f16_oper(value)\n\n if indirect:\n output.append('ld bc, %i' % I)\n output.append('call __PISTORE32')\n REQUIRES.add('pistore32.asm')\n return output\n\n # direct store\n output.append('ld bc, %i' % I)\n output.append('call __PSTORE32')\n REQUIRES.add('pstore32.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _pstoref(ins):\n value = ins.quad[2]\n offset = ins.quad[1]\n indirect = offset[0] == '*'\n if indirect:\n offset = offset[1:]\n\n I = int(offset)\n if I >= 0:\n I += 4 # Return Address + \"push IX\"\n\n output = _float_oper(value)\n\n if indirect:\n output.append('ld hl, %i' % I)\n output.append('call __PISTOREF')\n REQUIRES.add('storef.asm')\n return output\n\n # direct store\n output.append('ld hl, %i' % I)\n output.append('call __PSTOREF')\n REQUIRES.add('pstoref.asm')\n\n return output", "response": "Stores 2nd parameter at stack pointer X 1st parameter being\n X 1st operand must be a SIGNED integer."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of strings representing the store string.", "response": "def _pstorestr(ins):\n \"\"\" Stores 2nd parameter at stack pointer (SP) + X, being\n X 1st parameter.\n\n 1st operand must be a SIGNED integer.\n\n Note: This procedure proceeds as _pstore16, since STRINGS are 16bit pointers.\n \"\"\"\n output = []\n temporal = False\n\n # 2nd operand first, because must go into the stack\n value = ins.quad[2]\n\n if value[0] == '*':\n value = value[1:]\n indirect = True\n else:\n indirect = False\n\n if value[0] == '_':\n output.append('ld de, (%s)' % value)\n\n if indirect:\n output.append('call __LOAD_DE_DE')\n REQUIRES.add('lddede.asm')\n\n elif value[0] == '#':\n output.append('ld de, %s' % value[1:])\n else:\n output.append('pop de')\n temporal = value[0] != '$'\n if indirect:\n output.append('call __LOAD_DE_DE')\n REQUIRES.add('lddede.asm')\n\n # Now 1st operand\n value = ins.quad[1]\n if value[0] == '*':\n value = value[1:]\n indirect = True\n else:\n indirect = False\n\n I = int(value)\n if I >= 0:\n I += 4 # Return Address + \"push IX\"\n\n output.append('ld bc, %i' % I)\n\n if not temporal:\n if indirect:\n output.append('call __PISTORE_STR')\n REQUIRES.add('storestr.asm')\n else:\n output.append('call __PSTORE_STR')\n REQUIRES.add('pstorestr.asm')\n else:\n if indirect:\n output.append('call __PISTORE_STR2')\n REQUIRES.add('storestr2.asm')\n else:\n output.append('call __PSTORE_STR2')\n REQUIRES.add('pstorestr2.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a node for a unary operation.", "response": "def make_node(cls, lineno, operator, operand, func=None, type_=None):\n \"\"\" Creates a node for a unary operation. E.g. -x or LEN(a$)\n\n Parameters:\n -func: lambda function used on constant folding when possible\n -type_: the resulting type (by default, the same as the argument).\n For example, for LEN (str$), result type is 'u16'\n and arg type is 'string'\n \"\"\"\n assert type_ is None or isinstance(type_, SymbolTYPE)\n\n if func is not None: # Try constant-folding\n if is_number(operand): # e.g. ABS(-5)\n return SymbolNUMBER(func(operand.value), lineno=lineno)\n elif is_string(operand): # e.g. LEN(\"a\")\n return SymbolSTRING(func(operand.text), lineno=lineno)\n\n if type_ is None:\n type_ = operand.type_\n\n if operator == 'MINUS':\n if not type_.is_signed:\n type_ = type_.to_signed()\n operand = SymbolTYPECAST.make_node(type_, operand, lineno)\n elif operator == 'NOT':\n type_ = TYPE.ubyte\n\n return cls(operator, operand, lineno, type_)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef to_signed(cls, type_):\n if type_ in cls.unsigned:\n return {TYPE.ubyte: TYPE.byte_,\n TYPE.uinteger: TYPE.integer,\n TYPE.ulong: TYPE.long_}[type_]\n if type_ in cls.decimals or type_ in cls.signed:\n return type_\n return cls.unknown", "response": "Return signed type or equivalent\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef to_type(cls, typename):\n NAME_TYPES = {cls.TYPE_NAMES[x]: x for x in cls.TYPE_NAMES}\n return NAME_TYPES.get(typename, None)", "response": "Converts a type ID to name. On error returns None. On error returns None."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a floating point operand converted to 32 bits unsigned int.", "response": "def f16(op):\n \"\"\" Returns a floating point operand converted to 32 bits unsigned int.\n Negative numbers are returned in 2 complement.\n\n The result is returned in a tuple (DE, HL) => High16 (Int part), Low16 (Decimal part)\n \"\"\"\n op = float(op)\n\n negative = op < 0\n if negative:\n op = -op\n\n DE = int(op)\n HL = int((op - DE) * 2**16) & 0xFFFF\n DE &= 0xFFFF\n\n if negative: # Do C2\n DE ^= 0xFFFF\n HL ^= 0xFFFF\n\n DEHL = ((DE << 16) | HL) + 1\n HL = DEHL & 0xFFFF\n DE = (DEHL >> 16) & 0xFFFF\n\n return (DE, HL)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a pop sequence for 32 bits operands inversion calling __SWAP32.", "response": "def _f16_oper(op1, op2=None, useBC=False, reversed=False):\n \"\"\" Returns pop sequence for 32 bits operands\n 1st operand in HLDE, 2nd operand remains in the stack\n\n Now it does support operands inversion calling __SWAP32.\n\n However, if 1st operand is integer (immediate) or indirect, the stack\n will be rearranged, so it contains a 32 bit pushed parameter value for the\n subroutine to be called.\n\n If preserveHL is True, then BC will be used instead of HL for lower part\n for the 1st operand.\n \"\"\"\n output = []\n\n if op1 is not None:\n op1 = str(op1)\n\n if op2 is not None:\n op2 = str(op2)\n\n op = op2 if op2 is not None else op1\n\n float1 = False # whether op1 (2nd operand) is float\n\n indirect = (op[0] == '*')\n if indirect:\n op = op[1:]\n\n immediate = (op[0] == '#')\n if immediate:\n op = op[1:]\n\n hl = 'hl' if not useBC and not indirect else 'bc'\n\n if is_float(op):\n float1 = True\n op = float(op)\n\n if indirect:\n op = int(op) & 0xFFFF\n if immediate:\n output.append('ld hl, %i' % op)\n else:\n output.append('ld hl, (%i)' % op)\n\n output.append('call __ILOAD32')\n REQUIRES.add('iload32.asm')\n\n if preserveHL: # noqa TODO: it will fail\n output.append('ld b, h')\n output.append('ld c, l')\n else:\n DE, HL = f16(op)\n output.append('ld de, %i' % DE)\n output.append('ld %s, %i' % (hl, HL))\n else:\n if op[0] == '_':\n if immediate:\n output.append('ld %s, %s' % (hl, op))\n else:\n output.append('ld %s, (%s)' % (hl, op))\n else:\n output.append('pop %s' % hl)\n\n if indirect:\n output.append('call __ILOAD32')\n REQUIRES.add('iload32.asm')\n\n if preserveHL: # noqa TODO: it will fail\n output.append('ld b, h')\n output.append('ld c, l')\n else:\n if op[0] == '_':\n output.append('ld de, (%s + 2)' % op)\n else:\n output.append('pop de')\n\n if op2 is not None:\n op = op1\n\n indirect = (op[0] == '*')\n if indirect:\n op = op[1:]\n\n immediate = (op[0] == '#')\n if immediate:\n op = op[1:]\n\n if is_float(op):\n op = float(op)\n\n if indirect:\n op = int(op)\n output.append('exx')\n if immediate:\n output.append('ld hl, %i' % (op & 0xFFFF))\n else:\n output.append('ld hl, (%i)' % (op & 0xFFFF))\n\n output.append('call __ILOAD32')\n output.append('push de')\n output.append('push hl')\n output.append('exx')\n REQUIRES.add('iload32.asm')\n else:\n DE, HL = f16(op)\n output.append('ld bc, %i' % DE)\n output.append('push bc')\n output.append('ld bc, %i' % HL)\n output.append('push bc')\n else:\n if indirect:\n output.append('exx') # uses alternate set to put it on the stack\n if op[0] == '_':\n if immediate:\n output.append('ld hl, %s' % op)\n else:\n output.append('ld hl, (%s)' % op)\n else:\n output.append('pop hl') # Pointers are only 16 bits ***\n\n output.append('call __ILOAD32')\n output.append('push de')\n output.append('push hl')\n output.append('exx')\n REQUIRES.add('iload32.asm')\n elif op[0] == '_': # an address\n if float1 or op1[0] == '_': # If previous op was constant, we can use hl in advance\n tmp = output\n output = []\n output.append('ld hl, (%s + 2)' % op)\n output.append('push hl')\n output.append('ld hl, (%s)' % op)\n output.append('push hl')\n output.extend(tmp)\n else:\n output.append('ld bc, (%s + 2)' % op)\n output.append('push bc')\n output.append('ld bc, (%s)' % op)\n output.append('push bc')\n else:\n pass # 2nd operand remains in the stack\n\n if op2 is not None and reversed:\n output.append('call __SWAP32')\n REQUIRES.add('swap32.asm')\n\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _f16_to_32bit(ins):\n ins.quad = [x for x in ins.quad]\n for i in range(2, len(ins.quad)):\n if is_float(ins.quad[i]):\n de, hl = f16(ins.quad[i])\n ins.quad[i] = str((de << 16) | hl)\n\n ins.quad = tuple(ins.quad)\n return ins", "response": "Convert a 16 bit float to 32bit integer representation."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _mulf16(ins):\n op1, op2 = tuple(ins.quad[2:])\n\n if _f_ops(op1, op2) is not None:\n op1, op2 = _f_ops(op1, op2)\n\n if op2 == 1: # A * 1 => A\n output = _f16_oper(op1)\n output.append('push de')\n output.append('push hl')\n return output\n\n if op2 == -1:\n return _neg32(ins)\n\n output = _f16_oper(op1)\n if op2 == 0:\n output.append('ld hl, 0')\n output.append('ld e, h')\n output.append('ld d, l')\n output.append('push de')\n output.append('push hl')\n return output\n\n output = _f16_oper(op1, str(op2))\n output.append('call __MULF16')\n output.append('push de')\n output.append('push hl')\n REQUIRES.add('mulf16.asm')\n return output", "response": "Multiplies 2 32bit fixed point numbers."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndivides 2 32bit fixed point numbers.", "response": "def _divf16(ins):\n \"\"\" Divides 2 32bit (16.16) fixed point numbers. The result is pushed onto the stack.\n\n Optimizations:\n\n * If 2nd operand is 1, do nothing\n * If 2nd operand is -1, do NEG32\n \"\"\"\n op1, op2 = tuple(ins.quad[2:])\n\n if is_float(op2):\n if float(op2) == 1:\n output = _f16_oper(op1)\n output.append('push de')\n output.append('push hl')\n return output\n\n if float(op2) == -1:\n return _negf(ins)\n\n rev = not is_float(op1) and op1[0] != 't' and op2[0] == 't'\n\n output = _f16_oper(op1, op2, reversed=rev)\n output.append('call __DIVF16')\n output.append('push de')\n output.append('push hl')\n REQUIRES.add('divf16.asm')\n return output"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nraising an exception raised when an invalid number of params in the given quad code has been emmitted.", "response": "def throw_invalid_quad_params(quad, QUADS, nparams):\n \"\"\" Exception raised when an invalid number of params in the\n quad code has been emmitted.\n \"\"\"\n raise InvalidICError(str(quad),\n \"Invalid quad code params for '%s' (expected %i, but got %i)\" %\n (quad, QUADS[quad][0], nparams)\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a floating point number as EXP + 128 Mantissa AttributeNames", "response": "def fp(x):\n \"\"\" Returns a floating point number as EXP+128, Mantissa\n \"\"\"\n\n def bin32(f):\n \"\"\" Returns ASCII representation for a 32 bit integer value\n \"\"\"\n result = ''\n a = int(f) & 0xFFFFFFFF # ensures int 32\n\n for i in range(32):\n result = str(a % 2) + result\n a = a >> 1\n\n return result\n\n def bindec32(f):\n \"\"\" Returns binary representation of a mantissa x (x is float)\n \"\"\"\n result = '0'\n a = f\n\n if f >= 1:\n result = bin32(f)\n\n result += '.'\n c = int(a)\n\n for i in range(32):\n a -= c\n a *= 2\n c = int(a)\n result += str(c)\n\n return result\n\n e = 0 # exponent\n s = 1 if x < 0 else 0 # sign\n m = abs(x) # mantissa\n\n while m >= 1:\n m /= 2.0\n e += 1\n\n while 0 < m < 0.5:\n m *= 2.0\n e -= 1\n\n M = bindec32(m)[3:]\n M = str(s) + M\n E = bin32(e + 128)[-8:] if x != 0 else bin32(0)[-8:]\n\n return M, E"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn C DE HL as values for loading and immediate floating point.", "response": "def immediate_float(x):\n \"\"\" Returns C DE HL as values for loading\n and immediate floating point.\n \"\"\"\n def bin2hex(y):\n return \"%02X\" % int(y, 2)\n\n M, E = fp(x)\n\n C = '0' + bin2hex(E) + 'h'\n ED = '0' + bin2hex(M[8:16]) + bin2hex(M[:8]) + 'h'\n LH = '0' + bin2hex(M[24:]) + bin2hex(M[16:24]) + 'h'\n\n return C, ED, LH"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\niterates in order calling the function with the current node.", "response": "def inorder(self, funct, stopOn=None):\n \"\"\" Iterates in order, calling the function with the current node.\n If stopOn is set to True or False, it will stop on true or false.\n \"\"\"\n if stopOn is None:\n for i in self.children:\n i.inorder(funct)\n else:\n for i in self.children:\n if i.inorder(funct) == stopOn:\n return stopOn\n\n return funct(self)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef preorder(self, funct, stopOn=None):\n if funct(self.symbol) == stopOn and stopOn is not None:\n return stopOn\n\n if stopOn is None:\n for i in self.children:\n i.preorder(funct)\n else:\n for i in self.children:\n if i.preorder(funct) == stopOn:\n return stopOn", "response": "Iterates in preorder calling the function with the current node."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\niterates in postorder calling the function with the current node.", "response": "def postorder(self, funct, stopOn=None):\n \"\"\" Iterates in postorder, calling the function with the current node.\n If stopOn is set to True or False, it will stop on true or false.\n \"\"\"\n if stopOn is None:\n for i in range(len(self.children) - 1, -1, -1):\n self.children[i].postorder(funct)\n else:\n for i in range(len(self.children) - 1, -1, -1):\n if self.children[i].postorder(funct) == stopOn:\n return stopOn\n return funct(self.symbol)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nstore the symbol in an AST instance and left and right to the given ones", "response": "def makenode(clss, symbol, *nexts):\n \"\"\" Stores the symbol in an AST instance,\n and left and right to the given ones\n \"\"\"\n result = clss(symbol)\n for i in nexts:\n if i is None:\n continue\n if not isinstance(i, clss):\n raise NotAnAstError(i)\n result.appendChild(i)\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_include_path():\n f1 = os.path.basename(sys.argv[0]).lower() # script filename\n f2 = os.path.basename(sys.executable).lower() # Executable filename\n\n # If executable filename and script name are the same, we are\n if f1 == f2 or f2 == f1 + '.exe': # under a \"compiled\" python binary\n result = os.path.dirname(os.path.realpath(sys.executable))\n else:\n result = os.path.dirname(os.path.realpath(__file__))\n\n return result", "response": "Default include path using a tricky sys\n calls."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsearches a filename into the list of include paths.", "response": "def search_filename(fname, lineno, local_first):\n \"\"\" Search a filename into the list of the include path.\n If local_first is true, it will try first in the current directory of\n the file being analyzed.\n \"\"\"\n fname = api.utils.sanitize_filename(fname)\n i_path = [CURRENT_DIR] + INCLUDEPATH if local_first else list(INCLUDEPATH)\n i_path.extend(OPTIONS.include_path.value.split(':') if OPTIONS.include_path.value else [])\n if os.path.isabs(fname):\n if os.path.isfile(fname):\n return fname\n else:\n for dir_ in i_path:\n path = api.utils.sanitize_filename(os.path.join(dir_, fname))\n if os.path.exists(path):\n return path\n\n error(lineno, \"file '%s' not found\" % fname)\n return ''"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef include_file(filename, lineno, local_first):\n global CURRENT_DIR\n filename = search_filename(filename, lineno, local_first)\n if filename not in INCLUDED.keys():\n INCLUDED[filename] = []\n\n if len(CURRENT_FILE) > 0: # Added from which file, line\n INCLUDED[filename].append((CURRENT_FILE[-1], lineno))\n\n CURRENT_FILE.append(filename)\n CURRENT_DIR = os.path.dirname(filename)\n return LEXER.include(filename)", "response": "A function that performs a file inclusion in the preprocessor."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef include_once(filename, lineno, local_first):\n filename = search_filename(filename, lineno, local_first)\n if filename not in INCLUDED.keys(): # If not already included\n return include_file(filename, lineno, local_first) # include it and return\n\n # Now checks if the file has been included more than once\n if len(INCLUDED[filename]) > 1:\n warning(lineno, \"file '%s' already included more than once, in file \"\n \"'%s' at line %i\" %\n (filename, INCLUDED[filename][0][0], INCLUDED[filename][0][1]))\n\n # Empty file (already included)\n LEXER.next_token = '_ENDFILE_'\n return ''", "response": "A function that will include a file at a specific line."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nprograms : defs NEWLINE", "response": "def p_program_tokenstring(p):\n \"\"\" program : defs NEWLINE\n \"\"\"\n try:\n tmp = [str(x()) if isinstance(x, MacroCall) else x for x in p[1]]\n except PreprocError as v:\n error(v.lineno, v.message)\n\n tmp.append(p[2])\n p[0] = tmp"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ninclude : INCLUDE STRING", "response": "def p_include(p):\n \"\"\" include : INCLUDE STRING\n \"\"\"\n if ENABLED:\n p[0] = include_file(p[2], p.lineno(2), local_first=True)\n else:\n p[0] = []\n p.lexer.next_token = '_ENDFILE_'"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nincludes : INCLUDE FILENAME", "response": "def p_include_fname(p):\n \"\"\" include : INCLUDE FILENAME\n \"\"\"\n if ENABLED:\n p[0] = include_file(p[2], p.lineno(2), local_first=False)\n else:\n p[0] = []\n p.lexer.next_token = '_ENDFILE_'"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ninclude once is a no - op.", "response": "def p_include_once(p):\n \"\"\" include_once : INCLUDE ONCE STRING\n \"\"\"\n if ENABLED:\n p[0] = include_once(p[3], p.lineno(3), local_first=True)\n else:\n p[0] = []\n\n if not p[0]:\n p.lexer.next_token = '_ENDFILE_'"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndefining : DEFINE ID params defs", "response": "def p_define(p):\n \"\"\" define : DEFINE ID params defs\n \"\"\"\n if ENABLED:\n if p[4]:\n if SPACES.match(p[4][0]):\n p[4][0] = p[4][0][1:]\n else:\n warning(p.lineno(1), \"missing whitespace after the macro name\")\n\n ID_TABLE.define(p[2], args=p[3], value=p[4], lineno=p.lineno(2),\n fname=CURRENT_FILE[-1])\n p[0] = []"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_define_params_empty(p):\n # Defines the 'epsilon' parameter\n p[0] = [ID('', value='', args=None, lineno=p.lineno(1),\n fname=CURRENT_FILE[-1])]", "response": "Define the empty parameter list in the parameter list."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_paramlist_single(p):\n p[0] = [ID(p[1], value='', args=None, lineno=p.lineno(1),\n fname=CURRENT_FILE[-1])]", "response": "paramlist is a list of ID tuples where the first element is the parameter name and the second is the value of the parameter."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef p_ifdef(p):\n global ENABLED\n\n if ENABLED:\n p[0] = [p[2]] + p[3]\n p[0] += ['#line %i \"%s\"' % (p.lineno(4) + 1, CURRENT_FILE[-1])]\n else:\n p[0] = ['#line %i \"%s\"' % (p.lineno(4) + 1, CURRENT_FILE[-1])]\n\n ENABLED = IFDEFS[-1][0]\n IFDEFS.pop()", "response": "P - IF - DEFINE statement"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef p_ifdef_else(p):\n global ENABLED\n\n p[0] = p[1] + p[2]\n p[0] += ['#line %i \"%s\"' % (p.lineno(3) + 1, CURRENT_FILE[-1])]\n ENABLED = IFDEFS[-1][0]\n IFDEFS.pop()", "response": "P 3. 4. 3. ifdef else"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef p_if_header(p):\n global ENABLED\n\n IFDEFS.append((ENABLED, p.lineno(2)))\n ENABLED = ID_TABLE.defined(p[2])", "response": "Set ENABLED to True if the first line of the if header is defined."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nset ENABLED to True if the IFN header is not defined.", "response": "def p_ifn_header(p):\n \"\"\" if_header : IFNDEF ID\n \"\"\"\n global ENABLED\n\n IFDEFS.append((ENABLED, p.lineno(2)))\n ENABLED = not ID_TABLE.defined(p[2])"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef p_exprge(p):\n a = int(p[1]) if p[1].isdigit() else 0\n b = int(p[3]) if p[3].isdigit() else 0\n\n p[0] = '1' if a >= b else '0'", "response": "expr : expr GE expr"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef filter_(input_, filename='', state='INITIAL'):\n global CURRENT_DIR\n\n prev_dir = CURRENT_DIR\n CURRENT_FILE.append(filename)\n CURRENT_DIR = os.path.dirname(CURRENT_FILE[-1])\n LEXER.input(input_, filename)\n LEXER.lex.begin(state)\n parser.parse(lexer=LEXER, debug=OPTIONS.Debug.value > 2)\n CURRENT_FILE.pop()\n CURRENT_DIR = prev_dir", "response": "Filter the input string thought the preprocessor."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef del_data(name=None):\n if name is None:\n tplot_names = list(data_quants.keys())\n for i in tplot_names:\n del data_quants[i]\n return\n \n if not isinstance(name, list):\n name = [name]\n \n entries = [] \n ### \n for i in name:\n if ('?' in i) or ('*' in i):\n for j in data_quants.keys():\n var_verif = fnmatch.fnmatch(data_quants[j].name, i)\n if var_verif == 1:\n entries.append(data_quants[j].name)\n else:\n continue\n for key in entries:\n if key in data_quants:\n del data_quants[key] \n ### \n elif i not in data_quants.keys():\n print(str(i) + \" is currently not in pytplot.\")\n return\n \n else:\n temp_data_quants = data_quants[i]\n str_name = temp_data_quants.name\n \n del data_quants[str_name]\n \n return", "response": "This function deletes all tplot variables that are already stored in memory."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef xlim(min, max):\n if not isinstance(min, (int, float, complex)):\n min = tplot_utilities.str_to_int(min)\n if not isinstance(max, (int, float, complex)):\n max = tplot_utilities.str_to_int(max)\n if 'x_range' in tplot_opt_glob:\n lim_info['xlast'] = tplot_opt_glob['x_range']\n else:\n lim_info['xfull'] = Range1d(min, max)\n lim_info['xlast'] = Range1d(min, max)\n tplot_opt_glob['x_range'] = [min, max]\n return", "response": "This function will set the x axis range for all time series plots"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef ylim(name, min, max):\n if name not in data_quants.keys():\n print(\"That name is currently not in pytplot.\")\n return\n \n temp_data_quant = data_quants[name]\n temp_data_quant.yaxis_opt['y_range'] = [min, max]\n \n return", "response": "This function will set the y axis limits for a specific tplot variable."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef store_data(name, data=None, delete=False, newname=None):\n \n \"\"\"\n This function creates a \"Tplot Variable\" based on the inputs, and\n stores this data in memory. Tplot Variables store all of the information\n needed to generate a plot. \n \n Parameters:\n name : str \n Name of the tplot variable that will be created\n data : dict\n A python dictionary object. \n \n 'x' should be a 1-dimensional array that represents the data's x axis. Typically this data is time,\n represented in seconds since epoch (January 1st 1970)\n \n 'y' should be the data values. This can be 2 dimensions if multiple lines or a spectrogram are desired.\n \n 'v' is optional, and is only used for spectrogram plots. This will be a list of bins to be used. If this\n is provided, then 'y' should have dimensions of x by z.\n \n 'x' and 'y' can be any data format that can be read in by the pandas module. Python lists, numpy arrays,\n or any pandas data type will all work.\n delete : bool, optional\n Deletes the tplot variable matching the \"name\" parameter\n newname: str\n Renames TVar to new name\n \n .. note::\n If you want to combine multiple tplot variables into one, simply supply the list of tplot variables to the\n \"data\" parameter. This will cause the data to overlay when plotted.\n \n Returns:\n None\n \n Examples:\n >>> # Store a single line\n >>> import pytplot\n >>> x_data = [1,2,3,4,5]\n >>> y_data = [1,2,3,4,5]\n >>> pytplot.store_data(\"Variable1\", data={'x':x_data, 'y':y_data})\n \n >>> # Store a two lines\n >>> x_data = [1,2,3,4,5]\n >>> y_data = [[1,5],[2,4],[3,3],[4,2],[5,1]]\n >>> pytplot.store_data(\"Variable2\", data={'x':x_data, 'y':y_data})\n \n >>> # Store a spectrogram\n >>> x_data = [1,2,3]\n >>> y_data = [ [1,2,3] , [4,5,6], [7,8,9] ]\n >>> v_data = [1,2,3]\n >>> pytplot.store_data(\"Variable3\", data={'x':x_data, 'y':y_data, 'v':v_data})\n \n >>> # Combine two different line plots\n >>> pytplot.store_data(\"Variable1and2\", data=['Variable1', 'Variable2'])\n \n >>> #Rename TVar\n >>> pytplot.store_data('a', data={'x':[0,4,8,12,16], 'y':[1,2,3,4,5]})\n >>> pytplot.store_data('a',newname='f')\n\n \"\"\"\n \n global tplot_num\n create_time = datetime.datetime.now()\n \n if delete is True:\n del_data(name)\n return\n\n if data is None and newname is None:\n print('Please provide data.')\n return\n \n if newname is not None:\n pytplot.tplot_rename(name, newname)\n return\n \n if isinstance(data, list):\n base_data = get_base_tplot_vars(data)\n # Use first tplot var as the time range\n trange = [np.nanmin(data_quants[base_data[0]].data.index), \n np.nanmax(data_quants[base_data[0]].data.index)]\n df = base_data\n spec_bins = None\n else:\n df = format_ydata(data['y']) \n times = data['x']\n\n # If given a list of datetime objects, convert times to seconds since epoch.\n if any(isinstance(t, datetime.datetime) for t in times):\n for tt, time in enumerate(times):\n times[tt] = (time-datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)).total_seconds()\n # If given a list of datetime string, convert times to seconds since epoch\n elif any(isinstance(t, str) for t in times):\n for tt, time in enumerate(times):\n times[tt] = pytplot.tplot_utilities.str_to_int(time)\n\n if len(times) != len(df.index):\n print(\"The lengths of x and y do not match!\")\n return\n elif isinstance(times, pd.Series):\n df = df.set_index(data['x'])\n else:\n df['Index'] = times\n df = df.set_index('Index', drop=True)\n\n trange = [np.nanmin(times), np.nanmax(times)]\n \n if 'v' in data or 'v2' in data:\n # Generally the data is 1D, but occasionally\n # the bins will vary in time.\n if 'v' in data:\n spec_bins = data['v']\n else:\n spec_bins = data['v2']\n \n if type(spec_bins) is not pd.DataFrame:\n spec_bins = pd.DataFrame(spec_bins)\n if len(spec_bins.columns) != 1:\n if len(spec_bins) == len(df.index):\n spec_bins = spec_bins.set_index(df.index) \n else:\n print(\"Length of v and x do not match. Cannot create tplot variable.\")\n return \n else:\n spec_bins = spec_bins.transpose() \n else:\n spec_bins = None\n \n xaxis_opt = dict(axis_label='Time')\n yaxis_opt = dict(axis_label=name) if (spec_bins is None) else dict(axis_label='')\n zaxis_opt = dict(axis_label='') if (spec_bins is None) else dict(axis_label=name)\n line_opt = {}\n dtype = ''\n time_bar = []\n # Dictionary to keep track of extra details needed for plotting\n # that aren't actual attributes in Bokeh\n extras = dict(panel_size=1)\n links = {}\n temp = TVar(name, tplot_num, df, spec_bins, xaxis_opt, yaxis_opt, zaxis_opt, line_opt,\n trange, dtype, create_time, time_bar, extras, links)\n \n data_quants[name] = temp\n data_quants[name].yaxis_opt['y_range'] = get_y_range(df, spec_bins)\n \n return", "response": "This function creates a Tplot Variable based on the inputs and stores the data in memory."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_data(name):\n \n global data_quants\n if name not in data_quants.keys():\n print(\"That name is currently not in pytplot\")\n return\n \n temp_data_quant = data_quants[name]\n data_val = temp_data_quant.data.values\n time_val = temp_data_quant.data.index\n \n return(time_val, data_val)", "response": "This function extracts the data from the Tplot Variables stored in memory. This function extracts the data from the Tplot Variables stored in memory. This function returns the time and data values of the variable whose name is name."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngenerates a set of drawing specs for this item.", "response": "def generateDrawSpecs(self, p):\n \"\"\"\n Calls tickValues() and tickStrings() to determine where and how ticks should\n be drawn, then generates from this a set of drawing commands to be\n interpreted by drawPicture().\n \"\"\"\n profiler = debug.Profiler()\n\n # bounds = self.boundingRect()\n bounds = self.mapRectFromParent(self.geometry())\n\n linkedView = self.linkedView()\n if linkedView is None or self.grid is False:\n tickBounds = bounds\n else:\n tickBounds = linkedView.mapRectToItem(self, linkedView.boundingRect())\n\n if self.orientation == 'left':\n span = (bounds.topRight(), bounds.bottomRight())\n tickStart = tickBounds.right()\n tickStop = bounds.right()\n tickDir = -1\n axis = 0\n elif self.orientation == 'right':\n span = (bounds.topLeft(), bounds.bottomLeft())\n tickStart = tickBounds.left()\n tickStop = bounds.left()\n tickDir = 1\n axis = 0\n elif self.orientation == 'top':\n span = (bounds.bottomLeft(), bounds.bottomRight())\n tickStart = tickBounds.bottom()\n tickStop = bounds.bottom()\n tickDir = -1\n axis = 1\n elif self.orientation == 'bottom':\n span = (bounds.topLeft(), bounds.topRight())\n tickStart = tickBounds.top()\n tickStop = bounds.top()\n tickDir = 1\n axis = 1\n # print tickStart, tickStop, span\n\n ## determine size of this item in pixels\n points = list(map(self.mapToDevice, span))\n if None in points:\n return\n lengthInPixels = Point(points[1] - points[0]).length()\n if lengthInPixels == 0:\n return\n\n # Determine major / minor / subminor axis ticks\n if self._tickLevels is None:\n tickLevels = self.tickValues(self.range[0], self.range[1], lengthInPixels)\n tickStrings = None\n else:\n ## parse self.tickLevels into the formats returned by tickLevels() and tickStrings()\n tickLevels = []\n tickStrings = []\n for level in self._tickLevels:\n values = []\n strings = []\n tickLevels.append((None, values))\n tickStrings.append(strings)\n for val, strn in level:\n values.append(val)\n strings.append(strn)\n\n ## determine mapping between tick values and local coordinates\n dif = self.range[1] - self.range[0]\n if dif == 0:\n xScale = 1\n offset = 0\n else:\n if axis == 0:\n xScale = -bounds.height() / dif\n offset = self.range[0] * xScale - bounds.height()\n else:\n xScale = bounds.width() / dif\n offset = self.range[0] * xScale\n\n xRange = [x * xScale - offset for x in self.range]\n xMin = min(xRange)\n xMax = max(xRange)\n\n profiler('init')\n\n tickPositions = [] # remembers positions of previously drawn ticks\n\n ## compute coordinates to draw ticks\n ## draw three different intervals, long ticks first\n tickSpecs = []\n for i in range(len(tickLevels)):\n tickPositions.append([])\n ticks = tickLevels[i][1]\n\n ## length of tick\n tickLength = self.style['tickLength'] / ((i * 0.5) + 1.0)\n\n lineAlpha = 255 / (i + 1)\n if self.grid is not False:\n lineAlpha *= self.grid / 255. * np.clip((0.05 * lengthInPixels / (len(ticks) + 1)), 0., 1.)\n\n for v in ticks:\n ## determine actual position to draw this tick\n x = (v * xScale) - offset\n if x < xMin or x > xMax: ## last check to make sure no out-of-bounds ticks are drawn\n tickPositions[i].append(None)\n continue\n tickPositions[i].append(x)\n\n p1 = [x, x]\n p2 = [x, x]\n p1[axis] = tickStart\n p2[axis] = tickStop\n if self.grid is False:\n p2[axis] += tickLength * tickDir\n tickPen = self.pen()\n color = tickPen.color()\n color.setAlpha(lineAlpha)\n tickPen.setColor(color)\n tickSpecs.append((tickPen, Point(p1), Point(p2)))\n profiler('compute ticks')\n\n if self.style['stopAxisAtTick'][0] is True:\n stop = max(span[0].y(), min(map(min, tickPositions)))\n if axis == 0:\n span[0].setY(stop)\n else:\n span[0].setX(stop)\n if self.style['stopAxisAtTick'][1] is True:\n stop = min(span[1].y(), max(map(max, tickPositions)))\n if axis == 0:\n span[1].setY(stop)\n else:\n span[1].setX(stop)\n axisSpec = (self.pen(), span[0], span[1])\n\n textOffset = self.style['tickTextOffset'][axis] ## spacing between axis and text\n # if self.style['autoExpandTextSpace'] is True:\n # textWidth = self.textWidth\n # textHeight = self.textHeight\n # else:\n # textWidth = self.style['tickTextWidth'] ## space allocated for horizontal text\n # textHeight = self.style['tickTextHeight'] ## space allocated for horizontal text\n\n textSize2 = 0\n textRects = []\n textSpecs = [] ## list of draw\n\n # If values are hidden, return early\n if not self.style['showValues']:\n return (axisSpec, tickSpecs, textSpecs)\n\n for i in range(min(len(tickLevels), self.style['maxTextLevel'] + 1)):\n ## Get the list of strings to display for this level\n if tickStrings is None:\n spacing, values = tickLevels[i]\n strings = self.tickStrings(values, self.autoSIPrefixScale * self.scale, spacing)\n else:\n strings = tickStrings[i]\n\n if len(strings) == 0:\n continue\n\n ## ignore strings belonging to ticks that were previously ignored\n for j in range(len(strings)):\n if tickPositions[i][j] is None:\n strings[j] = None\n\n ## Measure density of text; decide whether to draw this level\n rects = []\n for s in strings:\n if s is None:\n rects.append(None)\n else:\n br = p.boundingRect(QtCore.QRectF(0, 0, 100, 100), QtCore.Qt.AlignCenter, asUnicode(s))\n ## boundingRect is usually just a bit too large\n ## (but this probably depends on per-font metrics?)\n br.setHeight(br.height() * 1.4)\n\n rects.append(br)\n textRects.append(rects[-1])\n\n if len(textRects) > 0:\n ## measure all text, make sure there's enough room\n if axis == 0:\n textSize = np.sum([r.height() for r in textRects])\n textSize2 = np.max([r.width() for r in textRects])\n else:\n textSize = np.sum([r.width() for r in textRects])\n textSize2 = np.max([r.height() for r in textRects])\n else:\n textSize = 0\n textSize2 = 0\n\n if i > 0: ## always draw top level\n ## If the strings are too crowded, stop drawing text now.\n ## We use three different crowding limits based on the number\n ## of texts drawn so far.\n textFillRatio = float(textSize) / lengthInPixels\n finished = False\n for nTexts, limit in self.style['textFillLimits']:\n if len(textSpecs) >= nTexts and textFillRatio >= limit:\n finished = True\n break\n if finished:\n break\n\n # spacing, values = tickLevels[best]\n # strings = self.tickStrings(values, self.scale, spacing)\n # Determine exactly where tick text should be drawn\n for j in range(len(strings)):\n vstr = strings[j]\n if vstr is None: ## this tick was ignored because it is out of bounds\n continue\n vstr = asUnicode(vstr)\n x = tickPositions[i][j]\n # textRect = p.boundingRect(QtCore.QRectF(0, 0, 100, 100), QtCore.Qt.AlignCenter, vstr)\n textRect = rects[j]\n height = textRect.height()\n width = textRect.width()\n # self.textHeight = height\n offset = max(0, self.style['tickLength']) + textOffset\n if self.orientation == 'left':\n textFlags = QtCore.Qt.TextDontClip | QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter\n rect = QtCore.QRectF(tickStop - offset - width, x - (height / 2), width, height)\n elif self.orientation == 'right':\n textFlags = QtCore.Qt.TextDontClip | QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter\n rect = QtCore.QRectF(tickStop + offset, x - (height / 2), width, height)\n elif self.orientation == 'top':\n textFlags = QtCore.Qt.TextDontClip | QtCore.Qt.AlignCenter | QtCore.Qt.AlignBottom\n rect = QtCore.QRectF(x - width / 2., tickStop - offset - height, width, height)\n elif self.orientation == 'bottom':\n textFlags = QtCore.Qt.TextDontClip | QtCore.Qt.AlignCenter | QtCore.Qt.AlignTop\n rect = QtCore.QRectF(x - width / 2., tickStop + offset, width, height)\n\n # p.setPen(self.pen())\n # p.drawText(rect, textFlags, vstr)\n textSpecs.append((rect, textFlags, vstr))\n profiler('compute text')\n\n ## update max text size if needed.\n self._updateMaxTextSize(textSize2)\n\n return (axisSpec, tickSpecs, textSpecs)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef static2dplot(var, time):\n\n # Grab names of data loaded in as tplot variables.\n names = list(pytplot.data_quants.keys())\n # Get data we'll actually work with here.\n valid_variables = tplot_utilities.get_data(names)\n\n # Don't plot anything unless we have spectrograms with which to work.\n if valid_variables:\n # Get z label\n labels = tplot_utilities.get_labels_axis_types(names)\n\n # Put together data in easy-to-access format for plots.\n data = {}\n for name in valid_variables:\n bins = tplot_utilities.get_bins(name)\n time_values, z_values = tplot_utilities.get_z_t_values(name)\n data[name] = [bins, z_values, time_values]\n\n # Set up the 2D static plot\n pytplot.static_window = pg.GraphicsWindow()\n pytplot.static_window.resize(1000, 600)\n pytplot.static_window.setWindowTitle('Static Window')\n plot = pytplot.static_window.addPlot(title='2D Static Plot', row=0, col=0)\n # Make it so that whenever this first starts up, you just have an empty plot\n plot_data = plot.plot([], [])\n\n if var in valid_variables:\n # Get min/max values of data's time range (in both datetime and seconds since epoch)\n t_min = np.nanmin(time_values)\n t_min_str = tplot_utilities.int_to_str(np.nanmin(time_values))\n t_min_conv_back = tplot_utilities.str_to_int(t_min_str)\n t_max = np.nanmax(time_values)\n t_max_str = tplot_utilities.int_to_str(np.nanmax(time_values))\n t_max_conv_back = tplot_utilities.str_to_int(t_max_str)\n # Convert user input to seconds since epoch\n user_time = tplot_utilities.str_to_int(time)\n\n # Covering situation where user entered a time not in the dataset!\n # As long as they used a time in the dataset, this will not trigger.\n if user_time not in range(t_min_conv_back, t_max_conv_back+1):\n while True:\n try:\n user_time = tplot_utilities.str_to_int(input(\n 'Chosen time not in range of data [{} to {}]. Input new time (%Y-%m-%d %H:%M:%S). '.format(\n t_min_str, t_max_str)))\n except:\n continue\n else:\n if user_time not in range(int(t_min), int(t_max)):\n continue\n else:\n break\n\n # Get time closest to the user's time choice\n time_array = np.array(data[var][2])\n array = np.asarray(time_array)\n idx = (np.abs(array - user_time)).argmin()\n # If user indicated they wanted the interactive plot's axes to be logged, log 'em.\n # But first make sure that values in x and y are loggable!\n x_axis = False\n y_axis = False\n # Checking x axis\n if np.nanmin(data[name][0][:]) < 0:\n print('Negative data is incompatible with log plotting.')\n elif np.nanmin(data[name][0][:]) >= 0 and labels[name][2] == 'log':\n x_axis = True\n # Checking y axis\n if np.nanmin(list(data[name][1][idx])) < 0:\n print('Negative data is incompatible with log plotting')\n elif np.nanmin(list(data[name][1][idx])) >= 0 and labels[name][3] == 'log':\n y_axis = True\n\n # Set plot labels\n plot.setLabel('bottom', '{}'.format(labels[name][0]))\n plot.setLabel('left', '{}'.format(labels[name][1]))\n plot.setLogMode(x=x_axis, y=y_axis)\n # Update x and y range if user modified it\n tplot_utilities.set_x_range(name, x_axis, plot)\n tplot_utilities.set_y_range(name, y_axis, plot)\n # Plot data based on time we're hovering over\n plot_data.setData(data[name][0][:], list(data[name][1][idx]))", "response": "This function will plot the static option of the current page."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncheck if the spec_bins are ascending or descending.", "response": "def _check_spec_bins_ordering(self):\n \"\"\"\n This is a private function of the TVar object, this is run during\n object creation to check if spec_bins are ascending or descending\n \"\"\"\n if self.spec_bins is None:\n return\n if len(self.spec_bins) == len(self.data.index):\n self.spec_bins_time_varying = True\n break_top_loop = False\n for index, row in self.spec_bins.iterrows():\n if row.isnull().values.all():\n continue\n else:\n for i in row.index:\n if np.isfinite(row[i]) and np.isfinite(row[i + 1]):\n ascending = row[i] < row[i + 1]\n break_top_loop = True\n break\n else:\n continue\n if break_top_loop:\n break\n else:\n ascending = self.spec_bins[0].iloc[0] < self.spec_bins[1].iloc[0]\n return ascending"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef interactiveplot(t_average=None):\n\n # Grab names of data loaded in as tplot variables.\n names = list(pytplot.data_quants.keys())\n # Get data we'll actually work with here.\n valid_variables = tplot_utilities.get_data(names)\n\n # Don't plot anything unless we have spectrograms with which to work.\n if valid_variables:\n # Get z label\n labels = tplot_utilities.get_labels_axis_types(names)\n\n # Put together data in easy-to-access format for plots.\n data = {}\n for name in valid_variables:\n bins = tplot_utilities.get_bins(name)\n time_values, z_values = tplot_utilities.get_z_t_values(name)\n data[name] = [bins, z_values, time_values]\n\n # Set up the 2D interactive plot\n pytplot.interactive_window = pg.GraphicsWindow()\n pytplot.interactive_window.resize(1000, 600)\n pytplot.interactive_window.setWindowTitle('Interactive Window')\n plot = pytplot.interactive_window.addPlot(title='2D Interactive Plot', row=0, col=0)\n # Make it so that whenever this first starts up, you just have an empty plot\n plot_data = plot.plot([], [])\n\n # The following update function is passed to change_hover_time in the HoverTime class\n # defined in __init__.py. For reference, \"t\" essentially originates inside of\n # TVarFigure(1D/Spec/Alt/Map), inside the _mousemoved function. It calls\n # \"self._mouseMovedFunction(int(mousePoint.x()))\" and that is called every time the mouse is\n # moved by Qt. Therefore, it gives the location of the mouse on the x axis. In tplot,\n # mouse_moved_event is set to pytplot.hover_time.change_hover_time, so the mouseMovedFunction\n # is pytplot.hover_time.change_hover_time. Thus, whenever change_hover_time is called, it\n # calls every other function that is registered. Since the below function update() is\n # registered as a listener, it'll update whenever hover_time is updated.\n # to the HoverTime class with \"t\" as the input.\n\n # TL;DR, t comes from getting the mouse location in pyqtgraph every time the mouse is moved\n # and the below function will update the plot's position as the mouse is moved.\n def update(t, name):\n if name in valid_variables:\n # Get the time closest to the x position the mouse is over.\n time_array = np.array(data[name][2])\n array = np.asarray(time_array)\n idx = (np.abs(array - t)).argmin()\n\n # If user indicated they wanted the interactive plot's axes to be logged, log 'em.\n # But first make sure that values in x and y are loggable!\n x_axis = False\n y_axis = False\n # Checking x axis\n if np.nanmin(data[name][0][:]) < 0:\n print('Negative data is incompatible with log plotting.')\n elif np.nanmin(data[name][0][:]) >= 0 and labels[name][2] == 'log':\n x_axis = True\n # Checking y axis\n if np.nanmin(list(data[name][1][idx])) < 0:\n print('Negative data is incompatible with log plotting')\n elif np.nanmin(list(data[name][1][idx])) >= 0 and labels[name][3] == 'log':\n y_axis = True\n\n # Set plot labels\n plot.setLabel('bottom', '{}'.format(labels[name][0]))\n plot.setLabel('left', '{}'.format(labels[name][1]))\n plot.setLogMode(x=x_axis, y=y_axis)\n # Update x and y range if user modified it\n tplot_utilities.set_x_range(name, x_axis, plot)\n tplot_utilities.set_y_range(name, y_axis, plot)\n\n if 't_average' in pytplot.data_quants[name].extras:\n # If the user indicated that they wanted to average the interactive plot's y values based on a\n # certain time range around the cursor location, we then want to get average of values around\n # the cursor location.\n t_min = data[name][2][0]\n t_max = data[name][2][-1]\n\n delta = pytplot.data_quants[name].extras['t_average']/int(2)\n\n left_bound = data[name][2][idx] - delta\n right_bound = data[name][2][idx] + delta\n\n if (left_bound - t_min >= 0) and (t_max - right_bound >= 0):\n # Find index of left and right bounds, no fancy foot work necessary.\n idx_left = (np.abs(array - left_bound)).argmin()\n idx_right = (np.abs(array - right_bound)).argmin()\n elif left_bound - t_min < 0:\n # Find the number of seconds difference between the cursor's\n # left bound and the minimum time in the dataset, add that\n # difference to the right bound time (since you want to push the bound\n # forward in time, and set the left bound's index to be 0.\n idx_left = 0\n diff = right_bound + (t_min - left_bound)\n idx_right = (np.abs(array - diff)).argmin()\n elif t_max - right_bound < 0:\n # Find the number of seconds difference between the cursor's\n # right bound and the maximum time in the dataset, subtract that\n # difference from the left bound time (since you want to push the bound\n # back in time), and set the right bound's index to be -1.\n idx_right = -1\n diff = left_bound - (right_bound - t_max)\n idx_left = (np.abs(array - diff)).argmin()\n elif (left_bound - t_min < 0) and (t_max - right_bound < 0):\n # The user is asking to average the entire time frame of the dataset...\n # dunno why they want that, but if they do, use the time-averaged static plot,\n # not this.\n print(\n 'This plot isn\\'t appropriate for what you want, use the time-averaged static plot.')\n\n # Average values based on the calculated right and left bounds' indices.\n time_diff = abs(idx_right - idx_left)\n # Make sure to account for edge problem\n if idx_right != -1:\n y_values_slice = data[name][1][idx_left:idx_right + 1]\n else:\n y_values_slice = data[name][1][idx_left:]\n y_values_avgd = np.sum(y_values_slice, axis=0)/np.float(time_diff)\n\n # Update x and y range if user modified it\n tplot_utilities.set_x_range(name, x_axis, plot)\n tplot_utilities.set_y_range(name, y_axis, plot)\n\n try:\n # Plot data based on time we're hovering over\n plot_data.setData(data[name][0][:], y_values_avgd)\n except ZeroDivisionError:\n pass\n else:\n # Update x and y range if user modified it\n tplot_utilities.set_x_range(name, x_axis, plot)\n tplot_utilities.set_y_range(name, y_axis, plot)\n # If the user just wants a plain jane interactive plot...\n # Plot data based on time we're hovering over'\n try:\n plot_data.setData(data[name][0][:], list(data[name][1][idx]))\n except ZeroDivisionError:\n pass\n\n else:\n # Cover the situation where you hover over a non-spectrogram plot.\n plot.setLogMode(False, False)\n plot.setLabel('bottom', '')\n plot.setLabel('left', '')\n plot_data.setData([], [])\n\n # Make the above function called whenever hover_time is updated.\n pytplot.hover_time.register_listener(update)", "response": "Create an interactive plot of the current tplot variables."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_timespan(name):\n \n if name not in data_quants.keys():\n print(\"That name is currently not in pytplot\") \n return\n print(\"Start Time: \" + tplot_utilities.int_to_str(data_quants[name].trange[0]))\n print(\"End Time: \" + tplot_utilities.int_to_str(data_quants[name].trange[1]))\n \n return(data_quants[name].trange[0], data_quants[name].trange[1])", "response": "This function extracts the time span from the Tplot Variables stored in memory. This function returns the time span of the variable that is currently in memory."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef timebar(t, varname = None, databar = False, delete = False, color = 'black', thick = 1, dash = False): \n \n # make sure t entered is a list\n if not isinstance(t, list):\n t = [t]\n \n #if entries in list not numerical, run str_to_int\n if not isinstance(t[0], (int, float, complex)):\n t1 = []\n for time in t:\n t1.append(tplot_utilities.str_to_int(time))\n t = t1\n \n dim = 'height'\n if databar is True:\n dim = 'width'\n \n dash_pattern = 'solid'\n if dash is True:\n dash_pattern = 'dashed'\n \n \n if delete is True:\n tplot_utilities.timebar_delete(t, varname, dim)\n return\n \n #if no varname specified, add timebars to every plot\n if varname is None:\n num_bars = len(t)\n for i in range(num_bars):\n tbar = {}\n tbar['location'] = t[i]\n tbar['dimension'] = dim\n tbar['line_color'] = pytplot.tplot_utilities.rgb_color(color)\n\n tbar['line_width'] = thick\n tbar['line_dash'] = dash_pattern\n for name in data_quants:\n temp_data_quants = data_quants[name]\n temp_data_quants.time_bar.append(tbar)\n #if varname specified\n else:\n if not isinstance(varname, list):\n varname = [varname]\n for j in varname:\n if j not in data_quants.keys():\n print(str(j) + \"is currently not in pytplot\")\n else:\n num_bars = len(t)\n for i in range(num_bars):\n tbar = {}\n tbar['location'] = t[i]\n tbar['dimension'] = dim\n tbar['line_color'] = pytplot.tplot_utilities.rgb_color(color)\n tbar['line_width'] = thick\n tbar['line_dash'] = dash_pattern\n temp_data_quants = data_quants[j]\n temp_data_quants.time_bar.append(tbar)\n return", "response": "This function will add a vertical bar to all time series plots."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef options(name, option, value):\n\n if not isinstance(name, list):\n name = [name]\n \n option = option.lower()\n \n for i in name:\n if i not in data_quants.keys():\n print(str(i) + \" is currently not in pytplot.\")\n return\n \n if option == 'color':\n if isinstance(value, list):\n data_quants[i].extras['line_color'] = value\n else:\n data_quants[i].extras['line_color'] = [value]\n \n if option == 'link':\n if isinstance(value, list):\n data_quants[i].link_to_tvar(value[0], value[1])\n \n if option == 'colormap':\n if isinstance(value, list):\n data_quants[i].extras['colormap'] = value\n else:\n data_quants[i].extras['colormap'] = [value]\n \n if option == 'spec':\n _reset_plots(i)\n data_quants[i].extras['spec'] = value\n \n if option == 'alt':\n _reset_plots(i)\n data_quants[i].extras['alt'] = value\n \n if option == 'map':\n _reset_plots(i)\n data_quants[i].extras['map'] = value\n\n if option == 'legend_names':\n data_quants[i].yaxis_opt['legend_names'] = value\n\n if option == 'xlog_interactive':\n if value:\n data_quants[i].interactive_xaxis_opt['xi_axis_type'] = 'log'\n else:\n data_quants[i].interactive_xaxis_opt['xi_axis_type'] = 'linear'\n\n if option == 'ylog':\n negflag = _ylog_check(data_quants, value, i)\n if negflag == 0:\n data_quants[i].yaxis_opt['y_axis_type'] = 'log'\n else:\n data_quants[i].yaxis_opt['y_axis_type'] = 'linear'\n\n if option == 'ylog_interactive':\n if value:\n data_quants[i].interactive_yaxis_opt['yi_axis_type'] = 'log'\n else:\n data_quants[i].interactive_xaxis_opt['xi_axis_type'] = 'linear'\n\n if option == 'zlog':\n negflag = _zlog_check(data_quants, value, i)\n if negflag == 0:\n data_quants[i].zaxis_opt['z_axis_type'] = 'log'\n else:\n data_quants[i].zaxis_opt['z_axis_type'] = 'linear'\n \n if option == 'nodata':\n data_quants[i].line_opt['visible'] = value\n \n if option == 'line_style':\n to_be = []\n if value == 0 or value == 'solid_line':\n to_be = []\n elif value == 1 or value == 'dot':\n to_be = [2, 4]\n elif value == 2 or value == 'dash':\n to_be = [6]\n elif value == 3 or value == 'dash_dot':\n to_be = [6, 4, 2, 4]\n elif value == 4 or value == 'dash_dot_dot_dot':\n to_be = [6, 4, 2, 4, 2, 4, 2, 4]\n elif value == 5 or value == 'long_dash':\n to_be = [10]\n \n data_quants[i].line_opt['line_dash'] = to_be\n \n if(value == 6 or value == 'none'):\n data_quants[i].line_opt['visible'] = False\n\n if option == 'char_size':\n data_quants[i].extras['char_size'] = value\n \n if option == 'name':\n data_quants[i].line_opt['name'] = value\n \n if option == \"panel_size\":\n if value > 1 or value <= 0:\n print(\"Invalid value. Should be (0, 1]\")\n return\n data_quants[i].extras['panel_size'] = value\n \n if option == 'basemap':\n data_quants[i].extras['basemap'] = value\n \n if option == 'alpha':\n if value > 1 or value < 0:\n print(\"Invalid value. Should be [0, 1]\")\n return\n data_quants[i].extras['alpha'] = value\n \n if option == 'thick':\n data_quants[i].line_opt['line_width'] = value\n \n if option == ('yrange' or 'y_range'):\n data_quants[i].yaxis_opt['y_range'] = [value[0], value[1]]\n \n if option == ('zrange' or 'z_range'):\n data_quants[i].zaxis_opt['z_range'] = [value[0], value[1]]\n\n if option == 'xrange_interactive':\n data_quants[i].interactive_xaxis_opt['xi_range'] = [value[0], value[1]]\n\n if option == 'yrange_interactive':\n data_quants[i].interactive_yaxis_opt['yi_range'] = [value[0], value[1]]\n \n if option == 'xtitle':\n data_quants[i].xaxis_opt['axis_label'] = value\n \n if option == 'ytitle':\n data_quants[i].yaxis_opt['axis_label'] = value\n \n if option == 'ztitle':\n data_quants[i].zaxis_opt['axis_label'] = value\n \n if option == 'plotter':\n _reset_plots(i)\n data_quants[i].extras['plotter'] = value\n\n if option == 'crosshair_x':\n data_quants[i].xaxis_opt['crosshair'] = value\n\n if option == 'crosshair_y':\n data_quants[i].yaxis_opt['crosshair'] = value\n\n if option == 'crosshair_z':\n data_quants[i].zaxis_opt['crosshair'] = value\n\n if option == 'static':\n data_quants[i].extras['static'] = value\n\n if option == 'static_tavg':\n data_quants[i].extras['static_tavg'] = [value[0], value[1]]\n\n if option == 't_average':\n data_quants[i].extras['t_average'] = value\n return", "response": "This function allows the user to set a large variety of options for individual plots."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef tplot_restore(filename):\n \n #Error check\n if not (os.path.isfile(filename)):\n print(\"Not a valid file name\")\n return\n \n #Check if the restored file was an IDL file\n \n if filename.endswith('.tplot'):\n temp_tplot = readsav(filename)\n for i in range(len(temp_tplot['dq'])):\n data_name = temp_tplot['dq'][i][0].decode(\"utf-8\")\n temp_x_data = temp_tplot['dq'][i][1][0][0]\n #Pandas reads in data the other way I guess\n if len(temp_tplot['dq'][i][1][0][2].shape) == 2:\n temp_y_data = np.transpose(temp_tplot['dq'][i][1][0][2])\n else:\n temp_y_data = temp_tplot['dq'][i][1][0][2]\n \n \n #If there are more than 4 fields, that means it is a spectrogram \n if len(temp_tplot['dq'][i][1][0]) > 4:\n temp_v_data = temp_tplot['dq'][i][1][0][4]\n \n #Change from little endian to big endian, since pandas apparently hates little endian\n #We might want to move this into the store_data procedure eventually\n if (temp_x_data.dtype.byteorder == '>'):\n temp_x_data = temp_x_data.byteswap().newbyteorder()\n if (temp_y_data.dtype.byteorder == '>'):\n temp_y_data = temp_y_data.byteswap().newbyteorder()\n if (temp_v_data.dtype.byteorder == '>'):\n temp_v_data = temp_v_data.byteswap().newbyteorder()\n \n store_data(data_name, data={'x':temp_x_data, 'y':temp_y_data, 'v':temp_v_data})\n else:\n #Change from little endian to big endian, since pandas apparently hates little endian\n #We might want to move this into the store_data procedure eventually\n if (temp_x_data.dtype.byteorder == '>'):\n temp_x_data = temp_x_data.byteswap().newbyteorder()\n if (temp_y_data.dtype.byteorder == '>'):\n temp_y_data = temp_y_data.byteswap().newbyteorder()\n store_data(data_name, data={'x':temp_x_data, 'y':temp_y_data})\n \n if temp_tplot['dq'][i][3].dtype.names is not None:\n for option_name in temp_tplot['dq'][i][3].dtype.names:\n options(data_name, option_name, temp_tplot['dq'][i][3][option_name][0])\n \n data_quants[data_name].trange = temp_tplot['dq'][i][4].tolist()\n data_quants[data_name].dtype = temp_tplot['dq'][i][5]\n data_quants[data_name].create_time = temp_tplot['dq'][i][6]\n \n for option_name in temp_tplot['tv'][0][0].dtype.names:\n if option_name == 'TRANGE':\n tplot_options('x_range', temp_tplot['tv'][0][0][option_name][0])\n if option_name == 'WSIZE':\n tplot_options('wsize', temp_tplot['tv'][0][0][option_name][0])\n if option_name == 'VAR_LABEL':\n tplot_options('var_label', temp_tplot['tv'][0][0][option_name][0])\n if 'P' in temp_tplot['tv'][0][1].tolist():\n for option_name in temp_tplot['tv'][0][1]['P'][0].dtype.names:\n if option_name == 'TITLE':\n tplot_options('title', temp_tplot['tv'][0][1]['P'][0][option_name][0])\n \n #temp_tplot['tv'][0][1] is all of the \"settings\" variables\n #temp_tplot['tv'][0][1]['D'][0] is \"device\" options\n #temp_tplot['tv'][0][1]['P'][0] is \"plot\" options\n #temp_tplot['tv'][0][1]['X'][0] is x axis options\n #temp_tplot['tv'][0][1]['Y'][0] is y axis options\n ####################################################################\n else:\n temp = pickle.load(open(filename,\"rb\"))\n num_data_quants = temp[0]\n for i in range(0, num_data_quants):\n data_quants[temp[i+1].name] = temp[i+1]\n tplot_opt_glob = temp[num_data_quants+1]\n \n return", "response": "This function will restore the data contained in the IDL file and return the IDL file that was saved with the tplot_save command."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef tplot(name,\n var_label=None,\n auto_color=True,\n interactive=False,\n combine_axes=True,\n nb=False,\n save_file=None,\n gui=False,\n qt=False,\n bokeh=False,\n save_png=None,\n display=True,\n testing=False):\n \"\"\"\n This is the function used to display the tplot variables stored in memory.\n The default output is to show the plots stacked on top of one another inside a GUI window.\n The GUI window has the option to export the plots in either PNG or HTML formats.\n\n .. note::\n This plotting routine uses the python Bokeh library, which creates plots using HTML and Javascript.\n Bokeh is technically still in beta, so future patches to Bokeh may require updates to this function.\n\n Parameters:\n name : str / list\n List of tplot variables that will be plotted\n var_label : str, optional\n The name of the tplot variable you would like as\n a second x axis.\n auto_color : bool, optional\n Automatically color the plot lines.\n interactive : bool, optional\n If True, a secondary interactive plot will be generated next to spectrogram plots.\n Mousing over the spectrogram will display a slice of data from that time on the\n interactive chart.\n combine_axes : bool, optional\n If True, the axes are combined so that they all display the same x range. This also enables\n scrolling/zooming/panning on one plot to affect all of the other plots simultaneously.\n nb : bool, optional\n If True, the plot will be displayed inside of a current Jupyter notebook session.\n save_file : str, optional\n A full file name and path.\n If this option is set, the plot will be automatically saved to the file name provided in an HTML format.\n The plots can then be opened and viewed on any browser without any requirements.\n bokeh : bool, optional\n If True, plots data using bokeh\n Else (bokeh=False or omitted), plots data using PyQtGraph\n gui : bool, optional\n If True, then this function will output the 2 HTML components of the generated plots as string variables.\n This is useful if you are embedded the plots in your own GUI. For more information, see\n http://bokeh.pydata.org/en/latest/docs/user_guide/embed.html\n qt : bool, optional\n If True, then this function will display the plot inside of the Qt window. From this window, you\n can choose to export the plots as either an HTML file, or as a PNG.\n save_png : str, optional\n A full file name and path.\n If this option is set, the plot will be automatically saved to the file name provided in a PNG format.\n display: bool, optional\n If True, then this function will display the plotted tplot variables. Necessary to make this optional\n so we can avoid it in a headless server environment.\n testing: bool, optional\n If True, doesn't run the '(hasattr(sys, 'ps1'))' line that makes plots interactive - i.e., avoiding issues\n\n Returns:\n None\n\n Examples:\n >>> #Plot a single line in bokeh\n >>> import pytplot\n >>> x_data = [2,3,4,5,6]\n >>> y_data = [1,2,3,4,5]\n >>> pytplot.store_data(\"Variable1\", data={'x':x_data, 'y':y_data})\n >>> pytplot.tplot(\"Variable1\",bokeh=True)\n\n >>> #Display two plots\n >>> x_data = [1,2,3,4,5]\n >>> y_data = [[1,5],[2,4],[3,3],[4,2],[5,1]]\n >>> pytplot.store_data(\"Variable2\", data={'x':x_data, 'y':y_data})\n >>> pytplot.tplot([\"Variable1\", \"Variable2\"])\n\n >>> #Display 2 plots, using Variable1 as another x axis\n >>> x_data = [1,2,3]\n >>> y_data = [ [1,2,3] , [4,5,6], [7,8,9] ]\n >>> v_data = [1,2,3]\n >>> pytplot.store_data(\"Variable3\", data={'x':x_data, 'y':y_data, 'v':v_data})\n >>> pytplot.options(\"Variable3\", 'spec', 1)\n >>> pytplot.tplot([\"Variable2\", \"Variable3\"], var_label='Variable1')\n\n >>> #Plot all 3 tplot variables, sending the output to an HTML file\n >>> pytplot.tplot([\"Variable1\", \"Variable2\", \"Variable3\"], save_file='C:/temp/pytplot_example.html')\n\n >>> #Plot all 3 tplot variables, sending the HTML output to a pair of strings\n >>> div, component = pytplot.tplot([\"Variable1\", \"Variable2\", \"Variable3\"], gui=True)\n \"\"\"\n\n if not pytplot.using_graphics and save_file is None:\n print(\"Qt was not successfully imported. Specify save_file to save the file as a .html file.\")\n return\n # Check a bunch of things\n if not isinstance(name, list):\n name = [name]\n num_plots = 1\n else:\n num_plots = len(name)\n\n for i in range(num_plots):\n if isinstance(name[i], int):\n name[i] = list(pytplot.data_quants.keys())[name[i]]\n if name[i] not in pytplot.data_quants.keys():\n print(str(i) + \" is currently not in pytplot\")\n return\n\n if isinstance(var_label, int):\n var_label = list(pytplot.data_quants.keys())[var_label]\n\n if bokeh:\n layout = HTMLPlotter.generate_stack(name, var_label=var_label, auto_color=auto_color, combine_axes=combine_axes,\n interactive=interactive)\n # Output types\n if gui:\n script, div = components(layout)\n return script, div\n elif nb:\n output_notebook()\n show(layout)\n return\n elif save_file is not None:\n output_file(save_file, mode='inline')\n save(layout)\n return\n elif qt:\n available_qt_window = tplot_utilities.get_available_qt_window()\n dir_path = tempfile.gettempdir() # send to user's temp directory\n output_file(os.path.join(dir_path, \"temp.html\"), mode='inline')\n save(layout)\n new_layout = WebView()\n available_qt_window.resize(pytplot.tplot_opt_glob['window_size'][0] + 100,\n pytplot.tplot_opt_glob['window_size'][1] + 100)\n new_layout.resize(pytplot.tplot_opt_glob['window_size'][0], pytplot.tplot_opt_glob['window_size'][1])\n dir_path = tempfile.gettempdir() # send to user's temp directory\n new_layout.setUrl(QtCore.QUrl.fromLocalFile(os.path.join(dir_path, \"temp.html\")))\n available_qt_window.newlayout(new_layout)\n available_qt_window.show()\n available_qt_window.activateWindow()\n if testing:\n return\n if not (hasattr(sys, 'ps1')) or not hasattr(QtCore, 'PYQT_VERSION'):\n QtGui.QApplication.instance().exec_()\n return\n else:\n dir_path = tempfile.gettempdir() # send to user's temp directory\n output_file(os.path.join(dir_path, \"temp.html\"), mode='inline')\n show(layout)\n return\n else:\n if save_png is not None:\n layout = QtPlotter.generate_stack(name, var_label=var_label, combine_axes=combine_axes,\n mouse_moved_event=pytplot.hover_time.change_hover_time)\n layout.resize(pytplot.tplot_opt_glob['window_size'][0], pytplot.tplot_opt_glob['window_size'][1])\n for i, item in enumerate(layout.items()):\n if type(item) == pg.graphicsItems.GraphicsLayout.GraphicsLayout:\n layout.items()[i].resize(pytplot.tplot_opt_glob['window_size'][0],\n pytplot.tplot_opt_glob['window_size'][1])\n exporter = PyTPlot_Exporter.PytplotExporter(layout)\n exporter.parameters()['width'] = pytplot.tplot_opt_glob['window_size'][0]\n exporter.parameters()['height'] = pytplot.tplot_opt_glob['window_size'][1]\n exporter.export(save_png)\n\n if display:\n # Set up all things needed for when a user asks to save plot from window\n layout_orig = QtPlotter.generate_stack(name, var_label=var_label, combine_axes=combine_axes,\n mouse_moved_event=pytplot.hover_time.change_hover_time)\n layout_orig.resize(pytplot.tplot_opt_glob['window_size'][0], pytplot.tplot_opt_glob['window_size'][1])\n for i, item in enumerate(layout_orig.items()):\n if type(item) == pg.graphicsItems.GraphicsLayout.GraphicsLayout:\n layout_orig.items()[i].resize(pytplot.tplot_opt_glob['window_size'][0],\n pytplot.tplot_opt_glob['window_size'][1])\n exporter = QtPlotter.PytplotExporter(layout_orig)\n\n # Set up displayed plot window and grab plots to plot on it\n available_qt_window = tplot_utilities.get_available_qt_window()\n layout = QtPlotter.generate_stack(name, var_label=var_label, combine_axes=combine_axes,\n mouse_moved_event=pytplot.hover_time.change_hover_time)\n\n available_qt_window.newlayout(layout)\n available_qt_window.resize(pytplot.tplot_opt_glob['window_size'][0],\n pytplot.tplot_opt_glob['window_size'][1])\n\n # Implement button that lets you save the PNG\n available_qt_window.init_savepng(exporter)\n\n # Show the plot window and plot\n available_qt_window.show()\n available_qt_window.activateWindow()\n\n if interactive:\n # Call 2D interactive window; This will only plot something when spectrograms are involved.\n interactiveplot.interactiveplot()\n\n static_list = [i for i in name if 'static' in pytplot.data_quants[i].extras]\n for tplot_var in static_list:\n # Call 2D static window; This will only plot something when spectrograms are involved.\n staticplot.static2dplot(tplot_var, pytplot.data_quants[tplot_var].extras['static'])\n\n static_tavg_list = [i for i in name if 'static_tavg' in pytplot.data_quants[i].extras]\n for tplot_var in static_tavg_list:\n # Call 2D static window for time-averaged values; This will only plot something when spectrograms\n # are involved\n staticplot_tavg.static2dplot_timeaveraged(\n tplot_var, pytplot.data_quants[tplot_var].extras['static_tavg'])\n\n # (hasattr(sys, 'ps1')) checks to see if we're in ipython\n # plots the plots!\n if testing:\n return\n if not (hasattr(sys, 'ps1')) or not hasattr(QtCore, 'PYQT_VERSION'):\n QtGui.QApplication.instance().exec_()\n\n return", "response": "This function displays the tplot variables stored in memory."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef static2dplot_timeaveraged(var, time):\n\n # Grab names of data loaded in as tplot variables.\n names = list(pytplot.data_quants.keys())\n # Get data we'll actually work with here.\n valid_variables = tplot_utilities.get_data(names)\n\n # Don't plot anything unless we have spectrograms with which to work.\n if valid_variables:\n # Get z label\n labels = tplot_utilities.get_labels_axis_types(names)\n\n # Put together data in easy-to-access format for plots.\n data = {}\n for name in valid_variables:\n bins = tplot_utilities.get_bins(name)\n time_values, z_values = tplot_utilities.get_z_t_values(name)\n data[name] = [bins, z_values, time_values]\n\n # Set up the 2D static plot\n pytplot.static_tavg_window = pg.GraphicsWindow()\n pytplot.static_tavg_window.resize(1000, 600)\n pytplot.static_tavg_window.setWindowTitle('Time-Averaged Values Static Window')\n plot = pytplot.static_tavg_window.addPlot(title='2D Static Plot for Time-Averaged Values', row=0, col=0)\n # Make it so that whenever this first starts up, you just have an empty plot\n plot_data = plot.plot([], [])\n\n if var in valid_variables:\n # Get min/max values of data's time range (in both datetime and seconds since epoch)\n t_min = np.nanmin(time_values)\n t_min_str = tplot_utilities.int_to_str(np.nanmin(time_values))\n t_min_conv_back = tplot_utilities.str_to_int(t_min_str)\n t_max = np.nanmax(time_values)\n t_max_str = tplot_utilities.int_to_str(np.nanmax(time_values))\n t_max_conv_back = tplot_utilities.str_to_int(t_max_str)\n # Convert user input to seconds since epoch\n user_time = [tplot_utilities.str_to_int(i) for i in time]\n\n # Covering situation where user entered a time not in the dataset!\n # As long as they used a time in the dataset, this will not trigger.\n for t, datetime in enumerate(user_time):\n if datetime not in range(t_min_conv_back, t_max_conv_back+1):\n while True:\n try:\n if t == 0:\n time_bound = 'left bound'\n else:\n time_bound = 'right bound'\n user_time[t] = tplot_utilities.str_to_int(input(\n 'Chosen {} time [{}] not in range of data [{} to {}]. Input new time (%Y-%m-%d %H:%M:%S).'.format(\n time_bound, tplot_utilities.int_to_str(datetime), t_min_str, t_max_str)))\n except ValueError:\n continue\n else:\n if user_time[t] not in range(int(t_min), int(t_max)):\n continue\n else:\n break\n\n # Get index of the time closest to the user's time choice\n time_array = np.array(data[var][2])\n array = np.asarray(time_array)\n idx = [(np.abs(array - i)).argmin() for i in user_time]\n\n # Average values based on the chosen time range's indices\n time_diff = abs(idx[0]-idx[1])\n # Make sure to account for edge problem\n if idx[1] != -1:\n y_values_slice = data[name][1][idx[0]:idx[1]+1]\n else:\n y_values_slice = data[name][1][idx[0]:]\n y_values_avgd = np.nansum(y_values_slice, axis=0)/np.float(time_diff)\n\n # If user indicated they wanted the interactive plot's axes to be logged, log 'em.\n # But first make sure that values in x and y are loggable!\n x_axis = False\n y_axis = False\n # Checking x axis\n if np.nanmin(data[name][0][:]) < 0:\n print('Negative data is incompatible with log plotting.')\n elif np.nanmin(data[name][0][:]) >= 0 and labels[name][2] == 'log':\n x_axis = True\n # Checking y axis\n if np.nanmin(list(data[name][1][idx[0]])) < 0 or np.nanmin(list(data[name][1][idx[1]])) < 0:\n print('Negative data is incompatible with log plotting')\n elif np.nanmin(list(data[name][1][idx[0]])) >= 0 and np.nanmin(list(data[name][1][idx[1]])) >= 0 and \\\n labels[name][3] == 'log':\n y_axis = True\n\n # Set plot labels\n plot.setLabel('bottom', '{}'.format(labels[name][0]))\n plot.setLabel('left', '{}'.format(labels[name][1]))\n plot.setLogMode(x=x_axis, y=y_axis)\n # Update x and y range if user modified it\n tplot_utilities.set_x_range(name, x_axis, plot)\n tplot_utilities.set_y_range(name, y_axis, plot)\n # Plot data based on time we're hovering over\n plot_data.setData(data[var][0][:], y_values_avgd)", "response": "This function will plot the static 2D static plot for the specified variable and time."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef timestamp(val): \n \n \n \n if val is 'on':\n todaystring = datetime.datetime.now().strftime('%Y-%m-%d %H%M%S')\n extra_layouts['time_stamp'] = todaystring\n else:\n if 'time_stamp' in extra_layouts:\n del extra_layouts['time_stamp']\n \n return", "response": "This function will turn on a time stamp that shows up at the bottom of every generated plot."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef paint(self, p, *args):\n '''\n I have no idea why, but we need to generate the picture after painting otherwise \n it draws incorrectly. \n '''\n if self.picturenotgened:\n self.generatePicture(self.getBoundingParents()[0].rect())\n self.picturenotgened = False\n pg.ImageItem.paint(self, p, *args)\n self.generatePicture(self.getBoundingParents()[0].rect())", "response": "This method is called by the PGImageItem class when painting. It generates the picture if needed."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset the image of the item.", "response": "def setImage(self, image=None, autoLevels=None, **kargs):\n \"\"\"\n Same this as ImageItem.setImage, but we don't update the drawing\n \"\"\"\n \n profile = debug.Profiler()\n\n gotNewData = False\n if image is None:\n if self.image is None:\n return\n else:\n gotNewData = True\n shapeChanged = (self.image is None or image.shape != self.image.shape)\n image = image.view(np.ndarray)\n if self.image is None or image.dtype != self.image.dtype:\n self._effectiveLut = None\n self.image = image\n if self.image.shape[0] > 2**15-1 or self.image.shape[1] > 2**15-1:\n if 'autoDownsample' not in kargs:\n kargs['autoDownsample'] = True\n if shapeChanged:\n self.prepareGeometryChange()\n self.informViewBoundsChanged()\n\n profile()\n\n if autoLevels is None:\n if 'levels' in kargs:\n autoLevels = False\n else:\n autoLevels = True\n if autoLevels:\n img = self.image\n while img.size > 2**16:\n img = img[::2, ::2]\n mn, mx = img.min(), img.max()\n if mn == mx:\n mn = 0\n mx = 255\n kargs['levels'] = [mn,mx]\n\n profile()\n\n self.setOpts(update=False, **kargs)\n\n profile()\n\n self.qimage = None\n self.update()\n\n profile()\n\n if gotNewData:\n self.sigImageChanged.emit()"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef getPaintItems(self, root=None):\n if root is None:\n root = self.item\n preItems = []\n postItems = []\n if isinstance(root, QtGui.QGraphicsScene):\n childs = [i for i in root.items() if i.parentItem() is None]\n rootItem = []\n else:\n # CHANGE: For GraphicsLayouts, there is no function for childItems(), so I just\n # replaced it with .items()\n try:\n childs = root.childItems()\n except:\n childs = root.items()\n rootItem = [root]\n childs.sort(key=lambda a: a.zValue())\n while len(childs) > 0:\n ch = childs.pop(0)\n tree = self.getPaintItems(ch)\n\n if int(ch.flags() & ch.ItemStacksBehindParent) > 0 or (\n ch.zValue() < 0 and int(ch.flags() & ch.ItemNegativeZStacksBehindParent) > 0):\n preItems.extend(tree)\n else:\n postItems.extend(tree)\n return preItems + rootItem + postItems", "response": "Return a list of all items that should be painted in the correct order."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nruns normalization on a network.", "response": "def run_norm(net, df=None, norm_type='zscore', axis='row', keep_orig=False):\n '''\n A dataframe (more accurately a dictionary of dataframes, e.g. mat,\n mat_up...) can be passed to run_norm and a normalization will be run (\n e.g. zscore) on either the rows or columns\n '''\n\n # df here is actually a dictionary of several dataframes, 'mat', 'mat_orig',\n # etc\n if df is None:\n df = net.dat_to_df()\n\n if norm_type == 'zscore':\n df = zscore_df(df, axis, keep_orig)\n\n if norm_type == 'qn':\n df = qn_df(df, axis, keep_orig)\n\n net.df_to_dat(df)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef qn_df(df, axis='row', keep_orig=False):\n '''\n do quantile normalization of a dataframe dictionary, does not write to net\n '''\n df_qn = {}\n\n for mat_type in df:\n inst_df = df[mat_type]\n\n # using transpose to do row qn\n if axis == 'row':\n inst_df = inst_df.transpose()\n\n missing_values = inst_df.isnull().values.any()\n\n # make mask of missing values\n if missing_values:\n\n # get nan mask\n missing_mask = pd.isnull(inst_df)\n\n # tmp fill in na with zero, will not affect qn\n inst_df = inst_df.fillna(value=0)\n\n # calc common distribution\n common_dist = calc_common_dist(inst_df)\n\n # swap in common distribution\n inst_df = swap_in_common_dist(inst_df, common_dist)\n\n # swap back in missing values\n if missing_values:\n inst_df = inst_df.mask(missing_mask, other=np.nan)\n\n # using transpose to do row qn\n if axis == 'row':\n inst_df = inst_df.transpose()\n\n df_qn[mat_type] = inst_df\n\n return df_qn", "response": "do quantile normalization of a dataframe dictionary does not write to net\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef calc_common_dist(df):\n '''\n calculate a common distribution (for col qn only) that will be used to qn\n '''\n\n # axis is col\n tmp_arr = np.array([])\n\n col_names = df.columns.tolist()\n\n for inst_col in col_names:\n\n # sort column\n tmp_vect = df[inst_col].sort_values(ascending=False).values\n\n # stacking rows vertically (will transpose)\n if tmp_arr.shape[0] == 0:\n tmp_arr = tmp_vect\n else:\n tmp_arr = np.vstack((tmp_arr, tmp_vect))\n\n tmp_arr = tmp_arr.transpose()\n\n common_dist = tmp_arr.mean(axis=1)\n\n return common_dist", "response": "calculate a common distribution for col qn only"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef zscore_df(df, axis='row', keep_orig=False):\n '''\n take the zscore of a dataframe dictionary, does not write to net (self)\n '''\n df_z = {}\n\n for mat_type in df:\n if keep_orig and mat_type == 'mat':\n mat_orig = deepcopy(df[mat_type])\n\n inst_df = df[mat_type]\n\n if axis == 'row':\n inst_df = inst_df.transpose()\n\n df_z[mat_type] = (inst_df - inst_df.mean())/inst_df.std()\n\n if axis == 'row':\n df_z[mat_type] = df_z[mat_type].transpose()\n\n if keep_orig:\n df_z['mat_orig'] = mat_orig\n\n return df_z", "response": "take the zscore of a dataframe dictionary does not write to net"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncalculate pvalue of category closeness \u2192 calculate pvalue of category closeness \u2192 calculate pvalue of category closeness \u2192 calculate pvalue of category closeness \u2192 calculate pvalue of category closeness \u2192 calculate pvalue of category closeness \u2192 calculate pvalue of category closeness \u2192 calculate pvalue of category closeness", "response": "def main(net):\n '''\n calculate pvalue of category closeness\n '''\n # calculate the distance between the data points within the same category and\n # compare to null distribution\n for inst_rc in ['row', 'col']:\n\n inst_nodes = deepcopy(net.dat['nodes'][inst_rc])\n\n inst_index = deepcopy(net.dat['node_info'][inst_rc]['clust'])\n\n # reorder based on clustered order\n inst_nodes = [ inst_nodes[i] for i in inst_index]\n\n # make distance matrix dataframe\n dm = dist_matrix_lattice(inst_nodes)\n\n node_infos = list(net.dat['node_info'][inst_rc].keys())\n\n all_cats = []\n for inst_info in node_infos:\n if 'dict_cat_' in inst_info:\n all_cats.append(inst_info)\n\n for cat_dict in all_cats:\n\n tmp_dict = net.dat['node_info'][inst_rc][cat_dict]\n\n pval_name = cat_dict.replace('dict_','pval_')\n net.dat['node_info'][inst_rc][pval_name] = {}\n\n for cat_name in tmp_dict:\n\n subset = tmp_dict[cat_name]\n\n inst_median = calc_median_dist_subset(dm, subset)\n\n hist = calc_hist_distances(dm, subset, inst_nodes)\n\n pval = 0\n\n for i in range(len(hist['prob'])):\n if i == 0:\n pval = hist['prob'][i]\n if i >= 1:\n if inst_median >= hist['bins'][i]:\n pval = pval + hist['prob'][i]\n\n net.dat['node_info'][inst_rc][pval_name][cat_name] = pval"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef df_to_dat(net, df, define_cat_colors=False):\n '''\n This is always run when data is loaded.\n '''\n from . import categories\n\n # check if df has unique values\n df['mat'] = make_unique_labels.main(net, df['mat'])\n\n net.dat['mat'] = df['mat'].values\n net.dat['nodes']['row'] = df['mat'].index.tolist()\n net.dat['nodes']['col'] = df['mat'].columns.tolist()\n\n for inst_rc in ['row', 'col']:\n\n if type(net.dat['nodes'][inst_rc][0]) is tuple:\n # get the number of categories from the length of the tuple\n # subtract 1 because the name is the first element of the tuple\n num_cat = len(net.dat['nodes'][inst_rc][0]) - 1\n\n net.dat['node_info'][inst_rc]['full_names'] = net.dat['nodes']\\\n [inst_rc]\n\n for inst_rcat in range(num_cat):\n net.dat['node_info'][inst_rc]['cat-' + str(inst_rcat)] = \\\n [i[inst_rcat + 1] for i in net.dat['nodes'][inst_rc]]\n\n net.dat['nodes'][inst_rc] = [i[0] for i in net.dat['nodes'][inst_rc]]\n\n if 'mat_up' in df:\n net.dat['mat_up'] = df['mat_up'].values\n net.dat['mat_dn'] = df['mat_dn'].values\n\n if 'mat_orig' in df:\n net.dat['mat_orig'] = df['mat_orig'].values\n\n categories.dict_cat(net, define_cat_colors=define_cat_colors)", "response": "This function takes a DataFrame and converts it to a dictionary of data structures."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nconvert list to numpy array", "response": "def mat_to_numpy_arr(self):\n ''' convert list to numpy array - numpy arrays can not be saved as json '''\n import numpy as np\n self.dat['mat'] = np.asarray(self.dat['mat'])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nclustering net. dat and make visualization json and net. viz.", "response": "def cluster_row_and_col(net, dist_type='cosine', linkage_type='average',\n dendro=True, run_clustering=True, run_rank=True,\n ignore_cat=False, calc_cat_pval=False, links=False):\n ''' cluster net.dat and make visualization json, net.viz.\n optionally leave out dendrogram colorbar groups with dendro argument '''\n\n import scipy\n from copy import deepcopy\n from scipy.spatial.distance import pdist\n from . import categories, make_viz, cat_pval\n\n dm = {}\n for inst_rc in ['row', 'col']:\n\n tmp_mat = deepcopy(net.dat['mat'])\n dm[inst_rc] = calc_distance_matrix(tmp_mat, inst_rc, dist_type)\n\n # save directly to dat structure\n node_info = net.dat['node_info'][inst_rc]\n\n node_info['ini'] = list(range( len(net.dat['nodes'][inst_rc]), -1, -1))\n\n # cluster\n if run_clustering is True:\n node_info['clust'], node_info['group'] = \\\n clust_and_group(net, dm[inst_rc], linkage_type=linkage_type)\n else:\n dendro = False\n node_info['clust'] = node_info['ini']\n\n # sorting\n if run_rank is True:\n node_info['rank'] = sort_rank_nodes(net, inst_rc, 'sum')\n node_info['rankvar'] = sort_rank_nodes(net, inst_rc, 'var')\n else:\n node_info['rank'] = node_info['ini']\n node_info['rankvar'] = node_info['ini']\n\n ##################################\n if ignore_cat is False:\n categories.calc_cat_clust_order(net, inst_rc)\n\n if calc_cat_pval is True:\n cat_pval.main(net)\n\n # make the visualization json\n make_viz.viz_json(net, dendro, links)\n\n return dm"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfinding out how many row and col categories are available \u00b0C", "response": "def check_categories(lines):\n '''\n find out how many row and col categories are available\n '''\n # count the number of row categories\n rcat_line = lines[0].split('\\t')\n\n # calc the number of row names and categories\n num_rc = 0\n found_end = False\n\n # skip first tab\n for inst_string in rcat_line[1:]:\n if inst_string == '':\n if found_end is False:\n num_rc = num_rc + 1\n else:\n found_end = True\n\n max_rcat = 15\n if max_rcat > len(lines):\n max_rcat = len(lines) - 1\n\n num_cc = 0\n for i in range(max_rcat):\n ccat_line = lines[i + 1].split('\\t')\n\n # make sure that line has length greater than one to prevent false cats from\n # trailing new lines at end of matrix\n if ccat_line[0] == '' and len(ccat_line) > 1:\n num_cc = num_cc + 1\n\n num_labels = {}\n num_labels['row'] = num_rc + 1\n num_labels['col'] = num_cc + 1\n\n return num_labels"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef dict_cat(net, define_cat_colors=False):\n '''\n make a dictionary of node-category associations\n '''\n\n # print('---------------------------------')\n # print('---- dict_cat: before setting cat colors')\n # print('---------------------------------\\n')\n # print(define_cat_colors)\n # print(net.viz['cat_colors'])\n\n net.persistent_cat = True\n\n for inst_rc in ['row', 'col']:\n inst_keys = list(net.dat['node_info'][inst_rc].keys())\n all_cats = [x for x in inst_keys if 'cat-' in x]\n\n for inst_name_cat in all_cats:\n\n dict_cat = {}\n tmp_cats = net.dat['node_info'][inst_rc][inst_name_cat]\n tmp_nodes = net.dat['nodes'][inst_rc]\n\n for i in range(len(tmp_cats)):\n inst_cat = tmp_cats[i]\n inst_node = tmp_nodes[i]\n\n if inst_cat not in dict_cat:\n dict_cat[inst_cat] = []\n\n dict_cat[inst_cat].append(inst_node)\n\n tmp_name = 'dict_' + inst_name_cat.replace('-', '_')\n net.dat['node_info'][inst_rc][tmp_name] = dict_cat\n\n # merge with old cat_colors by default\n cat_colors = net.viz['cat_colors']\n\n if define_cat_colors == True:\n cat_number = 0\n\n for inst_rc in ['row', 'col']:\n\n inst_keys = list(net.dat['node_info'][inst_rc].keys())\n all_cats = [x for x in inst_keys if 'cat-' in x]\n\n for cat_index in all_cats:\n\n if cat_index not in cat_colors[inst_rc]:\n cat_colors[inst_rc][cat_index] = {}\n\n cat_names = sorted(list(set(net.dat['node_info'][inst_rc][cat_index])))\n\n # loop through each category name and assign a color\n for tmp_name in cat_names:\n\n # using the same rules as the front-end to define cat_colors\n inst_color = get_cat_color(cat_number + cat_names.index(tmp_name))\n\n check_name = tmp_name\n\n # check if category is string type and non-numeric\n try:\n float(check_name)\n is_string_cat = False\n except:\n is_string_cat = True\n\n if is_string_cat == True:\n # check for default non-color\n if ': ' in check_name:\n check_name = check_name.split(': ')[1]\n\n # if check_name == 'False' or check_name == 'false':\n if 'False' in check_name or 'false' in check_name:\n inst_color = '#eee'\n\n if 'Not ' in check_name:\n inst_color = '#eee'\n\n # print('cat_colors')\n # print('----------')\n # print(cat_colors[inst_rc][cat_index])\n\n # do not overwrite old colors\n if tmp_name not in cat_colors[inst_rc][cat_index] and is_string_cat:\n\n cat_colors[inst_rc][cat_index][tmp_name] = inst_color\n # print('overwrite: ' + tmp_name + ' -> ' + str(inst_color))\n\n cat_number = cat_number + 1\n\n net.viz['cat_colors'] = cat_colors", "response": "make a dictionary of node - category associations\n \u00b0C"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncalculates the clustering ordering of the cluster categories in the given node info.", "response": "def calc_cat_clust_order(net, inst_rc):\n '''\n cluster category subset of data\n '''\n from .__init__ import Network\n from copy import deepcopy\n from . import calc_clust, run_filter\n\n inst_keys = list(net.dat['node_info'][inst_rc].keys())\n all_cats = [x for x in inst_keys if 'cat-' in x]\n\n if len(all_cats) > 0:\n\n for inst_name_cat in all_cats:\n\n tmp_name = 'dict_' + inst_name_cat.replace('-', '_')\n dict_cat = net.dat['node_info'][inst_rc][tmp_name]\n\n unordered_cats = dict_cat.keys()\n\n ordered_cats = order_categories(unordered_cats)\n\n # this is the ordering of the columns based on their category, not\n # including their clustering ordering within category\n all_cat_orders = []\n tmp_names_list = []\n for inst_cat in ordered_cats:\n\n inst_nodes = dict_cat[inst_cat]\n\n tmp_names_list.extend(inst_nodes)\n\n # cat_net = deepcopy(Network())\n\n # cat_net.dat['mat'] = deepcopy(net.dat['mat'])\n # cat_net.dat['nodes'] = deepcopy(net.dat['nodes'])\n\n # cat_df = cat_net.dat_to_df()\n\n # sub_df = {}\n # if inst_rc == 'col':\n # sub_df['mat'] = cat_df['mat'][inst_nodes]\n # elif inst_rc == 'row':\n # # need to transpose df\n # cat_df['mat'] = cat_df['mat'].transpose()\n # sub_df['mat'] = cat_df['mat'][inst_nodes]\n # sub_df['mat'] = sub_df['mat'].transpose()\n\n # # filter matrix before clustering\n # ###################################\n # threshold = 0.0001\n # sub_df = run_filter.df_filter_row_sum(sub_df, threshold)\n # sub_df = run_filter.df_filter_col_sum(sub_df, threshold)\n\n # # load back to dat\n # cat_net.df_to_dat(sub_df)\n\n # cat_mat_shape = cat_net.dat['mat'].shape\n\n # print('***************')\n # try:\n # if cat_mat_shape[0]>1 and cat_mat_shape[1] > 1 and all_are_numbers == False:\n\n # calc_clust.cluster_row_and_col(cat_net, 'cos')\n # inst_cat_order = cat_net.dat['node_info'][inst_rc]['clust']\n # else:\n # inst_cat_order = list(range(len(cat_net.dat['nodes'][inst_rc])))\n\n # except:\n # inst_cat_order = list(range(len(cat_net.dat['nodes'][inst_rc])))\n\n\n # prev_order_len = len(all_cat_orders)\n\n # # add prev order length to the current order number\n # inst_cat_order = [i + prev_order_len for i in inst_cat_order]\n # all_cat_orders.extend(inst_cat_order)\n\n # # generate ordered list of row/col names, which will be used to\n # # assign the order to specific nodes\n # names_clust_list = [x for (y, x) in sorted(zip(all_cat_orders,\n # tmp_names_list))]\n\n names_clust_list = tmp_names_list\n\n # calc category-cluster order\n final_order = []\n\n for i in range(len(net.dat['nodes'][inst_rc])):\n\n inst_node_name = net.dat['nodes'][inst_rc][i]\n inst_node_num = names_clust_list.index(inst_node_name)\n\n final_order.append(inst_node_num)\n\n inst_index_cat = inst_name_cat.replace('-', '_') + '_index'\n\n net.dat['node_info'][inst_rc][inst_index_cat] = final_order"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef order_categories(unordered_cats):\n '''\n If categories are strings, then simple ordering is fine.\n If categories are values then I'll need to order based on their values.\n The final ordering is given as the original categories (including titles) in a\n ordered list.\n '''\n\n no_titles = remove_titles(unordered_cats)\n\n all_are_numbers = check_all_numbers(no_titles)\n\n if all_are_numbers:\n ordered_cats = order_cats_based_on_values(unordered_cats, no_titles)\n else:\n ordered_cats = sorted(unordered_cats)\n\n return ordered_cats", "response": "Order the categories in a\n."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nloads file as a string.", "response": "def load_file_as_string(self, file_string, filename=''):\n '''\n Load file as a string.\n '''\n load_data.load_file_as_string(self, file_string, filename=filename)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nloading Clustergrammer s dat format saved as JSON.", "response": "def load_data_file_to_net(self, filename):\n '''\n Load Clustergrammer's dat format (saved as JSON).\n '''\n inst_dat = self.load_json_to_dict(filename)\n load_data.load_data_to_net(self, inst_dat)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nloads Pandas DataFrame. Load Pandas DataFrame.", "response": "def load_df(self, df):\n '''\n Load Pandas DataFrame.\n '''\n # self.__init__()\n self.reset()\n\n df_dict = {}\n df_dict['mat'] = deepcopy(df)\n # always define category colors if applicable when loading a df\n data_formats.df_to_dat(self, df_dict, define_cat_colors=True)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nload Pandas DataFrame into a dat file.", "response": "def df_to_dat(self, df, define_cat_colors=False):\n '''\n Load Pandas DataFrame (will be deprecated).\n '''\n data_formats.df_to_dat(self, df, define_cat_colors)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef widget(self, which_viz='viz'):\n '''\n Generate a widget visualization using the widget. The export_viz_to_widget\n method passes the visualization JSON to the instantiated widget, which is\n returned and visualized on the front-end.\n '''\n if hasattr(self, 'widget_class') == True:\n\n # run clustering if necessary\n if len(self.viz['row_nodes']) == 0:\n self.cluster()\n\n self.widget_instance = self.widget_class(network = self.export_viz_to_widget(which_viz))\n\n return self.widget_instance\n else:\n print('Can not make widget because Network has no attribute widget_class')\n print('Please instantiate Network with clustergrammer_widget using: Network(clustergrammer_widget)')", "response": "Generate a widget visualization using the widget class"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef widget_df(self):\n '''\n Export a DataFrame from the front-end visualization. For instance, a user\n can filter to show only a single cluster using the dendrogram and then\n get a dataframe of this cluster using the widget_df method.\n '''\n\n if hasattr(self, 'widget_instance') == True:\n\n if self.widget_instance.mat_string != '':\n\n tmp_net = deepcopy(Network())\n\n df_string = self.widget_instance.mat_string\n\n tmp_net.load_file_as_string(df_string)\n\n df = tmp_net.export_df()\n\n return df\n\n else:\n return self.export_df()\n\n else:\n if hasattr(self, 'widget_class') == True:\n print('Please make the widget before exporting the widget DataFrame.')\n print('Do this using the widget method: net.widget()')\n\n else:\n print('Can not make widget because Network has no attribute widget_class')\n print('Please instantiate Network with clustergrammer_widget using: Network(clustergrammer_widget)')", "response": "Export a DataFrame from the front - end visualization."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef write_json_to_file(self, net_type, filename, indent='no-indent'):\n '''\n Save dat or viz as a JSON to file.\n '''\n export_data.write_json_to_file(self, net_type, filename, indent)", "response": "Save dat or viz as a JSON to file."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nfilters a network based on the sum across rows or columns.", "response": "def filter_sum(self, inst_rc, threshold, take_abs=True):\n '''\n Filter a network's rows or columns based on the sum across rows or columns.\n '''\n inst_df = self.dat_to_df()\n if inst_rc == 'row':\n inst_df = run_filter.df_filter_row_sum(inst_df, threshold, take_abs)\n elif inst_rc == 'col':\n inst_df = run_filter.df_filter_col_sum(inst_df, threshold, take_abs)\n self.df_to_dat(inst_df)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nfilters the matrix rows or columns based on sum / variance and only keep the top N.", "response": "def filter_N_top(self, inst_rc, N_top, rank_type='sum'):\n '''\n Filter the matrix rows or columns based on sum/variance, and only keep the top\n N.\n '''\n inst_df = self.dat_to_df()\n\n inst_df = run_filter.filter_N_top(inst_rc, inst_df, N_top, rank_type)\n\n self.df_to_dat(inst_df)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef filter_threshold(self, inst_rc, threshold, num_occur=1):\n '''\n Filter the matrix rows or columns based on num_occur values being above a\n threshold (in absolute value).\n '''\n inst_df = self.dat_to_df()\n\n inst_df = run_filter.filter_threshold(inst_df, inst_rc, threshold,\n num_occur)\n\n self.df_to_dat(inst_df)", "response": "Filter the matrix rows or columns based on num_occur values being above threshold."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfiltering the matrix based on their category.", "response": "def filter_cat(self, axis, cat_index, cat_name):\n '''\n Filter the matrix based on their category. cat_index is the index of the category, the first category has index=1.\n '''\n run_filter.filter_cat(self, axis, cat_index, cat_name)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef clip(self, lower=None, upper=None):\n '''\n Trim values at input thresholds using pandas function\n '''\n df = self.export_df()\n df = df.clip(lower=lower, upper=upper)\n self.load_df(df)", "response": "Trim values at input thresholds using pandas function\n "} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nnormalizes the matrix rows or columns using Z - score or Quantile Normalization.", "response": "def normalize(self, df=None, norm_type='zscore', axis='row', keep_orig=False):\n '''\n Normalize the matrix rows or columns using Z-score (zscore) or Quantile Normalization (qn). Users can optionally pass in a DataFrame to be normalized (and this will be incorporated into the Network object).\n '''\n normalize_fun.run_norm(self, df, norm_type, axis, keep_orig)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef random_sample(self, num_samples, df=None, replace=False, weights=None, random_state=100, axis='row'):\n '''\n Return random sample of matrix.\n '''\n\n if df is None:\n df = self.dat_to_df()\n\n if axis == 'row':\n axis = 0\n if axis == 'col':\n axis = 1\n\n df = self.export_df()\n df = df.sample(n=num_samples, replace=replace, weights=weights, random_state=random_state, axis=axis)\n\n self.load_df(df)", "response": "Return random samples from the matrix."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd categories to rows or columns of the specified axis.", "response": "def add_cats(self, axis, cat_data):\n '''\n Add categories to rows or columns using cat_data array of objects. Each object in cat_data is a dictionary with one key (category title) and value (rows/column names) that have this category. Categories will be added onto the existing categories and will be added in the order of the objects in the array.\n\n Example ``cat_data``::\n\n\n [\n {\n \"title\": \"First Category\",\n \"cats\": {\n \"true\": [\n \"ROS1\",\n \"AAK1\"\n ]\n }\n },\n {\n \"title\": \"Second Category\",\n \"cats\": {\n \"something\": [\n \"PDK4\"\n ]\n }\n }\n ]\n\n\n '''\n for inst_data in cat_data:\n categories.add_cats(self, axis, inst_data)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef enrichrgram(self, lib, axis='row'):\n '''\n Add Enrichr gene enrichment results to your visualization (where your rows\n are genes). Run enrichrgram before clustering to incldue enrichment results\n as row categories. Enrichrgram can also be run on the front-end using the\n Enrichr logo at the top left.\n\n Set lib to the Enrichr library that you want to use for enrichment analysis.\n Libraries included:\n\n * ChEA_2016\n * KEA_2015\n * ENCODE_TF_ChIP-seq_2015\n * ENCODE_Histone_Modifications_2015\n * Disease_Perturbations_from_GEO_up\n * Disease_Perturbations_from_GEO_down\n * GO_Molecular_Function_2015\n * GO_Biological_Process_2015\n * GO_Cellular_Component_2015\n * Reactome_2016\n * KEGG_2016\n * MGI_Mammalian_Phenotype_Level_4\n * LINCS_L1000_Chem_Pert_up\n * LINCS_L1000_Chem_Pert_down\n\n '''\n\n df = self.export_df()\n df, bar_info = enr_fun.add_enrichr_cats(df, axis, lib)\n self.load_df(df)\n\n self.dat['enrichrgram_lib'] = lib\n self.dat['row_cat_bars'] = bar_info", "response": "Add enrichrgram results to the visualization."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nloading gene expression data from 10x in sparse matrix format and returns a Pandas dataframe containing the gene name and unique id.", "response": "def load_gene_exp_to_df(inst_path):\n '''\n Loads gene expression data from 10x in sparse matrix format and returns a\n Pandas dataframe\n '''\n\n import pandas as pd\n from scipy import io\n from scipy import sparse\n from ast import literal_eval as make_tuple\n\n # matrix\n Matrix = io.mmread( inst_path + 'matrix.mtx')\n mat = Matrix.todense()\n\n # genes\n filename = inst_path + 'genes.tsv'\n f = open(filename, 'r')\n lines = f.readlines()\n f.close()\n\n # # add unique id to all genes\n # genes = []\n # unique_id = 0\n # for inst_line in lines:\n # inst_line = inst_line.strip().split()\n\n # if len(inst_line) > 1:\n # inst_gene = inst_line[1]\n # else:\n # inst_gene = inst_line[0]\n\n # genes.append(inst_gene + '_' + str(unique_id))\n # unique_id = unique_id + 1\n\n # add unique id only to duplicate genes\n ini_genes = []\n for inst_line in lines:\n inst_line = inst_line.strip().split()\n if len(inst_line) > 1:\n inst_gene = inst_line[1]\n else:\n inst_gene = inst_line[0]\n ini_genes.append(inst_gene)\n\n gene_name_count = pd.Series(ini_genes).value_counts()\n duplicate_genes = gene_name_count[gene_name_count > 1].index.tolist()\n\n dup_index = {}\n genes = []\n for inst_row in ini_genes:\n\n # add index to non-unique genes\n if inst_row in duplicate_genes:\n\n # calc_non-unque index\n if inst_row not in dup_index:\n dup_index[inst_row] = 1\n else:\n dup_index[inst_row] = dup_index[inst_row] + 1\n\n new_row = inst_row + '_' + str(dup_index[inst_row])\n\n else:\n new_row = inst_row\n\n genes.append(new_row)\n\n # barcodes\n filename = inst_path + 'barcodes.tsv'\n f = open(filename, 'r')\n lines = f.readlines()\n f.close()\n\n cell_barcodes = []\n for inst_bc in lines:\n inst_bc = inst_bc.strip().split('\\t')\n\n # remove dash from barcodes if necessary\n if '-' in inst_bc[0]:\n inst_bc[0] = inst_bc[0].split('-')[0]\n\n cell_barcodes.append(inst_bc[0])\n\n # parse tuples if necessary\n try:\n cell_barcodes = [make_tuple(x) for x in cell_barcodes]\n except:\n pass\n\n try:\n genes = [make_tuple(x) for x in genes]\n except:\n pass\n\n # make dataframe\n df = pd.DataFrame(mat, index=genes, columns=cell_barcodes)\n\n return df"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncalculate the similarity of samples from the same and different categories.", "response": "def sim_same_and_diff_category_samples(self, df, cat_index=1, dist_type='cosine',\n equal_var=False, plot_roc=True,\n precalc_dist=False, calc_roc=True):\n '''\n Calculate the similarity of samples from the same and different categories. The\n cat_index gives the index of the category, where 1 in the first category\n '''\n\n cols = df.columns.tolist()\n\n if type(precalc_dist) == bool:\n # compute distnace between rows (transpose to get cols as rows)\n dist_arr = 1 - pdist(df.transpose(), metric=dist_type)\n else:\n dist_arr = precalc_dist\n\n # generate sample names with categories\n sample_combos = list(combinations(range(df.shape[1]),2))\n\n sample_names = [str(ind) + '_same' if cols[x[0]][cat_index] == cols[x[1]][cat_index] else str(ind) + '_different' for ind, x in enumerate(sample_combos)]\n\n ser_dist = pd.Series(data=dist_arr, index=sample_names)\n\n # find same-cat sample comparisons\n same_cat = [x for x in sample_names if x.split('_')[1] == 'same']\n\n # find diff-cat sample comparisons\n diff_cat = [x for x in sample_names if x.split('_')[1] == 'different']\n\n # make series of same and diff category sample comparisons\n ser_same = ser_dist[same_cat]\n ser_same.name = 'Same Category'\n ser_diff = ser_dist[diff_cat]\n ser_diff.name = 'Different Category'\n\n sim_dict = {}\n roc_data = {}\n sim_data = {}\n\n sim_dict['same'] = ser_same\n sim_dict['diff'] = ser_diff\n\n pval_dict = {}\n ttest_stat, pval_dict['ttest'] = ttest_ind(ser_diff, ser_same, equal_var=equal_var)\n\n ttest_stat, pval_dict['mannwhitney'] = mannwhitneyu(ser_diff, ser_same)\n\n if calc_roc:\n # calc AUC\n true_index = list(np.ones(sim_dict['same'].shape[0]))\n false_index = list(np.zeros(sim_dict['diff'].shape[0]))\n y_true = true_index + false_index\n\n true_val = list(sim_dict['same'].get_values())\n false_val = list(sim_dict['diff'].get_values())\n y_score = true_val + false_val\n\n fpr, tpr, thresholds = roc_curve(y_true, y_score)\n\n inst_auc = auc(fpr, tpr)\n\n if plot_roc:\n plt.figure()\n plt.plot(fpr, tpr)\n plt.plot([0, 1], [0, 1], color='navy', linestyle='--')\n plt.figure(figsize=(10,10))\n\n print('AUC', inst_auc)\n\n roc_data['true'] = y_true\n roc_data['score'] = y_score\n roc_data['fpr'] = fpr\n roc_data['tpr'] = tpr\n roc_data['thresholds'] = thresholds\n roc_data['auc'] = inst_auc\n\n sim_data['sim_dict'] = sim_dict\n sim_data['pval_dict'] = pval_dict\n sim_data['roc_data'] = roc_data\n\n return sim_data"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef generate_signatures(self, df_ini, category_level, pval_cutoff=0.05,\n num_top_dims=False, verbose=True, equal_var=False):\n\n ''' Generate signatures for column categories '''\n\n df_t = df_ini.transpose()\n\n # remove columns with constant values\n df_t = df_t.loc[:, (df_t != df_t.iloc[0]).any()]\n\n df = self.row_tuple_to_multiindex(df_t)\n\n cell_types = sorted(list(set(df.index.get_level_values(category_level).tolist())))\n\n keep_genes = []\n keep_genes_dict = {}\n gene_pval_dict = {}\n all_fold_info = {}\n\n for inst_ct in cell_types:\n\n inst_ct_mat = df.xs(key=inst_ct, level=category_level)\n inst_other_mat = df.drop(inst_ct, level=category_level)\n\n # save mean values and fold change\n fold_info = {}\n fold_info['cluster_mean'] = inst_ct_mat.mean()\n fold_info['other_mean'] = inst_other_mat.mean()\n fold_info['log2_fold'] = fold_info['cluster_mean']/fold_info['other_mean']\n fold_info['log2_fold'] = fold_info['log2_fold'].apply(np.log2)\n all_fold_info[inst_ct] = fold_info\n\n inst_stats, inst_pvals = ttest_ind(inst_ct_mat, inst_other_mat, axis=0, equal_var=equal_var)\n\n ser_pval = pd.Series(data=inst_pvals, index=df.columns.tolist()).sort_values()\n\n if num_top_dims == False:\n ser_pval_keep = ser_pval[ser_pval < pval_cutoff]\n else:\n ser_pval_keep = ser_pval[:num_top_dims]\n\n gene_pval_dict[inst_ct] = ser_pval_keep\n\n inst_keep = ser_pval_keep.index.tolist()\n keep_genes.extend(inst_keep)\n keep_genes_dict[inst_ct] = inst_keep\n\n keep_genes = sorted(list(set(keep_genes)))\n\n df_gbm = df.groupby(level=category_level).mean().transpose()\n cols = df_gbm.columns.tolist()\n new_cols = []\n for inst_col in cols:\n new_col = (inst_col, category_level + ': ' + inst_col)\n new_cols.append(new_col)\n df_gbm.columns = new_cols\n\n df_sig = df_gbm.ix[keep_genes]\n\n if len(keep_genes) == 0 and verbose:\n print('found no informative dimensions')\n\n df_gene_pval = pd.concat(gene_pval_dict, axis=1, sort=False)\n\n return df_sig, keep_genes_dict, df_gene_pval, all_fold_info", "response": "Generate signatures for column categories."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef predict_cats_from_sigs(self, df_data_ini, df_sig_ini, dist_type='cosine', predict_level='Predict Category',\n truth_level=1, unknown_thresh=-1):\n ''' Predict category using signature '''\n\n keep_rows = df_sig_ini.index.tolist()\n data_rows = df_data_ini.index.tolist()\n\n common_rows = list(set(data_rows).intersection(keep_rows))\n\n df_data = deepcopy(df_data_ini.ix[common_rows])\n df_sig = deepcopy(df_sig_ini.ix[common_rows])\n\n # calculate sim_mat of df_data and df_sig\n cell_types = df_sig.columns.tolist()\n barcodes = df_data.columns.tolist()\n sim_mat = 1 - pairwise_distances(df_sig.transpose(), df_data.transpose(), metric=dist_type)\n df_sim = pd.DataFrame(data=sim_mat, index=cell_types, columns=barcodes).transpose()\n\n # get the top column value (most similar signature)\n df_sim_top = df_sim.idxmax(axis=1)\n\n # get the maximum similarity of a cell to a cell type definition\n max_sim = df_sim.max(axis=1)\n\n unknown_cells = max_sim[max_sim < unknown_thresh].index.tolist()\n\n # assign unknown cells (need category of same name)\n df_sim_top[unknown_cells] = 'Unknown'\n\n # add predicted category name to top list\n top_list = df_sim_top.get_values()\n top_list = [ predict_level + ': ' + x[0] if type(x) is tuple else predict_level + ': ' + x for x in top_list]\n\n # add cell type category to input data\n df_cat = deepcopy(df_data)\n cols = df_cat.columns.tolist()\n new_cols = []\n\n # check whether the columns have the true category available\n has_truth = False\n if type(cols[0]) is tuple:\n has_truth = True\n\n if has_truth:\n new_cols = [tuple(list(a) + [b]) for a,b in zip(cols, top_list)]\n else:\n new_cols = [tuple([a] + [b]) for a,b in zip(cols, top_list)]\n\n # transfer new categories\n df_cat.columns = new_cols\n\n # keep track of true and predicted labels\n y_info = {}\n y_info['true'] = []\n y_info['pred'] = []\n\n if has_truth:\n y_info['true'] = [x[truth_level].split(': ')[1] for x in cols]\n y_info['pred'] = [x.split(': ')[1] for x in top_list]\n\n return df_cat, df_sim.transpose(), y_info", "response": "Predict a category using a signature."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef confusion_matrix_and_correct_series(self, y_info):\n ''' Generate confusion matrix from y_info '''\n\n\n a = deepcopy(y_info['true'])\n true_count = dict((i, a.count(i)) for i in set(a))\n\n a = deepcopy(y_info['pred'])\n pred_count = dict((i, a.count(i)) for i in set(a))\n\n sorted_cats = sorted(list(set(y_info['true'] + y_info['pred'])))\n conf_mat = confusion_matrix(y_info['true'], y_info['pred'], sorted_cats)\n df_conf = pd.DataFrame(conf_mat, index=sorted_cats, columns=sorted_cats)\n\n total_correct = np.trace(df_conf)\n total_pred = df_conf.sum().sum()\n fraction_correct = total_correct/float(total_pred)\n\n # calculate ser_correct\n correct_list = []\n cat_counts = df_conf.sum(axis=1)\n all_cols = df_conf.columns.tolist()\n for inst_cat in all_cols:\n inst_correct = df_conf[inst_cat].loc[inst_cat] / cat_counts[inst_cat]\n correct_list.append(inst_correct)\n\n ser_correct = pd.Series(data=correct_list, index=all_cols)\n\n populations = {}\n populations['true'] = true_count\n populations['pred'] = pred_count\n\n return df_conf, populations, ser_correct, fraction_correct", "response": "Generate a confusion matrix from y_info and correct the series."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nload data into nodes and mat convert mat to numpy array", "response": "def load_data_to_net(net, inst_net):\n ''' load data into nodes and mat, also convert mat to numpy array'''\n net.dat['nodes'] = inst_net['nodes']\n net.dat['mat'] = inst_net['mat']\n data_formats.mat_to_numpy_arr(net)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef export_net_json(net, net_type, indent='no-indent'):\n ''' export json string of dat '''\n import json\n from copy import deepcopy\n\n if net_type == 'dat':\n exp_dict = deepcopy(net.dat)\n\n if type(exp_dict['mat']) is not list:\n exp_dict['mat'] = exp_dict['mat'].tolist()\n if 'mat_orig' in exp_dict:\n exp_dict['mat_orig'] = exp_dict['mat_orig'].tolist()\n\n elif net_type == 'viz':\n exp_dict = net.viz\n\n elif net_type == 'sim_row':\n exp_dict = net.sim['row']\n\n elif net_type == 'sim_col':\n exp_dict = net.sim['col']\n\n # make json\n if indent == 'indent':\n exp_json = json.dumps(exp_dict, indent=2)\n else:\n exp_json = json.dumps(exp_dict)\n\n return exp_json", "response": "export json string of dat"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef write_matrix_to_tsv(net, filename=None, df=None):\n '''\n This will export the matrix in net.dat or a dataframe (optional df in\n arguments) as a tsv file. Row/column categories will be saved as tuples in\n tsv, which can be read back into the network object.\n '''\n import pandas as pd\n\n if df is None:\n df = net.dat_to_df()\n\n return df['mat'].to_csv(filename, sep='\\t')", "response": "This will write the matrix in net. dat or a dataframe to a tsv file."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef main(net, df=None):\n '''\n Run in load_data module (which runs when file is loaded or dataframe is loaded),\n check for duplicate row/col names, and add index to names if necesary\n '''\n if df is None:\n df = net.export_df()\n\n # rows\n #############\n rows = df.index.tolist()\n if type(rows[0]) is str:\n\n if len(rows) != len(list(set(rows))):\n new_rows = add_index_list(rows)\n df.index = new_rows\n\n elif type(rows[0]) is tuple:\n\n row_names = []\n for inst_row in rows:\n row_names.append(inst_row[0])\n\n if len(row_names) != len(list(set(row_names))):\n row_names = add_index_list(row_names)\n\n # add back to tuple\n new_rows = []\n for inst_index in range(len(rows)):\n inst_row = rows[inst_index]\n new_row = list(inst_row)\n new_row[0] = row_names[inst_index]\n new_row = tuple(new_row)\n new_rows.append(new_row)\n\n df.index = new_rows\n\n # cols\n #############\n cols = df.columns.tolist()\n if type(cols[0]) is str:\n\n # list column names\n if len(cols) != len(list(set(cols))):\n new_cols = add_index_list(cols)\n df.columns = new_cols\n\n elif type(cols[0]) is tuple:\n\n col_names = []\n for inst_col in cols:\n col_names.append(inst_col[0])\n\n if len(col_names) != len(list(set(col_names))):\n col_names = add_index_list(col_names)\n\n # add back to tuple\n new_cols = []\n for inst_index in range(len(cols)):\n inst_col = cols[inst_index]\n new_col = list(inst_col)\n new_col[0] = col_names[inst_index]\n new_col = tuple(new_col)\n new_cols.append(new_col)\n\n df.columns = new_cols\n\n # return dataframe with unique names\n return df", "response": "Main function for the necesary\n module."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef viz_json(net, dendro=True, links=False):\n ''' make the dictionary for the clustergram.js visualization '''\n from . import calc_clust\n import numpy as np\n\n all_dist = calc_clust.group_cutoffs()\n\n for inst_rc in net.dat['nodes']:\n\n inst_keys = net.dat['node_info'][inst_rc]\n all_cats = [x for x in inst_keys if 'cat-' in x]\n\n for i in range(len(net.dat['nodes'][inst_rc])):\n inst_dict = {}\n inst_dict['name'] = net.dat['nodes'][inst_rc][i]\n inst_dict['ini'] = net.dat['node_info'][inst_rc]['ini'][i]\n inst_dict['clust'] = net.dat['node_info'][inst_rc]['clust'].index(i)\n inst_dict['rank'] = net.dat['node_info'][inst_rc]['rank'][i]\n\n if 'rankvar' in inst_keys:\n inst_dict['rankvar'] = net.dat['node_info'][inst_rc]['rankvar'][i]\n\n # fix for similarity matrix\n if len(all_cats) > 0:\n\n for inst_name_cat in all_cats:\n\n actual_cat_name = net.dat['node_info'][inst_rc][inst_name_cat][i]\n inst_dict[inst_name_cat] = actual_cat_name\n\n check_pval = 'pval_'+inst_name_cat.replace('-','_')\n\n if check_pval in net.dat['node_info'][inst_rc]:\n tmp_pval_name = inst_name_cat.replace('-','_') + '_pval'\n inst_dict[tmp_pval_name] = net.dat['node_info'][inst_rc][check_pval][actual_cat_name]\n\n tmp_index_name = inst_name_cat.replace('-', '_') + '_index'\n\n inst_dict[tmp_index_name] = net.dat['node_info'][inst_rc] \\\n [tmp_index_name][i]\n\n\n if len(net.dat['node_info'][inst_rc]['value']) > 0:\n inst_dict['value'] = net.dat['node_info'][inst_rc]['value'][i]\n\n if len(net.dat['node_info'][inst_rc]['info']) > 0:\n inst_dict['info'] = net.dat['node_info'][inst_rc]['info'][i]\n\n if dendro is True:\n inst_dict['group'] = []\n for tmp_dist in all_dist:\n tmp_dist = str(tmp_dist).replace('.', '')\n tmp_append = float(\n net.dat['node_info'][inst_rc]['group'][tmp_dist][i])\n inst_dict['group'].append(tmp_append)\n\n net.viz[inst_rc + '_nodes'].append(inst_dict)\n\n mat_types = ['mat', 'mat_orig', 'mat_info', 'mat_hl', 'mat_up', 'mat_dn']\n\n # save data as links or mat\n ###########################\n if links is True:\n for i in range(len(net.dat['nodes']['row'])):\n for j in range(len(net.dat['nodes']['col'])):\n\n inst_dict = {}\n inst_dict['source'] = i\n inst_dict['target'] = j\n inst_dict['value'] = float(net.dat['mat'][i, j])\n\n if 'mat_up' in net.dat:\n inst_dict['value_up'] = net.dat['mat_up'][i, j]\n inst_dict['value_dn'] = net.dat['mat_dn'][i, j]\n\n if 'mat_orig' in net.dat:\n inst_dict['value_orig'] = net.dat['mat_orig'][i, j]\n\n if np.isnan(inst_dict['value_orig']):\n inst_dict['value_orig'] = 'NaN'\n\n\n if 'mat_info' in net.dat:\n inst_dict['info'] = net.dat['mat_info'][str((i, j))]\n\n if 'mat_hl' in net.dat:\n inst_dict['highlight'] = net.dat['mat_hl'][i, j]\n\n net.viz['links'].append(inst_dict)\n\n else:\n for inst_mat in mat_types:\n if inst_mat in net.dat:\n net.viz[inst_mat] = net.dat[inst_mat].tolist()", "response": "make the dictionary for the clustergram. js visualization"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef install_npm(path=None, build_dir=None, source_dir=None, build_cmd='build', force=False, npm=None):\n\n class NPM(BaseCommand):\n description = 'install package.json dependencies using npm'\n\n def run(self):\n if skip_npm:\n log.info('Skipping npm-installation')\n return\n node_package = path or HERE\n node_modules = pjoin(node_package, 'node_modules')\n is_yarn = os.path.exists(pjoin(node_package, 'yarn.lock'))\n\n npm_cmd = npm\n\n if npm is None:\n if is_yarn:\n npm_cmd = ['yarn']\n else:\n npm_cmd = ['npm']\n\n if not which(npm_cmd[0]):\n log.error(\"`{0}` unavailable. If you're running this command \"\n \"using sudo, make sure `{0}` is availble to sudo\"\n .format(npm_cmd[0]))\n return\n\n if force or is_stale(node_modules, pjoin(node_package, 'package.json')):\n log.info('Installing build dependencies with npm. This may '\n 'take a while...')\n run(npm_cmd + ['install'], cwd=node_package)\n if build_dir and source_dir and not force:\n should_build = is_stale(build_dir, source_dir)\n else:\n should_build = True\n if should_build:\n run(npm_cmd + ['run', build_cmd], cwd=node_package)\n\n return NPM", "response": "Returns a Command that can be used to install npm dependencies."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\njoins paths for glob processing", "response": "def _glob_pjoin(*parts):\n \"\"\"Join paths for glob processing\"\"\"\n if parts[0] in ('.', ''):\n parts = parts[1:]\n return pjoin(*parts).replace(os.sep, '/')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _get_data_files(data_specs, existing, top=HERE):\n # Extract the existing data files into a staging object.\n file_data = defaultdict(list)\n for (path, files) in existing or []:\n file_data[path] = files\n\n # Extract the files and assign them to the proper data\n # files path.\n for (path, dname, pattern) in data_specs or []:\n if os.path.isabs(dname):\n dname = os.path.relpath(dname, top)\n dname = dname.replace(os.sep, '/')\n offset = 0 if dname in ('.', '') else len(dname) + 1\n files = _get_files(_glob_pjoin(dname, pattern), top=top)\n for fname in files:\n # Normalize the path.\n root = os.path.dirname(fname)\n full_path = _glob_pjoin(path, root[offset:])\n print(dname, root, full_path, offset)\n if full_path.endswith('/'):\n full_path = full_path[:-1]\n file_data[full_path].append(fname)\n\n # Construct the data files spec.\n data_files = []\n for (path, files) in file_data.items():\n data_files.append((path, files))\n return data_files", "response": "Expand data file specs into valid data files metadata."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nexpand file patterns to a list of paths.", "response": "def _get_files(file_patterns, top=HERE):\n \"\"\"Expand file patterns to a list of paths.\n\n Parameters\n -----------\n file_patterns: list or str\n A list of glob patterns for the data file locations.\n The globs can be recursive if they include a `**`.\n They should be relative paths from the top directory or\n absolute paths.\n top: str\n the directory to consider for data files\n\n Note:\n Files in `node_modules` are ignored.\n \"\"\"\n if not isinstance(file_patterns, (list, tuple)):\n file_patterns = [file_patterns]\n\n for i, p in enumerate(file_patterns):\n if os.path.isabs(p):\n file_patterns[i] = os.path.relpath(p, top)\n\n matchers = [_compile_pattern(p) for p in file_patterns]\n\n files = set()\n\n for root, dirnames, filenames in os.walk(top):\n # Don't recurse into node_modules\n if 'node_modules' in dirnames:\n dirnames.remove('node_modules')\n for m in matchers:\n for filename in filenames:\n fn = os.path.relpath(_glob_pjoin(root, filename), top)\n fn = fn.replace(os.sep, '/')\n if m(fn):\n files.add(fn.replace(os.sep, '/'))\n\n return list(files)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nexpand file patterns to a list of package_data paths.", "response": "def _get_package_data(root, file_patterns=None):\n \"\"\"Expand file patterns to a list of `package_data` paths.\n\n Parameters\n -----------\n root: str\n The relative path to the package root from `HERE`.\n file_patterns: list or str, optional\n A list of glob patterns for the data file locations.\n The globs can be recursive if they include a `**`.\n They should be relative paths from the root or\n absolute paths. If not given, all files will be used.\n\n Note:\n Files in `node_modules` are ignored.\n \"\"\"\n if file_patterns is None:\n file_patterns = ['*']\n return _get_files(file_patterns, _glob_pjoin(HERE, root))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef df_filter_row_sum(df, threshold, take_abs=True):\n ''' filter rows in matrix at some threshold\n and remove columns that have a sum below this threshold '''\n\n from copy import deepcopy\n from .__init__ import Network\n net = Network()\n\n if take_abs is True:\n df_copy = deepcopy(df['mat'].abs())\n else:\n df_copy = deepcopy(df['mat'])\n\n ini_rows = df_copy.index.values.tolist()\n df_copy = df_copy.transpose()\n tmp_sum = df_copy.sum(axis=0)\n tmp_sum = tmp_sum.abs()\n tmp_sum.sort_values(inplace=True, ascending=False)\n\n tmp_sum = tmp_sum[tmp_sum > threshold]\n keep_rows = sorted(tmp_sum.index.values.tolist())\n\n if len(keep_rows) < len(ini_rows):\n df['mat'] = grab_df_subset(df['mat'], keep_rows=keep_rows)\n\n if 'mat_up' in df:\n df['mat_up'] = grab_df_subset(df['mat_up'], keep_rows=keep_rows)\n df['mat_dn'] = grab_df_subset(df['mat_dn'], keep_rows=keep_rows)\n\n if 'mat_orig' in df:\n df['mat_orig'] = grab_df_subset(df['mat_orig'], keep_rows=keep_rows)\n\n return df", "response": "filter rows in matrix at some threshold\n and remove columns that have a sum below this threshold"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfiltering columns in matrix at some threshold and remove rows that have all zero values", "response": "def df_filter_col_sum(df, threshold, take_abs=True):\n ''' filter columns in matrix at some threshold\n and remove rows that have all zero values '''\n\n from copy import deepcopy\n from .__init__ import Network\n net = Network()\n\n if take_abs is True:\n df_copy = deepcopy(df['mat'].abs())\n else:\n df_copy = deepcopy(df['mat'])\n\n df_copy = df_copy.transpose()\n df_copy = df_copy[df_copy.sum(axis=1) > threshold]\n df_copy = df_copy.transpose()\n df_copy = df_copy[df_copy.sum(axis=1) > 0]\n\n if take_abs is True:\n inst_rows = df_copy.index.tolist()\n inst_cols = df_copy.columns.tolist()\n df['mat'] = grab_df_subset(df['mat'], inst_rows, inst_cols)\n\n if 'mat_up' in df:\n df['mat_up'] = grab_df_subset(df['mat_up'], inst_rows, inst_cols)\n df['mat_dn'] = grab_df_subset(df['mat_dn'], inst_rows, inst_cols)\n\n if 'mat_orig' in df:\n df['mat_orig'] = grab_df_subset(df['mat_orig'], inst_rows, inst_cols)\n\n else:\n df['mat'] = df_copy\n\n return df"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef filter_threshold(df, inst_rc, threshold, num_occur=1):\n '''\n Filter a network's rows or cols based on num_occur values being above a\n threshold (in absolute_value)\n '''\n from copy import deepcopy\n\n inst_df = deepcopy(df['mat'])\n\n if inst_rc == 'col':\n inst_df = inst_df.transpose()\n\n inst_df = inst_df.abs()\n\n ini_rows = inst_df.index.values.tolist()\n\n inst_df[inst_df < threshold] = 0\n inst_df[inst_df >= threshold] = 1\n\n tmp_sum = inst_df.sum(axis=1)\n\n tmp_sum = tmp_sum[tmp_sum >= num_occur]\n\n keep_names = tmp_sum.index.values.tolist()\n\n if inst_rc == 'row':\n if len(keep_names) < len(ini_rows):\n df['mat'] = grab_df_subset(df['mat'], keep_rows=keep_names)\n\n if 'mat_up' in df:\n df['mat_up'] = grab_df_subset(df['mat_up'], keep_rows=keep_names)\n df['mat_dn'] = grab_df_subset(df['mat_dn'], keep_rows=keep_names)\n\n if 'mat_orig' in df:\n df['mat_orig'] = grab_df_subset(df['mat_orig'], keep_rows=keep_names)\n\n elif inst_rc == 'col':\n inst_df = inst_df.transpose()\n\n inst_rows = inst_df.index.values.tolist()\n inst_cols = keep_names\n\n df['mat'] = grab_df_subset(df['mat'], inst_rows, inst_cols)\n\n if 'mat_up' in df:\n df['mat_up'] = grab_df_subset(df['mat_up'], inst_rows, inst_cols)\n df['mat_dn'] = grab_df_subset(df['mat_dn'], inst_rows, inst_cols)\n\n if 'mat_orig' in df:\n df['mat_orig'] = grab_df_subset(df['mat_orig'], inst_rows, inst_cols)\n\n return df", "response": "Filter a network based on num_occur values being above threshold"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef make_clust(net, dist_type='cosine', run_clustering=True, dendro=True,\n requested_views=['pct_row_sum', 'N_row_sum'],\n linkage_type='average', sim_mat=False, filter_sim=0.1,\n calc_cat_pval=False, sim_mat_views=['N_row_sum'],\n run_enrichr=None, enrichrgram=None):\n '''\n This will calculate multiple views of a clustergram by filtering the\n data and clustering after each filtering. This filtering will keep the top\n N rows based on some quantity (sum, num-non-zero, etc).\n '''\n from copy import deepcopy\n import scipy\n from . import calc_clust, run_filter, make_views, make_sim_mat, cat_pval\n from . import enrichr_functions as enr_fun\n\n df = net.dat_to_df()\n\n threshold = 0.0001\n df = run_filter.df_filter_row_sum(df, threshold)\n df = run_filter.df_filter_col_sum(df, threshold)\n\n # default setting\n define_cat_colors = False\n\n if run_enrichr is not None:\n df = enr_fun.add_enrichr_cats(df, 'row', run_enrichr)\n\n define_cat_colors = True\n\n # calculate initial view with no row filtering\n net.df_to_dat(df, define_cat_colors=True)\n\n\n inst_dm = calc_clust.cluster_row_and_col(net, dist_type=dist_type,\n linkage_type=linkage_type,\n run_clustering=run_clustering,\n dendro=dendro, ignore_cat=False,\n calc_cat_pval=calc_cat_pval)\n\n all_views = []\n send_df = deepcopy(df)\n\n if 'N_row_sum' in requested_views:\n all_views = make_views.N_rows(net, send_df, all_views,\n dist_type=dist_type, rank_type='sum')\n\n if 'N_row_var' in requested_views:\n all_views = make_views.N_rows(net, send_df, all_views,\n dist_type=dist_type, rank_type='var')\n\n if 'pct_row_sum' in requested_views:\n all_views = make_views.pct_rows(net, send_df, all_views,\n dist_type=dist_type, rank_type='sum')\n\n if 'pct_row_var' in requested_views:\n all_views = make_views.pct_rows(net, send_df, all_views,\n dist_type=dist_type, rank_type='var')\n\n which_sim = []\n\n if sim_mat == True:\n which_sim = ['row', 'col']\n elif sim_mat == 'row':\n which_sim = ['row']\n elif sim_mat == 'col':\n which_sim = ['col']\n\n if sim_mat is not False:\n sim_net = make_sim_mat.main(net, inst_dm, which_sim, filter_sim, sim_mat_views)\n\n net.sim = {}\n\n for inst_rc in which_sim:\n net.sim[inst_rc] = sim_net[inst_rc].viz\n\n if inst_rc == 'row':\n other_rc = 'col'\n elif inst_rc == 'col':\n other_rc = 'row'\n\n # keep track of cat_colors\n net.sim[inst_rc]['cat_colors'][inst_rc] = net.viz['cat_colors'][inst_rc]\n net.sim[inst_rc]['cat_colors'][other_rc] = net.viz['cat_colors'][inst_rc]\n\n else:\n net.sim = {}\n\n net.viz['views'] = all_views\n\n if enrichrgram != None:\n # toggle enrichrgram functionality from back-end\n net.viz['enrichrgram'] = enrichrgram\n\n if 'enrichrgram_lib' in net.dat:\n net.viz['enrichrgram'] = True\n net.viz['enrichrgram_lib'] = net.dat['enrichrgram_lib']\n\n if 'row_cat_bars' in net.dat:\n net.viz['row_cat_bars'] = net.dat['row_cat_bars']", "response": "This function calculates multiple views of a clustergram from a network."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef infer_flags(bytecode, is_async=False):\n flags = CompilerFlags(0)\n if not isinstance(bytecode, (_bytecode.Bytecode,\n _bytecode.ConcreteBytecode,\n _bytecode.ControlFlowGraph)):\n msg = ('Expected a Bytecode, ConcreteBytecode or ControlFlowGraph '\n 'instance not %s')\n raise ValueError(msg % bytecode)\n\n instructions = (bytecode.get_instructions()\n if isinstance(bytecode, _bytecode.ControlFlowGraph) else\n bytecode)\n instr_names = {i.name for i in instructions\n if not isinstance(i, (_bytecode.SetLineno,\n _bytecode.Label))}\n\n if not (instr_names & {'STORE_NAME', 'LOAD_NAME', 'DELETE_NAME'}):\n flags |= CompilerFlags.OPTIMIZED\n\n flags |= bytecode.flags & (CompilerFlags.NEWLOCALS |\n CompilerFlags.VARARGS |\n CompilerFlags.VARKEYWORDS |\n CompilerFlags.NESTED)\n\n if instr_names & {'YIELD_VALUE', 'YIELD_FROM'}:\n if not is_async and not bytecode.flags & CompilerFlags.ASYNC_GENERATOR:\n flags |= CompilerFlags.GENERATOR\n else:\n flags |= CompilerFlags.ASYNC_GENERATOR\n\n if not (instr_names & {'LOAD_CLOSURE', 'LOAD_DEREF', 'STORE_DEREF',\n 'DELETE_DEREF', 'LOAD_CLASSDEREF'}):\n flags |= CompilerFlags.NOFREE\n\n if (not (bytecode.flags & CompilerFlags.ITERABLE_COROUTINE or\n flags & CompilerFlags.ASYNC_GENERATOR) and\n (instr_names & {'GET_AWAITABLE', 'GET_AITER', 'GET_ANEXT',\n 'BEFORE_ASYNC_WITH', 'SETUP_ASYNC_WITH'} or\n bytecode.flags & CompilerFlags.COROUTINE)):\n flags |= CompilerFlags.COROUTINE\n\n flags |= bytecode.flags & CompilerFlags.ITERABLE_COROUTINE\n\n flags |= bytecode.flags & CompilerFlags.FUTURE_GENERATOR_STOP\n\n if ([bool(flags & getattr(CompilerFlags, k))\n for k in ('COROUTINE', 'ITERABLE_COROUTINE', 'GENERATOR',\n 'ASYNC_GENERATOR')].count(True) > 1):\n raise ValueError(\"Code should not have more than one of the \"\n \"following flag set : generator, coroutine, \"\n \"iterable coroutine and async generator, got:\"\n \"%s\" % flags)\n\n return flags", "response": "Infer the proper flags for a given bytecode."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nmodify the instruction in - place.", "response": "def set(self, name, arg=UNSET):\n \"\"\"Modify the instruction in-place.\n\n Replace name and arg attributes. Don't modify lineno.\n \"\"\"\n self._set(name, arg, self._lineno)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef setup(tf, order=None):\n global _tf_version_string, _tf_version\n _tf_version_string = tf.__version__\n _tf_version = _parse_tf_version(_tf_version_string)\n\n if order is not None:\n optimize(order)", "response": "Sets up global variables to adapt to peculiarities of\n different tensorflow versions."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef optimize(order):\n if not isinstance(order, (list, tuple)):\n order = [order]\n\n for op in Operation.__subclasses__():\n for impl in order:\n if impl in op.impls:\n op.use_impl(impl)\n break", "response": "Optimize the order of the available resource types."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef print_tensor(td_tensor, indent=\"| \", max_depth=-1, depth=0):\n offset = depth * indent\n line = \"td tensor: %s\" % td_tensor.name\n if td_tensor.value is not None:\n line += \" (%s)\" % (\",\".join(str(i) for i in td_tensor.value.shape),)\n\n print(offset + line)\n\n if td_tensor.op and (max_depth < 0 or max_depth > depth):\n print_op(td_tensor.op, indent=indent, max_depth=max_depth, depth=depth+1)", "response": "Prints a tensor to stdout."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef print_op(td_op, indent=\"| \", max_depth=-1, depth=0):\n offset = depth * indent\n line = \"td op: %s (%s)\" % (td_op.name, \",\".join(td_op.types))\n\n print(offset + line)\n\n if max_depth < 0 or max_depth > depth:\n for td_tensor in td_op.inputs:\n print_tensor(td_tensor, indent=indent, max_depth=max_depth, depth=depth+1)", "response": "Prints a single Operation object."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef print_tf_tensor(tf_tensor, indent=\"| \", max_depth=-1, depth=0):\n offset = depth * indent\n shape = tuple(int(i) for i in tf_tensor.get_shape())\n line = \"tf tensor: %s (%s)\" % (tf_tensor.name, \",\".join(str(i) for i in shape))\n\n print(offset + line)\n\n if tf_tensor.op and (max_depth < 0 or max_depth > depth):\n print_tf_op(tf_tensor.op, indent=indent, max_depth=max_depth, depth=depth+1)", "response": "Prints a tensor to stdout."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nprinting a tensorflow operation to stdout.", "response": "def print_tf_op(tf_op, indent=\"| \", max_depth=-1, depth=0):\n \"\"\" print_tf_op(tf_tensor, indent=\" \", max_depth=-1)\n Prints the dependency graph of a tensorflow operation *tf_op*, where each new level is indented\n by *indent*. When *max_depth* is positive, the graph is truncated at that depth, where each\n tensor and each op count as a level.\n \"\"\"\n offset = depth * indent\n line = \"tf op: %s (%s)\" % (tf_op.name, tf_op.type)\n\n print(offset + line)\n\n if max_depth < 0 or max_depth > depth:\n for tf_tensor in tf_op.inputs:\n print_tf_tensor(tf_tensor, indent=indent, max_depth=max_depth, depth=depth+1)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef LinSpace(start, stop, num):\n return np.linspace(start, stop, num=num, dtype=np.float32),", "response": "Linspace implementation of the LinearSpace operator."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef Range(start, limit, delta):\n return np.arange(start, limit, delta, dtype=np.int32),", "response": "Returns an array of the specified range."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef RandomUniformInt(shape, minval, maxval, seed):\n if seed:\n np.random.seed(seed)\n return np.random.randint(minval, maxval, size=shape),", "response": "Random uniform int op."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nranks of the array.", "response": "def Rank(a):\n \"\"\"\n Rank op.\n \"\"\"\n return np.array([len(a.shape)], dtype=np.int32),"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsqueezes op. i. e. removes singular axes.", "response": "def Squeeze(a, squeeze_dims):\n \"\"\"\n Squeeze op, i.e. removes singular axes.\n \"\"\"\n if not squeeze_dims:\n squeeze_dims = list(range(len(a.shape)))\n slices = [(0 if (dim == 1 and i in squeeze_dims) else slice(None)) \\\n for i, dim in enumerate(a.shape)]\n return np.copy(a)[slices],"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef ExpandDims(a, dim):\n shape = list(a.shape)\n if dim >= 0:\n shape.insert(dim, 1)\n else:\n shape.insert(len(shape) + dim + 1, 1)\n return np.copy(a).reshape(*shape),", "response": "Expand dim op, i.e. add singular axis at dim."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nslicing a array by given indices.", "response": "def Slice(a, begin, size):\n \"\"\"\n Slicing op.\n \"\"\"\n return np.copy(a)[[slice(*tpl) for tpl in zip(begin, begin+size)]],"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nsplit op with n splits.", "response": "def Split(axis, a, n):\n \"\"\"\n Split op with n splits.\n \"\"\"\n return tuple(np.split(np.copy(a), n, axis=axis))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef SplitV(a, splits, axis):\n return tuple(np.split(np.copy(a), np.cumsum(splits), axis=axis))", "response": "Split op with multiple split sizes."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nbatch diag op that returns only the diagonal elements.", "response": "def MatrixDiagPart(a):\n \"\"\"\n Batched diag op that returns only the diagonal elements.\n \"\"\"\n r = np.zeros(a.shape[:-2] + (min(a.shape[-2:]),))\n for coord in np.ndindex(a.shape[:-2]):\n pos = coord + (Ellipsis,)\n r[pos] = np.diagonal(a[pos])\n return r,"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nselfing Adjoint Eigen decomp op.", "response": "def SelfAdjointEig(a):\n \"\"\"\n Eigen decomp op.\n \"\"\"\n shape = list(a.shape)\n shape[-2] += 1\n return np.append(*np.linalg.eig(a)).reshape(*shape),"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef Svd(a, uv, full):\n u, s, v = np.linalg.svd(a, full_matrices=full, compute_uv=uv)\n return s, u, v", "response": "Single value decomp op."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncompute prod along specified axis.", "response": "def Prod(a, axis, keep_dims):\n \"\"\"\n Prod reduction op.\n \"\"\"\n return np.prod(a, axis=axis if not isinstance(axis, np.ndarray) else tuple(axis),\n keepdims=keep_dims),"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef SparseSegmentSqrtN(a, idxs, ids):\n func = lambda _idxs: np.divide(reduce(np.add, a[idxs][_idxs]), np.math.sqrt(len(_idxs)))\n return seg_map(func, a, ids),", "response": "Sparse segmented sum / sqrt N"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef AvgPool3D(a, k, strides, padding):\n patches = _pool_patches(a, k, strides, padding.decode(\"ascii\"))\n return np.average(patches, axis=tuple(range(-len(k), 0))),", "response": "Average 3D pooling op."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get(self, *names, **kwargs):\n tensors = tuple(self._get(name, **kwargs) for name in names)\n return tensors[0] if len(names) == 1 else tensors", "response": "Get a set of tensors given by names using a deep lookup within the current model."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nadds a new root tensor for a given key.", "response": "def add(self, tensor, tf_sess=None, key=None, **kwargs):\n \"\"\"\n Adds a new root *tensor* for a *key* which, if *None*, defaults to a consecutive number.\n When *tensor* is not an instance of :py:class:`Tensor` but an instance of\n ``tensorflow.Tensor``, it is converted first. In that case, *tf_sess* should be a valid\n tensorflow session and *kwargs* are forwarded to the :py:class:`Tensor` constructor.\n \"\"\"\n if not isinstance(tensor, Tensor):\n tensor = Tensor(tensor, tf_sess, **kwargs)\n\n if key is None:\n if len(self.roots) == 0:\n key = 0\n else:\n key = max(self.roots.keys()) + 1\n\n self.roots[key] = tensor"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef load(self, path):\n path = os.path.expandvars(os.path.expanduser(path))\n with open(path, \"rb\") as f:\n roots = pickle.load(f)\n\n for key, tensor in roots.items():\n self.add(tensor, key=key)", "response": "Loads all tensors from a file defined by path and adds them to the set."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef save(self, path):\n path = os.path.expandvars(os.path.expanduser(path))\n with open(path, \"wb\") as f:\n pickle.dump(self.roots, f)", "response": "Saves all the tensors of the root set to a file defined by path."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get(self, *names, **kwargs):\n # create empty tensor ensembles with our method\n tensor_ensembles = [TensorEnsemble([], self.method) for name in names]\n\n # loop over models, collect and add tensors\n for model in self.models:\n tensors = model.get(*names, **kwargs)\n if not isinstance(tensors, tuple):\n tensors = (tensors,)\n for i, t in enumerate(tensors if isinstance(tensors, tuple) else (tensors,)):\n tensor_ensembles[i].tensors.append(t)\n\n return tensor_ensembles[0] if len(names) == 1 else tuple(tensor_ensembles)", "response": "Get one or more tensor ensembles given by names using a deep\n lookup within all read models."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef load(self, paths):\n for path in paths:\n self.models.append(Model(path))", "response": "Loads models from a list of paths."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nevaluate all contained tensors using a feed_dict and returns the ensemble value.", "response": "def eval(self, feed_dict=None):\n \"\"\"\n Evaluates all contained tensors using a *feed_dict* and returns the ensemble value. The keys\n of *feed_dict* must be tensor ensembles. Its values can be batches, i.e., numpy arrays, or\n lists or tuples of batches. In the latter case, these lists or tuples must have the same\n length as the list of stored tensors as they will be mapped.\n \"\"\"\n # first, check that the length of all feed_dict keys match our own length\n for tensor_ensemble in feed_dict:\n if len(tensor_ensemble.tensors) != len(self.tensors):\n raise EnsembleMismatchException(\"incompatible lengths of tensors: %d, %d\" \\\n % (len(self.tensors), len(tensor_ensemble.tensors)))\n\n # create a joined uuid\n _uuid = uuid4()\n\n # prepare feed_dicts\n feed_dicts = [{} for _ in range(len(self.tensors))]\n for tensor_ensemble, value in feed_dict.items():\n for i, tensor in enumerate(tensor_ensemble.tensors):\n if tensor is not None:\n feed_dicts[i][tensor] = value[i] if isinstance(value, (list, tuple)) else value\n\n # eval all tensors\n values = [t.eval(feed_dict=d, _uuid=_uuid) for t, d in zip(self.tensors, feed_dicts)]\n\n # return the computed ensemble value\n return self.func(values)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef pressed_keys(self):\n # make a list of all the keys that are detected\n pressed = []\n\n # set all pins pins to be inputs w/pullups\n for pin in self.row_pins+self.col_pins:\n pin.direction = Direction.INPUT\n pin.pull = Pull.UP\n\n for row in range(len(self.row_pins)):\n # set one row low at a time\n self.row_pins[row].direction = Direction.OUTPUT\n self.row_pins[row].value = False\n # check the column pins, which ones are pulled down\n for col in range(len(self.col_pins)):\n if not self.col_pins[col].value:\n pressed.append(self.keys[row][col])\n # reset the pin to be an input\n self.row_pins[row].direction = Direction.INPUT\n self.row_pins[row].pull = Pull.UP\n return pressed", "response": "An array containing all the keys that are pressed from the initalized\n list - of - lists passed in during creation"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_stdev(self, asset_type):\n load_times = []\n # Handle edge cases like TTFB\n if asset_type == 'ttfb':\n for page in self.pages:\n if page.time_to_first_byte is not None:\n load_times.append(page.time_to_first_byte)\n elif asset_type not in self.asset_types and asset_type != 'page':\n raise ValueError('asset_type must be one of:\\nttfb\\n{0}'.format(\n '\\n'.join(self.asset_types)))\n else:\n load_times = self.get_load_times(asset_type)\n\n if not load_times or not sum(load_times):\n return 0\n return round(stdev(load_times),\n self.decimal_precision)", "response": "Calculates the standard deviation for a set of asset types."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of all the pages of all the parser objects.", "response": "def pages(self):\n \"\"\"\n The aggregate pages of all the parser objects.\n \"\"\"\n pages = []\n for har_dict in self.har_data:\n har_parser = HarParser(har_data=har_dict)\n if self.page_id:\n for page in har_parser.pages:\n if page.page_id == self.page_id:\n pages.append(page)\n else:\n pages = pages + har_parser.pages\n return pages"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef time_to_first_byte(self):\n ttfb = []\n for page in self.pages:\n if page.time_to_first_byte is not None:\n ttfb.append(page.time_to_first_byte)\n return round(mean(ttfb), self.decimal_precision)", "response": "Returns the aggregate time to first byte for all pages."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the average load time for all runs.", "response": "def page_load_time(self):\n \"\"\"\n The average total load time for all runs (not weighted).\n \"\"\"\n load_times = self.get_load_times('page')\n return round(mean(load_times), self.decimal_precision)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn aggregate javascript load time.", "response": "def js_load_time(self):\n \"\"\"\n Returns aggregate javascript load time.\n \"\"\"\n load_times = self.get_load_times('js')\n return round(mean(load_times), self.decimal_precision)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn aggregate css load time for all pages.", "response": "def css_load_time(self):\n \"\"\"\n Returns aggregate css load time for all pages.\n \"\"\"\n load_times = self.get_load_times('css')\n return round(mean(load_times), self.decimal_precision)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef image_load_time(self):\n load_times = self.get_load_times('image')\n return round(mean(load_times), self.decimal_precision)", "response": "Returns aggregate image load time for all pages."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef html_load_time(self):\n load_times = self.get_load_times('html')\n return round(mean(load_times), self.decimal_precision)", "response": "Returns aggregate html load time for all pages."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef audio_load_time(self):\n load_times = self.get_load_times('audio')\n return round(mean(load_times), self.decimal_precision)", "response": "Returns aggregate audio load time for all pages."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef video_load_time(self):\n load_times = self.get_load_times('video')\n return round(mean(load_times), self.decimal_precision)", "response": "Returns aggregate video load time for all pages."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfunction to match headers.", "response": "def match_headers(self, entry, header_type, header, value, regex=True):\n \"\"\"\n Function to match headers.\n\n Since the output of headers might use different case, like:\n\n 'content-type' vs 'Content-Type'\n\n This function is case-insensitive\n\n :param entry: entry object\n :param header_type: ``str`` of header type. Valid values:\n\n * 'request'\n * 'response'\n\n :param header: ``str`` of the header to search for\n :param value: ``str`` of value to search for\n :param regex: ``bool`` indicating whether to use regex or exact match\n\n :returns: a ``bool`` indicating whether a match was found\n \"\"\"\n if header_type not in entry:\n raise ValueError('Invalid header_type, should be either:\\n\\n'\n '* \\'request\\'\\n*\\'response\\'')\n\n # TODO - headers are empty in some HAR data.... need fallbacks here\n for h in entry[header_type]['headers']:\n if h['name'].lower() == header.lower() and h['value'] is not None:\n if regex and re.search(value, h['value'], flags=re.IGNORECASE):\n return True\n elif value == h['value']:\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning True if the content type of a request matches the mimeType metadata.", "response": "def match_content_type(entry, content_type, regex=True):\n \"\"\"\n Matches the content type of a request using the mimeType metadata.\n\n :param entry: ``dict`` of a single entry from a HarPage\n :param content_type: ``str`` of regex to use for finding content type\n :param regex: ``bool`` indicating whether to use regex or exact match.\n \"\"\"\n mimeType = entry['response']['content']['mimeType']\n\n if regex and re.search(content_type, mimeType, flags=re.IGNORECASE):\n return True\n\n elif content_type == mimeType:\n return True\n\n return False"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn True if the entry with the given request type matches the given request_type.", "response": "def match_request_type(self, entry, request_type, regex=True):\n \"\"\"\n Helper function that returns entries with a request type\n matching the given `request_type` argument.\n\n :param entry: entry object to analyze\n :param request_type: ``str`` of request type to match\n :param regex: ``bool`` indicating whether to use a regex or string match\n \"\"\"\n if regex:\n return re.search(request_type, entry['request']['method'],\n flags=re.IGNORECASE) is not None\n else:\n return entry['request']['method'] == request_type"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncreates a dictionary of the asset timeline for the requested assets.", "response": "def create_asset_timeline(self, asset_list):\n \"\"\"\n Returns a ``dict`` of the timeline for the requested assets. The key is\n a datetime object (down to the millisecond) of ANY time where at least\n one of the requested assets was loaded. The value is a ``list`` of ALL\n assets that were loading at that time.\n\n :param asset_list: ``list`` of the assets to create a timeline for.\n \"\"\"\n results = dict()\n for asset in asset_list:\n time_key = dateutil.parser.parse(asset['startedDateTime'])\n load_time = int(asset['time'])\n # Add the start time and asset to the results dict\n if time_key in results:\n results[time_key].append(asset)\n else:\n results[time_key] = [asset]\n # For each millisecond the asset was loading, insert the asset\n # into the appropriate key of the results dict. Starting the range()\n # index at 1 because we already inserted the first millisecond.\n for _ in range(1, load_time):\n time_key = time_key + datetime.timedelta(milliseconds=1)\n if time_key in results:\n results[time_key].append(asset)\n else:\n results[time_key] = [asset]\n\n return results"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef pages(self):\n # Start with a page object for unknown entries if the HAR data has\n # any entries with no page ID\n pages = []\n if any('pageref' not in entry for entry in self.har_data['entries']):\n pages.append(HarPage('unknown', har_parser=self))\n for har_page in self.har_data['pages']:\n page = HarPage(har_page['id'], har_parser=self)\n pages.append(page)\n\n return pages", "response": "This is a list of HarPage objects each of which represents a page\n from the HAR file."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef filter_entries(self, request_type=None, content_type=None,\n status_code=None, http_version=None, regex=True):\n \"\"\"\n Returns a ``list`` of entry objects based on the filter criteria.\n\n :param request_type: ``str`` of request type (i.e. - GET or POST)\n :param content_type: ``str`` of regex to use for finding content type\n :param status_code: ``int`` of the desired status code\n :param http_version: ``str`` of HTTP version of request\n :param regex: ``bool`` indicating whether to use regex or exact match.\n \"\"\"\n results = []\n\n for entry in self.entries:\n \"\"\"\n So yea... this is a bit ugly. We are looking for:\n\n * The request type using self._match_request_type()\n * The content type using self._match_headers()\n * The HTTP response status code using self._match_status_code()\n * The HTTP version using self._match_headers()\n\n Oh lords of python.... please forgive my soul\n \"\"\"\n valid_entry = True\n p = self.parser\n if request_type is not None and not p.match_request_type(\n entry, request_type, regex=regex):\n valid_entry = False\n if content_type is not None:\n if not self.parser.match_content_type(entry, content_type,\n regex=regex):\n valid_entry = False\n if status_code is not None and not p.match_status_code(\n entry, status_code, regex=regex):\n valid_entry = False\n if http_version is not None and not p.match_http_version(\n entry, http_version, regex=regex):\n valid_entry = False\n\n if valid_entry:\n results.append(entry)\n\n return results", "response": "Returns a list of entries based on the filter criteria."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the total size of a collection of entries.", "response": "def get_total_size(self, entries):\n \"\"\"\n Returns the total size of a collection of entries.\n\n :param entries: ``list`` of entries to calculate the total size of.\n \"\"\"\n size = 0\n for entry in entries:\n if entry['response']['bodySize'] > 0:\n size += entry['response']['bodySize']\n return size"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_total_size_trans(self, entries):\n size = 0\n for entry in entries:\n if entry['response']['_transferSize'] > 0:\n size += entry['response']['_transferSize']\n return size", "response": "Returns the total size of a collection of entries - transferred."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef time_to_first_byte(self):\n # The unknown page is just a placeholder for entries with no page ID.\n # As such, it would not have a TTFB\n if self.page_id == 'unknown':\n return None\n ttfb = 0\n for entry in self.entries:\n if entry['response']['status'] == 200:\n for k, v in iteritems(entry['timings']):\n if k != 'receive':\n if v > 0:\n ttfb += v\n break\n else:\n ttfb += entry['time']\n\n return ttfb", "response": "Return the time to first byte of the page request in ms."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef group_by(self, *fields):\n fields = self._expand_group_by_fields(self.model, fields)\n return self._clone(klass=GroupByQuerySet, setup=True, _fields=fields)", "response": "Returns a new queryset with the specified fields grouped by the current queryset."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the cached data built from instance raw _values as a dictionary.", "response": "def _data(self):\n \"\"\"\n Cached data built from instance raw _values as a dictionary.\n \"\"\"\n d = {}\n\n # Iterate all keys and values\n for k, v in self._row_values.items():\n # Split related model fields\n attrs = k.rsplit('__', 1)\n\n # Set value depending case\n if len(attrs) == 2:\n # Related model field, store nested\n fk, fn = attrs\n if fk not in d:\n d[fk] = {}\n d[fk][fn] = v\n\n else:\n # Own model field, store directly\n d[k] = v\n\n # Return (+cache) data\n return d"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\npopulating instance with given.", "response": "def _set_values(self):\n \"\"\"\n Populate instance with given.\n \"\"\"\n # Iterate all keys and values in data\n for k, v in self._data.items():\n # If it's a dict, process it (it's probably instance data)\n if isinstance(v, dict):\n try:\n # Get related model from field (follow path)\n rel_model = self._model\n for attr in k.split('__'):\n rel_model = getattr(rel_model, attr).field.related_model\n\n except AttributeError:\n # Not a model, maybe it is a dict field (?)\n pass\n\n else:\n # Model, first shorten field name\n k = k.replace('__', '_')\n\n # Now init instance if required (not if we got ID None)\n if 'id' in v and v['id'] is None:\n # This means we grouped by ID, if it's none then FK is None\n v = None\n\n else:\n # Either we have ID or we didn't group by ID, use instance\n v = rel_model(**v)\n\n # Set value\n setattr(self, k, v)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef group_by(self, *fields):\n fields = self._expand_group_by_fields(self.model, fields)\n clone = self._values(*fields)\n clone._iterable_class = GroupByIterable\n return clone", "response": "Returns a new queryset with the given fields grouped by the current queryset."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nexpanding FK fields into all related object s fields to avoid future lookups.", "response": "def _expand_group_by_fields(cls, model, fields):\n \"\"\"\n Expand FK fields into all related object's fields to avoid future\n lookups.\n\n :param fields: fields to \"group by\"\n :return: expanded fields\n \"\"\"\n # Containers for resulting fields and related model fields\n res = []\n related = {}\n\n # Add own fields and populate related fields\n for field_name in fields:\n if '__' in field_name:\n # Related model field: append to related model's fields\n fk_field_name, related_field = field_name.split('__', 1)\n if fk_field_name not in related:\n related[fk_field_name] = [related_field]\n else:\n related[fk_field_name].append(related_field)\n\n else:\n # Simple field, get the field instance\n model_field = model._meta.get_field(field_name)\n\n if isinstance(model_field, (ForeignKey, ManyToManyField)):\n # It's a related field, get model\n related_model = model_field.related_model\n\n # Append all its fields with the correct prefix\n res.extend('{}__{}'.format(field_name, f.column)\n for f in related_model._meta.fields)\n\n else:\n # It's a common field, just append it\n res.append(field_name)\n\n # Resolve all related fields\n for fk_field_name, field_names in related.items():\n # Get field\n fk = model._meta.get_field(fk_field_name)\n\n # Get all fields for that related model\n related_fields = cls._expand_group_by_fields(fk.related_model,\n field_names)\n\n # Append them with the correct prefix\n res.extend('{}__{}'.format(fk_field_name, f) for f in related_fields)\n\n # Return all fields\n return res"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef write(self, addr, data):\n '''Write to dummy memory\n\n Parameters\n ----------\n addr : int\n The register address.\n data : list, tuple\n Data (byte array) to be written.\n\n Returns\n -------\n nothing\n '''\n logger.debug(\n \"Dummy SiTransferLayer.write addr: %s data: %s\" % (hex(addr), data))\n for curr_addr, d in enumerate(data, start=addr):\n self.mem[curr_addr] = array.array('B', [d])[0]", "response": "Write to dummy memory."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nread the data from memory.", "response": "def read(self, addr, size):\n '''\n Parameters\n ----------\n addr : int\n The register address.\n size : int\n Length of data to be read (number of bytes).\n\n Returns\n -------\n array : array\n Data (byte array) read from memory. Returns 0 for each byte if it hasn't been written to.\n '''\n logger.debug(\"Dummy SiTransferLayer.read addr: %s size: %s\" % (hex(addr), size))\n return array.array('B', [self.mem[curr_addr] if curr_addr in self.mem else 0 for curr_addr in range(addr, addr + size)])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nwrite a value to a memory address and offset.", "response": "def set_value(self, value, addr, size, offset, **kwargs):\r\n '''Writing a value of any arbitrary size (max. unsigned int 64) and offset to a register\r\n\r\n Parameters\r\n ----------\r\n value : int, str\r\n The register value (int, long, bit string) to be written.\r\n addr : int\r\n The register address.\r\n size : int\r\n Bit size/length of the value to be written to the register.\r\n offset : int\r\n Offset of the value to be written to the register (in number of bits).\r\n\r\n Returns\r\n -------\r\n nothing\r\n '''\r\n div_offset, mod_offset = divmod(offset, 8)\r\n div_size, mod_size = divmod(size + mod_offset, 8)\r\n if mod_size:\r\n div_size += 1\r\n if mod_offset == 0 and mod_size == 0:\r\n reg = BitLogic.from_value(0, size=div_size * 8)\r\n else:\r\n ret = self._intf.read(self._base_addr + addr + div_offset, size=div_size)\r\n reg = BitLogic()\r\n reg.frombytes(tobytes(ret))\r\n reg[size + mod_offset - 1:mod_offset] = value\r\n self._intf.write(self._base_addr + addr + div_offset, data=array.array('B', reg.tobytes()))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreading a value from a register of any arbitrary size and offset from a register.", "response": "def get_value(self, addr, size, offset, **kwargs):\r\n '''Reading a value of any arbitrary size (max. unsigned int 64) and offset from a register\r\n\r\n Parameters\r\n ----------\r\n addr : int\r\n The register address.\r\n size : int\r\n Bit size/length of the value.\r\n offset : int\r\n Offset of the value to be written to the register (in number of bits).\r\n\r\n Returns\r\n -------\r\n reg : int\r\n Register value.\r\n '''\r\n div_offset, mod_offset = divmod(offset, 8)\r\n div_size, mod_size = divmod(size + mod_offset, 8)\r\n if mod_size:\r\n div_size += 1\r\n ret = self._intf.read(self._base_addr + addr + div_offset, size=div_size)\r\n reg = BitLogic()\r\n reg.frombytes(tobytes(ret))\r\n return reg[size + mod_offset - 1:mod_offset].tovalue()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set_bytes(self, data, addr, **kwargs):\r\n '''Writing bytes of any arbitrary size\r\n\r\n Parameters\r\n ----------\r\n data : iterable\r\n The data (byte array) to be written.\r\n addr : int\r\n The register address.\r\n\r\n Returns\r\n -------\r\n nothing\r\n '''\r\n self._intf.write(self._conf['base_addr'] + addr, data)", "response": "Writes bytes of any arbitrary size to the specified address."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_bytes(self, addr, size, **kwargs):\r\n '''Reading bytes of any arbitrary size\r\n\r\n Parameters\r\n ----------.\r\n addr : int\r\n The register address.\r\n size : int\r\n Byte length of the value.\r\n\r\n Returns\r\n -------\r\n data : iterable\r\n Byte array.\r\n '''\r\n return self._intf.read(self._conf['base_addr'] + addr, size)", "response": "Reads bytes of any arbitrary size from the specified address."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nwrite the current entry to the file", "response": "def write(self, size=None):\n \"\"\"\n to call start() automatically, set yaml file as follows:\n registers:\n - name : CCPD_PCB\n type : StdRegister\n hw_driver : CCPD_PCB_SPI\n size : 32\n auto_start : True <------ add this\n fields: ......\n \"\"\"\n if size is None:\n self._drv.set_data(self.tobytes())\n else:\n self._drv.set_data(self.tobytes()[:size])\n\n if \"auto_start\" in self._conf:\n if self._conf[\"auto_start\"]:\n self._drv.start()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef from_value(cls, value, size=None, fmt='Q', **kwargs):\n '''\n Factory method\n\n For format characters see: https://docs.python.org/2/library/struct.html\n '''\n bl = cls(**kwargs) # size is 0 by default\n bl.fromvalue(value=value, size=size, fmt=fmt)\n return bl", "response": "Factory method\n Factory method\n FromValue"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef fromvalue(self, value, size=None, fmt='Q'):\n '''\n Append from a int/long number.\n '''\n if size and value.bit_length() > size:\n raise TypeError('Value is too big for given size')\n self.frombytes(struct.pack(fmt, value))\n if size:\n if not isinstance(size, integer_types) or not size > 0:\n raise TypeError('Size must be greater than zero')\n if size > self.length():\n bitarray.extend(self, (size - self.length()) * [0])\n else:\n bitarray.__delitem__(self, slice(size, self.length()))", "response": "Append from a int or long number."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nconverts bitstring to a int or long number.", "response": "def tovalue(self, fmt='Q'):\n '''\n Convert bitstring to a int/long number.\n '''\n format_size = struct.calcsize(fmt)\n if self.length() > format_size * 8:\n raise TypeError('Cannot convert to number')\n ba = self.copy()\n ba.extend((format_size * 8 - self.length()) * [0])\n return struct.unpack_from(fmt, ba.tobytes())[0]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _swap_slice_indices(self, slc, make_slice=False):\n '''Swap slice indices\n\n Change slice indices from Verilog slicing (e.g. IEEE 1800-2012) to Python slicing.\n '''\n try:\n start = slc.start\n stop = slc.stop\n slc_step = slc.step\n except AttributeError:\n if make_slice:\n if slc < 0:\n slc += self.length()\n return slice(slc, slc + 1)\n else:\n return slc\n else:\n if not start and start != 0:\n slc_stop = self.length()\n elif start < 0:\n slc_stop = self.length() + start + 1\n else:\n slc_stop = start + 1\n if not stop and stop != 0:\n slc_start = 0\n elif stop < 0:\n slc_start = self.length() + stop\n else:\n slc_start = stop\n return slice(slc_start, slc_stop, slc_step)", "response": "Swap slice indices from Verilog slicing ( e. g. IEEE 1800 -2012 to Python slicing."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _run_seq(self, size):\n \n # Write the sequence to the sequence generator (hw driver)\n self['SEQ'].write(size) #write pattern to memory\n \n self['SEQ'].set_size(size) # set size\n self['SEQ'].set_repeat(1) # set repeat\n for _ in range(1):\n self['SEQ'].start() # start\n \n while not self['SEQ'].get_done():\n #time.sleep(0.1)\n print(\"Wait for done...\")", "response": "Runs the sequence generator."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nclears all the stuff that are not set.", "response": "def _clear_strobes(self):\n \"\"\"\n Resets the \"enable\" and \"load\" output streams to all 0.\n\n \"\"\"\n #reset some stuff\n self['SEQ']['GLOBAL_SHIFT_EN'].setall(False)\n self['SEQ']['GLOBAL_CTR_LD'].setall(False)\n self['SEQ']['GLOBAL_DAC_LD'].setall(False)\n self['SEQ']['PIXEL_SHIFT_EN'].setall(False)\n self['SEQ']['INJECTION'].setall(False)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nset data for outgoing stream", "response": "def set_data(self, data, addr=0):\n '''\n Sets data for outgoing stream\n '''\n if self._mem_bytes < len(data):\n raise ValueError('Size of data (%d bytes) is too big for memory (%d bytes)' % (len(data), self._mem_bytes))\n self._intf.write(self._conf['base_addr'] + self._spi_mem_offset + addr, data)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_data(self, size=None, addr=None):\n '''\n Gets data for incoming stream\n '''\n # readback memory offset\n if addr is None:\n addr = self._mem_bytes\n\n if size and self._mem_bytes < size:\n raise ValueError('Size is too big')\n\n if size is None:\n return self._intf.read(self._conf['base_addr'] + self._spi_mem_offset + addr, self._mem_bytes)\n else:\n return self._intf.read(self._conf['base_addr'] + self._spi_mem_offset + addr, size)", "response": "Gets data for incoming stream\n "} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ninitializes serial device. Parameters of serial.Serial: http://pyserial.sourceforge.net/pyserial_api.html Plus termination string parameter eol", "response": "def init(self):\n '''\n Initialize serial device.\n Parameters of serial.Serial: http://pyserial.sourceforge.net/pyserial_api.html\n Plus termination string parameter eol\n '''\n super(Serial, self).init()\n self.read_termination = self._init.get('read_termination', None)\n self.write_termination = self._init.get('write_termination', self.read_termination)\n try:\n self.read_termination = bytes(self.read_termination, 'utf-8')\n self.write_termination = bytes(self.write_termination, 'utf-8')\n except TypeError as e:\n logger.debug(e)\n self.timeout = self._init.get('timeout', None) # timeout of 0 returns immediately\n\n self._port = serial.Serial(**{key: value for key, value in self._init.items() if key not in (\"read_termination\", \"write_termination\")})"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef read_eeprom_calibration(self): # use default values for temperature, EEPROM values are usually not calibrated and random\n '''Reading EEPROM calibration for sources and regulators\n '''\n header = self.get_format()\n if header == self.HEADER_GPAC:\n data = self._read_eeprom(self.CAL_DATA_ADDR, size=calcsize(self.CAL_DATA_GPAC_FORMAT))\n for idx, channel in enumerate(self._ch_cal.iterkeys()):\n ch_data = data[idx * calcsize(self.CAL_DATA_CH_GPAC_FORMAT):(idx + 1) * calcsize(self.CAL_DATA_CH_GPAC_FORMAT)]\n values = unpack_from(self.CAL_DATA_CH_GPAC_FORMAT, ch_data)\n self._ch_cal[channel]['name'] = \"\".join([c for c in values[0] if (c in string.printable)]) # values[0].strip()\n self._ch_cal[channel]['default'] = values[1]\n self._ch_cal[channel]['min'] = values[2]\n self._ch_cal[channel]['max'] = values[3]\n self._ch_cal[channel]['ADCI']['gain'] = -values[4] # fix gain sign\n self._ch_cal[channel]['ADCI']['offset'] = values[5]\n self._ch_cal[channel]['ADCV']['gain'] = values[6]\n self._ch_cal[channel]['ADCV']['offset'] = values[7]\n self._ch_cal[channel]['DAC']['gain'] = values[8]\n self._ch_cal[channel]['DAC']['offset'] = values[9]\n self._ch_cal[channel]['limit'] = values[10]\n else:\n raise ValueError('EEPROM data format not supported (header: %s)' % header)", "response": "Reads the EEPROM calibration for sources and regulators."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nread the voltage from the ADC.", "response": "def get_voltage(self, channel, unit='V'):\n '''Reading voltage\n '''\n adc_ch = self._ch_map[channel]['ADCV']['adc_ch']\n address = self._ch_map[channel]['ADCV']['address']\n raw = self._get_adc_value(address=address)[adc_ch]\n\n dac_offset = self._ch_cal[channel]['ADCV']['offset']\n dac_gain = self._ch_cal[channel]['ADCV']['gain']\n\n voltage = ((raw - dac_offset) / dac_gain)\n\n if unit == 'raw':\n return raw\n elif unit == 'V':\n return voltage / 1000\n elif unit == 'mV':\n return voltage\n else:\n raise TypeError(\"Invalid unit type.\")"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreads current from the ADC.", "response": "def get_current(self, channel, unit='A'):\n '''Reading current\n '''\n values = self._get_adc_value(address=self._ch_map[channel]['ADCI']['address'])\n raw = values[self._ch_map[channel]['ADCI']['adc_ch']]\n dac_offset = self._ch_cal[channel]['ADCI']['offset']\n dac_gain = self._ch_cal[channel]['ADCI']['gain']\n\n if 'PWR' in channel:\n current = ((raw - dac_offset) / dac_gain)\n\n if unit == 'raw':\n return raw\n elif unit == 'A':\n return current / 1000\n elif unit == 'mA':\n return current\n elif unit == 'uA':\n return current * 1000\n else:\n raise TypeError(\"Invalid unit type.\")\n else:\n voltage = values[self._ch_map[channel]['ADCV']['adc_ch']]\n current = (((raw - voltage) - dac_offset) / dac_gain)\n\n if unit == 'raw':\n return raw\n elif unit == 'A':\n return current / 1000000\n elif unit == 'mA':\n return current / 1000\n elif unit == 'uA':\n return current\n else:\n raise TypeError(\"Invalid unit type.\")"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nenable or disable output of power channel.", "response": "def set_enable(self, channel, value):\n '''Enable/Disable output of power channel\n '''\n try:\n bit = self._ch_map[channel]['GPIOEN']['bit']\n except KeyError:\n raise ValueError('set_enable() not supported for channel %s' % channel)\n self._set_power_gpio_value(bit=bit, value=value)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning True if the channel is over current.", "response": "def get_over_current(self, channel):\n '''Reading over current status of power channel\n '''\n try:\n bit = self._ch_map[channel]['GPIOOC']['bit']\n except KeyError:\n raise ValueError('get_over_current() not supported for channel %s' % channel)\n return not self._get_power_gpio_value(bit)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nsetting the current limit for the specified channel.", "response": "def set_current_limit(self, channel, value, unit='A'):\n '''Setting current limit\n\n Note: same limit for all channels.\n '''\n # TODO: add units / calibration\n if unit == 'raw':\n value = value\n elif unit == 'A':\n value = int(value * 1000 * self.CURRENT_LIMIT_GAIN)\n elif unit == 'mA':\n value = int(value * self.CURRENT_LIMIT_GAIN)\n elif unit == 'uA':\n value = int(value / 1000 * self.CURRENT_LIMIT_GAIN)\n else:\n raise TypeError(\"Invalid unit type.\")\n\n I2cAnalogChannel._set_dac_value(self, address=self.CURRENT_LIMIT_DAC_SLAVE_ADD, dac_ch=self.CURRENT_LIMIT_DAC_CH, value=value)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsetting the current of current source to the given value.", "response": "def set_current(self, channel, value, unit='A'):\n '''Setting current of current source\n '''\n dac_offset = self._ch_cal[channel]['DAC']['offset']\n dac_gain = self._ch_cal[channel]['DAC']['gain']\n if unit == 'raw':\n value = value\n elif unit == 'A':\n value = int((-value * 1000000 - dac_offset) / dac_gain) # fix sign of output\n elif unit == 'mA':\n value = int((-value * 1000 - dac_offset) / dac_gain) # fix sign of output\n elif unit == 'uA':\n value = int((-value - dac_offset) / dac_gain) # fix sign of output\n else:\n raise TypeError(\"Invalid unit type.\")\n\n self._set_dac_value(channel=channel, value=value)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _calculateParameters(self, fout):\n q_d_f = 0\n\n '''\n fout = fref * (p_total / q_total) * (1 / div)\n\n p_total = 2 * ((p_counter + 4) + p_0) [16..1023]\n q_total = q_counter + 2 [2..129]\n div = [2,(3),4..127]\n\n constraints:\n\n f_ref * p_total / q_total = [100..400] MHz\n f_ref / q_total > 0.25 MHz\n '''\n for self.q_counter in range(128):\n self.q_total = self.q_counter + 2\n if (self.fref / self.q_total) < 0.25: # PLL constraint\n break\n for self.div in range(2, 128):\n q_d_f = self.q_total * self.div * fout\n if isinstance(q_d_f, six.integer_types) and q_d_f > (15 * self.fref): # = f0 * p\n if int(q_d_f) % int(self.fref) == 0: # p, q, and d found\n self.p_total = q_d_f / self.fref\n while self.p_total <= 16: # counter constraint\n self.p_total = self.p_total * 2\n self.div = self.div * 2\n if self.div > 127:\n break\n if self.p_total > 1023:\n break\n if ((self.fref * self.p_total / self.q_total) < 100 or (self.fref * self.p_total / self.q_total) > 400): # PLL constraint\n break\n if int(self.p_total) % 2 == 0:\n self.p_0 = 0\n else:\n self.p_0 = 1\n self.p_counter = ((int(self.p_total) - self.p_0) / 2) - 4 # set p counter value\n\n if self.div == 2:\n self.clk1SRC = 0x02\n self.div1N = 4\n else:\n if self.div == 3:\n self.clk1SRC = 0x03\n self.div1N = 6\n else:\n self.clk1SRC = 0x01\n self.div1N = self.div\n\n if self.p_total <= 44:\n self.chg_pump = 0\n else:\n if self.p_total <= 479:\n self.chg_pump = 1\n else:\n if self.p_total <= 639:\n self.chg_pump = 2\n else:\n if self.p_total <= 799:\n self.chg_pump = 3\n else:\n if self.p_total <= 1023:\n self.chg_pump = 4\n ftest = self.fref * self.p_total / self.q_total * 1 / self.div\n fvco = self.fref * self.p_total / self.q_total\n logger.info('PLL frequency set to ' + str(ftest) + ' MHz' + ' (VCO @ ' + str(fvco) + ' MHz)')\n return True\n logger.error('MIO_PLL: Could not find PLL parameters')\n return False", "response": "This method calculates the parameters of the base class."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ninitialize the device. Parameters of visa.ResourceManager().open_resource()", "response": "def init(self):\n '''\n Initialize the device.\n Parameters of visa.ResourceManager().open_resource()\n '''\n super(Visa, self).init()\n backend = self._init.get('backend', '') # Empty string means std. backend (NI VISA)\n rm = visa.ResourceManager(backend)\n try:\n logger.info('BASIL VISA TL with %s backend found the following devices: %s', backend, \", \".join(rm.list_resources()))\n except NotImplementedError: # some backends do not always implement the list_resources function\n logger.info('BASIL VISA TL with %s backend', backend)\n self._resource = rm.open_resource(**{key: value for key, value in self._init.items() if key not in (\"backend\",)})"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nread data in BRAM. returns numpy array.", "response": "def get_data(self):\n ''' Reading data in BRAM.\n\n Returns\n -------\n array : numpy.ndarray\n Array of unsigned integers (32 bit).\n '''\n fifo_int_size_1 = self.FIFO_INT_SIZE\n fifo_int_size_2 = self.FIFO_INT_SIZE\n if fifo_int_size_1 > fifo_int_size_2:\n fifo_int_size = fifo_int_size_2 # use smaller chunk\n logger.warning(\"Reading wrong FIFO size. Expected: %d <= %d\" % (fifo_int_size_1, fifo_int_size_2))\n else:\n fifo_int_size = fifo_int_size_1 # use smaller chunk\n return np.frombuffer(self._intf.read(self._conf['base_data_addr'], size=4 * fifo_int_size), dtype=np.dtype('H', address & 0x3FFF))) # 14-bit address, 16384 bytes\n\n n_pages, n_bytes = divmod(size, self.CAL_EEPROM_PAGE_SIZE)\n data = array('B')\n for _ in range(n_pages):\n data.extend(self._intf.read(self._base_addr + self.CAL_EEPROM_ADD | 1, size=self.CAL_EEPROM_PAGE_SIZE))\n\n if n_bytes > 0:\n data.extend(self._intf.read(self._base_addr + self.CAL_EEPROM_ADD | 1, size=n_bytes))\n\n return data", "response": "Read the EEPROM at the specified address and size."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef set_default(self, channels=None):\n '''Setting default voltage\n '''\n if not channels:\n channels = self._ch_cal.keys()\n for channel in channels:\n self.set_voltage(channel, self._ch_cal[channel]['default'], unit='V')", "response": "Sets default voltage for all the specified channels."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_voltage(self, channel, unit='V'):\n '''Reading voltage\n '''\n kwargs = self._ch_map[channel]['ADCV']\n voltage_raw = self._get_adc_value(**kwargs)\n\n voltage = (voltage_raw - self._ch_cal[channel]['ADCV']['offset']) / self._ch_cal[channel]['ADCV']['gain']\n\n if unit == 'raw':\n return voltage_raw\n elif unit == 'V':\n return voltage\n elif unit == 'mV':\n return voltage * 1000\n else:\n raise TypeError(\"Invalid unit type.\")", "response": "Reads voltage from the ADC and returns it in the specified unit."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_current(self, channel, unit='A'):\n '''Reading current\n '''\n kwargs = self._ch_map[channel]['ADCI']\n current_raw = self._get_adc_value(**kwargs)\n voltage = self.get_voltage(channel)\n\n current_raw_iq = current_raw - (self._ch_cal[channel]['ADCI']['iq_offset'] + self._ch_cal[channel]['ADCI']['iq_gain'] * voltage) # quiescent current (IQ) compensation\n current = (current_raw_iq - self._ch_cal[channel]['ADCI']['offset']) / self._ch_cal[channel]['ADCI']['gain']\n\n if unit == 'raw':\n return current_raw\n elif unit == 'raw_iq':\n return current_raw_iq\n elif unit == 'A':\n return current\n elif unit == 'mA':\n return current * 1000\n elif unit == 'uA':\n return current * 1000000\n else:\n raise TypeError(\"Invalid unit type.\")", "response": "Reads current from the ADC and returns it in the specified unit"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreads the EEPROM calibration for power regulators and temperature.", "response": "def read_eeprom_calibration(self, temperature=False): # use default values for temperature, EEPROM values are usually not calibrated and random\n '''Reading EEPROM calibration for power regulators and temperature\n '''\n header = self.get_format()\n if header == self.HEADER_V1:\n data = self._read_eeprom(self.CAL_DATA_ADDR, size=calcsize(self.CAL_DATA_V1_FORMAT))\n for idx, channel in enumerate(self._ch_cal.iterkeys()):\n ch_data = data[idx * calcsize(self.CAL_DATA_CH_V1_FORMAT):(idx + 1) * calcsize(self.CAL_DATA_CH_V1_FORMAT)]\n values = unpack_from(self.CAL_DATA_CH_V1_FORMAT, ch_data)\n self._ch_cal[channel]['name'] = \"\".join([c for c in values[0] if (c in string.printable)]) # values[0].strip()\n self._ch_cal[channel]['default'] = values[1]\n self._ch_cal[channel]['ADCI']['gain'] = values[2]\n self._ch_cal[channel]['ADCI']['offset'] = values[3]\n self._ch_cal[channel]['ADCI']['iq_gain'] = values[4]\n self._ch_cal[channel]['ADCI']['iq_offset'] = values[5]\n self._ch_cal[channel]['ADCV']['gain'] = values[6]\n self._ch_cal[channel]['ADCV']['offset'] = values[7]\n self._ch_cal[channel]['DACV']['gain'] = values[8]\n self._ch_cal[channel]['DACV']['offset'] = values[9]\n const_data = data[-calcsize(self.CAL_DATA_CONST_V1_FORMAT):]\n values = unpack_from(self.CAL_DATA_CONST_V1_FORMAT, const_data)\n if temperature:\n for channel in self._ch_cal.keys():\n self._ch_cal[channel]['VNTC']['B_NTC'] = values[0]\n self._ch_cal[channel]['VNTC']['R1'] = values[1]\n self._ch_cal[channel]['VNTC']['R2'] = values[2]\n self._ch_cal[channel]['VNTC']['R4'] = values[3]\n self._ch_cal[channel]['VNTC']['R_NTC_25'] = values[4]\n self._ch_cal[channel]['VNTC']['VREF'] = values[5]\n else:\n raise ValueError('EEPROM data format not supported (header: %s)' % header)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_temperature(self, channel, sensor='VNTC'):\n '''Reading temperature\n '''\n# NTC type SEMITEC 103KT1608 http://www.semitec.co.jp/english/products/pdf/KT_Thermistor.pdf\n#\n# R_NTC = R_25 * exp(B_NTC * (1/T - 1/T_25))\n#\n# R_NTC measured NTC resistance\n# R_NTC_25 resistance @ 25C\n# B_NTC temperature coefficient\n# Temperature current temperature (Kelvin)\n# T_25 298,15 K (25C)\n#\n# B_NTC NTC 'b' coefficient, NTC Semitec 103KT1608-1P\n# R_NTC_25 NTC 25C resistance, NTC Semitec 103KT1608-1P\n# R1 resistor value for NTC voltage divider\n# R2 value of R2 in the reference voltage divider\n# R4 value of R4 in the reference voltage divider\n# VREF supply voltage of the resistor bridge\n#\n\n kwargs = self._ch_map[channel][sensor]\n temp_raw = self._get_adc_value(**kwargs)\n\n v_adc = ((temp_raw - self._ch_cal.items()[0][1]['ADCV']['offset']) / self._ch_cal.items()[0][1]['ADCV']['gain']) # voltage, VDDA1\n k = self._ch_cal[channel][sensor]['R4'] / (self._ch_cal[channel][sensor]['R2'] + self._ch_cal[channel][sensor]['R4']) # reference voltage divider\n r_ntc = self._ch_cal[channel][sensor]['R1'] * (k - v_adc / self._ch_cal[channel][sensor]['VREF']) / (1 - k + v_adc / self._ch_cal[channel][sensor]['VREF']) # NTC resistance\n\n return (self._ch_cal[channel][sensor]['B_NTC'] / (log(r_ntc) - log(self._ch_cal[channel][sensor]['R_NTC_25']) + self._ch_cal[channel][sensor]['B_NTC'] / self.T_KELVIN_25)) - self.T_KELVIN_0", "response": "Reading temperature in the specified sensor"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nfiltering and convert any given data array of any dtype.", "response": "def convert_data_array(arr, filter_func=None, converter_func=None):\r\n '''Filter and convert any given data array of any dtype.\r\n\r\n Parameters\r\n ----------\r\n arr : numpy.array\r\n Data array of any dtype.\r\n filter_func : function\r\n Function that takes array and returns true or false for each item in array.\r\n converter_func : function\r\n Function that takes array and returns an array or tuple of arrays.\r\n\r\n Returns\r\n -------\r\n array of specified dimension (converter_func) and content (filter_func)\r\n '''\r\n# if filter_func != None:\r\n# if not hasattr(filter_func, '__call__'):\r\n# raise ValueError('Filter is not callable')\r\n if filter_func:\r\n array = arr[filter_func(arr)] # Indexing with Boolean Arrays\r\n# if converter_func != None:\r\n# if not hasattr(converter_func, '__call__'):\r\n# raise ValueError('Converter is not callable')\r\n if converter_func:\r\n arr = converter_func(arr)\r\n return array"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef logical_and(f1, f2): # function factory\r\n '''Logical and from functions.\r\n\r\n Parameters\r\n ----------\r\n f1, f2 : function\r\n Function that takes array and returns true or false for each item in array.\r\n\r\n Returns\r\n -------\r\n Function\r\n\r\n Examples\r\n --------\r\n filter_func=logical_and(is_data_record, is_data_from_channel(4)) # new filter function\r\n filter_func(array) # array that has Data Records from channel 4\r\n '''\r\n def f_and(arr):\r\n return np.logical_and(f1(arr), f2(arr))\r\n f_and.__name__ = f1.__name__ + \"_and_\" + f2.__name__\r\n return f_and", "response": "Logical and from functions.\r\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef logical_or(f1, f2): # function factory\r\n '''Logical or from functions.\r\n\r\n Parameters\r\n ----------\r\n f1, f2 : function\r\n Function that takes array and returns true or false for each item in array.\r\n\r\n Returns\r\n -------\r\n Function\r\n '''\r\n def f_or(arr):\r\n return np.logical_or(f1(arr), f2(arr))\r\n f_or.__name__ = f1.__name__ + \"_or_\" + f2.__name__\r\n return f_or", "response": "Returns a function that returns true or false for each item in array f1 and f2."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef logical_not(f): # function factory\r\n '''Logical not from functions.\r\n\r\n Parameters\r\n ----------\r\n f1, f2 : function\r\n Function that takes array and returns true or false for each item in array.\r\n\r\n Returns\r\n -------\r\n Function\r\n '''\r\n def f_not(arr):\r\n return np.logical_not(f(arr))\r\n f_not.__name__ = \"not_\" + f.__name__\r\n return f_not", "response": "Returns a function that returns true or false for each item in array f"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef arr_select(value): # function factory\r\n '''Selecting array elements by bitwise and comparison to a given value.\r\n\r\n Parameters:\r\n value : int\r\n Value to which array elements are compared to.\r\n\r\n Returns:\r\n array : np.array\r\n '''\r\n def f_eq(arr):\r\n return np.equal(np.bitwise_and(arr, value), value)\r\n f_eq.__name__ = \"arr_bitwise_and_\" + str(value) # or use inspect module: inspect.stack()[0][3]\r\n return f_eq", "response": "Returns a function that returns True if the array elements are equal to a given value."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef arr_astype(arr_type): # function factory\r\n '''Change dtype of array.\r\n\r\n Parameters:\r\n arr_type : str, np.dtype\r\n Character codes (e.g. 'b', '>H'), type strings (e.g. 'i4', 'f8'), Python types (e.g. float, int) and numpy dtypes (e.g. np.uint32) are allowed.\r\n\r\n Returns:\r\n array : np.array\r\n '''\r\n def f_astype(arr):\r\n return arr.astype(arr_type)\r\n f_astype.__name__ = \"arr_astype_\" + str(arr_type) # or use inspect module: inspect.stack()[0][3]\r\n return f_astype", "response": "Returns a function that can be used to change dtype of array."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef send(self, obj):\n assert isinstance(obj, ProtocolBase)\n string = pickle.dumps(obj)\n length = len(string)\n self.sock.sendall(struct.pack(\" times_checked:\n False\n if delay_between:\n try:\n sleep(delay_between)\n except IOError: # negative values\n pass\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _set_dac_value(self, channel, value):\n '''Write DAC\n '''\n self._intf.write(self._base_addr + self.MAX_5380_ADD, array('B', pack('B', value)))", "response": "Write the value to the DAC."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _set_dac_value(self, channel, value):\n '''Write DAC\n '''\n # DAC value cannot be -128\n if value == -128:\n value = -127\n if value < 0:\n sign = 1\n else:\n sign = 0\n value = (sign << 7) | (0x7F & abs(value))\n self._intf.write(self._base_addr + self.DS_4424_ADD, array('B', pack('BB', channel, value)))", "response": "Write the value to the specified channel of the DAC."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef read_eeprom_calibration(self, temperature=False): # use default values for temperature, EEPROM values are usually not calibrated and random\n '''Reading EEPROM calibration for power regulators and temperature\n '''\n header = self.get_format()\n if header == self.HEADER_V2:\n data = self._read_eeprom(self.CAL_DATA_ADDR, size=calcsize(self.CAL_DATA_V2_FORMAT))\n for idx, channel in enumerate(self._ch_cal.iterkeys()):\n ch_data = data[idx * calcsize(self.CAL_DATA_CH_V2_FORMAT):(idx + 1) * calcsize(self.CAL_DATA_CH_V2_FORMAT)]\n values = unpack_from(self.CAL_DATA_CH_V2_FORMAT, ch_data)\n self._ch_cal[channel]['name'] = \"\".join([c for c in values[0] if (c in string.printable)]) # values[0].strip()\n self._ch_cal[channel]['default'] = values[1]\n self._ch_cal[channel]['ADCI']['gain'] = values[2]\n self._ch_cal[channel]['ADCI']['offset'] = values[3]\n self._ch_cal[channel]['ADCI']['iq_gain'] = values[4]\n self._ch_cal[channel]['ADCI']['iq_offset'] = values[5]\n self._ch_cal[channel]['ADCV']['gain'] = values[6]\n self._ch_cal[channel]['ADCV']['offset'] = values[7]\n self._ch_cal[channel]['DACV']['gain'] = values[8]\n self._ch_cal[channel]['DACV']['offset'] = values[9]\n if temperature:\n self._ch_cal[channel]['NTC']['B_NTC'] = values[10]\n self._ch_cal[channel]['NTC']['R1'] = values[11]\n self._ch_cal[channel]['NTC']['R2'] = values[12]\n self._ch_cal[channel]['NTC']['R4'] = values[13]\n self._ch_cal[channel]['NTC']['R_NTC_25'] = values[14]\n self._ch_cal[channel]['NTC']['VREF'] = values[15]\n else:\n raise ValueError('EEPROM data format not supported (header: %s)' % header)", "response": "Reads the EEPROM calibration for power regulators and temperature."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_current_limit(self, channel, value, unit='A'):\n '''Setting current limit\n\n Note: same limit for all channels.\n '''\n dac_offset = self._ch_cal[channel]['DACI']['offset']\n dac_gain = self._ch_cal[channel]['DACI']['gain']\n\n if unit == 'raw':\n value = value\n elif unit == 'A':\n value = int((value - dac_offset) / dac_gain)\n elif unit == 'mA':\n value = int((value / 1000 - dac_offset) / dac_gain)\n else:\n raise TypeError(\"Invalid unit type.\")\n\n DacMax5380._set_dac_value(self, channel, value)", "response": "Set current limit for all channels."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_position(self, x, y, speed=None):\n ''' Move chuck to absolute position in um'''\n if speed:\n self._intf.write('MoveChuckSubsite %1.1f %1.1f R Y %d' % (x, y, speed))\n else:\n self._intf.write('MoveChuckSubsite %1.1f %1.1f R Y' % (x, y))", "response": "Move chuck to absolute position in um"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef move_position(self, dx, dy, speed=None):\n ''' Move chuck relative to actual position in um'''\n if speed:\n self._intf.write('MoveChuckPosition %1.1f %1.1f R Y %d' % (dx, dy, speed))\n else:\n self._intf.write('MoveChuckPosition %1.1f %1.1f R Y' % (dx, dy))", "response": "Move chuck relative to actual position in um"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the current read chuck position.", "response": "def get_position(self):\n ''' Read chuck position (x, y, z)'''\n reply = self._intf.query('ReadChuckPosition Y H')[2:]\n return [float(i) for i in reply.split()]"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nmoves chuck to wafer map chip index", "response": "def get_die(self):\n ''' Move chuck to wafer map chip index'''\n reply = self._intf.query('ReadMapPosition')\n values = reply[2:].split(' ')\n return (int(values[0]), int(values[1]))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef clear(self):\n 'Clear tracks in memory - all zero'\n for track in self._tracks:\n self._tracks[track].setall(False)", "response": "Clear tracks in memory - all zero"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_data(self):\n ''' Reading data from SiTCP FIFO (via TCP).\n\n Returns\n -------\n array : numpy.ndarray\n Array of unsigned integers (32 bit).\n '''\n fifo_size = self._intf._get_tcp_data_size()\n fifo_int_size = int((fifo_size - (fifo_size % 4)) / 4)\n data = self._intf._get_tcp_data(fifo_int_size * 4)\n return np.frombuffer(data, dtype=np.dtype(' [\"--local-scheduler\"]\n \"\"\"\n global _global_cmdline_args\n\n if _global_cmdline_args:\n return _global_cmdline_args\n\n luigi_parser = luigi.cmdline_parser.CmdlineParser.get_instance()\n if not luigi_parser:\n return None\n\n _global_cmdline_args = root_task_parser().parse_known_args(luigi_parser.cmdline_args)[1]\n\n return _global_cmdline_args"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a dictionary of global command line arguments to their current values.", "response": "def global_cmdline_values():\n \"\"\"\n Returns a dictionary of global command line arguments (computed with\n :py:func:`global_cmdline_args`) to their current values. The returnd dictionary is cached.\n Example:\n\n .. code-block:: python\n\n global_cmdline_values()\n # -> {\"core_local_scheduler\": True}\n \"\"\"\n global _global_cmdline_values\n\n if _global_cmdline_values:\n return _global_cmdline_values\n\n luigi_parser = luigi.cmdline_parser.CmdlineParser.get_instance()\n if not luigi_parser:\n return None\n\n # go through all actions of the full luigi parser and compare option strings\n # with the global cmdline args\n parser = full_parser()\n global_args = global_cmdline_args()\n _global_cmdline_values = {}\n for action in parser._actions:\n if any(arg in action.option_strings for arg in global_args):\n _global_cmdline_values[action.dest] = getattr(luigi_parser.known_args, action.dest)\n\n return _global_cmdline_values"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_cmdline_arg(args, arg, *values):\n if arg not in args:\n args = list(args) + [arg] + list(values)\n return args", "response": "Adds a command line argument arg to a list of argument values."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nremoving the command line argument arg from a list of arguments args.", "response": "def remove_cmdline_arg(args, arg, n=1):\n \"\"\"\n Removes the command line argument *args* from a list of arguments *args*, e.g. as returned from\n :py:func:`global_cmdline_args`. When *n* is 1 or less, only the argument is removed. Otherwise,\n the following *n-1* values are removed. Example:\n\n .. code-block:: python\n\n args = global_cmdline_values()\n # -> [\"--local-scheduler\", \"--workers\", \"4\"]\n\n remove_cmdline_arg(args, \"--local-scheduler\")\n # -> [\"--workers\", \"4\"]\n\n remove_cmdline_arg(args, \"--workers\", 2)\n # -> [\"--local-scheduler\"]\n \"\"\"\n if arg in args:\n idx = args.index(arg)\n args = list(args)\n del args[idx:idx + max(n, 1)]\n return args"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef split_transfer_kwargs(kwargs, skip=None):\n skip = make_list(skip) if skip else []\n transfer_kwargs = {\n name: kwargs.pop(name)\n for name in [\"cache\", \"prefer_cache\", \"retries\", \"retry_delay\"]\n if name in kwargs and name not in skip\n }\n return transfer_kwargs, kwargs", "response": "Takes keyword arguments kwargs and returns them in two separate dictionaries depending on their\n content."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef job_data(cls, job_id=dummy_job_id, branches=None, **kwargs):\n return dict(job_id=job_id, branches=branches or [])", "response": "Returns a dictionary containing default job submission information such as the job_id and branches."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef job_data(cls, job_id=dummy_job_id, status=None, code=None, error=None, **kwargs):\n return dict(job_id=job_id, status=status, code=code, error=error)", "response": "Returns a dictionary containing default job status information such as the job_id status code and error message."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget the attribute of the actial task named name.", "response": "def _get_task_attribute(self, name, fallback=False):\n \"\"\"\n Return an attribute of the actial task named ``_``.\n When the attribute does not exist and *fallback* is *True*, try to return the task attribute\n simply named *name*. In any case, if a requested task attribute is eventually not found, an\n AttributeError is raised.\n \"\"\"\n attr = \"{}_{}\".format(self.workflow_type, name)\n if not fallback:\n return getattr(self.task, attr)\n else:\n value = getattr(self.task, attr, no_value)\n return value if value != no_value else getattr(self.task, name)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef output(self):\n task = self.task\n\n # get the directory where the control outputs are stored\n out_dir = self._get_task_attribute(\"output_directory\")()\n\n # define outputs\n outputs = OrderedDict()\n postfix = self._get_task_attribute(\"output_postfix\")()\n\n # a file containing the submission data, i.e. job ids etc\n submission_file = \"{}_submission{}.json\".format(self.workflow_type, postfix)\n outputs[\"submission\"] = out_dir.child(submission_file, type=\"f\")\n outputs[\"submission\"].optional = True\n\n # a file containing status data when the jobs are done\n if not task.no_poll:\n status_file = \"{}_status{}.json\".format(self.workflow_type, postfix)\n outputs[\"status\"] = out_dir.child(status_file, type=\"f\")\n outputs[\"status\"].optional = True\n\n # update with upstream output when not just controlling running jobs\n if not task.is_controlling_remote_jobs():\n outputs.update(super(BaseRemoteWorkflowProxy, self).output())\n\n return outputs", "response": "Returns the default workflow outputs in an ordered dictionary."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef dump_submission_data(self):\n # renew the dashboard config\n self.submission_data[\"dashboard_config\"] = self.dashboard.get_persistent_config()\n\n # write the submission data to the output file\n self._outputs[\"submission\"].dump(self.submission_data, formatter=\"json\", indent=4)", "response": "Dumps the current submission data to the submission file."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef run(self):\n task = self.task\n self._outputs = self.output()\n\n # create the job dashboard interface\n self.dashboard = task.create_job_dashboard() or NoJobDashboard()\n\n # read submission data and reset some values\n submitted = not task.ignore_submission and self._outputs[\"submission\"].exists()\n if submitted:\n self.submission_data.update(self._outputs[\"submission\"].load(formatter=\"json\"))\n task.tasks_per_job = self.submission_data.tasks_per_job\n self.dashboard.apply_config(self.submission_data.dashboard_config)\n\n # when the branch outputs, i.e. the \"collection\" exists, just create dummy control outputs\n if \"collection\" in self._outputs and self._outputs[\"collection\"].exists():\n self.touch_control_outputs()\n\n # cancel jobs?\n elif self._cancel_jobs:\n if submitted:\n self.cancel()\n\n # cleanup jobs?\n elif self._cleanup_jobs:\n if submitted:\n self.cleanup()\n\n # submit and/or wait while polling\n else:\n # maybe set a tracking url\n tracking_url = self.dashboard.create_tracking_url()\n if tracking_url:\n task.set_tracking_url(tracking_url)\n\n # ensure the output directory exists\n if not submitted:\n self._outputs[\"submission\"].parent.touch()\n\n # at this point, when the status file exists, it is considered outdated\n if \"status\" in self._outputs:\n self._outputs[\"status\"].remove()\n\n try:\n # instantiate the configured job file factory, not kwargs yet\n self.job_file_factory = self.create_job_file_factory()\n\n # submit\n if not submitted:\n # set the initial list of unsubmitted jobs\n branches = sorted(task.branch_map.keys())\n branch_chunks = list(iter_chunks(branches, task.tasks_per_job))\n self.submission_data.unsubmitted_jobs = OrderedDict(\n (i + 1, branches) for i, branches in enumerate(branch_chunks)\n )\n self.submit()\n\n # sleep once to give the job interface time to register the jobs\n post_submit_delay = self._get_task_attribute(\"post_submit_delay\")()\n if post_submit_delay:\n time.sleep(post_submit_delay)\n\n # start status polling when a) no_poll is not set, or b) the jobs were already\n # submitted so that failed jobs are resubmitted after a single polling iteration\n if not task.no_poll or submitted:\n self.poll()\n\n finally:\n # in any event, cleanup the job file\n if self.job_file_factory:\n self.job_file_factory.cleanup_dir(force=False)", "response": "This method is called by the base class to start the processing of jobs and creates the job dashboard interface and the status polling and cleanup jobs."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef cancel(self):\n task = self.task\n\n # get job ids from submission data\n job_ids = [\n d[\"job_id\"] for d in self.submission_data.jobs.values()\n if d[\"job_id\"] not in (self.submission_data.dummy_job_id, None)\n ]\n if not job_ids:\n return\n\n # cancel jobs\n task.publish_message(\"going to cancel {} jobs\".format(len(job_ids)))\n errors = self.job_manager.cancel_batch(job_ids)\n\n # print errors\n if errors:\n print(\"{} error(s) occured while cancelling {} job(s) of task {}:\".format(\n len(errors), len(job_ids), task.task_id))\n tmpl = \" {}\"\n for i, err in enumerate(errors):\n print(tmpl.format(err))\n if i + 1 >= self.show_errors:\n remaining = len(errors) - self.show_errors\n if remaining > 0:\n print(\" ... and {} more\".format(remaining))\n break\n\n # inform the dashboard\n for job_num, job_data in six.iteritems(self.submission_data.jobs):\n task.forward_dashboard_event(self.dashboard, job_data, \"action.cancel\", job_num)", "response": "Cancels running jobs. The job ids are read from the submission file which has to exist\n for obvious reasons."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef cleanup(self):\n task = self.task\n\n # get job ids from submission data\n job_ids = [\n d[\"job_id\"] for d in self.submission_data.jobs.values()\n if d[\"job_id\"] not in (self.submission_data.dummy_job_id, None)\n ]\n if not job_ids:\n return\n\n # cleanup jobs\n task.publish_message(\"going to cleanup {} jobs\".format(len(job_ids)))\n errors = self.job_manager.cleanup_batch(job_ids)\n\n # print errors\n if errors:\n print(\"{} error(s) occured while cleaning up {} job(s) of task {}:\".format(\n len(errors), len(job_ids), task.task_id))\n tmpl = \" {}\"\n for i, err in enumerate(errors):\n print(tmpl.format(err))\n if i + 1 >= self.show_errors:\n remaining = len(errors) - self.show_errors\n if remaining > 0:\n print(\" ... and {} more\".format(remaining))\n break", "response": "Clean up jobs on the remote run location."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef submit(self, retry_jobs=None):\n task = self.task\n\n # helper to check if a job can be skipped\n # rule: skip a job when only_missing is set to True and all its branch tasks are complete\n def skip_job(job_num, branches):\n if not task.only_missing:\n return False\n elif job_num in self.skip_jobs:\n return self.skip_jobs[job_num]\n else:\n self.skip_jobs[job_num] = all(task.as_branch(b).complete() for b in branches)\n # when the job is skipped, write a dummy entry into submission data\n if self.skip_jobs[job_num]:\n self.submission_data.jobs[job_num] = self.submission_data_cls.job_data(\n branches=branches)\n return self.skip_jobs[job_num]\n\n # collect data of jobs that should be submitted: num -> branches\n submit_jobs = OrderedDict()\n\n # handle jobs for resubmission\n if retry_jobs:\n for job_num, branches in six.iteritems(retry_jobs):\n # even retry jobs can be skipped\n if skip_job(job_num, branches):\n continue\n\n # the number of parallel jobs might be reached as well\n # in that case, add the jobs back to the unsubmitted ones and update the job id\n n = self.poll_data.n_active + len(submit_jobs)\n if n >= self.poll_data.n_parallel:\n self.submission_data.unsubmitted_jobs[job_num] = branches\n del self.submission_data.jobs[job_num]\n continue\n\n # mark job for resubmission\n submit_jobs[job_num] = sorted(branches)\n\n # fill with jobs from the list of unsubmitted jobs until maximum number of parallel jobs is\n # reached\n new_jobs = OrderedDict()\n for job_num, branches in list(self.submission_data.unsubmitted_jobs.items()):\n # remove jobs that don't need to be submitted\n if skip_job(job_num, branches):\n del self.submission_data.unsubmitted_jobs[job_num]\n continue\n\n # do nothing when n_parallel is already reached\n n = self.poll_data.n_active + len(submit_jobs) + len(new_jobs)\n if n >= self.poll_data.n_parallel:\n continue\n\n # mark jobs for submission\n del self.submission_data.unsubmitted_jobs[job_num]\n new_jobs[job_num] = sorted(branches)\n\n # add new jobs to the jobs to submit, maybe also shuffle\n new_submission_data = OrderedDict()\n new_job_nums = list(new_jobs.keys())\n if task.shuffle_jobs:\n random.shuffle(new_job_nums)\n for job_num in new_job_nums:\n submit_jobs[job_num] = new_jobs[job_num]\n\n # when there is nothing to submit, dump the submission data to the output file and stop here\n if not submit_jobs:\n if retry_jobs or self.submission_data.unsubmitted_jobs:\n self.dump_submission_data()\n return new_submission_data\n\n # create job submission files\n job_files = [self.create_job_file(*tpl) for tpl in six.iteritems(submit_jobs)]\n\n # log some stats\n dst_info = self.destination_info() or \"\"\n dst_info = dst_info and (\", \" + dst_info)\n task.publish_message(\"going to submit {} {} job(s){}\".format(\n len(submit_jobs), self.workflow_type, dst_info))\n\n # actual submission\n job_ids = self.submit_jobs(job_files)\n\n # store submission data\n errors = []\n for job_num, job_id in six.moves.zip(submit_jobs, job_ids):\n # handle errors\n error = (job_num, job_id) if isinstance(job_id, Exception) else None\n if error:\n errors.append((job_num, job_id))\n job_id = self.submission_data_cls.dummy_job_id\n\n # build the job data\n branches = submit_jobs[job_num]\n job_data = self.submission_data_cls.job_data(job_id=job_id, branches=branches)\n self.submission_data.jobs[job_num] = job_data\n new_submission_data[job_num] = job_data\n\n # set the attempt number in the submission data\n self.submission_data.attempts.setdefault(job_num, 0)\n\n # inform the dashboard\n task.forward_dashboard_event(self.dashboard, job_data, \"action.submit\", job_num)\n\n # dump the submission data to the output file\n self.dump_submission_data()\n\n # raise exceptions or log\n if errors:\n print(\"{} error(s) occured during job submission of task {}:\".format(\n len(errors), task.task_id))\n tmpl = \" job {}: {}\"\n for i, tpl in enumerate(errors):\n print(tmpl.format(*tpl))\n if i + 1 >= self.show_errors:\n remaining = len(errors) - self.show_errors\n if remaining > 0:\n print(\" ... and {} more\".format(remaining))\n break\n else:\n task.publish_message(\"submitted {} job(s)\".format(len(submit_jobs)) + dst_info)\n\n return new_submission_data", "response": "Submit all jobs in the current branch set."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef poll(self):\n task = self.task\n\n # total job count\n n_jobs = len(self.submission_data)\n\n # store the number of consecutive polling failures and get the maximum number of polls\n n_poll_fails = 0\n if task.walltime == NO_FLOAT:\n max_polls = sys.maxsize\n else:\n max_polls = int(math.ceil((task.walltime * 3600.) / (task.poll_interval * 60.)))\n\n # update variable attributes for polling\n self.poll_data.n_finished_min = task.acceptance * (1 if task.acceptance > 1 else n_jobs)\n self.poll_data.n_failed_max = task.tolerance * (1 if task.tolerance > 1 else n_jobs)\n\n # track finished and failed jobs in dicts holding status data\n finished_jobs = OrderedDict()\n failed_jobs = OrderedDict()\n\n # start the poll loop\n for i in six.moves.range(max_polls):\n # sleep after the first iteration\n if i > 0:\n time.sleep(task.poll_interval * 60)\n\n # determine the currently active jobs, i.e., the jobs whose states should be checked\n active_jobs = OrderedDict()\n for job_num, data in six.iteritems(self.submission_data.jobs):\n if job_num in finished_jobs or job_num in failed_jobs:\n continue\n if self.skip_jobs.get(job_num):\n finished_jobs[job_num] = self.status_data_cls.job_data(\n status=self.job_manager.FINISHED, code=0)\n else:\n data = data.copy()\n if not data[\"job_id\"]:\n data[\"job_id\"] = self.status_data_cls.dummy_job_id\n active_jobs[job_num] = data\n self.poll_data.n_active = len(active_jobs)\n\n # query job states\n job_ids = [data[\"job_id\"] for data in six.itervalues(active_jobs)] # noqa: F812\n _states, errors = self.job_manager.query_batch(job_ids)\n if errors:\n print(\"{} error(s) occured during job status query of task {}:\".format(\n len(errors), task.task_id))\n tmpl = \" {}\"\n for i, err in enumerate(errors):\n print(tmpl.format(err))\n if i + 1 >= self.show_errors:\n remaining = len(errors) - self.show_errors\n if remaining > 0:\n print(\" ... and {} more\".format(remaining))\n break\n\n n_poll_fails += 1\n if task.poll_fails > 0 and n_poll_fails > task.poll_fails:\n raise Exception(\"poll_fails exceeded\")\n else:\n continue\n else:\n n_poll_fails = 0\n\n # states stores job_id's as keys, so replace them by using job_num's\n states = OrderedDict()\n for job_num, data in six.iteritems(active_jobs):\n states[job_num] = self.status_data_cls.job_data(**_states[data[\"job_id\"]])\n\n # store jobs per status and take further actions depending on the status\n pending_jobs = OrderedDict()\n running_jobs = OrderedDict()\n newly_failed_jobs = OrderedDict()\n retry_jobs = OrderedDict()\n for job_num, data in six.iteritems(states):\n if data[\"status\"] == self.job_manager.PENDING:\n pending_jobs[job_num] = data\n task.forward_dashboard_event(self.dashboard, data, \"status.pending\", job_num)\n\n elif data[\"status\"] == self.job_manager.RUNNING:\n running_jobs[job_num] = data\n task.forward_dashboard_event(self.dashboard, data, \"status.running\", job_num)\n\n elif data[\"status\"] == self.job_manager.FINISHED:\n finished_jobs[job_num] = data\n self.poll_data.n_active -= 1\n task.forward_dashboard_event(self.dashboard, data, \"status.finished\", job_num)\n\n elif data[\"status\"] in (self.job_manager.FAILED, self.job_manager.RETRY):\n newly_failed_jobs[job_num] = data\n self.poll_data.n_active -= 1\n\n # retry or ultimately failed?\n if self.job_retries[job_num] < task.retries:\n self.job_retries[job_num] += 1\n self.submission_data.attempts[job_num] += 1\n data[\"status\"] = self.job_manager.RETRY\n retry_jobs[job_num] = self.submission_data.jobs[job_num][\"branches\"]\n task.forward_dashboard_event(self.dashboard, data, \"status.retry\", job_num)\n else:\n failed_jobs[job_num] = data\n task.forward_dashboard_event(self.dashboard, data, \"status.failed\", job_num)\n\n else:\n raise Exception(\"unknown job status '{}'\".format(data[\"status\"]))\n\n # gather some counts\n n_pending = len(pending_jobs)\n n_running = len(running_jobs)\n n_finished = len(finished_jobs)\n n_retry = len(retry_jobs)\n n_failed = len(failed_jobs)\n n_unsubmitted = len(self.submission_data.unsubmitted_jobs)\n\n # log the status line\n counts = (n_pending, n_running, n_finished, n_retry, n_failed)\n if task.parallel_jobs > 0:\n counts = (n_unsubmitted,) + counts\n status_line = self.job_manager.status_line(counts, last_counts=True, sum_counts=n_jobs,\n color=True, align=task.align_status_line)\n task.publish_message(status_line)\n self.last_status_counts = counts\n\n # inform the scheduler about the progress\n task.publish_progress(100. * n_finished / n_jobs)\n\n # log newly failed jobs\n if newly_failed_jobs:\n print(\"{} failed job(s) in task {}:\".format(len(newly_failed_jobs), task.task_id))\n tmpl = \" job: {}, branches: {}, id: {job_id}, status: {status}, code: {code}, \" \\\n \"error: {error}\"\n for i, (job_num, data) in enumerate(six.iteritems(newly_failed_jobs)):\n branches = self.submission_data.jobs[job_num][\"branches\"]\n print(tmpl.format(job_num, \",\".join(str(b) for b in branches), **data))\n if i + 1 >= self.show_errors:\n remaining = len(newly_failed_jobs) - self.show_errors\n if remaining > 0:\n print(\" ... and {} more\".format(remaining))\n break\n\n # infer the overall status\n reached_end = n_jobs == n_finished + n_failed\n finished = n_finished >= self.poll_data.n_finished_min\n failed = n_failed > self.poll_data.n_failed_max\n unreachable = n_jobs - n_failed < self.poll_data.n_finished_min\n if finished:\n # write status output\n if \"status\" in self._outputs:\n status_data = self.status_data_cls()\n status_data.jobs.update(finished_jobs)\n status_data.jobs.update(states)\n self._outputs[\"status\"].dump(status_data, formatter=\"json\", indent=4)\n break\n elif failed:\n failed_nums = [job_num for job_num in failed_jobs if job_num not in retry_jobs]\n raise Exception(\"tolerance exceeded for jobs {}\".format(failed_nums))\n elif unreachable:\n err = None\n if reached_end:\n err = \"acceptance of {} not reached, total jobs: {}, failed jobs: {}\"\n elif task.check_unreachable_acceptance:\n err = \"acceptance of {} unreachable, total jobs: {}, failed jobs: {}\"\n if err:\n raise Exception(err.format(self.poll_data.n_finished_min, n_jobs, n_failed))\n\n # configurable poll callback\n task.poll_callback(self.poll_data)\n\n # trigger automatic resubmission and submission of unsubmitted jobs\n self.submit(retry_jobs)\n\n # break when no polling is desired\n # we can get to this point when there was already a submission and the no_poll\n # parameter was set so that only failed jobs are resubmitted once\n if task.no_poll:\n break\n else:\n # walltime exceeded\n raise Exception(\"walltime exceeded\")", "response": "This method is called by the poll method in the job manager to check the status of the jobs in the submission."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating and saves dummy submission and status files. This method is called in case the branch task outputs exist.", "response": "def touch_control_outputs(self):\n \"\"\"\n Creates and saves dummy submission and status files. This method is called in case the\n collection of branch task outputs exists.\n \"\"\"\n task = self.task\n\n # create the parent directory\n self._outputs[\"submission\"].parent.touch()\n\n # get all branch indexes and chunk them by tasks_per_job\n branch_chunks = list(iter_chunks(task.branch_map.keys(), task.tasks_per_job))\n\n # submission output\n if not self._outputs[\"submission\"].exists():\n submission_data = self.submission_data.copy()\n # set dummy submission data\n submission_data.jobs.clear()\n for i, branches in enumerate(branch_chunks):\n job_num = i + 1\n submission_data.jobs[job_num] = self.submission_data_cls.job_data(branches=branches)\n self._outputs[\"submission\"].dump(submission_data, formatter=\"json\", indent=4)\n\n # status output\n if \"status\" in self._outputs and not self._outputs[\"status\"].exists():\n status_data = self.status_data_cls()\n # set dummy status data\n for i, branches in enumerate(branch_chunks):\n job_num = i + 1\n status_data.jobs[job_num] = self.status_data_cls.job_data(\n status=self.job_manager.FINISHED, code=0)\n self._outputs[\"status\"].dump(status_data, formatter=\"json\", indent=4)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef forward_dashboard_event(self, dashboard, job_data, event, job_num):\n # possible events:\n # - action.submit\n # - action.cancel\n # - status.pending\n # - status.running\n # - status.finished\n # - status.retry\n # - status.failed\n # forward to dashboard in any event by default\n return dashboard.publish(job_data, event, job_num)", "response": "Forward the event to the dashboard."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef localize(self, mode=\"r\", perm=None, parent_perm=None, **kwargs):\n if mode not in (\"r\", \"w\"):\n raise Exception(\"unknown mode '{}', use r or w\".format(mode))\n\n # get additional arguments\n skip_copy = kwargs.pop(\"skip_copy\", False)\n is_tmp = kwargs.pop(\"is_tmp\", mode == \"w\")\n\n if mode == \"r\":\n if is_tmp:\n # create a temporary target\n tmp = self.__class__(is_tmp=self.ext(n=1) or True)\n\n # always copy\n self.copy_to_local(tmp)\n\n # yield the copy\n try:\n yield tmp\n finally:\n tmp.remove()\n else:\n # simply yield\n yield self\n\n else: # write mode\n if is_tmp:\n # create a temporary target\n tmp = self.__class__(is_tmp=self.ext(n=1) or True)\n\n # copy when existing\n if not skip_copy and self.exists():\n self.copy_to_local(tmp)\n\n # yield the copy\n try:\n yield tmp\n\n # move back again\n if tmp.exists():\n tmp.move_to_local(self, dir_perm=parent_perm)\n self.chmod(perm)\n else:\n logger.warning(\"cannot move non-existing localized file target {!r}\".format(\n self))\n finally:\n tmp.remove()\n else:\n # create the parent dir\n self.parent.touch(perm=parent_perm)\n\n # simply yield\n yield self\n\n if self.exists():\n self.chmod(perm)", "response": "A generator that yields the local version of the current file."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nset up the command line parser for the index subprogram and adds it to the given sub_parsers.", "response": "def setup_parser(sub_parsers):\n \"\"\"\n Sets up the command line parser for the *index* subprogram and adds it to *sub_parsers*.\n \"\"\"\n parser = sub_parsers.add_parser(\"index\", prog=\"law index\", description=\"Create or update the\"\n \" (human-readable) law task index file ({}). This is only required for the shell\"\n \" auto-completion.\".format(Config.instance().get(\"core\", \"index_file\")))\n\n parser.add_argument(\"--modules\", \"-m\", nargs=\"+\", help=\"additional modules to traverse\")\n parser.add_argument(\"--no-externals\", \"-e\", action=\"store_true\", help=\"skip external tasks\")\n parser.add_argument(\"--remove\", \"-r\", action=\"store_true\", help=\"remove the index file and\"\n \" exit\")\n parser.add_argument(\"--location\", \"-l\", action=\"store_true\", help=\"print the location of the\"\n \" index file and exit\")\n parser.add_argument(\"--verbose\", \"-v\", action=\"store_true\", help=\"verbose output\")"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nexecutes the index subprogram with parsed commandline args.", "response": "def execute(args):\n \"\"\"\n Executes the *index* subprogram with parsed commandline *args*.\n \"\"\"\n index_file = Config.instance().get_expanded(\"core\", \"index_file\")\n\n # just print the file location?\n if args.location:\n print(index_file)\n return\n\n # just remove the index file?\n if args.remove:\n if os.path.exists(index_file):\n os.remove(index_file)\n print(\"removed index file {}\".format(index_file))\n return\n\n # get modules to lookup\n lookup = [m.strip() for m in Config.instance().keys(\"modules\")]\n if args.modules:\n lookup += args.modules\n\n print(\"loading tasks from {} module(s)\".format(len(lookup)))\n\n # loop through modules, import everything to load tasks\n for modid in lookup:\n if not modid:\n continue\n\n if args.verbose:\n sys.stdout.write(\"loading module '{}'\".format(modid))\n\n try:\n import_module(modid)\n except Exception as e:\n if not args.verbose:\n print(\"Error in module '{}': {}\".format(colored(modid, \"red\"), str(e)))\n else:\n print(\"\\n\\nError in module '{}':\".format(colored(modid, \"red\")))\n traceback.print_exc()\n continue\n\n if args.verbose:\n print(\", {}\".format(colored(\"done\", style=\"bright\")))\n\n # determine tasks to write into the index file\n seen_families = []\n task_classes = []\n lookup = [Task]\n while lookup:\n cls = lookup.pop(0)\n lookup.extend(cls.__subclasses__())\n\n # skip already seen task families\n if cls.task_family in seen_families:\n continue\n seen_families.append(cls.task_family)\n\n # skip when explicitly excluded\n if cls.exclude_index:\n continue\n\n # skip external tasks\n is_external_task = issubclass(cls, ExternalTask)\n if args.no_externals and is_external_task:\n continue\n\n # skip non-external tasks without run implementation\n run_is_callable = callable(getattr(cls, \"run\", None))\n run_is_abstract = getattr(cls.run, \"__isabstractmethod__\", False)\n if not is_external_task and (not run_is_callable or run_is_abstract):\n continue\n\n task_classes.append(cls)\n\n def get_task_params(cls):\n params = []\n for attr in dir(cls):\n member = getattr(cls, attr)\n if isinstance(member, luigi.Parameter):\n exclude = getattr(cls, \"exclude_params_index\", set())\n if not multi_match(attr, exclude, any):\n params.append(attr.replace(\"_\", \"-\"))\n return params\n\n def index_line(cls, params):\n # format: \"module_id:task_family:param param ...\"\n return \"{}:{}:{}\".format(cls.__module__, cls.task_family, \" \".join(params))\n\n stats = OrderedDict()\n\n # write the index file\n if not os.path.exists(os.path.dirname(index_file)):\n os.makedirs(os.path.dirname(index_file))\n\n with open(index_file, \"w\") as f:\n for cls in task_classes:\n # get prams\n params = get_task_params(cls)\n\n # fill stats\n if cls.__module__ not in stats:\n stats[cls.__module__] = []\n stats[cls.__module__].append((cls.task_family, params))\n\n f.write(index_line(cls, params) + \"\\n\")\n\n # print stats\n if args.verbose:\n for mod, data in six.iteritems(stats):\n print(\"\\nmodule '{}', {} task(s):\".format(colored(mod, style=\"bright\"), len(data)))\n for task_family, _ in data:\n print(\" - {}\".format(colored(task_family, \"green\")))\n print(\"\")\n\n print(\"written {} task(s) to index file '{}'\".format(len(task_classes), index_file))"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a list of luigi - internal configuration parameters.", "response": "def get_global_parameters(config_names=(\"core\", \"scheduler\", \"worker\", \"retcode\")):\n \"\"\"\n Returns a list of global, luigi-internal configuration parameters. Each list item is a 4-tuple\n containing the configuration class, the parameter instance, the parameter name, and the full\n parameter name in the cli. When *config_names* is set, it should be a list of configuration\n class names that are exclusively taken into account.\n \"\"\"\n params = []\n for cls in luigi.task.Config.__subclasses__():\n if config_names and cls.__name__ not in config_names:\n continue\n\n for attr in dir(cls):\n param = getattr(cls, attr)\n if not isinstance(param, luigi.Parameter):\n continue\n\n full_name = attr.replace(\"_\", \"-\")\n if getattr(cls, \"use_cmdline_section\", True):\n full_name = \"{}-{}\".format(cls.__name__.replace(\"_\", \"-\"), full_name)\n\n params.append((cls, param, attr, full_name))\n\n return params"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a relative path to a framgment file or directory.", "response": "def rel_path(anchor, *paths):\n \"\"\"\n Returns a path made of framgment *paths* relativ to an *anchor* path. When *anchor* is a file,\n its absolute directory is used instead.\n \"\"\"\n anchor = os.path.abspath(os.path.expandvars(os.path.expanduser(anchor)))\n if os.path.exists(anchor) and os.path.isfile(anchor):\n anchor = os.path.dirname(anchor)\n return os.path.normpath(os.path.join(anchor, *paths))"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef law_home_path(*paths):\n home = os.getenv(\"LAW_HOME\", \"$HOME/.law\")\n home = os.path.expandvars(os.path.expanduser(home))\n return os.path.normpath(os.path.join(home, *paths))", "response": "Returns the path to the law home directory that defaults to the user s home directory optionally with paths."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef print_err(*args, **kwargs):\n sys.stderr.write(\" \".join(str(arg) for arg in args) + \"\\n\")\n if kwargs.get(\"flush\", False):\n sys.stderr.flush()", "response": "print_err(*args, flush=False)\n Same as *print*, but outputs to stderr. If *flush* is *True*, stderr is flushed after printing."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef abort(msg=None, exitcode=1):\n if msg is not None:\n if exitcode in (None, 0):\n print(msg)\n else:\n print_err(msg)\n sys.exit(exitcode)", "response": "Aborts the process with an exitcode."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef colored(msg, color=None, background=None, style=None, force=False):\n try:\n if not force and not os.isatty(sys.stdout.fileno()):\n return msg\n except:\n return msg\n\n color = colors.get(color, colors[\"default\"])\n background = backgrounds.get(background, backgrounds[\"default\"])\n\n if not isinstance(style, (tuple, list, set)):\n style = (style,)\n style = \";\".join(str(styles.get(s, styles[\"default\"])) for s in style)\n\n return \"\\033[{};{};{}m{}\\033[0m\".format(style, background, color, msg)", "response": "Return a colored version of a string."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef multi_match(name, patterns, mode=any, regex=False):\n if not regex:\n return mode(fnmatch.fnmatch(name, pattern) for pattern in patterns)\n else:\n return mode(re.match(pattern, name) for pattern in patterns)", "response": "Returns True if name matches multiple patterns."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning whether the given object is iterable lazily.", "response": "def is_lazy_iterable(obj):\n \"\"\"\n Returns whether *obj* is iterable lazily, such as generators, range objects, etc.\n \"\"\"\n return isinstance(obj,\n (types.GeneratorType, collections.MappingView, six.moves.range, enumerate))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nconverting an object to a list and returns it.", "response": "def make_list(obj, cast=True):\n \"\"\"\n Converts an object *obj* to a list and returns it. Objects of types *tuple* and *set* are\n converted if *cast* is *True*. Otherwise, and for all other types, *obj* is put in a new list.\n \"\"\"\n if isinstance(obj, list):\n return list(obj)\n elif is_lazy_iterable(obj):\n return list(obj)\n elif isinstance(obj, (tuple, set)) and cast:\n return list(obj)\n else:\n return [obj]"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef make_tuple(obj, cast=True):\n if isinstance(obj, tuple):\n return tuple(obj)\n elif is_lazy_iterable(obj):\n return tuple(obj)\n elif isinstance(obj, (list, set)) and cast:\n return tuple(obj)\n else:\n return (obj,)", "response": "Converts an object to a tuple and returns it."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nflattens and returns a complex structured object *struct*.", "response": "def flatten(struct):\n \"\"\"\n Flattens and returns a complex structured object *struct*.\n \"\"\"\n if isinstance(struct, dict):\n return flatten(struct.values())\n elif isinstance(struct, (list, tuple, set)) or is_lazy_iterable(struct):\n objs = []\n for obj in struct:\n objs.extend(flatten(obj))\n return objs\n else:\n return [struct]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef merge_dicts(*dicts, **kwargs):\n # get or infer the class\n cls = kwargs.get(\"cls\", None)\n if cls is None:\n for d in dicts:\n if isinstance(d, dict):\n cls = d.__class__\n break\n else:\n raise TypeError(\"cannot infer cls as none of the passed objects is of type dict\")\n\n # start merging\n merged_dict = cls()\n for d in dicts:\n if isinstance(d, dict):\n merged_dict.update(d)\n\n return merged_dict", "response": "Takes multiple dicts and returns a single merged dict."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef which(prog):\n executable = lambda path: os.path.isfile(path) and os.access(path, os.X_OK)\n\n # prog can also be a path\n dirname, _ = os.path.split(prog)\n if dirname:\n if executable(prog):\n return prog\n elif \"PATH\" in os.environ:\n for search_path in os.environ[\"PATH\"].split(os.pathsep):\n path = os.path.join(search_path.strip('\"'), prog)\n if executable(path):\n return path\n\n return None", "response": "Pythonic which implementation. Returns the path to an executable by searching in the PATH environment variable."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef map_struct(func, struct, cls=None, map_dict=True, map_list=True, map_tuple=False,\n map_set=False):\n \"\"\"\n Applies a function *func* to each value of a complex structured object *struct* and returns the\n output in the same structure. Example:\n\n .. code-block:: python\n\n struct = {\"foo\": [123, 456], \"bar\": [{\"1\": 1}, {\"2\": 2}]}\n def times_two(i):\n return i * 2\n\n map_struct(struct, times_two)\n # -> {\"foo\": [246, 912], \"bar\": [{\"1\": 2}, {\"2\": 4}]}\n\n When *cls* is not *None*, it exclusively defines the class of objects that *func* is applied on.\n All other objects are unchanged. *map_dict*, *map_list*, *map_tuple* and *map_set* configure if\n objects of the respective types are traversed or mapped. The can be booleans or integer values\n that define the depth of that setting in the struct.\n \"\"\"\n # interpret generators and views as lists\n if is_lazy_iterable(struct):\n struct = list(struct)\n\n valid_types = tuple()\n if map_dict:\n valid_types += (dict,)\n if isinstance(map_dict, int) and not isinstance(map_dict, bool):\n map_dict -= 1\n if map_list:\n valid_types += (list,)\n if isinstance(map_list, int) and not isinstance(map_list, bool):\n map_list -= 1\n if map_tuple:\n valid_types += (tuple,)\n if isinstance(map_tuple, int) and not isinstance(map_tuple, bool):\n map_tuple -= 1\n if map_set:\n valid_types += (set,)\n if isinstance(map_set, int) and not isinstance(map_set, bool):\n map_set -= 1\n\n # is an instance of cls?\n if cls is not None and isinstance(struct, cls):\n return func(struct)\n\n # traverse?\n elif isinstance(struct, valid_types):\n # create a new struct, treat tuples as lists for itertative item appending\n new_struct = struct.__class__() if not isinstance(struct, tuple) else []\n\n # create type-dependent generator and addition callback\n if isinstance(struct, (list, tuple)):\n gen = enumerate(struct)\n add = lambda _, value: new_struct.append(value)\n elif isinstance(struct, set):\n gen = enumerate(struct)\n add = lambda _, value: new_struct.add(value)\n else: # dict\n gen = six.iteritems(struct)\n add = lambda key, value: new_struct.__setitem__(key, value)\n\n # recursively fill the new struct\n for key, value in gen:\n value = map_struct(func, value, cls=cls, map_dict=map_dict, map_list=map_list,\n map_tuple=map_tuple, map_set=map_set)\n add(key, value)\n\n # convert tuples\n if isinstance(struct, tuple):\n new_struct = struct.__class__(new_struct)\n\n return new_struct\n\n # when cls is set, just return\n elif cls is not None:\n return struct\n\n # apply func\n else:\n return func(struct)", "response": "Applies a function func to each value of a complex structured object struct and returns the resulting structure."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nmasking a complex structured object struct with a mask and returns the remaining values.", "response": "def mask_struct(mask, struct, replace=no_value):\n \"\"\"\n Masks a complex structured object *struct* with a *mask* and returns the remaining values. When\n *replace* is set, masked values are replaced with that value instead of being removed. The\n *mask* can have a complex structure as well. Examples:\n\n .. code-block:: python\n\n struct = {\"a\": [1, 2], \"b\": [3, [\"foo\", \"bar\"]]}\n\n # simple example\n mask_struct({\"a\": [False, True], \"b\": False}, struct)\n # => {\"a\": [2]}\n\n # omitting mask information results in keeping values\n mask_struct({\"a\": [False, True]}, struct)\n # => {\"a\": [2], \"b\": [3, [\"foo\", \"bar\"]]}\n \"\"\"\n # interpret lazy iterables lists\n if is_lazy_iterable(struct):\n struct = list(struct)\n\n # when mask is a bool, or struct is not a dict or sequence, apply the mask immediately\n if isinstance(mask, bool) or not isinstance(struct, (list, tuple, dict)):\n return struct if mask else replace\n\n # check list and tuple types\n elif isinstance(struct, (list, tuple)) and isinstance(mask, (list, tuple)):\n new_struct = []\n for i, val in enumerate(struct):\n if i >= len(mask):\n new_struct.append(val)\n else:\n repl = replace\n if isinstance(replace, (list, tuple)) and len(replace) > i:\n repl = replace[i]\n val = mask_struct(mask[i], val, replace=repl)\n if val != no_value:\n new_struct.append(val)\n\n return struct.__class__(new_struct) if new_struct else replace\n\n # check dict types\n elif isinstance(struct, dict) and isinstance(mask, dict):\n new_struct = struct.__class__()\n for key, val in six.iteritems(struct):\n if key not in mask:\n new_struct[key] = val\n else:\n repl = replace\n if isinstance(replace, dict) and key in replace:\n repl = replace[key]\n val = mask_struct(mask[key], val, replace=repl)\n if val != no_value:\n new_struct[key] = val\n return new_struct or replace\n\n # when this point is reached, mask and struct have incompatible types\n raise TypeError(\"mask and struct must have the same type, got '{}' and '{}'\".format(type(mask),\n type(struct)))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef readable_popen(*args, **kwargs):\n # force pipes\n kwargs[\"stdout\"] = subprocess.PIPE\n kwargs[\"stderr\"] = subprocess.STDOUT\n\n p = subprocess.Popen(*args, **kwargs)\n\n for line in iter(lambda: p.stdout.readline(), \"\"):\n if six.PY3:\n line = line.decode(\"utf-8\")\n yield line.rstrip()\n\n # yield the process itself in the end\n p.communicate()\n yield p", "response": "A generator that yields the output of a single process."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating a hash based on a given algorithm.", "response": "def create_hash(inp, l=10, algo=\"sha256\"):\n \"\"\"\n Takes an input *inp* and creates a hash based on an algorithm *algo*. For valid algorithms, see\n python's hashlib. *l* corresponds to the maximum length of the returned hash. Internally, the\n string representation of *inp* is used.\n \"\"\"\n return getattr(hashlib, algo)(six.b(str(inp))).hexdigest()[:l]"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef copy_no_perm(src, dst):\n shutil.copy(src, dst)\n perm = os.stat(dst).st_mode\n shutil.copystat(src, dst)\n os.chmod(dst, perm)", "response": "Copy a file from src to dst except for permission bits."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning True if a file located at path is owned by the user with uid.", "response": "def user_owns_file(path, uid=None):\n \"\"\"\n Returns whether a file located at *path* is owned by the user with *uid*. When *uid* is *None*,\n the user id of the current process is used.\n \"\"\"\n if uid is None:\n uid = os.getuid()\n path = os.path.expandvars(os.path.expanduser(path))\n return os.stat(path).st_uid == uid"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef human_bytes(n, unit=None):\n if n == 0:\n idx = 0\n elif unit:\n idx = byte_units.index(unit)\n else:\n idx = int(math.floor(math.log(abs(n), 1024)))\n idx = min(idx, len(byte_units))\n return n / 1024. ** idx, byte_units[idx]", "response": "Takes a number of bytes n assigns the best matching unit and returns the respective number\n and unit string in a tuple."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef human_time_diff(*args, **kwargs):\n secs = float(datetime.timedelta(*args, **kwargs).total_seconds())\n parts = []\n for unit, mul in time_units:\n if secs / mul >= 1 or mul == 1:\n if mul > 1:\n n = int(math.floor(secs / mul))\n secs -= n * mul\n else:\n n = round(secs, 1)\n parts.append(\"{} {}{}\".format(n, unit, \"\" if n == 1 else \"s\"))\n return \", \".join(parts)", "response": "Returns a human readable time difference."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn whether the exception e was raised due to an already existing file or directory.", "response": "def is_file_exists_error(e):\n \"\"\"\n Returns whether the exception *e* was raised due to an already existing file or directory.\n \"\"\"\n if six.PY3:\n return isinstance(e, FileExistsError) # noqa: F821\n else:\n return isinstance(e, OSError) and e.errno == 17"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nsend an email from sender to recipient with subject and content.", "response": "def send_mail(recipient, sender, subject=\"\", content=\"\", smtp_host=\"127.0.0.1\", smtp_port=25):\n \"\"\"\n Lightweight mail functionality. Sends an mail from *sender* to *recipient* with *subject* and\n *content*. *smtp_host* and *smtp_port* are forwarded to the ``smtplib.SMTP`` constructor. *True*\n is returned on success, *False* otherwise.\n \"\"\"\n try:\n server = smtplib.SMTP(smtp_host, smtp_port)\n except Exception as e:\n logger = logging.getLogger(__name__)\n logger.warning(\"cannot create SMTP server: {}\".format(e))\n return False\n\n header = \"From: {}\\r\\nTo: {}\\r\\nSubject: {}\\r\\n\\r\\n\".format(sender, recipient, subject)\n server.sendmail(sender, recipient, header + content)\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nflushes all registered consumer streams.", "response": "def _flush(self):\n \"\"\"\n Flushes all registered consumer streams.\n \"\"\"\n for consumer in self.consumers:\n if not getattr(consumer, \"closed\", False):\n consumer.flush()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _write(self, *args, **kwargs):\n for consumer in self.consumers:\n consumer.write(*args, **kwargs)", "response": "Write to all registered consumer streams passing args and kwargs."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nwrite to the consumer stream when filter_fn evaluates to True", "response": "def _write(self, *args, **kwargs):\n \"\"\"\n Writes to the consumer stream when *filter_fn* evaluates to *True*, passing *args* and\n *kwargs*.\n \"\"\"\n if self.filter_fn(*args, **kwargs):\n self.stream.write(*args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_default(self, section, option, default=None, type=None, expandvars=False,\n expanduser=False):\n \"\"\"\n Returns the config value defined by *section* and *option*. When either the section or the\n option does not exist, the *default* value is returned instead. When *type* is set, it must\n be either `\"str\"`, `\"int\"`, `\"float\"`, or `\"boolean\"`. When *expandvars* is *True*,\n environment variables are expanded. When *expanduser* is *True*, user variables are\n expanded as well.\n \"\"\"\n if self.has_section(section) and self.has_option(section, option):\n value = self.get(section, option)\n if isinstance(value, six.string_types):\n if expandvars:\n value = os.path.expandvars(value)\n if expanduser:\n value = os.path.expanduser(value)\n return value if not type else self._get_type_converter(type)(value)\n else:\n return default", "response": "Returns the default value for the option in section."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_expanded(self, *args, **kwargs):\n kwargs.setdefault(\"expandvars\", True)\n kwargs.setdefault(\"expanduser\", True)\n return self.get_default(*args, **kwargs)", "response": "Same as get_default but expandvars and expanduser arguments are set to True by default."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nupdate the current configuration with new data given as a dictionary.", "response": "def update(self, data, overwrite=None, overwrite_sections=True, overwrite_options=True):\n \"\"\"\n Updates the currently stored configuration with new *data*, given as a dictionary. When\n *overwrite_sections* is *False*, sections in *data* that are already present in the current\n config are skipped. When *overwrite_options* is *False*, existing options are not\n overwritten. When *overwrite* is not *None*, both *overwrite_sections* and\n *overwrite_options* are set to its value.\n \"\"\"\n if overwrite is not None:\n overwrite_sections = overwrite\n overwrite_options = overwrite\n\n for section, _data in six.iteritems(data):\n if not self.has_section(section):\n self.add_section(section)\n elif not overwrite_sections:\n continue\n\n for option, value in six.iteritems(_data):\n if overwrite_options or not self.has_option(section, option):\n self.set(section, option, str(value))"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nupdating the current configc with the config found in filename. All args and kwargs are passed to the update method.", "response": "def include(self, filename, *args, **kwargs):\n \"\"\"\n Updates the current configc with the config found in *filename*. All *args* and *kwargs* are\n forwarded to :py:meth:`update`.\n \"\"\"\n p = self.__class__(filename, skip_defaults=True, skip_fallbacks=True)\n self.update(p._sections, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns all keys of a section in a list.", "response": "def keys(self, section, prefix=None):\n \"\"\"\n Returns all keys of a *section* in a list. When *prefix* is set, only keys starting with\n that prefix are returned\n \"\"\"\n return [key for key, _ in self.items(section) if (not prefix or key.startswith(prefix))]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef sync_luigi_config(self, push=True, pull=True, expand=True):\n prefix = \"luigi_\"\n lparser = luigi.configuration.LuigiConfigParser.instance()\n\n if push:\n for section in self.sections():\n if not section.startswith(prefix):\n continue\n lsection = section[len(prefix):]\n\n if not lparser.has_section(lsection):\n lparser.add_section(lsection)\n\n for option in self.options(section):\n if not lparser.has_option(lsection, option):\n if expand:\n value = self.get_expanded(section, option)\n else:\n value = self.get(section, option)\n lparser.set(lsection, option, value)\n\n if pull:\n for lsection in lparser.sections():\n section = prefix + lsection\n\n if not self.has_section(section):\n self.add_section(section)\n\n for option, value in lparser.items(lsection):\n self.set(section, option, value)", "response": "Synchronizes the configuration with the luigi configuration parser."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef notify_telegram(title, content, token=None, chat=None, mention_user=None, **kwargs):\n # test import\n import telegram # noqa: F401\n\n cfg = Config.instance()\n\n # get default token and chat\n if not token:\n token = cfg.get_expanded(\"notifications\", \"telegram_token\")\n if not chat:\n chat = cfg.get_expanded(\"notifications\", \"telegram_chat\")\n\n if not token or not chat:\n logger.warning(\"cannot send Telegram notification, token ({}) or chat ({}) empty\".format(\n token, chat))\n return False\n\n # append the user to mention to the title\n # unless explicitly set to empty string\n mention_text = \"\"\n if mention_user is None:\n mention_user = cfg.get_expanded(\"notifications\", \"telegram_mention_user\")\n if mention_user:\n mention_text = \" (@{})\".format(mention_user)\n\n # request data for the API call\n request = {\n \"parse_mode\": \"Markdown\",\n }\n\n # standard or attachment content?\n if isinstance(content, six.string_types):\n request[\"text\"] = \"{}{}\\n\\n{}\".format(title, mention_text, content)\n else:\n # content is a dict, add some formatting\n request[\"text\"] = \"{}{}\\n\\n\".format(title, mention_text)\n\n for key, value in content.items():\n request[\"text\"] += \"_{}_: {}\\n\".format(key, value)\n\n # extend by arbitrary kwargs\n request.update(kwargs)\n\n # threaded, non-blocking API communication\n thread = threading.Thread(target=_notify_telegram, args=(token, chat, request))\n thread.start()\n\n return True", "response": "Send a Telegram notification and returns True on success."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef complete(self):\n if self.task.local_workflow_require_branches:\n return self._has_run\n else:\n return super(LocalWorkflowProxy, self).complete()", "response": "Returns True if the task has completed successfully."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef run(self):\n if not self._has_yielded and not self.task.local_workflow_require_branches:\n self._has_yielded = True\n\n yield list(self.task.get_branch_tasks().values())\n\n self._has_run = True", "response": "A generator that yields all branch - level task - related information."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef factory(**default_opts):\n def wrapper(decorator):\n @functools.wraps(decorator)\n def wrapper(fn=None, **opts):\n _opts = default_opts.copy()\n _opts.update(opts)\n\n def wrapper(fn):\n @functools.wraps(fn)\n def wrapper(*args, **kwargs):\n return decorator(fn, _opts, *args, **kwargs)\n return wrapper\n\n return wrapper if fn is None else wrapper(fn)\n return wrapper\n return wrapper", "response": "A function factory for tasks run methods."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef log(fn, opts, task, *args, **kwargs):\n _task = get_task(task)\n log = get_param(_task.log_file, _task.default_log_file)\n\n if log == \"-\" or not log:\n return fn(task, *args, **kwargs)\n else:\n # use the local target functionality to create the parent directory\n LocalFileTarget(log).parent.touch()\n with open_compat(log, \"a\", 1) as f:\n tee = TeeStream(f, sys.__stdout__)\n sys.stdout = tee\n sys.stderr = tee\n try:\n ret = fn(task, *args, **kwargs)\n except:\n traceback.print_exc(file=tee)\n raise\n finally:\n sys.stdout = sys.__stdout__\n sys.stderr = sys.__stderr__\n tee.flush()\n return ret", "response": "A decorator that wraps a bound method of a task and redirects output of both stdout and stderr to the file\n defined by the tasks s log_file parameter or the default_log_file attribute."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nwraps a bound method of a task and logs its execution time in a human readable format. Logs in info mode. When *publish_message* is *True*, the duration is also published as a task message to the scheduler.", "response": "def timeit(fn, opts, task, *args, **kwargs):\n \"\"\"\n Wraps a bound method of a task and logs its execution time in a human readable format. Logs in\n info mode. When *publish_message* is *True*, the duration is also published as a task message to\n the scheduler.\n \"\"\"\n start_time = time.time()\n try:\n return fn(task, *args, **kwargs)\n finally:\n duration = human_time_diff(seconds=round(time.time() - start_time, 1))\n\n # log\n timeit_logger = logger.getChild(\"timeit\")\n timeit_logger.info(\"runtime of {}: {}\".format(task.task_id, duration))\n\n # optionally publish a task message to the scheduler\n if opts[\"publish_message\"] and callable(getattr(task, \"publish_message\", None)):\n task.publish_message(\"runtime: {}\".format(duration))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_voms_proxy_user():\n out = _voms_proxy_info([\"--identity\"])[1].strip()\n try:\n return re.match(r\".*\\/CN\\=([^\\/]+).*\", out.strip()).group(1)\n except:\n raise Exception(\"no valid identity found in voms proxy: {}\".format(out))", "response": "Returns the owner of the voms proxy."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchecks if a voms proxy exists and returns True otherwise.", "response": "def check_voms_proxy_validity(log=False):\n \"\"\"\n Returns *True* when a valid voms proxy exists, *False* otherwise. When *log* is *True*, a\n warning will be logged.\n \"\"\"\n valid = _voms_proxy_info([\"--exists\"], silent=True)[0] == 0\n if log and not valid:\n logger.warning(\"no valid voms proxy found\")\n return valid"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nrenews the voms proxy using a password and virtual organization name vo and a default lifetime of 8 days.", "response": "def renew_voms_proxy(passwd=\"\", vo=None, lifetime=\"196:00\"):\n \"\"\"\n Renews the voms proxy using a password *passwd*, an optional virtual organization name *vo*, and\n a default *lifetime* of 8 days. The password is written to a temporary file first and piped into\n the renewal commad to ensure it is not visible in the process list.\n \"\"\"\n with tmp_file() as (_, tmp):\n with open(tmp, \"w\") as f:\n f.write(passwd)\n\n cmd = \"cat '{}' | voms-proxy-init --valid '{}'\".format(tmp, lifetime)\n if vo:\n cmd += \" -voms '{}'\".format(vo)\n code, out, _ = interruptable_popen(cmd, shell=True, executable=\"/bin/bash\",\n stdout=subprocess.PIPE, stderr=subprocess.STDOUT)\n if code != 0:\n raise Exception(\"proxy renewal failed: {}\".format(out))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef delegate_voms_proxy_glite(endpoint, stdout=None, stderr=None, cache=True):\n # get the proxy file\n proxy_file = get_voms_proxy_file()\n if not os.path.exists(proxy_file):\n raise Exception(\"proxy file '{}' does not exist\".format(proxy_file))\n\n if cache:\n if isinstance(cache, six.string_types):\n cache_file = cache\n else:\n cache_file = proxy_file + \"_delegation_cache.json\"\n\n def remove_cache():\n try:\n if os.path.exists(cache_file):\n os.remove(cache_file)\n except OSError:\n pass\n\n # create the hash of the proxy file content\n with open(proxy_file, \"r\") as f:\n proxy_hash = create_hash(f.read())\n\n # already delegated?\n cache_data = {}\n if os.path.exists(cache_file):\n with open(cache_file, \"r\") as f:\n try:\n cache_data = json.load(f)\n except:\n remove_cache()\n\n # is the hash up-to-date?\n if cache_data.get(\"hash\") != proxy_hash:\n remove_cache()\n cache_data = {}\n\n # proxy already delegated to that endpoint?\n elif endpoint in cache_data.get(\"ids\", []):\n return str(cache_data[\"ids\"][endpoint])\n\n # do the actual delegation\n delegation_id = uuid.uuid4().hex\n cmd = [\"glite-ce-delegate-proxy\", \"-e\", endpoint, delegation_id]\n code = interruptable_popen(cmd, stdout=stdout, stderr=stderr)[0]\n if code != 0:\n raise Exception(\"glite proxy delegation to endpoint {} failed\".format(endpoint))\n\n if cache:\n # write the id back to the delegation file\n cache_data[\"hash\"] = proxy_hash\n cache_data.setdefault(\"ids\", {})[endpoint] = delegation_id\n with open(cache_file, \"w\") as f:\n json.dump(cache_data, f, indent=4)\n os.chmod(cache_file, 0o0600)\n\n return delegation_id", "response": "Delegates the voms proxy to an endpoint using glite - ce - delegate - proxy."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the formatter class whose name attribute is name.", "response": "def get_formatter(name, silent=False):\n \"\"\"\n Returns the formatter class whose name attribute is *name*. When no class could be found and\n *silent* is *True*, *None* is returned. Otherwise, an exception is raised.\n \"\"\"\n formatter = FormatterRegister.formatters.get(name)\n if formatter or silent:\n return formatter\n else:\n raise Exception(\"cannot find formatter '{}'\".format(name))"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nfind the list of formatters that would accept the given file.", "response": "def find_formatters(path, silent=True):\n \"\"\"\n Returns a list of formatter classes which would accept the file given by *path*. When no classes\n could be found and *silent* is *True*, an empty list is returned. Otherwise, an exception is\n raised.\n \"\"\"\n formatters = [f for f in six.itervalues(FormatterRegister.formatters) if f.accepts(path)]\n if formatters or silent:\n return formatters\n else:\n raise Exception(\"cannot find formatter for path '{}'\".format(path))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef find_formatter(name, path):\n if name == AUTO_FORMATTER:\n return find_formatters(path, silent=False)[0]\n else:\n return get_formatter(name, silent=False)", "response": "Returns the formatter class that accepts path."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nencodes a list into a base64 encoded string via base64 encoding.", "response": "def encode_list(cls, value):\n \"\"\"\n Encodes a list *value* into a string via base64 encoding.\n \"\"\"\n encoded = base64.b64encode(six.b(\" \".join(str(v) for v in value) or \"-\"))\n return encoded.decode(\"utf-8\") if six.PY3 else encoded"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_args(self):\n return [\n self.task_cls.__module__,\n self.task_cls.__name__,\n self.encode_list(self.task_params),\n self.encode_list(self.branches),\n self.encode_bool(self.auto_retry),\n self.encode_list(self.dashboard_data),\n ]", "response": "Returns the list of encoded job arguments."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef load(*packages):\n for pkg in flatten(packages):\n if pkg in loaded_packages:\n logger.debug(\"skip contrib package '{}', already loaded\".format(pkg))\n continue\n loaded_packages.append(pkg)\n\n mod = __import__(\"law.contrib.{}\".format(pkg), globals(), locals(), [pkg])\n logger.debug(\"loaded contrib package '{}'\".format(pkg))\n\n for attr in mod.__all__:\n if hasattr(law, attr):\n logger.info(\"cannot register 'law.contrib.{0}.{1}' to 'law.{1}', \"\n \"already exists\".format(pkg, attr))\n else:\n setattr(law, attr, getattr(mod, attr))\n law.__all__.append(attr)\n logger.debug(\"registered 'law.contrib.{0}.{1}' to 'law.{1}'\".format(pkg, attr))", "response": "Loads a list of contrib packages and adds members exposed in law. all."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef notify_mail(title, message, recipient=None, sender=None, smtp_host=None, smtp_port=None,\n **kwargs):\n \"\"\"\n Mail notification method taking a *title* and a string *message*. *recipient*, *sender*,\n *smtp_host* and *smtp_port* default to the configuration values in the [notifications] section.\n \"\"\"\n cfg = Config.instance()\n\n if not recipient:\n recipient = cfg.get_expanded(\"notifications\", \"mail_recipient\")\n if not sender:\n sender = cfg.get_expanded(\"notifications\", \"mail_sender\")\n if not smtp_host:\n smtp_host = cfg.get_expanded(\"notifications\", \"mail_smtp_host\")\n if not smtp_port:\n smtp_port = cfg.get_expanded(\"notifications\", \"mail_smtp_port\")\n\n if not recipient or not sender:\n logger.warning(\"cannot send mail notification, recipient ({}) or sender ({}) empty\".format(\n recipient, sender))\n return False\n\n return send_mail(recipient, sender, title, message, smtp_host=smtp_host, smtp_port=smtp_port)", "response": "Send a mail notification to the specified recipient and sender."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nrun all patches. This function ensures that a second invocation has no effect.", "response": "def patch_all():\n \"\"\"\n Runs all patches. This function ensures that a second invocation has no effect.\n \"\"\"\n global _patched\n\n if _patched:\n return\n _patched = True\n\n patch_default_retcodes()\n patch_worker_run_task()\n patch_worker_factory()\n patch_keepalive_run()\n patch_cmdline_parser()\n\n logger.debug(\"applied law-specific luigi patches\")"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef patch_default_retcodes():\n import luigi.retcodes\n\n retcode = luigi.retcodes.retcode\n\n retcode.already_running._default = 10\n retcode.missing_data._default = 20\n retcode.not_run._default = 30\n retcode.task_failed._default = 40\n retcode.scheduling_error._default = 50\n retcode.unhandled_exception._default = 60", "response": "Patch luigi. retcodes. retcode with default luigi return codes."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef patch_worker_run_task():\n _run_task = luigi.worker.Worker._run_task\n\n def run_task(self, task_id):\n task = self._scheduled_tasks[task_id]\n\n task._worker_id = self._id\n task._worker_task = self._first_task\n\n try:\n _run_task(self, task_id)\n finally:\n task._worker_id = None\n task._worker_task = None\n\n # make worker disposable when sandboxed\n if os.getenv(\"LAW_SANDBOX_SWITCHED\") == \"1\":\n self._start_phasing_out()\n\n luigi.worker.Worker._run_task = run_task", "response": "Patches the luigi. worker. Worker. _run_task method to store the worker id and the id of its\n first task in the task."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef patch_worker_factory():\n def create_worker(self, scheduler, worker_processes, assistant=False):\n worker = luigi.worker.Worker(scheduler=scheduler, worker_processes=worker_processes,\n assistant=assistant, worker_id=os.getenv(\"LAW_SANDBOX_WORKER_ID\"))\n worker._first_task = os.getenv(\"LAW_SANDBOX_WORKER_TASK\")\n return worker\n\n luigi.interface._WorkerSchedulerFactory.create_worker = create_worker", "response": "Patches the luigi. interface. _WorkerSchedulerFactory to include sandboxing information when creating a worker instance."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef patch_keepalive_run():\n _run = luigi.worker.KeepAliveThread.run\n\n def run(self):\n # do not run the keep-alive loop when sandboxed\n if os.getenv(\"LAW_SANDBOX_SWITCHED\") == \"1\":\n self.stop()\n else:\n _run(self)\n\n luigi.worker.KeepAliveThread.run = run", "response": "Patches the luigi. worker. KeepAliveThread. run to immediately stop the keep - alive thread when running within a sandbox."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef patch_cmdline_parser():\n # store original functions\n _init = luigi.cmdline_parser.CmdlineParser.__init__\n\n # patch init\n def __init__(self, cmdline_args):\n _init(self, cmdline_args)\n self.cmdline_args = cmdline_args\n\n luigi.cmdline_parser.CmdlineParser.__init__ = __init__", "response": "Patches the luigi. cmdline_parser. CmdlineParser to store the original command line arguments\n for later processing in the Law. config. Config."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef notify_slack(title, content, attachment_color=\"#4bb543\", short_threshold=40, token=None,\n channel=None, mention_user=None, **kwargs):\n \"\"\"\n Sends a slack notification and returns *True* on success. The communication with the slack API\n might have some delays and is therefore handled by a thread. The format of the notification\n depends on *content*. If it is a string, a simple text notification is sent. Otherwise, it\n should be a dictionary whose fields are used to build a message attachment with two-column\n formatting.\n \"\"\"\n # test import\n import slackclient # noqa: F401\n\n cfg = Config.instance()\n\n # get default token and channel\n if not token:\n token = cfg.get_expanded(\"notifications\", \"slack_token\")\n if not channel:\n channel = cfg.get_expanded(\"notifications\", \"slack_channel\")\n\n if not token or not channel:\n logger.warning(\"cannot send Slack notification, token ({}) or channel ({}) empty\".format(\n token, channel))\n return False\n\n # append the user to mention to the title\n # unless explicitly set to empty string\n mention_text = \"\"\n if mention_user is None:\n mention_user = cfg.get_expanded(\"notifications\", \"slack_mention_user\")\n if mention_user:\n mention_text = \" (@{})\".format(mention_user)\n\n # request data for the API call\n request = {\n \"channel\": channel,\n \"as_user\": True,\n \"parse\": \"full\",\n }\n\n # standard or attachment content?\n if isinstance(content, six.string_types):\n request[\"text\"] = \"{}{}\\n\\n{}\".format(title, mention_text, content)\n else:\n # content is a dict, send its data as an attachment\n request[\"text\"] = \"{} {}\".format(title, mention_text)\n request[\"attachments\"] = at = {\n \"color\": attachment_color,\n \"fields\": [],\n \"fallback\": \"{}{}\\n\\n\".format(title, mention_text),\n }\n\n # fill the attachment fields and extend the fallback\n for key, value in content.items():\n at[\"fields\"].append({\n \"title\": key,\n \"value\": value,\n \"short\": len(value) <= short_threshold,\n })\n at[\"fallback\"] += \"_{}_: {}\\n\".format(key, value)\n\n # extend by arbitrary kwargs\n request.update(kwargs)\n\n # threaded, non-blocking API communication\n thread = threading.Thread(target=_notify_slack, args=(token, request))\n thread.start()\n\n return True", "response": "Send a Slack notification to the specified slack site."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef maybe_wait(func):\n def wrapper(self, *args, **kwargs):\n if self.slow:\n time.sleep(random.randint(5, 15))\n return func(self, *args, **kwargs)\n\n return wrapper", "response": "Wrapper around run method that reads the slow flag to decide whether to wait some seconds."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef parse_query_output(cls, out):\n query_data = {}\n\n for line in out.strip().split(\"\\n\"):\n parts = line.split()\n if len(parts) < 6:\n continue\n\n job_id = parts[0]\n status_flag = parts[2]\n\n # map the status\n status = cls.map_status(status_flag)\n\n # save the result\n query_data[job_id] = cls.job_status_dict(job_id=job_id, status=status)\n\n return query_data", "response": "Parse the output of a query from the server into a dictionary."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef setup_logging():\n global console_handler\n\n # make sure logging is setup only once\n if console_handler:\n return\n\n # set the handler of the law root logger\n console_handler = logging.StreamHandler()\n console_handler.setFormatter(LogFormatter())\n logging.getLogger(\"law\").addHandler(console_handler)\n\n # set levels for all loggers\n for name, level in Config.instance().items(\"logging\"):\n level = level.upper()\n if hasattr(logging, level):\n logger = logging.getLogger(name)\n logger.setLevel(getattr(logging, level))\n logger.debug(\"registered logger with level '{}'\".format(level))", "response": "Sets up the internal logging mechanism."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nexecute the config subprogram with parsed commandline args.", "response": "def execute(args):\n \"\"\"\n Executes the *config* subprogram with parsed commandline *args*.\n \"\"\"\n # just print the file location?\n if args.location:\n print(Config.instance().config_file)\n return\n\n # every option below requires the name to be set\n if not args.name:\n abort(\"please give the name of the config in the format
[.