-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python 3.7, 3.8 support, improved unittest support #7
Open
nanobowers
wants to merge
12
commits into
naitoh:master
Choose a base branch
from
nanobowers:new_versions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7dde4eb
Various fixes
Fryguy 3b6a1f1
stashing fixes
nanobowers 1a4900f
Merge branch 'fryguy_fixes' into bbfix2
nanobowers 3c3e066
adding support for python up to 3.8.7 and ruby up to 3.0.0
nanobowers f707cbf
switch to markdown
nanobowers 3daf30b
cleaning up readme.md
nanobowers 7a409eb
removing readme.rst
nanobowers 2441ac8
initial fstring test
nanobowers 73b307c
adding fstring tests ; adding support for unittest/assertNotEqual,ass…
nanobowers 6fdc5bc
Adding in support for additional unittest types. Merging some of the…
nanobowers 460ef7b
backed out some of fryguys edits to return to writing warnings for un…
nanobowers f8e4250
fixed setup.py to point to README.md instead of old README.rst
nanobowers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
*.py[co] | ||
*~ | ||
.*.swp | ||
hello.txt | ||
tests/*/*.rb | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
# py2rb.py | ||
|
||
A code translator using AST from Python to Ruby. This is basically a | ||
NodeVisitor with Ruby output. See ast documentation | ||
(<https://docs.python.org/3/library/ast.html>) for more information. | ||
|
||
## Installation | ||
|
||
Execute the following: | ||
``` | ||
pip install py2rb | ||
``` | ||
or | ||
``` | ||
git clone git://github.com/naitoh/py2rb.git | ||
``` | ||
|
||
## Versions | ||
|
||
- Python 3.5 .. 3.8.7 | ||
- Ruby 2.4 .. 3.0.0 | ||
|
||
## Dependencies | ||
|
||
### Python | ||
|
||
pip install six | ||
pip install pyyaml | ||
pip install numpy | ||
|
||
### Ruby | ||
|
||
gem install numo-narray | ||
|
||
## Methodology | ||
|
||
In addition to walking and writing the AST tree and writing a Ruby | ||
syntax output, this tool either: | ||
- Monkey-patches (or refines) some common Ruby Modules and Classes in order to emulate the Python equivalent. | ||
- Calls equivalent Ruby methods to the python equivalent | ||
|
||
## Usage | ||
|
||
Sample 1: | ||
|
||
$ cat tests/basic/oo_inherit_simple.py | ||
|
||
```python | ||
class bar(object): | ||
|
||
def __init__(self,name): | ||
self.name = name | ||
|
||
def setname(self,name): | ||
self.name = name | ||
|
||
class foo(bar): | ||
|
||
registered = [] | ||
|
||
def __init__(self,val,name): | ||
self.fval = val | ||
self.register(self) | ||
self.name = name | ||
|
||
def inc(self): | ||
self.fval += 1 | ||
|
||
def msg(self, a=None, b=None, c=None): | ||
txt = '' | ||
varargs = a, b, c | ||
for arg in varargs: | ||
if arg is None: | ||
continue | ||
txt += str(arg) | ||
txt += "," | ||
return txt + self.name + " says:"+str(self.fval) | ||
|
||
@staticmethod | ||
def register(f): | ||
foo.registered.append(f) | ||
|
||
@staticmethod | ||
def printregistered(): | ||
for r in foo.registered: | ||
print(r.msg()) | ||
|
||
a = foo(10,'a') | ||
a.setname('aaa') | ||
b = foo(20,'b') | ||
c = foo(30,'c') | ||
|
||
a.inc() | ||
a.inc() | ||
c.inc() | ||
|
||
print(a.msg()) | ||
print(b.msg()) | ||
print(c.msg(2,3,4)) | ||
|
||
print("---") | ||
|
||
foo.printregistered() | ||
``` | ||
|
||
The above will result in : | ||
|
||
$ py2rb tests/basic/oo_inherit_simple.py | ||
|
||
```ruby | ||
class Bar | ||
def initialize(name) | ||
@name = name | ||
end | ||
def setname(name) | ||
@name = name | ||
end | ||
end | ||
class Foo < Bar | ||
def method_missing(method, *args) | ||
self.class.__send__ method, *args | ||
end | ||
@@registered = [] | ||
def initialize(val, name) | ||
@fval = val | ||
Foo.register(self) | ||
@name = name | ||
end | ||
def inc() | ||
@fval += 1 | ||
end | ||
def msg(a: nil, b: nil, c: nil) | ||
txt = "" | ||
varargs = [a, b, c] | ||
for arg in varargs | ||
if arg === nil | ||
next | ||
end | ||
txt += arg.to_s | ||
txt += "," | ||
end | ||
return ((txt + @name) + " says:") + @fval.to_s | ||
end | ||
def self.register(f) | ||
@@registered.push(f) | ||
end | ||
def self.printregistered() | ||
for r in @@registered | ||
print(r.msg()) | ||
end | ||
end | ||
def self.registered; @@registered; end | ||
def self.registered=(val); @@registered=val; end | ||
def registered; @registered = @@registered if @registered.nil?; @registered; end | ||
def registered=(val); @registered=val; end | ||
end | ||
a = Foo.new(10, "a") | ||
a.setname("aaa") | ||
b = Foo.new(20, "b") | ||
c = Foo.new(30, "c") | ||
a.inc() | ||
a.inc() | ||
c.inc() | ||
print(a.msg()) | ||
print(b.msg()) | ||
print(c.msg(a: 2, b: 3, c: 4)) | ||
print("---") | ||
Foo.printregistered() | ||
``` | ||
|
||
Sample 2: | ||
|
||
$ cat tests/deep-learning-from-scratch/and_gate.py | ||
|
||
```python | ||
# coding: utf-8 | ||
import numpy as np | ||
|
||
def AND(x1, x2): | ||
x = np.array([x1, x2]) | ||
w = np.array([0.5, 0.5]) | ||
b = -0.7 | ||
tmp = np.sum(w*x) + b | ||
if tmp <= 0: | ||
return 0 | ||
else: | ||
return 1 | ||
|
||
if __name__ == '__main__': | ||
for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]: | ||
y = AND(xs[0], xs[1]) | ||
print(str(xs) + " -> " + str(y)) | ||
``` | ||
|
||
The above will result in : | ||
|
||
$ py2rb tests/deep-learning-from-scratch/and_gate.py | ||
```ruby | ||
require 'numo/narray' | ||
def AND(x1, x2) | ||
x = Numo::NArray.cast([x1, x2]) | ||
w = Numo::NArray.cast([0.5, 0.5]) | ||
b = -0.7 | ||
tmp = ((w * x).sum()) + b | ||
if tmp <= 0 | ||
return 0 | ||
else | ||
return 1 | ||
end | ||
end | ||
|
||
if __FILE__ == $0 | ||
for xs in [[0, 0], [1, 0], [0, 1], [1, 1]] | ||
y = AND(xs[0], xs[1]) | ||
print((xs.to_s + (" -> ")) + y.to_s) | ||
end | ||
end | ||
``` | ||
Sample 3 (Convert all local dependent module files of specified Python | ||
file): | ||
``` | ||
$ git clone git://github.com/chainer/chainer.git | ||
$ py2rb chainer/chainer/__init__.py -m -p chainer -r -w -f | ||
Try : chainer/chainer/__init__.py -> chainer/chainer/__init__.rb : [OK] | ||
Warning : yield is not supported : | ||
Warning : yield is not supported : | ||
Try : chainer/chainer/configuration.py -> chainer/chainer/configuration.rb : [Warning] | ||
Try : chainer/chainer/cuda.py -> chainer/chainer/cuda.rb : [OK] | ||
: | ||
: | ||
Try : chainer/chainer/utils/array.py -> chainer/chainer/utils/array.rb : [OK] | ||
``` | ||
## Tests | ||
``` | ||
$ ./run_tests.py | ||
``` | ||
Will run all tests, that are supposed to work. If any test fails, it\'s | ||
a bug. | ||
``` | ||
$ ./run_tests.py -a | ||
``` | ||
Will run all tests including those that are known to fail (currently). | ||
It should be understandable from the output. | ||
``` | ||
$ ./run_tests.py -x or $ ./run_tests.py --no-error | ||
``` | ||
Will 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 -h | ||
``` | ||
## License | ||
|
||
MIT, see the LICENSE file for exact details. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing README.rst and creating README.md causes
pip install 'git+https://github.com/nanobowers/py2rb@new_versions'
to fail because setup.py requiresREADME.rst
.