Using Luxafor with Linux

Luxafor makes a nice busy status indicator light for the office. Unfortunately it only provides software for Windows and Mac. But it’s not hard at all to get it working on Ubuntu with PyUSB.

1- You need PyUSB installed

pip install pyusb

2- PyUSB basically lets you do anything you want with the USB device including some pretty dangerous stuff so it’s naturally open to super users. It’d be really annoying to have to sudo every light control though, so we’ll open this one particular device to your user. Create a luxafor.rules file in /etc/udev/rules.d/ and put in:

SUBSYSTEM=="usb", ATTR{idVendor}=="04d8", ATTR{idProduct}=="f372" MODE="0664", OWNER="[your user name]"

Read More…

Iterating Python Lists 2 by 2

Not necessarily the most superior way performance/Pythonicity wise but it’s simple to remember and short:

list = [1, 2, 3, 4, 5, 6, ...]
pairs = zip(list[::2], list[1::2])
for pair in pairs:
    # do stuff

pair will be (1, 2), (3, 4) etc.
The 2 list slices turn the original into odd/even sets and zip merges them

Django ModelForm Required ForeignKey not Connecting?

You have a ModelForm based on a Model with a non-optional ForeignKey field. You already connected it via something like

instance = SomeClass(someRelatedObject = someObject)
form = SomeForm(request.POST, instance = instance)

but form.is_valid() is still giving you an error saying someRelatedObject can’t be null. One possible solution is to check your SomeClass’s definition of someRelatedObject. Try putting ‘editable = False’ in your ForeignKey constructor and it may solve your problem.

Django Development Server with HTTPS

Sometimes you can’t avoid using HTTPS right starting from development (such as for developing Facebook canvas applications). You can setup Apache on your local machine and go through a bunch of trouble setting up your domain and setting up the certificate etc but there’s an easy way of using Django’s python runserver to work with HTTPS.

This can be done with stunnel that lets the Facebook server and stunnel on your machine communicate in SSL and stunnel turns around to communicate with Python in HTTP. First install stunnel. For instance in Mac OS X:

brew install stunnel

Read More…

Django Compress Static Files and Compile CSS

I started off looking for a request time compiler of LESS for Django and initially found django-css which seems to serve the purpose great. Compressing static files on the fly is definitely a nice added bonus as well. It does so by containing a fork of django_compressor. But on further inspections, I jumped ship. The original project, django_compressor, sees a more regular update and is now Django 1.3 ready while the ‘successor’ isn’t. Funny thing is django_compressor supports compiling any CSS formats compilable via command line. With a better documentation overall, seems like the original has beat the sequel.

Validate and Format Addresses using Google API

Suppose you want to make some web app that lets users input addresses. I would be nice to

  1. Weed out minor misspelling
  2. Standardise format (proper capitalisation, abbreviated province/state or full name etc)
  3. Input in freeform but store civic number, street name, city etc separately
  4. Verify the address exists of course

Some governmental entities provide this information via public APIs but if you want a uniform service, why not use Google? Read More…

PIL: Get RGB Value from GIF

If you load a GIF file with PIL via‘giffile.gif’) and then try to look at its pixels, you would get integers instead of tuples since the GIF pixels refers to one of the 256 colours in the GIF colour palette. The palette would then contain the RGB value of the pixel.

To avoid all this hassle and just get RGB tuple directly:

gif ='giffile.gif')
rgbimage = GIF.convert ('RGB')
>>>(231, 10, 54)

Python Challenge Level 14: Italy – Code

Used a NumPy array. Doesn’t work if the diagonal+1s are white of course.

import Image
from numpy import array, zeros, uint8
line ='wire.png')
# third dimension is for colours
a = zeros((100, 100, 3), dtype=uint8)
step = array([0, 1])
position = array([0, 0])
count = 0
# continue until when the next spot expected to be unfilled is filled
while not max(a[tuple(position)]):
	# paint colour
	a[tuple(position)] = array(line.getpixel((count, 0)))
	# if reached a wall
	if max(position + step) > 99 or min(position + step) < 0 or max(a[tuple(position + step)]):
		# change direction
		step = (abs(step) ^ 1) * (-step[0] if step[0] else step[1])
	# advance
	position += step
	count += 1
# convert to image

Free OBD2 Software for Mac

If you just picked up a generic OBD-II – USB interface on eBay and have a Mac, it is true that it is generally more convenient to access these hardware on Windows. Even embedded device developers tend to use Windows to develop against these generic FT232R chip based USB-UART devices simply because of more available supports. But worry not, it can be done on Mac (really on Linux with a more open-platform framework than Windows), it’s just a bit more complicated since it tends to come in be open-source source code rather than prepackaged self-sufficient binaries. Read More…

1 2  Scroll to top