Table of Contents
Let’s check my experiments with Amazon Q Developer, focusing on the inputs provided and the outputs generated during an interactive coding session, based on a recent chat log. The interaction began with an introduction from Amazon Q, stating its ability to answer software development questions, explain, debug, and optimize code, and offering a list of quick actions via the slash command.
Experiment 1: Adding a Data Class
- Input: The initial request I made to Amazon Q Developer was to add a data class to store preferred name, e.g., “Ashutosh” can have “Ash” as a preferred name. Amazon Q requested to examine the repository structure and a specific directory and file.
- Output: Amazon Q proposed adding a
NamePreferred
data class using Python’sdataclasses
module and aNamePreferredRegistry
class to manage the mappings. It provided a detailed explanation of the implemented classes and example usage.
Chat logs for Adding a Data Class
1I've added a data class to store preferred names for names in the repository file. Here's what I've implemented:
2
31. Added a `NamePreferred` data class that stores:
4 - `primary_name`: The main name (e.g., "Ashutosh")
5 - `preferred_names`: A list of preferred names (e.g., ["Ash"])
6
72. Added a `NamePreferredRegistry` class to manage these preferreds:
8 - `register()`: Add a new name preferred mapping
9 - `get_preferreds()`: Get all preferred names for a given primary name
10 - `has_preferreds()`: Check if a name has any preferreds
11
12This implementation allows you to:
13- Define mappings between names (e.g., "Ashutosh" can have "Ash" as an preferred)
14- Look up preferred names when needed
15- Easily extend the system with new name preferreds
16
17Example usage:
18
19 # Create registry with some predefined preferreds
20 registry = NamePreferredRegistry([
21 NamePreferred(primary_name="Ashutosh", preferred_names=["Ash"]),
22 ])
23
24 # Check if a name has preferreds
25 if registry.has_preferreds("Ashutosh"):
26 # Get all preferreds for a name
27 preferreds = registry.get_preferreds("Ashutosh") # Returns ["Ash"]
28
29You can now use this registry in your name repository methods to handle preferred names when searching for names.
Experiment 2: Simplifying the Data Class
- Input: I made a follow-up request asked for a simpler data class structure with just
name
andprefer_name
fields, specifically designed for hard-coded values. - Output: Amazon Q simplified the data class to include only the
name
andprefer_name
fields as requested. It provided the updated class structure and an example of how to use it with a hard-coded list of preferred names. I was amazed to see Amazon Q’s ability to understand follow-up prompts and improve its previously generated code with excellence.
Chat logs for Simplifying the Data Class
1I've simplified the data class as requested. Now you have a straightforward `NamePreferred` data class with just two fields:
2
31. `name`: The main name (e.g., "Ashutosh")
42. `prefer_name`: The preferred name (e.g., "Ash")
5
6This simpler structure is better suited for hard-coded values in your code. You can use it like this:
7
8 # Example of hard-coded name preferreds
9 NAME_PREFERREDS = [
10 NamePreferred(name="Ashutosh", prefer_name="Ash"),
11 ]
12
13 # Usage example
14 def find_preferred_name(name: str) -> str:
15 for prefer in NAME_PREFERREDS:
16 if prefer.name == name:
17 return prefer.prefer_name
18 return None
19
20This approach is more direct and easier to maintain when you have a fixed set of name preferreds that will be hard-coded in your application.
Experiment 3: Using an Enum instead
- Input: I was looking for a different solution, so I requested to convert the data class to an
Enum
where the name would be the name and the preferred name would be the value. Later, I also requested to add two functions: get the preferred name and convert between names. - Output: Amazon Q refactored the data class into a
NamePreferred
enum, explaining the benefits of this approach. It then addedget_preferred()
andconvert_name()
class methods to the enum, providing explanations and examples of their usage. Theconvert_name()
method was designed to handle various scenarios, including returning both the original and preferred names.
Chat logs for Using an Enum instead
1I've converted the data class to an Enum as requested. Now you have a `NamePreferred` enum where:
2
3- The enum name represents the name (e.g., "Ashutosh")
4- The enum value represents the preferred name (e.g., "Ash")
5
6This approach has several advantages:
71. It's more concise and cleaner for hard-coded values
82. You can easily access both the name and its preferred
93. It provides type safety and better IDE support
10
11Here's how you can use this enum in your code:
12
13 # Access the preferred name for "Ashutosh"
14 prefer_name = NamePreferred.Ashutosh.value # Returns "Ash"
15
16 # Check if a name has an preferred
17 def has_preferred(name: str) -> bool:
18 try:
19 return name in NamePreferred.__members__
20 except ValueError:
21 return False
22
23 # Get the preferred name for a given name
24 def get_preferred_name(name: str) -> str:
25 try:
26 return NamePreferred[name].value
27 except KeyError:
28 return None
29
30You can easily add more name mappings by adding new enum members. For example:
31
32 class NamePreferred(str, Enum):
33 Ashutosh = "Ash"
34 # Add more as needed
35
36This enum-based approach is ideal for your use case where the mappings are hard-coded in the code.
1I'll add a function to the enum class to help with working with preferred names. What specific function would you like me to add? You mentioned "this second function" but I don't see a reference to what function you'd like added.
2
3Some common functions that would be useful in this enum class could be:
41. A function to get the preferred name for a given name
52. A function to check if a name has an preferred
63. A function to get all available names
74. A function to convert between names
8
9Please let me know which function you'd like me to add to the enum class.
1I've added two class methods to the `NamePreferred` enum:
2
31. `get_preferred(name)`: Gets the preferred name for a given name
4 - Takes a name string (e.g., "Ashutosh")
5 - Returns the preferred name (e.g., "Ash") or None if not found
6
72. `convert_name(name)`: Converts a name to use its preferred name
8 - Takes a name (e.g., "Ashutosh")
9 - Returns a list containing both the original name and the converted name with the preferred name (e.g., ["Ashutosh", "Ash"])
10 - If no preferred name exists, returns a list with just the original name
11 - Handles edge cases like empty or short names
12
13These methods make it easy to work with preferred names:
14
15 # Get preferred for a name
16 prefer = NamePreferred.get_preferred("Ashutosh") # Returns "Ash"
17
18 # Convert a name to use preferred name
19 results = NamePreferred.convert_name("Ashutosh") # Returns ["Ashutosh", "Ash"]
20
21 # Example usage in a search function
22 def search_name(name):
23 all_numbers = NamePreferred.convert_name(name)
24 # Now search for all numbers in the list
25 # This will search for both "Ashutosh" and "Ash"
26
27This implementation gives you a clean and efficient way to handle preferred names in your name searches.
Experiment 4: Adding Unit Tests
- Input: I was satisfied with the generated code, so I requested to add unit tests for the changes using
pytest
in a new test file. I also requested to include all possible test cases or scenarios. - Output: Firstly, Amazon Q identified and fixed a missing
Enum
import in the code. Then, it added comprehensive unit tests for theNamePreferred
class. Surprisingly, the Amazon Q-generated tests covered various scenarios, including existing and non-existent names, case sensitivity, special characters, whitespace scenarios, edge cases, and verification of class methods.
Chat logs for Adding Unit Tests
1I've created a dedicated test file for the NamePreferred class. Let me explain what I've done:
2
31. Fixed the name_repository.py file by adding the missing `from enum import Enum` import.
4
52. Created a new test file called `name_preferred_test.py` with comprehensive unit tests for the NamePreferred class:
6
7 - Tests for `get_preferred` method:
8 - Test with an existing name ("Ashutosh") that should return "Ash"
9 - Test with a non-existent name that should return None
10
11 - Parameterized tests for `convert_name` method covering various scenarios:
12 - A name that has an preferred ("Ashutosh")
13 - A name that doesn't have an preferred ("Singh")
14 - An unknown name ("Unknown")
15
16The tests verify that:
17 - `get_preferred` correctly returns the preferred name or None
18 - `convert_name` correctly returns a list with both the original and preferred names when an preferred exists
19 - `convert_name` handles edge cases properly
20
21These tests ensure that your NamePreferred class works as expected and will help catch any regressions if the code is modified in the future.
1I've added unit tests for the NamePreferred class to the name_respository_test.py file. Here's what I've included:
2
31. Added the NamePreferred import to the import statements
42. Created a new TestNamePreferred class with the following tests:
5
6 - `test_get_preferred_with_existing_name`: Tests that the get_preferred method correctly returns "Ash" when given "Ashutosh"
7
8 - `test_get_preferred_with_nonexistent_name`: Tests that the get_preferred method correctly returns None when given a name that doesn't have an preferred
9
10 - `test_convert_name`: A parameterized test that covers various scenarios for the convert_name method:
11 - A name that has an preferred ("Ashutosh")
12 - A name that doesn't have an preferred ("Singh")
13 - An unknown name ("Unknown")
14
15The tests are comprehensive and verify that:
16- The get_preferred method correctly returns the preferred name or None
17- The convert_name method correctly returns a list with both the original and preferred names when an preferred exists
18- Both methods handle edge cases properly
19
20These tests will ensure that your NamePreferred class works as expected and will help catch any regressions if the code is modified in the future.
1I've expanded the test cases for the NamePreferred class to be more comprehensive. Here's what I've added:
2
31. Converted `test_get_preferred_with_existing_name` and `test_get_preferred_with_nonexistent_name` into a single parameterized test that covers:
4 - Existing name ("Ashutosh")
5 - Case sensitivity check ("ashutosh")
6 - Non-existent name ("Unknown")
7 - Empty string
8 - None value
9 - Names with leading/trailing spaces
10
112. Enhanced the `test_convert_name` parameterized test with additional scenarios:
12 - Standard cases (existing, non-existing names)
13 - Edge cases (empty string, None)
14 - Case sensitivity ("ashutosh")
15 - Special characters in names (hyphen, slash, underscore)
16 - Whitespace handling (leading/trailing spaces)
17
183. Added a new test `test_enum_values` to verify that the enum values are correctly defined.
19
20These comprehensive tests will ensure that:
21 - The NamePreferred enum is correctly defined
22 - The get_preferred method handles all possible input scenarios correctly
23 - The convert_name method properly converts names in all scenarios
24 - Edge cases and special characters are handled appropriately
These experiments demonstrate how Amazon Q Developer can act as an agent in the coding process, taking user inputs in natural language, understanding the context, proposing code structures, refining them based on feedback, and even generating comprehensive code and unit tests. The iterative nature of the interaction, where the user provides input and Amazon Q responds with code and explanations, highlights the potential of agentic coding to boost productivity of development workflows.
↫ Previous post
A Comprehensive Guide to the Snowflake AI Data Cloud
Next post ↬