Saturday, October 26, 2013


Ah, Zolpidem.

Zolpidem is all I want.
Zolpidem is all I need.
Every night when I lack sleep,
Zolpidem I need for peace.

Wednesday, September 4, 2013

Python Decorators

I've been learning a lot about Python lately. Specifically about decorators, generators and other multiprocessing.

Here is a pretty good blog post about decorators:

Saturday, July 20, 2013

Modifying Subtitles with Python

Ok, so I have not been completely idle despite my debilitating sickness. I have actually been working on a problem regarding downloaded movies and their subtitles. I noticed that sometimes, movies that I have acquired have no subtitles. And the subtitles that I have downloaded from sites like are not synchronized with the movie itself.

So I spent an hour or so to write a simple Python app (now on Github) that will correct the problem. Just determine how far forward to push the subtitles so that it would be in synch with the movie.

For example, the following will push the subtitle movie by 2 minutes and 3 seconds:

python 00:02:03,000

I've tried it on a couple of subtitles. It all works great so far. Of course, it only pushes the subtitles forward -- not backward. It also does not do any error checking. It's also probably not very efficient.

Ah well, it does the job as I need it so far...

Sunday, July 14, 2013

Securing Documents

Several months ago, a NSA contractor named Ed Snowden released confidential   information which he had obtained as part of his job. 

The result of his whistle blowing/traitorous activities was felt around the world. People were outraged that the NSA was monitoring the emails and phone records of ordinary Americans. 

So how can the NSA prevent future breaches like this?

I think that, for each confidential document, they should separate how they got the information from what the information actually contains. Then just encrypt the how and make it available to those with the proper security clearance.

For example, suppose you have a confidential document XYZ and that document contains confidential information (the sky is falling) the source of the information (chicken little said so). If you want document XYZ available throughout the organization then just encrypt the source of it. That way even if it does get leaked -- the details of it will be based on the credibility of the leaker and not on the NSA itself. Making it hard to copy files to a physical device also helps. 

Also works for newspaper journalists. 

Sunday, July 7, 2013


I have been sick for nearly 2 months. No motivation to write any code. Ugh.

Why Google's Go Programming Language Sucks

They say that history repeats itself. Sometimes, this is made quite event in the IT field due to the fast pace of technology. I would hazard to guess that there is a general feeling that when history repeats itself in IT, that is generally considered not a good thing. Unless there is a radical change, reinventing something that has previously existed and touting it as "the next big thing", is not really productive.

Google's programming language "Go" is a good example. The language was invented at around 2007; After that, Google announced the new programming language to the world at around 2009. So what problem does Go solve? None. It's just a language that is basically like C with a few niceties here and there.

But really, why does Google Go suck (in my humble opinion)? Let me count the ways.

1. No Object Oriented Programming.

There is no inheritance, no method overloading, object oriented programming. Other than C or Javascript (which have been around for a while), what language does doesn't have it? OOP is proven and yet the designers of Go have decided to ignore it.

Why even bother with it then? Just use server-side Javascript with V8 to do everything.

2. No asserts. 

God, why not?

3. Pointers.

This is perhaps just a personal preference. Coming from a Java/Python background, I simply prefer languages that get out of the way and let me express what I want without having to worry too much about details. Like pointers. I had enough of that when I was doing C. Not that it was that bad, it's just... why put it in a brand new language?

4. Mascot.

The mascot sucks. Purely, subjective, I know.

Well, that ends my rant. Basically, Google's Go doesn't really do anything for me at all because I consider it a step back in terms of programming languages. There is an adage "use the right tool for the job". However, sometimes someone invents a tool, slaps "Google" on it, markets and popularizes it, that actually doesn't ever seem to be the right tool for any job.

Saturday, June 1, 2013

Iterating in Python

I was looking into creating a function which would convert a dictionary filled with unicode strings to a dictionary filled with non-unicode (ANSI) strings. I'm using Python 2.7.4. I found the answer to this in Stack Overflow post:, but the given solution didn't quite work for me.

I discovered that Python treats strings as iterable collections:

>>> import collections
>>> isinstance(u'abc', collections.Iterable)

A unicode string is iterable? It makes sense because I can do this:

>>> for c in u'abc':
>>>  print c

An ordinary string is also iterable:
>>> import collections
>>> isinstance('abc', collections.Iterable)

This means I need to tweak my function to check to see if an input is a string type first before checking to see if it is iterable. Here's what I got:

import collections
import types
def convert(in_data):
    if isinstance(in_data, types.StringTypes):
        return in_data.encode('utf-8')
    elif isinstance(in_data, collections.Mapping):
        return {convert(key): convert(value) for key, value in in_data.iteritems()}
    elif isinstance(in_data, collections.Iterable):
        return [convert(element) for element in in_data]
        return in_data

Now for a quick test:

    def test1(self):
        unicode_d = {u'key1': u'val1', u'sub_key': {u'name1': u'value1', u'name2': u'value2'},
             u'list': [1, u'a', u'b', 2, [u'you', u'and', u'me', 221, 321]]}
        ansi_d = {'key1': 'val1', 'sub_key': {'name1': 'value1', 'name2': 'value2'},
             'list': [1, 'a', 'b', 2, ['you', 'and', 'me', 221, 321]]}

        self.assertTrue(convert(unicode_d) == ansi_d)
        self.assertTrue(convert(unicode_d) == convert(ansi_d))

Of course, this function doesn't handle all possibilities like None types. It will also convert sets into lists...

Thursday, May 23, 2013

Using NANT with Visual Studio 2012

I have used Apache's Ant for quite a while. Now in there is NANT in the C# and .NET world. However Visual Studio only uses MSBUILD. Here's how you make it so that Visual Studio will use NANT instead:

First, open up the .csproj file and modify the DefaultTargets to "Nant" as follows:
<Project ToolsVersion="4.0" DefaultTargets="Nant" xmlns="">

Next, add the following at the bottom of the file:

<Target Name="Nant">
  <Exec Command="CALL nant.exe -D:buildtype=$(Configuration)" />

Now, in your NANT's file, add this as the build target:

  <target name="build" description="Compiles the source code">

    <echo message="Build Type: ${buildtype}"/>
    <property name="debug" value="false"/>
    <if test="${buildtype == 'Debug'}">
      <property name="debug" value="true"/>
    <echo message="Debug = ${debug}"/>

    <!-- Make sure the build directory is present -->
    <mkdir dir="${build.dir}" unless="${directory::exists('build')}" />

    <!-- Compile sources -->
    <csc target="library" output="${build.dir}\${project::get-name()}.dll" debug="${debug}">
        <include name="**/*.cs" />
        <include name="${log4net.dir}\log4net.dll"/>

    <!-- Copy log4net over. -->
    <copy file="${log4net.dir}\log4net.dll" tofile="${build.dir}\log4net.dll"/>


This will use whatever build configuration used in Visual Studio. It will also set debug mode if you build it in Debug (to generate those .pdb files).

Wednesday, May 15, 2013

Log4net not found

I have a console application. I added log4net as a reference. I included it in my program with the using keyword. I get an error. 

The type or namespace name 'log4net' could not be found (are you missing a using directive or an assembly reference?)

Yes, I really did add it as a reference. Yes, I changed my Target Framework to ".NET Framework 4.5". What now?

Here's the solution. Include System.Web as a reference to your project. It then compiles successfully! Argh!

What is even more aggravating is that it still seems to compile after you remove System.Web! Double argh!!

This is very misleading. And why would a logger need any kind of dependencies is beyond me. 

This kind if thing would never happen in Java. If there was a dependency in one of the libraries it would just throw a runtime exception. Sigh. Oh well. 

Monday, May 13, 2013

Mutable-Immutable Data Transfer Objects in C#

The getters and setters in C# can make implementing POCOs easy. But sometimes when you are writing an API you need an object that can be modified internally but is read-only externally. In my mind, it is ideally better to have both objects strongly related to each other because if you remove one field or property in one, then it is removed for the other.

So how is this done?

You can't do this:
    public class ImmutableThing
        int Id { get; }
        string Name { get; }

    public class MutableThing : ImmutableThing 
        int Id { get; set; }
        string Name { get; set; }

Because the compiler will complain. "Automatically implemented properties must define both get and set accessors." Having the Immutable object extend the Mutable object yields the same result. 

To solve this, an Interface must be defined:

    public interface IThing
        int Id { get; }
        string Name { get; }

Now have the Immutable object extend from the interface:

    public class ImmutableThing : IThing
        private int id = 0;
        private string name = null;
        public ImmutableThing(int id, string name)
   = id;
   = name;
        public int Id { get { return id; } }
        public string Name { get { return name; } }

And the Mutable interface:
    public class MutableThing : IThing
        public int Id { get; set; }
        public string Name { get; set; }

You can have the fields in an abstract class so that you can have methods available for both immutable and mutable objects:
     public abstract class AbstractThing
        protected int id = 0;
        protected string name = null;

    public class ImmutableThing : AbstractThing, IThing

        public ImmutableThing(int id, string name)
   = id;
   = name;
        public int Id { get { return id; } }
        public string Name { get { return name; } }

    public class MutableThing : AbstractThing, IThing

        public int Id 
            get { return id; }
            set { = value; }
        public string Name 
            get { return name; }
            set { = value; }

I wish Visual Studio 2012 can tell me right away if it won't compile. I currently have to rebuild it each time.