Phptimeseries.py

@ Trystan Lea
Hi …
I’ve noticed what I think is an error on line 22 in:

Python3 does not use str ?

Regards

Raise an issue on GitHub, but this was probably written long before Python3 (no Hash Bang).

Yes this is old, certainly not frequently used. Here’s a chatgpt suggested fix which looks pretty good to me with some additional checks as well :slight_smile: I havent tried to test it

import struct
import os

def read_phptimeseries_feed(datadir, feedid):
    file_path = os.path.join(datadir, f"feed_{feedid}.MYD")

    try:
        with open(file_path, 'rb') as fh:
            bytesize = os.stat(file_path).st_size
            npoints = int(bytesize / 9.0)

            for i in range(npoints):
                data = fh.read(9)
                if len(data) == 9:
                    array = struct.unpack('<cIf', data)
                    time, value = array[1], array[2]
                    print(f"{time} {value}")

    except FileNotFoundError:
        print(f"File {file_path} not found.")
    except Exception as e:
        print(f"An error occurred: {e}")

# Example usage:
datadir = "/var/lib/phptimeseries/"
feedid = 1
read_phptimeseries_feed(datadir, feedid)

@TrystanLea
Many thanks
Amazing that you used AI
How did you do it?
I’m sure there are many forum users who are amateur programmers like me and need all the help they can get…
Regards

Did it work? :slight_smile:

You just go to: https://chat.openai.com/

Start the prompt with something like:

“Can you fix this to work with python3?”

and paste the code in the same message (here’s the original):

import struct, os

# Directory of phptimeseries feeds, see: settings.php
datadir = "/var/lib/phptimeseries/"
    
# Feed id to read: 
feedid = 1
    
#----------------------------------------------------
    
fh = open(datadir+"feed_"+str(feedid)+".MYD", 'rb')
bytesize = os.stat(datadir+"feed_"+str(feedid)+".MYD").st_size
npoints = int(bytesize/9.0)
    
for i in range(npoints):

    array = struct.unpack('<cIf',fh.read(9))
    
    time = array[1]
    value = array[2]
    
    print str(time)+" "+str(value);

I followed it’s first suggestion up with:

“Can you improve on this code in anyway?”

To which it returned the above. I know that’s a bit lazy of me :laughing:

1 Like

But you can keep going and ask it to add things or fix errors that you get when you try and run it etc.
It’s very effective with small self contained code examples like the above. Especially with a little correction here and there. It does sometimes get it wrong so there is some careful review required.

@TrystanLea
Many many thanks for that explantion.
I signed up for a chatgpt account and then tried your suggestions.
An almost out of another world experience …
Here’s my chat with him/her/it ’ …

https://chat.openai.com/share/c2e0f6ff-61a7-4594-84ee-5fd7f7655ec4

(You might need to copy & paste into a web browser)

And seems definitely worth asking a second/third time for improvements.

My blunt objection to def functions was politely & effectively handled by him/her/it

And my closing thx was graciously acknowledged by him/her/it

Truly mind blowing – at least for my size of mind!

Again - many thanks for your help

1 Like