def bin(v): """ Similar to __builtin__.bin but works on ndarrays. Useful in queries for converting flags to bit strings. FIXME: The current implementation is painfully slow. """ import __builtin__ if not isinstance(v, np.ndarray): return __builtin__.bin(v) # Must be some kind of integer assert v.dtype.kind in ['i', 'u'] # Create compatible string array l = v.dtype.itemsize*8 ss = np.empty(v.shape, dtype=('a', v.dtype.itemsize*9)) s = ss.reshape(-1) for i, n in enumerate(v.flat): c = __builtin__.bin(n)[2:] c = '0'*(l-len(c)) + c ll = [ c[k:k+8] for k in xrange(0, l, 8) ] s[i] = ','.join(ll) return ss
def bin(v): """ Similar to __builtin__.bin but works on ndarrays. Useful in queries for converting flags to bit strings. FIXME: The current implementation is painfully slow. """ import __builtin__ if not isinstance(v, np.ndarray): return __builtin__.bin(v) # Must be some kind of integer assert v.dtype.kind in ['i', 'u'] # Create compatible string array l = v.dtype.itemsize * 8 ss = np.empty(v.shape, dtype=('a', v.dtype.itemsize * 9)) s = ss.reshape(-1) for i, n in enumerate(v.flat): c = __builtin__.bin(n)[2:] c = '0' * (l - len(c)) + c ll = [c[k:k + 8] for k in xrange(0, l, 8)] s[i] = ','.join(ll) return ss
def bin(n): """Return the equivalent to Python 2.6's bin function. Examples ======== >>> from sympy.core.compatibility import bin >>> bin(-123) '-0b1111011' >>> bin(0) # this is the only time a 0 will be to the right of 'b' '0b0' See Also ======== sympy.physics.quantum.shor.arr Modified from http://code.activestate.com/recipes/576847/ """ # ========================================================= # create hex of int, remove '0x'. now for each hex char, # look up binary string, append in list and join at the end. # ========================================================= if n < 0: return '-%s' % bin(-n) return '0b%s' % (''.join( [_hexDict[hstr] for hstr in hex(n)[2:].lower()]).lstrip('0') or '0')
def bin(n): """Return the equivalent to Python 2.6's bin function. Examples ======== >>> from sympy.core.compatibility import bin >>> bin(-123) '-0b1111011' >>> bin(0) # this is the only time a 0 will be to the right of 'b' '0b0' See Also ======== sympy.physics.quantum.shor.arr Modified from http://code.activestate.com/recipes/576847/ """ # ========================================================= # create hex of int, remove '0x'. now for each hex char, # look up binary string, append in list and join at the end. # ========================================================= if n < 0: return '-%s' % bin(-n) return '0b%s' % (''.join([_hexDict[hstr] for hstr in hex(n)[2:].lower() ]).lstrip('0') or '0')
def bin(obj): if isinstance(obj, Element): return obj.bin() elif hasattr(obj, '__bin__') and callable(obj.__bin__): return obj.__bin__() else: # will certainly raise return builtins.bin(obj)
def bin(x): """ bin(number) -> string Stringifies an int or long in base 2. """ if x < 0: return '-' + bin(-x) out = [] if x == 0: out.append('0') while x > 0: out.append('01'[x & 1]) x >>= 1 pass return '0b' + ''.join(reversed(out))
def bin(x): """ bin(number) -> string Stringifies an int or long in base 2. """ if x < 0: return "-" + bin(-x) out = [] if x == 0: out.append("0") while x > 0: out.append("01"[x & 1]) x >>= 1 pass return "0b" + "".join(reversed(out))
def bin(): """Return UUID as binary""" return __builtin__.bin(int())[2:]
def main(): # open an output file input_path = sys.argv[FILE_POS] if isdir(input_path): input_list = [ input_path + "/" +f for f in listdir(input_path) if (isfile(join(input_path, f))) and (f.endswith(".asm")) ] else: input_list = [input_path] for input_file in input_list: index = input_file.index(".") output_file = open(input_file[:index] + ".hack", "w") # parse a new line code = Code() symbol_table = SymbolTable() counter_address = FIRST_ADDRESS_RAM counter_rom = FIRST_ADDRESS_ROM # first pass parser_first_pass = Parser(input_file) while parser_first_pass.has_more_commands(): command = parser_first_pass.advance() parse_type = parser_first_pass.command_type() if parse_type == L_COMMAND: if not symbol_table.contains(command[1:-1]): symbol_table.add_entry(command[1:-1], counter_rom) else: counter_rom+=1 # second pass parser_second_pass = Parser(input_file) while parser_second_pass.has_more_commands(): command = parser_second_pass.advance() line_to_hack = "" parse_type = parser_second_pass.command_type() # translate the line to an A Command if parse_type == A_COMMAND: if command[1:].isdigit(): address = command[1:] else: if symbol_table.contains(command[1:]): address = symbol_table.get_address(command[1:]) else: symbol_table.add_entry(command[1:], counter_address) address = counter_address counter_address += 1 binary_repr = str(bin(int(address))[2:].zfill(15)) line_to_hack = A_PREFIX + binary_repr # translate the line to a C Command if parse_type == C_COMMAND: # C command comp comp_type = parser_second_pass.comp() code_comp = code.comp(comp_type) # C command dest dest_type = parser_second_pass.dest() code_dest = code.dest(dest_type) # C command jump jump_type = parser_second_pass.jump() code_jump = code.jump(jump_type) if ("<" in comp_type) or (">" in comp_type): line_to_hack = C_PREFIX_SPE + code_comp + code_dest + code_jump else: line_to_hack = C_PREFIX_REG + code_comp + code_dest + code_jump if parse_type == L_COMMAND: continue # write the line to the output file output_file.write(line_to_hack + "\n") output_file.close()
def subWorkerThread(self, bytechar): self.cmdDecodeArray.append(bytechar) self.cmdDecodeString = ''.join(str(x) for x in self.cmdDecodeArray) self.cmdDecodeArray = [] self.cmdDecodeArray.append(self.cmdDecodeString) #print self.cmdDecodeString cookee_cmd = varlisting.COOKEE offset_cookee = self.cmdDecodeString.find(cookee_cmd) offset_beacon1 = self.cmdDecodeString.find('aa55') offset_beacon2 = self.cmdDecodeString.find('bb66') #if offset_cookee != -1: # self.ackString = self.cmdDecodeString[offset_cookee + 10:] # #print self.ackString # if len(self.ackString) == 14: # if fnmatch.fnmatch(self.ackString, 'aaaa??????aaaa'): # self.ackPkt = self.ackString # print self.ackPkt # print 'ACK Pkt Received' # self.cmdDecodeArray = [] # self.cmdDecodeString = '' # return # else: # print 'Error Occurred' # self.cmdDecodeArray = [] # self.cmdDecodeString = '' # return # if len(self.ackString) == 12: # if fnmatch.fnmatch(self.ackString, 'bbbb????bbbb'): # self.ackPkt = self.ackString # print self.ackPkt # print 'ACK Pkt Received' # self.cmdDecodeArray = [] # self.cmdDecodeString = '' # return # else: # print 'Error Occurred' # self.cmdDecodeArray = [] # self.cmdDecodeString = '' # return ### ### Check for incoming bytes with 'aa55' value ### if offset_beacon1 != -1: self.statdlString = self.cmdDecodeString[offset_beacon1:] if len(self.statdlString) == 216: tempMarks = ['?' for i in xrange(208)] tempMarks = ''.join(str(x) for x in tempMarks) if fnmatch.fnmatch(self.statdlString, 'aa55' + '%s' %tempMarks + 'aa55'): self.pkt1 = self.statdlString pktfrmtd = " ".join(self.pkt1[i:i+2] for i in range(0, len(self.pkt1), 2)) pktNumDec = int(self.pkt1[4:8], 16) timeStamp = self.pkt1[8:16] timeStampDec = '%i' %int(timeStamp, 16) formattedTime = strftime("%Y-%m-%d %H:%M:%S", localtime(int(timeStampDec))) # Used to convert little endian hex to integers #print struct.unpack("<h", "\xee\xee")[0] #str = "\\xee\\xee" #print struct.unpack("<h", str)[0] #flip bytes, little endian to big endian #data[::-1] EPS_DATA = self.pkt1[20:152] EPS_DATA = " ".join(EPS_DATA[i:i+2] for i in range(0, len(EPS_DATA), 2)) RADIO_DATA = self.pkt1[156:192] RADIO_DATAARRAY = [RADIO_DATA[0:4], RADIO_DATA[4:8], RADIO_DATA[8:16], RADIO_DATA[16:20], RADIO_DATA[20:28], RADIO_DATA[28:36]] RADIO_DATAARRAY[0] = " ".join(RADIO_DATAARRAY[0][i:i+2] for i in range(0, len(RADIO_DATAARRAY[0]), 2)) RADIO_DATAARRAY[0] = RADIO_DATAARRAY[0].split(' ') RADIO_DATAARRAY[0] = RADIO_DATAARRAY[0][::-1] RADIO_DATAARRAY[0] = "".join(RADIO_DATAARRAY[0]) RADIO_DATAARRAY[0] = int(RADIO_DATAARRAY[0], 16) RADIO_DATAARRAY[1] = " ".join(RADIO_DATAARRAY[1][i:i+2] for i in range(0, len(RADIO_DATAARRAY[1]), 2)) RADIO_DATAARRAY[1] = RADIO_DATAARRAY[1].split(' ') RADIO_DATAARRAY[1] = RADIO_DATAARRAY[1][::-1] RADIO_DATAARRAY[1] = "".join(RADIO_DATAARRAY[1]) RADIO_DATAARRAY[1] = int(RADIO_DATAARRAY[1], 16) RADIO_DATAARRAY[2] = " ".join(RADIO_DATAARRAY[2][i:i+2] for i in range(0, len(RADIO_DATAARRAY[2]), 2)) RADIO_DATAARRAY[2] = RADIO_DATAARRAY[2].split(' ') RADIO_DATAARRAY[2] = RADIO_DATAARRAY[2][::-1] RADIO_DATAARRAY[2] = "".join(RADIO_DATAARRAY[2]) RADIO_DATAARRAY[2] = int(RADIO_DATAARRAY[2], 16) RADIO_DATAARRAY[3] = " ".join(RADIO_DATAARRAY[3][i:i+2] for i in range(0, len(RADIO_DATAARRAY[3]), 2)) RADIO_DATAARRAY[3] = RADIO_DATAARRAY[3].split(' ') RADIO_DATAARRAY[3] = RADIO_DATAARRAY[3][::-1] RADIO_DATAARRAY[3] = "".join(RADIO_DATAARRAY[3]) RADIO_DATAARRAY[3] = int(RADIO_DATAARRAY[3], 16) RADIO_DATAARRAY[4] = " ".join(RADIO_DATAARRAY[4][i:i+2] for i in range(0, len(RADIO_DATAARRAY[4]), 2)) RADIO_DATAARRAY[4] = RADIO_DATAARRAY[4].split(' ') RADIO_DATAARRAY[4] = RADIO_DATAARRAY[4][::-1] RADIO_DATAARRAY[4] = "".join(RADIO_DATAARRAY[4]) RADIO_DATAARRAY[4] = int(RADIO_DATAARRAY[4], 16) RADIO_DATAARRAY[5] = " ".join(RADIO_DATAARRAY[5][i:i+2] for i in range(0, len(RADIO_DATAARRAY[5]), 2)) RADIO_DATAARRAY[5] = RADIO_DATAARRAY[5].split(' ') RADIO_DATAARRAY[5] = RADIO_DATAARRAY[5][::-1] RADIO_DATAARRAY[5] = "".join(RADIO_DATAARRAY[5]) RADIO_DATAARRAY[5] = int(RADIO_DATAARRAY[5], 16) #print RADIO_DATAARRAY RADIO_DATA = " ".join(RADIO_DATA[i:i+2] for i in range(0, len(RADIO_DATA), 2)) ANTS_DATA = self.pkt1[196:212] ANTS_DATAARRAY = [ANTS_DATA[0:4], ANTS_DATA[4:8], ANTS_DATA[8:12], ANTS_DATA[12:16]] ANTS_DATAARRAY[0] = " ".join(ANTS_DATAARRAY[0][i:i+2] for i in range(0, len(ANTS_DATAARRAY[0]), 2)) ANTS_DATAARRAY[0] = ANTS_DATAARRAY[0].split(' ') ANTS_DATAARRAY[0] = ANTS_DATAARRAY[0][::-1] ANTS_DATAARRAY[0] = "".join(ANTS_DATAARRAY[0]) ANTS_DATAARRAY[1] = " ".join(ANTS_DATAARRAY[1][i:i+2] for i in range(0, len(ANTS_DATAARRAY[1]), 2)) ANTS_DATAARRAY[1] = ANTS_DATAARRAY[1].split(' ') ANTS_DATAARRAY[1] = ANTS_DATAARRAY[1][::-1] ANTS_DATAARRAY[1] = "".join(ANTS_DATAARRAY[1]) #ANTS_DATAARRAY[2] = " ".join(ANTS_DATAARRAY[2][i:i+2] for i in range(0, len(ANTS_DATAARRAY[2]), 2)) #ANTS_DATAARRAY[2] = ANTS_DATAARRAY[2].split(' ') #ANTS_DATAARRAY[2] = ANTS_DATAARRAY[2][::-1] #ANTS_DATAARRAY[2] = "".join(ANTS_DATAARRAY[2]) ANTS_DATAARRAY[2] = __builtin__.bin(int(ANTS_DATAARRAY[2], 16))[2:].zfill(len(ANTS_DATAARRAY[2])*4) #ANTS_DATAARRAY[3] = " ".join(ANTS_DATAARRAY[3][i:i+2] for i in range(0, len(ANTS_DATAARRAY[3]), 2)) #ANTS_DATAARRAY[3] = ANTS_DATAARRAY[3].split(' ') #ANTS_DATAARRAY[3] = ANTS_DATAARRAY[3][::-1] #ANTS_DATAARRAY[3] = "".join(ANTS_DATAARRAY[3]) ANTS_DATAARRAY[3] = __builtin__.bin(int(ANTS_DATAARRAY[3], 16))[2:].zfill(len(ANTS_DATAARRAY[3])*4) #print ANTS_DATAARRAY ANTS_DATA = " ".join(ANTS_DATA[i:i+2] for i in range(0, len(ANTS_DATA), 2)) self.tlmSeparated = self.tlmDecode(self.pkt1) self.emit(SIGNAL('latestPkt(PyQt_PyObject)'), [pktNumDec, timeStampDec, self.tlmSeparated, RADIO_DATAARRAY, ANTS_DATAARRAY]) self.emit(SIGNAL('latestPktLog(PyQt_PyObject)'), [0, pktfrmtd, pktNumDec, timeStampDec, self.tlmSeparated, EPS_DATA, RADIO_DATA, ANTS_DATA]) self.emit(SIGNAL('latestPktUpdate(PyQt_PyObject)'), [0, pktfrmtd, pktNumDec, timeStampDec, self.tlmSeparated, EPS_DATA, RADIO_DATA, ANTS_DATA]) self.cmdDecodeArray = [] self.cmdDecodeString = '' ### ### Check for incoming bytes with 'bb66' value ### if offset_beacon2 != -1: self.statdlString = self.cmdDecodeString[offset_beacon2:] #print self.statdlString if len(self.statdlString) == 80: tempMarks = ['?' for i in xrange(72)] tempMarks = ''.join(str(x) for x in tempMarks) if fnmatch.fnmatch(self.statdlString, 'bb66' + '%s' %tempMarks + 'bb66'): self.pkt2 = self.statdlString pktfrmtd = " ".join(self.pkt2[i:i+2] for i in range(0, len(self.pkt2), 2)) pktNumDec = int(self.pkt2[4:8], 16) timeStamp = self.pkt2[8:16] timeStampDec = '%i' %int(timeStamp, 16) formattedTime = strftime("%Y-%m-%d %H:%M:%S", localtime(int(timeStampDec))) DAVE_DATA = self.pkt2[20:24] DAVE_DATA = " ".join(DAVE_DATA[i:i+2] for i in range(0, len(DAVE_DATA), 2)) UTEP_DATA = self.pkt2[28:76] UTEP_DATA = " ".join(UTEP_DATA[i:i+2] for i in range(0, len(UTEP_DATA), 2)) self.tlmSeparated = '' #self.emit(SIGNAL('latestPkt(PyQt_PyObject)'), [pktNumDec, timeStampDec, formattedTime, self.tlmSeparated]) self.emit(SIGNAL('latestPktLog(PyQt_PyObject)'), [1, pktfrmtd, pktNumDec, timeStampDec, self.tlmSeparated, DAVE_DATA, UTEP_DATA]) self.emit(SIGNAL('latestPktUpdate(PyQt_PyObject)'), [1, pktfrmtd, pktNumDec, timeStampDec, self.tlmSeparated, DAVE_DATA, UTEP_DATA]) self.cmdDecodeArray = [] self.cmdDecodeString = ''