Friday, January 13, 2017

Tricks with Django URLs and knowing you suck doing RE

RE or regular expressions and I don't really see eye to eye. But as a programmer, I have to work with them. So it's awkward like your ex works in the same office as you. But lucky for us, we just stick to a couple of tricks to make it a bit palatable.

Numbers in URL

You'll need to use a \d+ in the pattern.

ex: (r’^status/(\d+)$’, ‘myapp.backbone_view’)

This would work on /api/status/1

Dealing with spaces

I found two ways to do this but then found out that they are the same: [\w|\W]+ AND .+

Yes that's a dot.

ex: (r’^status/([\w|\W]+)$’, ‘myapp.backbone_view’) OR
(r’^status/(.+)$’, ‘myapp.backbone_view’)

This would work on /api/status/dead on arrival

The browser will just replace the spaces with %20 and it should work.


Thursday, December 15, 2016

Back to teaching but part time mind you

So I'm just back to teaching part time at USTP which is formerly MUST. I've been asked to teach Object Oriented Programming. And this is what I've noticed.

Starting with the OOP syllabus, I noticed:
  • It's outdated as fuck. For example, it still mentions "Java applets". Yes, that's 1998 for you.
  • The syllabus reads like it's a Java 101 class instead of purely OOP class. 
Aside from the syllabus, the lab setup is wrong.
  • "Standalone Lab" which translates to "Computer Lab WITH NO Internet". Yes, we're back to 1998 again. All Java programming to date requires Internet access. Heck, ALL programming activity requires Internet access.
  • This assumption of using IDEs (Netbeans, in this case) at the start. I disagree with this. IDEs hide or automates the actual process of programming. Not understanding the actual process doesn't make for good devs or programmers.
This complaints are besides the point, what I really found was a wide, gaping disconnect on how OOP is seen in the industry and in academia. This could also be true for other IT related subjects.

We need to find a way to close the gap because if not we are condeming a generation of students, workers to "Hello sir, may I take your order?"

And that is just fucked up.

Thursday, October 27, 2016

SimpleDictDiffer.py when deepdiff is too much

Working with Python for the past 3 months coming from Java has been interesting (grossly understated).

Anyhow, I found myself needing a way to find the stuff (not just difference) between two dictionaries and a quick google led me to deepdiff by Sep Dehpour. It was good but found it a tad overkill for what I was doing. So another round of google and stackoverflow allowed me to cobble together this:

class SimpleDictDiffer(object):
    """
    Calculate the difference between two dictionaries as:
    (1) items added
    (2) items removed
    (3) keys same in both but changed values
    (4) keys same in both and unchanged values
    """

    def __init__(self, first_dict, second_dict):
        self.current_dict, self.past_dict = first_dict, second_dict
        self.set_current, self.set_past = set(first_dict.keys()), set(second_dict.keys())
        self.intersect = self.set_current.intersection(self.set_past)

    def added(self):
        return self.set_current - self.intersect

    def removed(self):
        return self.set_past - self.intersect

    def changed(self):
        return set(o for o in self.intersect if self.past_dict[o] != self.current_dict[o])

    def unchanged(self):
        return set(o for o in self.intersect if self.past_dict[o] == self.current_dict[o])

A github gist for this is also available.