{
// 使用 IntelliSense 以得知可用的屬性。
// 暫留以檢視現有屬性的描述。
// 如需詳細資訊,請瀏覽: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "java Debug (Launch)",
"request": "launch",
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopOnEntry": false,
"mainClass": "",
"args": ""
},
{
"name": "nodejs Launch Program",
"type": "node",
"request": "launch",
"program": "${workspaceFolder}/app.js"
},
{
"name": "nodejs: Current File",
"type": "node",
"request": "launch",
"program": "${file}"
},
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost"
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "integratedTerminal",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true
},
{
"name": "Python: Current File (External Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
}
]
}
2018年10月17日 星期三
2018年10月2日 星期二
jquery 靜態工具
camelCase: 把 style key 化為駝峰
cleanData: 清除 $.data() 資料
cssHooks: css 設定提取所需要的特殊處理
cssNumber: 哪些 css 是數值類
attrHooks: 處理頑疾 attr
support: 檢測瀏覽器的支援
valHooks: 針對 table value
contains(context, elem): context 是否包含 elem
easing: 動畫用的運動函式
$.css:
$.style:
parseHTML(內文, context, option): context 通常指 document, option 若內文含 script 是否要執行
cleanData: 清除 $.data() 資料
cssHooks: css 設定提取所需要的特殊處理
cssNumber: 哪些 css 是數值類
attrHooks: 處理頑疾 attr
support: 檢測瀏覽器的支援
valHooks: 針對 table value
contains(context, elem): context 是否包含 elem
easing: 動畫用的運動函式
$.css:
$.style:
parseHTML(內文, context, option): context 通常指 document, option 若內文含 script 是否要執行
2018年9月26日 星期三
jq 偌多個組件需要統一使用 queue
// queue 的統一者
const queue_1 = {
name: "queue"
};
function t_1() {
$(queue_1).queue(function (next) {
$("#box_1").animate({
left: "+=5%"
}, {
duration: 1000,
queue: false,
complete: function () {
next();
}
})
});
}
const queue_1 = {
name: "queue"
};
function t_1() {
$(queue_1).queue(function (next) {
$("#box_1").animate({
left: "+=5%"
}, {
duration: 1000,
queue: false,
complete: function () {
next();
}
})
});
}
2018年9月20日 星期四
2018年8月29日 星期三
php 正則易碰到的問題
$s = '/a/b';
printf('<p>%s</p>', $s);
var_dump($s);
$res = preg_replace('/\//', '@', $s);
printf('<p>%s</p><hr>', $res);
//----------------------------
$s = '\\a\\b';
printf('<p>%s</p>', $s);
var_dump($s);
$res = preg_replace('/\\\\/', '@', $s);
printf('<p>%s</p><hr>', $res);
//----------------------------
$s = '\a\b';
printf('<p>%s</p>', $s);
var_dump($s);
$res = preg_replace('/\\\\/', '@', $s);
printf('<p>%s</p><hr>', $res);
----------------------------------------------------------
'/\\\\/' => php string = /\\/ => reg = /\/(匹配 '\')
'///' => php string = /// => reg = error
'/\//' => php string = /\// => reg = ///(匹配 '/')
----------------------------------------------------------
對 php preg來說 (/) 也屬於保留字
----------------------------------------------------------
對 "" 來說,對''無用
\$ 顯示金錢符號 $
\" 顯示雙引號符號 "
\' 顯示單引號符號 '
\\ 顯示倒斜線符號 \
\b Backspace鍵
\n 換行符號<br>
\r Return 歸位字元
\t Tab鍵
\000 ~ \377 以16進位表示某一個字元
\x00 ~ \xFF 以8進位表示某一個字元
單引號 ' ' 是連跳脫字元都不處理,也就是 \n 會當成 \ 和 n ,而不是換行!這點要特別注意!很多 bug 就是由此而來,例如 str_replace \n 沒有作用之類的。
附帶一提,雖然單引號 ' ' 裡面不處理跳脫字元,但在單引號 ' ' 裡面要寫 (') 或 (\) 還是要記得跳脫一下,不然 PHP 會無法判斷字串結尾︰
PHP 正則表達式(PCRE)
preg_last_error 函數用於轉義正則表達式字符。
正則表達式特殊字符有: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
參數說明:
執行結果轉義了 $ 和 / 特殊字符,如下所示:
返回 \$40 for a g3\/400
執行結果如下所示:
This book is <i>*very*</i> difficult to find.
printf('<p>%s</p>', $s);
var_dump($s);
$res = preg_replace('/\//', '@', $s);
printf('<p>%s</p><hr>', $res);
//----------------------------
$s = '\\a\\b';
printf('<p>%s</p>', $s);
var_dump($s);
$res = preg_replace('/\\\\/', '@', $s);
printf('<p>%s</p><hr>', $res);
//----------------------------
$s = '\a\b';
printf('<p>%s</p>', $s);
var_dump($s);
$res = preg_replace('/\\\\/', '@', $s);
printf('<p>%s</p><hr>', $res);
----------------------------------------------------------
'/\\\\/' => php string = /\\/ => reg = /\/(匹配 '\')
'///' => php string = /// => reg = error
'/\//' => php string = /\// => reg = ///(匹配 '/')
----------------------------------------------------------
對 php preg來說 (/) 也屬於保留字
----------------------------------------------------------
PHP的跳脫字元
對 "" 來說,對''無用
\$ 顯示金錢符號 $
\" 顯示雙引號符號 "
\' 顯示單引號符號 '
\\ 顯示倒斜線符號 \
\b Backspace鍵
\n 換行符號<br>
\r Return 歸位字元
\t Tab鍵
\000 ~ \377 以16進位表示某一個字元
\x00 ~ \xFF 以8進位表示某一個字元
單引號 ' ' 是連跳脫字元都不處理,也就是 \n 會當成 \ 和 n ,而不是換行!這點要特別注意!很多 bug 就是由此而來,例如 str_replace \n 沒有作用之類的。
附帶一提,雖然單引號 ' ' 裡面不處理跳脫字元,但在單引號 ' ' 裡面要寫 (') 或 (\) 還是要記得跳脫一下,不然 PHP 會無法判斷字串結尾︰
<?php echo '\''; // 會印 ' echo '\\'; // 會印 \ ?>
PHP preg_quote() 函數
PHP 正則表達式(PCRE)preg_last_error 函數用於轉義正則表達式字符。
語法
string preg_quote ( string $str [, string $delimiter = NULL ] ) preg_quote() 需要參數 str 並向其中 每個正則表達式語法中的字符前增加一個反斜線。 這通常用於你有一些運行時字符串 需要作為正則表達式進行匹配的時候。正則表達式特殊字符有: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
參數說明:
- $str: 輸入字符串。
- $delimiter: 如果指定了可選參數 delimiter,它也會被轉義。這通常用於 轉義 PCRE 函數使用的分隔符。 / 是最通用的分隔符。
返回值
返回轉義後的字符串。實例
實例 1
<?php
$keywords = '$40 for a g3/400';
$keywords = preg_quote($keywords, '/');
echo $keywords;
?>
返回 \$40 for a g3\/400
將文本中的單詞替換為斜體
<?php
//在這個例子中,preg_quote($word) 用於保持星號原文涵義,使其不使用正則表達式中的特殊語義。
$textbody = "This book is *very* difficult to find.";
$word = "*very*";
$textbody = preg_replace ("/" . preg_quote($word) . "/",
"<i>" . $word . "</i>",
$textbody);
echo $textbody;
?>
This book is <i>*very*</i> difficult to find.
2018年8月21日 星期二
php 一些取得 object 資訊的 API
get_class(): 取得物件的 className
get_parent_class(): 取得物件繼承者的 className
get_object_vars(): 返回類中所有的非靜態屬性
get_class_methods(): 函數的作用是返回由類的方法名組成的數組
2018年7月25日 星期三
php 讓 container 注入模組的方式
class Container {
public $name = 'a';
public $age = 19;
public $load;
public function __construct() {
$this->load = new Loader($this);
}
public function getName() {
printf('<p>%s</p>', $this->name);
}
}
// 注入模組
class Loader {
public $container;
public function __construct($container) {
$this->container = $container;
}
public function __get($name) {
if (isset($this->container->$name)) {
return $this->container->$name;
}
}
}
$a = new Container();
$load = $a->load;
var_dump($load->name);
public $name = 'a';
public $age = 19;
public $load;
public function __construct() {
$this->load = new Loader($this);
}
public function getName() {
printf('<p>%s</p>', $this->name);
}
}
// 注入模組
class Loader {
public $container;
public function __construct($container) {
$this->container = $container;
}
public function __get($name) {
if (isset($this->container->$name)) {
return $this->container->$name;
}
}
}
$a = new Container();
$load = $a->load;
var_dump($load->name);
2018年7月20日 星期五
php 取得最上層的 className
function getParentClassName($o) {
$res = $o;
while (TRUE) {
$ref = new ReflectionClass($res);
$r = $ref->getParentClass();
if ($r) {
$ref = $r;
} else {
break;
}
// className
$res = $ref->getName();
}
return $res;
}
$res = $o;
while (TRUE) {
$ref = new ReflectionClass($res);
$r = $ref->getParentClass();
if ($r) {
$ref = $r;
} else {
break;
}
// className
$res = $ref->getName();
}
return $res;
}
2018年6月30日 星期六
php &fn
Example1>>
<?php
function &_copy(){
// 函數(&)的目的
static $data = array();
return $data;
}
// 呼叫函數還是需要&
$a = &_copy();
$a[] = 5;
// 呼叫函數還是需要&
$b = &_copy();
var_dump($a);
var_dump($b);
------------------------------------------------
Example2>>
function &xyz(Array &$d){
return $d;
}
$data_1 = array(5, 1, 6);
$data_2 = &xyz($data_1);
//----------------------------
var_dump($data_2);
//----------------------------
sort($data_1);
var_dump($data_2);
<?php
function &_copy(){
// 函數(&)的目的
static $data = array();
return $data;
}
// 呼叫函數還是需要&
$a = &_copy();
$a[] = 5;
// 呼叫函數還是需要&
$b = &_copy();
var_dump($a);
var_dump($b);
------------------------------------------------
Example2>>
function &xyz(Array &$d){
return $d;
}
$data_1 = array(5, 1, 6);
$data_2 = &xyz($data_1);
//----------------------------
var_dump($data_2);
//----------------------------
sort($data_1);
var_dump($data_2);
2018年6月13日 星期三
gseries.js
function gseries(fn, data) {
return new Promise(function (res, rej) {
let it = fn();
job(data);
//----------------------------
function job(d) {
// 主要要不斷重複的步驟
let g = it.next(d);
let p = g.value;
// p.then(...)
if (g.done) {
res(p);
} else if (p instanceof Promise) {
p.then(function (d) {
job(d);
}, function (err) {
rej(err);
});
}else{
job(p);
}
}
});
}
return new Promise(function (res, rej) {
let it = fn();
job(data);
//----------------------------
function job(d) {
// 主要要不斷重複的步驟
let g = it.next(d);
let p = g.value;
// p.then(...)
if (g.done) {
res(p);
} else if (p instanceof Promise) {
p.then(function (d) {
job(d);
}, function (err) {
rej(err);
});
}else{
job(p);
}
}
});
}
java extend 的問題
public class A {
String name = "A";
public void a(){
System.out.println("i am " + name);
}
public void getName(){
System.out.println(name);
}
}
//=====================================
public class B extends A {
String name = "B";
public void b(){
System.out.println("i am " + name);
}
public void getName(){
System.out.println("extend " + name);
}
}
//=====================================
public static void main(String[] args) {
A a = new B();
a.a(); // i am A
a.getName(); // extend B
//----------------------------
B b = (B)a;
b.a(); // 容易出問題 => i am A
b.getName(); // extend B
b.b(); // i am B
}
String name = "A";
public void a(){
System.out.println("i am " + name);
}
public void getName(){
System.out.println(name);
}
}
//=====================================
public class B extends A {
String name = "B";
public void b(){
System.out.println("i am " + name);
}
public void getName(){
System.out.println("extend " + name);
}
}
//=====================================
public static void main(String[] args) {
A a = new B();
a.a(); // i am A
a.getName(); // extend B
//----------------------------
B b = (B)a;
b.a(); // 容易出問題 => i am A
b.getName(); // extend B
b.b(); // i am B
}
2018年6月5日 星期二
讓 js.generator 不斷往下執行
let gseries = require('../node_modules/gseries');
function asynce(index) {
return new Promise(function (res, rej) {
console.log('index(%s) start', index);
setTimeout(function () {
console.log('index(%s) end', index);
res(index);
}, 3000);
});
}
function* main() {
yield asynce(0);
yield asynce(1);
return asynce(2);
}
let p = gseries(main);
--------------------------------------------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
module.exports = gseries;
function gseries(fn, data) {
return new Promise(function (res, rej) {
let it = fn();
job(data);
//----------------------------
function job(d) {
// 主要要不斷重複的步驟
let g = it.next(d);
let p = g.value;
// p.then(...)
if (g.done) {
res(p);
} else if (p instanceof Promise) {
p.then(function (d) {
job(d);
}, function (err) {
rej(err);
});
}else{
job(p);
}
}
});
}
function asynce(index) {
return new Promise(function (res, rej) {
console.log('index(%s) start', index);
setTimeout(function () {
console.log('index(%s) end', index);
res(index);
}, 3000);
});
}
function* main() {
yield asynce(0);
yield asynce(1);
return asynce(2);
}
let p = gseries(main);
--------------------------------------------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
module.exports = gseries;
function gseries(fn, data) {
return new Promise(function (res, rej) {
let it = fn();
job(data);
//----------------------------
function job(d) {
// 主要要不斷重複的步驟
let g = it.next(d);
let p = g.value;
// p.then(...)
if (g.done) {
res(p);
} else if (p instanceof Promise) {
p.then(function (d) {
job(d);
}, function (err) {
rej(err);
});
}else{
job(p);
}
}
});
}
2018年6月2日 星期六
php 檔案鎖定(檔案同步)
PHP flock() 函數
定義和用法
flock() 函數鎖定或釋放文件。若成功,則返回 true。若失敗,則返回 false。
語法
flock(file,lock,block)
| 參數 | 描述 |
|---|---|
| file | 必需。規定要鎖定或釋放的已打開的文件。 |
| lock | 必需。規定要使用哪種鎖定類型。 |
| block | 可選。若設置為 1 或 true,則當進行鎖定時阻擋其他進程。 |
說明
flock() 操作的 file 必須是一個已經打開的文件指針。lock 參數可以是以下值之一:
- 要取得共享鎖定(讀取的程序),將 lock 設為 LOCK_SH(PHP 4.0.1 以前的版本設置為 1)。
- 要取得獨佔鎖定(寫入的程序),將 lock 設為 LOCK_EX(PHP 4.0.1 以前的版本中設置為 2)。
- 要釋放鎖定(無論共享或獨佔),將 lock 設為 LOCK_UN(PHP 4.0.1 以前的版本中設置為 3)。
- 如果不希望 flock() 在鎖定時堵塞,則給 lock 加上 LOCK_NB(PHP 4.0.1 以前的版本中設置為 4)。
提示和註釋
提示:可以通過 fclose() 來釋放鎖定操作,代碼執行完畢時也會自動調用。
註釋:由於 flock() 需要一個文件指針, 因此可能不得不用一個特殊的鎖定文件來保護打算通過寫模式打開的文件的訪問(在 fopen() 函數中加入 "w" 或 "w+")。
-----------------------------------------------------------
例子
a_1.php>>
<?php
ob_start();
$filename = "a1.txt";
$file = fopen($filename, "r+");
$res = '';
// 排它性的锁定
if (flock($file, LOCK_EX)) {
sleep(15);
while(!feof($file)){
$res .= fgets($file);
}
flock($file, LOCK_UN);
} else {
echo "Error locking file!";
}
fclose($file);
printf('<p>finish(%s)</p>', $res);
-----------------------------------------------------------
a_2.php>>
<?php
ob_start();
$filename = "a1.txt";
$file = fopen($filename, "r+");
$res = '';
// 排它性的锁定
if (flock($file, LOCK_EX)) {
while(!feof($file)){
$res .= fgets($file);
}
flock($file, LOCK_UN);
} else {
echo "Error locking file!";
}
fclose($file);
printf('<p>finish(%s)</p>', $res);
訂閱:
文章 (Atom)
