AdSense

Tuesday, May 19, 2015

Installing MySQL on Mac: guide for dummies

This tutorial provides instructions to install mysql using Mac mini vault's Mac - script. For dummies and newbies like me, it is extremely convenient.

Before you start, please read the README file provided by MMV.

1. Copy the following command:

bash <(curl -Ls http://git.io/eUx7rg)

This command will automatically MySQL and install it. It will ask if you want to load MySQL on boot. Since I am installing in it on my laptop, I didn't choose that.

2. After MySQL is installed, close the terminal and reopen it again. Now you are able to operate MySQL on terminal.

3. Log in
If you are using your computer as the host, enter the following command:

mysql -u root -p

Otherwise, use this command:

mysql -h host -u username -p

You probably will have to ask your admin for the hostname.
Enter the password in the file that is located on your Desktop with the name "MYSQL_PASSWORD". It was generated automatically when you were installing MySQL.
If you are successfully logged in, you should be able to see this:


NOTE: 
If you get this error message: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

That usually means your MySQL server is not started. Go to step 5, start your server and try again.


4. Changing password 
MMV's Mac script will automatically generate a random password. Most of time it's hard to remember. If you want to change the password to a personalized one, after you log on to the server, do this: 


SET PASSWORD FOR 'root'@'localhost' = PASSWORD('SHIRLEY_IS_AWESOME');

Put your personalized password within the ' ' unless you want to set 'SHIRLEY_IS_AWESOME' as your password. ;)

If you happen to forget your password, MMV has this command to help you reset the password.Don't forget to read the README file.

bash <(curl -Ls http://git.io/9xqEnQ)

5. Start and Stop sql server (if it is your computer)
If you have selected to load MySQL on boot, then MySQL server will automatically start every time you start your computer. I don't use it very often, so I didn't select such option. Thus, I need to manually turn the server on.
Go to System Preferences:



Find MySQL: 

Open it, select Start MySQL Server:

Enter your admin password. If you see this green "running" signal, you are ready to go!


There are other more professional ways to start and stop the server, see here. 


Friday, May 15, 2015

Matrix using Python


My second post regarding data structure using Python is about 2 dimension array and matrix.

For 2D array, naturally we can think of using nested lists to implement it, here is the class: 



class Array2d:
    def __init__(self, numRows, numCols):
        self._theRows = [None] * numRows
        for i in range (numRows):
            self._theRows[i] = [None] * numCols
    #return the number of rows
    def numRows(self):
        return len(self._theRows)

    #return number of columns
    def numCols(self):
        return len(self._theRows[0])

    def clear(self):
        for row in range(self.numRows()):
            row.clear()

    #initialize all 2d array to a value
    def initialize(self, value):
        for row in range(self.numRows()):
            for col in range(self.numCols()):
                self[row, col] = value

 
    def __getitem__(self, key):
        assert len(key) == 2, "Invalid number of coordinates"
        row = key[0]
        col = key[1]
        assert 0 <= row < self.numRows() \
            and 0 <= col < self.numCols(), \
            "Array subscript out of range."
        return self._theRows[row][col]

    def __setitem__(self, key, value):
        assert len(key) == 2, "Invalid number of coordinates"
        row = key[0]
        col = key[1]
        assert 0 <= row < self.numRows() \
            and 0 <= col < self.numCols(), \
            "Array subscript out of range."
        self._theRows[row][col] = value




The matrix class is implemented from the above Array2d class:


from src import Array2D

class Matrix:

    def __init__(self, numRows, numCols,initialValue):
        self._matrix = Array2D.Array2d(numRows, numCols)
        self._matrix.initialize(initialValue)

    def numRows(self):
        return self._matrix.numRows()

    def numCols(self):
        return self._matrix.numCols()

    def __getitem__(self, key):
        return self._matrix.__getitem__(key)

    def __setitem__(self, key, value):
        return self._matrix.__setitem__(key, value)

    def scaleBy(self,scalar):
        for row in range(self.numRows()):
            for col in range(self.numCols()):
                self[row, col] *= scalar

    def transpose(self):
        trans = Matrix(self.numCols(),self.numRows(), 1)
        for r in range(self.numCols()):
            for c in range(self.numRows()):
                trans[r, c] = self[c, r]
        return trans

    def __add__(self, matrixB):
        assert matrixB.numRows() == self.numRows() \
            and matrixB.numCols() == self.numCols(), \
            "Matrix sizes not compatible for the add operation"
        sumMatrix = Matrix(self.numRows(),self.numCols(),0)
        for r in range(self.numRows()):
            for c in range(self.numCols()):
                sumMatrix[r, c] = self[r, c] + matrixB[r, c]
        return sumMatrix

    def __sub__(self, matrixB):
        #indention after '\'
        assert matrixB.numRows() == self.numRows() \
            and matrixB.numCols() == self.numCols(), \
            "Matrix sizes not compatible for the subtraction operation"
        difference = Matrix(self.numRows(),self.numCols(),0)
        for r in range(self.numRows()):
            for c in range(self.numCols()):
                difference[r, c] = self[r, c] - matrixB[r, c]# = difference.__getitem__(r, c)
        return difference

    def __mul__(self, matrixB):
        assert matrixB.numRows() == self.numCols() \
            and matrixB.numCols() == self.numRows(), \
            "Matrix sizes not compatible for the subtraction operation"
        product = Matrix(self.numRows(), matrixB.numCols(), 0)
        for r in range(self.numRows()):
            for c in range(matrixB.numCols()):
                for n in range(self.numCols()):
                    product[r, c] += self[r, n] * matrixB[n, c]
        return product

    def print(self):
        for r in range(self.numRows()):
            for c in range(self.numCols()):
                print(self[r, c], end=' ')
            print()


This blog is more about the details of writing Python than the actual implementation of the two classes. Here are some of the notes:

1. Modules
Module is a familiar name in Python. Officially, it defines as:
A module is a file containing Python definitions and statements.a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. 
It would be easier to think it as just files you write your code.  There is no module in Java upon Java 8 ("In Java 9, "modules", a kind of collection of packages, are planned as part of Project jigsaw these were earlier called "superpackages" and originally planned for Java 7"), but Java uses "package" for similar purposes.

To create an object from a class written in a module, we have to call that class from the module. e.g.,

matrixA = Matrix.Matrix(rows, cols, 1)


This definitely is not a good naming strategy. I was following a tutorial in which they defined a class inside the module, I thought it was the same as in Java, apparently I was not right. However, there is an easier way, which is just writing functions inside the file without defining "class Matrix", I will remember next time. :)

2. __setitem__(self, key, value), __getitem__(self, key) and methods to emulating container types
Theres two methods are part of the methods that can be used to emulate containers (e.g., dictionary, list, etc. ). __getitem__ () can be called by self[key]. 
One easy way to set a value is to call:

self[key] = value

3. Explicit and implicit line joining
It is better for the length of a line of code to be within 80 characters, so when a line is too long, we need to separate it to two lines. In Python, two or more physical lines may be joined into logical lines using backslash ('\'). Note a line ending in a backslash cannot carry a comment. Indention is preferred after the first line.


assert matrixB.numRows() == self.numRows() \
            and matrixB.numCols() == self.numCols(), \
            "Matrix sizes not compatible for the subtraction operation"


Implicit line joining is used in parentheses, square brackets or curly braces and can carry comments in the end of the line.
Java does not require explicit line joining.

4. Comparison
 This is actually pretty cool. Finally we can write comparison the way we write on paper:

0 <= row < self.numRows()


Apparently Java does not allow us to write it in such way.


The tester code:

from src import Matrix

def main():
    if __name__ == "__main__":
        rows = 3
        cols = 2
        matrixA = Matrix.Matrix(rows, cols, 1)
        matrixB = Matrix.Matrix(cols, rows, 1)
        matrixC = Matrix.Matrix(rows, cols, 1)
        matrixA[0, 1] = 2
        matrixA[2, 1] = 3
        matrixB[1, 1] = 3
        matrixB[0, 2] = 5
        matrixC[0, 1] = 3
        matrixC[0, 1] = 1

        print("Matrix A", end='\n')
        matrixA.print()

        print("\nMatrix B", end='\n')
        matrixB.print()

        print("\nMatrix C", end='\n')
        matrixC.print()

        sumM = matrixA.__add__(matrixC)
        print("\nSum of A and C", end='\n')
        sumM.print()

        difM = matrixA.__sub__(matrixC)
        print("\nDifference between A and C", end='\n')
        difM.print()

        prodM = matrixA.__mul__(matrixB)
        print("\nProduct of A and B", end='\n')
        prodM.print()

        print("\nTranspose of A", end='\n')
        matrixA.transpose().print()


main()

More
I was going through the fancy Numpy's implementation of matrix, then I found the build in function __new__(). I thought that __init__() serves as the constructor role, apparently I was only partially right: they do together. __new__() is responsible of creating a new instance, it can return an instance of the class, while __init__() takes charge of customizing it. If __new__() returns an instance of the class, then the new instance's __init__() method will be invoked. Since __new__() and __init__() work together to construct new objects,  NO non - None value may be returned by __init__(). 

More2
I wanted to check Numpy's matrix dot product implementation, since my brutal force one requires O(n^3), apparently it turns out to be a more complicated subject. See here for the Stackoverflow discussion and here for the Numpy implementation. I am going to stop here since the ALTAS is apparently beyond my concern. :)


Tuesday, May 12, 2015

XML syntax

I have always been annoyed by the imperfections on webpages, e.g., weird characters. One of the websites I used a lot have the following defects:
while the other one doesn't:
I am always interested in figuring out what have caused this problem.

Last night, I discovered a new feature on my browser: view source (forgive my ignorance). So naturally I looked the source code of both websites. Apparently I didn't figure out the reason is because of my lack of knowledge on the XML:

XML has a feature called entity reference: in order to avoid errors generated from parsing special characters in XML, replace such characters with entity reference: e.g., '<' -> '&lt'
There are 5 pre-defined entity references:
source: http://www.w3schools.com/xml/xml_syntax.asp
So the answer for the previous question is: the first website parses the entity reference instead of the character itself. I believe a simple code should fix this problem, which I hope they will do it.

I don't have much knowledge on the XML, but here is some useful resources:

W3school
W3C

I probably will dive a little bit on the topic, e.g., the namespace: http://www.w3.org/1999/xhtml/

P.S. All copyright reserved to the websites which I got the snapshots from. Nevertheless, they are great websites. :)



Tuesday, May 5, 2015

Map ADT using Python

I start to go through Data Structure and Algorithm Using Python to get familiar on how to write classes using Python. The first thing is my favorite map. This structure is not the fancy hashmap, it's the simplest key - value pair structure. This map is a list implementation.
Basic structure:

a private mapEntry class which is used to store the key - value pair.

Note:
private classes, methods are identified by an underscore, e.g., _add(self, value).

add method to add key - value pairs
valueOf method to find the value of a given key, assume the key exists
remove method to remove the key - value pair given the key, assume the key exists
_iter_ method that returns an iterator of the key value pairs
a helper method findPosition that will find the position given the key, return None if no such key


__author__ = 'shirleyyoung'

class Map:
    # creates an empty map instance
    #like constructor, self: this object
    def __init__(self):
        #create empty list
        self._entryList=list()

    #when call map.len(), it will call __len__(map) and get the length of the object
    def __len__(self):
        return len(self._entryList)

    #determine if the map contains the given key
    def __contains__(self, key):
        ndx = self._findPosition(key)
        return ndx is not None

    #adds a new entry to the map if the key does not exist
    #otherwise replace the old value with the new value
    def add(self, key, value):
        ndx = self._findPosition(key)
        if ndx is not None:
            self._entryList[ndx].value = value
            return False
        else:
            entry = _MapEntry(key,value)
            self._entryList.append(entry)
            return True

    def valueOf(self, key):
        ndx = self._findPosition(key)
        assert ndx is not None, "Invalid map key"
        return self._entryList[ndx].value

    def remove(self, key):
        ndx = self._fndPosition(key)
        assert ndx is not None, "Invalid map key."
        self._entryList.pop(ndx)

    #return an iterator for traversing the keys in the map
    def __iter__(self):
        return iter(self._entryList)

    #find the index position of a category
    #if key is not found, none is returned
    def _findPosition(self, key):
        for i in range(len(self)):
            if self._entryList[i].key == key:
                return i
        return None

#private class
class _MapEntry:
    def __init__(self, key, value):
        self.key = key
        self.value = value

map = Map()
map.add(1, 1)
map.add(2, 2)
map.add(3, 3)
for entry in iter(map):
    print(entry.key)
print(len(map))
print(map.valueOf(1))

If you are interested in how Python implements dictionary, click here.

*********************************************************************************
I become interested in viewing source code since the time I was looking for jobs. And besides the one I mentioned in my previous couple blogs, I found two more interesting website.

GrepCode: this one provides Java src as well as interesting projects written in Java, e.g., Hadoop, Android.
searchcode: this one is neater, and provides code/projects written in various languages from C++ to Python. They also provides code from multiple sources, e.g., Github, Google Code, BitBucket (I didn't know this one before), etc. Interestingly, they also have an API.
Codatlas: this delicate website is the one I referred in couple of my previous posts. They claim to be IDE based, which is pretty impressive. One interesting feature is that they allow you to upload your own code. However, most of their projects are from Github, it could be better if they provide more projects from more sources.


Wednesday, April 29, 2015

One headlight

I have this evil idea to reorganize the lyrics of this song I am recently addicted. Honestly, I don't even know why I love this song, probably because of the current mess (thesis, defense, moving...) I am dealing with. I used my old Markov Chain code and it's indeed a beautiful song: no matter how I try to destroy it, it still means something.

As in the original lyric:
"This place is always such a mess
Sometimes I think I'd like to watch it burn"


Here is my favorite one:
"same
But me and Cinderella
We can drive it all together
We can drive it all together
We can drive it all together
We put it all together
We put it all together
We put it home With one headlight
Well it home With one
headlight Well it home With one headlight
She ran until she's out of cheap wine cigarettes
This place is forever
There's got to be something better than In the middle But me
Hey come on try a little
Nothing is always seemed such a little
Nothing is old
It feels just like a little Nothing is dead
We'll run until
there's got to be an opening
Somewhere here in front of cheap wine cigarettes This place
Hey , Hey come on try a little
Nothing is always such a little
Nothing is forever
There's got to be an opening
Somewhere here in between the end
it's cold
It feels just her window ledge
Hey , I'd like to watch it
must be something better than In the county line ."

It's hard to be in spotlight
It's also hard to be a wallflower. 


Friday, April 17, 2015

Silicon Valley S2E1: What does Erlich Bachman's binary T-shirt mean?

I saw this Quora question today. It's interesting. Of course as shown in the answer, we can use a binaryToText converter, but what's the fun part if I don't write my own. I am learning Python, so here is the Python and Java version:

Python:

import math
def binaryToText(binary):
 l = len(binary)
 c = 0
 i = 0
 for ch in binary:
  c += math.pow(2, l - i - 1) * (ord(ch) - ord('0'))
  i += 1
 return chr(int(c))

def main():
 Tshirt = ['01000010','01101001','01110100','01100011','01101111','01101001','01101110']
 for t in Tshirt:
  print(binaryToText(t), end = '')
 print('\n')

main()

Java:


public class BinaryToText {
 public static char binaryToText(String binary){
  int c = 0;
  int l = binary.length();
  for(int i = 0; i < l; i++)
   c += (int)Math.pow(2, l - i - 1) * (int)(binary.charAt(i) - '0');
  System.out.println(c);
  return (char)c;
 }

 public static void main(String[] args) {
  String[] Tshirt = {"01000010",
       "01101001",
       "01110100",
       "01100011",
       "01101111",
       "01101001",
       "01101110"
  };
  for(String t : Tshirt)
   System.out.print(binaryToText(t));
  System.out.println();
 }
}


Probably I still haven't got rid of the whole Java class concept, I don't find many advantages of using Python. But, just for fun.


Wednesday, April 15, 2015

Data Structures in Python compared with Java: Sets

A set is an unordered collection with no duplicate elements. Set models the mathematical set abstraction. Both Python and Java use hashtable as the underlying data structure of set.

Creating a set
Also allows different types of element
#empty set
basket = set()
basket = {'shirley',2014}

Python also allows for creating a character set using the following method:

basket = set('abaabab')

Duplicate elements will be removed.

In Java:

Set basket = new HashSet ()

Of course only one type is allowed.
Java also provide constructor to create a hash set from another collection, if created from a collection that contains duplicate elements, duplicate ones will be removed.

List listA = new ArrayList ();
  listA.add(1);
  listA.add(2);
  listA.add(1);
  Set t = new HashSet (listA);


Union
In Python, it is the same way as you do binary operation:

>>> a = {'shirley',2015}
>>> b = {'dora', 2014}
>>> a | b
{'shirley', 'dora', 2014, 2015}

In Java, we use addAll(Collection<? extends E> c) method:

a.addAll(b);

Complements
Python:

>>> a = set('aabbccabc')
>>> b = set('abdd')
>>> a - b
{'c'}

It will return the elements in a that is not in b.

In Java, we use removeAll(Collection<? extends E> c) method:

a.remveAll(b);

Intersection
Python:

>>> a
{'a', 'b', 'c'}
>>> b
{'a', 'b', 'd'}
>>> a&b
{'a', 'b'}
>>> 

Java: retainAll(Collection<? extends E> c) method:

a.retainAll(b);

XOR (?)
Actually, it is elements in a or b but not both

>>> a
{'a', 'b', 'c'}
>>> b
{'a', 'b', 'd'}
>>> a^b
{'d', 'c'}

I couldn't find any built in methods to do this in Java, however, we can always do a little bit coding to acquire the desired result:


Set a = new HashSet ();
  a.add(1);
  a.add(2);
  Set b = new HashSet ();
  b.add(2);
  b.add(6);
  Set c = new HashSet(a);
  c.removeAll(b);
  Set d = new HashSet(b);
  d.removeAll(a);
  c.addAll(d);


Sorted sets:
Java provides TreeSet data structure. It is based on red - black tree implementation (See here for implementations). In Python, I think this needs to be acquired by using lambda function(?). I am not quite familiar with the lambda function, but I will take a look later.



References:
[1]. Not-very-familiar Python doc
[2]. www.grepcode.com
[3]. Beloved Java doc
[4]. www.codatlas.com