API
Contained within is exhaustive documentation of this application’s internal Python API.
This is the Mitype main app script.
- class mitype.app.App[source]
Class for enclosing all methods required to run Mitype.
- appendkey(key)[source]
Append a character to the end of the current word.
- Parameters:
key (key) – Character to append.
- clear_line(win, line)[source]
Clear a line on the window.
- Parameters:
win (any) – Curses window.
line (int) – Line number.
- static get_dimensions(win)[source]
Get the height and width of terminal.
- Parameters:
win (any) – Curses window object.
- Returns:
Tuple of height and width of terminal window.
- Return type:
(int, int)
- initialize(win)[source]
Configure the initial state of the curses interface.
- Parameters:
win (any) – Curses window.
- key_printer(win, key)[source]
Print required key to terminal.
- Parameters:
win (any) – Curses window object.
key (str) – Individual characters are returned as 1-character strings, and special keys such as function keys return longer strings containing a key name such as KEY_UP or ^G.
- static keyinput(win)[source]
Retrieve next character of text input.
- Parameters:
win (any) – Curses window.
- Returns:
Value of typed key.
- Return type:
str
- main(win)[source]
Respond to user inputs.
This is where the infinite loop is executed to continuously serve events.
- Parameters:
win (any) – Curses window object.
- print_realtime_wpm(win)[source]
Print realtime wpm during the test.
- Parameters:
win (any) – Curses window.
- print_stats(win)[source]
Print the bottom stats bar after each run.
- Parameters:
win (any) – Curses window.
- replay(win)[source]
Play out a recording of the user’s last session.
- Parameters:
win (any) – Curses window.
- setup_print(win)[source]
Print setup text at beginning of each typing session.
- Parameters:
win (any) – Curses window object.
Open a twitter intent on a browser.
- switch_text(win, value)[source]
Load next or previous text snippet from database.
- Parameters:
win (any) – Curses window.
value (int) – value to increase or decrement the text ID by.
- test_end(win)[source]
Trigger at the end of the test.
Display options for the user to choose at the end of the test. Display stats.
- Parameters:
win (any) – Curses window.
Calculations.
- mitype.calculations.accuracy(total_chars_typed, wrongly_typed)[source]
Get accuracy for the current test.
- Parameters:
total_chars_typed (int) – Total characters typed.
wrongly_typed (int) – Mistyped characters.
- Returns:
Return accuracy.
- Return type:
float
- mitype.calculations.first_index_at_which_strings_differ(string1, string2)[source]
Return index at which there is a change in strings.
This is used to determine the index up to which text must be dimmed and after which must be coloured red (indicating mismatch).
- Parameters:
string1 (str) – The string which is a combination of last typed keys in a session.
string2 (str) – The string corresponding to sample text.
- Returns:
Index at which mismatch occurs for the first time.
- Return type:
int
- mitype.calculations.get_space_count_after_ith_word(index, text)[source]
Return number of spaces after a given word.
- Parameters:
index (int) – Index of word in text list
text (str) – Text without appending extra spaces
- Returns:
The number of spaces required after ith word
- Return type:
int
- mitype.calculations.number_of_lines_to_fit_text_in_window(string, window_width)[source]
Count number of lines required for displaying text.
- Parameters:
string (str) – String containing sample text.
window_width (int) – Width of terminal.
- Returns:
The number of lines required to display sample text
- Return type:
int
- mitype.calculations.speed_in_wpm(text, start_time)[source]
Calculate typing speed in WPM.
- Parameters:
text (list) – List of words from sample text.
start_time (float) – The time when user starts typing the sample text.
- Returns:
Speed in WPM up to 2 decimal places.
- Return type:
str
- mitype.calculations.word_wrap(text, width)[source]
Wrap text on the screen according to the window width.
Returns text with extra spaces which makes the string word wrap.
- Parameters:
text (str) – Text to wrap.
width (int) – Width to wrap around.
- Returns:
Return altered text.
- Return type:
str
Start of app.
Parses command line arguments and decides and fills text accordingly.
- mitype.commandline.load_based_on_difficulty(difficulty_level=3)[source]
Load text of given difficulty from database if parameter is passed.
Defaults to random difficulty level when none is provided.
- Parameters:
difficulty_level (int) – difficulty level in a range of 1 - 5
- Returns:
Tuple of text content followed by DB row identifier.
- Return type:
(str, int)
- mitype.commandline.load_from_database(text_id)[source]
Load given text from database with given id.
- Parameters:
text_id (int) – Row identifier of database text to load.
- Returns:
Tuple of text content followed by DB row identifier.
- Return type:
(str, int)
- mitype.commandline.load_text_from_file(file_path)[source]
Load file contents.
- Parameters:
file_path (str) – Full path to text to load.
- Returns:
Tuple of text content followed by file path.
- Return type:
(str, str)
- mitype.commandline.parse_arguments()[source]
Parse command line arguments.
- Returns:
Parsed command line arguments.
- Return type:
str
- mitype.commandline.resolve_commandline_arguments()[source]
Parse CLI arguments and return practice text details.
- Returns:
Tuple of text content and text ID.
- Return type:
(str, Union[str, int])
Deals with fetching texts from database.
- mitype.database.database_file_absolute_path()[source]
Get full path of directory where source files are stored.
This is required for later fetching entry from data.db which is stored in same directory as app.
- Returns:
The path of directory of source file.
- Return type:
str
- mitype.database.fetch_text_from_id(serial_id)[source]
Fetch row from data.db database.
- Parameters:
serial_id (int) – The unique ID of database entry.
- Returns:
The text corresponding to the entry_id.
- Return type:
str
Handle with user history.
- mitype.history.get_history_records(number_of_records)[source]
Get records from history.
Defaults to -1 if argument value not provided on command line.
- Parameters:
number_of_records (int) – Number of last records to print.
- Returns:
A list of records. The len of this list is number_of_records or all records
- Return type:
list
- mitype.history.history_file_absolute_path()[source]
Get full path of history file.
This is required for later reading or modifying records from history file.
- Returns:
The path of history file.
- Return type:
str
- mitype.history.save_history(text_id, current_speed_wpm, accuracy)[source]
Save test stats to history file.
- Parameters:
text_id (int) – Row identifier of database text loaded.
current_speed_wpm (float) – Speed result from test.
accuracy (str) – Accuracy result from test.
- mitype.history.show_history(number_of_records)[source]
Show records from history.
Defaults to -1 if argument value not provided on command line.
- Parameters:
number_of_records (int) – Number of last records to print.
Keypress.
- mitype.keycheck.get_key_mapping(key)[source]
Map key to its corresponding character.
Addresses a bug in windows-curses where the apostrophe key is incorrectly returned as 530 - https://github.com/zephyrproject-rtos/windows-curses/issues/41
- Parameters:
key (Union[int, str]) – Character to check.
- Returns:
Returns character if mapping is present or original key otherwise.
- Return type:
Union[int, str]
- mitype.keycheck.is_backspace(key)[source]
Detect BACKSPACE key.
- Parameters:
key (str) – Character to check.
- Returns:
Returns True if pressed key is BACKSPACE key. Returns False otherwise.
- Return type:
bool
- mitype.keycheck.is_ctrl_backspace(key)[source]
Detect Ctrl+backspace key combination.
Used to delete the last typed word
- Parameters:
key (str) – Character to check.
- Returns:
Returns True if Ctrl+backspace is pressed. Returns False otherwise.
- Return type:
bool
- mitype.keycheck.is_ctrl_c(key)[source]
Detect Ctrl+c key combination.
This is used to exit the application.
- Parameters:
key (str) – Character to check.
- Returns:
Returns True if Ctrl+c is pressed. Returns False otherwise.
- Return type:
bool
- mitype.keycheck.is_ctrl_t(key)[source]
Detect Ctrl+t key combination.
This is used to share results on twitter
- Parameters:
key (str) – Character to check.
- Returns:
Return True if Ctrl+t is pressed. Return False otherwise.
- Return type:
bool
- mitype.keycheck.is_enter(key)[source]
Detect enter key.
- Parameters:
key (str) – Character to check.
- Returns:
True if line feed or False otherwise.
- Return type:
bool
- mitype.keycheck.is_escape(key)[source]
Detect ESC key.
This is used to exit the application.
- Parameters:
key (str) – Individual characters are returned as 1-character strings, and special keys such as function keys return longer strings containing a key name such as KEY_UP or ^G.
- Returns:
Returns True if pressed key is ESC key. Returns False otherwise.
- Return type:
bool
- mitype.keycheck.is_ignored_key(key)[source]
Detect if key press should be ignored.
Special function keys, page navigation keys must be ignored.
- Parameters:
key (str) – Character to check.
- Returns:
Returns True if pressed key must be ignored or False otherwise.
- Return type:
bool
- mitype.keycheck.is_left_arrow_key(key)[source]
Detect left arrow key.
- Parameters:
key (str) – Character to check.
- Returns:
True if key is a valid text character or False otherwise.
- Return type:
bool
- mitype.keycheck.is_null(key)[source]
Detect null keys like super key.
- Parameters:
key (str) – Character to check.
- Returns:
True if “null” key or False otherwise.
- Return type:
bool
- mitype.keycheck.is_resize(key)[source]
Detect if terminal was resized.
- Parameters:
key (str) – Character to check.
- Returns:
True if resize request or False otherwise.
- Return type:
bool
- mitype.keycheck.is_right_arrow_key(key)[source]
Detect right arrow key.
- Parameters:
key (str) – Character to check.
- Returns:
True if key is a valid text character or False otherwise.
- Return type:
bool
- mitype.keycheck.is_tab(key)[source]
Detect tab key to start mitype again.
- Parameters:
key (str) – Character to check.
- Returns:
True if tab key or False otherwise.
- Return type:
bool
- mitype.keycheck.is_valid_initial_key(key)[source]
Detect if the pressed key is a valid key to start timer.
- Parameters:
key (str) – Character to check.
- Returns:
True if key is a valid text character or False otherwise.
- Return type:
bool
Custom handlers for received signals.
Timer.
- mitype.timer.get_elapsed_minutes_since_first_keypress(start_time)[source]
Get time elapsed since initial keypress.
This is required to calculate speed.
- Parameters:
start_time (float) – The time when user starts typing the sample text.
- Returns:
Time elapsed since start of typing session till calling this function.
- Return type:
float