2016年1月30日 星期六

Python VS Swift

Code Comments

Both languages have comments:
# Python has single line comments 
 
// Swift has single line comments
/* Swift also has multi-line
   comments in C style
*/

Declaring Constants and Variables

Swift has rich support for type inference and constants. Python is dynamic and does not natively support constants.
name = "A string variable in Python"
age = 42 # An integer variable in Python
 
var name = "A string variable in Swift"
var age : Int // An explicit integer variable in Swift
age = 42
 
let pi = 3.14 // Constant in Swift

Integer Bounds

# Python does not have upper bounds for integer numbers (Python 3)
large_nun = 10000000000000000000000000000000000000000000000000000
 
// Swift
var a = Int32.min
var b = Int32.max

Type Inference

Swift is a strongly-typed language which makes heavy use of type-inference although you can declare explicit types. Python is a dynamic language so while there is a type system it is not evident in the syntax.
# Python
name = "Michael" # string variable, but can change
name = 42        # would run
n = 42           # currently an int
d = 42.0         # currently a float
 
// Swift
var name = "Michael" // string
name = 42            // Error
var n = 42           // int
var d = 42.0         // double

String Comparison

Python and Swift both have Unicode strings. Python generally has richer string support than Swift (especially around string formatting).
# python
a = "some text"
b = "some text"
if a == b:
    print("The strings are equal")
 
# swift
var a = "some text"
var b = "some text"
if a == b {
    println("The strings are equal")
}
Both languages have many functions on strings
# python
if a.startswith("some"): 
     print("Starts with some")

if a.endswith("some"):
     print("Ends with some")
 
// swift
if a.hasPrefix("some") {
     println("Starts with some")
 }
 
if a.hasSuffix("some") {
     println("Endss with some")
 }

String Upper or Lower Case

# python
s = "some text"
u = s.upper()
l = s.lower()
 
// swift
var s = "some text"
var u = s.uppercaseString
var l = s.lowercaseString

Declaring Arrays

Neither language has strict array types in the sense of C-based arrays. The arrays in Swift and Python are closer to lists. Python’s lists are not typed (hence can be heterogeneous).
# python
nums = [1,1,3,5,8,13,21]

// swift
var nums = [1,1,3,5,8,13,21]          // int array
var strings = ["one", "two", "three"] // string array

Working with Arrays

# Iteration in python
nums = [1,1,3,5,8,13,21]
for n in nums:
    print(n)
 
# Iteration in Swift:
var nums = [1,1,3,5,8,13,21]
for n in nums {
    println(n)
}
 
# Element access
n = nums[2]      # python, n = 3
var n = nums[2]  # swift,  n = 3
 
# Updating values
nums[2] = 10 # python
nums[2] = 10 # swift
 
# Check for elements
# python
if nums:
    print("Nums is not empty")

// swift
if !nums.isEmpty {
    println("Nums is not empty")
} 
 
# Adding items:
nums.append(7) # python
nums.append(7) # swift
 
# Slicing
nums = [1,1,3,5,8,13,21]
middle     = nums[2:4]       # python, middle = [3, 5]
var middle = nums[2..<4]     // swift, middle = [3,5]

Dictionaries

Dictionaries play important roles in both languages and are fundamental types.
# python
d = dict(name="Michael", state="OR")
d = { "name": "Michael", "state": "OR" }
the_name = d["name"]
 
// swift
var empty_dict = Dictionary<String, String>()
var d = ["name": "Michael", "state": "OR"]
var the_name = d["name"]
 
Adding items is the same in both languages. Removing entries is arguably clearer in Swift.
# add an item
d["hobby"] = "Biking" # python
d["hobby"] = "Biking" // swift
 
# remove an item
del d["hobby"]                # python
d.removeValueForKey("hobby")  // swift
 
Checking for the existence of a key can also be done in both languages.
# python
if "hobby" in d:
    print("Your hobby is " + d["hobby"])
 
// swift
if let theHobby = d["hobby"] {
     println("Your hobby is \(theHobby)")
}

Conditional Statements

Conditional statements are quite similar.
# python
n = 40
m = 2

if n > 40:
    print("n bigger than 40") 
elif m == 2 and n % 2 == 0:
    print("m is 2")
else:
    print("else")
 
# swift
var n = 40
var m = 2

if n > 40 {
    println("n bigger than 40") 
}
else if m == 2 && n % 2 == 0 {
    print("m is 2")
}
else {
    print("else")
}

Switch statements

Swift has them, Python does not.

Functions

Functions are very rich in both languages. They have closures, multiple return values, lambdas, and more. Here is a simple version. Note that this example also leverages tuples and tuple unpacking in both languages.

# python
def get_user(id):
    name = "username"
    email = "email"
    return name,email
 
n, e = get_user(1)
 
// swift
func getUser(id : Int) -> (String, String) {
     var username = "username"
     var email = "email"
     return (username, email)
}
 
var (n, e) = getUser(1) // n = username, e = email

2016年1月13日 星期三

VMware Workstation 10安裝Mac OS X El Capitan 10.11.x虛擬機器

想在32位元作業系統上執行64位元作業系統VM,可以使用 BootCamp 在Macmini上安裝 Windows 10 (32 bit),然後把OS X El Capitan (10.11)安裝到VMware Workstation 10。 

要注意的是VMW10的Hardware版本是10,可以在VMW 10/11,Fusion 6/7上執行。 但是Fusion 7的Hardware版本是11,只支援原生64 bit OS。
Native support for Mavericks (OS X 10.9) is included in VMware Fusion 6 and ESXi 5.5 running on Apple hardware, and once unlocked Workstation 10, Player 6 and ESXi 5.5 but require hardware compatibility set to version 10.
Native support for Yosemite (OS X 10.10) is included in VMware Fusion 7 and ESXi 6 running on Apple hardware, and once unlocked Workstation 11 and Player 7 but require hardware compatibility set to version 11.
Native support for El Capitan (OS X 10.11) is included in VMware Fusion 8 and ESXi 6 running on Apple hardware, and once unlocked Workstation 12 and Player 8 but require hardware compatibility set to version 11 or 12.

安裝步驟:
  1. 首先在Windows 10 PC上安裝 VMWare Workstation 10。
  2. 關閉VMware程式之後,然後依不同VMWare版本執行 unlocker 修改。VMware Unlocker for OS X 1.3.0。如果是在Mac使用Fusion或是使用Parallel,直接把Installer App拉過來就可以安裝了。
  3. 開始建立 VM上的Guest OS時,要注意看下拉式選單中有沒有OSX,如果你發現沒有,表示你安裝的unlocker是有問題的。
  4. VM設定完成後,就可以開始安裝OSX。
開啟虛擬機器之後首先是選語系。然後必須先進行「磁碟工具程式」,先把磁碟處理好,才能再進行後續的安裝動作。否則在安裝階段會找不到硬碟。過程請參考影片。

安裝完畢之後,要修改VM才能把機器序號傳給VM。
--------
board-id.reflectHost = "TRUE"
hw.model.reflectHost = "TRUE"
serialNumber.reflectHost = "TRUE"
smbios.reflectHost = "TRUE"

此外也可以使用以下 Shell Script 來獲得機器的資訊。

-------- #!/bin/sh
ioreg -l | awk '/product-name/ { split($0, line, "\""); printf("%s\n", line[4]); }'
ioreg -l | awk '/IOPlatformSerialNumber/ { split($0, line, "\""); printf("%s\n", line[4]); }'
ioreg -lp IOService | awk '/board-id/ { split($0, line, "\""); printf("%s\n", line[4]); }'

eg:
MacBookPro6,2
RM129481AGW
MAC-F22589C8

然後自己填上機器的資訊。
--------
board-id.reflectHost = "FALSE"
board-id = "MAC-F22589C8"
hw.model.reflectHost = "FALSE"
hw.model = "MacBookPro6,2"
serialNumber.reflectHost = "FALSE"
serialNumber = "RM129481AGW"
smbios.reflectHost = "FALSE"

附註:
  • smbios.use12CharSerialNumber = "TRUE" 這行好像不需要
  • 指定 board-id 不能用在VMW10,開不了機。
  • 修改 hw.model 似乎沒有影響,VM會根據序號給出model。
  • VMW10 的 guestOS 只支援 darwin13-64,Fusion 7支援darwin14-64
另外VMW10的Hardware 10不支援以下參數,所以不能使用iMessage等服務:
 6 digits ROM 12 digits MLB
--------
efi.nvram.var.ROM.reflectHost = "FALSE" 
efi.nvram.var.MLB.reflectHost = "FALSE"
efi.nvram.var.ROM = "123456"
efi.nvram.var.MLB = "DONKDONKDONKDONKA"

後記:

對於開發者而言,使用這種方式可以正確的安裝MAC虛擬機器來作為開發平台。缺點是Windows 10 32位元的作業系統記憶體受限制,最大可用記憶體只有顯示2.2G。

2016年1月10日 星期日

Converting CDR to ISO via Command Line

1. Converting a DMG to CDR

  • Mount the DMG disk image you want converted to CDR format by double-clicking it in the Finder
  • Launch Disk Utility found in /Applications/Utilities/
  • Select the .dmg image from the sidebar list and then click the “Convert” button in the toolbar
  • Pull down the “Image Format” menu and choose “DVD/CD master”, then click “Save”
2. Converting CDR to ISO via Command Line

hdiutil convert /path/imagefile.cdr -format UDTO -o /path/convertedimage.iso

2016年1月5日 星期二

Acronis True Image 2015 Backup Windows 10

使用 True Image 2015 來備份Windows 7時都很正常
升級為Windows 10之後卻一直出現錯誤訊息

 Unable to create volume snapshot

到官方網站查詢發現是要先關掉二個 Windows 服務
依照官網指示先改為 Manual 也不能備份
出現一樣的錯誤訊息
改為Disabled之後
再將服務Stop
終於成功備份了

https://kb.acronis.com/content/48686

Do the following if the issue still persists:
Set the status of Volume Shadow Copy and Microsoft Software Shadow Copy to Manual:
  1. Start -> Run -> services.msc.
  2. Locate Volume Shadow Copy and Microsoft Software Shadow Copy services, right-click and select Properties.
  3. Set the startup type to Manual.
  4. Restart the backup.
Alternatively, you can disable Volume Shadow Copy, if other programs do not require running VSS:
  1. Start -> Run -> services.msc
  2. Locate Volume Shadow Copy service, right-click and select Properties.
  3. Set the startup type to Disabled.
  4. Stop the service.
  5. Restart the backup.