Random Pandas Notes, Note 2

Reference: https://www.mikulskibartosz.name/how-to-split-a-list-inside-a-dataframe-cell-into-rows-in-pandas/

Given a table containing a column of lists, like:

id name tags
1 winter squash [‘60-minutes’, ‘time-to-make’, ‘healthy’]
2 braised pork [‘2-hours-more’, ‘time-to-make’, ‘chinese’]
3 chilli Beef [‘4-hours-more’, ‘chinese’]

Like the ‘unwind’ function in mongodb, to turn it into:

id name tags
1 winter squash ‘60-minutes’
1 winter squash ‘time-to-make’
1 winter squash ‘healthy’
2 braised pork ‘2-hours-more’
2 braised pork ‘time-to-make’
2 braised pork ‘chinese’
3 chilli Beef ‘4-hours-more’
3 chilli Beef ‘chinese’

Here’s who we do it:

1
2
3
4
5
tags_df['tags'] = tags_df.tags.apply(lambda x: x[1:-1].split(','))
clean_tag_df = tags_df.tags.apply(pd.Series).merge(tags_df, right_index = True, left_index = True) \
.drop(["tags"], axis = 1) \
.melt(id_vars = ['recipe_id'], value_name = "tags") \
.drop("variable", axis = 1).dropna()

Breaking it down, we have the following to turn the tags from a string into a list of strings.

1
tags_df['tags'] = tags_df.tags.apply(lambda x: x[1:-1].split(','))

Next step, turning a list of tags into multiple columns:

1
tags_df.tags.apply(pd.Series)

It turns the chart like:

1 2 3
‘60-minutes’ ‘time-to-make’ ‘healthy’
‘2-hours-more’ ‘time-to-make’ ‘chinese’
‘4-hours-more’ ‘chinese’ NaN

Then, we join tags with the rest of the list:

1
prev_df.merge(tags_df, right_index = True, left_index = True)

Then, we drop the duplicated “tags” column and unwind different columns of the tags into different rows:

1
prev_df.drop(["tags"], axis = 1).melt(id_vars = ['recipe_id'], value_name = "tags")

Lastly, we remove the “variable” column, which we might not need:

1
prev_df.drop("variable", axis = 1).dropna()

Binary Tree Traversal, Part 1

outline:

  1. pre-order, post-order, in-order traversal of binary tree
  2. level-order traversal of binary tree

pre-order, post-order, in-order traversal of binary tree

pre-order:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
preorder(root, res);
return res;
}

public void preorder(TreeNode node, List res) {
if (node != null) {
res.add(node.val);
preorder(node.left, res);
preorder(node.right, res);
}
}
}

post-order:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
postorder(root, res);
return res;
}

public void postorder(TreeNode node, List res) {
if (node != null) {
postorder(node.left, res);
postorder(node.right, res);
res.add(node.val);
}
}
}

in-order:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
inorder(root, res);
return res;
}

public void inorder(TreeNode node, List res) {
if (node != null) {
inorder(node.left, res);
res.add(node.val);
inorder(node.right, res);
}
}
}

level-order

recursive method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
int level = 0;
if (root == null)
return res;
level_recur(root, res, level);
return res;
}

public void level_recur(TreeNode node, List<List<Integer>> res, int level) {
// add new level if necessary
if (res.size() == level)
res.add(new ArrayList<Integer>());
// add current value to corresponding level
res.get(level).add(node.val);
// recursion
if (node.left != null)
level_recur(node.left, res, level+1);
if (node.right != null)
level_recur(node.right, res, level+1);
}
}

iterative method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if (root == null)
return res;
int level = 0;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while(!queue.isEmpty()) {
res.add(new ArrayList<Integer>());
int level_len = queue.size();
for (int i = 0; i < level_len; i++) {
TreeNode cur = queue.remove();
res.get(level).add(cur.val);
if (cur.left != null)
queue.add(cur.left);
if (cur.right != null)
queue.add(cur.right);
}
level += 1;
}
return res;
}
}

Backtracking Leetcode Summary, Part 2

  1. Subsets
  2. Palindrome
  3. Combination Sum, repeated number used
  4. Combination Sum, unique number used

Subsets:

Problem Description: Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets.
Few Key Points:

  • This algorithm is very close to that of permutation. The only difference is that this counts for subsets instead of permutations.
  • One of the key point is to keep the order, don’t permute like the permutation algorithm

Code:
backtracking part, takes parameters:

  • result: LinkedList>
  • current: ArrayList
  • nums: nums to extract from
  • cur_idx: current index
    Note: when adding to result, convert the current list into a new list
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public void backtrack(List<List<Integer>> l, 
    List<Integer> tmpList,
    int [] nums,
    int start) {
    l.add(new ArrayList<>(tmpList));
    for (int i = start; i < nums.length; i++) {
    tmpList.add(nums[i]);
    backtrack(l, tmpList, nums, i+1);
    tmpList.remove(tmpList.size() - 1);
    }
    }
    The solution part:
    1
    2
    3
    4
    5
    6
    7
    public List<List<Integer>> subsets(int[] nums) {
    List<List<Integer>> res = new LinkedList<>();
    List<Integer> tmp = new ArrayList<Integer>();
    int start = 0;
    backtrack(res, tmp, nums, start);
    return res;
    }

Palindrome:

Problem Description: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.
e.g.:
Input: “aab”
Output:
[[“aa”,”b”],
[“a”,”a”,”b”]]
Few Key Points:
An utility function can be used to determine if str is palindrome or not.
Code:
backtrack:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void backtrack(List<List<String>> result, 
List<String> tmp,
String str,
int index) {
if (index == str.length())
result.add(new ArrayList<String>(tmp));
else {
for (int i = index; i < str.length(); i++) {
if (isPalindrome(str, index, i)) {
tmp.add(str.substring(index, i+1));
backtrack(result, tmp, str, i+1);
tmp.remove(tmp.size()-1);
}
}
}
}

Check if str is palindrome:

1
2
3
4
5
6
7
public boolean isPalindrome(String str, int s, int e) {
while (s < e) {
if (str.charAt(s++) != str.charAt(e--))
return false;
}
return true;
}

The solution part:

1
2
3
4
5
6
7
public List<List<String>> partition(String s) {
List<List<String>> res = new LinkedList<List<String>>();
List<String> list = new ArrayList<String>();
int idx = 0;
backtrack(res, list, s, idx);
return res;
}

Combination Sum, repeat allowed

Problem Description: Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. The same repeated number may be chosen from candidates unlimited number of times.
e.g.:
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[[7],
[2,2,3]]
Codes:
backtrack:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void backtrack(List<List<Integer>> res, 
List<Integer> tmp,
int[] cand,
int rem,
int cur) {
if (rem == 0) {
res.add(new ArrayList<Integer>(tmp));
} else if (rem < 0) {
return;
} else {
for (int i = cur; i < cand.length; i++) {
if (rem >= cand[i]) {
tmp.add(cand[i]);
backtrack(res, tmp, cand, rem - cand[i], i);
tmp.remove(tmp.size()-1);
} else {
break;
}
}
}
}

The solution part:

1
2
3
4
5
6
7
8
9
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new LinkedList<>();
List<Integer> tmp = new ArrayList<Integer>();
int cur = 0;
int rem = target;
Arrays.sort(candidates);
backtrack(res, tmp, candidates, rem, cur);
return res;
}

Combination Sum, no repeats

Problem Description: Same as combination sum, with only difference that each number can only be used once.

Codes:
backtrack: (added while loop to eliminate repeating results)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public void backtrack(List<List<Integer>> res, 
List<Integer> tmp,
int[] cand,
int rem,
int cur) {
if (rem == 0) {
res.add(new ArrayList<Integer>(tmp));
} else if (rem < 0) {
return;
} else {
for (int i = cur; i < cand.length; i++) {
if (rem >= cand[i]) {
tmp.add(cand[i]);
backtrack(res, tmp, cand, rem - cand[i], i+1);
tmp.remove(tmp.size()-1);
while (i < cand.length-1 && cand[i] == cand[i+1])
i += 1;
} else {
break;
}
}
}
}

The solution part is the same as the last section.

Confusion Matrix Summary

A simple cheat-sheet for confusion matrix.

Random Pandas Notes, Note 1

To change a row value based on more than one conditions, use the following:

1
df['A'] = np.where(((df['B'] == 'some_value') & (df['C'] == 'some_other_value')), true_value, false_value)

Another equally efficient approach is using loc:
Note that this method does not give a default value when the condition is not met. So, the same code might be required to run twice or using .fillna() method.

1
df.loc[(df.B == 'some_value') | (df.C == 'some_other_value'), 'A'] = true_value

ps: for ‘and’ or ‘or’ operation, use symbol ‘&’ or ‘|’.

Backtracking Leetcode Summary, Part 1

  1. N-Queens
  2. Permutation

N-Queens:

Problem Description: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of the n-queens’ placement, where ‘Q’ and ‘.’ both indicate a queen and an empty space respectively.
Thought Process in Short: Backtracking with DFS. We know each row has one queen, so we move from top line to the bottom line line by line, checking each column, and two diagonals.
Few Key Points:

  • For all “hill” diagonals row + column = const, and for all “dale” diagonals row - column = const.
  • There could be the only one queen in a row and the only one queen in a column.

Algorithms:

  1. Start from the first row = 0.
  2. Iterate over the columns and try to put a queen in each column.
    1. If square (row, column) is not under attack
      1. Place the queen in (row, column) square.
      2. Exclude one row, one column and two diagonals from further consideration.
      3. If all rows are filled up row == N
        • That means that we find out one more solution.
          Else
        • Proceed to place further queens backtrack(row + 1).
      4. Now backtrack : remove the queen from (row, column) square.

Codes:
Initialize log variables:

1
2
3
4
5
6
int rows[];
int hills[];
int dales[];
int n;
List<List<String>> output = new ArrayList();
int queens[];

Checking if a cell is under attack:

1
2
3
4
5
6
7
8
public boolean isNotUnderAttack(int row, int col) {
int res = rows[col] + hills[row - col + 2 * n] + dales[row + col];
if (res == 0) {
return true;
} else {
return false;
}
}

Method placing and removing queens:

1
2
3
4
5
6
7
8
9
10
11
12
13
public void placeQueen(int row, int col) {
queens[row] = col;
rows[col] = 1;
hills[row - col + 2 * n] = 1;
dales[row + col] = 1;
}

public void removeQueen(int row, int col) {
queens[row] = 0;
rows[col] = 0;
hills[row - col + 2 * n] = 0;
dales[row + col] = 0;
}

Adding a solution to existing solutions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void addSolution() {
List<String> solution = new ArrayList<String>();
for (int i = 0; i < n; ++i) {
int col = queens[i];
StringBuilder sb = new StringBuilder();
for (int j = 0; j < col; ++j) {
sb.append(".");
}
sb.append("Q");
for (int j = 0; j < n - col - 1; ++j) {
sb.append(".");
}
solution.add(sb.toString());
}
output.add(solution);
}

The actual backtracking process:

1
2
3
4
5
6
7
8
9
10
11
12
13
public void backtrack (int row) {
for (int col = 0; col < n; col++) {
if (isNotUnderAttack(row, col)) {
placeQueen(row, col);
if (row + 1 == n) {
addSolution();
} else {
backtrack(row + 1);
}
removeQueen(row, col);
}
}
}

Main function of solution:

1
2
3
4
5
6
7
8
9
10
public List<List<String>> solveNQueens(int n) {
this.n = n;
rows = new int[n];
hills = new int[4 * n - 1];
dales = new int[2 * n - 1];
queens = new int[n];

backtrack(0);
return output;
}

Permutation:

One of the more basic backtracking problem.
Algorithms:

  1. If the first integer to consider has index n that means that the current permutation is done.
  2. Iterate over the integers from index first to index n - 1.
    1. Place i-th integer first in the permutation, i.e. swap(nums[first], nums[i]).
    2. Proceed to create all permutations which starts from i-th integer : backtrack(first + 1).
    3. Now backtrack, i.e. swap(nums[first], nums[i]) back.

Code
BFS to find solutions, then backtracking to reverse to previous states. The following is the backtracking part.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void backtrack(int n, 
ArrayList<Integer> nums,
List<List<Integer>> res,
int first_idx) {
if (first_idx == n) {
res.add(new ArrayList<Integer>(nums));
}
for (int i = first_idx; i < n; i++) {
# swap first and ith
Collections.swap(nums, first_idx, i);
backtrack(n, nums, res, first_idx+1);
# swap back to original
Collections.swap(nums, first_idx, i);
}
}

Main function of solution:

1
2
3
4
5
6
7
8
9
10
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new LinkedList();
ArrayList<Integer> nums_list = new ArrayList<Integer>();
for (int num : nums) {
nums_list.add(num);
}
int n = nums.length;
backtrack(n, nums_list, res, 0);
return res;
}

Oct 12th trip to New Yorks

A snap-shot of NYC at sunset
A snap-shot of NYC at sunset

A Brief Summary of Sorting Algorithm, Part 1

  1. Basics:
    • in-place sorting vs. out-place sorting
    • internal sorting vs. external sorting
    • stable vs. unstable sorting
  2. Bubble Sort
  3. Selection Sort
  4. Insertion Sort
  5. Merge Sort
  6. Quick Sort
  7. Heap Sort

Internal vs. external sorting

Internal sorting and external sorting describes where the sorting occurs:

  • internal sorting located entirely in memory
  • external sorting utilizes hard disk and external storage

Stable vs. unstable sorting

A sorting algorithm is said to be stable if two objects with equal keys appear in the same order in sorted output as they appear in the input array to be sorted.

  • stable sorting algorithms includes:
    1. Bubble Sort
    2. Insertion Sort
    3. Merge Sort
    4. Count Sort

Bubble Sort

Bubble Sort is a type of stable sorting algorithm. The algorithm compares two elements that are next to each other and swap two element is the left one is larger than the right one. Time complexity of bubble sort is O(n*n).

1
2
3
4
5
6
7
def bubbleSort(arr): 
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr

Selection Sort

In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted sub-array. Selection sort can be done stably. Time complexity of selection sort is O(n*n).

1
2
3
4
5
6
7
8
9
10
def selectionSort(arr):
for i in range(len(arr)):
min_idx = i
for j in range(i+1, len(arr)):
if arr[min_idx] > arr[j]:
min_idx = j
tmp = arr[i]
arr[i] = arr[min_idx]
arr[min_idx] = tmp
return arr

Insertion Sort

Insertion sort is stable. In every iteration of insertion sort, the first element is selected and inserted into the correct location in the sorted half of the array. Time complexity of Insertion sort is O(n*n).

1
2
3
4
5
6
7
8
9
def insertionSort(arr): 
for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >= 0 and key < arr[j] :
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr

Merge Sort

Merge sort is stable. Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. Time complexity is O(n*log(n)).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def mergeSort(arr): 
if len(arr) >1:
mid = len(arr)//2
L = mergeSort(arr[:mid])
R = mergeSort(arr[mid:])
i = j = k = 0
while i < len(L) and j < len(R):
if L[i] < R[j]:
arr[k] = L[i]
i+=1
else:
arr[k] = R[j]
j+=1
k+=1
while i < len(L):
arr[k] = L[i]
i+=1
k+=1
while j < len(R):
arr[k] = R[j]
j+=1
k+=1
return arr

Quick Sort

For quick sort, we pick a random element as pivot. Compare each element with the pivot to create first half of the list smaller than the pivot and the second half larger than the pivot. After that, quick sort divide conquer two halves. Time complexity for quick sort is O(nlog(n)), worst case is O(nn). Quick sort can be made stable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def partition(arr,low,high): 
i = (low-1)
pivot = arr[high]
for j in range(low , high):
if arr[j] < pivot:
i = i+1
arr[i],arr[j] = arr[j],arr[i]
arr[i+1],arr[high] = arr[high],arr[i+1]
return ( i+1 )

def quickSort(arr,low,high):
if low < high:
pi = partition(arr,low,high)
quickSort(arr, low, pi-1)
quickSort(arr, pi+1, high)

Heap Sort

  1. Build a max heap from the input data.
  2. At this point, the largest item is stored at the root of the heap. Replace it with the last item of the heap followed by reducing the size of heap by 1. Finally, heapify the root of tree.
  3. Repeat above steps while size of heap is greater than 1.
    Heapify: this procedure calls itself recursively to move the max value to the top of the heap. Time complexity for heapify is O(log(n)) and time complexity for building a heap is O(n). Thus, heap sort gives the overall time complexity as O(n*log(n)).
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    def heapify(arr, n, i): 
    largest = i # Initialize largest as root
    l = 2 * i + 1 # left = 2*i + 1
    r = 2 * i + 2 # right = 2*i + 2
    # left child
    if l < n and arr[i] < arr[l]:
    largest = l
    # right child
    if r < n and arr[largest] < arr[r]:
    largest = r
    # change the root
    if largest != i:
    arr[i],arr[largest] = arr[largest],arr[i] # swap
    # Heapify upward
    heapify(arr, n, largest)

    def heapSort(arr):
    n = len(arr)
    for i in range(n, -1, -1):
    heapify(arr, n, i)
    for i in range(n-1, 0, -1):
    arr[i], arr[0] = arr[0], arr[i] # swap
    heapify(arr, i, 0)
    return a

Multiple Thread (Runnable vs. Thread), Part 1

  1. Multi-threading states
  2. Java thread init methods
  3. Examples of Thread and Runnable

Multi-threading states

  1. new: 新建状态,保持这个状态直到程序start()
  2. ready: 调用了start(),就绪状态的线程处于就绪队列中,要等待JVM里线程调度器的调度
  3. running: 获取了CPU资源,执行run()里面的指令
  4. suspend: 失去所占用资源之后,该线程就从运行状态进入阻塞状态。在睡眠时间已到或获得设备资源后可以重新进入就绪状态。可以分为三种
    • 等待阻塞: wait()
    • 同步阻塞: 线程在获取synchronized同步锁失败后
    • 其他阻塞: 其他阻塞:通过调用线程的sleep()或join()发出了I/O请求时,线程就会进入到阻塞状态
  5. dead: 一个运行状态的线程完成任务或者其他终止条件发生时,该线程就切换到终止状态

Java thread init methods

Java 提供了三种创建线程的方法

  • 通过实现Runnable接口
  • 通过继承Thread类本身
  • 通过Callable和Future创建线程

Java thread example

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
// class extending Thread
import java.util.*;
// new class
class MyThread extends Thread {
private int ticket = 10;
private String name;
private Random rand = new Random();
// constructors
public MyThread(String name) {
this.name = name;
System.out.println(this.name + " starts with " + this.ticket + ".");
}
// Override the run function to implement functionality
@Override
public void run() {
for (int i = 0; i < 500; i++) {
if (this.ticket > 0) {
// create a random number between two sales
int nxt = rand.nextInt(20) * 10;
this.ticket--;
System.out.println(this.name + " sold a ticket, now has " + this.ticket + " tickets.");
try {
Thread.sleep(nxt);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("Now, " + this.name + " has ended.");
}
}
// method to run the class above
public class ThreadDemo {
public static void main(String[] args) {
System.out.println("start");
MyThread mt1 = new MyThread("Counter 1");
MyThread mt2 = new MyThread("Counter 2");
MyThread mt3 = new MyThread("Counter 3");
mt1.start();
mt2.start();
mt3.start();
}
}

Runnable Example

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
45
46
47
48
49
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.Random;
// now using Runnable interface instead of extending a class
public class MyRunnable implements Runnable{
private int ticket = 30;
private Random rand = new Random();
private Lock lock = new ReentrantLock();
public MyRunnable() {
System.out.println("This class starts with " + this.ticket + " tickets.");
}
// overriding the run method just as before
@Override
public void run() {
while (this.ticket > 0) {
// tryLock() checks if the lock is available, takes the lock and return true if yes, else return false
if (lock.tryLock()){
try {
// command to execute with the lock
System.out.println(Thread.currentThread().getName() + " acquired lock.");
this.ticket--;
System.out.println(Thread.currentThread().getName() + " sold a ticket, now has " + this.ticket + " tickets.");
} catch (Exception e) {
System.err.println(e);
} finally {
// releasing the lock
System.out.println(Thread.currentThread().getName() + " released lock.");
lock.unlock();
}
// wait for a random length after selling a ticket
int nxt = rand.nextInt(20) * 10;
try {
Thread.sleep(nxt);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// if lock is not available, wait
else {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("Now, " + Thread.currentThread().getName() + " has ended.");
}
}

Django Basics, Part 1

Django directory levels

  1. manage.py: we use this to communicate with Django server
  2. project directory:
    1. __init__.py: tells Django to use directory as a python package
    2. settings.py: Django configuration file
    3. urls.py: url schema
    4. wsgi.py: project and wsgi integration web server

Database (MySQL) config

  • dependencies used:
    mysql 5.7.24 h56848d4_0
    mysql-connector-c 6.1.11 hccea1a4_0
    mysql-connector-python 8.0.17 py27h3febbb0_0 anaconda
    mysql-python 1.2.5 py27h1de35cc_0 anaconda
    pymysql 0.9.3 py27_0
  • __init__.py:, add the following code:
    1
    2
    import pymysql
    pymysql.install_as_MySQLdb()
  • in settings.py: add database configurations to connect to the database
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': # server name
    'USER': # user name
    'PASSWORD': # password
    'HOST': 'localhost',
    'PORT': '3306',
    }
    }

Init new application

  • init new application:
    1
    python manage.py startapp <myApp>
  • codes above creates:
    • admin.py: website config
    • models.py: model
    • views.py: view
  • In setting, add to INSTALLED_APP:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myApp'
    ]

models.py file

  • the following is an example of models.py
    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
    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    from django.db import models

    # Create your models here.
    class Grades(models.Model):
    gname = models.CharField(max_length=20)
    gdate = models.DateTimeField()
    ggirlnum = models.IntegerField()
    gboynum = models.IntegerField()
    isDelete = models.BooleanField(default=False)

    def __str__(self):
    return "%s-%d-%d"%(self.gname, self.gboynum, self.ggrilnum)


    class Students(models.Model):
    sname = models.CharField(max_length=20)
    sgender = models.BooleanField(default=True)
    sage = models.IntegerField()
    scontend = models.CharField(max_length=20)
    isDelete = models.BooleanField(default=False)
    sgrade = models.ForeignKey("Grades")

    def __str__(self):
    return "%s-%d-%s"%(self.sname, self.sage, self.scontend)
  • make migration and execute migration file: will create database according to models.py
    1
    2
    python manage.py makemigrations # create migration file
    python manage.py migrate # execute migration file

To add new entries with manage.py shell commands

  1. enter shell:
    1
    python manage.py shell
  2. import packages in shell:
    1
    2
    3
    from myApp.models import Grades, Students
    from django.utils import timezone
    from datetime import *
  3. add new entry to db:
    1
    2
    3
    4
    5
    6
    grade1 = Grades()
    grade1.gname = "something"
    grade1.gdate = datetime(year=,month=,day=)
    grade1.ggirlnum = 70
    grade1.gboynum = 35
    grade1.save()
  4. some other command:
    1
    2
    3
    4
    5
    6
    Grades.objects.all()    # get all data entries in Grades
    g = Grades.objects.get(pk=1) # get the 1st object in Grades
    g.gboynum = 45
    g.save() # alternate existing data
    d.delete() # delete, including the one in db
    stu1 = g.students_set.create(sname=...) # add new student with one line

To run server

  • execute:
    1
    python manage.py runserver ip:port
  • Admin:
    • publish content
    • add/modify/delete content
    • add “‘django.contrib.admin’” in INSTALLED_APPS
    • exists by defualt
    • add “/admin” and log in
    • change language or timezone based on preference

(to be continued…)