Saturday, October 26, 2013

Zolpidem

Ah, Zolpidem.

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



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:

http://pythonconquerstheuniverse.wordpress.com/2012/04/29/python-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 opensubtitles.org 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 subtitle.py IronMan3.srt 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

Sick

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: http://stackoverflow.com/questions/1254454/fastest-way-to-convert-a-dicts-keys-values-from-unicode-to-str, 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)
True

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

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

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

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]
    else:
        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="http://schemas.microsoft.com/developer/msbuild/2003">

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

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

Now, in your NANT's Default.build 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"/>
    </if>
    <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}">
      <sources>
        <include name="**/*.cs" />
      </sources>
      <references>
        <include name="${log4net.dir}\log4net.dll"/>
      </references>
    </csc>

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

  </target>

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)
        {
            this.id = id;
            this.name = 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)
        {
            base.id = id;
            base.name = 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 { base.id = value; }
        }
        public string Name 
        { 
            get { return name; }
            set { base.name = value; }
        }
    }

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

Sunday, May 12, 2013

사다리 타기 (Sadari Tagi)

Today I learned about  Sadari Tagi (사다리 타기) a Korean way of drawing straws. It is a way to randomly assign N number of people to N number of tasks.

Process

First, a number of vertical lines are drawn, with numbers from 1 to N at the top and the tasks to be completed at the bottom (in random order). The numbers at the top correspond to each person. After that, a random number of staggered lines are drawn which interconnect each column.

For the picking process, a number is randomly picked. The idea is to follow down the line in the column. When an interconnecting line is first reached, cross over to the next column. After that, continue downward until another interconnecting line is reached. This is done until the task is reached at the end.

The following is an example. 3 people are supposed to complete 3 tasks. The mapping between person and task is random. The diagram is as follows (I made it horizontal instead of vertical):

1 ------------------------------------------ Task 1
              |           |       |
2 ------------------------------------------ Task 2
       |             |
3 ------------------------------------------ Task 3

So there are 3 interconnecting lines between 1 and 2, while there are only 2 interconnecting lines between 2 and 3. The number of interconnecting lines is irrelevant provided that they are staggered.

Here is an example, suppose 3 individuals (Jay, Sam and Bob) are required to perform three odious tasks. Which individual performs which task? If we use the Sadari Tagi (사다리 타기), we would construct the following diagram:

Jay ------------------------------------------ Task 1
              |           |       |
Sam ------------------------------------------ Task 2
       |             |
Bob ------------------------------------------ Task 3

To find out which task Jay is to do, start from Jay and move down until we hit the first intersection. Keep going down until we hit the second intersection. Keep going down until we reach Task #3.

Jay ------------------------------------------ Task 1
              |           |       |
Sam ------------------------------------------ Task 2
       |             |
Bob ------------------------------------------ Task 3

Using the same process, you will see that Sam is assigned to odious Task #2, and Bob is assigned to odious Task #1. (Apologies for the ASCII art!)

Question

My question is how do we know that there will always be a one-to-one mapping between number and task? So, could it be that, depending on the interconnected lines drawn, could it be that Sam and Bob be assigned to the same task? After constructing several such diagrams, I found out that the answer is no.

But how? The answer, in my mind, is quite simply. Suppose the entire diagram is broken into sections, where each section contains only one interconnecting line. For example, the diagram above is broken into 5 sections:

      #1  I  #2  I  #3  I #4 I  #5         
1 --------I------I------I----I-------------- Task 1
          I   |  I      I |  I    |        
2 --------I------I------I----I-------------- Task 2
       |  I      I   |  I    I               
3 --------I------I------I----I-------------- Task 3
          I      I      I    I               

For each section, if the process as described above is followed, there is always a one-to-one-mapping between the N numbers and each task. It does not matter how many sections there are, there always be a one-to-one mapping between N numbers and the task.

It would probably be easier just to draw straws (or randomly pick numbers from a hat) -- however the picking process of going through Sadari Tagi seems a lot more fun and exciting!

Friday, May 10, 2013

Function Call Can be Replaced by Set Literal

I got this warning in PyCharm today: Function call can be replaced by set literal

It was for the following code:
    hand[unique_id] = set([seat.player])

Basically it means that you don't have to say set([seat.player]) you could just do this:
    hand[unique_id] = {seat.player}

It took a while to process what the message was actually saying...