{"id":20037999,"url":"https://github.com/perfectlysoft/perfect-python","last_synced_at":"2025-05-05T06:31:53.090Z","repository":{"id":63920175,"uuid":"100990291","full_name":"PerfectlySoft/Perfect-Python","owner":"PerfectlySoft","description":"An expressway to import Python 2.7 modules into Server Side Swift","archived":false,"fork":false,"pushed_at":"2018-06-09T20:19:41.000Z","size":46,"stargazers_count":62,"open_issues_count":3,"forks_count":9,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-20T19:03:10.644Z","etag":null,"topics":["python","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PerfectlySoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-21T20:32:35.000Z","updated_at":"2024-03-26T09:03:36.000Z","dependencies_parsed_at":"2023-01-14T14:00:33.220Z","dependency_job_id":null,"html_url":"https://github.com/PerfectlySoft/Perfect-Python","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PerfectlySoft","download_url":"https://codeload.github.com/PerfectlySoft/Perfect-Python/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252451594,"owners_count":21749955,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python","swift"],"created_at":"2024-11-13T10:24:35.988Z","updated_at":"2025-05-05T06:31:52.759Z","avatar_url":"https://github.com/PerfectlySoft.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Perfect - Python [简体中文](README.zh_cn.md)\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"http://perfect.org/get-involved.html\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://perfect.org/assets/github/perfect_github_2_0_0.jpg\" alt=\"Get Involed with Perfect!\" width=\"854\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/PerfectlySoft/Perfect\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_1_Star.jpg\" alt=\"Star Perfect On Github\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"http://stackoverflow.com/questions/tagged/perfect\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/perfect_gh_button_2_SO.jpg\" alt=\"Stack Overflow\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"https://twitter.com/perfectlysoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_3_twit.jpg\" alt=\"Follow Perfect on Twitter\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"http://perfect.ly\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_4_slack.jpg\" alt=\"Join the Perfect Slack\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://developer.apple.com/swift/\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Swift-4.1-orange.svg?style=flat\" alt=\"Swift 4.1.2\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://developer.apple.com/swift/\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Platforms-OS%20X%20%7C%20Linux%20-lightgray.svg?style=flat\" alt=\"Platforms OS X | Linux\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://perfect.org/licensing.html\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/License-Apache-lightgrey.svg?style=flat\" alt=\"License Apache\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://twitter.com/PerfectlySoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Twitter-@PerfectlySoft-blue.svg?style=flat\" alt=\"PerfectlySoft Twitter\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://perfect.ly\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://perfect.ly/badge.svg\" alt=\"Slack Status\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\n\nThis project provides an expressway to import Python 2.7 module as a Server Side Swift Library.\n\nThis package builds with Swift Package Manager and is part of the [Perfect](https://github.com/PerfectlySoft/Perfect) project, but can also run independently.\n\nEnsure you have installed and activated the latest Swift 4.1.2 tool chain.\n\n## Acknowledgement\n\nA few updates were suggested by Chris Lattner. Thanks, @Chris.\n\n## Linux Build Note\n\nPlease make sure libpython2.7-dev was installed on Ubuntu 16.04:\n\n```\n$ sudo apt-get install libpython2.7-dev\n```\n\n## MacOS Build Note\n\nPlease make sure Xcode 9.0 or later version was installed.\n\n## Quick Start\n\nAdd PerfectPython dependency to your Package.swift\n\n``` swift\n.package(url: \"https://github.com/PerfectlySoft/Perfect-Python.git\", \n\tfrom: \"3.2.0\")\n\n// on target section:\n.target(\n\t// name: \"your project name\",\n\tdependencies: [\"PerfectPython\"]),\n```\n\nThen import two different libraries into the swift source code:\n\n``` swift\nimport PythonAPI\nimport PerfectPython\n```\n\nBefore any python api calls, make sure to initialize the library by calling `Py_Initialize()` function:\n\n``` swift\nPy_Initialize()\n```\n\n### Import Python Modules\n\nUse `PyObj` class to import python modules. In the following example, a python script `/tmp/clstest.py` has been imported into the current Swift context:\n\n``` swift\nlet pymod = try PyObj(path: \"/tmp\", import: \"clstest\")\n```\n\n### Access Python Variables\n\nOnce imported modules, you can use `PyObj.load()` function to access a variable value, or using `PyObj.save()` to store a new value to the current python variable.\n\nFor example, if there is a variable called `stringVar` in a python script:\n\n``` python\nstringVar = 'Hello, world'\n```\n\nThen you can read its value in such a form:\n\n``` swift\nif let str = pymod.load(\"stringVar\")?.value as? String {\n\tprint(str)\n\t// will print it out as \"Hello, world!\"\n}\n```\n\nYou can also directly overwrite the value of the same variable:\n\n``` swift\ntry pymod.save(\"stringVar\", newValue: \"Hola, 🇨🇳🇨🇦！\")\n```\n\n\n**NOTE** Currently, Perfect-Python supports the following data types between Swift and Python:\n\nPython Type|Swift Type|Remark\n----------|---------|-------\nint|Int|\nfloat|Double|\nstr|String|\nlist|[Any]|Recursively\ndict|[String:Any]|Recursively\n\nFor example, you can convert a Swift `String` to `PyObj` by: `let pystr = \"Hello\".python()` or `let pystr = try PyObj(value:\"Hello\")`.\n\nTo convert a `PyObj` to a Swift data type, e.g., a `String`, there are also two available approaches: `let str = pystr.value as? String` and `let str = String(python: pystr)`.\n\n### Call A Python Function\n\nMethod `PyObj.call()` is available to execute function call with arguments. Consider the python code below:\n\n``` python\ndef mymul(num1, num2):\n\treturn num1 * num2\n```\n\nPerfect-Python can wrap this call by its name as a string and the arguments as an array:\n\n``` swift\nif let res = pymod.call(\"mymul\", args: [2,3])?.value as? Int {\n\tprint(res)\n\t// the result will be 6\n}\n```\n\n### Python Object Classes\n\nThe same `PyObj.load()` function helps to access the python class type, however, a following method `PyObj.construct()` should be called for object instance initialization. This method also supports parameters as an array for python object class construction.\n\nAssume that there is a typical python class called `Person`, which has two properties `name` and `age`, and an object method called `intro()`:\n\n``` python\nclass Person:\n\tdef __init__(self, name, age):\n\t\tself.name = name\n\t\tself.age = age\n\t\t\n\tdef intro(self):\n\t\treturn 'Name: ' + self.name + ', Age: ' + str(self.age)\n```\n\nTo initialize such a class object in Swift, the first two steps look like:\n\n``` swift\nif let personClass = pymod.load(\"Person\"),\n    let person = personClass.construct([\"rocky\", 24]) {\n    // person is now the object instance\n  }\n```\n\nThen you can access the properties and class methods as common variables and functions do:\n\n``` swift\nif let name = person.load(\"name\")?.value as? String,\n    let age = person.load(\"age\")?.value as? Int,\n    let intro = person.call(\"intro\", args: [])?.value as? String {\n      print(name, age, intro)\n}\n```\n\n### Callbacks\n\nConsider the following python code as you can execute a function as a parameter like `x = caller('Hello', callback)`:\n\n``` python\ndef callback(msg):\n    return 'callback: ' + msg\n\ndef caller(info, func):\n    return func(info)\n```\n\nThe equivalent Swift code is nothing special but using the objective callback function as an argument before calling:\n\n``` swift\nif let fun = pymod.load(\"callback\"),\n   let result = pymod.call(\"caller\", args: [\"Hello\", fun]),\n   let v = result.value as? String {\n   \t\tprint(v)\n   \t// it will be \"callback: Hello\"\n}\n```\n\n## Further Information\nFor more information on the Perfect project, please visit [perfect.org](http://perfect.org).\n\n\n## Now WeChat Subscription is Available (Chinese)\n\u003cp align=center\u003e\u003cimg src=\"https://raw.githubusercontent.com/PerfectExamples/Perfect-Cloudinary-ImageUploader-Demo/master/qr.png\"\u003e\u003c/p\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperfectlysoft%2Fperfect-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fperfectlysoft%2Fperfect-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperfectlysoft%2Fperfect-python/lists"}