git 範例

git 分支, 合併, tag 之範例

# 建立 develop branch
git branch develop
git checkout develop
git branch

for t in {1..5};
do
  TAG=v0.${t}
  FEATURE=feature/${TAG}

  # 建立 feature branch
  git branch ${FEATURE}
  git checkout ${FEATURE}
  git branch

  # commit 5個版本
  for i in {1..5};
  do
    data=$(printf "${FEATURE}-%03d" $i)
    echo $data >> history.txt
    history=$(cat history.txt)
    printf "# Test\n## Version\n${data}\n## History\n\`\`\`\n${history}\n\`\`\`\n" > readme.md
    git add .
    git commit -m "version: ${data}"
    git push -u origin ${FEATURE}
  done
  git branch

  # 切換develop
  git checkout develop
  git branch

  # 合併
  git merge --no-ff -m "合併(from ${FEATURE} to develop)" ${FEATURE}
  git push -u origin develop

  # 刪除分支
  git branch -d ${FEATURE}
  git push -u origin :${FEATURE}
  git push -u origin develop

  # 切換到master
  git checkout master
  git branch

  # 合併
  git merge --no-ff -m "合併(from 合併 to master)" develop
  git push -u origin master

  # 建立release
  git tag ${TAG}
  git tag -l
  git push origin ${TAG}
done

php發送log至syslog server

範例

<?php
function BSD_Syslog($msg,$severity=7, $program="php-test",$remote_ip='127.0.0.1',$remote_port=514){
  date_default_timezone_set('Asia/Taipei');
  $facility=5;
  $severity=7;
  $PRI = $facility*8+$severity;
  $HEADER=date('M d H:i:s ') . $program;
  $syslog_message = "<{$PRI}>{$HEADER} {$msg}";
  $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  socket_sendto($sock, $syslog_message, strlen($syslog_message), 0, $remote_ip, (int)$remote_port);
  socket_close($sock);
}
BSD_Syslog("test: ".rand()%10000);

其中facility的值如下:

         Code

          0             kernel messages
          1             user-level messages
          2             mail system
          3             system daemons
          4             security/authorization messages
          5             messages generated internally by syslogd
          6             line printer subsystem
          7             network news subsystem
          8             UUCP subsystem
          9             clock daemon
         10             security/authorization messages
         11             FTP daemon
         12             NTP subsystem
         13             log audit
         14             log alert
         15             clock daemon (note 2)
         16             local use 0  (local0)
         17             local use 1  (local1)
         18             local use 2  (local2)
         19             local use 3  (local3)
         20             local use 4  (local4)
         21             local use 5  (local5)
         22             local use 6  (local6)
         23             local use 7  (local7)

severity的值如下:


         Code
          0       Emergency: system is unusable
          1       Alert: action must be taken immediately
          2       Critical: critical conditions
          3       Error: error conditions
          4       Warning: warning conditions
          5       Notice: normal but significant condition
          6       Informational: informational messages
          7       Debug: debug-level messages

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)