Ruby eating up all my CPU resources

I’ve recently been programming in Ruby on both of my Macs, a MacBook Pro with an Intel Core 2 Duo and 3 GB RAM, and an iMac with an Intel Core i3 and 4 GB RAMs. Well, I’ve been mostly using irb, the Ruby interpreter, for loads and loads of tests while writing the actual code. I usually test fragments of code interactively to see if things are working, and, thus, I open a few terminal tabs and fire up irb in them.

The thing is that I’ve noticed in the iStat Pro widget that the CPU is 90%–100% active… And both have at least two cores… with HyperThreading! Even when I quit the terminal app (iTerm in my case), the CPU is still working a lot! Then and idea came to me: launch Activity Monitor and see what application is consuming all those resources. What a surprise when I see 4 ruby processes eating up 90-ish% of the CPU!

After forcing quit those processes, everything becomes normal again! Notice the CPU usage graph before and after:

And I guess it wasn’t only eating up the CPU resources, but also the memory. And that’s happened to me with both computers. Unbelievable!

Android Tutorials, by Android Research Blog

I’ve recently discovered Android Tutorials, a collection of tutorials and recipes by Android Research Blog regarding Android programming.

I’ve been interested in Android programming for a while, but never had the time to start learning. I’ve read a few magazine articles on Android programming, but I haven’t found a good introductory tutorial either, and I think some good recipes are also necessary for doing some things (like, e.g., a good preferences panel).

Anyway, I guess this is gonna be a good resource when I start learning, and, after taking a look at some of the articles, it looks promising!

Generating timetables with Python

I hope I’ll be going next semester to Germany as an Erasmus student, and I’m now struggling with the Learning Agreement, courses, and timetables. In particular, there are a lot of interesting courses being offered by my guest university, but there are many factors to take into account when deciding what course you’re going to take, and one of them is timetable compatibility.

So, after a lot of struggle with the university’s website and trying to find some timetables, I’ve thought of making a timetable where I’ll put all the courses I’m interested in, so that I can see which ones are compatible with each other, i.e., which ones don’t overlap.

But the timetable has time slots of 15 minutes, from 8:00 to 18:15, and, of course, I’m naturally lazy. I don’t want to create a spreadsheet with as many rows as the 15-minutes-slots there are in the timetable!

And here’s when Python comes to the rescue!

I’ve coded a Python class that represents time, and which has methods for addition and logical comparison:

class Time:
    def __init__(self, hour, minutes):
        self.hour = hour
        self.minutes = minutes
    def normalize(self):
        hour = self.hour
        minutes = self.minutes
        quotient = minutes / 60
        if quotient > 0:
            hour += quotient
            minutes = minutes % 60
        self.hour = hour
        self.minutes = minutes
        return self
    def __add__(self, t):
        """add two times (sum)"""
        hour = self.hour + t.hour
        minutes = self.minutes + t.minutes
        res = Time(hour, minutes)
        return res
    def __mul__(self, k):
        """multiply a time and an integer constant k (product)"""
        hour = self.hour * k
        minutes = self.minutes * k
        res = Time(hour, minutes)
        return res
    def __lt__(self, t):
        """less than"""
        if self.hour < t.hour or (self.hour == t.hour and self.minutes < t.minutes):
            return True
            return False
    def __eq__(self, t):
        if self.hour == t.hour and self.minutes == t.minutes:
            return True
            return False
    def __le__(self, t):
        """less or equal"""
        return self < t or self == t
    def __gt__(self, t):
        """greater than"""
        return not self <= t
    def __ge__(self, t):
        """greater or equal"""
        return self > t or self == t
    def __ne__(self, t):
        """not equal"""
        return not self == t
    def __str__(self):
        hour = fill(str(self.hour), 2, '0')
        minutes = fill(str(self.minutes), 2, '0')
        return '%s:%s' % (hour, minutes)

I’ve implemented all the basic logical operators for comparison, because they’re very easy to implement, but we won’t be needing all of them.

In addition, I’ve also coded the __str__ method, which allows you to convert Time objects into Python strings by just using the str() function. This method needs a function called fill, which is intended to pad or fill a string with some character, so that the string reaches a given maximum size. That is, for example, when you have a string '8' that you want to be 2 characters long; you would fill it with zeroes until you reach that length: '08'. The code for this function is pretty simple:

def fill(s, size, c=' ', position='before'):
    """s: string; c: char"""
    if position == 'before':
        s = c * (size - len(s)) + s
    elif position == 'after':
        s += c * (size - len(s))
    return s

And, finally, this is the function to generate the time slots from a start time up to a given end time by some time increment:

def generate_timetable(start_time, interval=Time(0, 15), times=5, end_time=None):
    timetable = []
    if end_time is None:
        end_time = start_time + interval*times
    time = start_time
    while time < end_time:
        timetable.append(tuple([time, time + interval]))
        time += interval
    return timetable

In my case, I’ll just execute the following in the Python interpreter:

start_time = Time(8, 0)
end_time = Time(18, 15)
for start, end in generate_timetable(start_time, end_time=end_time):
    print '%s-%s' % (start, end)

This prints the time slots that should go into each row of the spreadsheet, so I’ll just have to copy that, and I’ll be able to start filling my timetable!

Evan Miller: Rank Hotness With Newton’s Law of Cooling

Evan Miller: Rank Hotness With Newton’s Law of Cooling

Evan Miller: How Not To Sort By Average Rating

Evan Miller: How Not To Sort By Average Rating

Via Jeff Croft’s blog.

Lea Verou: In defense of reinventing wheels

Lea Verou: In defense of reinventing wheels

Pretty interesting article on reinventing wheels and DRY (well, not quite “yourself”). Found via Jeff Croft’s blog.

H2O templates not working with functions (method calls)

I love Django. And I love its template engine. It’s simply awesome, and template inheritance is such a powerful tool!

So, when I started developing in PHP with CodeIgniter, I hated its views and templates from the very beginning. Every second I dreamt of using Django views… And then I thought “Then I’ll code such an engine for PHP mysel!”

But, hey, are you mad? What are you thinking about? I don’t have time for that, and I don’t want to spend my time doing that, either. The first thing you should do is look up on the internet if there’s something like that!

And I found H2O (among others) and started using it. And happiness came to my life! …Until I realized I couldn’t evaluate method calls in the templates (although the syntax supports that).

So, if a class Field has a method widget_html(), and an instance variable field of class Field is visible from the template, this wouldn’t work:

{{ field.widget_html|safe }}

But why? If that’s supported by the syntax! After digging a bit on the internet (well, I didn’t have to dig that much; it was the second result), I found the solution on Stack Overflow. The issue is due to H2O needing to be told what methods are safe to be called from the templating language, and the way to do that is add an attribute to you class (in this case Field) where you specify the method names that are suitable for being called from the templates:

class Field {
    var $h2o_safe = array('label_html', 'widget_html');
    // ...

where we’re telling H2O that both methods label_html and widget_html of class Field are safe to be called.