2018年12月13日 星期四

how to check windows version

查看windows版本兩種方法:

1. 指令ver

    Microsoft Windows [版本 10.0.17134.471]

2. 指令 slmgr.vbs -dlv

    軟體授權服務版本: 10.0.17134.471

    名稱: Windows(R), Professional edition
    描述: Windows(R) Operating System, VOLUME KMSCLIENT channel
    啟用識別碼: x-x-x-x-x
    應用程式識別碼: x-x-x-x-x
    延伸的PID: x-x-x-x-x-1028-17134.0000-1382018
    產品金鑰通路: Volume: GVLK
    安裝識別碼: x
    部分產品金鑰: x
    授權狀態: 已取得授權
    大量授權啟用到期: 255777分鐘(178天)
    剩餘的Windows重設授權狀態計數: 1001
    剩餘的SKU重設授權狀態計數: 1001
    信任時間: 2018/12/13 下午06:42:18
    已設定的啟用類型: All

    最近的啟用資訊:
    金鑰管理服務用戶端資訊
      用戶端電腦識別碼(CMID): x-x-x-x-x
      已登錄的KMS電腦名稱:  x.x.edu.tw:1688
      KMS電腦IP位址: x.x.x.x
      KMS電腦延伸的PID: x-x-x-x-x-1028-9600.0000-2932015
      啟用間隔: 120分鐘
      更新間隔: 10080分鐘
      啟用KMS主機快取

2018年11月28日 星期三

four design examples for the add function as class or instance methods

一個運算的方法到底要定義為類別方法物件方法,完全取決於設計者。
底下以簡單的整數加法運算為例,說明定義為類別方法或物件方法時,製作及使用上的差別。
一般是類別設計者先決定運算要如何使用,再決定如何製作。

四種設計關鍵在於:  類別方法或物件方法 vs 結果回傳或覆蓋第1數

(1) 類別方法,回傳結果

 public class WholeNumber
 {
    public int number;

    public WholeNumber(int n)
    {
      this.number = n;
    }

    public String toString()
    {
      return String.format("%d", number);
    }

    public static WholeNumber add(WholeNumber n1, WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = n1.number + n2.number;
      return n;
    }

    // n3:7 = n1:3 + n2:4
    public static void main(String args[])
    {
      WholeNumber n1 = new WholeNumber(3);
      WholeNumber n2 = new WholeNumber(4);
      WholeNumber n3 = WholeNumber.add(n1, n2);
      System.out.printf("n3:%s = n1:%s + n2:%s\n", n3, n1, n2);
    }
  } 


(2) 類別方法,結果覆蓋第1數

 public class WholeNumber
 {
    public int number;

    public WholeNumber(int n)
    {
      this.number = n;
    }

    public String toString()
    {
      return String.format("%d", number);
    }

    public static void add(WholeNumber n1, WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = n1.number + n2.number;
      n1.number = n.number;
    }

    // n1:7 = n1:3 + n2:4
    public static void main(String args[])
    {
      WholeNumber n1 = new WholeNumber(3);
      WholeNumber n2 = new WholeNumber(4);
      WholeNumber n1_copy = new WholeNumber(3);
      WholeNumber.add(n1, n2);
      System.out.printf("n1:%s = n1:%s + n2:%s\n", n1, n1_copy, n2);
    }
  } 


(3) 物件方法,回傳結果

 public class WholeNumber
 {
    public int number;

    public WholeNumber(int n)
    {
      this.number = n;
    }

    public String toString()
    {
      return String.format("%d", number);
    }

    public WholeNumber add(WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = this.number + n2.number;
      return n;
    }

    // n3:7 = n1:3 + n2:4
    public static void main(String args[])
    {
      WholeNumber n1 = new WholeNumber(3);
      WholeNumber n2 = new WholeNumber(4);
      WholeNumber n3 = n1.add(n2);
      System.out.printf("n3:%s = n1:%s + n2:%s\n", n3, n1, n2);
    }
 } 


(4) 物件方法,結果覆蓋第1數

 public class WholeNumber
 {
    public int number;

    public WholeNumber(int n)
    {
      this.number = n;
    }

    public String toString()
    {
      return String.format("%d", number);
    }

    public void add(WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = this.number + n2.number;
      this.number = n.number;
    }

    // n1:7 = n1:3 + n2:4
    public static void main(String args[])
    {
      WholeNumber n1 = new WholeNumber(3);
      WholeNumber n2 = new WholeNumber(4);
      WholeNumber n1_copy = new WholeNumber(3);
      n1.add(n2);
      System.out.printf("n1:%s = n1:%s + n2:%s\n", n1, n1_copy, n2);
    }
 }

以上四種寫法也可以合併成一支如下類別,方便比較四種用法的不同:

 class WholeNumber
 {
    public int number;

    public WholeNumber(int n)
    {
      this.number = n;
    }
    
    public WholeNumber(WholeNumber n)
    {
      this.number = n.number;
    }

    public String toString()
    {
      return String.format("%d", number);
    }

    public static WholeNumber classAddReturn(WholeNumber n1, WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = n1.number + n2.number;
      return n;
    }

    public static void classAddOverwrite(WholeNumber n1, WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = n1.number + n2.number;
      n1.number = n.number;
    }

    public WholeNumber instanceAddReturn(WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = this.number + n2.number;
      return n;
    }

    public void instanceAddOverwrite(WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = this.number + n2.number;
      this.number = n.number;
    }
/*
n3:7 = n1:3 + n2:4
n1:7 = n1:3 + n2:4
n4:7 = n1:3 + n2:4
n1:7 = n1:3 + n2:4
*/
    public static void main(String args[])
    {
      WholeNumber n1,n2,n3,n4,n1_copy;
      
      n1 = new WholeNumber(3);
      n2 = new WholeNumber(4);
      
      n3 = WholeNumber.classAddReturn(n1, n2);
      System.out.printf("n3:%s = n1:%s + n2:%s\n", n3, n1, n2);
      
      n1_copy = new WholeNumber(n1);
      WholeNumber.classAddOverwrite(n1_copy, n2);
      System.out.printf("n1:%s = n1:%s + n2:%s\n", n1_copy, n1, n2);
      
      n4 = n1.instanceAddReturn(n2);
      System.out.printf("n4:%s = n1:%s + n2:%s\n", n4, n1, n2);

      n1_copy = new WholeNumber(n1);
      n1_copy.instanceAddOverwrite(n2);
      System.out.printf("n1:%s = n1:%s + n2:%s\n", n1_copy, n1, n2);
    }
 } 

Virtual Machine Spec for Google Colab Environment

Google Colab提供最多12小時連線的Jupyter Notebook開發環境,其2018年底層的虛擬機實測規格如下:

CPU: Intel(R) Xeon(R) TwinCore @ 2.20GHz x 2

Memory: 13GB

Drive: 347GB

GPU: Tesla K80 with 4992 cores at 556MHz + 11GB Memory

OS: Ubuntu 18.04.1 LTS

Time Limit: 12 hours

規格實測的python指令如下:

# https://stackoverflow.com/questions/48750199/google-colaboratory-misleading-information-about-its-gpu-only-5-ram-available
# memory footprint support libraries/code
!ln -sf /opt/bin/nvidia-smi /usr/bin/nvidia-smi
!pip install gputil
!pip install psutil
!pip install humanize
import psutil
import humanize
import os
import GPUtil as GPU
GPUs = GPU.getGPUs()
# XXX: only one GPU on Colab and isn? guaranteed
gpu = GPUs[0]
def printm():
 process = psutil.Process(os.getpid())
 print("Gen RAM Free: " + humanize.naturalsize( psutil.virtual_memory().available ), " | Proc size: " + humanize.naturalsize( process.memory_info().rss))
 print("GPU RAM Free: {0:.0f}MB | Used: {1:.0f}MB | Util {2:3.0f}% | Total {3:.0f}MB".format(gpu.memoryFree, gpu.memoryUsed, gpu.memoryUtil*100, gpu.memoryTotal))
printm()

!df
!cat /etc/issue
!nvidia-smi
!nvidia-smi -L

!cat /proc/cpuinfo
!cat /proc/meminfo

2018年6月27日 星期三

create bootable iso from windows installation files

如果擁有Windows作業系統安裝檔案,可以利用ImgBurn軟體,將微軟安裝目錄製作成.iso,方便虛擬機安裝作業系統之用。 其關鍵步驟在選擇正確開機檔案,及載入磁區數目。重要參數如下:
  1.Source: Windows作業系統安裝檔案位置
  2.Destination: .iso檔案位罝
  3.Advanced/Bootable Disc
     Make Image Bootable
       Emulation Type: None (Custom)
       Boot Image: \boot\etfsboot.com
       Platform ID: 80x86
       Developer ID: Microsoft Corporation
       Load Segment: 07C0
       Sectors To Load: 8
  4.Build

  其中,載入磁區數目為8,推測是因為etfsboot.com檔案為4KB,佔512B磁區8個。

參考: Youtube: How to create a Bootable ISO Image *from* a Bootable USB Drive

2018年1月25日 星期四

how to decode web content in gzip or deflate format using Java API?

有些網站回傳網頁會進行內容壓縮,壓縮方法常見有gzip或deflate,
可由回傳內容的ContentEncoding標頭決定如何處理回傳的壓縮內容。

以Java為例,解壓縮寫法如下:


      // 設定下載網址
      URL url = new URL("http://comment.bilibili.tv/29545595.xml");  
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
      //conn.setRequestProperty("Accept-Encoding", "identity");

      // 連線取得網頁輸入流
      conn.connect();  
      System.out.printf("con.getContentEncoding()=%s\n",conn.getContentEncoding());
      InputStream in = conn.getInputStream();

      if(conn.getContentEncoding().equals("gzip"))
          in = new GZIPInputStream(conn.getInputStream());  

      if(conn.getContentEncoding().equals("deflate"))
          in = new InflaterInputStream(conn.getInputStream(), new Inflater(true));

      // 從網頁輸入流列印內容到螢幕
      BufferedReader bin = new BufferedReader(new InputStreamReader(in, "UTF-8"));  
      String s = null;  
      while((s=bin.readLine())!=null){  
         System.out.println(s);  
      }  
      bin.close();