Hey Stewart,
Although ChatGPT is by far the most well-known LLM, there's several LLMs or simply front-ends better suited to code generation.
For example, JetBrain's AI assistant just backs onto ChatGPT, as far as I know, but from a prompt asking for a function that checks a string is a valid IP address (and with an explicit hint to support IPv4 and IPv6), it takes the same approach I would:
def is_valid_ip(ip_address: str) -> bool:
try:
socket.inet_pton(socket.AF_INET, ip_address)
except socket.error:
try:
socket.inet_pton(socket.AF_INET6, ip_address)
except socket.error:
return False
return True
Half the battle is, of course, being able to read the result and decide if it's actually a valid approach - but there are tools to assist with that too.
A colleague and I have been experimenting with taking specifications (XMPP XEPs rather than RFCs, but the same concept applies) and generating conformance tests in Python/pytest; somewhat harder, but I think it's somewhere close to attainable.
Dave.
On Wed, 20 Dec 2023 at 15:06, Stewart Bryant <stewart.bryant@xxxxxxxxx> wrote:
I am building a small program and thought I would ask CharGPT to build theFramework. I asked it the following question as part of asking alarger question about some code I wanted it to write:write python3 code to read two parameters from the command line. The first is IP Address the second is Port. If the command line is not properly formatted print an error with the correct format. Now construct an http URL using the IP address and the port with trailing /. store the URL in string global variable RelayURL Print RelayURLI gotif len(sys.argv) != 3:
print("Error: Incorrect number of arguments.")
print("Usage: python script.py <IP Address> <Port>")
sys.exit(1)
ip_address = sys.argv[1]
port = sys.argv[2]
# Check if the IP Address and Port are properly formatted
try:
# Check if IP Address is valid
parts = ip_address.split('.')
if len(parts) != 4 or not all(0 <= int(part) < 256 for part in parts):
raise ValueError("Invalid IP Address format")
# Check if Port is a valid integer
if not (0 < int(port) < 65536):
raise ValueError("Invalid Port number”)
I must say that I was impressed with the quality of the checking it automatically generated but perhaps disappointed that it assumed IP Address == IPv4. As it turns out I am am using IPv4 but it was a bit presumptuous to assume that :)
- Stewart