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

Allow vose_sampler to be consumable as a library #4

Merged
merged 2 commits into from
Nov 2, 2018

Conversation

DidgetyTech
Copy link
Contributor

Requesting a pull to asmith26:master from hoganbc:library to address #3

Changes

324d084 (hoganbc, 3 hours ago)
Move sys.exit calls to script logic

This will prevent unintended exits when the code is consumed as a library.
The functionality is preserved for script users.

Tests were updated to check for the exception type.

Additional change includes closing the bin_file when an Exception is-
thrown. There were warnings in the test output about this.

3d64a58 (hoganbc, 3 hours ago)
Move print statements and sample_n returns results

These two changes allow this project to be consumed as a library. The print
statements are moved to main(). There is no functional change for script
users.

Testing

Versions

$> python2 --version
Python 2.7.15
$> python3 --version
Python 3.7.0

Test results: passed

With Python 2.7

$> python2 setup.py test
running test
running egg_info
writing Vose_Alias_Method.egg-info/PKG-INFO
writing top-level names to Vose_Alias_Method.egg-info/top_level.txt
writing dependency_links to Vose_Alias_Method.egg-info/dependency_links.txt
writing entry points to Vose_Alias_Method.egg-info/entry_points.txt
reading manifest file 'Vose_Alias_Method.egg-info/SOURCES.txt'
writing manifest file 'Vose_Alias_Method.egg-info/SOURCES.txt'
running build_ext
running test
running egg_info
writing Vose_Alias_Method.egg-info/PKG-INFO
writing top-level names to Vose_Alias_Method.egg-info/top_level.txt
writing dependency_links to Vose_Alias_Method.egg-info/dependency_links.txt
writing entry points to Vose_Alias_Method.egg-info/entry_points.txt
reading manifest file 'Vose_Alias_Method.egg-info/SOURCES.txt'
writing manifest file 'Vose_Alias_Method.egg-info/SOURCES.txt'
running build_ext
test_output_alias_generation (tests.TestAccuracy)
Test vose_sampler.ProbDistribution.alias_generation to ensure it ... WARNING: There is a random element to test_output_alias_generation
        so it is likely to occasionally fail, nonetheless if the alias_generation
        method is working correctly failures will be very rare (testing at alpha=0.01
        implies we should expect a Type I error about 1% of the time).
ok
test_output_create_dist (tests.TestAccuracy)
Test vose_sampler.ProbDistribution.create_dist to ensure it correctly ... ok
test_output_get_word (tests.TestAccuracy)
Test vose_sampler.get_words to ensure it correctly produces a list of ... ok
test_roundtrip (tests.TestAccuracy) ... ok
test_binary_file1 (tests.TestValidation)
Test vose_sampler.get_words against .epub files ... ok
test_binary_file2 (tests.TestValidation)
Test vose_sampler.get_words against .mobi files ... ok
test_binary_file3 (tests.TestValidation)
Test vose_sampler.get_words against .pdf files ... ok
test_binary_file4 (tests.TestValidation)
Test vose_sampler.get_words against .wav files ... ok
test_empty_file (tests.TestValidation)
Test vose_sampler.get_words against empty files ... ok
test_negative_integer (tests.TestValidation)
Test vose_sampler.VoseAlias.alias_generation against a size ... ok
test_zero_integer (tests.TestValidation)
Test vose_sampler.ProbDistribution.alias_generation against a size ... ok

----------------------------------------------------------------------
Ran 11 tests in 3.146s

OK
test_output_alias_generation (tests.TestAccuracy)
Test vose_sampler.ProbDistribution.alias_generation to ensure it ... WARNING: There is a random element to test_output_alias_generation
        so it is likely to occasionally fail, nonetheless if the alias_generation
        method is working correctly failures will be very rare (testing at alpha=0.01
        implies we should expect a Type I error about 1% of the time).
ok
test_output_create_dist (tests.TestAccuracy)
Test vose_sampler.ProbDistribution.create_dist to ensure it correctly ... ok
test_output_get_word (tests.TestAccuracy)
Test vose_sampler.get_words to ensure it correctly produces a list of ... ok
test_roundtrip (tests.TestAccuracy) ... ok
test_binary_file1 (tests.TestValidation)
Test vose_sampler.get_words against .epub files ... ok
test_binary_file2 (tests.TestValidation)
Test vose_sampler.get_words against .mobi files ... ok
test_binary_file3 (tests.TestValidation)
Test vose_sampler.get_words against .pdf files ... ok
test_binary_file4 (tests.TestValidation)
Test vose_sampler.get_words against .wav files ... ok
test_empty_file (tests.TestValidation)
Test vose_sampler.get_words against empty files ... ok
test_negative_integer (tests.TestValidation)
Test vose_sampler.VoseAlias.alias_generation against a size ... ok
test_zero_integer (tests.TestValidation)
Test vose_sampler.ProbDistribution.alias_generation against a size ... ok

----------------------------------------------------------------------
Ran 11 tests in 3.209s

OK

With Python3.7

$> python3 setup.py test
running test
running egg_info
writing Vose_Alias_Method.egg-info/PKG-INFO
writing dependency_links to Vose_Alias_Method.egg-info/dependency_links.txt
writing entry points to Vose_Alias_Method.egg-info/entry_points.txt
writing top-level names to Vose_Alias_Method.egg-info/top_level.txt
reading manifest file 'Vose_Alias_Method.egg-info/SOURCES.txt'
writing manifest file 'Vose_Alias_Method.egg-info/SOURCES.txt'
running build_ext
running test
running egg_info
writing Vose_Alias_Method.egg-info/PKG-INFO
writing dependency_links to Vose_Alias_Method.egg-info/dependency_links.txt
writing entry points to Vose_Alias_Method.egg-info/entry_points.txt
writing top-level names to Vose_Alias_Method.egg-info/top_level.txt
reading manifest file 'Vose_Alias_Method.egg-info/SOURCES.txt'
writing manifest file 'Vose_Alias_Method.egg-info/SOURCES.txt'
running build_ext
test_output_alias_generation (tests.TestAccuracy)
Test vose_sampler.ProbDistribution.alias_generation to ensure it ... WARNING: There is a random element to test_output_alias_generation
        so it is likely to occasionally fail, nonetheless if the alias_generation
        method is working correctly failures will be very rare (testing at alpha=0.01
        implies we should expect a Type I error about 1% of the time).
ok
test_output_create_dist (tests.TestAccuracy)
Test vose_sampler.ProbDistribution.create_dist to ensure it correctly ... ok
test_output_get_word (tests.TestAccuracy)
Test vose_sampler.get_words to ensure it correctly produces a list of ... ok
test_roundtrip (tests.TestAccuracy) ... ok
test_binary_file1 (tests.TestValidation)
Test vose_sampler.get_words against .epub files ... /Users/hoganb/workplace/Vose-Alias-Method/tests/tests.py:32: DeprecationWarning: Please use assertRaisesRegex instead.
  self.assertRaisesRegexp(IOError, binary_file_error, vose_sampler.get_words, invalid_folder + "Alice.epub")
ok
test_binary_file2 (tests.TestValidation)
Test vose_sampler.get_words against .mobi files ... ok
test_binary_file3 (tests.TestValidation)
Test vose_sampler.get_words against .pdf files ... ok
test_binary_file4 (tests.TestValidation)
Test vose_sampler.get_words against .wav files ... ok
test_empty_file (tests.TestValidation)
Test vose_sampler.get_words against empty files ... ok
test_negative_integer (tests.TestValidation)
Test vose_sampler.VoseAlias.alias_generation against a size ... ok
test_zero_integer (tests.TestValidation)
Test vose_sampler.ProbDistribution.alias_generation against a size ... ok

----------------------------------------------------------------------
Ran 11 tests in 0.443s

OK
test_output_alias_generation (tests.TestAccuracy)
Test vose_sampler.ProbDistribution.alias_generation to ensure it ... WARNING: There is a random element to test_output_alias_generation
        so it is likely to occasionally fail, nonetheless if the alias_generation
        method is working correctly failures will be very rare (testing at alpha=0.01
        implies we should expect a Type I error about 1% of the time).
ok
test_output_create_dist (tests.TestAccuracy)
Test vose_sampler.ProbDistribution.create_dist to ensure it correctly ... ok
test_output_get_word (tests.TestAccuracy)
Test vose_sampler.get_words to ensure it correctly produces a list of ... ok
test_roundtrip (tests.TestAccuracy) ... ok
test_binary_file1 (tests.TestValidation)
Test vose_sampler.get_words against .epub files ... /Users/hoganb/workplace/Vose-Alias-Method/tests/tests.py:32: DeprecationWarning: Please use assertRaisesRegex instead.
  self.assertRaisesRegexp(IOError, binary_file_error, vose_sampler.get_words, invalid_folder + "Alice.epub")
ok
test_binary_file2 (tests.TestValidation)
Test vose_sampler.get_words against .mobi files ... ok
test_binary_file3 (tests.TestValidation)
Test vose_sampler.get_words against .pdf files ... ok
test_binary_file4 (tests.TestValidation)
Test vose_sampler.get_words against .wav files ... ok
test_empty_file (tests.TestValidation)
Test vose_sampler.get_words against empty files ... ok
test_negative_integer (tests.TestValidation)
Test vose_sampler.VoseAlias.alias_generation against a size ... ok
test_zero_integer (tests.TestValidation)
Test vose_sampler.ProbDistribution.alias_generation against a size ... ok

----------------------------------------------------------------------
Ran 11 tests in 0.436s

OK

Script output: same

Before

$> git co master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$> vose_sampler/vose_sampler.py -p data/Alice.txt -n 10

Generating 10 random samples:

here,'
heads
her.
grin,
went
be
heard
again.
and
the

After

$> git co library
Switched to branch 'library'
Your branch is ahead of 'hoganbc/master' by 2 commits.
  (use "git push" to publish your local commits)
$> vose_sampler/vose_sampler.py -p data/Alice.txt -n 10

Generating 10 random samples:

of
meaning
she
you
enough
just
talk
she
For
_I_

This two changes allow this project to be consumed as a library. The
print statements are moved to `main()`. There is no functional change
for script users.
This will prevent unintended exits when the code is consumed as a
library. The functionality is preserved for script users.

Tests were updated to check for the exception type.

Additional change includes closing the `bin_file` when an Exception is
thrown. There were warnings in the test output about this.
@asmith26 asmith26 merged commit 370fe6e into asmith26:master Nov 2, 2018
@DidgetyTech DidgetyTech deleted the library branch November 5, 2018 15:44
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.

2 participants