switch-case in python

Python is a great language if you’re coding to IoT (Internet of Things) like small devices. If you’re coming from another language though you might be surprised that it doesn’t include the classic switch-case statement construct like you’d find in JavaScript, for example.

Fortunately, I just managed to create something that seems to work and the syntax isn’t too far off from the expected.

def switch(key, default):
    case = {
        0.0: 'zero-point-zero',
        0.1: 'zero-point-one',
        0.2: 'zero-point-two',
        0.3: 'zero-point-three',
        0.4: 'zero-point-four'
    }
    return case.get(key, default)

print switch(0.1, 'Unknown')
Running this would produce “zero-point-one”. This isn’t as robust as JavaScript’s or C’s implementation but this can be adjusted for lambda functions in a similar fashion.

 

python

remember internet radio on itunes?

Years ago on macOS and when iTunes first came out, it included an awesome feature—you could easily stream Internet radio from within the interface in iTunes. Apparently that got in the way of Apple’s revenue on iTunes, always trying to sell you something.

morpheus

iTunes -> click on the down arrow next to Library -> Ctl+click Songs -> Edit List -> add a checkmark next to Internet Radio

 

And that’s all it takes to return this sought-after feature to iTunes.

Screen Shot 2019-02-11 at 12.06.24 PM

kivy, the hero of iot gui developers

Kivy – An open-source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps.

The Internet of Things (IoT) is the likely future of gadgets and devices that you’ll have in your homes and cars (if you don’t already) as well as technology that you wear. As a minimal criteria, these things use the cloud to gather and store information. In a way, even smartphones fall into this category if you think about it. Amazon’s Echo device is a good example.

For many of us who self-identify as “makers”, we use small computers with similar capabilities and we create these type of gadgets. Often when we’re coding software in this space, the Python language is the usual choice for the task.

Until now, we’ve not had many options for displaying graphical menus on such tiny screens other than the full “Desktop” GUI of some Linux-like operating system which didn’t really work, to be honest.

Introducing Kivy

A relatively new technology is the Kivy library. Imagine being able to describe the many screens you’d find in an application, whether it’s a smartphone or the touchscreen of a printer or even a watch. Then Kivy takes care of the rest for you, rendering those screens using a graphics engine behind-the-scenes. It even manages clicks and other gestures, getting these to fire off portions of your code.

Kivy comes equipped with an impressive collection of pre-defined screen widgets as well as the ability to create your own custom types. And you get all this for the low, low price of free (unlike its $5K+/year—priced competitor Qt).

I’ve had the pleasure of working on an almost daily basis with Kivy over the last two months and I must say that I’m still just as fond of it now as the day I originally learned of it.

If you’re a coder and you know Python, I would suggest that you add Kivy to your toolbelt. You’ll find that it’s easy to use and worth the effort you put into it.