Coder Social home page Coder Social logo

pythoncodable's Introduction

PythonCodable

Swift framework to efficiently bridge objects from Python to Swift.

Requirements

PythonCodable requires Swift 5 or higher.

Usage

PythonCodable builds on PythonKit to allow bridging arbitrary Python objects into Swift types efficiently using PythonDecoder:

import PythonKit
import PythonCodable

// 1. Get a valid Python object:

let urllibParse = Python.import("urllib.parse")
let pythonParsedURL = urllibParse.urlparse("http://www.cwi.nl:80/%7Eguido/Python.html")

print(pythonParsedURL)               // ParseResult(scheme='http', netloc='www.cwi.nl:80'...
print(Python.type(pythonParsedURL))  // <class 'urllib.parse.ParseResult'>

// 2. Define a compatible Swift struct conforming to `Decodable`:

struct ParsedURL: Decodable {
    let scheme: String
    let netloc: String
    let path: String
    let params: String?
    let query: String?
    let fragment: String?
}

// 3. Decode the Python object as a Swift type using `PythonDecoder`:

let parsedURL = try PythonDecoder.decode(ParsedURL.self, from: pythonParsedURL)

XCTAssertEqual(parsedURL.scheme, "http")
XCTAssertEqual(parsedURL.netloc, "www.cwi.nl:80")
XCTAssertEqual(parsedURL.path, "/%7Eguido/Python.html")

PythonDecoder supports multiple Python to Swift type conversions:

  • int to Int
  • float to Double
  • bool to Bool
  • None to nil
  • list[t] to Array<T> where t is one of the supported types.
  • dict[k, v] to Dictionary<K, T> where k is a str and v is one of the supported types.
  • object to Any : Decodable where object is a dict, a named tuple or an object with a dictionary representation.

Swift Package Manager

Add the following dependency to your Package.swift manifest:

.package(url: "https://github.com/pvieito/PythonCodable.git", .branch("master")),

References

pythoncodable's People

Contributors

pvieito avatar jkatzer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.