From b173d11ac84386e603119ae959734e3c50531aa5 Mon Sep 17 00:00:00 2001 From: Andrew Tavis McAllister Date: Thu, 7 Nov 2024 22:29:34 +0100 Subject: [PATCH] Update user prompts and code formatting --- docs/source/scribe_data/cli.rst | 30 ++++++++++++------------- src/scribe_data/cli/interactive.py | 36 ++++++++++++++++++------------ tests/cli/test_interactive.py | 22 +++++++++--------- tests/cli/test_total.py | 8 +++---- 4 files changed, 52 insertions(+), 44 deletions(-) diff --git a/docs/source/scribe_data/cli.rst b/docs/source/scribe_data/cli.rst index db67abb5..5df9ee92 100644 --- a/docs/source/scribe_data/cli.rst +++ b/docs/source/scribe_data/cli.rst @@ -167,7 +167,7 @@ Examples: $ scribe-data get -l English --data-type verbs -od ~/path/for/output Getting and formatting English verbs - Data updated: 100%|████████████████████████| 1/1 [00:29<00:00, 29.73s/process] + Data updated: 100%|████████████████████████| 1/1 [00:XY<00:00, XY.Zs/process] Behavior and Output: ^^^^^^^^^^^^^^^^^^^^ @@ -197,7 +197,7 @@ Behavior and Output: .. code-block:: text Getting and formatting English verbs - Data updated: 100%|████████████████████████| 1/1 [00:29<00:00, 29.73s/process] + Data updated: 100%|████████████████████████| 1/1 [00:XY<00:00, XY.Zs/process] 4. If no data is found, you'll see a warning: @@ -272,8 +272,8 @@ Examples: ============================================== Language Data Type Total Lexemes ============================================== - English nouns 123456 - verbs 234567 + English nouns 123,456 + verbs 234,567 ... .. code-block:: text @@ -283,9 +283,9 @@ Examples: Language Data Type Total Wikidata Lexemes ================================================================ - English adjectives 12,848 - adverbs 19,998 - nouns 30,786 + English adjectives 12,345 + adverbs 23,456 + nouns 34,567 ... .. code-block:: text @@ -295,12 +295,12 @@ Examples: Language Data Type Total Wikidata Lexemes ================================================================ - Q1860 adjectives 12,848 - adverbs 19,998 - articles 0 - conjunctions 72 - nouns 30,786 - personal pronouns 32 + Q1860 adjectives 12,345 + adverbs 23,456 + articles 30 + conjunctions 40 + nouns 56,789 + personal pronouns 60 ... .. code-block:: text @@ -308,14 +308,14 @@ Examples: $ scribe-data total --language English -dt nouns Language: English Data type: nouns - Total number of lexemes: 12345 + Total number of lexemes: 12,345 .. code-block:: text $ scribe-data total --language Q1860 -dt verbs Language: Q1860 Data type: verbs - Total number of lexemes: 23456 + Total number of lexemes: 23,456 Convert Command ~~~~~~~~~~~~~~~ diff --git a/src/scribe_data/cli/interactive.py b/src/scribe_data/cli/interactive.py index d79101a9..3ed568e6 100644 --- a/src/scribe_data/cli/interactive.py +++ b/src/scribe_data/cli/interactive.py @@ -33,10 +33,10 @@ from rich.logging import RichHandler from rich.table import Table from tqdm import tqdm -from scribe_data.cli.total import total_wrapper # from scribe_data.cli.list import list_wrapper from scribe_data.cli.get import get_data +from scribe_data.cli.total import total_wrapper from scribe_data.cli.version import get_version_message from scribe_data.utils import ( DEFAULT_JSON_EXPORT_DIR, @@ -98,7 +98,7 @@ def display_summary(): console.print("\n") -# Helper function to create a WordCompleter +# Helper function to create a WordCompleter. def create_word_completer( options: List[str], include_all: bool = False ) -> WordCompleter: @@ -108,11 +108,12 @@ def create_word_completer( # MARK: Language Selection + + def prompt_for_languages(): """ Requests language and data type for lexeme totals. """ - # MARK: Language Selection language_completer = create_word_completer(config.languages, include_all=True) initial_language_selection = ", ".join(config.selected_languages) selected_languages = prompt( @@ -122,7 +123,7 @@ def prompt_for_languages(): ) if "All" in selected_languages: config.selected_languages = config.languages - elif selected_languages.strip(): # Check if input is not just whitespace + elif selected_languages.strip(): # check if input is not just whitespace config.selected_languages = [ lang.strip() for lang in selected_languages.split(",") @@ -135,6 +136,8 @@ def prompt_for_languages(): # MARK: Data Type Selection + + def prompt_for_data_types(): data_type_completer = create_word_completer(config.data_types, include_all=True) initial_data_type_selection = ", ".join(config.selected_data_types) @@ -147,14 +150,14 @@ def prompt_for_data_types(): if "All" in selected_data_types.capitalize(): config.selected_data_types = config.data_types break - elif selected_data_types.strip(): # Check if input is not just whitespace + elif selected_data_types.strip(): # check if input is not just whitespace config.selected_data_types = [ dt.strip() for dt in selected_data_types.split(",") if dt.strip() in config.data_types ] if config.selected_data_types: - break # Exit loop if valid data types are selected + break # exit loop if valid data types are selected rprint("[yellow]No data type selected. Please try again.[/yellow]") @@ -173,12 +176,11 @@ def configure_settings(): rprint( "[cyan]Follow the prompts below. Press tab for completions and enter to select.[/cyan]" ) - # MARK: Languages prompt_for_languages() - # MARK: Data Types prompt_for_data_types() # MARK: Output Type + output_type_completer = create_word_completer(["json", "csv", "tsv"]) config.output_type = prompt( "Select output type (json/csv/tsv): ", completer=output_type_completer @@ -257,8 +259,8 @@ def request_total_lexeme_loop(): choice = questionary.select( "What would you like to do?", choices=[ - Choice("Request total lexeme", "total"), - Choice("Run for total lexeme", "run"), + Choice("Configure total lexemes request", "total"), + Choice("Run total lexemes request", "run"), Choice("Exit", "exit"), ], ).ask() @@ -312,28 +314,30 @@ def start_interactive_mode(selectMode: str = None): f"[bold cyan]Welcome to {get_version_message()} interactive mode![/bold cyan]" ) while True: - # Check if both selected_languages and selected_data_types are empty + # Check if both selected_languages and selected_data_types are empty. if not config.selected_languages and not config.selected_data_types: if selectMode == "Get": choices = [ - Choice("Request get data", "configure"), + Choice("Configure get data request", "configure"), # Choice("See list of languages", "languages"), Choice("Exit", "exit"), ] + elif selectMode == "Total": choices = [ - Choice("Request total lexeme", "total"), + Choice("Configure total lexemes request", "total"), # Choice("See list of languages", "languages"), Choice("Exit", "exit"), ] else: choices = [ - Choice("Request get data", "configure"), + Choice("Configure get data request", "configure"), Choice("Exit", "exit"), ] if config.configured: choices.insert(1, Choice("Request for get data", "run")) + else: choices.insert(1, Choice("Request for total lexeme", "total")) @@ -341,18 +345,22 @@ def start_interactive_mode(selectMode: str = None): if choice == "configure": configure_settings() + elif choice == "total": prompt_for_languages() prompt_for_data_types() request_total_lexeme_loop() break + # elif choice == "languages": # see_list_languages() # break + elif choice == "run": run_request() rprint(THANK_YOU_MESSAGE) break + else: rprint(THANK_YOU_MESSAGE) break diff --git a/tests/cli/test_interactive.py b/tests/cli/test_interactive.py index df15e556..e229a588 100644 --- a/tests/cli/test_interactive.py +++ b/tests/cli/test_interactive.py @@ -21,15 +21,16 @@ """ import unittest -from unittest.mock import patch, MagicMock, call from pathlib import Path +from unittest.mock import MagicMock, call, patch + from scribe_data.cli.interactive import ( ScribeDataConfig, configure_settings, display_summary, - run_request, - prompt_for_languages, prompt_for_data_types, + prompt_for_languages, + run_request, ) @@ -54,7 +55,7 @@ def test_scribe_data_config_initialization(self): @patch("scribe_data.cli.interactive.rprint") def test_configure_settings_all_languages(self, mock_rprint, mock_prompt): """Test configure_settings with 'All' languages selection.""" - # Set up mock responses + # Set up mock responses. responses = iter( [ "All", # languages @@ -79,7 +80,7 @@ def test_configure_settings_all_languages(self, mock_rprint, mock_prompt): @patch("scribe_data.cli.interactive.rprint") def test_configure_settings_specific_languages(self, mock_rprint, mock_prompt): """Test configure_settings with specific language selection.""" - # Set up mock responses + # Set up mock responses. responses = iter( [ "english, spanish", # languages @@ -106,7 +107,6 @@ def test_configure_settings_specific_languages(self, mock_rprint, mock_prompt): @patch("scribe_data.cli.interactive.logger") def test_run_request(self, mock_logger, mock_tqdm, mock_get_data): """Test run_request functionality.""" - # Setup self.config.selected_languages = ["english"] self.config.selected_data_types = ["nouns"] self.config.configured = True @@ -131,10 +131,10 @@ def test_run_request(self, mock_logger, mock_tqdm, mock_get_data): @patch("scribe_data.cli.interactive.rprint") def test_request_total_lexeme(self, mock_rprint, mock_prompt): """Test request_total_lexeme functionality.""" - # Set up mock responses + # Set up mock responses. mock_prompt.side_effect = [ - "english, french", # First call for languages - "nouns", # First call for data types + "english, french", # first call for languages + "nouns", # first call for data types ] with patch("scribe_data.cli.interactive.config", self.config): @@ -145,11 +145,11 @@ def test_request_total_lexeme(self, mock_rprint, mock_prompt): prompt_for_languages() prompt_for_data_types() - # Verify the config was updated correctly + # Verify the config was updated correctly. self.assertEqual(self.config.selected_languages, ["english", "french"]) self.assertEqual(self.config.selected_data_types, ["nouns"]) - # Verify prompt was called with correct arguments + # Verify prompt was called with correct arguments. expected_calls = [ call( "Select languages (comma-separated or 'All'): ", diff --git a/tests/cli/test_total.py b/tests/cli/test_total.py index 88ec2838..0bbe340f 100644 --- a/tests/cli/test_total.py +++ b/tests/cli/test_total.py @@ -24,10 +24,10 @@ from unittest.mock import MagicMock, call, patch from scribe_data.cli.total import ( + check_qid_is_language, + get_datatype_list, get_qid_by_input, get_total_lexemes, - get_datatype_list, - check_qid_is_language, total_wrapper, ) @@ -140,7 +140,7 @@ def test_get_total_lexemes_various_data_types(self, mock_query, mock_get_qid): @patch("scribe_data.cli.total.sparql.query") @patch("scribe_data.cli.total.LANGUAGE_DATA_EXTRACTION_DIR") def test_get_total_lexemes_sub_languages(self, mock_dir, mock_query, mock_get_qid): - # Setup for sub-languages + # Setup for sub-languages. mock_get_qid.side_effect = lambda x: { "bokmål": "Q25167", "nynorsk": "Q25164", @@ -151,7 +151,7 @@ def test_get_total_lexemes_sub_languages(self, mock_dir, mock_query, mock_get_qi } mock_query.return_value = mock_results - # Mocking directory paths and contents + # Mocking directory paths and contents. mock_dir.__truediv__.return_value.exists.return_value = True mock_dir.__truediv__.return_value.iterdir.return_value = [ MagicMock(name="verbs", is_dir=lambda: True),