Skip to content

Instantly share code, notes, and snippets.

View dboyliao's full-sized avatar

dboyliao dboyliao

View GitHub Profile
@dboyliao
dboyliao / count_pattern.py
Last active March 8, 2016 14:42
Count Matched Pattern in The File
#!/usr/bin/env python
# Simple counting program
from __future__ import print_function
import re
import argparse
def count_pattern(fname, pattern):
count = 0
pattern = re.compile(pattern)
with open(fname) as rf:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# A simple fibonacci number server using TCP/IP,
# which can handle multiple clients.
from __future__ import print_function
from socket import *
import argparse
import os, sys
cache = {0: 1, 1:1}
from __future__ import print_function
import numpy as np
m = np.array([[1, 2], [3, 4]])
print("m**2: ")
print(m**2)
print("matrix_power:")
print(np.linalg.matrix_power(m, 2))
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
class ParentA(object):
def __init__(self):
self.a = "parent a"
class ParentB(object):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
class ParentA(object):
def __init__(self):
super(ParentA, self).__init__()
self.a = "parent a"
@dboyliao
dboyliao / comprehensions.md
Created March 29, 2016 08:33 — forked from bearfrieze/comprehensions.md
Comprehensions in Python the Jedi way

Comprehensions in Python the Jedi way

Beautiful is better than ugly. Explicit is better than implicit.

-- The Zen of Python

I frequently deal with collections of things in the programs I write. Collections of droids, jedis, planets, lightsabers, starfighters, etc. When programming in Python, these collections of things are usually represented as lists, sets and dictionaries. Oftentimes, what I want to do with collections is to transform them in various ways. Comprehensions is a powerful syntax for doing just that. I use them extensively, and it's one of the things that keep me coming back to Python. Let me show you a few examples of the incredible usefulness of comprehensions.

All of the tasks presented in the examples can be accomplished with the extensive standard library available in Python. These solutions would arguably be more terse and efficient in some cases. I don't have anything against the standard library. To me there is a certain

@dboyliao
dboyliao / gist:52e68a5d9cab3592cd700eb86b099024
Created March 30, 2016 00:45 — forked from arvearve/gist:4158578
Mathematics: What do grad students in math do all day?

Mathematics: What do grad students in math do all day?

by Yasha Berchenko-Kogan

A lot of math grad school is reading books and papers and trying to understand what's going on. The difficulty is that reading math is not like reading a mystery thriller, and it's not even like reading a history book or a New York Times article.

The main issue is that, by the time you get to the frontiers of math, the words to describe the concepts don't really exist yet. Communicating these ideas is a bit like trying to explain a vacuum cleaner to someone who has never seen one, except you're only allowed to use words that are four letters long or shorter.

What can you say?

@dboyliao
dboyliao / on_fire.sh
Last active April 13, 2016 17:09
command for commit before fire.
git_branch_info() {
ref=$(git symbolic-ref HEAD 2> /dev/null) || ref='None';
[ "$ref" == 'None' ] && echo "None" && return;
echo "${ref#refs/heads/}"
}
fire(){
BRANCH_NAME=$(git_branch_info);
@dboyliao
dboyliao / struct_align.c
Last active April 17, 2016 09:35
Playing with struct alignment.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
struct A {
char *p;
char c;
int x;
};
@dboyliao
dboyliao / while_example.py
Last active May 26, 2016 14:01
a simple while example
while True:
guess = int(input("Enter a number: "))
if guess == 5:
print("you win!")
break
else:
print("do other things here")