def __init__(self, output_folder, logging_folder): # setup keyboard listener self.listener = keyboard.Listener(on_press=self._on_press) self.listener.start() # labels self.intro_start = None self.intro_end = None self.outro_start = None self.outro_end = None # constants self.skip_time = 5000 # 5 sec self.end_margin = 500 # 0.5 sec self.thread_period = 0.1 # 0.1 sec self.numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] self.output_folder = output_folder self.logging_folder = logging_folder # setup signal handler signal.signal(signal.SIGINT, self._sigint_handler) self.displayer = output(initial_len=5, interval=0) self.output_buffer = self.displayer.__enter__() self.status = ""
def outer(*a, watch=False, headers=None, times=None, timer=True, **kw): if not watch: # return _watch((lambda data: disp(data, headers)), *a, times=times, timer=timer, **kw) result = func(*a, **kw) print(disp(list(result), headers=headers)) return result import datetime import reprint items = {} with reprint.output() as out: try: out.append('Starting scan...') for i in _loop(times): t0 = time.time() j = 0 for j, x in enumerate(func(*a, **kw)): items[x['ip']] = _dict_update(items.get(x['ip']), x) out.change(disp(list(items.values()), headers).splitlines()) if timer: out.append('Scan {} finished at {}. took {:.1f}s. Found {} hosts.'.format( i+1, datetime.datetime.now().strftime('%c'), time.time() - t0, len(items))) except KeyboardInterrupt: out.change(disp(list(items.values())).splitlines()) return list(items.values())
def mainLoop(host, port): with output(output_type="list") as outStream: telnet = telnetlib.Telnet() telnet.open(host, port) while (True): command = "status\n".encode('ascii') telnet.write(command) breaker = '>'.encode('ascii') outStuff = telnet.read_until(breaker, 1).decode('ascii') statusLines = outStuff.strip().split('\n') statusMap = {} for line in statusLines: if (line == ">"): break statusMap.update({line.split(':')[0]: line.split(':')[1]}) outList = [] for key, value in statusMap.items(): outList.append(getHealthBar(key, int(value))) outStream.change(outList) time.sleep(0.5) telnet.close()
def main(args): assert os.path.exists(args.dir), f'The specified working directory {args.dir} does not exist.' assert cmd != 'python /path/to/script.py', 'Please specify the command to run in gpu_monitor.py' ready, id, info = gpu_info(args) gpu_info_format = '|{:^7d}| {:>3.0f}W / {:>3.0f}W | {:>5d}MiB / {:>5d}MiB |\r\n' i = 0 with output(output_type='list', initial_len=len(info) + 2, interval=0) as gpu_info_print: while not ready: # set waiting condition ready, id, info = gpu_info(args) i = i % 5 gpu_info_print[0] = '| GPU | Pwr:Usage/Cap | Memory-Usage |\r\n' for j, info_j in enumerate(info, start=1): gpu_info_print[j] = gpu_info_format.format(*info_j) gpu_info_print[-1] = 'monitoring' + '.' * (i + 1) time.sleep(args.interval) i += 1 start = time.time() send_mail(cmd) # send notification via email to the user os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(list(map(str, id[:args.num_gpus]))) os.chdir(os.path.expanduser(args.dir)) # change working directory print('\n' + cmd) try: if os.system(cmd) != 0: raise Exception('Error encountered.') except Exception: sub = 'Error Encountered' content = 'Hi,\n\nWe regret to inform you that your program encountered some errors ' \ 'during running, please check with it, Thanks.' \ '\n\nThis is a system generated email, do not reply.\n\n' \ 'Nice day,\n' + socket.gethostname() send_mail(cmd, subject=sub, content=content) else: send_mail(cmd, finish=True, time_used=time.time() - start)
def interfaceLoop(t,nprint): try: with output(output_type='dict') as output_lines: while(running): idict = dict(ha) PC = str(pacchetticatturati) LE = str(lastexport) ch = canale ee = str(imprexc) interfacedict = {} output_lines['Channel'] = "[{cc}] - Packet: [{pp}] - Interface: [{ii}]".format(cc=ch, pp=PC, ii=moninterface) output_lines['Last Export'] = "[{le}]".format(le=LE) output_lines['Debug'] = "[{exc}]".format(exc = ee) for k in idict: interfacedict[k] = idict.get(k)[-1][1] if (nprint > 0): ssize = min(nprint, len(interfacedict)) else: ssize = len(interfacedict) sorted_d = sorted(interfacedict.items(), key=lambda x: x[1]) for i in range(0,ssize): k = sorted_d[i][0] s = sorted_d[i][1] output_lines[i] = "MAC: {mc} - Signal: {sg}".format(mc = k, sg = s) time.sleep(t) except KeyboardInterrupt: raise
def _render_result(self, submission_url_ret: str) -> None: """ Continuously polling for result from `submission_url_ret` Parameters ---------- submission_url_ret : str url for the submission to be checked """ time_out = 60 cur_time = 0 done = False sleep_time = 0.4 with output(output_type='dict') as output_lines: while cur_time < time_out and not done: try: self.login() page = self.request_get(submission_url_ret) soup = BeautifulSoup(page.content, 'html.parser') submission_data = soup.find('div', class_='testcases') if submission_data is not None: submission_ret = submission_data.find_all('span') status_ret = soup.find( 'td', class_='status middle').find('span').text runtime_ret = soup.find('td', class_='runtime middle').text done = self.is_finished(output_lines, submission_ret, status_ret, runtime_ret) except Exception as e: log_red(f'Internal error: {e!r}') time.sleep(sleep_time) cur_time += sleep_time
def print_adrenaline(self): with output(output_type='dict') as output_lines: adrenaline = self.adrenaline output_lines[ 'Adrenaline '] = "[{done}{padding}] {percent}%".format( done=d.color.RED + "#" * int(adrenaline / 10) + d.color.END, padding=" " * (10 - int(adrenaline / 10)), percent=adrenaline)
def main(): connection = AuthorizedConnection(f'http://{ip}/', login, password) with output(output_type='dict') as output_lines: while True: try: client = Client(connection) print_signal(client, output_lines) print_traffic(client, output_lines) time.sleep(1) except ResponseErrorLoginCsfrException: connection = AuthorizedConnection(f'http://{ip}/', login, password) client.user.logout()
def print_progress_generator(progress_generator): """ Uses the reprint to attempt and replace the terminal output characters for the progress bars. """ with output(output_type='dict') as output_lines: fmt = '{status} {progress}' for progress in progress_generator: if 'id' in progress: output_lines[progress['id']] = fmt.format( status=progress.get('status', ''), progress=progress.get('progress', '')) else: # try status first, then try stream line = progress.get('status', progress.get('stream')) click.echo(line)
def handle(self, *args, **options): self.started_at = now() self.path = options['dataset'] existing = Reimbursement.objects.count() print('Starting with {:,} reimbursements'.format(existing)) self.count = {'updated': 0, 'created': 0, 'skip': 0} if options.get('drop', False): self.drop_all(Reimbursement) with output() as status: status.change(self.status) self.create_or_update(self.reimbursements, status) self.mark_not_updated_reimbursements()
def main(): import argparse, sys, math import plotille import reprint from pklaus.network.ping.ping_wrapper import PingWrapper parser = argparse.ArgumentParser( description='Ping a host and create histogram.') parser.add_argument('host', help='The host to ping') parser.add_argument('--count', '-c', type=int, default=60, help='Number of times the host should be pinged') parser.add_argument('--interval', '-i', type=float, default=1.0, help='Interval between individual pings.') parser.add_argument('--debug', '-d', action='store_true', help='Enable debug output for this script') args = parser.parse_args() if args.debug: logging.basicConfig(format='%(levelname)s:%(message)s', level='DEBUG') ping_wrapper = PingWrapper( f'ping {args.host} -c{args.count} -i{args.interval}') try: round_trip_times = [] with reprint.output(initial_len=24, interval=0) as output_lines: for round_trip_time in ping_wrapper.run(): round_trip_times.append(round_trip_time) if len(round_trip_times) < 2: continue x_min = math.floor(min(round_trip_times)) x_max = math.ceil(max(round_trip_times)) hist_string = plotille.histogram(round_trip_times, width=60, height=20, x_min=x_min, x_max=x_max) output_lines.change(hist_string.split('\n')) except KeyboardInterrupt: sys.exit() exit(ping_wrapper.returncode)
def test_main(): handle_dirs() checkout_info = {} budget = {} monitor_url = {} debug = False headless = True if "--debug" in sys.argv: debug = True if "--gui" in sys.argv: headless = False if os.path.exists("../cfg/config.json"): with open("../cfg/config.json", "r") as f: cfg = json.load(f) checkout_info = cfg['checkout_info'] budget = cfg['budget'] if os.path.exists("../cfg/urls.json"): with open("../cfg/urls.json", "r") as f: monitor_url = json.load(f) urlc = 0 monitors = [] with output(output_type="dict", interval=50) as output_lines: for site in monitor_url: output_lines[site] = "starting monitor...." monitors.append( SiteMonitor(headless, monitor_url[site], auto_buy=True, debug=debug, budget=budget, checkout=checkout_info)) for p in monitor_url[site]: urlc += len(monitor_url[site][p]) for monitor in monitors: monitor.start() monitor_display = MonitorDisplay(monitors) try: monitor_display.run() except: traceback.print_exc()
def print_output(self): """ Prints the table and continues to update it, blocking while doing so When all recursive size calculations are finished, it will return. The inital table order is by inital base level calculations. The table will only re-order based on sort_by and desc when all other calculations are done. This is for processing efficiency on larger directories. TODO: q to quit functionality TODO: I am using the reprint library, which works exaclt how i want it to with smaller directories but larger directories thatn the size of the terminal window can be a problem. Additionally, preformance is a concern. I played around with curses and manually edditing previous lines with limmited success. But ideally this library should be replaces, or possibly this functionality removed entierly. """ self._sort() inital_table = self._get_output_table().split("\n") table_size = len(inital_table) tick = 0 with reprint.output(output_type='list', initial_len=table_size + 1) as output_lines: inital = True while inital or not self.payload[DONE]: inital = False self._load_table_to_reprint(output_lines) # Just makes a plesant Loading... annimation tail = "Loading" + "." * (tick % 4) output_lines[ table_size] = f"Count: {self.payload[COUNT]} Size: {self.payload[SIZE]} - {tail}" # TODO: this 'q to quit' functionality does not work # if keyboard.is_pressed('q'): # self.payload[DONE] = True # exit() time.sleep(1) tick += 1 # print out the final offical version of the table self._sort() self._load_table_to_reprint(output_lines) output_lines[ table_size] = f"Count: {self.payload[COUNT]} Size: {self.payload[SIZE]} - Done."
def on_upload(self, namespace): bucket = self.bucket() if not bucket: raise errors.UserError('Missing bucket') files = {} for local_path in utils.iter_local_path(namespace.path, namespace.recursive): if not os.path.isfile(local_path): continue key = utils.file_key(local_path) files[key] = { 'local_size': os.stat(local_path).st_size, 'local_path': local_path, } for remote in utils.iter_remote_path(bucket, namespace.path, namespace.recursive): if remote.name in files: files[remote.name]['key'] = remote conflicts = 0 pool = tasks.ThreadPool(settings.THREAD_MAX_COUNT, auto_start=False) for key, data in six.iteritems(files): if 'key' in data and namespace.force: task = tasks.ReplaceUpload() elif 'key' not in data: data['key'] = boto.s3.key.Key(bucket=bucket, name=key) task = tasks.Upload() else: conflicts += 1 continue pool.add_task(task, bucket, self.conf, key, data) if conflicts: print('{} remote paths exists, use force flag'.format(conflicts)) with reprint.output(initial_len=settings.THREAD_MAX_COUNT) as output: pool.start(output) pool.join()
def scale(ctx, appname, cpus, memories, replicas): appname = get_appname(appname=appname) if not (cpus or memories or replicas): fatal("you must at least sapecify one of cpu memory and replicas") cpus_dict = cfg_list_to_dict(cpus) memories_dict = cfg_list_to_dict(memories) kae = ctx.obj['kae_api'] with output(output_type="list", initial_len=10, interval=0) as output_list: for m in kae.scale(appname, cpus_dict, memories_dict, replicas): if m['error']: fatal(m['error']) else: table = PrettyTable(['name', 'status', 'ready']) for p in m['pods']: table.add_row([p['name'], p['status'], p['ready']]) merge_list(output_list, str(table).split('\n')) click.echo(info("scale done.."))
def __iter__(self) -> Iterator[_T]: max_len = len(str(len(self.id_range) - 1)) self.current = self.id_range[0] style = self.fix_style(self.style) context = output(output_type='dict' ) if style != Style.NON_TTY_SAFE else nullcontext({}) with context as output_lines: while True: self.print_info(style, self.bar_length, output_lines, max_len, self.fill_char, self.main_name) try: yield self.next(timeout=0.05) # pylint: disable=not-callable except TimeoutError: pass except StopIteration: break # TODO: maybe make this configurable? # if style == 2: # print() yield from self._wrapped_object
def read_sensors(sensor_names): sensors = [Sensor(sensor_name) for sensor_name in sensor_names] context = Context(bridge=DrHueBridge(), refresh_interval=1) for sensor in sensors: sensor.attach_context(context) while True: info = {} for sensor in sensors: info[sensor.name] = { "dark": sensor.read('dark'), 'motion': sensor.read('motion') } with output(output_type="dict") as output_dict: for sensor, sensor_data in info.items(): output_dict[sensor + ' dark'] = sensor_data['dark'] output_dict[sensor + ' motion'] = sensor_data['motion'] context.update_and_wait(log=False) for sensor in sensors: sensor.sync_states()
def get(self, timeout: float = None, style: Style = Style.ACTIVE_JOBS_AND_TOTAL, bar_length: int = 10, fill_char: str = "#", main_name: str = 'Total Progress'): if timeout is None: limit = float('inf') else: limit = time() + timeout max_len = len(str(len(self.id_range) - 1)) style = self.fix_style(style) context = output(output_type='dict' ) if style != Style.NON_TTY_SAFE else nullcontext({}) with context as output_lines: while not self.ready() and time() < limit: self.print_info(style, bar_length, output_lines, max_len, fill_char, main_name) sleep(0.05) # if style == 2: # print() return self._wrapped_object.get(timeout=0)
def main(): global arrs, column, row STRINGER = "" Numbs = ["0", "1"] x = 0 for i in range(column): stream = Stream() y = random.randint(0, row - 5) stream.generateSymbols(y, x) streams.append(stream) x += 1 with output(output_type="list", initial_len=3, interval=0) as outputLines: while 1: for i in range(row): STRINGER += "".join(arrs[i]) for stream in streams: stream.render() outputLines[0] = STRINGER STRINGER = "" time.sleep(0.1)
def run(self): columns = ["Vendor", "Products", "Price", "Status", "Info", "Error"] with output( output_type="dict", sort_key=lambda x: "_Status" not in x[0] or "_" not in x[0], interval=250) as output_lines: output_lines.clear() urls = [] for m in self.monitors: urls.extend(list(map(lambda u: (u, m.URL[u]), m.URL))) while not self.stopped and len( [m for m in self.monitors if not m.exited]) > 0: output_lines[col('_Monitor', 'header')] = ''.join(f"{c} {self.sp(c)}|\t\t" for c in columns) for i in range(500): try: for u in enumerate(urls, start=1): output_lines[str(u[0])] = self.format_url(u) output_lines[col('_Status_', 'header')] = "".join([ f"{m.domain}:{statcol(m.STATUS)} |" for m in self.monitors ]) except KeyboardInterrupt: output_lines.append(col("Stopping Monitors!", "red")) # output_lines['_Monitor'] = ''.join(f"{c} {self.sp(c)}|\t\t" for c in columns) for m in self.monitors: m.kill() except: output_lines.clear() traceback.print_exc() output_lines.append(col("Stopping Monitors!", "red")) for m in self.monitors: m.kill() output_lines.clear() output_lines.append(col("All monitors stopped. Exiting.", "yellow"))
def progress(self, futures): """ Print a progressbar Note: futures is a dictionary. Keys = Name of the thread, Value = concurrent.futures object. The function being executed MUST return a dictionary with 'status' key that defines the status code. """ done = defaultdict(bool) isatty = sys.stdout.isatty() with reprint.output( output_type="list", initial_len=len(futures.items()), interval=0 ) as output: num_iterations = 0 self.print_lines(output) while True: i = 0 num_iterations += 1 for image, thread in futures.items(): output[i] = image if thread.done(): output[i] += ( "." * 10 + constants.STATUS_MESSAGE[futures[image].result()] ) done[image] = True else: output[i] += "." * (num_iterations % 10) done[image] = False i += 1 if all(done.values()): break time.sleep(1) self.print_lines(output)
def monitor_deployment(ecs, cluster, service): # Reprint service and deployments info with output(initial_len=25, interval=0) as out: while True: response = ecs.describe_services(cluster=cluster, services=[service]) s = response['services'][0] # Print Service Info cls_name = s['clusterArn'].split('/')[-1] def_name = s['taskDefinition'].split('/')[-1] general_info = (cls_name, s['serviceName'], def_name) counts = (s['desiredCount'], s['runningCount'], s['pendingCount']) srv_info = general_info + counts out[0] = '{} {:<24} {} desired: {} running: {} pending: {}'.format( *srv_info) out[1] = '\n' out[2] = 'Deployments:' # Print Deployments Info for d in s['deployments']: d_info = (d['status'], d['taskDefinition'].split('/')[-1], d['desiredCount'], d['runningCount'], d['pendingCount']) index = s['deployments'].index(d) + 3 out[index] = '\t\t{:<31} {} desired: {} running: {} pending: {}'.format( *d_info) out[(len(s['deployments']) + 4)] = '\n' for e in s['events'][:5]: index = s['events'].index(e) + len(s['deployments']) + 5 out[index] = '{} {}'.format(e['createdAt'], e['message']) time.sleep(2)
def upload_small_file(access_token: str, info: UploadInfo): # 小于或等于4MB的文件直接上传 with output(initial_len=4) as out_lines: out_lines[0] = color_print.bs('上传小文件中,请勿强行停止') out_lines[1], out_lines[2] = table_header_and_divider(info.filename) out_lines[3] = table_data(info) start = time.time() with open(info.local_file_path, 'rb') as f: resp_json = drive_api.put_content( access_token, info.onedrive_dir_path + info.filename, f.read()).json() if 'id' in resp_json.keys(): # 上传成功 info.spend_time = time.time() - start info.speed = int(info.size / info.spend_time) info.finish_time = utc_datetime_str() info.status = 'finished' info.finished = info.size out_lines[3] = table_data(info) out_lines.append(color_print.gs('上传成功. 文件: %s' % info.filename)) else: raise Exception(str(resp_json.get('error'))) return
# sudo python bench/test.py from __future__ import print_function from reprint import output from devices.kpro import Kpro kpro = Kpro() with output(output_type="dict", initial_len=1, interval=0) as output_list: while True: output_list['ETH'] = str(kpro.eth()) output_list['FLT'] = str(kpro.flt()) output_list['BAT'] = str(kpro.bat()) output_list['CAM'] = str(kpro.cam()) output_list['O2'] = str(kpro.o2()) output_list['IAT'] = str(kpro.iat()) output_list['RPM'] = str(kpro.rpm()) output_list['TPS'] = str(kpro.tps()) output_list['VSS'] = str(kpro.vss()) output_list['ECT'] = str(kpro.ect()) output_list['GEAR'] = str(kpro.gear()) output_list['EPS'] = str(kpro.eps()) output_list['SCS'] = str(kpro.scs()) output_list['RVSLCK'] = str(kpro.rvslck()) output_list['BKSW'] = str(kpro.bksw()) output_list['ACSW'] = str(kpro.acsw()) output_list['ACCL'] = str(kpro.accl()) output_list['FLR'] = str(kpro.flr()) output_list['FANC'] = str(kpro.fanc()) output_list['MAP'] = str(kpro.map()) output_list['AN0'] = str(kpro.analog_input(0))
def mainLoop(self,d): t0 = time.process_time() if self.verbose: print("Starting control main loop") t = 0 if not self.norobot: if self.verbose: print("Connecting to Thymio at address "+self.address+" ... ") try: self.th = Thymio.serial(port=self.address, refreshing_rate=0.1) time.sleep(3) self.th.set_var_array("leds.top", [0, 0, 0]) if self.verbose: print("connection successful !") except: print("Unexpected error:", sys.exc_info()[0]) return if self.verbose: print("Starting main loop") # Initialise robot class Init_pos = False Ts = 0.1 kp = 3. #2 #3 #0.15 #0.5 ka = 35. #22 #35 #0.4 #0.8 kb = -8. #-4 #-8 #-0.07 #-0.2 vTOm=31.5 #30.30 wTOm=(200*180)/(80*math.pi) #130.5 # thym = Robot(False,False,Ts, kp,ka,kb,vTOm,wTOm) # Initialise Filtering class Rvel = np.array([[2., 0.], [0.,2.]]) Hvel = np.array([[0.,0.,0.,1.,0.],[0.,0.,0.,0.,1.]]) Rcam = np.array([[2.,0.,0.],[0.,2.,0.],[0.,0.,0.3]]) Hcam = np.array([[1.,0.,0.,0.,0.],[0.,1.,0.,0.,0.],[0.,0.,1.,0.,0.]]) filter = Filtering(Rvel, Rcam, thym, Hvel, Hcam,Ts) init = False go=1 cnt = 0 """ wait until vision is go""" while not d['started']: time.sleep(0.1) print("STARTING CONTROL, PRESS Q TO SHUTDOWN : \n") with output(output_type='dict') as output_lines: while go: # Some simulation variables tps1 = time.monotonic() cnt += 1 ######################################################################### # get every 1 second the position of the robot given by the camera # # when it is possible if not possible set the updateWithCam bolean # # to False # ######################################################################### pos_cam = np.array(d['visPos'],ndmin=2).T if not cnt%10 : update_cam = False if pos_cam is False or (pos_cam[0] == 0) else True else : update_cam = False ######################################################################### # # # FSM of the Robot # # # ######################################################################### if not self.norobot: if thym.state =='ASTOLFI' : thym.ASTOLFI(self.th,Ts, filter,pos_cam, update_cam) if thym.state == 'INIT': d['pathComputed'] = False d['path'] = False elif thym.state == 'TURN' : thym.TURN(self.th,Ts, filter, pos_cam, update_cam) elif thym.state == 'LOCAL' : thym.LOCAL(self.th,Ts, filter, pos_cam, update_cam) # Tell vision to recompute a path if thym.state == 'INIT': d['pathComputed'] = False d['path'] = False elif thym.state == 'INIT' : thym.INIT(d['path'],d['visPos']) ######################################################################### # # # Compute the time the control took, to adjust Ts # # # ######################################################################### tps2 = time.monotonic() Ts=tps2-tps1 d['fltPos'] = thym.Pos ######################################################################### # # # Check if the robot is at the goal # # # ######################################################################### if not self.norobot: go = thym.FINISH(self.th, go) tfinal=time.monotonic() rt = time.process_time() - t0 t0 = time.process_time() precision = 1 # we update display and history 10x a second if round(time.process_time(),precision) > t: t = round(time.process_time(),precision) """ Nice display for the robot control """ if os.name == 'nt': os.system("cls") if self.verbose: output_lines['HISTORY SAMPLES'] = len(self.history) output_lines['CTRL PERIOD'] = str(rt) output_lines['VISION PERIOD'] = str(d["vtime"]) output_lines['VISION POS'] = str(d['visPos']) output_lines['KALMAN POS'] = str(d['fltPos']) output_lines['GOAL'] = str(d['goal']) output_lines['PATH'] = str(d['path']) output_lines['RUNNING'] = str(d['running']) if not self.norobot: output_lines['STATE'] = str(thym.state) else: output_lines['STATE'] = " NO ROBOT" histPoint = self.copyProxyDict(d) histPoint['time'] = time.process_time() if not self.norobot: histPoint['state'] = thym.state histPoint['ML'] = thym.ML histPoint['MR'] = thym.MR histPoint['alpha'] = thym.a histPoint['beta'] = thym.b histPoint['rho'] = thym.p histPoint['sensor'] = thym.sensor else: histPoint['state'] = "NO ROBOT" self.history.append(histPoint) if d['running'] == False: go = 0 print("CONTROL STOPPED\n") if(self.save,str): self.saveHistory()
def main(): try: thisdir = str(pathlib.Path(__file__).parent.absolute()) logdirs = [thisdir + d for d in ["/scrn/error/", "/log/"]] for d in logdirs: if not os.path.exists(os.path.dirname(d)): try: os.makedirs(os.path.dirname(d)) except OSError as exc: if exc.errno != errno.EEXIST: raise checkout_info = {} budget = {} monitor_url = {} debug = False headless = True if "--debug" in sys.argv: debug = True if "--gui" in sys.argv: headless = False if os.path.exists("../cfg/config.json"): with open("../cfg/config.json", "r") as f: cfg = json.load(f) checkout_info = cfg['checkout_info'] budget = cfg['budget'] if os.path.exists("../cfg/urls.json"): with open("../cfg/urls.json", "r") as f: monitor_url = json.load(f) urlc = 0 monitors = [] with output(output_type="dict", interval=50) as output_lines: for site in monitor_url: output_lines[site] = "starting monitor...." monitors.append( SiteMonitor(headless, monitor_url[site], auto_buy=not debug, debug=debug, budget=budget, checkout=checkout_info)) for p in monitor_url[site]: urlc += len(monitor_url[site][p]) for monitor in monitors: monitor.start() exitc = 0 if not debug: with output(initial_len=urlc + 1, interval=50) as output_lines: output_lines[ 0] = f"Monitor\t\t\t\t|\t\tProduct\t\t|\t\tPrice|\t\tStatus\t\t|\t\tInfo|Error\t\t|Threads:{threading.activeCount()}" try: while exitc < len(monitors): i = 1 monitors_alive = [m for m in monitors if not m.exited] if len(monitors_alive) < len(monitors): monitors = monitors_alive ol = [output_lines[0]] for m in monitors: ol.append(["....." for u in m.URL]) if len(output_lines) > urlc + 1: ol.append(output_lines[urlc + 1:]) output_lines.change(ol) for m in monitors: for u in m.URL: instock = col( "IN STOCK", "green") if m.URL[u]['status'] else col( "OUT OF STOCK", "red") instock = col( "!!!! PURCHASE INITIATED !!!!", "green") if m.purchasing == u and m.URL[u][ 'status'] else instock exc = col(m.URL[u]['exc'], "red") if "purchased" in m.URL[u] and m.URL[u][ 'prod'] == "test": exc = col("Test passed.", "blue") output_lines[ i] = "{}\t|\t\t{}\t\t|\t\t{}\t\t|\t{}\t|{}|{}".format( m.domain, col(m.URL[u]['prod'], "bold"), col( m.URL[u]['price'], "cyan" if m.price_check(u) else "yellow"), instock, col( m.URL[u]['info'], "yellow" if "purchased" not in m.URL[u] else "blue"), exc) if m.URL[u]['status']: alert(m) i += 1 if m.exited: exitc += 1 if len(output_lines) > i: output_lines[i] = m.STATUS i += 1 except KeyboardInterrupt: output_lines.append(col("Killing all monitors!", "red")) for m in monitors: try: m.kill() except: output_lines.append(m.STATUS) print(os.system(f"taskkill /F /IM Firefox.exe")) exit(0) output_lines.append(m.STATUS) else: while stoppedc < len(monitors): stoppedc = 0 for i, m in enumerate(monitors): if m.stopped: stoppedc += 1 except Exception as e: print("Catastrophic error. Killing monitors.") traceback.print_exc() print(os.system(f"taskkill /F /IM Firefox.exe")) exit(0)
def train(model, train_features, train_targets): print("Labeled pool size: {}".format(len(train_features))) parameters = filter(lambda p: p.requires_grad, model.parameters()) if params["MODEL"] == "rnn": optimizer = optim.Adadelta( parameters, params["LEARNING_RATE"], weight_decay=params["WEIGHT_DECAY"]) else: optimizer = optim.Adadelta(parameters, params["LEARNING_RATE"]) # Softmax is included in CrossEntropyLoss criterion = nn.CrossEntropyLoss() model.train() best_model = None best_acc = 0 best_epoch = 0 with output(initial_len=2, interval=0) as output_lines: for e in range(params["EPOCH"]): shuffle(train_features, train_targets) size = len(train_features) avg_loss = 0 corrects = 0 for i in range(0, len(train_features), params["BATCH_SIZE"]): batch_range = min(params["BATCH_SIZE"], len(train_features) - i) batch_x = train_features[i:i + batch_range] batch_y = train_targets[i:i + batch_range] feature = Variable(torch.LongTensor(batch_x)) target = Variable(torch.LongTensor(batch_y)) if params["CUDA"]: feature, target = feature.cuda(), target.cuda() optimizer.zero_grad() pred = model(feature) loss = criterion(pred, target) loss.backward() optimizer.step() avg_loss += loss.data[0] corrects += (torch.max(pred, 1) [1].view(target.size()).data == target.data).sum() avg_loss = avg_loss * params["BATCH_SIZE"] / size if params["SCORE_FN"] == "all": accuracy = 100.0 * corrects / size dev_accuracy, dev_loss, dev_corrects, dev_size = evaluate( data, model, params, e, mode="dev") s1 = "{:10s} loss: {:10.6f} acc: {:10.4f}%({}/{})".format( "train", avg_loss, accuracy, corrects, size) s2 = "{:10s} loss: {:10.6f} acc: {:10.4f}%({}/{})".format( "dev", dev_loss, dev_accuracy, dev_corrects, dev_size) output_lines[0] = s1 output_lines[1] = s2 if dev_accuracy < best_acc: break best_acc = max(dev_accuracy, best_acc) best_model = model elif ((e + 1) % 10) == 0: accuracy = 100.0 * corrects / size dev_accuracy, dev_loss, dev_corrects, dev_size = evaluate(model, e, mode="dev") s1 = "{:10s} loss: {:10.6f} acc: {:10.4f}%({}/{})".format( "train", avg_loss, accuracy, corrects, size) s2 = "{:10s} loss: {:10.6f} acc: {:10.4f}%({}/{})".format( "dev", dev_loss, dev_accuracy, dev_corrects, dev_size) output_lines[0] = s1 output_lines[1] = s2 # TODO check if this should also apply for cnn if dev_accuracy > best_acc: best_acc = dev_accuracy best_model = copy.deepcopy(model) best_epoch = e # return best_model if best_model != None else model return best_model
from reprint import output import time filenames = [ 'File_1', 'SomeFileWithAVeryLongNameWhichMakeitToANewLineWhichMayInfluenceTheReprintModuleButActuallyNot.exe', 'File_3', 'File_4', ] if __name__ == "__main__": with output(output_type='dict') as output_lines: for i in range(4): output_lines['Moving file'] = filenames[i] for progress in range(100): output_lines[ 'Total Progress'] = "[{done}{padding}] {percent}%".format( done="#" * int(progress / 10), padding=" " * (10 - int(progress / 10)), percent=progress) time.sleep(0.02)
#sudo python bench/test.py from __future__ import print_function from devices.kpro import Kpro from reprint import output kpro = Kpro() with output(output_type="dict", initial_len=1, interval=0) as output_list: while True: output_list['ETH'] = str(kpro.eth()) output_list['FLT'] = str(kpro.flt()) output_list['BAT'] = str(kpro.bat()) output_list['CAM'] = str(kpro.cam()) output_list['O2'] = str(kpro.o2()) output_list['IAT'] = str(kpro.iat()) output_list['RPM'] = str(kpro.rpm()) output_list['TPS'] = str(kpro.tps()) output_list['VSS'] = str(kpro.vss()) output_list['ECT'] = str(kpro.ect()) output_list['GEAR'] = str(kpro.gear()) output_list['EPS'] = str(kpro.eps()) output_list['SCS'] = str(kpro.scs()) output_list['RVSLCK'] = str(kpro.rvslck()) output_list['BKSW'] = str(kpro.bksw()) output_list['ACSW'] = str(kpro.acsw()) output_list['ACCL'] = str(kpro.accl()) output_list['FLR'] = str(kpro.flr()) output_list['MAP'] = str(kpro.map()) output_list['AN0'] = str(kpro.analog_input(0)) output_list['AN1'] = str(kpro.analog_input(1)) output_list['AN2'] = str(kpro.analog_input(2)) output_list['AN3'] = str(kpro.analog_input(3))
from kafka import KafkaConsumer from reprint import output import json topic = 'click_rank' bootstrap_servers = ['localhost:9092'] group_id = 'group7' consumer = KafkaConsumer( topic, # topic的名称 group_id=group_id, # 指定此消费者实例属于的组名,可以不指定 bootstrap_servers=bootstrap_servers, # 指定kafka服务器 auto_offset_reset='latest', # 'smallest': 'earliest', 'largest': 'latest' ) with output(output_type="list", initial_len=22, interval=0) as output_lines: # 初始化打印行 output_lines[0] = '=== 男 ===' output_lines[6] = '=== 女 ===' for msg in consumer: # 解析结果 data = json.loads(msg.value) start_index = 1 if data['sex'] == '男' else 7 rank = json.loads('[' + data['top10'] + ']') # 逐行打印 for i in range(5): index = start_index + i if i < len(rank): name = list(rank[i].keys())[0]
np.random.seed(1) def epsilon_greedy(Q, state): if (np.random.uniform() > 1 - EPSILON) or ((Q[state, :] == 0).all()): action = np.random.randint(0, 4) # 0~3 else: action = Q[state, :].argmax() return action e = Env() Q = np.zeros((e.state_num, 4)) with output(output_type="list", initial_len=len(e.map), interval=0) as output_list: for i in range(100): e = Env() action = epsilon_greedy(Q, e.present_state) while (e.is_end is False) and (e.step < MAX_STEP): state = e.present_state reward = e.interact(action) new_state = e.present_state new_action = epsilon_greedy(Q, e.present_state) Q[state, action] = (1 - ALPHA) * Q[state, action] + \ ALPHA * (reward + GAMMA * Q[new_state, new_action]) action = new_action e.print_map_with_reprint(output_list) time.sleep(0.1) for line_num in range(len(e.map)): if line_num == 0:
elif cmd == 'n' or cmd == '1': cleanScreen() displayStockName = not displayStockName printStock() elif cmd == 'c': colored = not colored time.sleep(1) reciveCmd() def cleanScreen(): os.system("clear && printf '\e[3J'") def terminate(): cleanScreen() os._exit(1) if __name__ == '__main__': readData() t = Thread(target=reciveCmd, args=()) t.start() with output(initial_len=50, interval=0) as output_lines: while True: if stop == 1: terminate() printStock() time.sleep(3)
def train_model(self, epochs): #1. construct the computation graph self.net.init_modules() #save net structure to data folder net_f = open(os.path.join(self.output_dir, 'nn.txt'), 'w') net_f.write(str(self.net)) net_f.close() #find previous snapshot lsf, nfiles, sfiles = self.find_previous() #2. restore weights if lsf == 0: lr, last_iter, stepsizes, self.np_paths, self.ss_paths = self.initialize() else: lr, last_iter, stepsizes, self.np_paths, self.ss_paths = self.restore(str(sfiles[-1]), str(nfiles[-1])) #3. fix weights and eval mode self.fix_eval_parts() # construct optimizer self.construct_optimizer(lr) if len(stepsizes) != 0: next_stepsize = stepsizes.pop(0) else: next_stepsize = -1 train_timer = Timer() current_snapshot_epoch = int(last_iter / len(self.dataloader_train)) for epoch in range(current_snapshot_epoch, epochs): print("start epoch {}".format(epoch)) with output(initial_len=9, interval=0) as content: for iter, blobs in enumerate(tqdm(self.dataloader_train)): last_iter += 1 # adjust learning rate if last_iter == next_stepsize: lr *= cfg.GAMMA self.scale_lr(self.optimizer, lr) if len(stepsizes) != 0: next_stepsize = stepsizes.pop(0) batch_size = blobs['data'].shape[0] if len(blobs['gt_box']) < batch_size: #invalid sample continue train_timer.tic() # IMAGE PART if cfg.USE_IMAGES: grid_shape = blobs['data'].shape[-3:] projection_helper = ProjectionHelper(cfg.INTRINSIC, cfg.PROJ_DEPTH_MIN, cfg.PROJ_DEPTH_MAX, cfg.DEPTH_SHAPE, grid_shape, cfg.VOXEL_SIZE) proj_mapping = [[projection_helper.compute_projection(d.cuda(), c.cuda(), t.cuda()) for d, c, t in zip(blobs['nearest_images']['depths'][i], blobs['nearest_images']['poses'][i], blobs['nearest_images']['world2grid'][i])] for i in range(batch_size)] jump_flag = False for i in range(batch_size): if None in proj_mapping[i]: #invalid sample jump_flag = True break if jump_flag: continue blobs['proj_ind_3d'] = [] blobs['proj_ind_2d'] = [] for i in range(batch_size): proj_mapping0, proj_mapping1 = zip(*proj_mapping[i]) blobs['proj_ind_3d'].append(torch.stack(proj_mapping0)) blobs['proj_ind_2d'].append(torch.stack(proj_mapping1)) self.net.forward(blobs) self.optimizer.zero_grad() self.net._losses["total_loss"].backward() self.optimizer.step() train_timer.toc() # Display training information if iter % (cfg.DISPLAY) == 0: self.log_print(epoch*len(self.dataloader_train)+iter, lr, content, train_timer.average_time()) self.net.delete_intermediate_states() # validate if satisfying the time criterion if train_timer.total_time() / 3600 >= cfg.VAL_TIME: print('------------------------VALIDATION------------------------------') self.validation(last_iter, 'val') print('------------------------TRAINVAL--------------------------------') self.validation(last_iter, 'trainval') # snapshot if cfg.VAL_TIME > 0.0: ss_path, np_path = self.snapshot(last_iter) self.np_paths.append(np_path) self.ss_paths.append(ss_path) #remove old snapshots if too many if len(self.np_paths) > cfg.SNAPSHOT_KEPT and cfg.SNAPSHOT_KEPT: self.remove_snapshot() train_timer.clean_total_time()