Total Pageviews

2018/04/15

[Spring Boot] How turn off debug log in spring boot?

Problem
I am using spring boot to implement Neo4j, but the log file contains lots of debug message


How-To
Add the following configuration in your application.yml:
spring:
  data:
    neo4j:
      uri: bolt://localhost
      username: neo4j
      password: secret

logging:
  level:
    root: INFO
    org:
      springframework: 
        web: WARN
      hibernate: ERROR

Get rid of debug level log successfully:


2018/04/14

[Mac] 透過 Welly 連 ptt 出現亂碼

Problem
透過 Welly 連 ptt 出現亂碼


已在偏好設定,將預設編碼設定為 Big5 仍舊是亂碼


How-To
點選選單 "顯示方式" => "編碼" => 選擇 "Big5"


亂碼問題即可獲得解決


2018/04/13

[Mac] 如何變更截圖存放位置

Problem
在 Mac 中,可以透過 command(⌘)+ shift + 3​ 來截取整個桌面的圖


可透過 command(⌘)+ shift + 4 來截取所選定區域的圖


但是,系統預設會將截圖的圖檔放在桌面,如
 /Users/albert/Desktop  

若想要把它變更到某一特定目錄,如
 /Users/albert/Desktop/截圖  

該如何設定?


How-To
修改流程如下:

語法:
 defaults write com.apple.screencapture location /Users/albert/Desktop/截圖  


2018/04/12

[snakeyaml] How to hide bean type in snakeyaml?

Problem
I try to serialize a Java object into a YAML String, the expected YAML content looks like:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
  intents:
  - greet
  - inform
  - others
  - question
  entities:
  - userid
  - is_account_locked
  - pass_company_check
  actions:
  - utter_default
  - utter_greet
  - confirm:
      dependency:
        slots:
        - userid
  - unlock:
      dependency:
        slots:
        - userid
        - is_account_locked

But the actual YAML content is:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
  intents:
  - greet
  - inform
  - others
  - question
  entities:
  - userid
  - is_account_locked
  - pass_company_check
  actions:
  - utter_default
  - utter_greet
  - confirm: !!test.albert.snakeyaml.SnakeYamlTest$ActionAttribute
      dependency:
        slots:
        - userid
  - unlock: !!test.albert.snakeyaml.SnakeYamlTest$ActionAttribute
      dependency:
        slots:
        - userid
        - is_account_locked

How to hide bean type in snakeyaml?


Here has sample code:
 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
50
51
52
53
54
55
56
  package test.albert.snakeyaml;
  
  import java.util.Arrays;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import org.yaml.snakeyaml.DumperOptions;
  import org.yaml.snakeyaml.Yaml;
  import org.yaml.snakeyaml.representer.Representer;
  
  import lombok.AllArgsConstructor;
  import lombok.Data;
  import lombok.extern.slf4j.Slf4j;
  
  @Slf4j
  public class SnakeYamlTest {
  
      public static void main(String[] args) {
          // Set DumperOptions options
          DumperOptions options = new DumperOptions();
          options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
          options.setPrettyFlow(true);
            
          // Create Yaml instance with DumperOptions
          Yaml yaml = new Yaml(options);
  
          // Prepare map data for YAML
          Map<String, Object> actionMap1 = new HashMap<>();
          actionMap1.put("confirm", new ActionAttribute(new Dependency(Arrays.asList("userid"))));
  
          Map<String, Object> actionMap2 = new HashMap<>();
          actionMap2.put("unlock", new ActionAttribute(new Dependency(Arrays.asList("userid", "is_account_locked"))));
  
          Map<String, Object> map = new HashMap<>();
          map.put("intents", Arrays.asList("greet", "inform", "others", "question"));
          map.put("entities", Arrays.asList("userid", "is_account_locked", "pass_company_check"));
          map.put("actions", Arrays.asList("utter_default", "utter_greet", actionMap1, actionMap2));
  
          // Serialize a Java object into a YAML String
          String output = yaml.dump(map);
          log.info("\n" + output);
      }
  
      @Data
      @AllArgsConstructor
      private static class ActionAttribute {
          Dependency dependency;
      }
  
      @Data
      @AllArgsConstructor
      private static class Dependency {
          private List<String> slots;
      }
  }



How-To
Here has updated source code:
 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
50
51
52
53
54
55
56
57
58
59
60
61
  package test.albert.snakeyaml;
  
  import java.util.Arrays;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import org.yaml.snakeyaml.DumperOptions;
  import org.yaml.snakeyaml.Yaml;
  import org.yaml.snakeyaml.nodes.Tag;
  import org.yaml.snakeyaml.representer.Representer;
  
  import lombok.AllArgsConstructor;
  import lombok.Data;
  import lombok.extern.slf4j.Slf4j;
  
  @Slf4j
  public class SnakeYamlTest {
  
      public static void main(String[] args) {
          // Set DumperOptions options
          DumperOptions options = new DumperOptions();
          options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
          options.setPrettyFlow(true);
          
          // overwrite Representer
          Representer representer = new Representer();
          representer.addClassTag(ActionAttribute.class, Tag.MAP);
  
          // Create Yaml instance with Representer and DumperOptions
          Yaml yaml = new Yaml(representer, options);
  
          // Prepare map data for YAML
          Map<String, Object> actionMap1 = new HashMap<>();
          actionMap1.put("confirm", new ActionAttribute(new Dependency(Arrays.asList("userid"))));
  
          Map<String, Object> actionMap2 = new HashMap<>();
          actionMap2.put("unlock", new ActionAttribute(new Dependency(Arrays.asList("userid", "is_account_locked"))));
  
          Map<String, Object> map = new HashMap<>();
          map.put("intents", Arrays.asList("greet", "inform", "others", "question"));
          map.put("entities", Arrays.asList("userid", "is_account_locked", "pass_company_check"));
          map.put("actions", Arrays.asList("utter_default", "utter_greet", actionMap1, actionMap2));
  
          // Serialize a Java object into a YAML String
          String output = yaml.dump(map);
          log.info("\n" + output);
      }
  
      @Data
      @AllArgsConstructor
      private static class ActionAttribute {
          Dependency dependency;
      }
  
      @Data
      @AllArgsConstructor
      private static class Dependency {
          private List<String> slots;
      }
  }

2018/04/11

[Java 8] Using lambda instead of Comparator in Collections.sort

Assume I have a Java Bean class which named Sentence
import java.io.Serializable;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(of = "id", callSuper = false)
public class Sentence implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer id;

    private Integer projectId;
   
    private String text;

    private Integer intentId;

}


Assume I have a List of Sentence which named result:
List<Sentence> result = new ArrayList<>();


If I would like to sort result by intendId, we can using Comparator to fulfill this requirement before Java 8
    Collections.sort(result, new Comparator<Sentence>() {
        @Override
        public int compare(Sentence o1, Sentence o2) {
            return o1.getIntentId().compareTo(o2.getIntentId());
        }
    });


In Java 8, we can take advantage of Lambda to cope with this requirement easily:     
    Collections.sort(result, (s1, s2) -> s1.getIntentId().compareTo(s2.getIntentId()));


Reference
[1] https://dzone.com/articles/dont-fear-the-lambda

2018/04/10

[JavaScript] Remove an item from array

Scenario
Assume I have an array of data in $scope.tab2.sentence.entities
I would like to remove an item from array based on the value which I provide, how to do it?


How-To

You can utilize splice to fulfill the requirement.
Here has the code snippet:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 $scope.removeTab2Entity = function(entity) {
      var index;
      for (var i = 0; i < $scope.tab2.sentence.entities.length; i++) {
          if ($scope.tab2.sentence.entities[i].value == entity.value) {
              index = i;
              break;
          }
      }
      $scope.tab2.sentence.entities.splice(index, 1);
  }

splice syntax and its parameter information are as bellows:

Reference
[1] https://www.w3schools.com/jsref/jsref_splice.asp

2018/04/09

[Spring] Using RestTemplate to do HTTP Post and Get

Problem
How to use Spring RestTemplate to implement HTTP Post and Get.
The requirement is:
1. Using HTTP Get to get JSON string
2. Using HTTP Post to post JSON string to server




How-To

The example is as bellows:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    @Autowired
    private RestTemplate restTemplate;
    
    // example for HTTP Get
    String getUrl = "http://192.168.0.1:8080/status";
    try {
       String statusJSONString = restTemplate.getForObject(new URI(getUrl), String.class);
    } catch (RestClientException | URISyntaxException e) {
        throw new IllegalArgumentException(e.getMessage());
    }
    
    // example for HTTP Post
    String postUrl = "http://192.168.0.1:8080/train?name=test1";
        
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
  
    HttpEntity<String> entity = new HttpEntity<String>(jsonString, headers);
  
    try {
          restTemplate.postForEntity(new URI(url), entity, String.class);
    } catch (RestClientException | URISyntaxException e) {
        throw new IllegalArgumentException(e.getMessage());
    }


2018/04/08

[Angular] [Bootbox] File Upload Example

Problem
How to implement file upload function via Angular and Bootbox?

How-To

Here has sample code.
Code snippet in JavaScript:
 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
    $scope.importJson = function() {
     $scope.attachment = undefined;
     
     bootbox.dialog({
      message: "<input class='form-control' type='file' id='jsonFile' name='jsonFile' /> ",
      title: '匯入 JSON',
      buttons: {
       upload: {
           label: '上傳',
           callback: function() {
               cResource().uploadFile({
                   url: '/api/projects/' + $scope.project.id + '/importJSON',
                   file: $scope.attachment
               }).success(function(res, status, headers, config) {
                   cAlerter.success('上傳成功');
               }).error(function(res, status, headers, config) {
               });
           }
       },
       cancel: {
             label: '取消'
       }
      }
     });
     var jsonFile = document.getElementById("jsonFile");
     jsonFile.addEventListener("change", function (e) {
         $scope.attachment = this.files[0];
     }, false);
    };


Code snippet in controller class:

1
2
3
4
5
6
    @Transactional
    @PostMapping(value = "/{projectId}/importJSON", produces = MediaType.APPLICATION_JSON_VALUE)
    public void importJSON(@PathVariable Integer projectId,
      @RequestParam(value = "file") MultipartFile jsonFile) throws IOException {
  
    }





2018/04/07

[Maven] How to specify JDK version for Maven?

Problem
I have multiple JDK version in my computer, how to specify JDK version for Maven?

How-to

Add the two properties in your pom.xml
 <properties>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
 </properties>


2018/04/06

[Python] namedtuple

Assume I create a Dog class as bellows:
class Dog():
    def __init__(self, size, name):
        self.size = size
        self.name = name

If I would like to set values into Dog class, the code snippet looks like:
pug = Dog('小型犬', '巴哥犬')
shiba = Dog('中型犬', '柴犬')
print('size = {}, name = {}'.format(pug.size, pug.name))
print('size = {}, name = {}'.format(shiba.size, shiba.name))


We can use namedtuple instead of Class.The code snippet are the following:
from collections import namedtuple
# namedtuple instances are just as memory efficient as regular tuples 
# because they do not have per-instance dictionaries. 
# Each kind of namedtuple is represented by its own class, 
# created by using the namedtuple() factory function. 
# The arguments are the name of the new class and a string containing the names of the elements.
Dog = namedtuple('Dog', 'size name')
corgi = Dog('小型犬', '柯基')
husky = Dog('中型犬', '哈士奇')
print('size = {}, name = {}'.format(corgi.size, corgi.name))
print('size = {}, name = {}'.format(husky.size, husky.name))

2018/04/05

[Python] How to parse JSON string?

Problem
I plan to connect to a url, http://od.moi.gov.tw/api/v1/rest/datastore/A01010000C-000628-023, to get data with JSON format. How to parse JSON string vi Python, and get specific data?


How-to
Here has sample code:
import urllib.request, json
from test.json.row import row

import os
os.environ['http_proxy'] = 'http://proxy.cht.com.tw:8080'

with urllib.request.urlopen("http://od.moi.gov.tw/api/v1/rest/datastore/A01010000C-000628-023") as url:
    # return with dict data type
    data = json.loads(url.read().decode())
    dataList = []
    count = 1
    # 取得 result / records 下的資料
    for value in data['result']['records']:
        # Skip the first record
        if(count > 1):
            no = value['No']              # 編號
            address = value['Address']    # 地點位置
            deptNm = value['DeptNm']      # 管轄警察局
            branchNm = value['BranchNm']  # 分局
            dataList.append(row(no, address, deptNm, branchNm)) 
            print('no={}, address={}, deptNm={}, branchNm={}'.format(no, address, deptNm, branchNm))
        count += count 


Reference
[1] https://data.gov.tw/dataset/6247



2018/04/04

[閱讀筆記] The Little Book That Builds Wealth (3/7)



  1. 以生產流程所建立的成本優勢 (process-based advantages) ,通常只能建立暫時的護城河,當競爭者複製了你的低成本流程或自己也發明一套低成本流程,這個成本優勢很快就會消失
  2. 相較於以生產流程 (process-based advantages) 所建立的成本優勢,地點優勢 (location advantages) 可以較持久且難以複製
  3. 若一家公司很幸運地擁有天然資源且有較低的開採成本,通常就會擁有較佳的競爭優勢。如北美天然氣市場,Utrla Petoeum 就是一個很好的例子,它擁有豐富的天然資源,且它的開採成本約 700 百萬美金,相較於其他鄰近的對手需耗費 1700 ~ 2500 萬美金的開採成本,其競爭優勢不言可喻
  4. 擁有地理優勢的公司通常可以建立小壟斷 (mini-monopoly),擁有世界級天然資源蘊藏的優勢,難以被競爭對手複製
  5. 若此商品或服務的價格會是客戶的購買決策的主要因素,成本優勢 (cost advantages) 就會是個重要的事情。想想有沒有什麼產品或服務,你會因為另外一個品牌比較便宜就會去買它,如果有,若此產品或服務有成本優勢就可以建立起護城河
  6. 較便宜的流程 (cheapter processes)、較好的地理位置 (better locations) 與獨特的天然資源 (unique resources) 都可以建立起成本優勢。但是要多關注以流程為主的競爭優勢 (process-based advantages),因為這很容易被複製與模仿,當對手複製與模仿成功,護城河就馬上消失
  7. 在了解規模優勢 (scale advantages) 前,必須先區分固定成本 (fixed costs) 與變動成本 (variable costs)。以你家附近的便利超商為例,店面租金、水電費與員工薪資等,都屬於 fixed costs。貨架上的商品零售價格,則是屬於 variable costs
  8. 固定成本(又稱固定費用)相對於變動成本,是指成本總額在一定時期和一定業務量範圍內,不受業務量增減變動影響而能保持不變的成本。變動成本是指成本總額隨著業務量的變動而成正比例變動的成本。
  9. 為何速食店要在半夜營業?這是因為半夜人潮雖少,但其帶來的營收,在扣除掉員工薪資、水電費等「變動成本」之後,仍足以彌補部分「固定成本」。 例如,速食店晚上不管有沒有營業,每晚「固定成本」是一萬元,如果它晚上營業,要付夜班員工薪資、水電費的「變動成本」是八千元,這樣如果它選擇營業,每晚總成本就是一萬八千元。 在什麼情況下,業者會願意夜間營業?一般的答案是每晚營收要超過一萬八千元,業者才有利可圖而願意半夜營業。其實,只要半夜時段該速食店營收超過「變動成本」八千元,它就願意營業。
  10. 若某一個產業的 fixed costs 相對高於 variable costs,在此產業越能從規模優勢中獲得好處,如配銷業 (distribution)、製造業 (manufacturing) 與利基市場 (niche markets)
  11. 以配銷業 (distribution) 為例,最大的 fixed costs 就是運送貨物的卡車 (可能是購買或是租賃) 與司機薪水,其餘的如 variable costs 浮動的汽油價格與因送禮旺季所產生的司機加班費用 (overtime wages)
  12. 若你是配銷業 (distribution) 的新進入者,必須要跟已建立好配銷網絡的業者進行競爭,你就必須想辦法 cover 所需的 fixed costs,快遞更多的貨物才能有營利。若你無法建立起足夠的規模來 cover 所需的 fixed costs,在此之前,你必須一直承受損失
  13. 巨大與綿密的配銷網路 (distribution network) 非常難易複製,且常成為廣大經濟護城河的來源
  14. 大魚在小池塘裡可以賺大錢
  15. 儘管公司相對於其他競爭者來說不是很大,但是若能夠在特定市場區隔稱王,一樣可以獲取巨大的優勢。事實上,儘管所處的利基市場不大,只要能夠獲利就可以在此利基市場建立起接近獨佔的優勢,其他競爭者也會認為市場小,不想投入資金進入此小市場
  16. 以 The Washington Post 為例,其有在一些小城市如 Boise、Idaho 經營 cable-TV system,由於這些小城市的居民較少,故只能養得起一家 cable-service 營運商。其他競爭者也不想在多花資金來與  The Washington Post 競爭,因為此小市場只夠養一家廠商,再進來此市場恐會賠錢
  17. Blackboard Inc. 是企業技術和創新解決方案領域的全球領先供應商,致力於幫助改善全球數百萬學生及學習者的學習體驗。Blackboard 的解決方案使數千家高等教育、K-12、專業、企業和政府機構能夠進行網上教學拓展、推動校園商業及安全並更加有效地與他們所在社區進行溝通。在學習管理系統的領域 Blackboard 就吃下 ⅔ 的市場,儘管這不是一個很大的市場,無法吸引一些巨人如 Microsoft 或 Adobe 跨入此市場, 但也足夠讓 Blackboard 獲利,其他競爭者也因為不想投入資源來學習、了解客戶要什麼,所以也沒有什麼新的競爭者
  18. 成為小池塘中的大魚(a big fish in a small pond,有大材小用的意味),比去成為大池塘的大魚(a bigger fish in a bigger pond)來得好。聚焦於 fish-to-pond ratio,而非魚的大小 (利基市場的概念)
  19. 只要你的配銷成本比別人低,你就可以獲得可觀的獲利,配銷越多賺越多
  20. 規模經濟可以建立持久的競爭優勢
  21. 世界一直在改變,儘管改變是一個機會,但也可以嚴重地侵蝕現有的護城河,如底片、長途電話、報紙等。你必須持續觀察你所投資的公司的競爭優勢,是否有護城河被侵蝕的徵兆,若有發現就要進找替除此投資標的,避免蒙受損失
  22. 科技業的變化非常快,若投資人無法及早認清改變的趨勢,就必須承受痛苦的賠錢經驗。如底片業的 Kodak 還在掙扎中、報紙頁迅速被網際網路所取代、長途電話被 Internet-protocol networks 所取代等
  23. 若一家公司無法向客戶提高其所提供的產品或服務的價格,這就是一個強烈的訊號,代表此家公司的競爭力正被削弱中。以 Oracle 為例,其銷售資料庫軟體給客戶,並定期收取豐厚的維護費用。當版本更新時,會以舊版本不再維護為由,強迫客戶收集並收取費用,雖然客戶會有所抱怨,但還是得支付此生及費用。但是隨著 third-party company 的產生,其強調能持續維護舊版本的 Oracle database 的能力,讓原廠在定價上產生價格壓力,也縮小了原先已建立的護城河
  24. 科技的改變可以摧毀競爭優勢,而且需更加憂心的是被技術所推動的公司,而非販售技術的公司,其效應難以預期
  25. 若一家公司的客戶群變得很集中,或競爭對手攻擊的策略並不是要賺錢,護城河就會變得岌岌可危