def get_auth_settings(self): """ Get the email and password from the user """ self.auth['email'] = util.prompt('LendingClub email', self.auth['email']) self.auth['pass'] = util.get_password() return self.auth
def portfolio_picker(self, default=None): """ Shows a list picker of porfolios Parameters: default -- The portfolio name to have selected by default """ folios = self.investor.lc.get_portfolio_list(names_only=True) print '\nPortfolios...' folios.sort() while True: if len(folios) == 0: picked = util.prompt('Enter the name for your new portfolio') else: picked = self.list_picker( items=folios, default=default, allow_other=True, other_prompt='Enter the name for your new portfolio') # Validate custom value if picked and picked not in folios and re.search('[^a-zA-Z0-9 ,_\-#\.]', picked): print 'The portfolio name \'{0}\' is not valid! Only alphanumeric, spaces , _ - # and . are allowed.'.format(picked) else: break return picked
def portfolio_picker(self, default=None): """ Shows a list picker of porfolios Parameters: default -- The portfolio name to have selected by default """ folios = self.investor.lc.get_portfolio_list(names_only=True) print('\nPortfolios...') folios.sort() while True: if len(folios) == 0: picked = util.prompt('Enter the name for your new portfolio') else: picked = self.list_picker( items=folios, default=default, allow_other=True, other_prompt='Enter the name for your new portfolio') # Validate custom value if picked and picked not in folios and re.search('[^a-zA-Z0-9 ,_\-#\.]', picked): print('The portfolio name \'{0}\' is not valid! Only alphanumeric, spaces , _ - # and . are allowed.'.format(picked)) else: break return picked
def list_picker(self, items, default=None, label_key=None, id_key=None, allow_other=None, other_prompt='Enter a value'): """ Shows a list of items the user can pick from. Parameters items -- The list of items to display. This is either a list of strings or objects. If objects, the label_key must be set default -- The item or ID that should be selected by default. label_key -- If items is a list of objects, this is the key or attribute of the object with the label to show for each item. id_key -- If items is a list of objects, this defined what the ID key/attribute is on each object. allow_other -- If an 'Other' option should be allowed. If selected the user will be able to type their own item, which will be returned. other_prompt -- The prompt to show when the user selects 'Other' Returns The item chosen from the list, a string if the user choose 'Other' or False if the user cancels the selection """ assert len(items) > 0 or default is not False, 'You cannot select from a list without any items' try: string_list = False if (len(items) > 0 and type(items[0]) in [str, unicode]) or (len(items) == 0 and type(default) is str): string_list = True # If the default item isn't in the list of strings, add it if default and default not in items and string_list: items.append(default) # Print out the list i = 1 other_index = -1 cancel_index = 0 default_index = False for item in items: gutter = ' ' is_default = False # Get item label if string_list: label = item else: label = str(item) if label_key: if type(item) is dict and label_key in item: label = item[label_key] elif hasattr(item, label_key): label = getattr(item, label_key) # Selected indicator if default is not None: if string_list and default == item: is_default = True elif id_key is not None: if type(item) is dict and id_key in item and item[id_key] == default: is_default = True elif hasattr(item, label_key) and getattr(item, id_key) == default: is_default = True if is_default: gutter = '> ' default_index = str(i) print '{0}{1}: {2}'.format(gutter, i, label) i += 1 if allow_other: other_index = i print ' {0}: Other'.format(other_index) i += 1 cancel_index = i print ' {0}: Cancel'.format(cancel_index) # Choose a portfolio while(True): choice = util.prompt('Choose one', default_index) # If no digit was chosen, ask again unless a default portfolio is present if not choice.isdigit(): if not default_index: continue else: return default choice = int(choice) # Out of range if choice == 0 or choice > cancel_index: continue # List item chosen if choice <= len(items): return items[choice - 1] # Other elif choice == other_index: while(True): other = util.prompt(other_prompt) # Empty string entered, show list again if other.strip() == '': break # Return custom portfolio name else: return other # Cancel else: return False except Exception as e: self.logger.error(e)
def test_prompt_prefill(self): # User enters empty string, select prefill util.get_input = lambda msg: '' self.assertEqual(util.prompt('msg'), '') self.assertEqual(util.prompt('msg', 'not test'), 'not test')
def test_prompt(self): # User enters 'test' util.get_input = lambda msg: 'test' self.assertEqual(util.prompt('msg'), 'test') self.assertEqual(util.prompt('msg', 'not test'), 'test')
def test_prompt_prefill(self): # User enters empty string, select prefill util.get_input = lambda msg: "" self.assertEqual(util.prompt("msg"), "") self.assertEqual(util.prompt("msg", "not test"), "not test")
def test_prompt(self): # User enters 'test' util.get_input = lambda msg: "test" self.assertEqual(util.prompt("msg"), "test") self.assertEqual(util.prompt("msg", "not test"), "test")
def list_picker(self, items, default=None, label_key=None, id_key=None, allow_other=None, other_prompt='Enter a value'): """ Shows a list of items the user can pick from. Parameters items -- The list of items to display. This is either a list of strings or objects. If objects, the label_key must be set default -- The item or ID that should be selected by default. label_key -- If items is a list of objects, this is the key or attribute of the object with the label to show for each item. id_key -- If items is a list of objects, this defined what the ID key/attribute is on each object. allow_other -- If an 'Other' option should be allowed. If selected the user will be able to type their own item, which will be returned. other_prompt -- The prompt to show when the user selects 'Other' Returns The item chosen from the list, a string if the user choose 'Other' or False if the user cancels the selection """ assert len(items) > 0 or default is not False, 'You cannot select from a list without any items' try: string_list = False if (len(items) > 0 and type(items[0]) in [str, str]) or (len(items) == 0 and type(default) is str): string_list = True # If the default item isn't in the list of strings, add it if default and default not in items and string_list: items.append(default) # Print out the list i = 1 other_index = -1 cancel_index = 0 default_index = False for item in items: gutter = ' ' is_default = False # Get item label if string_list: label = item else: label = str(item) if label_key: if type(item) is dict and label_key in item: label = item[label_key] elif hasattr(item, label_key): label = getattr(item, label_key) # Selected indicator if default is not None: if string_list and default == item: is_default = True elif id_key is not None: if type(item) is dict and id_key in item and item[id_key] == default: is_default = True elif hasattr(item, label_key) and getattr(item, id_key) == default: is_default = True if is_default: gutter = '> ' default_index = str(i) print('{0}{1}: {2}'.format(gutter, i, label)) i += 1 if allow_other: other_index = i print(' {0}: Other'.format(other_index)) i += 1 cancel_index = i print(' {0}: Cancel'.format(cancel_index)) # Choose a portfolio while(True): choice = util.prompt('Choose one', default_index) # If no digit was chosen, ask again unless a default portfolio is present if not choice.isdigit(): if not default_index: continue else: return default choice = int(choice) # Out of range if choice == 0 or choice > cancel_index: continue # List item chosen if choice <= len(items): return items[choice - 1] # Other elif choice == other_index: while(True): other = util.prompt(other_prompt) # Empty string entered, show list again if other.strip() == '': break # Return custom portfolio name else: return other # Cancel else: return False except Exception as e: self.logger.error(e)