Installing zenburn theme on Aquamacs

I followed the steps in https://github.com/bbatsov/zenburn-emacs to install zenburn theme using MELPA. But I had difficulty making it stick the next time it loads. These are the steps that works with me:

  1. Run Aquamacs
  2. Type: M-x package-install zenburn-theme
  3. I also added (load-theme ‘zenburn t) to “~/Library/Preferences/Aquamacs Emacs/Preferences.el”

But when I close and run Aquamacs again it is not exactly zenburn as the lower bar text are too dark. So I need to load the theme manually:

M-x load-theme zenburn

To make it stick, I follow the steps in: https://www.emacswiki.org/emacs/AquamacsFAQ#toc21

  1. With zenburn theme loaded, I go to: Menu -> Options → Appearance → Auto Faces → Use Current Face as Default
  2. And I do the next step: Menu → Options → Appearance → Adopt Face and Frame Parameters as Default
  3. Answer “Yes” to the dialog.
  4. Exit Aquamacs and “Y” to save the settings.

Multiple LDAP authentication in Django

Django allows multiple LDAP configuration. The example of the configuration is given here: https://pythonhosted.org/django-auth-ldap/multiconfig.html

To do this we need to create a Python module under site-packages. For example we can create a module called ‘mypackage’.

$ ls /home/administrator/websenv/lib/python2.7/site-packages/mypackage
__init__.py ldap.py

We can leave __init__.py empty, and write the following entry into ldap.py:

#ldap.py
from django_auth_ldap.backend import LDAPBackend

class LDAPBackend1(LDAPBackend):
 settings_prefix="AUTH_LDAP_1_"

class LDAPBackend2(LDAPBackend):
 settings_prefix="AUTH_LDAP_2_"

class LDAPBackend3(LDAPBackend):
 settings_prefix="AUTH_LDAP_3_"

Now, we can configure the ldap in Django settings.py. This is an example of a configuration in Django.

import ldap
from django_auth_ldap.config import LDAPSearch


AUTHENTICATION_BACKENDS = (
    'mypackage.ldap.LDAPBackend1',
    'mypackage.ldap.LDAPBackend2',
    'mypackage.ldap.LDAPBackend3',
    'django.contrib.auth.backends.ModelBackend',
)

AUTH_LDAP_2_SERVER_URI = "ldap://student.myschool.edu.sg"
AUTH_LDAP_3_SERVER_URI = "ldap://stafff.myschool.edu.sg"
AUTH_LDAP_1_SERVER_URI = "ldap://192.168.3.16"

AUTH_LDAP_2_USER_DN_TEMPLATE = "%(user)s@student.myschool.edu.sg"
AUTH_LDAP_3_USER_DN_TEMPLATE = "%(user)s@staff.myschool.edu.sg"
AUTH_LDAP_1_USER_DN_TEMPLATE = "%(user)s@myschool.edu.sg"

AUTH_LDAP_2_BIND_DN = "dc=STUDENT,dc=MYSCHOOL,dc=EDU,dc=SG"
AUTH_LDAP_3_BIND_DN = "dc=STAFF,dc=MYSCHOOL,dc=EDU,dc=SG"
AUTH_LDAP_1_BIND_DN = "dc=MYSCHOOL,dc=EDU,dc=SG"

In the example above, we created three backends LDAP1, 2, and 3. We first import the backends to Django and set the URI, DN_TEMPLATE, and bind the domain.

We may need to restart Django and the server:

$python manage.py syncdb
$sudo apachectl restart

Erasing External Harddisk that cannot be opened

I got problem with my external hard disk. After some issues of umounting the disk, I couldn’t open the hard disk anymore. I tried to use Disk Util to repair, but it said it cannot be repaired and must be erased. But I can’t even erased it! It says disk can’t be opened. So I finally managed to erase it.

First, go to Terminal and type:

$ diskutil list

This will show a list of disk and partition. It happens that I can’t seem to erase the volume. But somehow I can erase the disk. So this what I do.

$ diskutil unmountDisk force /dev/disk1

and after it unmount, I type:

$ diskutil eraseDisk JHFS+ Backup /dev/disk1
Started erase on disk1
Unmounting disk
Creating the partition map
Waiting for the disks to reappear
Formatting disk1s2 as Mac OS Extended (Journaled) with name Backup
Initialized /dev/rdisk1s2 as a 931 GB case-insensitive HFS Plus volume with a 81920k journal
Mounting disk
Finished erase on disk1

Introducing scikit-learn for Machine Learning in Python

In this tutorial, we will go through a simple steps to classify whether a voltage is a digital 0 or 1. We will make things pretty straight forward with large margin so that we can easily visualize. The idea is just to introduce some of the functions in scikit-learn that we can use for linear classification.

The simple problem we want to do is given a set of voltages and its digital logic, we want to be able to determine what would be the digital logic given a new set of voltages. In this example, we use supervised learning, where we label the data and have the training set. Then given a new data, we want to determine which class it belongs to. We will use linear classifier from Support Vector Machine.

First given a training data sets of voltages, e.g.

>>> import numpy as np
>>> data=[0.5,0.1,0.2,0.15,0,0.3,0.4,0.5,0.35,0.45]
>>> data=np.array(data).reshape(len(data),1)

The first line of the above code import numpy module so that we can use its array data stucture. The second line create a list of voltages as a Python list. The third line, convert the Python list to Numpy array and change from a row vector to a column vector.

We can then train the data set by labeling the voltages. For simplicity, let’s label any voltages below 0.2 to be digital logic 0, and anything above 0.3 to be logic 1. To do this we create a target list according to our data set.

>>> target=np.array([0,0,0,0,0,1,1,1,1,1])

Now we can create our linear classifier using SVM.

>>> from sklearn import svm
>>> clf = svm.SVC(kernel='linear')

The first line import the module svm, and the second line creates a linear classifier. Next, we have to fit the data with the target.

>>> clf.fit(data,target)

And now we can predict new data. Let’s say we want to determine what the digital logic is for voltage 0.21V, we can type:

>>> clf.predict(0.21)
array([0])

Or we can also give an array:

>>> newdata=[0,0.5,0.22, 0.1,0.44]
>>> newdata=np.array(newdata).reshape(len(newdata),1)
>>> clf.predict(newdata)
array([0, 1, 0, 0, 1])

As you can see that voltages around 0.2 and below is classified as logic 0 while those around 0.3 and above is classified as logic 1.

Using SublimeText to write JSim file

50.002 Computation Structure course in SUTD requires students to write a kind of SPICE-like code using JSim, an MIT software to simulate circuit design. The editor, however, is pretty basic, and I have noticed that some students prefer to use SublimeText to write the JSim file. Here is the instruction to setup SublimeText to use with JSim.

  1. Download SublimeText.
  2. In Mac OS X, run SublimeText, press “CMD-SHIFT-P” to open the Command Palette. Otherwise, go to Menu -> Tools -> Command Palette.
  3. Type “browse packages” this will open a new Finder window with the location to install the packages.
  4. Create a new Folder called “JSim”.
  5. Download JSim syntax highlighter. Copy jsim.tmlanguage to the newly created folder “JSim”.
  6. Associate jsim extention to open with SublimeText. To do this (in Mac OS X):
    1. Find a JSim file, e.g. lab3.jsim. Right click and choose “Get Info”.
    2. Under “Open With”, choose “Sublime Text”.
    3. Click “Change All”.

UPDATE: There is some issue when using SublimeText in Ubuntu. A solution was to rename jsim.tmlanguage to jsim.tmLanguage (Note the capital “L”).

A free alternative to create Evernote notes from your email

As of July 2015, Evernote has changed its feature to create note from email to a “paid” service. Luckily, there are always alternatives. This post share with you how to do it for free using IFTTT.

  1. Create a free account with ifttt.com using the EMAIL that you want to use to create notes in Evernote. You will need to confirm your account by clicking the link from your email inbox.
  2. Once you are set, you can start creating recipe by clicking “Create a Recipe”  and then click “This”. create_recipeclick_this
  3. Select “Email” for your channel. trigger_channel
  4. And then select the trigger. choose_a_trigger
  5. To complete the trigger, key in the hash tag you want. You can create just one recipe with one hastag, let’s say “#evernote” or you can create multiple recipes with different hash tags. trigger_fields
  6. After that create the “Do” part. click_that
  7. Choose “Evernote” as the channel.  choose_evernote
  8. You will then have to authorize IFTTT to connect to your Evernote account by logging in to Evernote. connect_evernote
  9. Then, you can complete the action fields. In my body I chose “{{BodyHTML}}” rather than Body and I added “{{AttachmentUrl}}”. I decided to create a special Notebook to dump all my notes created from IFTTT. But as I said before, you can just create multiple recipes with different hash tags and for each hash tag you can set your notebook destination. action_fields
  10. Once, you are done, you can send email to create an Evernote note. email_evernote

Android emulator device not showing in Running Devices

Sometimes with the android virtual device running, I can’t seem to see them under the Running Devices when I click “run app”. To solve that, I need to restart my adb. To do that go to the “Android SDK/platform-tools” folder. In my case for Mac OS X, it is at:

/Users/my_user_name/Library/Android/sdk/platform-tools

From there I need to run the following command:

./adb kill-server
./adb start-server

Then you can check whether the device has been attached by:

./adb devices