def start_program_reset_handlers_test(self): """Test the reset_handlers parameter of startProgram.""" with tempfile.NamedTemporaryFile(mode="w+t") as testscript: testscript.write("""#!/bin/sh # Just hang out and do nothing, forever while true ; do sleep 1 ; done """) testscript.flush() # Start a program with reset_handlers proc = util.startProgram(["/bin/sh", testscript.name]) with timer(5): # Kill with SIGPIPE and check that the python's SIG_IGN was not inheritted # The process should die on the signal. proc.send_signal(signal.SIGPIPE) proc.communicate() self.assertEqual(proc.returncode, -(signal.SIGPIPE)) # Start another copy without reset_handlers proc = util.startProgram(["/bin/sh", testscript.name], reset_handlers=False) with timer(5): # Kill with SIGPIPE, then SIGTERM, and make sure SIGTERM was the one # that worked. proc.send_signal(signal.SIGPIPE) proc.terminate() proc.communicate() self.assertEqual(proc.returncode, -(signal.SIGTERM))
def run(fn, name, imshow=False): print print "---", name for (prefix, wrapper) in [('parakeet-', jit), ('numba-', autojit)]: try: wrapped_fn = wrapper(fn) with timer(prefix + name + '-compile', True): wrapped_fn(image[:1, :1], k) with timer(prefix + name, False): result = wrapped_fn(image, k) if imshow: import pylab pylab.imshow(image) pylab.figure() pylab.imshow(result) pylab.figure() if scipy_result is not None: pylab.imshow(scipy_result) pylab.show() if not running_pypy and scipy_result is not None: assert allclose(result, scipy_result) except KeyboardInterrupt: raise except: print "%s failed" % (prefix + name) import sys print sys.exc_info()[1]
def exec_readlines_test_filter_stderr(self): """Test execReadlines and filter_stderr.""" # Test that stderr is normally included with tempfile.NamedTemporaryFile(mode="w+t") as testscript: testscript.write("""#!/bin/sh echo "one" echo "two" >&2 echo "three" exit 0 """) testscript.flush() with timer(5): rl_iterator = util.execReadlines("/bin/sh", [testscript.name]) self.assertEqual(next(rl_iterator), "one") self.assertEqual(next(rl_iterator), "two") self.assertEqual(next(rl_iterator), "three") self.assertRaises(StopIteration, rl_iterator.__next__) # Test that filter stderr removes the middle line with tempfile.NamedTemporaryFile(mode="w+t") as testscript: testscript.write("""#!/bin/sh echo "one" echo "two" >&2 echo "three" exit 0 """) testscript.flush() with timer(5): rl_iterator = util.execReadlines("/bin/sh", [testscript.name], filter_stderr=True) self.assertEqual(next(rl_iterator), "one") self.assertEqual(next(rl_iterator), "three") self.assertRaises(StopIteration, rl_iterator.__next__)
def setup(self): """Sets up matrices for Hamiltonian construction.""" with timer("Generating states"): if self.hamiltonian.endswith("relevant"): self.states = States(self.n, basis=Basis.N_L_ML_MS_RELEVANT) print("Loaded relevant N L ML MS states.") else: self.states = States(self.n, basis=Basis.N_L_ML_MS) print("Loaded N L ML MS states.") with timer("Loading Hamiltonian"): mat_1, mat_1_zeeman, mat_2, mat_2_minus, mat_2_plus = load_hamiltonian( self.hamiltonian) mat_2_combination = mat_2_plus + mat_2_minus with timer("Loading transformations"): transform_1 = load_transformation(self.n, Basis.N_L_J_MJ_RELEVANT, Basis.N_L_ML_MS_RELEVANT) with timer("Applying transformation to nlmlms"): mat_1 = transform_basis(mat_1, transform_1) mat_1_zeeman = transform_basis(mat_1_zeeman, transform_1) mat_2 = transform_basis(mat_2, transform_1) # mat_2_plus = transform_basis(mat_2_plus, transform_1) # mat_2_minus = transform_basis(mat_2_minus, transform_1) mat_2_combination = transform_basis(mat_2_combination, transform_1) self.mat_1 = mat_1 self.mat_1_zeeman = mat_1_zeeman self.mat_2 = mat_2 # self.mat_2_plus = mat_2_plus # self.mat_2_minus = mat_2_minus self.mat_2_combination = mat_2_combination
def exec_readlines_test_filter_stderr(self): """Test execReadlines and filter_stderr.""" # Test that stderr is normally included with tempfile.NamedTemporaryFile(mode="w+t") as testscript: testscript.write("""#!/bin/sh echo "one" echo "two" >&2 echo "three" exit 0 """) testscript.flush() with timer(5): rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name]) self.assertEqual(next(rl_iterator), "one") self.assertEqual(next(rl_iterator), "two") self.assertEqual(next(rl_iterator), "three") self.assertRaises(StopIteration, rl_iterator.__next__) # Test that filter stderr removes the middle line with tempfile.NamedTemporaryFile(mode="w+t") as testscript: testscript.write("""#!/bin/sh echo "one" echo "two" >&2 echo "three" exit 0 """) testscript.flush() with timer(5): rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name], filter_stderr=True) self.assertEqual(next(rl_iterator), "one") self.assertEqual(next(rl_iterator), "three") self.assertRaises(StopIteration, rl_iterator.__next__)
def inference_with_graph(graph_def, image, labels): """ Predict for single images """ with tf.Graph().as_default() as graph: with tf.Session() as sess: # Import a graph_def into the current default Graph print("import graph") input_, predictions = tf.import_graph_def( graph_def, name='', return_elements=input_output_placeholders) timer.timer('predictions.eval') time_res = [] for i in range(10): p_val = predictions.eval(feed_dict={input_: [image]}) index = np.argmax(p_val) label = labels[index] dt = timer.timer('{0}: label={1}'.format(i, label)) time_res.append(0) #print('index={0}, label={1}'.format(index, label)) print('mean time = {0}'.format(np.mean(time_res))) return index
def start_program_reset_handlers_test(self): """Test the reset_handlers parameter of startProgram.""" with tempfile.NamedTemporaryFile() as testscript: testscript.write("""#!/bin/sh # Just hang out and do nothing, forever while true ; do sleep 1 ; done """) testscript.flush() # Start a program with reset_handlers proc = iutil.startProgram(["/bin/sh", testscript.name]) with timer(5): # Kill with SIGPIPE and check that the python's SIG_IGN was not inheritted # The process should die on the signal. proc.send_signal(signal.SIGPIPE) proc.communicate() self.assertEqual(proc.returncode, -(signal.SIGPIPE)) # Start another copy without reset_handlers proc = iutil.startProgram(["/bin/sh", testscript.name], reset_handlers=False) with timer(5): # Kill with SIGPIPE, then SIGTERM, and make sure SIGTERM was the one # that worked. proc.send_signal(signal.SIGPIPE) proc.terminate() proc.communicate() self.assertEqual(proc.returncode, -(signal.SIGTERM))
def fetch_people_page(conn, username, page = 1): url = "/people/{}/answers".format(username) url_page = "{}?page={:d}".format(url, page) print("\n{}\t".format(url_page), end='') sys.stdout.flush() timer.timer() try: conn.request("GET", url_page) except socket.timeout as e: print('wow! timeout') raise e response = conn.getresponse() t = timer.timer() avg = int(get_average(t, 'user page')) code = response.status print("[{}]\t{} ms\tAvg: {} ms".format(code, t, avg)) if code == 404: slog("user username fetch fail, code code") dbhelper.update_user_by_name(username, {'fetch': dbhelper.FETCH_FAIL}) print( "没有这个用户", username) return None if code != 200: slog("user username fetch fail, code code") dbhelper.update_user_by_name(username, {'fetch': dbhelper.FETCH_FAIL}) print( "奇奇怪怪的返回码", code) return None content = response.read() return content
def run(fn, name, imshow=False): print print "---", name for (prefix, wrapper) in [('parakeet-', jit), ('numba-', autojit)]: try: wrapped_fn = wrapper(fn) with timer(prefix + name + '-compile', True): wrapped_fn(image[:1, :1], k) with timer(prefix + name, False): result = wrapped_fn(image, k) if imshow: import pylab pylab.imshow(image) pylab.figure() pylab.imshow(result) pylab.figure() pylab.imshow(scipy_result) pylab.show() if not running_pypy: assert allclose(result, scipy_result) except KeyboardInterrupt: raise except: print "%s failed" % (prefix+name) import sys print sys.exc_info()[1]
def saveAnswer(conn, username, answer_link_list, dblock): regex = re.compile(r'^/question/(\d+)/answer/(\d+)') success_ratio = None avg = None for url in answer_link_list: matches = regex.search(url) if matches is None: raise Exception('url not good') qid = matches.group(1) aid = matches.group(2) slog("\t{}".format(url)) sys.stdout.flush() timer.timer('saveAnswer') content = get_url(url) if content is None: continue success_ratio = get_average(0 if content is None else 1, 'success_ratio') t = timer.timer('saveAnswer') avg = int(get_average(t)) slog("\t{} ms".format(t)) if len(content) == 0: slog("content is empty\n") slog("url [code] empty") return False question, descript, content, vote = parse_answer_pure(content) slog("{}\t^{}\t{}".format(url, vote, question)) with dblock: dbhelper.saveQuestion(qid, question, descript) dbhelper._saveAnswer(aid, qid, username, content, vote) if success_ratio is not None and avg is not None: success_ratio = int(success_ratio*100) print("\tAvg: {} ms\tsuccess_ratio: {}%".format(avg, success_ratio))
def rdt_send_3(self, new_socket): queue = [] seq = 0 inputthread = runScriptThread(self.userinput, queue) inputthread.start() while True: while True: lock = threading.Lock() time.sleep(1) lock.acquire() if len(queue) != 0: lock.release() break lock.release() lock = threading.Lock() lock.acquire() data = queue[0] queue.remove(queue[0]) lock.release() sndpkt = self.make_pkt(data, True, seq) self.udt_send(new_socket, sndpkt) print("Start timer") atimer = timer() atimer.starttimer(3) rcvpkt = [] rcvthread = runScriptThread(self.rdt_rcv, new_socket, rcvpkt) rcvthread.start() while True: print("iteration") if not len(rcvpkt) == 0: rcvpkt = rcvpkt[0] unseq = 0 if seq == 1 else 1 if self.corrupt(rcvpkt) or self.isack(rcvpkt, unseq): rcvpkt = [] rcvthread = runScriptThread(self.rdt_rcv, new_socket, rcvpkt) rcvthread.start() continue else: break if atimer.isTimeOut(): self.udt_send(new_socket, sndpkt) atimer = timer() atimer.starttimer(10) continue print(rcvpkt) print(rcvpkt.data) if seq == 0: seq = 1 else: seq = 0
def exec_readlines_test_signals(self): """Test execReadlines and signal receipt.""" # ignored signal old_HUP_handler = signal.signal(signal.SIGHUP, signal.SIG_IGN) try: with tempfile.NamedTemporaryFile(mode="wt") as testscript: testscript.write( """#!/bin/sh echo "one" kill -HUP $PPID echo "two" echo -n "three" exit 0 """ ) testscript.flush() with timer(5): rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name]) self.assertEqual(next(rl_iterator), "one") self.assertEqual(next(rl_iterator), "two") self.assertEqual(next(rl_iterator), "three") self.assertRaises(StopIteration, rl_iterator.__next__) finally: signal.signal(signal.SIGHUP, old_HUP_handler) # caught signal def _hup_handler(signum, frame): pass old_HUP_handler = signal.signal(signal.SIGHUP, _hup_handler) try: with tempfile.NamedTemporaryFile(mode="wt") as testscript: testscript.write( """#!/bin/sh echo "one" kill -HUP $PPID echo "two" echo -n "three" exit 0 """ ) testscript.flush() with timer(5): rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name]) self.assertEqual(next(rl_iterator), "one") self.assertEqual(next(rl_iterator), "two") self.assertEqual(next(rl_iterator), "three") self.assertRaises(StopIteration, rl_iterator.__next__) finally: signal.signal(signal.SIGHUP, old_HUP_handler)
def test_exec_readlines_signals(self): """Test execReadlines and signal receipt.""" # ignored signal old_HUP_handler = signal.signal(signal.SIGHUP, signal.SIG_IGN) try: with tempfile.NamedTemporaryFile(mode="wt") as testscript: testscript.write("""#!/bin/sh echo "one" kill -HUP $PPID echo "two" echo -n "three" exit 0 """) testscript.flush() with timer(5): rl_iterator = util.execReadlines("/bin/sh", [testscript.name]) assert next(rl_iterator) == "one" assert next(rl_iterator) == "two" assert next(rl_iterator) == "three" with pytest.raises(StopIteration): rl_iterator.__next__() finally: signal.signal(signal.SIGHUP, old_HUP_handler) # caught signal def _hup_handler(signum, frame): pass old_HUP_handler = signal.signal(signal.SIGHUP, _hup_handler) try: with tempfile.NamedTemporaryFile(mode="wt") as testscript: testscript.write("""#!/bin/sh echo "one" kill -HUP $PPID echo "two" echo -n "three" exit 0 """) testscript.flush() with timer(5): rl_iterator = util.execReadlines("/bin/sh", [testscript.name]) assert next(rl_iterator) == "one" assert next(rl_iterator) == "two" assert next(rl_iterator) == "three" with pytest.raises(StopIteration): rl_iterator.__next__() finally: signal.signal(signal.SIGHUP, old_HUP_handler)
def exec_readlines_test_signals(self): """Test execReadlines and signal receipt.""" # ignored signal old_HUP_handler = signal.signal(signal.SIGHUP, signal.SIG_IGN) try: with tempfile.NamedTemporaryFile() as testscript: testscript.write("""#!/bin/sh echo "one" kill -HUP $PPID echo "two" echo -n "three" exit 0 """) testscript.flush() with timer(5): rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name]) self.assertEqual(rl_iterator.next(), "one") self.assertEqual(rl_iterator.next(), "two") self.assertEqual(rl_iterator.next(), "three") self.assertRaises(StopIteration, rl_iterator.next) finally: signal.signal(signal.SIGHUP, old_HUP_handler) # caught signal def _hup_handler(signum, frame): pass old_HUP_handler = signal.signal(signal.SIGHUP, _hup_handler) try: with tempfile.NamedTemporaryFile() as testscript: testscript.write("""#!/bin/sh echo "one" kill -HUP $PPID echo "two" echo -n "three" exit 0 """) testscript.flush() with timer(5): rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name]) self.assertEqual(rl_iterator.next(), "one") self.assertEqual(rl_iterator.next(), "two") self.assertEqual(rl_iterator.next(), "three") self.assertRaises(StopIteration, rl_iterator.next) finally: signal.signal(signal.SIGHUP, old_HUP_handler)
def upload_files(folder_name): # list images images_folder = dir_path + 'files/' + folder_name images = [f for f in listdir(images_folder) if isfile(join(images_folder, f))] # add start time timer(f"yandex s3 start upload {folder_name}") upload_entry_list = [] for image in images: f = open(images_folder + image, 'rb') response = client.upload_fileobj(f, BUCKET, f"{folder_name}{image}") #add stop time timer(f"yandex s3 stop upload {folder_name}")
def get_objects_from_folder(list_, folder_name, thread, node_count): client = session.client(service_name="s3", endpoint_url=YANDEX_S3_ENDPOINT, aws_access_key_id=ACCESS_KEY_ID, aws_secret_access_key=SECRET_ACCESS_KEY) timer(f"yandex s3 start get objects {BUCKET} node {thread}") for file_ in list_: if file_['Key'] == folder_name: continue try: downloaded = client.get_object(Bucket=BUCKET, Key=file_['Key']) except Exception as e: with open("results.txt", 'a+') as results: results.writelines(f"Error raised trying to download file from s3 in node {thread}. Max retries achived\n") continue timer(f"yandex s3 stop get objects {BUCKET} node {thread}") print_times(f"yandex s3 start get objects {BUCKET} node {thread}", f"yandex s3 stop get objects {BUCKET} node {thread}")
def download_files_from_folder(list_, folder_name, thread, node_count): dropbox_ = create_dropbox_instance() timer(f"dropbox start download files {folder_name} node {thread}") for file_ in list_: try: meta, res = dropbox_.files_download(path=file_.path_display) except Exception as e: with open("results.txt", 'a+') as results: results.writelines( f"Error raised trying to download file from dropbox in node {thread}. Max retries achived\n" ) continue timer(f"dropbox stop download files {folder_name} node {thread}") print_times(f"dropbox start download files {folder_name} node {thread}", f"dropbox stop download files {folder_name} node {thread}")
def main(): # Handle command line arguments args = handle_args() # Initialize the configuration object config = MultiEngineQueryConfig(args) # Get a logger instance logger = logging.getLogger('logger') start_time = time.time() logger.info( '================ Starting Multi-Engine Query script ================') # Start the process finished = run_multi_engine_query(config) end_time = time.time() total = timer(start_time, end_time) logger.info( '================ Multi-Engine Query script execution completed in {0} ================' .format(total)) # Send the email if finished completely, or if no Connected Engine Appliacnes were found.. # Otherwise we had no active metris to process, so ignore. if finished: send_mail(logger, config)
def exec_readlines_auto_kill_test(self): """Test execReadlines with reading only part of the output""" with tempfile.NamedTemporaryFile() as testscript: testscript.write("""#!/bin/sh # Output forever while true; do echo hey done """) testscript.flush() with timer(5): rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name]) # Save the process context proc = rl_iterator._proc # Read two lines worth self.assertEqual(rl_iterator.next(), "hey") self.assertEqual(rl_iterator.next(), "hey") # Delete the iterator and wait for the process to be killed del rl_iterator proc.communicate() # Check that the process is gone self.assertIsNotNone(proc.poll())
def start_qt_app(config): import sys from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QMessageBox from quamash import QEventLoop from ui.main import Window from ui.qt_gui_connection import qSignal app = QApplication(sys.argv) loop = QEventLoop(app) asyncio.set_event_loop(loop) if not QSystemTrayIcon.isSystemTrayAvailable(): QMessageBox.critical(None, "Systray", "I couldn't detect any system tray on this system.") sys.exit(1) QApplication.setQuitOnLastWindowClosed(False) gui_connection = qSignal() window = Window(gui_connection) def closeApp(): print("Close app signal") for task in asyncio.Task.all_tasks(): print(task) task.cancel() loop.stop() gui_connection.closeApp.connect(closeApp) with loop: #asyncio.run_coroutine_threadsafe(timer(loop, config, gui_connection), loop) try: loop.run_until_complete(timer(loop, config, gui_connection)) except asyncio.CancelledError: pass
def execute_json_api(self, api): """ Executes the specified API against the Appliance and retusns the resulting json objecs as a list of dict objects api = The api after the fqdn of the Appliance to execute """ func_name = self.__class__.__name__ + '.' + inspect.currentframe( ).f_code.co_name results = [] api = self._format_api(api) if self.debug_mode(): start_time = time.time() logger.debug("{} - api: {}".format(func_name, api)) # Execute the API try: api_response = self._session.get(api, stream=False, verify=False) if self.debug_mode(): logger.debug('{} - api_response.status_code: {}'.format( func_name, api_response.status_code)) except requests.exceptions.ConnectionError as e: logger.error("{} - Unable to get results from Nexthink. {}".format( func_name, e)) return None # Process and parse the response results = [] if api_response.ok: results = api_response.json() if self.debug_mode(): end_time = time.time() logger.debug("{} - Retrieved {} objects in {}".format( func_name, len(results), timer(start_time, end_time))) return results
def upload_files(folder_name): # list images images_folder = dir_path + 'files/' + folder_name images = [ f for f in listdir(images_folder) if isfile(join(images_folder, f)) ] # add start time timer(f"dropbox start upload {folder_name}") upload_entry_list = [] for image in images: f = open(images_folder + image, 'rb') dropbox_.files_upload(f=f.read(), path=f"/{folder_name}{image}") #add stop time timer(f"dropbox stop upload {folder_name}")
def execute_remote_action(self, remote_action_id, device_list): func_name = self.__class__.__name__ + '.' + inspect.currentframe( ).f_code.co_name results = [] if self.debug_mode(): start_time = time.time() logger.debug( "{} - remote_action_id: {}, device_list count: {}".format( func_name, remote_action_id, len(device_list))) # Construct payload payload = { "RemoteActionUid": remote_action_id, "DeviceUids": [d.device_uid for d in device_list] } if self.debug_mode(): logger.debug('{} - Payload before calling RA: {}'.format( func_name, payload)) response = self.post_json_api(self._act_api, payload) if len(response) == 0: response = None if self.debug_mode(): end_time = time.time() logger.debug( "{} - Executed Remote Action on {} objects in {}. Response: {}" .format(func_name, len(device_list), timer(start_time, end_time), response)) return response
def get_engine_list(self, engine_port=1671, only_connected=False): """Retursn a list of EngineAppliance instances from this Portal.""" func_name = self.__class__.__name__ + '.' + inspect.currentframe( ).f_code.co_name results = [] if self.debug_mode(): start_time = time.time() logger.debug('{} - Start.'.format(func_name)) response = self.execute_json_api(self._list_engines_api) # Create an EngineAppliance instance for each result for resp in response: if self.debug_mode(): logger.debug('{0} - Processing resp: {1!r}'.format( func_name, resp)) # If only_active, make sure the engine is active before creating if ((not only_connected) or (only_connected and (resp['status'] == 'CONNECTED'))): eng = EngineAppliance(resp['address'], resp['name'], engine_port, self._credentials) results.append(eng) else: if self.debug_mode(): logger.debug( '{} - Skipping disconnected Engine: {}'.format( func_name, resp['name'])) if self.debug_mode(): end_time = time.time() logger.debug( '{} - Execute of List Engines API returned {} objects in {}. Results: {}' .format(func_name, len(results), timer(start_time, end_time), results)) return results
async def with_blocking_function(): """ブロックしてしまう関数はラップすれば同様に扱える""" with timer("with blocking function"): results = await asyncio.gather(block_say(2, "hello"), block_say(1, "world")) print("results:", results)
def big_question(method): A = np.array([ [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1], [0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0], [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0], [500, 0, 0, 700, 0, 0, 600, 0, 0, 400, 0, 0, 900, 0, 0], [0, 500, 0, 0, 700, 0, 0, 600, 0, 0, 400, 0, 0, 900, 0], [0, 0, 500, 0, 0, 700, 0, 0, 600, 0, 0, 400, 0, 0, 900] ]) b = np.array([12, 18, 10, 20, 16, 25, 13, 7000, 9000, 5000]) A_ = np.array([ [3, -2, 0, 3, -2, 0, 3, -2, 0, 3, -2, 0, 3, 2, 2], [5, 0, -6, 5, 0, -6, 5, 0, -6, 5, 0, -6, -1, 7, 4] ]) b_ = np.array([0, 0]) c = np.array([320, 320, 320, 400, 400, 400, 360, 360, 360, 290, 290, 290, 100, 100, 100]) * -1 print('In big question:') print() with timer(method): res = linprog(c, A_ub=A, b_ub=b, A_eq = A_, b_eq=b_, bounds=(0, None), method=method) print('Optimal value: ', res.fun, '\nX:', res.x) print(""" --------------------------------- """)
def __init__(self): threading.Thread.__init__(self) logger.info("Starting Steuerungthread as " + threading.currentThread().getName()) self.t_stop = threading.Event() self.read_config() self.system = {"ModeReset":"2:00"} self.set_hw() if(self.mixer_addr != -1): #self.mix = mixer(addr=self.mixer_addr) self.mix = mixer() self.mix.run() self.w1 = tempsensors.onewires() self.w1_slaves = self.w1.enumerate() self.Timer = timer(self.timerfile) #Starting Threads self.set_pumpe() self.short_timer() self.timer_operation() self.udpServer() self.udp = udpBroadcast() self.garagenmeldung(self.garagenmelder) self.broadcast_value() self.udpRx()
def load_meanflow(self, path, FileList=None, OutFile=None): exists = os.path.isfile(path + 'MeanFlow/MeanFlow.h5') if exists: df = pd.read_hdf(path + 'MeanFlow/MeanFlow.h5') df = df.drop_duplicates(keep='last') grouped = df.groupby(['x', 'y', 'z']) df = grouped.mean().reset_index() else: equ = ['{|gradp|}=sqrt(ddx({<p>})**2+ddy({<p>})**2+ddz({<p>})**2)'] # nfiles = np.size(os.listdir(path + 'TP_stat/')) print('try to calculate data') with timer('load mean flow from tecplot data'): if FileList is None: df = p2p.ReadAllINCAResults(path + 'TP_stat/', path + 'MeanFlow/', SpanAve=True, Equ=equ, OutFile='MeanFlow') else: df = p2p.ReadAllINCAResults(path + 'TP_stat/', path + 'MeanFlow/', FileName=FileList, SpanAve=True, Equ=equ, OutFile='MeanFlow') print('done with saving data') self._data_field = df
def test_exec_readlines_auto_kill(self): """Test execReadlines with reading only part of the output""" with tempfile.NamedTemporaryFile(mode="w+t") as testscript: testscript.write("""#!/bin/sh # Output forever while true; do echo hey done """) testscript.flush() with timer(5): rl_iterator = util.execReadlines("/bin/sh", [testscript.name]) # Save the process context proc = rl_iterator._proc # Read two lines worth assert next(rl_iterator) == "hey" assert next(rl_iterator) == "hey" # Delete the iterator and wait for the process to be killed del rl_iterator proc.communicate() # Check that the process is gone assert proc.poll() is not None
def extract_point(InFolder, OutFolder, timezone, xy, skip=1, col=None): if col is None: col = ["u", "v", "w", "p", "vorticity_1", "vorticity_2", "vorticity_3"] dirs = sorted(os.listdir(InFolder)) data = pd.read_hdf(InFolder + dirs[0]) NewFrame = data.loc[data["x"] == xy[0]] TemFrame = NewFrame.loc[NewFrame["y"] == xy[1]] ind = TemFrame.index.values[0] xarr = np.zeros((np.size(timezone), np.size(col))) j = 0 for i in range(np.size(dirs)): if i % skip == 0: with timer("Extracting probes"): frame = pd.read_hdf(InFolder + dirs[i]) frame = frame.iloc[ind] xarr[j, :] = frame[col].values j = j + 1 probe = np.hstack((timezone.reshape(-1, 1), xarr)) col.insert(0, "t") np.savetxt( OutFolder + "x" + str(xy[0]) + "y" + str(xy[1]) + ".dat", probe, fmt="%.8e", delimiter=" ", header=", ".join(col), )
def _stepFade(self, final=False): #Figure out what step we're on secondsSoFar = (datetime.now() - self.fadeStartTime).total_seconds() prog = min(secondsSoFar / self.fadeSeconds, 1) self.currentStep = int(round(prog * self.numSteps)) #print ("%3.2f%%, step %d/%d"%(prog, self.currentStep, self.numSteps)) #set colors to correct output self.r = self.last_r + int(prog * self.delta_r) self.g = self.last_g + int(prog * self.delta_g) self.b = self.last_b + int(prog * self.delta_b) self.apply() # Are we done yet? if self.currentStep < self.numSteps: # if not, set up the next timeout self.fadeTimer = timer( timeout = datetime.now() + timedelta(seconds=self.secondsPerStep), callback = self._stepFade, res = self.secondsPerStep) self.fadeTimer.start() elif not final: #This is the second-to-last step! # make sure we get to do the last step (because we # don't want rounding or CPU delays to make us stop at 96.7%.. self._stepFade(final=True) else: # call the callback if there is one, then reset it so # it doens't get called again if it's not set in the future self.fadeCallback and self.fadeCallback()
def walk (self, x, y): assert(x == 1 or x == -1 or x == 0) assert(y == 1 or y == -1 or y == 0) if x == 0 and y == 0: return elif self.timer is None or self.timer.is_complete(): self.x += x self.y += y self.__x = x self.__y = y self.__offset_x = common.TILE_SIZE * x self.__offset_y = common.TILE_SIZE * y if x < 0: self.ori = 'left' anim = self.left elif x > 0: self.ori = 'right' anim = self.right elif y > 0: self.ori = 'down' anim = self.down else: self.ori = 'up' anim = self.up anim.play(WALK_MOVEMENT_TIME) self.timer = timer(WALK_MOVEMENT_TIME)
def playMusic(self): if len(self.playlist) == 0: return 0 if self.checker != '': if self.checker.check == False: return 0 # self.cursor.execute("SELECT (playList) FROM room WHERE idx=%s", (self.idx, )) # play = list(self.cursor.fetchall()[0]) now = self.playlist.pop(0) self.nowPlay = now[0] self.window.songInfo.setText("now Playing - " + str(self.nowPlay)) url = now[1] video = pafy.new(url) bestAudio = video.getbestaudio() length = video.duration length = length.split(":") length = int(length[0])*60*60 + int(length[1])*60 + int(length[2]) audioUrl = str(bestAudio.url) option = webdriver.ChromeOptions() option.add_argument("headless") # chromeDriverPath = "D:/Desktop/PYQT/IGRUS_Contest_2020/chromedriver.exe" # self.playPage = webdriver.Chrome(chromeDriverPath, options = option) if getattr(sys, 'frozen', False): chromedriver_path = os.path.join(sys, '_MEIPASS', "chromedriver.exe") self.playPage = webdriver.Chrome(chromedriver_path, options = option) else: self.playPage = webdriver.Chrome("D:/Desktop/PYQT/IGRUS_Contest_2020/chromedriver.exe", options = option) self.playPage.get(audioUrl) self.timer = timer(length) self.checker = checker(self.timer) self.timer.start() self.checker.start()
def test_still_running(): with timer(5): # Run something forever so we can kill it proc = util.startProgram(["/bin/sh", "-c", "while true; do sleep 1; done"]) WatchProcesses.watch_process(proc, "test1") proc.kill() # Wait for the SIGCHLD signal.pause()
def test(): time = timer() print(time.startTimer()) expression = peg.genAddPFE(1000000) print(expression) result = solvePF(expression) print(result) print(time.endTimer())
def fade(self, handle, ftime): if type(handle) is int: return tmr = timer.timer() while handle.handle.volume != -100: if tmr.elapsed >= ftime: tmr.restart() handle.handle.volume -= 1 sp.p.destroy_sound(handle)
def test_still_running(): with timer(5): # Run something forever so we can kill it proc = iutil.startProgram(["/bin/sh", "-c", "while true; do sleep 1; done"]) iutil.watchProcess(proc, "test1") proc.kill() # Wait for the SIGCHLD signal.pause()
def test_sqlite3(n=100000, dbname='sqlite3.db'): conn = init_sqlite3(dbname) c = conn.cursor() with timer("SQLite"): for i in range(n): row = ('NAME ' + str(i), ) c.execute("INSERT INTO customer (name) VALUES (?)", row) conn.commit()
def form_mash(au_id, lot): from timer import timer text = '' price = 0 lot_id = '' channel = 'au' status = 'None' modifiers = 'None' stamp_now = time_now() - 24 * 60 * 60 stamp = stamp_now - 10 for g in lot.split('\n'): for i in search_array: search = re.search(search_array.get(i), g) if search: if i == 'Лот #': text += i + search.group(1) + ' : ' + objects.bold( search.group(2)) + '\n' lot_id = search.group(1) elif i == 'Модификаторы:': text += i + '\n{0}' modifiers = '' elif i == 'Цена: ': text += i + search.group(1) + ' 👝\n' price = int(search.group(1)) elif i == 'cw3': price = int(search.group(1)) channel = i elif i == 'Срок: ': stamp = timer(search) text += i + str(time_mash(stamp)) + '\n' elif i == 'Статус: ': text += i if search.group(1) in ['Cancelled', 'Failed', 'Отменен']: status = 'Отменен' elif search.group(1) == '#активен': status = '#активен' elif search.group(1) == '#active': if stamp < stamp_now: status = 'Закончился' else: status = '#активен' else: status = 'Закончился' text += status if status == '#активен': text += '\n\n/bet_{1} /l_{1}' else: if search.group(1) == 'None': text += i + 'Нет\n' else: text += i + search.group(1) + '\n' if modifiers != 'None' and g.startswith(' '): modifiers += g + '\n' if channel == 'au': text = text.format(modifiers, lot_id) return [au_id, text, price, status] else: return [au_id, price, status]
def test_sqlalchemy_orm_pk_given(n=100000): init_sqlalchemy() with timer("SQLAlchemy ORM pk given"): for i in range(n): customer = Customer(id=i + 1, name="NAME " + str(i)) DBSession.add(customer) if i % 1000 == 0: DBSession.flush() DBSession.commit()
def main(input, multi, compression): if os.path.isfile(input) or os.path.isdir(input): start = timer() if os.path.isdir(input): list = os.listdir(input) files = [] for l in list: if os.path.splitext(l)[1] == ".psd": l = os.path.join(input, l) if os.path.isfile(l): files.append(l) else: files = [input] print("Found {} files to convert.".format(len(files))) for f in files: layers = extract_layers(f) i = 0 for layer in layers.split("\n"): i += 1 layer = layer.strip() if layer == "": print("Skipping empty layer name. Likely flattened compatibility layer.") else: print("layer {}: {}".format(i, layer)) tmpfile = export_layer(i, layer, f, compression) exr_compression(tmpfile, compression) cleanup(tmpfile) if multi: exr_multipart(layers.split("\n"), f) else: print("Not a PSD document. Skipping.") timer(start, "PSD To EXR Conversion")
def pvalue(log_pmf, s0, L, desired_beta): """Compute $log((exp(log_pmf)**L)[s0:])$, such that the relative error to the exact answer is less than or equal to $desired_beta$.""" total_len, _ = utils.iterated_convolution_lengths(len(log_pmf), L) if s0 >= total_len: return NEG_INF _, p_lower_preshift, p_upper_preshift = _bounds(log_pmf, log_pmf, 0, 0.0, s0, L, desired_beta) sfft_good_preshift, sfft_pval_preshift = _check_sfft_pvalue(p_lower_preshift, p_upper_preshift, desired_beta) if sfft_good_preshift: logging.debug(' pre-shift sfft worked %.20f', sfft_pval_preshift) return sfft_pval_preshift with timer('computing theta'): theta = _compute_theta(log_pmf, s0, L) logging.debug('raw theta %s', theta) # TODO: too-large or negative theta causes numerical instability, # so this is a huge hack theta = utils.clamp(theta, 0, THETA_LIMIT) shifted_pmf, log_mgf = utils.shift(log_pmf, theta) beta = desired_beta / 2.0 with timer('bounds'): log_delta, p_lower, p_upper = _bounds(log_pmf, shifted_pmf, theta, log_mgf, s0, L, desired_beta) sfft_good, sfft_pval = _check_sfft_pvalue(p_lower, p_upper, desired_beta) logging.debug('theta %s, log_mgf %s, beta %s, log delta %s', theta, log_mgf, beta, log_delta) if sfft_good: logging.debug(' sfft worked %.20f', sfft_pval) return sfft_pval delta = np.exp(log_delta) conv = conv_power(shifted_pmf, L, beta, delta) pval = utils.log_sum(utils.unshift(conv, theta, (log_mgf, L))[s0:]) logging.debug(' sis pvalue %.20f', pval) return pval
def consumer(index, filename, start, end, u): parser = apparser.apparser() t = timer.timer() # print('started job %d' % index) i = 0 t2 = timer.timer() # map(parser.proto, iterate_log(filename, start, end)) # t.stop('job %d' % index, i) # return (index, i) for l in iterate_log(filename, start, end): # if (i % 100001) == 0 and not i == 0: # t2.stop('[%d] parsed %10d lines' % (index, i), i) i += 1 # r = apparser._parse2(l) r = parser.proto(l) # break # print(parser) # u.get(parser.vhost, index) t.stop("job %d" % index, i) return (index, i)
def main(): # Get forecasts and save to DB def get_forecasts(): data = wfs.wfs().get_all_sources() dbase = db.db() dbase.insert_wfs_data(data) dbase.close_connection() # Read data from sensors and save to DB def read_sensors(): data = sensors.sensors().read_all() dbase = db.db() dbase.insert_sensor_data(data) dbase.close_connection() # Screensaver scr = Thread(target = pir.screensaver) scr.daemon = True scr.start() ##test ##get_forecasts() ##read_sensors() # get data from forecasts each hour repeat_get_forecasts = timer.timer(3600, get_forecasts) repeat_get_forecasts.start() # get data from sensors each minute repreat_read_sensors = timer.timer(60, read_sensors) repreat_read_sensors.start() # Send sensor data to openweathermap.com #response = wfs.wfs().send_to_owm() #print (response) # Interface root = gui.Interface() root.mainloop()
def exec_readlines_test_normal_output(self): """Test the output of execReadlines.""" # Test regular-looking output with tempfile.NamedTemporaryFile(mode="w+t") as testscript: testscript.write( """#!/bin/sh echo "one" echo "two" echo "three" exit 0 """ ) testscript.flush() with timer(5): rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name]) self.assertEqual(next(rl_iterator), "one") self.assertEqual(next(rl_iterator), "two") self.assertEqual(next(rl_iterator), "three") self.assertRaises(StopIteration, rl_iterator.__next__) # Test output with no end of line with tempfile.NamedTemporaryFile(mode="w+t") as testscript: testscript.write( """#!/bin/sh echo "one" echo "two" echo -n "three" exit 0 """ ) testscript.flush() with timer(5): rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name]) self.assertEqual(next(rl_iterator), "one") self.assertEqual(next(rl_iterator), "two") self.assertEqual(next(rl_iterator), "three") self.assertRaises(StopIteration, rl_iterator.__next__)
def __init__(self,website,query,max_results = 500): self.base_url = google_search_url(website,query) self.url = self.base_url self.max_results = max_results self.max_pages = math.ceil(max_results/100.) self.page_number = 1 self.results_list = [] self.timer = timer.timer(mean = 3.13,sd = 0.62, floor = 15.12, ceiling = 46.1) self.website = website self.query = query self.driver = setup_google_driver() self.open_first_url() time.sleep(1) self.terminated = False
def start_program_stdout_test(self): """Test redirecting stdout with startProgram.""" marker_text = "yo wassup man" # Create a temporary file that will be written by the program with tempfile.NamedTemporaryFile() as testfile: # Open a new copy of the file so that the child doesn't close and # delete the NamedTemporaryFile stdout = open(testfile.name, 'w') with timer(5): proc = iutil.startProgram(["/bin/echo", marker_text], stdout=stdout) proc.communicate() # Rewind testfile and look for the text testfile.seek(0, os.SEEK_SET) self.assertEqual(testfile.read().strip(), marker_text)
def __init__(self, cf): super(TaskBarIcon, self).__init__() self.__config__ = cf self.mute = False self.SetIcon(res.icon.GetIcon(), self.__config__.result['name']) # Set icon try: # Choose an startup message msg = random.choice(self.__config__.findMessageByCondition({'startup': True})) except IndexError: msg = [] if msg != []: self.show(msg.chooseMessage()) # Set a timer self.__timer__ = timer.timer(self.__config__, self) self.__timer__.start()
def watch_process_test(self): """Test watchProcess""" def test_still_running(): with timer(5): # Run something forever so we can kill it proc = iutil.startProgram(["/bin/sh", "-c", "while true; do sleep 1; done"]) iutil.watchProcess(proc, "test1") proc.kill() # Wait for the SIGCHLD signal.pause() self.assertRaises(iutil.ExitError, test_still_running) # Make sure watchProcess checks that the process has not already exited with timer(5): proc = iutil.startProgram(["true"]) proc.communicate() self.assertRaises(iutil.ExitError, iutil.watchProcess, proc, "test2")
def main(f): i = sp.arange(1500, 2500 + 200, 200) y = [] with timer() as t: for n in i: if f == MatrixAdd: t.time(f, sp.rand(n, n), sp.rand(n, n)) elif f == MatrixSolve: t.time(f, sp.rand(n, n), sp.rand(n, 1)) else: t.time(f, sp.rand(n, n)) y.append(t.results[0][0]) X = sp.row_stack([sp.log(i), sp.ones_like(i)]) sol = la.lstsq(X.T, sp.log(y)) print sol[0][0] plt.loglog(i, y) plt.show()
def iterate_log(filename, start, end): mapping = timer.timer() fd = open(filename) mm = mmap.mmap(fd.fileno(), 0, prot=mmap.PROT_READ) eob = end end = -1 while True: if start == eob: break end = mm.find(b"\n", start) if end == -1: break line = mm[start:end] # print('[%10d:%10d:%10d]' % (start, end, end - start)) yield line.decode() # yield line start = end + 1 fd.close()
def start_program_preexec_fn_test(self): """Test passing preexec_fn to startProgram.""" marker_text = "yo wassup man" # Create a temporary file that will be written before exec with tempfile.NamedTemporaryFile() as testfile: # Write something to testfile to show this method was run def preexec(): # Open a copy of the file here since close_fds has already closed the descriptor testcopy = open(testfile.name, 'w') testcopy.write(marker_text) testcopy.close() with timer(5): # Start a program that does nothing, with a preexec_fn proc = iutil.startProgram(["/bin/true"], preexec_fn=preexec) proc.communicate() # Rewind testfile and look for the text testfile.seek(0, os.SEEK_SET) self.assertEqual(testfile.read(), marker_text)
def WeakCheckStartButton(self): print "Check Weak Password: "******"请在左侧选择系统!", u"提示", wx.YES_NO | wx.ICON_QUESTION) if dlg.ShowModal() == wx.ID_YES: dlg.Destroy() else: useMetal = False if 'wxMac' in wx.PlatformInfo: useMetal = self.cb.IsChecked() self.thread = timer(self, idx=self.idList[self.listBox.GetSelection()]) dlg = VerifDialog(self, -1, u"输入口令", idx=self.idList[self.listBox.GetSelection()], size=(350, 200), style=wx.DEFAULT_DIALOG_STYLE, # & ~wx.CLOSE_BOX, useMetal=useMetal, ) dlg.CenterOnScreen() val = dlg.ShowModal() # if val == wx.ID_OK: # print("You pressed OK\n") # else: # print("You pressed Cancel\n") dlg.Destroy()
def exec_readlines_test_exits(self): """Test execReadlines in different child exit situations.""" # Tests that exit on signal will raise OSError once output # has been consumed, otherwise the test will exit normally. # Test a normal, non-0 exit with tempfile.NamedTemporaryFile() as testscript: testscript.write("""#!/bin/sh echo "one" echo "two" echo "three" exit 1 """) testscript.flush() with timer(5): rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name]) self.assertEqual(rl_iterator.next(), "one") self.assertEqual(rl_iterator.next(), "two") self.assertEqual(rl_iterator.next(), "three") self.assertRaises(OSError, rl_iterator.next) # Test exit on signal with tempfile.NamedTemporaryFile() as testscript: testscript.write("""#!/bin/sh echo "one" echo "two" echo "three" kill -TERM $$ """) testscript.flush() with timer(5): rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name]) self.assertEqual(rl_iterator.next(), "one") self.assertEqual(rl_iterator.next(), "two") self.assertEqual(rl_iterator.next(), "three") self.assertRaises(OSError, rl_iterator.next) # Repeat the above two tests, but exit before a final newline with tempfile.NamedTemporaryFile() as testscript: testscript.write("""#!/bin/sh echo "one" echo "two" echo -n "three" exit 1 """) testscript.flush() with timer(5): rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name]) self.assertEqual(rl_iterator.next(), "one") self.assertEqual(rl_iterator.next(), "two") self.assertEqual(rl_iterator.next(), "three") self.assertRaises(OSError, rl_iterator.next) with tempfile.NamedTemporaryFile() as testscript: testscript.write("""#!/bin/sh echo "one" echo "two" echo -n "three" kill -TERM $$ """) testscript.flush() with timer(5): rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name]) self.assertEqual(rl_iterator.next(), "one") self.assertEqual(rl_iterator.next(), "two") self.assertEqual(rl_iterator.next(), "three") self.assertRaises(OSError, rl_iterator.next)
elt = x_strip[j] if elt > currmax: currmax = elt y_strip[i] = currmax return y_strip def dilate_decompose_interior(x, k): m,n = x.shape y = np.array([dilate_1d_interior(x[row_idx, :],k) for row_idx in xrange(m)]) return np.array([dilate_1d_interior(y[:, col_idx],k) for col_idx in xrange(n)]).T if __name__ == '__main__': if not running_pypy: import scipy.ndimage with timer('scipy'): scipy_result = scipy.ndimage.grey_dilation(image, k, mode='nearest') from numba import autojit from parakeet import jit run(dilate_naive, 'naive', imshow=False) run(dilate_naive_inline, 'naive-inline') run(dilate_decompose_loops, 'decompose-loops') run(dilate_decompose_loops_inline, 'decompose-loops-inline') run(dilate_decompose, 'decompose-slices' ) run(dilate_decompose_interior, 'decompose-interior') with timer('cpython-naive'): dilate_naive(image, k,) with timer('cpython-naive-inline'): dilate_naive_inline(image, k)
break elif line[0:len(h2)] == h2: l = line[len(h2)::] parts = l.split(" ") R2 = np.array([[float(parts[0]), float(parts[1]), float(parts[2])], [float(parts[3]), float(parts[4]), float(parts[5])], [float(parts[6]), float(parts[7]), float(parts[8])]]) print 'T' print T2 print 'R' print R2 __draw__ = False start = 40 end = 40 tm = timer.timer(start, end, date=True) for frame in range(start, end+1): tm.progress(frame) data = read_velo(frame) img = images.fetch_disp('bm', frame, lib='kitti') if __draw__: cv2.imshow('left', images.fetch_orig(frame, lib='kitti')) disp = project_velo(data, np.shape(img), R2, T2) print 'disp type', disp.dtype disps = list() disps.append(disp) #disps.append(interpolate(disp, 3))
return for var_entry in data: print var_entry to_insert = [[query,url,site,var_entry,e] for e in data[var_entry]] c.executemany("""INSERT INTO data VALUES(?,?,?,?,?);""",to_insert) conn.commit() c.close() conn.close() print "QUERY FINISHED: " + query def perform_mass_scraping(sitelist,querylist,max_results,chainlist,validation_functions=None,dbname='test.db'): br = create_browser() if chainlist = None: chainlist = [make_basic_chain_elements('p','text',None,True) for i in range(len(sitelist))] i = 0 new_timer = timer.timer() for site in sitelist: for query in querylist: engine = google_search(site,query,max_results) engine.loop_until_complete() results_list = engine.results_list if validation_functions <> None: func = validation_functions[i] results_list = [o for o in results_list if func(o)] for url in results_list: data = grab_url_info(url,br,chainlist[i]) store_data_in_sql_database(query,url,data,dbname) while not new_timer.check_time(): pass i+=1
#!/bin/python import sys import timer if __name__ == '__main__': f = 'file://'+sys.argv[3] sc = 'cvlc '+f+' &>/dev/null' timer.timer(sys.argv[1].split(':'),sys.argv[2].split(':'),sc)
""" Project Euler Question # 1 Attempting a quicker solution than 0.6x ms responses This one takes 0.8 ms, is indeed slower """ import sys sys.path.append('../') import timer as _timer from question import * # Start the timer now = _timer.timer() # The solution starter = 0 total = 0 while starter < 1000: if starter%3 == 0 or starter%5 == 0: total += starter starter += 1 # print the answer _timer.print_answer(now, total)