天天看點

AI實作代碼轉換,Python轉Java,Java轉Go不再困難?

今天看到個有趣的網站,給大家分享一下。

AI實作代碼轉換,Python轉Java,Java轉Go不再困難?

該網站的功能很神奇,可以實作程式設計語言的轉化。

感覺在一些場景之下還是有點作用的,比如你原來跟我一樣是做Java的,因為工作需要突然轉Go。這個時候用你Java的經驗 + 這個工具,或許可以起到一定的幫助作用。

工具的使用很簡單,隻需要在左側黏貼你想轉換的原始代碼,然後點選CONVERT CODE,右側輸入框就會轉換成目标代碼:

AI實作代碼轉換,Python轉Java,Java轉Go不再困難?

是不是很神奇?關注公衆号“程式猿DD”,回複“代碼轉換”擷取網站位址,一起來試試看吧!

也許你跟我一樣,覺得隻能實作這樣簡單的代碼轉換。但實際上像下面這樣更加複雜的代碼轉換也是不在話下:

const fs = require('fs');
 const AWS = require('aws-sdk');
 
 const s3 = new AWS.S3({
     accessKeyId: process.env.AWS_ACCESS_KEY,
     secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
 });
 
 const fileName = 'contacts.csv';
 
 const uploadFile = () => {
   fs.readFile(fileName, (err, data) => {
       if (err) throw err;
       const params = {
           Bucket: 'testBucket', // pass your bucket name
           Key: 'contacts.csv', // file will be saved as testBucket/contacts.csv
           Body: JSON.stringify(data, null, 2)
       };
       s3.upload(params, function(s3Err, data) {
           if (s3Err) throw s3Err
       });
   });
 };
 
 uploadFile();
           

可以看到其中還包含了一些非JDK中的内容,居然也能順利的轉成Java。

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;

public class UploadObject {

    public static void main(String[] args) throws IOException {
        String clientRegion = "*** Client region ***";
        String bucketName = "*** Bucket name ***";
        String stringObjKeyName = "*** String object key name ***";
        String fileObjKeyName = "*** File object key name ***";
        String fileName = "*** Path to file to upload ***";

        try {
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(clientRegion)
                    .withCredentials(new ProfileCredentialsProvider())
                    .build();

            // Upload a text string as a new object.
            s3Client.putObject(bucketName, stringObjKeyName, "Uploaded String Object");

            // Upload a file as a new object with ContentType and title specified.
            PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType("plain/text");
            metadata.addUserMetadata("x-amz-meta-title", "someTitle");
            request.setMetadata(metadata);
            s3Client.putObject(request);
        }
        catch(AmazonServiceException e) {
            // The call was transmitted successfully, but Amazon S3 couldn't process 
            // it, so it returned an error response.
            e.printStackTrace();
        }
        catch(SdkClientException e) {
            // Amazon S3 couldn't be contacted for a response, or the client
            // couldn't parse the response from Amazon S3.
            e.printStackTrace();
        }
    }
}
           

根據官方說明,該工具也是通過AI實作的,是不是很神奇呢?這個到底是怎麼實作的呢?有了解的小夥伴留言區一起探讨下吧!