Simple Tricks To Make Your Python Code Faster

Python has become one of the most popular programming languages out there, particularly for beginners and those new to the hacker/maker world. Unfortunately, while it’s easy to  get something up and running in Python, it’s performance compared to other languages is generally lacking. Often, when starting out, we’re just happy to have our code run successfully. Eventually, though, performance always becomes a priority. When that happens for you, you might like to check out the nifty tips from [Evgenia Verbina] on how to make your Python code faster.

Many of the tricks are simple common sense. For example, it’s useful to avoid creating duplicates of large objects in memory, so altering an object instead of copying it can save a lot of processing time. Another easy win is using the Python math module instead of using the exponent (**) operator since math calls some C code that runs super fast. Others may be unfamiliar to new coders—like the benefits of using sets instead of lists for faster lookups, particularly when it comes to working with larger datasets. These sorts of efficiency gains might be merely useful, or they might be a critical part of making sure your project is actually practical and fit for purpose.

It’s worth looking over the whole list, even if you’re an intermediate coder. You might find some easy wins that drastically improve your code for minimal effort. We’ve explored similar tricks for speeding up code on embedded platforms like Arduino, too. If you’ve got your own nifty Python speed hacks, don’t hesitate to notify the tipsline!

Audio field emission map

Audio Sound Capture Project Needs Help

When you are capturing audio from a speaker, you are rarely capturing the actual direct output of such a system. There are reflections and artifacts caused by anything and everything in the environment that make it to whatever detector you might be using. With the modern computation age, you would think there would be a way to compensate for such artifacts, and this is what [d.fapinov] set out to do.

[d.fapinov] has put together a code base for simulating and reversing environmental audio artifacts made to rival systems, entirely orders of magnitude higher in cost. The system relies on similar principles used in radio wave antenna transmission to calculate the audio output map, called spherical harmonic expansion. Once this map is calculated and separated from outside influence, you can truly measure the output of an audio device.

The only problem is that the project needs to be tested in the real world. [d.fapinov] has gotten this far but is unable to continue with the project. A way to measure audio from precise locations around the output is required, as well as the appropriate control for such a device.

Audio enthusiasts go deep into this tech, and if you want to become one of them, check out this article on audio compression and distortion.

A 2D simple regression analysis.

Making Math Less Stressful With A Python Super-Calculator

In a recent write-up, [David Delony] explains how he built a Wolfram Mathematica-like engine with Python.

Core to the system is SymPy for symbolic math support. [David] said being able to work with symbolic math easily has helped his understanding of calculus and linear algebra. For statistics support he includes NumPy, pandas, and SciPy. NumPy is useful for creating multidimensional arrays and supports basic descriptive statistics such as mean, median, and standard deviation; pandas is a library for operating on tabular data arranged into “DataFrames”, it can load data from spreadsheets (including Excel) and relational databases; and SciPy is a “grab bag” of operations designed for scientific computing, it includes some useful statistics operations, including common probability distributions, such as the binomial, normal, and Student’s t-distribution.

For regression analysis [David] includes statsmodels and Pingouin. If you’re not familiar with the term “regression analysis” it basically refers to the process of curve fitting. When your data is two-dimensional, with one dependent variable, the simple linear regression algorithm will generate a function that fits the data as y = mx + b, including the slope (m) and the y-intercept (b); this can be extrapolated to higher dimensional data and other types of regression.

If you have an interest in symbolic math you might enjoy learning about Mathematica And Wolfram On The Raspberry Pi.

Creating Python GUIs With GIMP

GUI design can be a tedious job, requiring the use of specialist design tools and finding a suitable library that fits your use case. If you’re looking for a lightweight solution, though, you might consider just using a simple image editor with a nifty Python library that [Manish Kathuria] whipped up.

[Manish’s] intention was to create a better-looking user interface solution for Python apps that was also accessible. He’d previously considered other Python GUI options to be unimpressive, requiring a lot of code and delivering undesirable results. His solution enables the use of just about any graphic you can think of as a UI object, creating all kinds of visually-appealing possibilities. He also was eager to make sure his solution would work with irregular-shaped buttons, sliders, and other controls—a limitation popular libraries like Tkinter never quite got around.

The system simply works by using layered image files to create interactive interfaces, with a minimum of code required to define the parameters and performance of the interface. You’re not strictly limited to using the GIMP image editor, either; some of the examples use MS Paint instead. Files are on Github for those eager to try the library for themselves.

We’ve featured some neat GUI tools before, too, like this library for embedded environments. Video after the break.

Continue reading “Creating Python GUIs With GIMP”

Two hands working a TekaSketch

TekaSketch: Where Etch A Sketch Meets Graph Theory

The Etch A Sketch was never supposed to meet a Raspberry Pi, a camera, or a mathematical algorithm, but here we are. [Tekavou]’s Teka-Cam and TekaSketch are a two-part hack that transforms real photos into quite stunning, line-drawn Etch A Sketch art. Where turning the knobs only results in wobbly doodles, this machine plots out every curve and contour better than your fingertips ever could.

Essentially, this is a software hack mixed with hardware: an RPi Zero W 2, a camera module, Inkplate 6, and rotary encoders. Snap a picture, and the image is conveyed to a Mac Mini M4 Pro, where Python takes over. It’s stripped to black and white, and the software creates a skeleton of all black areas. It identifies corner bridges, and unleashes a modified Chinese Postman Algorithm to stitch everything into one continuous SVG path. That file then drives the encoders, producing a drawing that looks like a human with infinite patience and zero caffeine jitters. Originally, the RPi did all the work, but it was getting too slow so the Mac was brought in.

It’s graph theory turned to art, playful and serious at the same time, and it delivers quite unique pieces. [Tekavou] is planning on improving with video support. A bit of love for his efforts might accellerate his endeavours. Let us know in the comments below!

Continue reading “TekaSketch: Where Etch A Sketch Meets Graph Theory”

Wood bent into a spiral

Make Magical-Looking Furniture With Kerf Bend Wizard

The intersection between “woodworkers” and “programmers” is not a densely populated part of the Venn diagram, but [Michael Schiebler] is there with his Kerf Bend Wizard to help us make wood twist and bend like magic.

Kerf bending is a fine technique we have covered before: by cutting away material on the inside face of a piece of wood, you create an area weak enough to allow for bending. The question becomes: how much wood do I remove? And where? That’s where Kerf Bend Wizard comes to the rescue.

More after the break…

Continue reading “Make Magical-Looking Furniture With Kerf Bend Wizard”

ManiPylator focusing its laser pointer at a page.

Simulation And Motion Planning For 6DOF Robotic Arm

[Leo Goldstien] recently got in touch to let us know about a fascinating update he posted on the Hackaday.io page for ManiPylator — his 3D printed Six degrees of freedom, or 6DOF robotic arm.

This latest installment gives us a glimpse at what’s involved for command and control of such a device, as what goes into simulation and testing. Much of the requisite mathematics is introduced, along with a long list of links to further reading. The whole solution is based entirely on free and open source (FOSS) software, in fact a giant stack of such software including planning and simulation software on top of glue like MQTT message queues.

The practical exercise for this installment was to have the arm trace out the shape of a heart, given as a mathematical equation expressed in Python code, and it fared quite well. Measurements were taken! Science was done!

We last brought you word about this project in October of 2024. Since then, the project name has changed from “ManiPilator” to “ManiPylator”. Originally the name was a reference to the Raspberry Pi, but now the focus is on the Python programming language. But all the bot’s best friends just call him “Manny”.

If you want to get started with your own 6DOF robotic arm, [Leo] has traced out a path for you to follow. We’d love to hear about what you come up with!

Continue reading “Simulation And Motion Planning For 6DOF Robotic Arm”