Opinions on my unicorn hat extension?


#1

I have made a small python modular thing for unicorn hat on GitHub Link here.

I would like reviews on it and suggestions for improving it.

Thanks.


#2

First spot- watch out for Python2/3 compatibility. It might not be on your radar, but so you know:

In Python 2:

1/100 = 0

And in Python 3:

1/100 = 0.01

This is because Python 2 treats the two numbers as integers and performs integer division with an integer result.

For back-compatibility you could use:

int(b/100.0)

I’d also replace import unicornhat as uh with import unicornhat and rename all instances of uh. to unicornhat. since it’s not necessary to introduce ambiguity for the sake of saving a few keys.

Good stuff, though. There might be a way to import all of the unicornhat methods into your module so you just import your add-on and can use it as a total replacement.

Perhaps this is one of those cases where from unicornhat import * is okay :D


#3

Thanks for the suggestions, i have made the changes 👍


#4

Also is there any way i can install the library onto my pi in the same way as the unicorn hat so it won’t require the unicorn_expanded.py in the same folder?


#5

You will have to set it up as a Python library. Take a look at the setup.py and MANIFEST.in files in any of our Python libraries and tweak accordingly.

I’d suggest starting with Blinkt! since it’s one of the simpler libraries, and changing it to include your code.


#6

i’m a bit confused by what you just said


#7

setup.py is the file that has all the information about a library, see: https://github.com/pimoroni/blinkt/blob/master/library/setup.py

And MANIFEST.in is the file that lists all of the files important to your library, see: https://github.com/pimoroni/blinkt/blob/master/library/MANIFEST.in

Once you’ve set up these files properly, you will be able to install your new library and use it from anywhere.

You install by running sudo python ./setup.py install