Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyuff
#1
Hello,

I am a beginner with python and I would like to use the package pyuff to read some unv files using python3.

I try to use the examples here https://github.com/openmodal/pyuff (test_58.py)
but I don't get how to make it work.

I started by having the problem "NameError: name '__file__' is not defined" happening here
Quote:import numpy as np
import sys, os
my_path = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, my_path + '/../')

then I think I fixed it by changing to
Quote:import numpy as np
import sys, os
my_path = os.path.dirname(os.path.abspath('__file__'))
sys.path.insert(0, my_path + '/../')

But I don't know why I needed to make this modification, more than that the code is not returning any thing.
Why do I have such a main in the code
Quote:if __name__ == '__mains__':
np.testing.run_module_suite()

Why is it mains not main and what is the testing line there for ?


I ll appreciate if you help me understand all this.


Best regards,
Oussama
Reply
#2
in the tests folder you have tests, not examples. These are test that package developers of/contributors to pyuff package would run to test functionality they have developed.
the example usage is in the Showcase, which is IPython notebook
https://github.com/openmodal/pyuff/blob/...case.ipynb

as to the if __name__ == '__main__': it is paradigm that allows to import module as well to execute it as separate script. Usually above that line one would not put executable code (only imports, class/ function definitions, etc.), when the file is executed as a script its __name__ is always __main__ (that is python internals), so the body of the if block would be executed. However if you import the module, it would not be executed, but you will have access to all classes/functions imported.
Reply
#3
For this it was clear, now I can understand.(I am now curious how would they do the test i mean globally, so if you have a link where this is explained i ll appreciate it)
(Mar-09-2018, 07:27 AM)buran Wrote: in the tests folder you have tests, not examples. These are test that package developers of/contributors to pyuff package would run to test functionality they have developed.
the example usage is in the Showcase, which is IPython notebook
https://github.com/openmodal/pyuff/blob/...case.ipynb

For the main I understand that this is the main body of the script where the execution starts and calls the subroutines above, but it was mains not main in their code.

as to the if __name__ == '__main__': it is paradigm that allows to import module as well to execute it as separate script. Usually above that line one would not put executable code (only imports, class/ function definitions, etc.), when the file is executed as a script its __name__ is always __main__ (that is python internals), so the body of the if block would be executed. However if you import the module, it would not be executed, but you will have access to all classes/functions imported.
[/quote]

Thank you for helping :)
Reply
#4
I believe that __mains__ is an error. it should be __main__.
One way to run the tests is to run each script in test folder separately. That is why they have the last two lines in each file at the end. In this case it will use numpy built-in testing support which is based on nose. However due to mistake with __mains__ vs. __main__ I believe they don't runt it even once this way. Otherwise they would have discovered the mistake.
Because numpy uses nose, I believe they were running nosetests from command line and nose just grabs all tests from the files in test folder (based on the name) and execute them one by one.
Here is more info or nose and testing with nose http://nose.readthedocs.io/en/latest/testing.html

There are also other test suites, incl. unittest module from standard python library, pytest, etc.

there are plenty of resources online on unittesting, just google the term if you wnat more info
Reply
#5
Thank you :)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020