class Context(object): def __init__(self, instructions): self.registers = Registers() self.flags = Flags() self.instructions = instructions self.heap = Memory(size=40, mode=HEAP, start_address=0x0000) self.stack = Memory(size=40, mode=STACK, start_address=0xFFFF) self.registers.set(SP, 0xFFFF) def run(self): """ initialize the context and execute the first instruction """ self.registers.reset() def step(self): """ execute the next instruction whose address in the EIP value :return: """ self.registers.tick() self.flags.tick() self.heap.tick() self.stack.tick() next_address = self.registers.get(IP).value if next_address < len(self.instructions): next_instruction = self.instructions[next_address] next_instruction.execute(self) self.registers.set(IP, next_address + 1) return True else: return False
def __init__(self, memorymap): self.mem = memorymap self.iodevices = list() self.interrupts = True self.immode = 0 self.a = 0x00 self.b = 0x00 self.c = 0x00 self.d = 0x00 self.e = 0x00 self.f = Flags() self.h = 0x00 self.l = 0x00 self.aa = 0x00 self.ba = 0x00 self.ca = 0x00 self.da = 0x00 self.ea = 0x00 self.fa = Flags() self.ha = 0x00 self.la = 0x00 self.ix = 0x0000 self.iy = 0x0000 self.pc = 0x0000 self.sp = 0x0000 self.i = 0x00 self.r = 0x00
def __init__( self ): self.completer = ycm_core.ClangCompleter() self.completer.EnableThreading() self.contents_holder = [] self.filename_holder = [] self.last_prepared_diagnostics = [] self.parse_future = None self.flags = Flags() self.diagnostic_store = None
def __init__(self, instructions): self.registers = Registers() self.flags = Flags() self.instructions = instructions self.heap = Memory(size=40, mode=HEAP, start_address=0x0000) self.stack = Memory(size=40, mode=STACK, start_address=0xFFFF) self.registers.set(SP, 0xFFFF)
def build_config(): args = ArgsParser().parse_args() flags = Flags(args.config).get() log_file_path = os.path.join(flags.Global.save_model_dir, time.strftime('%Y%m%d_%H%M%S') + '.log') os.makedirs(flags.Global.save_model_dir, exist_ok=True) logger = initial_logger(log_file_path) return flags
def render_library_button(self, bot, query, chat_id): """ Отображение библиотеки пользователя Args: bot (:class:`telegram.Bot`): хэндлер бота query (:class:`telegram.CallbackQuery`): возвращаемое значение от inline клавиатуры chat_id (:obj:`int`) id пользователя """ postgre = DataBase() flags[chat_id] = Flags() flags[chat_id].set_library(postgre.GetUserLibrary(chat_id)) library = flags[chat_id].get_library() if library == {}: bot.edit_message_text( text=Settings.EmptyLibraryTxt, chat_id=chat_id, message_id=query.message.message_id, ) return text_out = "http://imdb.com/title/tt%s" % library[0][1] series_info = postgre.GetSeriesInfo(library[0][1]) if (series_info): text_out += "\n *Season* - %s *Episode* - %s" % ( series_info['season'], series_info['episode']) reply_markup = library_navigate_markup(len(library)) bot.edit_message_text(text=text_out, chat_id=chat_id, message_id=query.message.message_id, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN) logger.info("rendered library list for %s " % chat_id)
def main(): Flags.PARSER.add_argument('--woreda_latlon_path', type=str, required=True, help='path to woreda_info.csv') Flags.PARSER.add_argument('--output_data_path', type=str, required=True, help='path folder with weather data csv') Flags.PARSER.add_argument('--start_date', type=str, required=True, help='start date "YYYY-MM-DD"') Flags.PARSER.add_argument('--end_date', type=str, required=True, help='end date "YYYY-MM-DD"') Flags.PARSER.add_argument('--batch_type', type=str, required=True, help='single or batch') Flags.InitArgs() batch_type = Flags.ARGS.batch_type if batch_type not in {'single', 'batch'}: raise ValueError('batch_type needs to be either "single" or "batch"') get_weather(Flags.ARGS.woreda_latlon_path, \ Flags.ARGS.output_data_path, \ Flags.ARGS.start_date, \ Flags.ARGS.end_date, \ batch_type)
def create_data(self): # Write variables variables = ProjectVariables(self.data) variables.define_variable() files = ProjectFiles(self.data) files.write_variables() variables.define_project() variables.define_target() # Write Macro macros = Macro() macros.write_macro(self.data) # Write Output Variables variables.write_output() # Write Include Directories depends = Dependencies(self.data) if self.data['includes']: depends.write_include_dir() else: send('Include Directories is not set.', '') # Write Dependencies depends.write_dependencies() # Add additional code or not if self.data['additional_code'] is not None: files.add_additional_code(self.data['additional_code']) # Write Flags all_flags = Flags(self.data) all_flags.write_flags() # Write and add Files files.write_files() files.add_artefact() # Link with other dependencies depends.link_dependencies() # Close CMake file self.data['cmake'].close()
def changeState(c, message): header = message.header if header.flagSet(Flag.F): c.ackPackages(message.header.aNum + 1) h = Header(c.streamID, c.sNum, c.aNum, Flags([Flag.A]), c.window) m = Message(h) c.send(m, True) c.resetFinTimer() return Time_Wait return Closed
class Gui(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.flags = Flags(self.ui) self.video = openCV(cv2.VideoCapture(0), self.flags, self.ui.thresh_val) self._timer = QtCore.QTimer(self) self._timer.timeout.connect(self.play) self._timer.start(20) self.update() self.ui.thresh_trigger.stateChanged.connect(self.thresh_trigger) def thresh_trigger(self): self.video.thresh_change_trigger = not self.video.thresh_change_trigger def play(self): # try: self.video.captureNextFrame() self.ui.videoFrame.setPixmap(self.video.convertFrame()) self.ui.videoFrame.setScaledContents(True) self.flags.checkFlags() # except Exception, e: # print "play(): ", e def keyPressEvent(self, event): if event.isAutoRepeat(): return self.key = QtCore.QString() if Qt.Key_A <= event.key() <= Qt.Key_Z: self.key = event.text() self.flags.isLatch_button = True self.key = event.text() self.ui.latch.setStyleSheet('background-color :rgbrgb(0, 131, 0);') def keyReleaseEvent(self, event): if event.isAutoRepeat(): return self.flags.isLatch_button = False self.ui.latch.setStyleSheet('background-color :rgb(190, 56, 56) ;')
def changeState(c: Connection, message: Message): header = message.header if header.flagSet(Flag.A): if header.sNum == c.aNum: c.ackPackages(header.aNum) #if it fits if header.dataLength == 0: if (c.lastReceivedAck == header.aNum): c.duplicateAcks += 1 else: c.lastReceivedAck = header.aNum c.duplicateAcks = 0 if c.sendData(): return Fin_Wait_1 if c.duplicateAcks > 2: c.resendInFlight() if c.sendData(): return Fin_Wait_1 else: #add data c.lastReceivedAck = header.aNum c.received += message.payload[:header.dataLength] c.aNum = message.header.sNum + message.header.dataLength # update num if c.sendData(True): return Fin_Wait_1 else: #duplicate Ack back - package did not fit h = Header(c.streamID, c.sNum, c.aNum, Flags([Flag.A]), c.window) c.send(Message(h), True) return Established if header.flagSet(Flag.F): h = Header(c.streamID, c.sNum, c.aNum, Flags([Flag.F]), c.window) message = Message(h) c.send(message) c.sNum += 1 return Close_Wait return Established
def main(): app = QCoreApplication([]) flags = Flags() proc = subprocess.Popen([sys.executable, "subprogram.py"], stdout=subprocess.PIPE, shell=False) thread = SubProgramWatcher(proc, flags, 55) thread.finished.connect(app.exit) thread.finished.connect(cleanup) thread.start() sys.exit(app.exec_())
class PicklingFlagsCreatedAndStoredAtClassScope( test_base.PicklingSuccessTestBase): DynamicallyCreatedInnerFlags = Flags( 'DynamicallyCreatedInnerFlags', 'f0 f1 f2 f3', module=__name__, qualname= 'PicklingFlagsCreatedAndStoredAtClassScope.DynamicallyCreatedInnerFlags', ) FlagsClass = DynamicallyCreatedInnerFlags
def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.flags = Flags(self.ui) self.video = openCV(cv2.VideoCapture(0), self.flags, self.ui.thresh_val) self._timer = QtCore.QTimer(self) self._timer.timeout.connect(self.play) self._timer.start(20) self.update() self.ui.thresh_trigger.stateChanged.connect(self.thresh_trigger)
def changeState(c, message): header = message.header if header.flagSet(Flag.S): c.window = header.window c.aNum += 1 h = Header(c.streamID, c.sNum, c.aNum, Flags([Flag.A, Flag.S]), c.window) message = Message(h) c.send(message) c.sNum += 1 return Syn_Rcvd return Listen
def search_button(self, bot, query, chat_id): """ Кнопка поиска Args: bot (:class:`telegram.Bot`): хэндлер бота query (:class:`telegram.CallbackQuery`): возвращаемое значение от inline клавиатуры chat_id (:obj:`int`) id пользователя """ bot.edit_message_text(text="Please send me the name of the title: ", chat_id=chat_id, message_id=query.message.message_id) flags[chat_id] = Flags() flags[chat_id].set_flag_search(True)
def changeState(c, message): header = message.header if header.flagSet(Flag.S) and header.flagSet(Flag.A): c.aNum = header.aNum c.sNum += 1 h = Header(c.streamID, c.sNum, c.aNum, Flags([Flag.A]), c.window) c.send(Message(h), True) c.ackPackages(header.aNum) if c.sendData(): return Fin_Wait_1 return Established return Syn_Sent
def inference(config_file, image_file): """ Run text recognition network on an image file. """ # Get config FLAGS = Flags(config_file).get() out_charset = load_charset(FLAGS.charset) num_classes = len(out_charset) net = get_network(FLAGS, out_charset) if FLAGS.use_rgb: num_channel = 3 mode = cv2.IMREAD_COLOR else: num_channel = 1 mode = cv2.IMREAD_GRAYSCALE # Input node image = tf.placeholder(tf.uint8, shape=[None, None, num_channel], name='input_node') # Network proc_image = net.preprocess_image(image, is_train=False) proc_image = tf.expand_dims(proc_image, axis=0) proc_image.set_shape( [None, FLAGS.resize_hw.height, FLAGS.resize_hw.width, num_channel]) logits, sequence_length = net.get_logits(proc_image, is_train=False, label=None) prediction, log_prob = net.get_prediction(logits, sequence_length) prediction = tf.sparse_to_dense(sparse_indices=prediction.indices, sparse_values=prediction.values, output_shape=prediction.dense_shape, default_value=num_classes, name='output_node') # Restore restore_model = get_init_trained() sess = tf.Session() restore_model(sess, FLAGS.eval.model_path) # Run img = cv2.imread(image_file, mode) img = np.reshape(img, [img.shape[0], img.shape[1], num_channel]) predicted = sess.run(prediction, feed_dict={image: img}) string = get_string(predicted[0], out_charset) string = adjust_string(string, FLAGS.eval.lowercase, FLAGS.eval.alphanumeric) print(string) return string
class PicklingFailure(TestCase): DynamicInnerFlags = Flags('DynamicInnerFlags', 'f0 f1 f2 f3', module=__name__) def test_pickling_fails_with_dynamically_created_class_without_module_name( self): self.assertRaises(pickle.PicklingError, pickle.dumps, DynamicModuleScopeFlagsWithoutModuleName.f0) def test_pickling_fails_with_dynamically_created_inner_class_without_qualname( self): self.assertRaises(pickle.PicklingError, pickle.dumps, self.DynamicInnerFlags.f0)
def run_flags(): from flags import Flags f = Flags.from_tiles_attr(tiles, 'is_open') o = Flags(size) for i in range(10**4): f[(10,10)] f[(11,11)] = False f & o f | o f ^ o ~f print f
def test_subclassing_fails_if_at_least_one_flags_base_class_isnt_abstract(self): class NonAbstract(Flags): f0 = () class Abstract1(Flags): pass class Abstract2(Flags): pass with self.assertRaisesRegex( RuntimeError, re.escape("You can't subclass 'NonAbstract' because it has already defined flag members")): NonAbstract('MyFlags', 'flag1 flag2') with self.assertRaisesRegex( RuntimeError, re.escape("You can't subclass 'NonAbstract' because it has already defined flag members")): NonAbstract('MyFlags', 'flag1 flag2', mixins=[Abstract1]) with self.assertRaisesRegex( RuntimeError, re.escape("You can't subclass 'NonAbstract' because it has already defined flag members")): Flags('MyFlags', 'flag1 flag2', mixins=[NonAbstract]) with self.assertRaisesRegex( RuntimeError, re.escape("You can't subclass 'NonAbstract' because it has already defined flag members")): Flags('MyFlags', 'flag1 flag2', mixins=[Abstract1, NonAbstract]) with self.assertRaisesRegex( RuntimeError, re.escape("You can't subclass 'NonAbstract' because it has already defined flag members")): Flags('MyFlags', 'flag1 flag2', mixins=[Abstract1, NonAbstract, Abstract2]) with self.assertRaisesRegex( RuntimeError, re.escape("You can't subclass 'NonAbstract' because it has already defined flag members")): Abstract1('MyFlags', 'flag1 flag2', mixins=[NonAbstract]) with self.assertRaisesRegex( RuntimeError, re.escape("You can't subclass 'NonAbstract' because it has already defined flag members")): Abstract1('MyFlags', 'flag1 flag2', mixins=[Abstract2, NonAbstract])
def test_get_z(self): # testing using 1 everywhere # thus z = sin 1 + cos 1 = 1.3817732906760363 T = 2 N = 3 new_flags = {'n_mc': 5, 'n_rff': 1, 'd': 1} myflags = Flags(T, None, None, None) myflags.set_flags(new_flags) t = np.matrix([[1], [1]]) y = np.matrix([[1, 1, 1], [1, 1, 1]]) sess = tf.InteractiveSession() self.myLatnet = ScalableLatnet(flags=myflags, s=None, d=1, t=t, y=y, logger=None, session=sess) t_tf = tf.cast(t, self.myLatnet.FLOAT) some_omega = tf.ones( (myflags.get_flag('n_mc'), myflags.get_flag('n_rff'), myflags.get_flag('d')), dtype=self.myLatnet.FLOAT) some_gamma = tf.ones( (myflags.get_flag('n_mc'), N, 2 * myflags.get_flag('n_rff')), dtype=self.myLatnet.FLOAT) z = self.myLatnet.get_z(gamma=some_gamma, omega=some_omega, t=t_tf, log_variance=tf.log( tf.constant(1, dtype=self.myLatnet.FLOAT))) assert z.shape[0] == myflags.get_flag('n_mc') assert z.shape[1] == N assert z.shape[2] == T assert tf.reduce_max(z).eval() == 1.3817732906760363 assert tf.reduce_min(z).eval() == 1.3817732906760363
def sendData(self, ackIfNone=False): if self.toSend is not None: for _ in range(self.window - len(self.inFlight)): if self.toSend.empty(): if len(self.inFlight) == 0: h = Header(self.streamID, self.sNum, self.aNum, Flags([Flag.F]), self.window) m = Message(h) self.send(m) self.sNum += 1 return True return False payload = self.toSend.next(100) h = Header(self.streamID, self.sNum, self.aNum, Flags([Flag.A]), self.window) m = Message(h, payload) self.send(m) self.sNum += len(payload) elif ackIfNone: h = Header(self.streamID, self.sNum, self.aNum, Flags([Flag.A]), self.window) self.send(Message(h), True) return False
def __init__(self): self.__dict__['_internals'] = dict(ax=0, cx=0, dx=0, bx=0, sp=0, bp=0, si=0, di=0, cs=0, ds=0, ss=0, es=0, ip=0, flags=Flags())
def main(): # Load initial config cfgList = {} macroList = {} flags = Flags() loadConfig(flags.dir + 'pulautin.conf', flags) # Parse command line arguments parser = createArgParser() parseArgs(parser, flags) # Specify template directory, check for existence templateDir = 'templates/projects/' + flags.cfgList['TEMPLATE'] + '/' if not os.path.isdir(templateDir): print('ERROR: project template from directory \'' + templateDir + '\' not found') printConfig(flags) return 1 printConfig(flags) # TEMP # Check for configuration errors if flags.cfgList['PROJECT_NAME'] == '': print("Error: Please provide project name (option -p)") return 1 if flags.cfgList['DEST_ROOT'] == '': print("Error: Please provide destination root directory (option -d)") return 1 destDir = flags.cfgList['DEST_ROOT'] + '/' + flags.cfgList[ 'PROJECT_NAME'] + '/' if os.path.isdir(destDir): if flags.force: shutil.rmtree(destDir) else: print('Error: directory \'' + destDir + '\' already exists') return 1 # Copy template to destination directory copyTemplate(templateDir, destDir) # Build the project according to the XML buildProject(destDir, flags) setupRemote(destDir, flags)
def main(argv): printtime = False i = 0 while i < len(argv): if argv[i] == "--jit": jitarg = argv[i + 1] del argv[i:i + 2] print jitarg, argv jit.set_user_param(None, jitarg) if argv[i] == "--time": printtime = True i += 1 patternfilename = argv[1] inputfilename = argv[2] patternfile = open(patternfilename, "r") inputfile = open(inputfilename, "r") pattern = patternfile.read() patternfile.close() inputstring = inputfile.read() inputfile.close() inputstring = inputstring.strip() flags = Flags(optimize_char=True, optimize_testchar=True) if not flags.optimize_choicepoints: jit.set_param( None, "enable_opts", "intbounds:rewrite:virtualize:string:pure:earlyforce:heap") if we_are_translated(): gct1 = rgc.get_stats(rgc.TOTAL_GC_TIME) else: gct1 = 0 t1 = time.time() captures = runbypattern(pattern, inputstring, flags=flags).captures output = processcaptures(captures, inputstring) t2 = time.time() if we_are_translated(): gct2 = rgc.get_stats(rgc.TOTAL_GC_TIME) else: gct2 = 0 print output if printtime: print "time:", t2 - t1 print "gc time:", (gct2 - gct1) / 1000. return 0
def processcaptures(captures, inputstring, flags=Flags()): #returnlist = [] out = rstring.StringBuilder() if flags.debug: print captures while captures.prev is not None: if isinstance(captures, SimpleCapture): size = captures.get_size() index = captures.index newindex = index - size assert newindex >= 0 assert index >= 0 out.append_slice(inputstring, newindex, index) out.append("\n") elif isinstance(captures, PositionCapture): appendee = "POSITION:" + str(captures.index) out.append(appendee + "\n") captures = captures.prev # capturelist return out.build() # returnlist
def test_instantiation_of_abstract_flags_class_fails(self): # A flags class is considered to be "abstract" if it doesn't define any members. with self.assertRaisesRegex(RuntimeError, r"Instantiation of abstract flags class '.+\.Flags' isn't allowed."): Flags() class MyAbstractFlagsClass(Flags): pass with self.assertRaisesRegex(RuntimeError, r"Instantiation of abstract flags class " r"'.+\.MyAbstractFlagsClass' isn't allowed."): MyAbstractFlagsClass() # Subclassing MyAbstractFlagsClass by calling it: MyAbstractFlagsClass_2 = MyAbstractFlagsClass('MyAbstractFlagsClass_2', '') with self.assertRaisesRegex(RuntimeError, r"Instantiation of abstract flags class " r"'.+\.MyAbstractFlagsClass_2' isn't allowed."): MyAbstractFlagsClass_2()
def main(): # Load initial config flags = Flags() loadConfig(flags.dir + 'pulautin.conf', flags) # Parse command line arguments parser = createArgParser() parseArgs(parser, flags) # Check for configuration errors if flags.cfgList['PROJECT_PATH'] == '': print("Error: Please provide project path") return 1 destDir = flags.cfgList['PROJECT_PATH'] + '/' if not os.path.isdir(destDir): print('Error: directory \'' + destDir + '\' does not exist') return 1 buildProject(destDir, flags) printConfig(flags.cfgList, flags)
def parseArguments() -> Tuple[str, List[str], Flags]: """ Process sys.argv to get command, arguments and flags """ # Ignore `sys.argv[0]` and empty args argv: List[str] = [arg for arg in sys.argv[1:] if len(arg)] # From argv, separate flags and arguments args: List[str] = [v for v in argv if v[0] != '-'] flags: List[str] = [v for v in argv if v[0] == '-'] # Fixing args that start with an escaped hyphen for i in range(len(args)): if args[i].startswith(r'\-'): args[i] = args[i][1:] # Ignore r'\' at [0] # Safe equivalent to: `command = args.pop(0)` command: str = args[0] if len(args) else '' args = args[1:] flags_object = Flags(flags) # `flags` parsing can cause termination return (command, args, flags_object)
def delete_title_from_library_button(self, bot, query, chat_id): """ Кнопка для удаления фильма из библиотеки Args: bot (:class:`telegram.Bot`): хэндлер бота query (:class:`telegram.CallbackQuery`): возвращаемое значение от inline клавиатуры chat_id (:obj:`int`) id пользователя """ index = int(query.data.split("_")[1]) data = flags[chat_id].get_library() logger.info("deleting %s" % data[index][1]) postgre = DataBase() postgre.DeleteFilmFromLibrary(chat_id, data[index][1]) flags[chat_id] = Flags() flags[chat_id].set_library(postgre.GetUserLibrary(chat_id)) dic = flags[chat_id].get_library() print(dic) if dic == {}: bot.edit_message_text(text=Settings.EmptyLibraryTxt, chat_id=chat_id, message_id=query.message.message_id) return index = index % len(dic) text_out = "http://imdb.com/title/tt%s" % dic[index][1] series_info = postgre.GetSeriesInfo(dic[index][1]) if (series_info): text_out += "\n *Season* - %s *Episode* - %s" % ( series_info['season'], series_info['episode']) reply_markup = library_navigate_markup(len(dic), index) bot.edit_message_text(text=text_out, chat_id=chat_id, message_id=query.message.message_id, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN)
def runARM(self): """ Execute the program using the ARM instruction set. """ self.flags = Flags() cur_line = 0 while cur_line < len(self.instructions): instr = self.instructions[cur_line] operation, condition, sets_flags = parse_arm_instr(instr.operation) # check for presence of condition and that we DO NOT meet it, in which case we skip to the next instruction if (cur_line in self.labels.values()) or \ (condition == "eq" and (not self.flags.Z)) or \ (condition == "ne" and self.flags.Z ) or \ (condition in ["cs", "hs"] and (not self.flags.C)) or \ (condition in ["cc", "lo"] and self.flags.C ) or \ (condition == "mi" and (not self.flags.N)) or \ (condition == "pl" and self.flags.N ) or \ (condition == "vs" and (not self.flags.V)) or \ (condition == "vc" and self.flags.V ) or \ (condition == "hi" and ((not self.flags.C) or self.flags.Z) ) or \ (condition == "ls" and ( self.flags.C and (not self.flags.Z))) or \ (condition == "ge" and (self.flags.N != self.flags.V)) or \ (condition == "lt" and (self.flags.N == self.flags.V)) or \ (condition == "gt" and ( self.flags.Z or (self.flags.N != self.flags.V))) or \ (condition == "le" and (not self.flags.Z and (self.flags.N == self.flags.V))): cur_line += 1 continue else: # we checked for all of our conditions and either there was none specified or there was one and we met its requirements if operation == "add": self.registers[instr.operand0] = self.registers[instr.operand1] + getval(self.registers, instr.operand2) elif operation == "adc": self.registers[instr.operand0] = self.registers[instr.operand1] + getval(self.registers, instr.operand2) + self.flags.C elif operation == "sub": self.registers[instr.operand0] = self.registers[instr.operand1] - getval(self.registers, instr.operand2) elif operation == "sbc": self.registers[instr.operand0] = self.registers[instr.operand1] - getval(self.registers, instr.operand2) + (self.flags.C - 1) elif operation == "rsb": self.registers[instr.operand0] = getval(self.registers, instr.operand2) - self.registers[instr.operand1] elif operation == "rsc": self.registers[instr.operand0] = getval(self.registers, instr.operand2) - self.registers[instr.operand1] + (self.flags.C - 1) elif operation == "mul": self.registers[instr.operand0] = self.registers[instr.operand1] * getval(self.registers, instr.operand2) elif operation == "div": self.registers[instr.operand0] = self.registers[instr.operand1] // getval(self.registers, instr.operand2) # branching elif operation == "b": cur_line = self.labels[instr.operand0] elif operation == "bl": self.registers[15] = cur_line + 1 cur_line = self.labels[instr.operand0] # comparisons for setting flags elif operation == "cmp": res = self.registers[instr.operand0] - self.registers[instr.operand1] self.flags.update(N = int(res < 0), Z = int(res == 0)) elif operation == "cmn": res = self.registers[instr.operand0] + self.registers[instr.operand1] self.flags.update(N = int(res < 0), Z = int(res == 0), C = int(2**32-1 < res), V = int(2**31-1 < res < 2**32-1)) elif operation == "tst": res = self.registers[instr.operand0] & self.registers[instr.operand1] self.flags.update(Z = int(res == 0)) elif operation == "teq": res = self.registers[instr.operand0] ^ self.registers[instr.operand1] self.flags.update(Z = int(res == 0)) # logical operations elif operation == "and": self.registers[instr.operand0] = self.registers[instr.operand1] & self.registers[instr.operand2] elif operation == "eor": self.registers[instr.operand0] = self.registers[instr.operand1] ^ self.registers[instr.operand2] elif operation == "orr": self.registers[instr.operand0] = self.registers[instr.operand1] | self.registers[instr.operand2] elif operation == "bic": self.registers[instr.operand0] = self.registers[instr.operand1] & (~self.registers[instr.operand2] & 0xFFFFFFFF) # data movement elif operation == "mov": self.registers[instr.operand0] = self.registers[instr.operand1] elif operation == "mvn": self.registers[instr.operand0] = (~self.registers[instr.operand1] & 0xFFFFFFFF) else: raise ValueError("Unrecognized operation: {0}".format(instr.operation)) if sets_flags: N = int(self.registers[instr.operand0] < 0) Z = int(self.registers[instr.operand0] == 0) C = int(2**32-1 < self.registers[instr.operand0]) V = int(2**31-1 < self.registers[instr.operand0] < 2**32-1) self.flags.update(N, Z, C, V) cur_line += 1 return self
#!/usr/bin/python import numpy as np import time from behaviour_exercise import Behaviour_exercise from pynput.keyboard import Key, Listener from flags import Flags from time import time timeStart = 0 timeEnd = 0 distance = 1 f = Flags() behaviour = Behaviour_exercise(f) def set_global(): global timeStart global timeEnd timeStart = 0 timeEnd = 0 ######################### Jumping and Sound Tests ############################## """ print "Kubo says KuBo and jumps" kubo.say('kuuu.mp3') time.sleep(1) kubo.say('bo.mp3') # Frequency = 0 means kubo jumps only once and exits the thread automatically
class Assembler(object): """ The actual assembler, which takes an assembly program and simulates it. """ def __init__(self, program, mode = "MIPS"): super(Assembler, self).__init__() try: text = program.read() except AttributeError: text = program self.mode = mode.upper() self.registers = Registers(self.mode) lines = text.split("\n") lines = clean(lines, self.mode) instrs, data = split_sections(lines) self.memory = Memory() for d in data: self.memory.insert(d) instrs = preprocess(instrs, self.mode) self.labels = label_positions(instrs) self.instructions = [Instruction(instr) for instr in instrs] def __str__(self): """ String representation of the assembler. """ return "{0} Assembler".format(self.mode) def run(self): """ Execute the program's instructions, modifying the given registers. """ if self.mode == "MIPS": return self.runMIPS() elif self.mode == "ARM": return self.runARM() else: raise ValueError("Invalid mode: {0}".format(self.mode)) def runMIPS(self): """ Execute the program using the MIPS instruction set. """ HI, LO = 0, 0 cur_line = 0 while cur_line < len(self.instructions): instr = self.instructions[cur_line] if cur_line in self.labels.values(): pass elif instr.operation in ["add", "addu"]: self.registers[instr.operand0] = self.registers[instr.operand1] + self.registers[instr.operand2] elif instr.operation == "addi": self.registers[instr.operand0] = self.registers[instr.operand1] + getimm(instr.operand2, True) elif instr.operation == "addiu": self.registers[instr.operand0] = self.registers[instr.operand1] + getimm(instr.operand2, False) elif instr.operation == "and": self.registers[instr.operand0] = self.registers[instr.operand1] & self.registers[instr.operand2] elif instr.operation == "andi": self.registers[instr.operand0] = self.registers[instr.operand1] & getimm(instr.operand2, False) elif instr.operation == "beq": if self.registers[instr.operand0] == self.registers[instr.operand1]: cur_line = self.labels[instr.operand2] # jump straight to the label rather than the following instruction because we increment the line counter at the end anyway elif instr.operation == "bgez": if self.registers[instr.operand0] >= 0: cur_line = self.labels[instr.operand1] elif instr.operation == "bgezal": if self.registers[instr.operand0] >= 0: self.registers[31] = cur_line + 1 cur_line = self.labels[instr.operand1] elif instr.operation == "bltz": if self.registers[instr.operand0] < 0: cur_line = self.labels[instr.operand1] elif instr.operation == "bltzal": if self.registers[instr.operand0] < 0: self.registers[31] = cur_line + 1 cur_line = self.labels[instr.operand1] elif instr.operation == "bne": if self.registers[instr.operand0] != self.registers[instr.operand1]: cur_line = self.labels[instr.operand2] elif instr.operation in ["div", "divu"]: LO = self.registers[instr.operand0] // self.registers[instr.operand1] HI = self.registers[instr.operand0] % self.registers[instr.operand1] elif instr.operation == "j": cur_line = self.labels[instr.operand0] elif instr.operation == "jal": self.registers[31] = cur_line + 1 cur_line = self.labels[instr.operand0] elif instr.operation == "jr": cur_line = self.registers[instr.operand0] elif instr.operation == "la": self.registers[instr.operand0] = self.memory.labels[instr.operand1] elif instr.operation in ["lb", "lw"]: outside, inside = parse_address(instr.operand1) address = calcval(outside, self) + calcval(inside, self) self.registers[instr.operand0] = self.memory[address] elif instr.operation == "lui": self.registers[instr.operand0] = getimm(instr.operand1, False) << 16 elif instr.operation == "mfhi": self.registers[instr.operand0] = HI elif instr.operation == "mflo": self.registers[instr.operand0] = LO elif instr.operation in ["mult", "multu"]: LO = self.registers[instr.operand0] * self.registers[instr.operand1] elif instr.operation == "nor": self.registers[instr.operand0] = ~(self.registers[instr.operand1] | self.registers[instr.operand2]) & 0xFFFFFFFF elif instr.operation == "or": self.registers[instr.operand0] = self.registers[instr.operand1] | self.registers[instr.operand2] elif instr.operation == "ori": self.registers[instr.operand0] = self.registers[instr.operand1] | getimm(instr.operand2, False) elif instr.operation == "sb": outside, inside = parse_address(instr.operand1) address = calcval(outside, self) + calcval(inside, self) self.memory[address] = self.registers[instr.operand0] & 0xFF elif instr.operation in ["slt", "sltu"]: self.registers[instr.operand0] = int(self.registers[instr.operand1] < self.registers[instr.operand2]) elif instr.operation == "slti": self.registers[instr.operand0] = int(self.registers[instr.operand1] < getimm(instr.operand2, True)) elif instr.operation == "sltiu": self.registers[instr.operand0] = int(self.registers[instr.operand1] < getimm(instr.operand2, False)) elif instr.operation == "sll": self.registers[instr.operand0] = self.registers[instr.operand1] << getimm(instr.operand2, False) elif instr.operation == "sllv": self.registers[instr.operand0] = self.registers[instr.operand1] << self.registers[instr.operand2] elif instr.operation == "sra": self.registers[instr.operand0] = self.registers[instr.operand1] >> getimm(instr.operand2, True) elif instr.operation == "srl": self.registers[instr.operand0] = self.registers[instr.operand1] >> getimm(instr.operand2, False) elif instr.operation == "srlv": self.registers[instr.operand0] = self.registers[instr.operand1] >> self.registers[instr.operand2] elif instr.operation in ["sub", "subu"]: self.registers[instr.operand0] = self.registers[instr.operand1] - self.registers[instr.operand2] elif instr.operation == "sw": outside, inside = parse_address(instr.operand1) address = calcval(outside, self) + calcval(inside, self) self.memory[address] = self.registers[instr.operand0] elif instr.operation == "syscall": retval = mips_syscall(self.registers[2]) if retval: break elif instr.operation == "xor": self.registers[instr.operand0] = self.registers[instr.operand1] ^ self.registers[instr.operand2] elif instr.operation == "xori": self.registers[instr.operand0] = self.registers[instr.operand1] ^ getimm(instr.operand2, False) elif instr.operation == "break": break else: raise ValueError("Unrecognized operation: {0}".format(instr.operation)) cur_line += 1 return self def runARM(self): """ Execute the program using the ARM instruction set. """ self.flags = Flags() cur_line = 0 while cur_line < len(self.instructions): instr = self.instructions[cur_line] operation, condition, sets_flags = parse_arm_instr(instr.operation) # check for presence of condition and that we DO NOT meet it, in which case we skip to the next instruction if (cur_line in self.labels.values()) or \ (condition == "eq" and (not self.flags.Z)) or \ (condition == "ne" and self.flags.Z ) or \ (condition in ["cs", "hs"] and (not self.flags.C)) or \ (condition in ["cc", "lo"] and self.flags.C ) or \ (condition == "mi" and (not self.flags.N)) or \ (condition == "pl" and self.flags.N ) or \ (condition == "vs" and (not self.flags.V)) or \ (condition == "vc" and self.flags.V ) or \ (condition == "hi" and ((not self.flags.C) or self.flags.Z) ) or \ (condition == "ls" and ( self.flags.C and (not self.flags.Z))) or \ (condition == "ge" and (self.flags.N != self.flags.V)) or \ (condition == "lt" and (self.flags.N == self.flags.V)) or \ (condition == "gt" and ( self.flags.Z or (self.flags.N != self.flags.V))) or \ (condition == "le" and (not self.flags.Z and (self.flags.N == self.flags.V))): cur_line += 1 continue else: # we checked for all of our conditions and either there was none specified or there was one and we met its requirements if operation == "add": self.registers[instr.operand0] = self.registers[instr.operand1] + getval(self.registers, instr.operand2) elif operation == "adc": self.registers[instr.operand0] = self.registers[instr.operand1] + getval(self.registers, instr.operand2) + self.flags.C elif operation == "sub": self.registers[instr.operand0] = self.registers[instr.operand1] - getval(self.registers, instr.operand2) elif operation == "sbc": self.registers[instr.operand0] = self.registers[instr.operand1] - getval(self.registers, instr.operand2) + (self.flags.C - 1) elif operation == "rsb": self.registers[instr.operand0] = getval(self.registers, instr.operand2) - self.registers[instr.operand1] elif operation == "rsc": self.registers[instr.operand0] = getval(self.registers, instr.operand2) - self.registers[instr.operand1] + (self.flags.C - 1) elif operation == "mul": self.registers[instr.operand0] = self.registers[instr.operand1] * getval(self.registers, instr.operand2) elif operation == "div": self.registers[instr.operand0] = self.registers[instr.operand1] // getval(self.registers, instr.operand2) # branching elif operation == "b": cur_line = self.labels[instr.operand0] elif operation == "bl": self.registers[15] = cur_line + 1 cur_line = self.labels[instr.operand0] # comparisons for setting flags elif operation == "cmp": res = self.registers[instr.operand0] - self.registers[instr.operand1] self.flags.update(N = int(res < 0), Z = int(res == 0)) elif operation == "cmn": res = self.registers[instr.operand0] + self.registers[instr.operand1] self.flags.update(N = int(res < 0), Z = int(res == 0), C = int(2**32-1 < res), V = int(2**31-1 < res < 2**32-1)) elif operation == "tst": res = self.registers[instr.operand0] & self.registers[instr.operand1] self.flags.update(Z = int(res == 0)) elif operation == "teq": res = self.registers[instr.operand0] ^ self.registers[instr.operand1] self.flags.update(Z = int(res == 0)) # logical operations elif operation == "and": self.registers[instr.operand0] = self.registers[instr.operand1] & self.registers[instr.operand2] elif operation == "eor": self.registers[instr.operand0] = self.registers[instr.operand1] ^ self.registers[instr.operand2] elif operation == "orr": self.registers[instr.operand0] = self.registers[instr.operand1] | self.registers[instr.operand2] elif operation == "bic": self.registers[instr.operand0] = self.registers[instr.operand1] & (~self.registers[instr.operand2] & 0xFFFFFFFF) # data movement elif operation == "mov": self.registers[instr.operand0] = self.registers[instr.operand1] elif operation == "mvn": self.registers[instr.operand0] = (~self.registers[instr.operand1] & 0xFFFFFFFF) else: raise ValueError("Unrecognized operation: {0}".format(instr.operation)) if sets_flags: N = int(self.registers[instr.operand0] < 0) Z = int(self.registers[instr.operand0] == 0) C = int(2**32-1 < self.registers[instr.operand0]) V = int(2**31-1 < self.registers[instr.operand0] < 2**32-1) self.flags.update(N, Z, C, V) cur_line += 1 return self
def main(config_file): """ Train text recognition network """ # Parse configs FLAGS = Flags(config_file).get() # Set directory, seed, logger model_dir = create_model_dir(FLAGS.model_dir) logger = get_logger(model_dir, 'train') best_model_dir = os.path.join(model_dir, 'best_models') set_seed(FLAGS.seed) # Print configs flag_strs = [ '{}:\t{}'.format(name, value) for name, value in FLAGS._asdict().items() ] log_formatted(logger, '[+] Model configurations', *flag_strs) # Print system environments num_gpus = count_available_gpus() num_cpus = os.cpu_count() mem_size = virtual_memory().available // (1024**3) log_formatted(logger, '[+] System environments', 'The number of gpus : {}'.format(num_gpus), 'The number of cpus : {}'.format(num_cpus), 'Memory Size : {}G'.format(mem_size)) # Get optimizer and network global_step = tf.train.get_or_create_global_step() optimizer, learning_rate = get_optimizer(FLAGS.train.optimizer, global_step) out_charset = load_charset(FLAGS.charset) net = get_network(FLAGS, out_charset) is_ctc = (net.loss_fn == 'ctc_loss') # Multi tower for multi-gpu training tower_grads = [] tower_extra_update_ops = [] tower_preds = [] tower_gts = [] tower_losses = [] batch_size = FLAGS.train.batch_size tower_batch_size = batch_size // num_gpus val_tower_outputs = [] eval_tower_outputs = [] for gpu_indx in range(num_gpus): # Train tower print('[+] Build Train tower GPU:%d' % gpu_indx) input_device = '/gpu:%d' % gpu_indx tower_batch_size = tower_batch_size \ if gpu_indx < num_gpus-1 \ else batch_size - tower_batch_size * (num_gpus-1) train_loader = DatasetLodaer( dataset_paths=FLAGS.train.dataset_paths, dataset_portions=FLAGS.train.dataset_portions, batch_size=tower_batch_size, label_maxlen=FLAGS.label_maxlen, out_charset=out_charset, preprocess_image=net.preprocess_image, is_train=True, is_ctc=is_ctc, shuffle_and_repeat=True, concat_batch=True, input_device=input_device, num_cpus=num_cpus, num_gpus=num_gpus, worker_index=gpu_indx, use_rgb=FLAGS.use_rgb, seed=FLAGS.seed, name='train') tower_output = single_tower(net, gpu_indx, train_loader, out_charset, optimizer, name='train', is_train=True) tower_grads.append([x for x in tower_output.grads if x[0] is not None]) tower_extra_update_ops.append(tower_output.extra_update_ops) tower_preds.append(tower_output.prediction) tower_gts.append(tower_output.text) tower_losses.append(tower_output.loss) # Print network structure if gpu_indx == 0: param_stats = tf.profiler.profile(tf.get_default_graph()) logger.info('total_params: %d\n' % param_stats.total_parameters) # Valid tower print('[+] Build Valid tower GPU:%d' % gpu_indx) valid_loader = DatasetLodaer(dataset_paths=FLAGS.valid.dataset_paths, dataset_portions=None, batch_size=FLAGS.valid.batch_size // num_gpus, label_maxlen=FLAGS.label_maxlen, out_charset=out_charset, preprocess_image=net.preprocess_image, is_train=False, is_ctc=is_ctc, shuffle_and_repeat=False, concat_batch=False, input_device=input_device, num_cpus=num_cpus, num_gpus=num_gpus, worker_index=gpu_indx, use_rgb=FLAGS.use_rgb, seed=FLAGS.seed, name='valid') val_tower_output = single_tower(net, gpu_indx, valid_loader, out_charset, optimizer=None, name='valid', is_train=False) val_tower_outputs.append( (val_tower_output.loss, val_tower_output.prediction, val_tower_output.text, val_tower_output.filename, val_tower_output.dataset)) # Aggregate gradients losses = tf.reduce_mean(tower_losses) grads = _average_gradients(tower_grads) with tf.control_dependencies(tower_extra_update_ops[-1]): if FLAGS.train.optimizer.grad_clip_norm is not None: grads, global_norm = _clip_gradients( grads, FLAGS.train.optimizer.grad_clip_norm) tf.summary.scalar('global_norm', global_norm) train_op = optimizer.apply_gradients(grads, global_step=global_step) # Define config, scaffold saver = tf.train.Saver() sess_config = get_session_config() scaffold = get_scaffold(saver, FLAGS.train.tune_from, 'train') restore_model = get_init_trained() # Define validation saver, summary writer summaries = tf.get_collection(tf.GraphKeys.SUMMARIES) val_summary_op = tf.summary.merge( [s for s in summaries if 'valid' in s.name]) val_summary_writer = { dataset_name: tf.summary.FileWriter(os.path.join(model_dir, 'valid', dataset_name)) for dataset_name in valid_loader.dataset_names } val_summary_writer['total_valid'] = tf.summary.FileWriter( os.path.join(model_dir, 'valid', 'total_valid')) val_saver = tf.train.Saver(max_to_keep=len(valid_loader.dataset_names) + 1) best_val_err_rates = {} best_steps = {} # Training print('[+] Make Session...') with tf.train.MonitoredTrainingSession( checkpoint_dir=model_dir, scaffold=scaffold, config=sess_config, save_checkpoint_steps=FLAGS.train.save_steps, save_checkpoint_secs=None, save_summaries_steps=FLAGS.train.summary_steps, save_summaries_secs=None, ) as sess: log_formatted(logger, 'Training started!') _step = 0 train_t = 0 start_t = time.time() while _step < FLAGS.train.max_num_steps \ and not sess.should_stop(): # Train step step_t = time.time() [step_loss, _, _step, preds, gts, lr] = sess.run([ losses, train_op, global_step, tower_preds[0], tower_gts[0], learning_rate ]) train_t += time.time() - step_t # Summary if _step % FLAGS.valid.steps == 0: # Train summary train_err = 0. for i, (p, g) in enumerate(zip(preds, gts)): s = get_string(p, out_charset, is_ctc=is_ctc) g = g.decode('utf8').replace(DELIMITER, '') s = adjust_string(s, FLAGS.train.lowercase, FLAGS.train.alphanumeric) g = adjust_string(g, FLAGS.train.lowercase, FLAGS.train.alphanumeric) e = int(s != g) train_err += e if FLAGS.train.verbose and i < 5: print('TRAIN :\t{}\t{}\t{}'.format(s, g, not bool(e))) train_err_rate = \ train_err / len(gts) # Valid summary val_cnts, val_errs, val_err_rates, _ = \ validate(sess, _step, val_tower_outputs, out_charset, is_ctc, val_summary_op, val_summary_writer, val_saver, best_val_err_rates, best_steps, best_model_dir, FLAGS.valid.lowercase, FLAGS.valid.alphanumeric) # Logging log_strings = ['', '-' * 28 + ' VALID_DETAIL ' + '-' * 28, ''] for dataset in sorted(val_err_rates.keys()): if dataset == 'total_valid': continue cnt = val_cnts[dataset] err = val_errs[dataset] err_rate = val_err_rates[dataset] best_step = best_steps[dataset] s = '%s : %.2f%%(%d/%d)\tBEST_STEP : %d' % \ (dataset, (1.-err_rate)*100, cnt-err, cnt, best_step) log_strings.append(s) elapsed_t = float(time.time() - start_t) / 60 remain_t = (elapsed_t / (_step+1)) * \ (FLAGS.train.max_num_steps - _step - 1) log_formatted( logger, 'STEP : %d\tTRAIN_LOSS : %f' % (_step, step_loss), 'ELAPSED : %.2f min\tREMAIN : %.2f min\t' 'STEP_TIME: %.1f sec' % (elapsed_t, remain_t, float(train_t) / (_step + 1)), 'TRAIN_SEQ_ERR : %f\tVALID_SEQ_ERR : %f' % (train_err_rate, val_err_rates['total_valid']), 'BEST_STEP : %d\tBEST_VALID_SEQ_ERR : %f' % (best_steps['total_valid'], best_val_err_rates['total_valid']), *log_strings) log_formatted(logger, 'Training is completed!')
|.........| ##+........| |.........| # |........| |.........+########## |........| |.........| |........| |.........| ---------- |.........| ----------- ''' tiles = [] for row in map_.split('\n')[1:-1]: tiles.append([]) for char in row: tiles[-1].append(tiletypes[char]) size = len(tiles[0]), len(tiles) grid = Grid(size) open_ = Flags.from_tiles_attr(tiles, 'is_open') print open_ seen = grid.fov(open_, (23,5), 4) print seen #fov lit walls print ~open_ & seen #path = Flags(size) #for idx in grid.get_path(open_, (15, 5), (40,5)): # path[idx] = True #print path