Aleks' Domain

Elitism in IRC

After getting tired of VIM’s default syntax highlighting for Python, I set out to find a way to make it better. I ended up stumbling across this which sounded promising. Being a VIM user for the better part of three years, I have never actually installed any VIM scripts or anything like that.

After searching for a good 30 minutes on what should be a pretty simple solution, I decided to ask the good folks in #vim on FreeNode. I’m no stranger to IRC, so I know that if someone goes in asking a simple question that can be looked up in some manual somewhere, that someone is going to get burned pretty badly. I knew that my question was such a question, but I was different. I spent half an hour looking for a solution, trying instructions from dozens of different websites, the official instructions on the main page, and manual after manual. Surely, they would sympathize!

At the time, this was the set topic for the channel.

Vim 7.2.356: http://vim.sf.net | Don’t ask to ask, just ask! …

I’m going to stop talking now, and let the following excerpt do the talking for me. No changes have been made to the chat log. I’ve labelled myself in blue, and everyone else talking about me, or to me, in red.

[metaleks]: how do I install a .vim plugin (python.vim)? I’ve placed it in ~/.vim/plugin and in /usr/share/vim/vim72/plugin/, but nothing seems to be working.
[Araxia]: hey, naquad. i wrote an auto tag completion function last night, if you’re interested in having a look. not super pretty, but functional: http://pastey.net/132688
[naquad]: Araxia: hi!
[skrite]: do tabs in vim 7 eliminate the need of buffers? why use buffers (like with mini buff explorer) instead of tabs?
[arm]: metaleks: I think it should be in ftplugin…
[tpope]: python.vim does NOT sound like a plugin
it might be an ftplugin, might be a syntax file, might be an indenting algorithm

* spiiph bets there were installation instructions there somewhere.
[spiiph]: skrite, no
vimgor, tabs
[vimgor]: Tabs are not buffers, don’t try to force them to act like buffers. Consider tabs like viewports, layouts, or workspaces. Trying to setup 1 tab == 1 buffer is an exercise in futility. Do ‘:set hidden’ and get started. Get FuzzyFinder, LustyExplorer or BufExplorer to make getting around your buffers easier. See http://vim.wikia.com/wiki/Category:Tabs for more info.
[tpope]: I’m pretty sure the installation instructions read “head to irc and ask a question that makes you look like you don’t know what you’re talking about”
[metaleks]: there were no instructions in the .vim file
nor could I find any after a good while of searching

[naquad]: Araxia: cool. i didn’t understand even a half, but tested and it works pretty nice, thank you
[metaleks]: sorry if it seems like a newb question
I will try ftplugin, thanks

[spiiph]: metaleks, well, where did you find the file in the first place?
[arm]: is it just me, or noobs that read the smartQuestions.html keep asking FAQ shit but now saying they have not found anything when they supposedly did search for stuff in the docs
[metaleks]: spiiph, the main vimscripts site
arm, I assure you that I never resort to IRC unless I’ve spent a good while searching for the solution myself

[qz]: any ideas how to find out which plugin tries to “indent” my code when i press enter?
[arm]: metaleks: if you are desperate to get it working, try :source
[tpope]: metaleks: this one? http://www.vim.org/scripts/script.php?script_id=790
metaleks: the one that clearly says “Place python.vim file in ~/.vim/syntax/ folder.”

[Araxia]: naquad: one limitation is that it will only complete a tag that was started on the same line. a lot of the script is just using normal mode commands. if you have any questions, i’d be happy to explain.
[spiiph]: Bah, tpope beat me to it.
[metaleks]: tpope, yes, that’s the one, and yes I tried the instructions there
[tpope]: metaleks: then why’d you come in babbling about sticking it in .vim/plugin?
[metaleks]: arm, thanks for that tip, that’s one more thing I will try before coming here next time
[naquad]: Araxia: i’m learning to work with vim manual, trying to find out everything myself
[metaleks]: tpope, I was just listing stuff I tried
[Araxia]: naquad: that’s definitely the way to do it.
[tpope]: metaleks: you never once mentioned you tried following the actual instructions
[metaleks]: tpope, because that’s obvious that you would right?
[tpope]: hey guys I keep shoving cucumbers up my butt but I’m still hungry
[metaleks]: no need to be hostile
[tpope]: jesus fucking christ what is wrong with you?
[darkfuneral]: is there a way i can set a colorscheme but only or a certain plugin? i’m using vimoutliner and i only want to change it’s colorscheme for hilighting, not the entire colorscheme of vim
[tpope]: yes, when you come in playing the part of the village idiot, you do need to explain that you tried following instructions
[tpope]: darkfuneral: no. but you can create a super duper colorscheme that highlights different files different ways
[metaleks]: I’m sorry I didn’t list the 3000 different things i tried, honestly good sir, your hostility is unwarranted
[darkfuneral]: tpope: ok how
[tpope]: darkfuneral: colorschemes define very specific groups (“rubyInteger”) that link to more generic groups (“Number”). simply highlight the more specific groups instead

After that, nothing was said to me, which was good because I didn’t really feel like talking anymore. Thanks for making me feel welcome, asshat. I’ve been nothing but polite to you, but apparently it’s too much to return the favour when your ego is as big as the channel itself.

I think this sums up this strange phenomenon, because honestly, stuff like this is rampant on IRC.


In the end, I never did find a solution to my problem. I can live without the Python syntax script for now, but I’ll come back to it some time in the future. Anyway, I’d like to end this post off with one of my favourite quotes.


Edit: Boy, this thing really took off, albeit a few months later. Reddit, Twitter, blogs… I don’t promote my blog at all, so the fact that so many people have read one of my posts gives me a warm and fuzzy feeling. However, there are a few clarifications I should add.

1. The problem turned out to be Ubuntu and the way it handled autoloading of plugins. Props to Dace for the comment below on how to solve this problem.

2. Part of the problem, looking back now, was that I didn’t really articulate myself well enough while asking for help. I apologize for that, but I still think the responses I received weren’t warranted.

The Evolution of a Python Programmer

A while back, clever snippets of code portraying how different people programming in Python went about solving the same problem, appeared on the internet. Needless to say, it was pretty funny and despite its popularity, not many people seem to know of it when I bring it up. Being as lazy as I am when it comes to these sorts of things, I am blogging about it now. Almost three years later. It has been posted and reposted dozens of times, but full of geek pride, I can proudly say that I helped create it by correcting some of the mistakes in the original. How’s that for major geek cred?

The original is right here, if you’re interested.

Newbie Programmer

1
2
3
4
5
6
def factorial(x):
    if x == 0:
        return 1
    else:
        return x * factorial(x - 1)
print factorial(6)

First Year Programmer (Studied Pascal)

1
2
3
4
5
6
7
8
def factorial(x):
    result = 1
    i = 2
    while i <= x:
        result = result * i
        i = i + 1
    return result
print factorial(6)

First Year Programmer (Studied C)

1
2
3
4
5
6
7
8
9
def fact(x): #{
    result = i = 1;
    while (i <= x): #{
        result *= i;
        i += 1;
    #}
    return result;
#}
print(fact(6))

First Year Programmer (Read SICP)

1
2
3
4
5
@tailcall
def fact(x, acc=1):
    if (x > 1): return (fact((x - 1), (acc * x)))
    else:       return acc
print(fact(6))

First Year Programmer (Python)

1
2
3
4
5
6
def Factorial(x):
    res = 1
    for i in xrange(2, x + 1):
        res *= i
    return res
print Factorial(6)

Lazy Python Programmer

1
2
3
def fact(x):
    return x > 1 and x * fact(x - 1) or 1
print fact(6)

Lazier Python Programmer

1
2
f = lambda x: x and x * f(x - 1) or 1
print f(6)

Python Expert Programmer

1
2
fact = lambda x: reduce(int.__mul__, xrange(2, x + 1), 1)
print fact(6)

Python Hacker

1
2
3
4
5
6
import sys
@tailcall
def fact(x, acc=1):
    if x: return fact(x.__sub__(1), acc.__mul__(x))
    return acc
sys.stdout.write(str(fact(6)) + '\n')

EXPERT PROGRAMMER

1
2
from c_math import fact
print fact(6)

BRITISH EXPERT PROGRAMMER

1
2
from c_maths import fact
print fact(6)

Web Designer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def factorial(x):
    #-------------------------------------------------
    #--- Code snippet from The Math Vault          ---
    #--- Calculate factorial (C) Arthur Smith 1999 ---
    #-------------------------------------------------
    result = str(1)
    i = 1 #Thanks Adam
    while i <= x:
        #result = result * i  #It's faster to use *=
        #result = str(result * result + i)
           #result = int(result *= i) #??????
        result = str(int(result) * i)
        #result = int(str(result) * i)
        i = i + 1
    return result
print factorial(6)

Unix Programmer

1
2
3
4
import os
def fact(x):
    os.system('factorial ' + str(x))
fact(6)

Windows Programmer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
NULL = None
def CalculateAndPrintFactorialEx(dwNumber,
                                 hOutputDevice,
                                 lpLparam,
                                 lpWparam,
                                 lpsscSecurity,
                                 *dwReserved):
    if lpsscSecurity != NULL:
        return NULL #Not implemented
    dwResult = dwCounter = 1
    while dwCounter <= dwNumber:
        dwResult *= dwCounter
        dwCounter += 1
    hOutputDevice.write(str(dwResult))
    hOutputDevice.write('\n')
    return 1
import sys
CalculateAndPrintFactorialEx(6, sys.stdout, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)

Enterprise Programmer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def new(cls, *args, **kwargs):
    return cls(*args, **kwargs)
 
class Number(object):
    pass
 
class IntegralNumber(int, Number):
    def toInt(self):
        return new (int, self)
 
class InternalBase(object):
    def __init__(self, base):
        self.base = base.toInt()
 
    def getBase(self):
        return new (IntegralNumber, self.base)
 
class MathematicsSystem(object):
    def __init__(self, ibase):
        Abstract
 
    @classmethod
    def getInstance(cls, ibase):
        try:
            cls.__instance
        except AttributeError:
            cls.__instance = new (cls, ibase)
        return cls.__instance
 
class StandardMathematicsSystem(MathematicsSystem):
    def __init__(self, ibase):
        if ibase.getBase() != new (IntegralNumber, 2):
            raise NotImplementedError
        self.base = ibase.getBase()
 
    def calculateFactorial(self, target):
        result = new (IntegralNumber, 1)
        i = new (IntegralNumber, 2)
        while i <= target:
            result = result * i
            i = i + new (IntegralNumber, 1)
        return result
 
print StandardMathematicsSystem.getInstance(new (InternalBase, new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, 6))

A Fansub Editing Tip

It seems that most of my posts have been crazy one-liners that do more than they should. This post is no different.

Motivation

Remember how your high school English teachers always stressed that you should be reading and re-reading your own work before submitting it? Out loud? It’s almost a fool-proof way to catch mistakes, but reading can get tedious depending on what you have to read. In my case, I’ve recently started editing fansubs (Japanese animation, specifically) and found that reading the scripts can be quite difficult at times. So, I’ve put a small one-liner together to read the script for me!

The Code

Here is the line that solves my problems, followed by an explanation of it, for those of you who care.

cat TARGET.ass | grep "Dialogue" | sed -e 's/.*}\|.*0,,\|\\N//g' | festival --pipe --tts

This only works on .ass files, and if you want to run it replace the TARGET.ass with the path to your script file. So, how does this work? Like this.

We first grab all of the file’s contents with cat TARGET.ass. Then, the entire script is filtered line by line allowing only the lines that start with “Dialogue” to pass through to the next stage. This is done with the grep “Dialogue” part. Next, and probably the most funky part of this entire one-liner is when the program sed is invoked. Basically, what sed -e ’s/.*}\|.*0,,\|\\N//g’ does is it removes everything that’s not actual dialogue. Why is this needed? Unfortunately, even though we have all of the lines that contain actual dialogue, every single one of these lines is littered with .ass format and control tags. All of this is filtered out, and we’re left with clean dialogue. The funky looking code is called a regular expression, if you’re interested.

At this point, we’re pretty much done. The last thing that needs to be done is to somehow give our script to a text-to-speech program. This is done with the festival –pipe –tts part. The program festival is a text-to-speech program, and if you’re wondering about the extra arguments passed to it, feel free to hit up the manual.

The default voice is pretty crap, but a simple search on Google reveals a plethora of guides on how to install different (and better!) voices.

I hope that this helps some of you!

My Portfolio

A blog makes a horrible portfolio…which is why I am proud to announce an official portfolio right here! Hopefully, this is a little more presentable to prospective employers than this blog.

Removing Audio Delay From Videos

I’m often amazed at the things that can be accomplished with a single command. Seriously. A show I had recently downloaded didn’t have its video and audio synced. I don’t know about you, but for me, this is incredibly annoying. So much so, that I couldn’t watch it.

Luckily, there is a way to fix this. If you’re suffering from the same problem, you’ll need mencoder. If you use Ubuntu, it’s in the repositories. Here is the command:

mencoder -audio-delay 0.5 -oac copy -ovc copy TARGET.mpg -o OUTPUT.mpg

If your audio is too fast, this example slows the audio down by half a second. The -oac copy and -ovc copy are so that we preserve the original quality of our file. You can change them if you’d like. TARGET is your target file, and OUTPUT is obviously your output. Make sure that you add the proper file extensions. In the example above I used .mpg.

Anyway, you’ll probably have to run this a few times, changing the delay so that it matches your video, and then testing it. It took me three tries to get it synced properly.

Recursively Removing .svn

I recently started working on an assignment that required me to copy some code from another repository. I did just that, but had a little trouble trying to add the new stuff to my new repository. After about a minute or two I realized that all of the newly copied old code still had the hidden folder .svn…in every directory. I was copying an OS kernel, so there were quite a bit of these .svns sticking around. They needed to go if I was going to add any of my code into my repository.

How did I remove them? Like this.

find . -name .svn -print0 | xargs -0 rm -rf

Removing Extra Ubuntu Entries in GRUB

If you’ve used Ubuntu through any kernel updates, you’ll know that each major update will place not one, but two entries for booting from GRUB. A normal boot, and a safe mode. If you receive three updates, that’s a total of six new entries. Not to mention that if you’re dual booting like me, you’ll have even more. I’ve been putting it off for a while, mostly because I didn’t care for the solution. But, I was bored today and I decided to look it up. I’m glad I did. It’s a lot easier than I thought.

sudo vim /boot/grub/menu.lst

That’s it. Then just comment out the entries (they span multiple lines, and are near the end of the file). If you don’t like vim, or working through the terminal then run the command below to edit the file in gedit.

gksu gedit /boot/grub/menu.lst

Once you have the file open, just comment out the entries by placing a “#” before every line in the entry. You may be tempted to delete some of the entries, but really, why would you want to do that? Commenting them out solves the problem, and if something does go wrong with your current kernel, you can always uncomment an Ubuntu entry that doesn’t have the problem.

A commented entry looks like this.

#title Ubuntu 9.04, kernel 2.6.28-11-generic
#uuid b511d0d6-7d11-4909-bf5e-9764fce42f96
#kernel /boot/vmlinuz-2.6.28-11-generic root=UUID=b511d0d6-7d11-4909-bf5e-9764fce42f96 ro quiet splash
#initrd /boot/initrd.img-2.6.28-11-generic
#quiet

Hope this helps.

The Wonder That Is Hardware

The further I get in CSC258 (Computer Organization) the more I realize how amazing the computer really is. It’s almost a humbling experience. Most people don’t realize how much they don’t know about the hardware and what goes on inside a computer, but if they did their jaw would drop. Mine is constantly on the floor, and I’m only scratching the surface.

Needless to say, I think this has been the best course that I’ve ever taken at school. At least, so far. I honestly feel as if I could build a very simple calculator from scratch if given the appropriate tools. And you know what? I probably could. Giving me transistors alone would be tedious, and I would have one very large calculator on my hands. But, I would still be able to build it. In fact, you can make transistors on your own out of pretty simple things, but they would be pretty large. Which means that I would be able to build a very simple calculator out of nothing (provided that I know how to make my own transistors consistently). And that excites me. I feel like I’m learning something useful. It’s wonderful.

But my example of a simple calculator doesn’t even compare to a computer’s processor. No sir. In fact, even talking about it doesn’t do it justice. There is so much that goes on that it’s mind blowing. So I’m going to stop here. It’s very humbling to know how far we’ve gone as a race, and I can think of no better example than the computer’s processor as a testament to our achievements.

If that isn’t enough, there are even talks of quantum computing. What’s that? Well, I’ll explain it in a nutshell. You know all those 1s and 0s that your computer understands? You’ve probably seen it in movies, or heard of it being referred to as binary. In any case, binary only has two states. 1 or 0. On or off. What quantum computing does is add a third state. Both on and off at the same time. I tried thinking about this and only found that if I kept at it long enough my head would start to hurt.

Technology is amazing.

Back To School

So, today was my first day of school. Woot! I was really tired, so I didn’t enjoy it that much, but some would argue school isn’t meant to be enjoyed anyway. I beg to differ. In any case, this always happens. Whenever I get excited about some event, whether it be a concert or the first day of school, I always get no sleep whatsoever the night before. I’m sure some people can relate. I hope. Because being the only person like this, that’s kind of sad.

Anyhoo, forgetting that I have back-to-back 2 hour lectures, I think my Mondays in first semester are going to be all right. I’m really looking forward to the software development course. Stuff like version control, SSH, build tools and vi (bonus! take that emacs!) are finally going to be covered. I’ve wanted to be an Ubuntu MOTU for quite a while now, and I feel that after this semester I can finally ease into the transition of becoming one. What else? Oh yeah, the CSC258 course is going to be pretty sweet. It has a pretty awesome prof, and it’s all about computer hardware, logic circuits, and other related stuff. In other words, a continuation of high school computer engineering. Fun.

Oh, and the other courses aren’t worth mentioning, so I won’t. Maybe it was me being tired that made me extremely drowsy in these classes, or maybe it was that it was well…for lack of a better word: boring. Ah well, it’s the first day of school. Can’t expect to much from a course on the first day.

But yeah, it’s a wonder I’m even still typing. I made a pact with one of my friends that we’d hit the haystack before midnight every school night. I wonder how long that will last, haha. 34 more minutes and I break that pact. God damn…

All in all, I’m going to be very busy this semester. Three math courses and three computer science courses are enough to drive any person insane. I’ve also restricted the amount of hobbies this semester. I’m going to actually have to try for once in my life. I look forward to seeing what I can accomplish when I actually give it my best.

Good night!

I’m Blind In My Left Eye!

Just kidding. It turns out that I’m actually really farsighted in my left eye. And I just found this out yesterday. Went to an eye examination for the first time, and this is the news I get. Psh. This is why I don’t go to eye exams. If it weren’t for my good eye, I wouldn’t be able to see at all.

And even my good eye is slightly far sighted! The nice Vietnamese lady told me that there is no point in getting glasses now since I’ve made it this far in life without them. But eventually, I will need them. I told her that I wouldn’t mind wearing glasses, so she brought out a pair of experimental glasses. The kind that look like a big pair of evil robot eyes with removable lenses. Tried a few of the lenses on and found out that they actually helped. And they felt kinda relaxing for my eyes, as well. So I’ve decided that I will get glasses. I could do without them. Really, I could. I mean, my good eye is pulling the weight for both my eyes, but how long can he keep it up?

But yes. I really can’t read at all with my left eye. Even now, as I type this I’m covering my right eye and trying to read what’s on the screen. I can sorta make out the letters, but I wouldn’t be able to read them coherently. And it’s worse the more I move back. Meh, it looks like I’m going to be completing the geek stereotype once I get my glasses. Thank goodness I already went through the braces phase. Fear of the sun can’t be too far behind, though.