Latest Entries »

Here’s a couple of quotes I gathered from a simple stroll around stackoverflow:

– Academic world: “Encapsulation ensures the safety of your code”
– CS world: “Encapsulation is a neat pattern to avoid bugs and ease readability.”
– Production world: “You’re not trying to add security by encapsulation, ARE YOU?”
– Python world: “We are all adults. Feel free to shoot yourself in the foot if you must.”

You see, back when I was beginning with Python,  I thought that encapsulation was an oppressive idea, fit to languages like Java that only seemed to make life difficult for programmers. But as I ease my mind into the possibility of enterprise scale development, the merits of encapsulation as a pattern become evident. Still, I do hate having to write two line getters and setters in Java.

So, does python have encapsulation? Of course it does:

– It’d seem that all class variables are public in Python. Not so. The variables live inside the object’s dict. What Python does is that it gives you default public getters and setters with neat syntax, and that’s all you need in the majority of cases. Need some specialized behavior? Just use property and override getters and setters with your own.

– Then there is encapsulation by convention. Don’t want someone accessing something from the outside? Prepend with single underscore. Someone REALLY shouldn’t rely on an implementation detail? Use double underscores which also mangles the name. There’s protected and private for you. Sure, no-one can really stop you from peaking inside and messing things up. But this convention is so strong in python that if you do, by design or mistake and not with a very, very good excuse, you might as well forfeit your pythonic license.

Python basically says: You are an adult, act like it. And treat others like adults too. But the pattern is still there.

Advertisements

Ok, let me state up front: Trying to get Processing to read your Arduino on linux (ones that get the /dev/ttyACM0 port) is broken! For all the good work all the devs are doing, this has been the case for far too long.

I spent a whole afternoon trying everything suggested on the RTXT site, Processing site, countless forums, including:

  • The version mismatch is at fault
  • You should be in the uucp group
  • Downgrading/Installing the TXRX package from Ubuntu
  • Throwing your linux machine off the cliff (not really tried)

Nothing of these work. And they shouldn’t be needed, because the Arduino IDE uses RXTX and still manages to work.

Why, you ask? Because our lovely Arduino devs recompiled RTXT doing two essential things:

  • Forget about locking the tty file
  • Have RXTX search ttyACM ports

So, let’s see how we can use their version of RXTX with Processing and forget this nightmare once and for all 😉

First, download the Arduino software for both 32bit and 64bit architectures from here http://www.arduino.cc/en/Main/Software .

Untar those files to some temporary folders, for which I’ll be using the names <arduino-32bit> and <arduino-64bit>

Let’s also name the folder where you have Processing lying around as <processing>.

Do the following:

  • Backup the <processing>/modes/java/libraries/serial/library folder for some good measure.
  • Copy <arduino-32bit>/lib/RXTXcomm.jar overwriting the one in <processing>/modes/java/libraries/serial/library
  • Copy <arduino-32bit>/lib/librxtxSerial.so overwriting the one in <processing>/modes/java/libraries/serial/library/linux32
  • Copy <arduino-64bit>/lib/librxtxSerial.so overwriting the one in <processing>/modes/java/libraries/serial/library/linux64
  • Copy <arduino-64bit>/lib/librxtxSerial64.so to <processing>/modes/java/libraries/serial/library/linux64
  • You’re done! 😉

Or, you could compile them from source to see what works for you, but really, why go through it?

Please, please, if you find this useful, try and locate some of all those forum threads regarding this problem and let them know ^^

So, yesterday NASA published a collection of sounds from historic spaceflights and current missions.
You can check them out here.

Well, I noticed there was no ‘download ALL’ button there, and because most links at a time were hidden by means of javascript my normal tools didn’t really work.

SO! It was time for some scripting!

First, I opened up the FireBug console [link] and tried to see if jQuery was used on the page.
Of course it was, BUT, with a twist. The $ shortcut was assigned to another library. No matter, I can still use jQuery ‘s full name.

So, let’s get a list with all the links to .mp3 files:

jQuery('a[href$=".mp3"]')

which means, get all anchor elements whose href attribute ends with “.mp3”

What should we do to them then?
Keeping it simple, let’s just print the href attribute for each of them on the console. So we go:

jQuery('a[href$=".mp3"]').each(
    function(){
        console.log(jQuery(this).attr('href'));
    })

Pretty straightforward. It’s a one-liner, but I indented here for clarity.

You’ll notice that at the end of the links, we also get an array of all the elements. Well, that’s because our whole statement also returns the collection.
So let’s copy-paste the links to a text file, let’s name it ‘in’.

........
/mp3/590318main_ringtone_135_launch.mp3
445945....min.js (line 30)
/mp3/577774main_STS-135Launchringtone-v2.mp3
445945....min.js (line 30)
/mp3/590196main_ringtone_135_landingCommanderComments.mp3
445945....min.js (line 30)
/mp3/590316main_ringtone_135_landingNaviusComments.mp3
445945....min.js (line 30)
/mp3/581549main_Apollo-8_Merry-Christmas.mp3
........

Oh bummer! We also got a lot of lines like “445945….min.js (line 30)” on them. Not really a problem, let’s crunch it up with python for the geek-out factor:
I ran this:

with open('in') as f:
    lines = f.readlines()
final = [line for line in lines if line.endswith('.mp3\n')]
with open('out', 'w') as f:
    f.writelines(final)
    f.write('\n')

Again, we only keep that lines that end with ‘.mp3’ (and the newline) as relevant.
What should we do with our list file now you ask? But of course use wget.
We want wget to grab all our .mp3s from the list in the file, prepending ‘http://www.nasa.gov&#8217;, as the urls are relative
Here it is:

wget -i b -B http://www.nasa.gov

And voila! Here is our collection of NASA mp3s for our geeky pleasure!

For the past couple of weeks I’m  (re)learning a couple of web frameworks. Namely, Yii for PHP and jQuery for JavaScript.

Almost always, my process when learning something new goes like this:

  1. Find an introductory tutorial or book to get a glimpse of what is possible and get a feeling of how it all works.
  2. Establish a robust source of documentation, which, at least for OSS stuff, will most probably be the guide found on the developers’ site.
  3. Play around with it. For me that almost always means “Make a simple game with it”

And this is what I came up with jQuery after just 2-3 days of getting serious about it:

http://pastehtml.com/view/b6ih9wnt2.html 😉

Such a great and easy framework!!

All code is embedded in the HTML in case you want to take a look.

Nick

The Morse Buzzer

This is the Arduino project I worked on while attending last weekend’s all-night hackfest at our local hackspace, P-space.

It is a simple Arduino project for playing back any text sent to the Arduino via the serial interface in Morse code.

The hardware setup is trivial, just hook up a buzzer in one of the Arduino pins (existing code uses pin 10) and you are set.

The pin and playback speed are configurable, and so is the mapping of ASCII characters to Morse sequences if you’d like to add something.

Since the Arduino reads from the serial link, any number of applications computer-side could send text to be played out. Maybe a twitter client in Morse? Or you could hook up an ethernet shield to your Arduino and have it fetch any web site you want to play it back.

Code-wise, this is definitely not my best written code. Damn, that chartobuffer() function looks ugly to me. But I’m happy that I stepped out of my Python comfort zone and explored the C-like language of Arduino with it’s quirks and limitations. This board is a beginner’s dream, just as advertized 😉

Code can be found in my bitbucket repositories, free for you to look and play. Link is here.

Oh, and here is a picture of me having great fun at it =)

Morse code

Nick

P3191597

This weekend was full of fun and WIN!

We had an all-night hackfest on both Friday and Saturday nights at our local hackerspace, P-Space. The event was the first of it’s kind for us and was an open invitation for local hackers to come, socialize and write code on Arduino (or whatever else) projects.

The event was a HUGE success. It marked an attendance of over 50 people, many that we hadn’t seen before, with various levels of experise, from begginer to pro, all hacking together in groups, attending classes, producing interesting sprint projects and generally having tons of fun.

The first day produced lots of introductory classes on Arduino hacking, then working in groups to produce a plethora of projects. Such were, to name a few in my vicinity:

  • A question and answer game, with an LCD and buttons interface (watch out for the annoying FAIL sound there when you got an answer wrong),
  • A guitar-hero push-the-right-button-when-a-LED-comes-on game,
  • An arduino playing the Star Wars Imperial Theme (geekgasm),
  • An interactive light writer with a LED array and accelerometers,
  • My own Morse code buzzer, which played back in Morse whatever text it was fed to the Arduino via the serial interface. More on a later post 😉
  • The award winning (the winners took home everything they used) project that notified you in real life of unread e-mails (via POP3) or a file changing on your computer.

And that’s just the projects which were happening on the room I was in, with lots others being fleshed out in 3 other rooms I unfortunately didn’t have time to look at.

The second day of the event was a special charm as our friend Dimitris held a great class on PCB manufacturing, covering hands-on the whole process of etching UV-sensitive copper boards. A rare and mind-blowing class. Thank you Dimitris =)

I’ll add material to projects and photos as I find them, in the meantime, you can check out powerfool’s Flickr for photos from the event.

Congrats,

A very happy (and sleepy) Nick
The class

Couple of days ago a drummer friend of mine asked for some help with setting up an I/O sound card for his home studio.

Said sound card was the Focuswrite Saffire Pro10

My friend who studies as a recording studio engineer had set up the box to his Windows PC via firewire and had done geat work installing all his 8 mics to his drum set.

What was needed from me was to hook up the audio outputs to his amplifier setup and most importantly, tinker his Cubase 5 installation to interface the sound card.

A couple of hours latter we had a great working system. Now, I never counted myself as having sound engineering expertise, but it’s great to know that after all those years, my all-things-computer common sense and sound systems knowledge enabled me to make this work flawlessly.

I rock!

Nick =)

Prompted by an article in the latest totalXAKER magazine, I decided to give VirtualBSD a try 😉

VirtualBSD is a FreeBSD 8.1 desktop installation, packaged as VMware appliance. Go check their website out.

Now, being more familiar with VirtualBox than VMware, I decided to take the bit more complicated installation decribed here. I’m delighted to report that everything works as a charm out of the box.

My VirtualBox installation is the Oracle proprietary one (not virtualbox-ose) at version 3.2.12 straight out of their ubuntu ppa. Host system is as always my Linux Mint 9 (which is based on Ubuntu 10.4).

Here are some additional changes/remarks to the VirtualBSD walkthrough for VirtualBox:

  • I personally didn’t like the way the .vmdk is split in the VirtualBSD folder. No problem there, just register the main .vmdk in Virtual Media Manager beforehand, and clone it using “VBoxManage clonehd” to a new file. Then discard the whole folder and just use the new file instead. The UUID of the virtual hard disk changes with this process, but it’s no problem because internally, VirtualBSD doesn’t hardcode UUIDs in it’s /etc/fstab like some linux distros do.
  • I also changed the Network parameters in VirtualBox to “Bridged adapter” instead of “NAT”. VirtualBSD’s networking was smart enough to configure its IP via DHCP. Great!
  • What the vmware2vbox script does is first uninstall any VMware specific software, then install virtualbox-ose Guest Additions. Now, since I’m not using virtualbox-ose, that is not optimal, but everything seems to be in order (with maybe USB missing, but untested). Removing that and installing the right GuestAdditions is still a work in progress which I reserve for when I’m more acquainted with FreeBSD internals (lazy).

So, that’s all for this installment and I hope I have a great time exploring FreeBSD via VirtualBSD and get some more of you to check it out. Great little appliance there 😉

Nick

VirtualBSD running in VirtualBox

So, here’s a recipe for destruction:

  • Open your Windows VM you have for uni classes to write up a nice little recursive version of a simple algorithm.
  • Put some printf statements there for debugging too. Should look something like this:
int eukleid(int x, int y)
{
    printf("%10d %10d", x, y);
    if(y=0) return x;
    else return eukleid(y, x%y);
}
  • Fail to notice that y=0 instead of y==0 sends your program to endless recursion
  • After 10 minutes of crashing in Windows, decide that, what the hell, I’ll try it in my linux machine.
  • Compile program
  • Instead of running in a shell, double click the executable in nautilus by negligence.
  • Watch your /home partition run out of space in seconds!!!!

Now, what happened is that stdout and stderr of programs started outside a terminal get logged in .xsession-errors. The recursion shouldn’t have gone forever, but, by some strange bad luck, it filled up what was left of my 5GB headspace I had in /home

Not finding the culprit at first glance, I decided to close all programs and restart to single-user to look into it. Unfortunately, closing the VM meant virtualbox trying to update the VM’s xml file. THAT didn’t go well.

So, I was left with an unusable VM which took me a couple of hours to restore to some working condition.

Hey, it could have been worse! All the other programs (firefox, thunderbird to name a couple) handled the problem nicely. And thank Cthulu I had a separate partition for /home. Can’t imagine the mayhem if the root partition had zeroed out.

But everything is swell! I now know a couple tricks more than yesterday and opportunities like this don’t come everyday 😉 Such is the mantra of a troubleshooter soul like me! =)

Nick

Python Object Reference

I have a friend struggling with some code wight now, so I’m copy-pasting an interactive session with some object reference basics that I hope will clarify some things 😉

There’s also a pastie for this in http://paste.lisp.org/+2E0W for easier reading.

>>> # First, lets make a Class to help with tests
>>> class Coco(object):
	def __init__(self, value=0):
		self.value = value

>>> # Lets make a list of instances, the dumb way
>>> a = [Coco()] * 4
>>> a
[<__main__.Coco object at 0xb64c9cac>, <__main__.Coco object at 0xb64c9cac>,
 <__main__.Coco object at 0xb64c9cac>, <__main__.Coco object at 0xb64c9cac>]

>>> # See that the addresses are the same? Lets look values
>>> [i.value for i in a]
[0, 0, 0, 0]

>>> # Now, let's try to change one of them
>>> a[0].value = 1

>>> # We now expect [1, 0, 0, 0], right?
>>> [i.value for i in a]
[1, 1, 1, 1]

>>> # NOPE! All of them changed because it's THE SAME OBJECT
>>> 

>>> # Let's do it again the smart way
>>> a = []
>>> for i in range(4):
	a.append(Coco())

>>> a
[<__main__.Coco object at 0xb64b906c>, <__main__.Coco object at 0xb64d128c>,
 <__main__.Coco object at 0xb64d1c2c>, <__main__.Coco object at 0xb64d1c0c>]

>>> # See that the addresses are different?
>>> [i.value for i in a]
[0, 0, 0, 0]

>>> a[0].value = 1
>>> [i.value for i in a]
[1, 0, 0, 0]

>>> # Pretty cool huh?
>>> 

>>> # Lets also put the list in variable b
>>> b = a
>>> [i.value for i in a]
[1, 0, 0, 0]

>>> [i.value for i in b]
[1, 0, 0, 0]

>>> # And change a value in b
>>> b[0].value = 0
>>> [i.value for i in b]
[0, 0, 0, 0]
>>> [i.value for i in a]
[0, 0, 0, 0]
>>> # a and b are the same object. A no brainer!
>>> 

>>> # Now let's copy once with copy() and once with deepcopy()
>>> from copy import copy, deepcopy
>>> a_copy = copy(a) # You can also do =a[:] for lists and =a.copy() for dicts
>>> a_deepcopy = deepcopy(a)
>>> a
[<__main__.Coco object at 0xb64b906c>, <__main__.Coco object at 0xb64d128c>,
 <__main__.Coco object at 0xb64d1c2c>, <__main__.Coco object at 0xb64d1c0c>]

>>> a_copy
[<__main__.Coco object at 0xb64b906c>, <__main__.Coco object at 0xb64d128c>,
 <__main__.Coco object at 0xb64d1c2c>, <__main__.Coco object at 0xb64d1c0c>]

>>> a_deepcopy
[<__main__.Coco object at 0xb64d1a4c>, <__main__.Coco object at 0xb64d1a2c>,
 <__main__.Coco object at 0xb64d1a0c>, <__main__.Coco object at 0xb64d19ec>]

>>> a_copy[0].value = 4
>>> a_deepcopy[1].value = 6
>>> # Can you foretell what will happen now?
>>> [i.value for i in a_copy]
[4, 0, 0, 0]

>>> [i.value for i in a_deepcopy]
[0, 6, 0, 0]

>>> [i.value for i in a]
[4, 0, 0, 0]

>>> # While a and a_copy are not the same, their contents are shared!
>>> # Here's how to try when in doubt
>>> a is b
True

>>> a is a_copy
False

>>> a[0] is a_copy[0]
True

>>> a[0] is a_deepcopy[0]
False

>>>
>>> # PS. If you make a list the dumb way
>>> a = [Coco()] * 4

>>> # ..you'll have a list of the same objects even after a deepcopy
>>> k = [Coco()] * 4
>>> k_deepcopy = deepcopy(k)
>>> k_deepcopy[0].value = 1
>>> [i.value for i in k_deepcopy]
[1, 1, 1, 1]

>>> k_deepcopy[0] is k_deepcopy[3]
True

>>> # ..although, not the same ones as the originals
>>> [i.value for i in k]
[0, 0, 0, 0]

>>> k_deepcopy[0] is k[0]
False

>>>
>>> # Hope that's helpful ;)
>>>

Nick