naitoh / py2rb Goto Github PK
View Code? Open in Web Editor NEWA code translator using AST from Python to Ruby.
License: MIT License
A code translator using AST from Python to Ruby.
License: MIT License
It appears there is no translation of sys.argv
and sys.exit
. Without these it isnt possible to transpile CLI scripts that use args or have exit codes.
Hi there, I was giving a try, but I was a bit unfortunate.
I use rbenv
and pyenv
python --version
Python 3.6.10
ruby --version
ruby 2.5.4p155 (2019-03-13 revision 67245) [x86_64-darwin18]
I tried several different version of python
and ruby
but still failing
./run_tests.py
Looking for "ruby", "py2rb/builtins/module.rb", "py2rb/builtins/require.rb" [3]:
.. [OK]
tests/test_builtins.rb [1]: . [OK]
tests/algorithms/sqrt.py [4]: . [FAIL]
tests/algorithms/triangulation.py [4]: . [FAIL]
tests/basic/assert.py [4]: . [FAIL]
tests/basic/assign.py [4]: . [FAIL]
tests/basic/assign_slice.py [4]: . [FAIL]
tests/basic/augassign.py [4]: . [FAIL]
tests/basic/augassign2.py [4]: . [FAIL]
tests/basic/binaryops.py [4]: . [FAIL]
Any help would be appreciated
Hi,
could you please add more instructions how to use it?
Thanks
b'a'
are emitted as is, causes a syntax error in the ruby
for i in range(0, 10, 2):
print(i)
should emit 2->8, stepped by two
The transpiler emits
for i in 0.step(10, 2)
print(i)
end
which emits 2->10, stepped by two
The follow (bad) Python will catch the exception
try:
raise Exception("foo")
except:
print("Got it")
It is transpiled as
begin
raise Exception, "foo"
rescue
print("Got it")
end
That only catches the default Ruby exception StandardError
.
To catch the exception in Ruby, it needs to catch the top level Ruby exception class.
begin
raise Exception, "foo"
rescue Exception
print("Got it")
end
Hello.
Today I use py2rb for the first time to translate some Python scripts into Ruby.
I think this tool is very useful.
Keep up your great work ๐
gap = max(1, floor(gap / 1.25))
is transpiled to
gap = (1, floor(gap / 1.25.to_f)).max
error:
syntax error, unexpected ',', expecting ')'
I just found your package and it seems to be a good one.
I installed it and tried to run it but I just found this error message on my terminal:
$ py2rb python.py
Traceback (most recent call last):
File "/usr/local/bin/py2rb", line 7, in <module>
from py2rb import main
File "/usr/local/lib/python2.7/site-packages/py2rb/__init__.py", line 2919
print('Try : ' + py_path + ' -> ' + output + ' : ', end='')
^
SyntaxError: invalid syntax
The contents of my python.py
file:
result=0
a,b=0,2
for x in range(1500):
a,b=b,a+(4*b)
if a<4000000:
result+=a
print(result)
Can you help me?
Python
def f():
CODES = {"KEY": 1}
return CODES
becomes
def f()
CODES = {"KEY" => 1}
end
Ruby complains that CODES
is a constant, when it was a local variable in Python.
foo.rb:2: dynamic constant assignment
CODES = {"KEY" => 1}
ast.AnnAssign
proves typed assignments (a: int = 1
).
It is slightly different structure from ast.Assign
as there is only one target
instead of an array of targets
, and includes an annotation
which could be ignored initially.
output:
$ python -V
Python 3.10.4
$ pip -V
pip 22.1.2 from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip (python 3.10)
$ ruby -v
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [arm64-darwin21]
$ pip install pyyaml
Requirement already satisfied: pyyaml in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (6.0)
$ pip install numpy
Requirement already satisfied: numpy in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (1.23.0)
$ python run_tests.py
Looking for "ruby", "py2rb/builtins/module.rb", "py2rb/builtins/require.rb" [3]:
.. [OK]
tests/test_builtins.rb [1]: . [OK]
tests/algorithms/sqrt.py [4]: . [FAIL]
tests/algorithms/triangulation.py [4]: . [FAIL]
tests/basic/assert.py [4]: . [FAIL]
tests/basic/assign.py [4]: . [FAIL]
tests/basic/assign_slice.py [4]: . [FAIL]
tests/basic/augassign.py [4]: . [FAIL]
tests/basic/augassign2.py [4]: . [FAIL]
tests/basic/binaryops.py [4]: . [FAIL]
tests/basic/break.py [4]: . [FAIL]
tests/basic/class.py [4]: . [FAIL]
tests/basic/class2.py [4]: . [FAIL]
tests/basic/class3.py [4]: . [FAIL]
tests/basic/class4.py [4]: . [FAIL]
tests/basic/class5.py [4]: . [FAIL]
tests/basic/class6.py [4]: . [FAIL]
tests/basic/closure.py [4]: . [FAIL]
tests/basic/closure_in_class.py [4]: . [FAIL]
tests/basic/continue.py [4]: . [FAIL]
tests/basic/del_array.py [4]: . [FAIL]
tests/basic/del_attr.py [4]: . [FAIL]
tests/basic/del_dict.py [4]: . [FAIL]
tests/basic/del_slice.py [4]: . [FAIL]
tests/basic/dictcomp1.py [4]: . [FAIL]
tests/basic/dictcomp2.py [4]: . [FAIL]
tests/basic/dictionary.py [4]: . [FAIL]
tests/basic/dictionary2.py [4]: . [FAIL]
tests/basic/dictionary3.py [4]: . [FAIL]
tests/basic/docstring.py [4]: . [FAIL]
tests/basic/embedding.py [4]: . [FAIL]
tests/basic/fib.py [4]: . [FAIL]
tests/basic/float2int.py [4]: . [FAIL]
tests/basic/for_in.py [4]: . [FAIL]
tests/basic/for_range1.py [4]: . [FAIL]
tests/basic/for_range2.py [4]: . [FAIL]
tests/basic/for_step.py [4]: . [FAIL]
tests/basic/getattr.py [4]: . [FAIL]
tests/basic/globalvar.py [4]: . [FAIL]
tests/basic/hasattr1.py [4]: . [FAIL]
tests/basic/helloworld.py [4]: . [FAIL]
tests/basic/ifelse.py [4]: [FAIL]
tests/basic/ifs.py [4]: . [FAIL]
tests/basic/ifs2.py [4]: . [FAIL]
tests/basic/keys.py [4]: . [FAIL]
tests/basic/kwargs.py [4]: . [FAIL]
tests/basic/kwargs2.py [4]: [FAIL]
tests/basic/kwargs3.py [4]: . [FAIL]
tests/basic/lambda.py [4]: . [FAIL]
tests/basic/lambda2.py [4]: [FAIL]
tests/basic/lambda3.py [4]: [FAIL]
tests/basic/lambda_map.py [4]: . [FAIL]
tests/basic/list.py [4]: . [FAIL]
tests/basic/list2.py [4]: . [FAIL]
tests/basic/list3.py [4]: . [FAIL]
tests/basic/listcomp1.py [4]: [FAIL]
tests/basic/literals.py [4]: . [FAIL]
tests/basic/logicalops.py [4]: . [FAIL]
tests/basic/loops.py [4]: . [FAIL]
tests/basic/multiassign.py [4]: . [FAIL]
tests/basic/none.py [4]: . [FAIL]
tests/basic/numeric.py [4]: . [FAIL]
tests/basic/oo.py [4]: . [FAIL]
tests/basic/oo_attributes.py [4]: . [FAIL]
tests/basic/oo_inherit.py [4]: . [FAIL]
tests/basic/oo_inherit2.py [4]: . [FAIL]
tests/basic/oo_inherit3.py [4]: . [FAIL]
tests/basic/oo_inherit_simple.py [4]: . [FAIL]
tests/basic/oo_inherit_simple_super1.py [4]: . [FAIL]
tests/basic/oo_inherit_simple_super2.py [4]: . [FAIL]
tests/basic/oo_inherit_simple_super3.py [4]: [FAIL]
tests/basic/oo_static_inherit.py [4]: . [FAIL]
tests/basic/pass.py [4]: . [FAIL]
tests/basic/print.py [4]: . [FAIL]
tests/basic/raise.py [4]: . [FAIL]
tests/basic/raise2.py [4]: . [FAIL]
tests/basic/scope.py [4]: . [FAIL]
tests/basic/set.py [4]: . [FAIL]
tests/basic/str1.py [4]: . [FAIL]
tests/basic/str_endswith.py [4]: . [FAIL]
tests/basic/sumcomp.py [4]: . [FAIL]
tests/basic/super1.py [4]: . [FAIL]
tests/basic/super2.py [4]: . [FAIL]
tests/basic/trueorfalse.py [4]: . [FAIL]
tests/basic/try.py [4]: . [FAIL]
tests/basic/tuple.py [4]: . [FAIL]
tests/basic/tuple2.py [4]: . [FAIL]
tests/basic/vargs.py [4]: . [FAIL]
tests/basic/while.py [4]: . [FAIL]
tests/basic/with.py [4]: . [FAIL]
tests/collections/ordered_dict.py [4]: . [FAIL]
tests/decorator/function1.py [4]: . [FAIL]
tests/decorator/function2.py [4]: . [FAIL]
tests/decorator/function3.py [4]: . [FAIL]
tests/decorator/property.py [4]: . [FAIL]
tests/decorator/setter.py [4]: . [FAIL]
tests/deep-learning-from-scratch/and_gate.py [4]: [FAIL]
tests/deep-learning-from-scratch/relu.py [4]: [FAIL]
tests/deep-learning-from-scratch/sigmoid.py [4]: [FAIL]
tests/deep-learning-from-scratch/step_function.py [4]: [FAIL]
tests/errors/py_collision.py [4]: . [FAIL]
tests/functions/and.py [4]: . [FAIL]
tests/functions/append.py [4]: . [FAIL]
tests/functions/aug.py [4]: . [FAIL]
tests/functions/bitand.py [4]: . [FAIL]
tests/functions/bitor.py [4]: . [FAIL]
tests/functions/bitxor.py [4]: . [FAIL]
tests/functions/divfloor.py [4]: . [FAIL]
tests/functions/float.py [4]: . [FAIL]
tests/functions/floatdiv.py [4]: . [FAIL]
tests/functions/gtge.py [4]: . [FAIL]
tests/functions/in.py [4]: . [FAIL]
tests/functions/int.py [4]: . [FAIL]
tests/functions/isinstance.py [4]: . [FAIL]
tests/functions/len.py [4]: . [FAIL]
tests/functions/lshift.py [4]: . [FAIL]
tests/functions/ltle.py [4]: . [FAIL]
tests/functions/ne.py [4]: . [FAIL]
tests/functions/or.py [4]: . [FAIL]
tests/functions/pop.py [4]: . [FAIL]
tests/functions/rshift.py [4]: . [FAIL]
tests/functions/sort.py [4]: . [FAIL]
tests/functions/str.py [4]: . [FAIL]
tests/functions/ubitcomp.py [4]: . [FAIL]
tests/functions/uminus.py [4]: . [FAIL]
tests/functions/uplus.py [4]: . [FAIL]
tests/lists/all.py [4]: . [FAIL]
tests/lists/any.py [4]: . [FAIL]
tests/lists/extend.py [4]: . [FAIL]
tests/lists/filter.py [4]: . [FAIL]
tests/lists/in.py [4]: . [FAIL]
tests/lists/insert.py [4]: . [FAIL]
tests/lists/map.py [4]: . [FAIL]
tests/lists/max.py [4]: . [FAIL]
tests/lists/min.py [4]: . [FAIL]
tests/lists/reverse.py [4]: . [FAIL]
tests/lists/subclass.py [4]: . [FAIL]
tests/lists/subclass2.py [4]: . [FAIL]
tests/lists/subclass3.py [4]: . [FAIL]
tests/lists/sum.py [4]: . [FAIL]
tests/lists/sum2.py [4]: . [FAIL]
tests/lists/xrange.py [4]: . [FAIL]
tests/lists/zip.py [4]: . [FAIL]
tests/modules/classname.py [4]: . [FAIL]
tests/modules/from_import.py [4]: . [FAIL]
tests/modules/from_import_as_module.py [4]: . [FAIL]
tests/modules/import.py [4]: . [FAIL]
tests/modules/import_alias.py [4]: . [FAIL]
tests/modules/import_as_module.py [4]: . [FAIL]
tests/modules/import_class.py [4]: . [FAIL]
tests/modules/import_global.py [4]: . [FAIL]
tests/modules/import_init.py [4]: [FAIL]
tests/modules/import_multi.py [4]: . [FAIL]
tests/numpy/abs.py [4]: [FAIL]
tests/numpy/all.py [4]: [FAIL]
tests/numpy/any.py [4]: [FAIL]
tests/numpy/arange.py [4]: [FAIL]
tests/numpy/array2string.py [4]: [FAIL]
tests/numpy/asarray.py [4]: [FAIL]
tests/numpy/ellipsis.py [4]: [FAIL]
tests/numpy/empty.py [4]: [FAIL]
tests/numpy/ext_slice.py [4]: [FAIL]
tests/numpy/full.py [4]: [FAIL]
tests/numpy/insert.py [4]: [FAIL]
tests/numpy/like.py [4]: [FAIL]
tests/numpy/linspace.py [4]: [FAIL]
tests/numpy/max_min.py [4]: [FAIL]
tests/numpy/maximum_minimum.py [4]: [FAIL]
tests/numpy/ndarray.py [4]: [FAIL]
tests/numpy/ndarray2.py [4]: [FAIL]
tests/numpy/ndarray3.py [4]: [FAIL]
tests/numpy/not_like.py [4]: [FAIL]
tests/numpy/np_copy.py [4]: [FAIL]
tests/numpy/product.py [4]: [FAIL]
tests/numpy/random_rand.py [4]: [FAIL]
tests/numpy/random_randint.py [4]: [FAIL]
tests/numpy/random_random.py [4]: [FAIL]
tests/numpy/random_uniform.py [4]: [FAIL]
tests/numpy/special_values.py [4]: [FAIL]
tests/numpy/sqrt.py [4]: [FAIL]
tests/numpy/sum.py [4]: [FAIL]
tests/numpy/trigonometric_funcitons.py [4]: [FAIL]
tests/numpy/type.py [4]: [FAIL]
tests/os/path_basename.py [4]: . [FAIL]
tests/os/path_dirname.py [4]: . [FAIL]
tests/os/path_join.py [4]: . [FAIL]
tests/os/walk.py [4]: . [FAIL]
tests/random/random_random.py [4]: . [FAIL]
tests/six/integer_types.py [4]: . [FAIL]
tests/six/iteritems.py [4]: . [FAIL]
tests/six/itervalues.py [4]: . [FAIL]
tests/six/moves.range.py [4]: . [FAIL]
tests/six/py23.py [4]: . [FAIL]
tests/strings/count.py [4]: . [FAIL]
tests/strings/find.py [4]: . [FAIL]
tests/strings/join.py [4]: . [FAIL]
tests/strings/lstrip0.py [4]: . [FAIL]
tests/strings/lstrip1.py [4]: . [FAIL]
tests/strings/replace.py [4]: . [FAIL]
tests/strings/rfind.py [4]: . [FAIL]
tests/strings/rstrip0.py [4]: . [FAIL]
tests/strings/rstrip1.py [4]: . [FAIL]
tests/strings/split.py [4]: . [FAIL]
tests/strings/splitlines.py [4]: . [FAIL]
tests/strings/string_format_combined_simple.py [4]: . [FAIL]
tests/strings/string_format_d.py [4]: . [FAIL]
tests/strings/string_format_d_simple.py [4]: . [FAIL]
tests/strings/string_format_f_simple.py [4]: . [FAIL]
tests/strings/string_format_i.py [4]: . [FAIL]
tests/strings/string_format_s_simple.py [4]: . [FAIL]
tests/strings/string_format_u.py [4]: . [FAIL]
tests/strings/strings_in_strings.py [4]: . [FAIL]
tests/strings/strip.py [4]: . [FAIL]
tests/strings/strip1.py [4]: . [FAIL]
tests/strings/ulcase.py [4]: . [FAIL]
tests/strings/zipstring.py [4]: . [FAIL]
tests/unittest/assertAlmostEqual.py [4]: . [FAIL]
tests/unittest/assertEqual.py [4]: . [FAIL]
tests/unittest/assertIn.py [4]: . [FAIL]
tests/unittest/assertIs.py [4]: . [FAIL]
tests/unittest/assertIsInstance.py [4]: . [FAIL]
tests/unittest/assertRaises.py [4]: . [FAIL]
tests/unittest/assertTrue.py [4]: . [FAIL]
tests/unittest/class.py [4]: . [FAIL]
Ran 224 tests in 14.336s
FAILED (failures=222)
Hi, thanks for this great project!
When trying to translate a simple script with Python 3.8, the constants are missing.
print(1 + 2)
outputs
Warning : syntax not supported (<_ast.Constant object at 0x10c4da040>)
Warning : syntax not supported (<_ast.Constant object at 0x10c4da2e0>)
print( + )
It looks like there were changes in ast
for Python 3.8. From the docs:
Changed in version 3.8: Class ast.Constant is now used for all constants.
Deprecated since version 3.8: Old classes ast.Num, ast.Str, ast.Bytes, ast.NameConstant and ast.Ellipsis are still available, > but they will be removed in future Python releases. In the meanwhile, instantiating them will return an instance of a different > class.
There's also an unrelated warning about YAML.
/usr/local/lib/python3.8/site-packages/py2rb/__init__.py:29: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
module_map.update(yaml.load(f))
https://github.com/adsharma/py2many/blob/main/tests/cases/assert.py is roughly
#!/usr/bin/env python3
def compare_assert(a: int, b: int):
assert a == b
assert not (0 == 1)
if __name__ == "__main__":
assert True
assert not False
compare_assert(1, 1)
...
The assert of compare_assert
fails, with a ruby "unhandled exception"
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.