#Python
13 posts tagged “Python”
13 posts tagged “Python”
Spent today “vibe coding” with Claude, just describing what I wanted and letting it drive, to see how far that gets you on a small self-contained project. The result is fractalPy, a small Numba-accelerated Mandelbrot set renderer.

It started as a single script — render the set to a PNG, colour it, done — using @njit(parallel=True, fastmath=True, cache=True) to JIT-compile the escape-time kernel so it’s fast enough to iterate on interactively:
Do you ever use print() or log() to debug your code? Of course you do. IceCream, or ic for short, makes print debugging a little sweeter.
ic() is like print(), but better.
I got a cheap SPI TFT ST7735 Display module with SD Card reader off ebay, it had an unusual number of pins on the back.
It took a while searching about on the internet and some head scratching to find a library and work out what connected to what on the Pico.
This is what I found, and worked out.
| TFT Board | Raspberry Pi Pin |
|---|---|
| LED | 3v3(Out) |
| SCK | GP10 |
| SDA | GP11 |
| AO/DC | GP16 |
| Reset | GP17 |
| CS | GP18 |
| GND | GND |
| VCC | VBUS 5V |
Using that information I was able to put together this library based on a few others I found and a few test programs.
Something I did a few (8) years ago on the Arduino to display two-digit numbers on a 8x8 LED matrix, this is an update to do the same in Micropthon.
Numbers have been defined to 3x5 pixels, so for a two-digit number it fits into 8x5 LED grid with spacing, saving some LED Matrix space.
The code for this can be found here numbers.py
Just a short bit of code to test a DHT11 temperature and humidity sensor module.
This version has just three pins VCC, GND and data, so easy to connect and use with the DHT library.
Found a LED Matrix with a Max7219 in my parts bin (box of random electronics in the garage) so looked into getting it working with the Pico and MicroPython.
This is an example why I love open source. The original library I found was functional but sparse, last committed to 4 years ago. There were a few forks, most not much activity. One had been active recently and had some major improvements in functionality so switched over to that fork. https://github.com/enchant97/micropython-max7219
Stumbled across a Python script to run a Mandelbrot set in ASCII in Python so I thought I would see how it ran on the RaspberryPi Pico.
After tweaking it slightly from the source it ended up like this mandelbrot.py
This results in the following;
...................................................................................................
.....................................................................................................
.......................................................................................................
.........................................................................................................
...................................,,,,,,,,,,,,,,,,,.......................................................
..........................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.............................................
.....................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,......................................
.................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,----------,,,,,,,,,,,,.................................
...............,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,---------:i;;::::-------,,,,,,,,,.............................
.............,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,----------:::;ih$+;;::::-------,,,,,,,,,..........................
...........,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,------------::::;;+# hiiii :---------,,,,,,,,,.......................
.........,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,-------------:::::;;i+hM MM #+;;:---------,,,,,,,,,,....................
.........,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,--------------::::::;;ii+hH* $H+ii;:::---------,,,,,,,,,,..................
........,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,---------------::::::;i+HhH *h++;;::::---------,,,,,,,,,,................
.......,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,---------------::::::;;;+ *@ +i;::::::-------,,,,,,,,,,,..............
.....,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,---------------::::;;;;;ii+hM@ $hii;;:::::::------,,,,,,,,,,,............
.....,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,--------------:::;;;;;;iiii++h *h+ii;;;;:::::::----,,,,,,,,,,,...........
....,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,-------------::::;ii++ ++++++hhHH$@ @MHhh++ii;;;;;ihi;:---,,,,,,,,,,,,.........
....,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,----------:::::::;;+H @MHhH$ # $@ h++ii++M$i;:---,,,,,,,,,,,,........
...,,,,,,,,,,,,,,,,,,,,,,,,,,,,--------::::::::::;;;ii@ #@ $HH* $# #;:---,,,,,,,,,,,,.......
..,,,,,,,,,,,,,,,,,,,,,,,,,,,------:::::::::::::;;;;ii+hM * i;::---,,,,,,,,,,,,,.....
..,,,,,,,,,,,,,,,,,,,,,,,,,-----:::::::::::::::;;;;;i++HHM# *+;;:::---,,,,,,,,,,,,,....
..,,,,,,,,,,,,,,,,,,,,,-----::::::::::::::::;;;;;;iiM #Mhii;;::---,,,,,,,,,,,,,....
..,,,,,,,,,,,,,,,,,------::;iH+;;;;;;;;;;;;;;;iiiii++H$ Mh++;::----,,,,,,,,,,,,,...
.,,,,,,,,,,,,---------::::;;iH#++iiii++H*++iiiiii+++H$ $ H;::----,,,,,,,,,,,,,..
,,,,,,,,------------:::::;;;+h **M HhhM $HHMhhhhhhHM@ H;::----,,,,,,,,,,,,,,.
.,,,,,------------::::::::;;ii+hM# @@ #$HHHHM H+i;::-----,,,,,,,,,,,,,.
,,,,------------:::::::::;iiiihh$ $$* @ i;::-----,,,,,,,,,,,,,,
,,-------------::::::::;;iii+H * @ M+;:::-----,,,,,,,,,,,,,,
,------------:::::;;;;i+#HhhhM$@ Mi;:::------,,,,,,,,,,,,,
----------::;;;;;;;iii+hH* * @ hi;::::------,,,,,,,,,,,,,
::::;;;iiMiiiiii+++++MHM $+i;;::::------,,,,,,,,,,,,,
*Hh+i;;::::------,,,,,,,,,,,,,
::::;;;iiMiiiiii+++++MHM $+i;;::::------,,,,,,,,,,,,,
----------::;;;;;;;iii+hH* * @ hi;::::------,,,,,,,,,,,,,
,------------:::::;;;;i+#HhhhM$@ Mi;:::------,,,,,,,,,,,,,
,,-------------::::::::;;iii+H * @ M+;:::-----,,,,,,,,,,,,,,
,,,,------------:::::::::;iiiihh$ $$* @ i;::-----,,,,,,,,,,,,,,
.,,,,,------------::::::::;;ii+hM# @@ #$HHHHM H+i;::-----,,,,,,,,,,,,,.
,,,,,,,,------------:::::;;;+h **M HhhM $HHMhhhhhhHM@ H;::----,,,,,,,,,,,,,,.
.,,,,,,,,,,,,---------::::;;iH#++iiii++H*++iiiiii+++H$ $ H;::----,,,,,,,,,,,,,..
..,,,,,,,,,,,,,,,,,------::;iH+;;;;;;;;;;;;;;;iiiii++H$ Mh++;::----,,,,,,,,,,,,,...
..,,,,,,,,,,,,,,,,,,,,,-----::::::::::::::::;;;;;;iiM #Mhii;;::---,,,,,,,,,,,,,....
..,,,,,,,,,,,,,,,,,,,,,,,,,-----:::::::::::::::;;;;;i++HHM# *+;;:::---,,,,,,,,,,,,,....
..,,,,,,,,,,,,,,,,,,,,,,,,,,,------:::::::::::::;;;;ii+hM * i;::---,,,,,,,,,,,,,.....
...,,,,,,,,,,,,,,,,,,,,,,,,,,,,--------::::::::::;;;ii@ #@ $HH* $# #;:---,,,,,,,,,,,,.......
....,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,----------:::::::;;+H @MHhH$ # $@ h++ii++M$i;:---,,,,,,,,,,,,........
....,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,-------------::::;ii++ ++++++hhHH$@ @MHhh++ii;;;;;ihi;:---,,,,,,,,,,,,.........
.....,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,--------------:::;;;;;;iiii++h *h+ii;;;;:::::::----,,,,,,,,,,,...........
.....,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,---------------::::;;;;;ii+hM@ $hii;;:::::::------,,,,,,,,,,,............
.......,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,---------------::::::;;;+ *@ +i;::::::-------,,,,,,,,,,,..............
........,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,---------------::::::;i+HhH *h++;;::::---------,,,,,,,,,,................
.........,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,--------------::::::;;ii+hH* $H+ii;:::---------,,,,,,,,,,..................
.........,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,-------------:::::;;i+hM MM #+;;:---------,,,,,,,,,,....................
...........,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,------------::::;;+# hiiii :---------,,,,,,,,,.......................
.............,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,----------:::;ih$+;;::::-------,,,,,,,,,..........................
...............,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,---------:i;;::::-------,,,,,,,,,.............................
.................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,----------,,,,,,,,,,,,.................................
.....................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,......................................
..........................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.............................................
...................................,,,,,,,,,,,,,,,,,.......................................................
.........................................................................................................
.......................................................................................................
.....................................................................................................
I amended the code to update the OLED I was using in the temperature sensor tests mand_oled.
Have been tinkering with temperature measurement using the Raspberry Pi Pico.
First with the internal temperature sensor and then using a TMP36 temperature sensor externally.
I wrote two classes to access these and return the reading.
Here is an example using the two sensors together temps.py
You will need to upload the two classes to the Pico first in a library sub-directory.
Here is how you would wire it up on a breadboard tmp36.fzz
Some components arrived from AliExpress recently so been able to play with an I2C OLED display with my Pico W.
Hello World Test
Code of this is here https://github.com/alastairhm/pico_w/blob/main/i2c_scan.py
And also used here to display the IP address after connected to the wifi https://github.com/alastairhm/pico_w/blob/main/wifi_oled.py
Next thing I’ve been playing with is internal and external temperature sensors, I will post about that soon.
Just before Christmas I got a Raspberry Pi Pico W to have a play with.
Looking forward to using MicroPython to do some LED flashing and other things.
Started a repo on Github for it Pico W Repo
A simple Python script to allow you to quickly search from the command line that I’ve been playing with on and off for a while.
https://github.com/alastairhm/searchit
It uses the Python Library Fire to automatically generate the command line interface which is something I stumbled across recently.
You either pass it a paramater for the search term or if one it not passed it used whatever is in your clipboard buffet.
Updating the parameter file you can add other search engines and pick which one to use as your default.
I wrote a simple Python script to monitor the clipboard and write any unique text content out to a YAML file for a history. It might turn into something better eventually.
Updated to use a settings file for file location and size.
#!/usr/bin/env python3
import pyperclip
import time
import yaml
import toml
import os
from yaml.loader import SafeLoader
script_path = os.path.dirname(os.path.abspath(__file__))
settings = toml.load(os.path.join(script_path, "clippy.toml"))
history = settings["history"]
max_size = settings["size"]
cliptext = ""
clip_array = []
with open(history, "r") as f:
clip_array = list(yaml.load_all(f, Loader=SafeLoader))
clip_array = sum(clip_array, [])
while True:
tmptext = pyperclip.paste()
if tmptext != cliptext:
cliptext = tmptext
if cliptext not in clip_array:
clip_array.insert(0,tmptext)
if len(clip_array) > max_size:
clip_array.pop()
with open(history, "w") as f:
f.write(yaml.dump(clip_array))
time.sleep(1)
Script on GitHub https://github.com/alastairhm/automate_python/blob/main/clippy.py
Recently had a need to get the CA Thumbprint from the Root certificate of a domain. This was for creating OIDC provider for an AWS EKS cluster.
This is the solution I came up with.