ChatGPT can bridge languages like Python and JavaScript
Written on 2024-05-18
Hi, I'm Chris Dawson a dad and writer from Portland, OR, now living in Florida. My book is Building Tools with GitHub from O'Reilly. I'm an inventor, have started several companies and worked for several non-startups like Apple and eBay. I am sometimes available for hire as a consultant or part time contributor.

Sigh, ok, ChatGPT is really useful in certain contexts. I’m not sold on it as replacing software developers, but this really helped me. I needed to calculate the days since the epoch given a date, in both JavaScript and Python. The data insertion happens on the python side, and when the data is retrieved it happens on the JavaScript side. I needed to make sure dates work both ways. I was struggling to get the code to work for a few hours. Then, I asked ChatGPT. I feel like code generated by ChatGPT will likely have the same bugs that are found in the source material, and you, not being the author of that code, will not see those bugs as easily. But, if you have tests that prove the code works in the way you expect, then that gives you as much assurance as you would have if you were working off someone else’s codebase. Yet another reason to use unit tests.

Here is the test code. It uses the python version to calculate the days from epoch, then runs a nodejs subprocess to calculate using the same date. I need the results to match. I probably should add more edge case dates, but that’s not important to me right now.

import unittest  
import os  
from decoder import days_since_epoch, get_date  
  
class TestDecoder(unittest.TestCase):  
    def test_easy_epoch(self):
        s = "01/05/1970 12:01 AM"
        start = get_date(s)
        day = days_since_epoch(start)
        print(f"Day is {day}")
        output = int(os.popen(f"node node_epoch_date.js '{s}'").read())
        print(f"Output was {output}")
        self.assertEqual(day, output)
        self.assertEqual(day, 4)

That results in a passing test! I actually have a few more tests with more complex dates to make sure the code is resilient, but my brain can easily calculate that Jan 5th, 1970 should result in “4 days from the epoch” as my test shows.

This is the javascript script I shell out to inside the python code

import { getDaySinceEpoch } from '../frontend/src/filter.js';  
  
const input = process.argv[2];  
const date = Date.parse(input);  
const date2 = new Date();  
date2.setTime(date);  
console.log(getDaySinceEpoch(date2));

That saved me a lot of work.

Subscribe to RSS by tags if you only want certain types of posts. Or, subscribe to all posts.
View source to this post
Other interesting things:
<
>
Webiphany.com
https://webiphany.com
1
2
3
4
5
6
7
8
9