Episode: 4089 Title: HPR4089: Modifying a Python script with some help from ChatGPT Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr4089/hpr4089.mp3 Transcribed: 2025-10-25 19:29:30 --- This is Hacker Public Radio Episode 4189 for Thursday the 4th of April 2024. Today's show is entitled Modifying a Python script with some help from chat GPT. It is part of the series a little bit of Python. It is hosted by Mr. X and is about 23 minutes long. It carries an explicit flag. The summary is, in this episode I describe my experience of fixing some Python code with some help from chat GPT. Hello and welcome Hacker Public Radio Audience. My name is Mr. X and welcome to this show. As usual, I would like to start by thanking the people HPR for making this service available to us all. It is really an invaluable service on these heritage troops. HPR is a community led podcast provided by the community for the community. That means you can contribute to. It is always short to show and I am sure you must have something you can record or something you can send in. It is very easy. HPR people have gone to a lot of effort to make the process as simple as possible. Just record button on something on your phone, your computer, your tablet, whatever you got to hand and send it in. Anyway, so this show, I am not prepared enough as per usual and I am a little bit short of time. We will see how it goes, hopefully I can not be too unstructured about it all. This came about because way back in the day, I have a Python, I have a Raspberry Pi that I use sits in the living room basically and it is a Pi face command and display module installed onto it and a case in about it. So it is basically an LCD display, it has got a back plate, it has a number of buttons on it. And a toggle switch on the top which you can toggle back, left and right and there is a Python module that came with it at the time. It allows you to write programs that allow you to interact with the Raspberry Pi and display things on the display. And I use this to stop and play, predominantly to stop and play podcasts and I listen to them on headphones around the house and I can also interact with it on what control, our television and what control. And I covered all this in earlier episodes, this is module and we will get this right now. One of the episodes I explained, I used a sort of expanded this script so that when I turn the Raspberry Pi on, it would go to the HPR website and grab the number of free slots and display it on the display and if it was below a certain number, above a certain number or it would be green, below a certain number it would go blue, I think, or another color in any of it, below a certain number would go red or something like that. So I glanced, I could tell whether the shows were low or not. So every time I powered the device, it went off and checked that. That was, I scripted that and what I was actually, I think there was an early episode, but this is the most recent one which explains the script and improved version of the script and it was HPR2340 and so that was that, I'll put a link to that in the show notes when I get right in the show notes and that was all fine. So I used a file that was on the HPR website, sorry, it was HPR2344 was the follow up one, HPR2344 which is an improved version and in that improved version, HPR2344, I said, this is the follow up to my previous HPR show 2340, the improvement being that I used at available stats file from the HPR website rather than scraping the content from the HPR calendar page, yes, that was a kind of more robust way of capturing that number shows in the queue and that was all getting dandy, but when the HPR website has been changed to a static site, the stats page was no longer generated and so that, so I kind of just gave up at that point and I did sort of mention it to Dave and Ken and I don't, from where they were speaking, it didn't sound, obviously they had a lot of things on their place, obviously the janitor's always very busy and it didn't necessarily sound like they were going to be able to implement anything like that now with a new site and I did say it was a very, very low priority, it's not that big a deal, but to my delight, so Ken, let me just see if what was it now, I've got this here, Dave, I got an email from Dave saying, Ken was keen for you to know about some changes he made to the dynamic part of the HPR website, so yeah, it's documented in their HPR changes, you were using the static statistics page to run some software you wrote in the past and we had not added this to the new site after the changeover, have a look, have a look and see what you think about the changes, so there we go, so there's now when you go to the, I can find it now, I can pop up the radio and you go to that and you go to give shows and then if you go to the calendar page, so at the bottom I think it is, oh there we go, work flowed, so in the calendar page, work flow, the HPR statistics are regenerated every 15 minutes and are available in JSON format, note the format is liable to change without notice, yes or that, so it might break, I suppose, so https, colon slash slash hub.hackerpublicradio.org, forward slash stats.json, yeah that's the link and if I click on that, you get this JSON for which, I actually didn't know anything about JSON file format, it kind of looks, it's kind of like an indented sort of, I guess it's a bit, I believe it's a bit like XML, if you don't know XML, it's a way of structuring data and a text file I believe, what I should have done is going to work with the PDF and got the, sort of brief summary about the JSON file format, but of course I didn't, so sorry, I kind of was put a link to that, I suppose, in the show notes. So it starts by saying, stats generated 1709, I'm looking at now, 170938808, age and then you've got, you've got a start field, a rename field, since starts, so that's going to be a total number of seconds since beginning of HPR I'm guessing, the number of, so the total seconds for HPR, let's see if I can get this right, I'm just looking just now, obviously just now at a date, but it's 9,000, 10,000, 300,000, 10,000, 15 million, oh, it's I'm like 15 million, 200, yeah, I can't need that, I was just too complicated, it's a lot of seconds, but it's 18 years, five months and 19 days since HPR started, wow, that's quite a astounding, since rename, total seconds, year 16, two months and six days, shows total 4,610, this week with a ticket, 300, HPR 4,310, Shiminjiration, zero, what's that, Shiminjiration, zero years, two months, 26 days, 23 hours, 42 minutes, 55 seconds, I'm not sure what that's about, hosts, 356, and then I'll blow that, there's slot, and then within the slot field there's an indented deeper field called next free, and it currently says 13, and that's what I'm interested in, obviously, to read, so I need to look at the slots field within that, the next free, and then there's workflow, upload to IA2, release show, submitted 26, Q, and all of that is Q, as in the field, number, future hosts, 10, number of future shows, 34, and process show zero, submitted show zero, shows aren't workflow to reserve 26, so that gives you the sort of thing that you can obviously write, you could create a script or something to discreet that information, and do something with it, happy to use, as I say, I'm a Raspberry Pi with a Pi, so what's it called, command and display module basically, now this command and display modules, on a blank stick, a blank stick as well, so that pops from an LED on the side of the Raspberry Pi, I mean, I don't have blank sticks still available, and the command and display was updated to a newer version, but it was a slightly higher resolution I believe, but I don't know if that's available anymore, and certainly the one I've got isn't available anymore, and the software's not available for the latest version of Raspberry Pi, and I run an old version of Linux on the Raspberry Pi, it's just like me to keep working basically, and during one of the upgrades I did today, the Raspberry Pi, I had to change from Python 3 back to Python 2 to make it work, and I had to mess about, change the code, well, I actually found that there's a number of modules you can, including your Python script to do that, actually I'll explain more of that when I talk about how I'm up to it actually worked, so I was a bit short in time, that was the problem, as per usual, let me just think where was I, so I was a bit short in time, and I thought, well, I would be really keen to get this working, and I'm also really, really rusty with Python, I've kind of forgotten how to write, I mean obviously, if I look at Python, if you've got programming experience, and you look at how Python code, it's really very readable, so you can guess what a lot of it is doing and all that, and in my Python code, I had a lot of comments and stuff, so that helps as well, so I decided that I wanted to speed things up by getting some advice, so to speak, from chat GPT, so that's basically what I did, and obviously I won't go into the detail of the code and whatnot, but I can describe, generally, what I did, and the outcome, and I don't think that's what I was trying to cover, and to say, I found it to be a very good way of speeding up the flow, and especially if you're a bit rusty sort of thing, obviously you've got to, you can't just blindly go into it, I guess, and use chat GPT, well you can, you could, but it helps if you understand, you've got some programming experience and whatnot, but so anyway, the first thing I said to it was, you know, I said, you know, write a script, ask it to write a script that could pass the next free value from the JSON file located at, and I pointed it to the, I had a public radio site, and it basically said, yeah, I can do that, and it fetches the JSON data from the provided URL and extracts the appropriate field and whatnot, and it came back with some code, and I had comments in it, it was all colour coded as well, so you could, you could work out which bits of the code was getting the URL, and then it would do some error checking and whatnot, and it would then print a message if it was available, and it would print the next free value, and then if not, then I would say there's no free value found and whatnot on the JSON file, and then there's also a final bit about what to do if it filled for some other reason, and then it gave an explanation of the code down below, and I thought, wow, that's quite, quite good. So, and then of course I remember, as I was saying to you earlier, that I had, where I looked at my main Python script, which is a lot of other things, obviously, and I checked to see what version of Python it was using, because at the top of the, the Python script, it says it gives a path to the interpreter, it was Python 2, I thought, oh heck, so I thought, hmm, I wonder what version of Python that, that chat GPT was writing that in, so, you know, as I said, I said, you know, what, I asked it, if the script would, would work with Python 2, and it told me no, it wouldn't work Python 2, and it explained a number of reasons why, and it's, it's specifically was talking about the, the, the requests, use the request library, um, to, to capture the, to stats, um, from the, from the, the JSON file, and it says it's not included in, in Python 2, so use it, use something else, so it was basically, you, you are a lib2, I think it used for that, and it, yeah, you're a lib2 for making HTTP requests, and then it also, um, was it, is it a JSON module, was it, uh, yeah, I think I had to add a, a JSON module as well, to, to, to deal with it, with, with, with JSON, the JSON format, yep, so that was, that was fine. So of course, I ran, and, and I got a, uh, uh, an error about SSL certificate thing, and, um, it, it, it replied saying, oh, well, you know, um, this was because, um, the SSL verification, verification had failed, um, and it was, explaining that this was, um, because the SSL certificate store, um, didn't include the necessary certificates, um, so, and, and it, it, it, it, it, it, it, it, it, it, it, it, it, it said, that, uh, gave an example of one way to resolve this, um, but it, it, it's really just bypassing it and picking a, uh, valid certificate, uh, uh, a fake valicity certificate. I don't, I, I don't fully understand exactly how SSL certificates work, um, I haven't really dealt into that, but it was are going to hack to get around it, say do I, noted that you know, disabling SSL certificate verification should not be used for testing purposes and not production environments, that's what I think I said, don't know, that's quite impressive. And then I got a final thing about an error, about NEMER or SSL, and then I apologise, it's so very sorry that I forgot to tell you to include SSL module, and then he's a corrected version sort of thing, you know, and so I ran that code, and that was fine. And then of course, when I was, obviously I copied and pasted this, not exactly, but the similar text, and of course I then realised that the next free value field is actually indented and held within, so it's like a nested field, if you like. Where was it? Where was that? So we're going to find it. Oh yeah, there we go. So the next free slot is within the slot field. So you've got the slot field, and inside that slot field is two further fields, either next free or no media. And so it was the next free one I wanted. I thought, well, how the heck do you interrogate a nested field sort of thing, a one field within another field, basically. And so I, and yeah, actually I said, you know, I asked how I could modify the script to do the value of a JSON file within a field. So it's like an indented one. And I said, oh yeah, you could say you could change the fields together sort of thing. And it gave an example of an outer field and inner field value, and then explained how to do that. And so I was able to adapt my code to use that sort of technique. Obviously, the example it was giving was to do with an outer field and inner field. And it wasn't, it wasn't really, you know, I don't adapt it and modify the code. But it worked. And, you know, I can't remember, it took my hour or so to pull all that together. And I thought, you know, that's incredible. For a novice programmer, such as myself, I'm not a professional programmer. It really, really speeded things up, giving suggestions and examples and explanations. You know, if I've tried to, I mean, I could have, you know, searched all over the internet, trying to find examples and then re-work it, understand it. And I guess even if you were looking online and found examples, you could ask it, you know, if you didn't understand it, you could explain, well, can you explain this bit of code and how does it, how is it working? I mean, so in some instances, it's absolutely phenomenal. But not always because, because another example I can give you is, as well, Mrs. X, she was, she'd been learning the R programming language for, for, for work. And she's not got that much experience of programming, but even less than me, so to speak. And she was looking online, trying to find example, code and stuff. So R, the R programming language, again, I should have had that at the Wikipedia article up a bit, the R programming language is basically are issues by like statisticians and stuff like that. It can, it can grab data and manipulate data within spreadsheets. I think that's basically what it's, what it's strong point is. And it can graph and do take it at graphs and stuff as well. I believe. So Mrs. X found a website that gave in a number of functions you can use to do various things. And so I, yeah, but I don't go down to sand that. And then below each function, there was a wee bit, there was a thing that said explain. So could you push that and just say them. And then the next thing, text started appearing on, it was like, like right now, it was, like, it was getting typed very quickly, sort of thing below the code example. And then it, below it, it said powered by chat GPT. So basically the lazy, the lazy site person who produced the site, or I can't be bothered to spend the time explaining this code, I'll get chat GPT to do it. So, you know, I've got a little bit of programming experience. And I think when you're assigning a value to a variable on, on, on most programming lines, you use like an equal sign or something like that, or a doubly equal sign or whatever, you know, but I think, and I could be wrong here. And by all means, you can correct me. But I think in the R programming language, the user are less than sign and a dash, isn't it, maybe? I'm not sure. Something like that anyway, but anyway, chat GPT interpreted that and said from what I call, it was less than the value or something like this. So, the explanation was, I think, rubbish, absolutely rubbish, you know. So, it didn't understand what it was saying. It just, it was just talking rubbish, basically. So, you know, you can't rely on it and it can make complete mistakes. But my God, it was really impressive. How, how quickly I was able to pull that time out script together and that really saved me a lot of effort. So, I just thought I'd share that with you all and just, you might find it kind of interesting. I don't know if I've got much else more to say, but I'm not going to bother, obviously, I can't share the code necessarily. When you, I think I rewrote it anyway to be fair. So, it wasn't really my final code. It wasn't finally actually generated by chat GPT. But I just think, obviously, there's a thing about what's, what's compatible with the licensing for HPR. But I think, obviously, just giving a general overview of the process and what happens, you should be okay, did the post. If you want a copy of the code, just send me an email and I'm more than happy to give you a copy of that code if you want. But, yeah, I think actually all I've got to say on that anyway. And, I hope you all find it vaguely interesting. And, you know, I'm sure you must have something you can send, you can send it as well, you know. It's very easy. So, if you want to, I think if you want to contact me, you can contact me at MrX at HPR at GoogleMail.com, as MRX, AT, HPR, the At symbol, GoogleMail.com. So, until next time, thank you and goodbye. You have been listening to Hecker Public Radio at Hecker Public Radio. Does it work? Today's show was contributed by a HPR listener like yourself. If you ever thought of recording podcast, you click on our contribute link to find out how easy it really is. Hosting for HPR has been kindly provided by an honesthost.com, the Internet Archive and our sync.net. On the Sadois status, today's show is released under Creative Commons, Attribution 4.0 International License.