Posts

Showing posts from 2023

Get rid of Magic Numbers from your code

Last November I got really perplexed. I was looking at some code for the Pimoroni Explorer Base. I love that hardware. It comes with lots of useful peripherals and plenty of sample code to show you how to control them. One of the examples is called noise. It plays a simple tune on the Explorer Base Piezo buzzer, and shows the rise and fall of the notes on the built-in display. You tell the program what notes to play by creating a list like this: tones = [ "AS6" , "A6" , "AS6" , "P" , "AS5" , "P" , "AS5" , "P" , "F6" , "DS6" , "D6" , "F6" , "AS6" ,] The code is easy to follow, but one thing really puzzled me. The program turns each note into a frequency by looking up the name of the note in tones , a massive dictionary with names as keys and integer frequencies as values. I could read the code, but I was still puzzled. Where did all those frequencies

Draw pictures on your Pico!

Image
Would you like to draw pictures on your Pico?  A while ago I experimented with sketcher - a program for the Rasperry Pi Pico that connected to a Raspberry Pi. The Pico had two potentiometers that you could use to move a virtual pen in the Pi; the Pi ran the turtle graphics package which it used to draw the pictures. The project was fun, and I wondered how easy it would be to run a similar program that would use a display with the Pico. That potential project sat in my pending tray for ages, but I recently got an incentive to re-visit it. A new resource Pimoroni have asked me to write a booklet for their Pico Explorer Base . I've been a fan of the product since it first came out, and I'm really enjoying a chance to revisit its awesome capabilities. I decided I'd write a simple sketcher program that ran on the Pico. It uses two potentiometers to move a virtual pen up/down and left/right. The pen draws on the Explorer base display. I've now got a simple version running, a

ChatGPT Meets de Bono: A Powerful Combo

I'm struggling to make a decision at the moment, so I've enlisted the help of ChatGPT. I am experimenting a lot with some of the new AI tools, and it's a bit like playing whack-a-mole at the moment; as soon as I get to grips with one bit of software, another appears and demands my attention. A lot of what I'm doing is based on OpenAI's ChatGPT. I'm concerned that I'll spend too much on paid services, so I am actively looking at Open Source alternatives that I could run locally. Alas, many Large Language Models (LLMs) are too resource-hungry to run on my workstation, and I've been thinking of trying out a Jetson Xavier which I'm not using at the moment. Worth the time? I know I'll need to spend a few days getting to the point where I know whether I can use it for the projects I have in mind. Should I invest the time? I decide to take a Judo approach and use ChatGPT to help me make the decision. I used a prompt that's worked well for me before

Finding the Best Solution with ChatGPT: A Personal Experience

When you're developing software for technically savvy users, you'll often encounter an annoying problem: users provide their preferred solution instead of their requirements. This issue is even more pronounced when you are both developer and user. In such situations, you're likely to choose the first solution that seems to meet your needs, even though there might be better alternatives. Pair programming can help, as your partner can spot what's going on and suggest other ideas. But what if you're programming on your own? That's where ChatGPT comes in handy. ChatGPT to the Rescue I've recently been working on an application that downloads interesting arXiv PDFs, and I wanted to access them remotely. I could think of a couple of ways to do that, but I decided to ask ChatGPT for advice. I used a prompt that had been helpful before: " Suggest three ways I could accomplish that, with the pros, cons, and interesting features of each approach. " I present

Pair Programming with ChatGPT

  Earlier today I started work on a small admin project to help me organise my excessive collection of repositories on GitHub. I wanted a list of my repositories, showing the name, url, description, stars and forks. I decided to get ChatGPT to write the code for me. In the conversation that follows, you can imagine an embodied ChatBot sitting next to me as we pair program. I'm the customer, ChatGPT is the programmer. If I'm not happy with the results, I ask my pair to change the code. My part is in italics. Everything else came from ChatGPT. As you'll see, there are a couple of wrinkles I didn't anticipate, but ChatGPT fixed them. This took less time than I'd have taken to write the code, and it was much more fun. I recommend it. Here's the session. Write python to find the names of all my repositories on GitHub, My username is romilly. You can use the GitHub API to retrieve the names of all your repositories. Here is the Python code to achieve that: i

Can ChatGPT code like a pro?

I've been experimenting with the code generation capabilities of ChatGPT, and I'm blown away. As a test of its capabilities I decided to ask it to write a generic version of some code I've used, over and over, in slightly different forms, in real applications. The Problem The code implements a Throttler - a class that wraps something that makes requests and makes sure that requests are not made too often. Lots of APIs apply rate limits to requests, and this code makes it easy to enforce those limits within your application. A coding session with ChatGPT Here'es the session I had with ChatGPT. My instructions are in italics . Everything else - code, comments, and explanations - was written by ChatGPT in response. Write Python to create an Abstract Class called Requester. It should have a single abstract method called request that takes an argument called command. Add docstring comments. Here is the Python code to create an Abstract Class called Requester with a single

Will ChatGPT save your job - or take it over?

Wondering whether ChatGPT will make you more effective, or do your job so well that you are no longer needed? ChatGPT is one of the new generation of AI tools. You can ask it for help and it can turn its hand to a huge range of tasks. ChatGPT as a Python coder Today I used it to write a very simple but useful computer application. The task would have taken me half an hour or so. ChatGPT did the job in a few seconds, and the code it write was well-structured, readable and well commented. So far so good - but what about other tasks? I decided to make a short video about what I'd done. (Watch this space to see it when it's ready).  I knew that my first step would be to write a script. Could I ask ChatGPT to help me do that? Sure. Here's what happened next. ChatGPT as scriptwriter Here's what I typed: Earlier today I got ChatGPT to write a simple application for me. Now I want to make a video showing the process and the result. I'd also like to explain why the automatio

Complex Network Analysis

Image
 I've recently been working on two fascinating, challenging projects. One is a guide to the Pimoroni Explorer Base. That's going to take a while, and I'll report on progress as i go. The other is a tool to help me keep track of published papers on some of the AI areas the interest me. Therese's so much published that it's a real challenge to keep up. I rely heavily on the wonderful service provided by Semantic Scholar; that helps me keep track of new papers that cite older papers that I've identified as interesting. Here's the citation  network for one of the papers that interest me: I've done quite a bit of work on networks with Python, but I've recently been reading  Complex Network Analysis in Python by Dmitry Zinoviev. It's well-written, broad in coverage, and has plenty of useful sample code. I love it.