No project description provided
Project description
UnityPython
Key points:
- Run a Python-like programming language on ALL platforms such as iOS, Android, etc.
- Good IDE support via Pylance, type checked! (see unitypython-typeshed)
- Dynamic and fast code loading in Unity. Edit code and see how UI changes immediately (Unity Editor is painful to me)!
- An extensible coroutine implementation.
async
andawait
are not as restricted as that in CPython and can be used as fast/fine-grained controlled event loops in game development.
UnityPython is based on CPython 3.10, but not fully compatible to CPython. For instance, StopIteration
is not used for loop constructs (exceptions are heavy and not used for control flow); type
is the only metaclass, etc.
import UnityEngine
class MyComponent(UnityEngine.MonoBehaviour):
def Start(self):
async def coro():
while not cond_satisfied:
await Task.Yield
do_something()
def coro2():
while not cond_satisfied:
yield
do_something()
self.StartCoroutine(coro())
self.StartCoroutine(coro2())
Contributing
See 0.1 roadmap
Basic development workflow:
cd UnityPython.FrontEnd && pip install -e . && cd ..
; requires Python 3.8+ (Python 3.10 is better)cd UnityPython.BackEnd
dotnet restore
bash build-code.sh && bash runtests.sh
UnityPython.BackEnd/tests/test_*.py
will be executed.
Besides, we badly need more tests! Help us and refer test_semantics.
How to add a method to datatypes? (concise way)
[PyBind]
can be used to bind Python methods. You can bind a Python staticmethod
or instance method through the following the steps:
-
implement an instacne/static method/instance property
[PyBind] public TrObject append(TrObject elt) { container.Add(elt); return MK.None(); }
-
build an DLL for
UnityPython.BackEnd
dotnet public -c Release
-
codegen using
UnityPython.BackEnd.CodeGen
dotnet run --project ../UnityPython.BackEnd.CodeGen/UnityPython.BackEnd.CodeGen.csproj
Then append
is bound.
[PyBind]
works for static methods and properties as well. Default arguments work.
NOTE: functions annotated by [PyBind]
must take a return value!!!
How to add a method to datatypes? (verbose way)
For example, if we want to implement append
for list
,
-
we firstly get to UnityPython.BackEnd/src/Traffy.Objects/List.cs
-
then we find the method annotated with
[Mark(Initialization.TokenClassInit)]
-
see the code
public static TrObject append(TrObject self, TrObject value) { ((TrList)self).container.Add(value); return RTS.object_none; // RTS = runtime support } [Mark(Initialization.TokenClassInit)] static void _Init() { CLASS = TrClass.FromPrototype<TrList>(); CLASS.Name = "list"; CLASS.InitInlineCacheForMagicMethods(); CLASS[CLASS.ic__new] = TrStaticMethod.Bind("list.__new__", TrList.datanew); // 1. 'TrSharpFunc.FromFunc' converts a CSharp function to UnityPython 'builtin_function' // 2. 'CLASS["somemethod".ToIntern()] = python-object' is equal to something like // class list: // somemethod = expr CLASS["append".ToIntern()] = TrSharpFunc.FromFunc("list.append", TrList.append); ... }
PS:
TrSharpFunc.FromFunc
creates a methodTrStaticMethod.Bind
creates astaticmethod
TrClassMethod.Bind
creates aclassmethod
TrProperty.Create(getter=null, setter=null)
creates aproperty
How to add a builtin-function
Basically, you just need to call Initialization.Prelude(string name, TrObject o)
.
For maintainability, please collect all builtin callables (other than datatypes) at UnityPython.BackEnd/src/Builtins.cs
namespace Traffy
{
public static class Builtins
{
static IEnumerator<TrObject> mkrange(int start, int end, int step)
{
for (int i = start; i < end; i += step)
yield return MK.Int(i);
}
// BLists, or double-ended lists, are designed for fast method call (requires adding a 'self' to the left end)
static TrObject range(BList<TrObject> args, Dictionary<TrObject, TrObject> kwargs)
{
var narg = args.Count;
switch(narg)
{
case 1:
return MK.Iter(mkrange(0, args[0].AsInt(), 1));
case 2:
return MK.Iter(mkrange(args[0].AsInt(), args[1].AsInt(), 1));
case 3:
return MK.Iter(mkrange(args[0].AsInt(), args[1].AsInt(), args[2].AsInt()));
default:
throw new TypeError($"range() takes 1 to 3 positional argument(s) but {narg} were given");
}
}
[Mark(Initialization.TokenBuiltinInit)]
public static void InitRuntime()
{
Initialization.Prelude(TrSharpFunc.FromFunc("range", range));
}
}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file unitypython-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: unitypython-0.2.1-py3-none-any.whl
- Upload date:
- Size: 21.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bed7805b44832811e6416393bc835b7b05dbfb95dce34b175244ccfc4d9024c |
|
MD5 | ee629483ec47c1b0244dbb63db75111a |
|
BLAKE2b-256 | 1da677cbffa26c46c0b33cce4a8150f79f44113756425f1956f249d7147a9621 |