Visualize Reddit upvotes by subreddit

这是我reddit上所有的upvote在各个subreddit上的分布情况,这个情况还是可以说明我是个普通青年。
Reddit upvotes visualization

排在前几位的分别是

  • Programming
  • Linux
  • Python
  • f7u12
  • Clojure
  • Ubuntu

如果你还不了解什么是Reddit:Reddit是一个巨大的社会书签+论坛网站,他的频道叫做subreddit,每个频道有一个相应的主题,涵盖了从IT技术到新闻到生活的各个角落。

这些数据是通过下面的Clojure程序获得(使用reddit.clj库),并通过jfreechart展现出来的。

Substract a vector from a matrix in Octave

假设你有一个矩阵:

A = [1,2;3,4;5,6]

以及一个矢量:

B = [2,5]

你希望对A的每一行元素对元素地减B,例如第一行

[1-2,2-5]

但你不希望用循环完成这个工作,那么你可能想到利用B创建一个和A一样维度的矩阵然后进行.-:

C = [2,5;2,5;2,5]
A .- C

在Octave中可以利用repmat这个函数获得C:

C = repmat(B, length(A), 1)

但是对于大矩阵来说这是一种对内存的浪费。

更好的方法是利用bsxfun:

bsxfun(@minus, A, B)

bsxfun在octave的文档中似乎鲜有提及。

手动设置Fedora15网卡

今天上午的Fedora更新,版本为0.8.999.3的NetworkManager会致使NetworkManager和network service的版本不一致,从而导致网络功能无法使用。而这个时候要通过yum downgrade NetworkManager*降级又没有网络连接可用,悲剧。

这时只有手动设置网卡了。service network restartservice NetworkManager restart都报告失败,ifconfig查看网卡,只有本地回环启动。

接下来接上网线,通过 ifconfig -a 查看所有的网卡接口,例如我的接口叫做em2.

启动网卡
ifconfig em2 up

查看网卡情况
ifconfig em2

如果没有获得ip,可以通过dhcpclient获得ip
dhclient -4 em2

再查看网卡情况,如果获得了ip,可以尝试ping www.baidu.com,如果出现network unreachable的报错,需要再配置一下路由信息,其中gw是你的网关:
route add default gw 192.168.1.1

这时你的网络应该暂时可用了,立刻执行yum downgrade NetworkManager*降级吧。

Correct source file encoding with one liner

Amoeba项目最早的代码可以追溯到2008年了,其中有多个作者贡献代码,因为一直在Windows下开发,所以没有使用UTF8编码,最近大家统一到UTF8,遇到了代码编码不正确的问题。

于是我们需要统一解决一下这个问题:

iconv -f gbk -t utf8 -o ConnectionManager.java ConnectionManager.java

这样可以把gbk编码的源文件转换为UTF8,原地转换。

推广到整个代码目录,用find和xargs做,xargs通过-I来制定一个占位符。

find . -name "*.java" -type f -perm +600 -print | xargs -I _ iconv -f gbk -t utf8 -o _ -c _

结果发现iconv运行中报了错,进一步检查发现一部分代码正常转换了,另一部分却乱码了。原来,两个作者的代码分别是gbk和gb2312,这iconv转换的时候两种编码并不兼容。这就麻烦了,必须对代码分别处理才可以,区别代码的编码,暂时就用Java源文件里的作者名字。又看了一下find似乎没有对文件内容过滤的条件,不过不要紧,我们可以用xargs做:

find . -name "*.java" -type f -perm +600 -print | xargs -I _ sh -c 'grep -q hexianmao _ && iconv -f gb2312 -t utf8 -o _ -c _ '

对这位hexianmao作者的代码,我们利用grep进程的返回值来进行判断。grep的-q参数相当于>/dev/null。这里有一个tricky的地方,再xargs里我们不能直接用&&来组合命令,不过可以通过sh -c这样的方式,并且其中的占位符会被xargs合适地替换掉。

这样执行之后,这位作者的gb2312代码就被成功转换了。而另一部分作者的gbk代码也可以用同样方式解决了。

Fix ogg/oga not play in Firefox 3.6

Native audio support was introduced in since Firefox 3.5 . Ogg is one of the media format supported by Firefox. However, sometimes you may find it doesn’t work even if you set the right source path. And you just check the network status of audio element:

document.getElementsByTagName(“audio”)[0].networkState

Then you get the constant of a 4, which is HTMLMediaElement.NETWORK_NO_SOURCE.

This is because firefox checks the Content-Type header to make sure it’s a media file. (Webkit based browsers don’t have this restriction.) However, ogg format is not configured on most http servers. You can check the content type by:
curl -I <url-to-media-file>

Take Apache as example, you can add following content to your configuration file:
AddType audio/ogg .oga
AddType video/ogg .ogv .ogg

For more, check this article:
https://developer.mozilla.org/en/Configuring_servers_for_Ogg_media