2022年4月20日 星期三

Python 筆記

10 reasons why Python is better than C#

已經會C#,這篇是很好的起點,知道 Python 和C#有何不同,好在哪裡,要補K哪些文件。

The Python Standard Library

要補K那些?就看這簡潔又完整的官方文件。

Python 程式慣例與風格

每種程式都有它的風格和慣例,寫 Python 就用 Python 的慣例。透過好/壞寫法的比較,清楚該怎麼寫才是 Python style。

大多數情況下,我們是「讀」程式(code is read much more often than it is written),程式好讀對生產力大有幫助。

Switch to Python

已有程式經驗,切換至 Python 的步驟:

  1. The Basic Python Syntax
  2. Python args and kwargs: Demystified – Real Python
  3. Primer on Python Decorators – Real Python
  4. Python's Instance, Class, and Static Methods Demystified – Real Python
  5. Python Virtual Environments: A Primer – Real Python
  6. Python REST APIs With Flask, Connexion, and SQLAlchemy – Real Python
Real Python 網站很棒的地方是,Python 教學之後,還會提供「Real World Examples」,介紹實務上的用法及例子!

Python學習地圖

  • Python notes for beginners
    1. Python basic tutorial
    2. Syntax
    3. Statement, Indentation, and Comments
    4. Variables and Datatypes
    5. Operators
    6. Numbers
    7. Strings
    8. Data structure
    9. List
    10. Tuples
  • Python notes for intermediates
    1. Module
    2. Classes
    3. Methods
    4. Iterators
    5. Decorators
    6. Generators
  • Python notes for advanced learners
    1. Web framework
    2. Machine Learning
    3. Deep learning
    4. Artificial Intelligence
    5. Relational Database

避免錯誤

Why Python?

最後來點輕鬆的。

Guido van Rossum 命名靈感來自1970年代英國一個超現實/荒誕幽默喜劇團體 Monty Python。 Python 範例程式中的變數名稱叫 spam、eggs,也是在向他們的短劇 SPAM 致敬。

Monty Python 短劇: SPAM


2021年10月15日 星期五

C# 6 null ? 運算子

var readingCustomers = allCustomers

    .Where(c => c.Profile != null && 

c.Profile.DefaultShippingAddress != null && 

c.Profile.DefaultShippingAddress.Town == "Reading");

可以簡化成:

var readingCustomers = allCustomers

.Where(c => c.Profile?.DefaultShippingAddress?.Town == "Reading");

再來一個很適合用的例子:XML

string authorName = book.Element("author")?.Attribute("name")?.Value; 

如果有 author 元素,且它也有 name 屬性,就回傳,否則就是 null。


2021年9月25日 星期六

Speed Code 2: Effective C#

寫出有效率的C#程式,依主題條列化的筆記式寫法,光看目錄就馬上吸收到許多 tips,比如第一章〈C# Language idioms〉指出:

  • Item 1: Prefer Implicitly Typed Local Variables
  • Item 2: Prefer readonly to const 
  • Item 3: Prefer the is or as Operators to Casts
  • Item 4: Replace string.Format() with Interpolated Strings
  • Item 5: Prefer Formattable String for Culture-Specific Strings
  • Item 6: Avoid Stringly-Typed APIs
  • Item 7: Express Callbacks with Delegates
  • Item 8: Use the Null Conditional Operator for Event Invocations
  • Item 9: Minimize Boxing and Unboxing
  • Item 10: Use the new Modifier Only to React to Base Class Updates
  • .NET 臉笑維 已經讀了,而且寫下中文心得,請參考。

    「效率/Effective」是什麼?書裡提到 :

     a slower, correct program is better than a faster, broken program

    慢但正確比快但殘破的程式好。所以這本書的重點不在寫出跑得快的程式,而在寫出「正確」的程式,做對了,就做好了。

    作者深入 C# Compiler 行為,當我們知道程式怎麼被編譯,便自然能理解如何寫出正確、快速的程式。


    2021年9月22日 星期三

    Speed Code 1: 一定要會的 Visual Studio 快速鍵

    寫程式要快,熟悉開發工具是很基本的一件事,尤其是編寫程式時的鍵盤快捷鍵,不僅可以省下時間,還可以大大降低肌鍵炎的發生,這很重要!

    像是按著 Shift及Home/End 鍵可以選取至頭/至尾的文字,光是少掉一個抓滑鼠的動作,編寫程式的流程就順暢許多。

    看一下這十多分鐘的影片,可以幫你省下很多時間。

    Visual Studio Keyboard Shortcuts

    Visual Studio 的熱門鍵盤快速鍵

    剛開始不熟,多用幾次,把這張表印下來參考。相信我,只要一個上午你就都會了!

    https://visualstudio.microsoft.com/keyboard-shortcuts.pdf



    2020年10月19日 星期一

    Kendo UI Validator

    設定驗證規則

    HTML5 表單輸入欄位限制(Form input constraint attributes)

    透過 HTML 5 輸入欄位驗證屬性,瀏覽器會對這些條件進行驗證,基本上分為兩大類:
    • 資料型別(type)。如:url、email。
    • 驗證屬性(validation attribute),如:pattern、required、min、max、step、minlength、maxlength。
    但要考慮:使用者瀏覽器版本是否支援,即便支援也可能支援的不完全。還有一個問題:無法自訂驗證錯誤的訊息、位置及樣式。
    <form>
        姓名:<input type="text" name="name" required />
        網誌:<input type="url" name="blogUrl" required />
        <button>儲存</button>
    </form>
    https://dojo.telerik.com/@suefengcheng/OyiDiDaR/2

    Kendo UI Validator

    支援 HTML 5 表單輸入欄位限制外,還可自訂驗證條件。
      <form id="myform">
        姓名:<input type="text" name="name" required />
        網誌:<input type="url" name="blogUrl" required />
        <button id="save">儲存</button>
      </form>
      
      <script>
        $("#myform").kendoValidator({
             messages: {
                 onlyTomIsAllowed: "只接受Tom。",
                 url: "要是網址!",
                 required: "必要!",
             },
             rules: {
               onlyTomIsAllowed: function(input) {
                 if (input.is("[name=name]")) {
                     return input.val() === "Tom";
                 }
                 return true;
               }
             }
        });
        
        var validator = $("#myform").kendoValidator().data("kendoValidator");
        $("#save").on("click", function() {
          validator.validate();
        });
      </script>

    驗證對象

    Form 表單中所有輸入欄位。
    因此若要針對特定欄位驗證,請自行判斷input欄位。如下,針對名稱(name)為name的輸入欄位,驗證其值是否為 "Tom":
               onlyTomIsAllowed: function(input) {
                 if (input.is("[name=name]")) {
                     return input.val() === "Tom";
                 }
                 return true;
               }
    https://dojo.telerik.com/@suefengcheng/OyiDiDaR/3

    驗證順序

    Kendo UI Validator會先做HTML 5 輸入欄位驗證,通過後才跑自訂驗證。
    1. HTML 5 輸入欄位限制
    2. 自訂驗證條件

    顯示錯誤訊息

    Kendo UI Validator預設會將訊息放在輸入欄位旁,但這很可能被遮到。

    <span/>

    可自行加入一個<span/>,以data-for屬性指定對應的輸入欄位。如此,這輸入欄位的錯誤訊息便會顯示在這個<span/>裡。
      <form id="myform">
        <p>
          姓名:<input type="text" name="name" required /><br/>
          <span class="k-invalid-msg" data-for="name"></span>
        </p>
        <p>
          網誌:<input type="url" name="blogUrl" required /><br/>
          <span class="k-invalid-msg" data-for="blogUrl"></span>
        </p>
        <button id="save">儲存</button>
      </form>
    https://dojo.telerik.com/@suefengcheng/OyiDiDaR/3

    整筆/跨欄位驗證

    https://demos.telerik.com/kendo-ui/validator/custom-validation

    2018年7月27日 星期五

    如何 "使用" IFTTT Applet 教學: 以 IG/Instagram To Blogger 為例

    IFTTT (if this, then that) 意思是:如果這樣就那樣,也就是說它可以把兩個服務串接在一起。像是如果我在Instagram貼了一張照片,可請 IFTTT 自動幫我轉貼到 Google Blogger,發一篇貼文。

    IFTTT 提供許多 Applet 可幫我們串接網路上許多服務,像是Instagram、Facebook、Dropbox、Google Mail...,或是 App、不同裝置。

    以下就以 IG/Instagram 轉貼 Blogger 發文為例,說明如何使用 IFTTT 的 Applet。

    事實上本文例子使用的 Applet: Instagram To Blogger Post / IG 轉貼 Blogger 部落格發文,即是我在 IFTTT Platform 上設計的 Applet。若對如何設計IFTTT Applet 有興趣,可參考我的另一篇貼文:如何"建立" IFTTT Applet 教學: 以 IG/Instagram To Blogger 為例。

    Instagram To Blogger Post / IG 轉貼 Blogger 部落格發文

    step1: 先到 https://ifttt.com 註冊帳號。可用 Google/FB 帳號登入。

    step2: 點選上方的 Search,輸入關鍵字搜詢。如:轉貼。即可找到: Instagram To Blogger Post / IG 轉貼 Blogger 部落格發文,這個Applet。

    step3: 設定 Applet。

    以這個 Applet 為例,會針對特定 hashtag 的照片才做轉貼,因此需指定IG照片的HashTag。如下為只有標註為 #blog 的照片,才轉貼。
    另外還可設定要不要收到通知,以及部落格貼文的標籤。以上為例,會將部落格貼文標籤設為 Instagram。

    step4: My Applets

    之後在 IFTTT 可點選上方的 My Applets 檢視已啟用的 Applet 有那些。

    測試: 在 Instagram 上傳照片 

    如下我在 Instagram 上傳一張照片,注意:這張照片要記得加上 #blog,如此才滿足 IFTTT Applet 的觸發條件。

    檢視 Blogger 轉貼結果

    如下為 Blogger 轉貼的內容,可再連結回 IG 按讚!
    基本上它會自動觸發,但是有點時間差,不會馬上。若你心急可到 IFTTT 的 My Applets 點選它下方的 Check Now,馬上檢查,就會馬上執行了。

    如何 "建立" IFTTT Applet 教學: 以 IG/Instagram To Blogger 為例

    本文說明如何在 IFTTT Platform 上建立/設計自己的 Applet 供其他人使用。建議可先看 如何 "使用" IFTTT Applet 教學: 以 IG/Instagram To Blogger 為例,了解 IFTTT Applet 的用途及用法。

    註冊IFTTT Platform帳號

    先到 https://platform.ifttt.com 建立一個帳號。可用Google/FB帳號註冊。

    IFTTT Platform可建立 個人(Personal)/組織(Organization) 的Applet。在此我以個人的Applet為例。

    編輯Applet/Edit Applet

    step1: 設定Applet的來源服務為 Instagram, 觸發條件為特定的 Hashtag 標註, 標註可由使用者自行設定

     step2: 設定目標服務為 Blogger,  動作為發文

    step3: 設定部落格發文的標題

    step4: 嵌入IG照片做為發文內容

    step5: 設定部落格發文標籤

    step6: Applet的名稱及說明