No project description provided
Project description
pyala
Python 3.10 to Scala 2.13 Transpiler
Warning Project under development. Not available right now. Come back later.
Tutorial
How to use pyala
- Define your python function and add type annotations to its input parameters.
- Declare new variables with empty annotated assignment to specify their local scope.
- Use one of
pyala.to_str,pyala.to_objectorpyala.to_fileto transpile your code.
from pyala import pyala
# define your functions
def foo():
...
def goo():
...
# get the source code of a single function
print(pyala.to_str(foo))
# get the source code of multiple functions bundled in an object
print(pyala.to_object(foo, goo, object_name='Example'))
# save the source code in a file
print(pyala.to_file(foo, goo, filepath='/tmp/Example.scala'))
Examples
def foo():
y: int
if x < 0:
y = x**2
else:
y = x * 2
return y + x
Discrepancies
Here are a list of considerations to take into account due to discrepancies between scala and python.
Imports
Imports are not supported except for:
import math
The module scala.math is automatically imported and will mimic python built-in functions related to math, and the library math itself. This means that this code is valid, even though it uses an import:
import math
def foo(x: float):
return math.sqrt(x)
Reference types
A python reference cannot change type because scala does not allow it.
For instance, this python code is invalid in scala:
x = 4
x = True
because x is defined as a scala.Integer and cannot change to scala.Boolean.
Local Scope
In python, you can return a variable defined in a local scope. This is not allowed in scala.
For instance, this is valid python code:
def foo():
if True:
x = 5
return x
but this is not valid is scala:
def foo() = {
if (true) {
val x = 5
}
x
}
However, you can access variable defined in a parent scope, like this:
def foo() = {
var x: Integer = 0
if (true) {
x = 5
}
x
}
For this reason, pyala requires that you add an annotated assignment for each variable that you want to create:
def foo():
x: int = 0
if True:
x = 5
return x
Variable function arguments
You cannot assign a new value to a function argument. This code is invalid:
def foo(x: int):
x = x + 1
return x
You must create a new variable:
def foo(x: int):
z = x + 1
return z
F-string
String interpolation (f-string) is typed in scala. This means that this code is invalid:
def foo(x: int):
return f"{x:0.2f}"
You can only using float with "f" format:
def foo(x: int):
z: float = float(x)
return f"{z:0.2f}"
*args and **kwargs
First, **kwargs are not supported. Second, *args must always comes last in the argument list. This means that this code:
def foo(*x: int, z:int = 0):
return sum(x) - z
is transpiled to:
def foo(z: Int = 0, x: Int*) = {
(x.sum - z)
}
Note the order of the parameters is z then x instead of x then z.
You can call this function correctly in python with goo(3,4,5,6,z=7) but it will give a compilation error in scala because the correct call is goo(7,3,4,5,6).
Be extra careful when calling function defined in this way. The order will be correct in python but not in scala.
operator
FloorDiv
The return of floor div will always be a scala.Long, even if you use float.
Eg.: In python: 3.0//2 -> 1.0 (float) In scala: 3.0//2 -> 1 (Long)
hex
In python, hex(-42) returns "-0x2a", in scala it returns "0xffffffd6".
References
- Python AST: https://docs.python.org/3/library/ast.html
- Built-in Functions: https://docs.python.org/3/library/functions.html
- Math library: https://docs.python.org/3/library/math.html
Not Supported
expr NamedExpr
Lambda
Lambda expressions are not supported:
lambda x: x +1
operator MatMul
for loop with continue
async
The keyword async is not supported
complex
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 Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyala-0.1.0.tar.gz.
File metadata
- Download URL: pyala-0.1.0.tar.gz
- Upload date:
- Size: 21.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.4 Linux/5.4.0-110-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f93d78791ef82871f673b9a8e2288da23798532b749ffa0ddcf4a72b696dfc6
|
|
| MD5 |
4f8b1b3a36d5a8a120769305d3c33765
|
|
| BLAKE2b-256 |
b31bfb7e7118b706a6b7760cdda0dc2c159e240b6eff755d32d543526cbc8ad4
|
File details
Details for the file pyala-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pyala-0.1.0-py3-none-any.whl
- Upload date:
- Size: 20.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.4 Linux/5.4.0-110-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09e4d103c9ebe2e24c6d80ad4f94ce48dec5e8bc7d44651d3bacedfb4bcf4a38
|
|
| MD5 |
cc690cb0670ab57cb88388c111d0a870
|
|
| BLAKE2b-256 |
b8974ad1e80ebb1eb607eac74d5e8564e51f134b3a2c4d7885c1db153c3c3a64
|