Package: py2cr
py2cr.pyA code translator using AST from Python to Crystal. This is basically a
NodeVisitor with Crystal output. See AST documentation
(https://docs.python.org/3/library/ast.html) for more information.StatusCurrently more than 80% of the relevant tests are passing.  See more information below.InstallationExecute the following:pip install py2crorgit clone git://github.com/nanobowers/py2cr.gitVersionsPython 3.6 .. 3.9Crystal 1.1+DependenciesPythonpip install pyyaml

# Converts to num.cr in crystal-space
pip install numpy

# Probably not needed for much longer since py2 support is going to be removed.
pip install sixCrystalnum.cr- Preliminary support for translating numpy python code into crystal relies on num.cr.  This requires the latest (master) branch.MethodologyIn addition to walking and writing the AST tree and writing a Crystal
syntax output, this tool either:Monkey-patches some common Crystal stdlib Structs/Classes in order to emulate the Python equivalent functionality.Calls equivalent Crystal methods to the Python equivalentCalls wrapped Crystal methods that provide Python equivalent functionalityUsageGenerally,py2cr.py somefile.py > somefile.crThere is a Crystal shim/wrapper library insrc/py2cr(and linked intolib/py2cr) that is also referenced in the generated script.  You may need to copy that as needed, though eventually it may be appropriate to convert it to a shard if that is more appropriate.Tests$ ./run_tests.pyWill run all tests that are supposed to work. If any test fails, its
a bug.  (Currently there are a lot of failing tests!!)$ ./run_tests.py -aWill run all tests including those that are known to fail (currently).
It should be understandable from the output.$ ./run_tests.py basicWill run all tests matchingbasic.  Useful because running the entire test-suite can take a while.$ ./run_tests.py -x or $ ./run_tests.py --no-errorWill run tests but ignore if an error is raised by the test. This is not
affecting the error generated by the test files in the tests directory.For additional information on flags, run:./run_tests.py -hWriting new testsAdding tests for most new or existing functionality involves adding additional python files attests/<subdirectory/<testname>.py.The test-runner scripts will automatically run py2cr to produce a Crystal script, then run both the Python and Crystal scripts, then compare stdout/stderr and check return codes.For special test-cases, it is possible to provide a configuration YAML file on a per test basis namedtests/<subdirectory>/<testname>.config.yamlwhich overrides defaults for testing.  The following keys/values are supported:min_python_version: [int, int] # minimum major/minor version
max_python_version: [int, int] # maximum major/minor version
expected_exit_status: int      # exit status for py/cr test script
argument_list: [str, ... str]  # list of strings as extra args for argvTypingSome amount of typing support in Python is translated to Crystal.  Completely untyped Python code in many cases will not be translatable to compilable Crystal.   Rudimentary for pythonOptionalandUnionshould convert appropriately to Crystal typing.Some inference of bare list/dict types can now convert to[] of Xand{} of X, howeversetandtuplemay not work properly.StatusThis is incomplete and many of the tests brought forward from py2rb do not pass.  Some of them may never pass as-is due to significant language / compilation differences (even moreso than Python vs. Ruby)To some extent, it will always be incomplete.  The goal is to cover common cases and reduce the additional work to minimum-viable-program.Numpy tests have been disabled from the default testsuite, but about 1/3rd of them are operational and can be run with./run_tests.py numpyAdditional tests have been imported frompy2many.  Many of these do not operate (known to fail), but some have been used to enhance coverage of py2cr. :tada:  Run these with./run_tests.py py2manyIt is possible some of the py2many tests cover pre-existing tests, so some many be pruned out later on.LimitationsMany Python run-time exceptions are not translatable into Crystal as these issues manifest in Crystal as compile-time errors.A significant portion of python code is untyped and may not translate properly in places where Crystal demands type information.e.g. Crystal Lambda function parameters require typing and this is very uncommon in Python, though may be possible withCallable[]on the python side.Python importing is significantly different than Crystal and thus may not ever map well.Python Unittest does not have an equivalent in Crystal.  With some significant additional work, converting tests intoSpecformat may be possible viahttps://github.com/jaredbeck/minitest_to_rspecas a guide.To-doRemove python2/six dependencies to reduce clutter. Py2 has been end-of-lifed for a while now.Remove numpy dependencies unless/until a suitable target for Crystal can be identified (targeting num.cr now)Add additional Crystal shim methods to translate common python3 stdlib methods.  Consider a mode that just maps to a close Crystal method rather than using a shim-method to reduce the python-ness.Refactor the code-base.  Most of it is in the__init__.pyAdd additional unit-testsMulti-thread the test-suite so it can run faster.ContributeFree to submit an issue.   This is very much a work in progress, contributions or constructive feedback is welcome.If you'd like to hack onpy2cr, start by forking the repo on GitHub:https://github.com/nanobowers/py2crContributingThe best way to get your changes merged back into core is as follows:Fork it (https://github.com/nanobowers/py2cr/fork)Create a thoughtfully named topic branch to contain your change (git checkout -b my-new-feature)Hack awayAdd tests and make sure everything still passes by runningcrystal specIf you are adding new functionality, document it in the READMEIf necessary, rebase your commits into logical chunks, without errorsCommit your changes (git commit -am 'Add some feature')Push to the branch (git push origin my-new-feature)Create a new Pull RequestLicenseMIT, see the LICENSE file for exact details.