Switching from Python to Java with CodeWhisperer
Want to easily port code between programming languages? This post shares how CodeWhisperer can make switching between Python and Java a breeze.
Teaching Computer Vision Labelling ML with Python
How CodeWhisperer Supports Language Porting
Addendum: Steps to Port Python to Java
Step 2: Porting Python code to Java with CodeWhisperer
Step 2.2 Arrays of Poultry and Birds of Prey
Step 2.3 Instantiate the Amazon Rekognition Client and Loop through Every File in the Current Folder
I want to recognize that I've heard concerns about using Generative AI in teaching and learning. I've heard fears that students will not learn to code or do their own assignments because they will just 'ask the AI to do it for them'. I've also heard from lecturers who are excited to adopt GenAI as a teaching tool. For this post, I'm focusing on a small use case that I think may be familiar to many lecturers, trainers, and those who volunteer to teach coding: How to move from one language and stack to another. I'm not making any pronouncements on whether you should teach with Gen AI, but I'd like to offer one way you could teach with it.
- Switching/moving/porting code from one language to another, and
- Using Java as the target language.
While my use case is aimed at a lecturer in the class room, many professional developers have to 'context switch' daily. It's not uncommon for a 'full stack' developer to be working on multiple languages at once. They are expected to seamlessly move between technologies; and in a small company, they might be the only developer, so they can't specialize on one stack or aspect of the system. An AI coding companion helps with that constant context switching.
Tab
key. If no code is suggested, I can request it by pressing ALT + c
. Finally, if the suggestion isn't exactly what I want, I can press left and right arrow keys to cycle through alternative suggestions in case one of those suits me better.- My system didn't have a JDK installed, but that was quickly rectified by installing Amazon Coretto.
- I installed the Coding Pack for Java which bundles Visual Studio code and recommended extensions.
- I installed Maven, my preferred Java project management and dependency tool.
- I launched Visual Studio Code and signed into AWS Toolbox and enabled CodeWhisperer.
- I needed to access the AWS SDK for Java v2 and the recommended way to use it is via a Maven Bill of Materials import.
- I created a new Java project with Maven and edited my POM.xml.
# Import Amazon Rekognition for Python libraries
was slightly edited to // Import Amazon Rekognition for Java libraries
.I changed the symbols indicating this line is a comment, and changed
Python
to Java
.The resulting code suggestion looked reasonable, though I wouldn't be sure until I tried to use the libraries suggested:
// String ArraysLists defining birds of prey ('Hawk', 'Buzzard', 'Eagle', 'Vulture', 'Bald Eagle') and poultry birds ('Chicken', 'Fowl', 'Ducks', 'Goose', 'Poultry', 'Hen')
. I slightly modified the original comment to change Arrays to ArrayLists. This is because I prefer to use typed ArrayLists if the students have already learned simple arrays. The resulting code suggestions were exactly what I wanted, though I couldn't remember the unwieldy Arrays.asList
syntax until CodeWhisperer suggested it.// Create Amazon Rekognition client
produced the expected AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.defaultClient();
.The next step could be done in a few different ways. I need to check every file in the current folder and if it's a PNG file, read the bytes (for sending to Rekognition). Java has added some nice syntax and core libraries to make this easier than the last time I coded in it, so I was curious to see what CodeWhisperer would suggest. It also added the necessary imports too. I'm omitting the final two parentheses that CodeWhisperer suggested as I need to add some more code in.
// Call Amazon Rekognition to detect labels in the image, max labels of 5, minimum confidence of 95%
produced:else
clause I wanted, but that reinforces the iterative way of building up the code suggestion by suggestion.Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.