brew local modification error and solution

$ brew update error: Your local changes to the following files would be overwritten by merge: Library/Formula/erlang.rb Please, commit your changes or stash them before you can merge. Aborting Error: Failure while executing: git pull -q origin refs/heads/master:refs/remotes/origin/master

Solution

$ cd `brew --prefix`
$ cd Cellar/erlang/
$ git reset --hard HEAD

Whenever you think you need urllib2, you actually need requests

Since I have started using python since version 2.4, I have a lot of old die-hard habit. For one, I write exception handler like this

except Exception, e:

instead of

except Exception as e:

The later is of course a better and more flexible style.

So, today, when I want to write up a simple test to post data to a web server, I imported lib urllib2 right away.

It usually works great… until you need something more than basic.

Apparently I need to set some cookie and set to the server. Let’s import cookiejar and cookie…

Why it throws an exception AttributeError: ‘SimpleCookie’ object has no attribute +’rfc2965’?

OK, let’s import DefaultCookiePolicy. Then I got hit by a missing domain attribute exception. “Do I need to use a lower level Cookie class to set the domain?”, I wonder. Try switching to Cookie and it throw this exception

Traceback (most recent call last):
    ...
    c = Cookie()
TypeError: __init__() takes at least 17 arguments (1 given)  

At this point I said to myself: No, I am not going to figure out the 17 arguments. My test code is at this point in this dismal state:.

    import urllib2
    from cookielib import Cookie, CookieJar, DefaultCookiePolicy  
    from Cookie import SimpleCookie        

    policy = DefaultCookiePolicy(rfc2965=True)        

    c = SimpleCookie()
    c['secretkey'] = auth_token
    cj = CookieJar(policy)
    cj.set_cookie(c)

    req = urllib2.Request("%s/postdata" % self.server_url)
    proxyHandler = urllib2.ProxyHandler( {} )
    defaultErrorHandler = urllib2.HTTPDefaultErrorHandler()
    cookieHandler = urllib2.HTTPCookieProcessor(cj)
    opener = urllib2.build_opener( proxyHandler, defaultErrorHandler, cookieHandler )
    opener.open(req, data, 10000)

Instead of continuing banging my head against a wall, I recall reading about ‘requests’ module. Let give it a go.

Like magic, it is all I need. 2 lines.

     import requests
     requests.post(url, data, cookie=cookie, headers=headers)

It just works. Well done, requests!

Algorithms for Making Good Decisions (by Kevin Leyton-Brown)

The video was posted in 2011, but it is still very informative.

Kevin is also a contributor to the Game Theory course in coursera.org.

Presentation slide can be found here: http://www.cs.ubc.ca/~kevinlb/talk.php?u=2011-Calumet-AlgorithmsDecisions.pdf

Useful erlang function

Tell you the location of library files

code:lib_dir(xmerl).

XPath, Python and Erlang

Just feel that python is easier for XML manipulation.

e.g.

from lxml import etree
f = open("digg-science-rss1.xml")
tree  = etree.parse(f)
tree.xpath(expr) 

Very concise.

whereas in erlang, since the function xmerl:string may return a list that’s composed of various record type (such as xmlRecord or xmlText etc), it makes the handling more complex (one clause per record type). The printout from REPL is also a lot more verbose. e.g.

      #xmlText{
          parents = [{item,16},{channel,2},{rss,1}],
          pos = 3,language = [],value = "\n   ",type = text},
      #xmlElement{
          name = link,expanded_name = link,nsinfo = [],
          namespace = 
              #xmlNamespace{
                  default = [],
                  nodes =

To find out where homebrew keeps the files of a formula

… Simply follow the link.

e.g.

ls -l `which erl`
lrwxr-xr-x  1 antkong  admin  29 29 Apr 23:49 /usr/local/bin/erl -> ../Cellar/erlang/R16B/bin/erl

Single quote is for something else in erlang

In python, single quote and double quote are pretty much interchangable. Not so in erlang.

254> httpd_util:convert_request_date('Sun, 25 Jan 2009 19:20:02 +0000').
** exception error: no function clause matching     httpd_util:convert_request_date('Sun, 25 Jan 2009 19:20:02 +0000') (httpd_util.erl, line 257)
255> httpd_util:convert_request_date("Sun, 25 Jan 2009 19:20:02 +0000").
{{2009,1,25},{19,20,2}}

homebrew

Just find out

Trying out cocoapods

The installation failed at the end

$ sudo  gem install cocoapods
Password:

Building native extensions.  This could take a while...
Building native extensions.  This could take a while...
Successfully installed i18n-0.6.1
Successfully installed multi_json-1.7.2
Successfully installed activesupport-3.2.13
Successfully installed multipart-post-1.2.0
Successfully installed faraday-0.8.7
Successfully installed addressable-2.3.4
Successfully installed faraday_middleware-0.9.0
Successfully installed hashie-2.0.4
Successfully installed netrc-0.7.7
Successfully installed octokit-1.24.0
Successfully installed rake-10.0.4
Successfully installed cocoapods-core-0.18.1
Successfully installed claide-0.2.0
Successfully installed cocoapods-downloader-0.1.0
Successfully installed colored-1.2
Successfully installed xcodeproj-0.5.5
Successfully installed escape-0.0.4
Successfully installed json-1.7.7
Successfully installed open4-1.3.0
Successfully installed cocoapods-0.18.1
20 gems installed
Installing ri documentation for i18n-0.6.1...
Installing ri documentation for multi_json-1.7.2...
Installing ri documentation for activesupport-3.2.13...

unrecognized option `--encoding'

For help on options, try 'rdoc --help'](null)

It failed because I did not install the command line tools in XCode. After installing the tool, the gem command ran fine.

$ sudo  gem install cocoapods
Successfully installed cocoapods-0.18.1
1 gem installed
Installing ri documentation for cocoapods-0.18.1...
Installing RDoc documentation for cocoapods-0.18.1...