Skip to content
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

Fix Python syntax errors and undefined names in examples #421

Closed

Conversation

cclauss
Copy link

@cclauss cclauss commented Apr 15, 2018

  • Add flake8 testing of Python code to just one of the Travis CI runs.
  • Fix the following issues to make all code syntax compatible with both Python 2 and Python 3:

Python 2

$ python2 -m flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics

./examples/Python3/demo4.py:41:34: E999 SyntaxError: invalid syntax
  print('recording audio...', end='', flush=True)
                                 ^
1     E999 SyntaxError: invalid syntax
1

Python 3

$ python3 -m flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics

./examples/Python/demo_threaded.py:43:33: E999 SyntaxError: invalid syntax
        print "Sum of number: {}".format(num1 + num2)
                                ^
./examples/Python/demo4.py:21:36: E999 SyntaxError: invalid syntax
    print "converting audio to text"
                                   ^
./examples/REST_API/training_service.py:28:73: E999 SyntaxError: invalid syntax
        print "Usage: %s wave_file1 wave_file2 wave_file3 out_model_name" % sys.argv[0]
                                                                        ^
./examples/Python/demo_threaded.py:43:20: F821 undefined name 'raw_input'
        num1 = int(raw_input("Enter the first number to add: "))
                   ^
./examples/Python/demo_threaded.py:44:20: F821 undefined name 'raw_input'
        num2 = int(raw_input("Enter the second number to add: "))
                   ^
3     E999 SyntaxError: invalid syntax
2     F821 undefined name 'raw_input'
5
  • The REST_API change is essential because it is not in either the Python or the Python3 directory.
  • Perhaps the Python directory should be renamed to Python2.

@chenguoguo
Copy link
Collaborator

Thanks! I'll check this PR together with other PRs later this week.

@AtosNicoS
Copy link

Would it be possible to merge the python2 and 3 demos and make them compatible for both versions? With your changes I can imagine that this will work.

@cclauss
Copy link
Author

cclauss commented May 3, 2018

Should be possible.

@AtosNicoS
Copy link

Please also take a look at this PR:
#383

The two could be merged. I think both PRs improve the python bindings a lot.

@chenguoguo I reviewed the PR, it looks good to me. I did not test it though.

@NicoHood
Copy link

NicoHood commented May 6, 2018

I adapted your changes to my PR (basically I just added the print import), but I cannot get the python2 code to run at all because of import errors. This does not happen for python3, and I dont know why. Any ideas?

python2 demo.py 
Traceback (most recent call last):
  File "demo.py", line 1, in <module>
    from snowboy import snowboydecoder
  File "/usr/lib/python2.7/site-packages/snowboy/snowboydecoder.py", line 5, in <module>
    import snowboydetect
  File "/usr/lib/python2.7/site-packages/snowboy/snowboydetect.py", line 17, in <module>
    _snowboydetect = swig_import_helper()
  File "/usr/lib/python2.7/site-packages/snowboy/snowboydetect.py", line 16, in swig_import_helper
    return importlib.import_module('_snowboydetect')
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named _snowboydetect

@cclauss
Copy link
Author

cclauss commented May 6, 2018

Are you in the Python or the Python3 directory when you do this?
What other changes (if any) are in your PR?

@NicoHood
Copy link

NicoHood commented May 6, 2018

I am in the python (for python2) directory. I removed all symlinks and the detector python file, as this should be inside the global system package, not the local one. But the same error also happens on my Python3 example, which should work on both versions

I am using this branch as base:
#383

I do compile the files in the swig/Python directory and then set PYTHONPATH=$(pwd). The demos are then completely separated from the python modul. The import works for python3, not for python 2 (considering you always update the pythonpath for v2/3).

Another thing I noticed: The snowboydecoder.py differs from python2 to 3. I think this should be avoided.

@chenguoguo
Copy link
Collaborator

Thank you very much guys! Since Nico have merged the changes to his PR, let's sort things out in his PR: #383

@AtosNicoS
Copy link

Should I also include the travis changes? Those are not yet included in the PR.

@chenguoguo
Copy link
Collaborator

@AtosNicoS please do

@cclauss cclauss closed this Nov 24, 2019
@cclauss cclauss deleted the Fix-Python-syntax-errors branch November 24, 2019 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants