Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swiftpy: embedding Python in Swift

Johnlin
June 05, 2016

Swiftpy: embedding Python in Swift

Swiftpy: embedding Python in Swift
https://github.com/johnlinvc/swiftpy
Pycon Taiwan 2016 Lightning Talk

Johnlin

June 05, 2016
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

  1. Swift • New Open Source programming language backed by Apple®

    • Created by Chris Lattner, main author of LLVM • Now supports Linux!
  2. Swiftpy • http://github.com/johnlinvc/swiftpy • Evaluating Python code in Swift •

    Calling Python function in Swift • Conversion between Python object & Swift object
  3. Calling Python Function let hello:PythonString = "hello function" let upperHello

    = hello.call("upper") print(upperHello) λ ~/swiftpy_demo/ master .build/debug/swiftpy_demo HELLO FUCTION
  4. Define Python Function let defLeftPad = "def leftPad(str,len,fillchar):\n" + "

    return str.rjust(len,fillchar)" evalStatement(defLeftPad)
  5. Calling LeftPad let leftPadStr:PythonString = "leftPad" let n:PythonInt = 10

    let fillChar:PythonString = "j" let leftPadded = call("leftPad",args: leftPadStr, n, fillChar) print(leftPadded)
  6. Calling LeftPad let leftPadStr:PythonString = "leftPad" let n:PythonInt = 10

    let fillChar:PythonString = "j" let leftPadded = call("leftPad",args: leftPadStr, n, fillChar) print(leftPadded) λ ~/swiftpy_demo/ master .build/debug/swiftpy_demo jjjleftPad
  7. eval (expression only) let evalStr = eval("\"look! EVIL\"") print(evalStr) let

    addStr = eval("1 + 1") print(addStr) λ ~/swiftpy_demo/ master .build/debug/swiftpy_demo look! EVIL 2
  8. Define Class let defFoo = "class Foo:\n" + " def

    __init__(self):\n" + " self.bar = 'i am an ivar'" evalStatement(defFoo)
  9. Getting Attribute let foo = eval("Foo()") let bar = foo.attr("bar")

    print(bar) λ ~/swiftpy_demo/ master .build/debug/swiftpy_demo i am an ivar
  10. Setting Attribute let newBarVal:PythonString = "i'm the new bar" foo.setAttr("bar",

    value:newBarVal) let newBar = foo.attr("bar") print(newBar)
  11. Setting Attribute let newBarVal:PythonString = "i'm the new bar" foo.setAttr("bar",

    value:newBarVal) let newBar = foo.attr("bar") print(newBar) λ ~/swiftpy_demo/ master .build/debug/swiftpy_demo i am an ivar i'm the new bar
  12. λ ~//swiftpy_demo/ pandas_demo .build/debug/pandas_demo Traceback (most recent call last): File

    "<string>", line 1, in <module> ImportError: No module named pandas fatal error: unexpectedly found nil while unwrapping an Optional value Current stack trace: 0 libswiftCore.dylib 0x000000010863ab20 swift_reportError + 125 … 20 libdyld.dylib 0x00007fff913205ac start + 1 [1] 68673 illegal hardware instruction .build/ debug/pandas_demo
  13. λ ~/Projs/2016/pyconftw/swiftpy_demo/ pandas_demo .build/debug/pandas_demo Traceback (most recent call last): File

    "<string>", line 1, in <module> ImportError: No module named pandas fatal error: unexpectedly found nil while unwrapping an Optional value Current stack trace: 0 libswiftCore.dylib 0x000000010863ab20 swift_reportError + 125 … 20 libdyld.dylib 0x00007fff913205ac start + 1 [1] 68673 illegal hardware instruction .build/ debug/pandas_demo
  14. Future • Fix import error!! • Better conversion between Swift

    object & Python object • Support more python types • Calling Swift from Python
  15. Swift.Taipei meetup • http://swift.taipei • Every 3 weeks at iCHEF

    office. • Next is 6/14 • FREE! Beer and Food