Show More
@@ -1,121 +1,121 b'' | |||
|
1 | 1 | """Utility functions for interacting with the console""" |
|
2 | 2 | #----------------------------------------------------------------------------- |
|
3 | 3 | # Copyright (c) 2013, the IPython Development Team. |
|
4 | 4 | # |
|
5 | 5 | # Distributed under the terms of the Modified BSD License. |
|
6 | 6 | # |
|
7 | 7 | # The full license is in the file COPYING.txt, distributed with this software. |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | # Imports |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | # Used to determine python version |
|
15 | 15 | import sys |
|
16 | 16 | |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | # Classes and functions |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | |
|
21 |
def input( |
|
|
21 | def input(prompt_text): | |
|
22 | 22 | """ |
|
23 | 23 | Prompt the user for input. |
|
24 | 24 | |
|
25 | 25 | The input command will change depending on the version of python |
|
26 | 26 | installed. To maintain support for 2 and earlier, we must use |
|
27 | 27 | raw_input in that case. Else use input. |
|
28 | 28 | |
|
29 | 29 | Parameters |
|
30 | 30 | ---------- |
|
31 | 31 | prompt_text : str |
|
32 | 32 | Prompt to display to the user. |
|
33 | 33 | """ |
|
34 | 34 | |
|
35 | 35 | # Try to get the python version. This command is only available in |
|
36 | 36 | # python 2 and later, so it's important that we catch the exception |
|
37 | 37 | # if the command isn't found. |
|
38 | 38 | try: |
|
39 | 39 | majorversion = sys.version_info[0] |
|
40 | 40 | except: |
|
41 | 41 | majorversion = 1 |
|
42 | 42 | |
|
43 | 43 | # Use the correct function to prompt the user for input depending on |
|
44 | 44 | # what python version the code is running in. |
|
45 | 45 | if majorversion >= 3: |
|
46 | 46 | return input(prompt_text) |
|
47 | 47 | else: |
|
48 | 48 | return raw_input(prompt_text) |
|
49 | 49 | |
|
50 | 50 | |
|
51 |
def prompt_boolean( |
|
|
51 | def prompt_boolean(prompt, default=False): | |
|
52 | 52 | """ |
|
53 | 53 | Prompt the user for a boolean response. |
|
54 | 54 | |
|
55 | 55 | Parameters |
|
56 | 56 | ---------- |
|
57 | 57 | prompt : str |
|
58 | 58 | prompt to display to the user |
|
59 | 59 | default : bool, optional |
|
60 | 60 | response to return if none is given by the user |
|
61 | 61 | """ |
|
62 | 62 | |
|
63 |
response = |
|
|
63 | response = input(prompt) | |
|
64 | 64 | response = response.strip().lower() |
|
65 | 65 | |
|
66 | 66 | #Catch 1, true, yes as True |
|
67 | 67 | if len(response) > 0 and (response == "1" or response[0] == "t" or response[0] == "y"): |
|
68 | 68 | return True |
|
69 | 69 | |
|
70 | 70 | #Catch 0, false, no as False |
|
71 | 71 | elif len(response) > 0 and (response == "0" or response[0] == "f" or response[0] == "n"): |
|
72 | 72 | return False |
|
73 | 73 | |
|
74 | 74 | else: |
|
75 | 75 | return default |
|
76 | 76 | |
|
77 | 77 | |
|
78 |
def prompt_dictionary( |
|
|
78 | def prompt_dictionary(choices, default_style=1, menu_comments={}): | |
|
79 | 79 | """ |
|
80 | 80 | Prompt the user to chose one of many selections from a menu. |
|
81 | 81 | |
|
82 | 82 | Parameters |
|
83 | 83 | ---------- |
|
84 | 84 | choices : dictionary |
|
85 | 85 | Keys - choice numbers (int) |
|
86 | 86 | Values - choice value (str), this is what the function will return |
|
87 | 87 | default_style : int, optional |
|
88 | 88 | Choice to select if the user doesn't respond |
|
89 | 89 | menu_comments : dictionary, optional |
|
90 | 90 | Additional comments to append to the menu as it is displayed |
|
91 | 91 | in the console. |
|
92 | 92 | Keys - choice numbers (int) |
|
93 | 93 | Values - comment (str), what will be appended to the |
|
94 | 94 | corresponding choice |
|
95 | 95 | """ |
|
96 | 96 | |
|
97 | 97 | # Build the menu that will be displayed to the user with |
|
98 | 98 | # all of the options available. |
|
99 | 99 | prompt = "" |
|
100 | 100 | for key, value in choices.iteritems(): |
|
101 | 101 | prompt += "%d %s " % (key, value) |
|
102 | 102 | if key in menu_comments: |
|
103 | 103 | prompt += menu_comments[key] |
|
104 | 104 | prompt += "\n" |
|
105 | 105 | |
|
106 | 106 | # Continue to ask the user for a style until an appropriate |
|
107 | 107 | # one is specified. |
|
108 | 108 | response = -1 |
|
109 | 109 | while (not response in choices): |
|
110 | 110 | try: |
|
111 |
text_response = |
|
|
111 | text_response = input(prompt) | |
|
112 | 112 | |
|
113 | 113 | # Use default option if no input. |
|
114 | 114 | if len(text_response.strip()) == 0: |
|
115 | 115 | response = default_style |
|
116 | 116 | else: |
|
117 | 117 | response = int(text_response) |
|
118 | 118 | except: |
|
119 | 119 | print("Error: Value is not an available option. 0 selects the default.\n") |
|
120 | 120 | return choices[response] |
|
121 | 121 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now