Converts python files to starlark files
python -m venv venv
source venv/bin/activate
python setup.py installcd src/py2star
python cli.py larkify ~/src/pycryptodome/lib/Crypto/SelfTest/PublicKey/test_RSA.py > test_RSA.star
python cli.py tests test_RSA.star >> test_RSA.starThe list of differences between Starlark and Python are documented at https://bazel.build site:
- Differences with Python
- More differences documented as a Work In Progress in this Github issue
- Global variables are immutable.
forstatements are not allowed at the top-level. Use them within functions instead.ifstatements are not allowed at the top-level. However,ifexpressions can be used:first = data[0] if len(data) > 0 else None.- Deterministic order for iterating through Dictionaries.
- Recursion is not allowed.
- Modifying a collection during iteration is an error.
- Except for equality tests, comparison operators
<,<=,>=,>, etc. are not defined across value types. In short:5 < 'foo'will throw an error and5 == "5"will returnFalse. - In tuples, a trailing comma is valid only when the tuple is between parentheses, e.g.
write (1,)instead of1,. - Dictionary literals cannot have duplicated keys. For example, this is an error:
{"a": 4, "b": 7, "a": 1}. - Strings are represented with double-quotes (e.g. when you call repr).
- Strings aren't iterable (WORKAROUND: use
.elems()to iterate over it.)
- most
builtinfunctions, most methods. -
settypes (WORKAROUND: usesets.starinstead) - [ ]
implicit string concatenation (use explicit + operator). - Chained comparisons (e.g. 1 < x < 5)`.
-
class(seelarky.structfunction). -
import(seeloadstatement). -
while. -
generatorsandgenerator expressions. -
isandis not(use==and!=instead, respectively). -
raise(seefailfor fatal errors). -
try,except,finally. (seefailfor fatal errors). -
yield. -
global,nonlocal.
py2star.fixes.fix_declass-- de-classes and de-indents a class
So, it goes from:
class Foo(object):
def bar(self):
return "baz"To:
def bar(self):
return "baz"