Best Python code snippet using autotest_python
test_ledger.py
Source:test_ledger.py  
...12from hwilib.cli import process_commands13SCREEN_TEXT_SOCKET = '/tmp/ledger-screen.sock'14KEYBOARD_PORT = 123515class ScreenTextThread(Thread):16    def get_screen_text(self, use_timeout=False):17        if use_timeout:18            self.sock.settimeout(5)19        else:20            self.sock.settimeout(None)21        data_str = self.sock.recv(200)22        if len(data_str) == 0:23            return ''24        data = json.loads(data_str.decode())25        text = ''26        if data['y'] == 12: # Upper line27            text = data['text']28            text += self.get_screen_text() # Get next line29        elif data['y'] == 26 or data['y'] == 28: # lower line or single line30            text = data['text']31        return text32    def run(self):33        self.running = True34        self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)35        while True:36            try:37                self.sock.bind(SCREEN_TEXT_SOCKET)38                break39            except:40                os.remove(SCREEN_TEXT_SOCKET)41        self.key_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)42        self.key_sock.connect(('127.0.0.1', KEYBOARD_PORT))43        seen_msg_hash = False44        while True:45            try:46                text = self.get_screen_text(False)47                break48            except:49                continue50        while self.running:51            just_wait = False52            if text.startswith('Address') or text.startswith('Message hash') or text.startswith("Reviewoutput") or text.startswith("Amount") or text.startswith("Fees") or text == 'Confirmtransaction':53                time.sleep(0.05)54                self.key_sock.send(b'Rr')55                if text.startswith('Message hash'):56                    seen_msg_hash = True57            elif text == 'Approve' or text.startswith('Accept'):58                time.sleep(0.05)59                self.key_sock.send(b'LRlr')60            elif text == 'Signmessage':61                time.sleep(0.05)62                if seen_msg_hash:63                    self.key_sock.send(b'LRlr')64                    seen_msg_hash = False65                else:66                    self.key_sock.send(b'Rr')67            elif text == 'Cancel' or text == 'Reject':68                time.sleep(0.05)69                self.key_sock.send(b'Ll')70            else:71                # For everything else, do nothing and wait for next text72                just_wait = True73            try:74                if just_wait:75                    # Main screen, don't do anything76                    new_text = self.get_screen_text(False)77                else:78                    # Try to fetch the next text79                    # If it times out, maybe our input didn't make it, so try processing text again80                    new_text = self.get_screen_text(True)81                text = new_text82            except:83                continue84        self.sock.close()85    def stop(self):86        self.running = False87        self.sock.shutdown(socket.SHUT_RDWR)88        self.sock.close()89        self.key_sock.close()90        os.remove(SCREEN_TEXT_SOCKET)91class LedgerEmulator(DeviceEmulator):92    def __init__(self, path):93        self.emulator_path = path94        self.emulator_proc = None...main.py
Source:main.py  
...45    def get_second_operand(self):46        return self.second_operand47    def set_second_operand(self, new_second_operand):48        self.second_operand = new_second_operand49    def get_screen_text(self):50        return self.ids["screen"].text51    def number_decimal_points_on_screen(self):52        return self.get_screen_text().count(".")53    def find_decimal_point_on_screen(self):54        return self.get_screen_text().find(".")55    def has_number_on_screen(self):56        screen_text = self.ids['screen'].text57        found_decimal = self.find_decimal_point_on_screen()58        return screen_text.isdigit() or (screen_text[1:].isdigit() and screen_text[0] == '-') or \59                  (self.number_decimal_points_on_screen() == 1 and ((screen_text[0] == '-' and \60                    (screen_text[1:found_decimal] + screen_text[found_decimal + 1:]).isdigit()) \61                        or (screen_text[0:found_decimal] + screen_text[found_decimal+1:]).isdigit()))62    def set_screen_text(self, new_text):63        self.ids['screen'].text = new_text64    def clear_screen_text(self):65        self.ids['screen'].text = ''66    def add_to_screen(self, to_be_added):67        current_screen_text = self.get_screen_text()68        self.set_screen_text(current_screen_text + to_be_added)69    def switch_displayed_number_parity(self):70        if self.has_number_on_screen():71            self.set_screen_text('%g'%(float(self.get_screen_text()) * -1))72    def operate_on_operands(self, first_operand, second_operand, operation):73        if operation == "+":74            return '%g'%(float(first_operand) + float(second_operand))75        if operation == "-":76            return '%g'%(float(first_operand) - float(second_operand))77        if operation == "x":78            return '%g'%(float(first_operand) * float(second_operand))79        if operation == "/":80            return '%g'%(float(first_operand) / float(second_operand))81    def enter_button(self, button_entry):82        if len(button_entry) == 1:83            if button_entry.isdigit():84                if self.get_entry_state() == "accepting_first_operand" or self.get_entry_state() == \85                        "accepting_second_operand":86                    self.add_to_screen(button_entry)87                elif self.get_entry_state() == "result":88                    self.clear_screen_text()89                    self.set_entry_state("accepting_first_operand")90                    self.add_to_screen(button_entry)91            elif button_entry in "+-x/":92                if self.get_entry_state() == "accepting_first_operand":93                    self.set_first_operand(self.get_screen_text())94                    self.clear_screen_text()95                    self.set_current_operation(button_entry)96                    self.set_entry_state("accepting_second_operand")97                if self.get_entry_state() == "result":98                    self.set_first_operand(self.get_screen_text())99                    self.set_current_operation(button_entry)100                    self.clear_screen_text()101                    self.set_entry_state("accepting_second_operand")102            elif button_entry == "=":103                if self.get_entry_state() == "accepting_second_operand":104                    if self.has_number_on_screen():105                        self.set_second_operand(self.get_screen_text())106                        result = self.operate_on_operands(self.get_first_operand(), self.get_second_operand(), \107                            self.get_current_operation())108                        self.clear_operands_and_operation()109                        self.set_screen_text(str(result))110                        self.set_entry_state("result")111            elif button_entry == ".":112                if self.get_entry_state() == "accepting_first_operand" or self.get_entry_state() == \113                    "accepting_second_operand":114#<<<<<<< HEAD:calculator.py115                    if self.has_number_on_screen() and self.number_decimal_points_on_screen() < 1:116#=======117                    if self.has_number_on_screen() and self.number_decimal_points_on_screen() < 1:118#>>>>>>> main-py-file-rename:main.py119                        self.add_to_screen(".")120                    elif self.get_screen_text() == "":121                        self.add_to_screen("0.")122                elif self.get_entry_state() == "result":123                    self.clear_screen_text()124                    self.clear_operands_and_operation()125                    self.set_entry_state("accepting_first_operand")126                    self.add_to_screen("0.")127            elif button_entry == 'C':128                #############129                #self.set_screen_text(str(self.has_number_on_screen()))130                #time.sleep(3)131                self.clear_screen_text()132                self.set_entry_state("accepting_first_operand")133                self.clear_operands_and_operation()134        elif len(button_entry) == 3:...progressbar.py
Source:progressbar.py  
...41            amount = self.maximum42        self.current_amount = amount43        if update_screen:44            self.update_screen()45    def get_screen_text(self):46        '''47        Builds the actual progress bar text48        '''49        diff = float(self.current_amount - self.minimum)50        done = (diff / float(self.range)) * 100.051        done = int(round(done))52        all = self.width - 253        hashes = (done / 100.0) * all54        hashes = int(round(hashes))55        hashes_text = '#' * hashes56        spaces_text = ' ' * (all - hashes)57        screen_text = "[%s%s]" % (hashes_text, spaces_text)58        percent_text = "%s%%" % done59        percent_text_len = len(percent_text)60        percent_position = (len(screen_text) / 2) - percent_text_len61        screen_text = (screen_text[:percent_position] + percent_text +62                       screen_text[percent_position + percent_text_len:])63        if self.title:64            screen_text = '%s: %s' % (self.title,65                                      screen_text)66        return screen_text67    def update_screen(self):68        '''69        Prints the updated text to the screen70        '''...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
