Pythonic.swift is a Swift library implementating selected parts of Python's standard library and making them available to your Swift code.
import Pythonic
allows you to write Python flavored code such as:
#!/usr/bin/env xcrun swift -i -I .
import Pythonic
if re.search("^foo", "foobar") {
println(["foo", "bar", "zonk"].index("foo")) // 0
println(["foo", "bar", "zonk"].count("bar")) // 1
println(["foo", "bar", "zonk"].count("zoo")) // 0
}
if any(["foo", "bar", "zonk"]) {
println(chr(ord("a"))) // a
}
var strings = ["foo", "bar"]
println(":".join(strings)) // foo:bar
if strings {
println(strings[0]) // foo
}
if len(strings) == 2 {
println(strings[1].upper()) // BAR
println(strings[1].split("a")) // ["b", "r"]
}
var greeting = " hello pythonista "
if greeting.strip().startswith("hello") {
println(greeting.strip().title()) // Hello Pythonista
}
var numbers = [1, 2, 3, 4, 5]
println(sum(numbers)) // 15
println(max(numbers)) // 5
See the the test suite Pythonic-test.swift and the Swift syntax checker for more examples. Questions? Get in touch on Twitter @practicalswift.
git clone https://github.com/practicalswift/Pythonic.swift.git
cd Pythonic.swift/src/
make
make test
mkdir my-pythonic-app/
cd my-pythonic-app/
cp ../Pythonic.swiftdoc ../Pythonic.swiftmodule ../libPythonic.dylib .
cat << EOF > my_pythonic_app.swift
#!/usr/bin/env xcrun swift -I . -i
import Pythonic
assert(" hello ".strip() == "hello")
println("This feels really.. Pythonic!")
EOF
chmod +x my_pythonic_app.swift
./my_pythonic_app.swift
Code contributions are more than welcome! This is the quick guide to contributing:
- Fork the project
- Implement function
foo
from the Python standard library - Add a test case for
foo
inPythonic-test.swift
- Make sure the test case passes in both Python and Swift by running
make test
- Submit a pull request
- Repeat until we're done :-)