php 常用函式

mk_dir: 遞迴建立資料夾
ls: 遞迴取得所有檔案清單(不含目錄)

function mk_dir($dir, $mode = 0777){
  if (is_dir($dir) || @mkdir($dir,$mode)) return TRUE;
  if (!mk_dir(dirname($dir),$mode)) return FALSE;
  return @mkdir($dir,$mode);
}

function ls($path,$loop=true){
  $items=[];
  $files=scandir($path);
  foreach($files as $item){
    if($item!="." && $item!=".."){
      $file="${path}/${item}";
      if(is_dir($file) && $loop){
        $items=array_merge($items,ls($file));
      }
      else if(is_file($file)){
        $items[]=($file); //realpath
      }
    }
  }
  return $items;
}

SQL, 帳號密碼查詢的建議做法

測試資料:

INSERT INTO `test` (`user`, `pswd`) VALUES ('test', 'ABcd+1234');

一般常見做法

SELECT `user` FROM `test` WHERE `user`='test' and `pswd`=sha1('ABcd+1234')

較安全做法,但是會增加DB CPU的功耗!

SELECT `user` FROM `test` WHERE `user`='test' and sha1(CONCAT(`pswd`,9999))=sha1(CONCAT(sha1('ABcd+1234'),9999))

[code] post

function post($url, $post){
  $context = array();
  if (is_array($post)){
    ksort($post);
    $context['http'] = array(
      'method' => 'POST',
      'content' => http_build_query($post, '', '&')
    );
  }
  else{
    $context['http'] = array(
      'method' => 'POST',
      'content' => $post
    );
  }
  return @file_get_contents($url, false, stream_context_create($context));
}

miniconda 安裝

安裝 miniconda

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh -O ~/miniconda.sh
bash ~/miniconda.sh -b -p $HOME/miniconda

路徑設定

echo 'PATH="$HOME/miniconda/bin:$PATH"' >> ~/.bash_profile 
chmod +x ~/.bash_profile
PATH="$HOME/miniconda/bin:$PATH"

git windows 換行問題

git在windows下對於pull下來檔案的換行有三種處理方式:

  1. true: check out時自動將\n轉換成\r\n,commit時將\r\n轉成\n.
  2. input: check out時不轉換\n,commit時將\r\n轉成\n.
  3. false: 不做任何轉換

若不清楚目前設定是哪一種,可以用下列指令查詢:

git config core.autocrlf

若要修改成input,則可用下列指令修改(請用超級管理者執行命令提示字元)

git config --system core.autocrlf input

或是(一般使用者)

git config --global core.autocrlf input

generator & list on python

generator不同於list,沒有list,且只能遊走一次.
下面的code可以發現generator的元素是在被用的時候才產生.

import time

b=[time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) for i in range(3)]

time.sleep(1)
print("A:",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
time.sleep(1)

for ii in b:
  print("B:",ii)
  time.sleep(1)

###########################################################################

b=(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) for i in range(3))

time.sleep(1)
print("C:",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
time.sleep(1)

for ii in b:
  print("D:",ii)
  time.sleep(1)

install nodejs on macos with nvm

#安裝nvm
brew install nvm

#設定路徑
echo "source $(brew --prefix nvm)/nvm.sh" >> ~/.bash_profile
. ~/.bash_profile

#查看版本
nvm ls-remote

#安裝8.x
nvm install v8.15.1

#顯示已安裝版本
nvm ls

#切換版本
nvm use v8.15.1

#設定PATH
echo "PATH=$PATH:$(dirname $(nvm which v8.15.1))" >> ~/.bash_profile
. ~/.bash_profile

[python練習] numpy

引用

import numpy as np;

1維陣列

a = np.array([1, 2, 3]);

2維陣列

a = np.array([ [1, 2, 3], [4, 5, 6] ]);

3維陣列

a = np.array([ [ [1, 2, 3], [4, 5, 6] ], [ [7, 8, 9], [10, 11, 12] ] ]);

內積 (vector dot)

vector dot

v1 = np.array([1, 2, 3]);
v2 = np.array([4, 5, 6]);
v3 = np.dot(v1,v2); #v3 = 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32
print(v3);

矩陣相乘 (matrix multiplication)

matrix_multiplication

m1 = np.array([ [1, 2, 3], [4, 5, 6] ]);      #size: 2*3
m2 = np.array([ [1, 2 ], [3, 4], [5, 6] ]);   #size: 3*2
m3 = np.matmul(m1,m2); # m3= [ [ 1*1+2*3+3*5 1*2+2*4+3*6] [ 4*1+5*3+6*5 4*2+5*4+6*6 ] ] = [ [ 22 28 ] [ 49 64 ] ]
print(m3);

元素相乘 (multiply)

  • 不常用
m1 = np.arange(1,5).reshape(2,2); # m1 = [ [ 1 2 ] [ 3 4 ] ]
m2 = np.arange(0,4).reshape(2,2); # m2 = [ [ 0 1 ] [ 2 3 ] ]
m3 = np.multiply(m1,m2);          # m3 = [ [ 1*0 2*1 ] [ 3*2 4*3 ] ] = [ [ 0 2 ] [ 6 12 ] ]
print(m3);

原始碼