Skip to content

Commit

Permalink
performance improvement and allow user to access all facts
Browse files Browse the repository at this point in the history
  • Loading branch information
TabulateJarl8 committed Mar 5, 2021
1 parent bf0de90 commit 1b25543
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ This package has a filter option to filter out potentially inappropriate facts.
```python
from randfacts import getFact
print(getFact(False))
```
```

If you want to access the list of facts directly, you can just import the `safeFacts`, `unsafeFacts`, or `allFacts` lists from the randfacts module.
2 changes: 1 addition & 1 deletion randfacts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .__version__ import __title__, __description__, __url__, __version__, __author__, __author_email__, __license__, __copyright__
from randfacts.main import getFact
from randfacts.main import getFact, safeFacts, unsafeFacts, allFacts
Binary file added randfacts/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file added randfacts/__pycache__/__version__.cpython-39.pyc
Binary file not shown.
Binary file added randfacts/__pycache__/main.cpython-39.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion randfacts/__version__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__title__ = "randfacts"
__description__ = "Package to generate random facts"
__url__ = "https://github.com/TabulateJarl8/randfacts"
__version__ = "0.2.11"
__version__ = "0.3.0"
__author__ = "Tabulate"
__author_email__ = "[email protected]"
__license__ = "MIT"
Expand Down
20 changes: 12 additions & 8 deletions randfacts/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from random import randint
from random import choice
import os

dir_path = os.path.dirname(os.path.realpath(__file__))

with open(os.path.join(dir_path, "safe.txt")) as f:
safeFacts = [fact.rstrip('\r\n ') for fact in f.readlines() if fact != '']
with open(os.path.join(dir_path, "unsafe.txt")) as f:
unsafeFacts = [fact.rstrip('\r\n ') for fact in f.readlines() if fact != '']

allFacts = safeFacts + unsafeFacts

def getFact(filter=True):
dir_path = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(dir_path, "safe.txt")) as f:
safelist = [fact.rstrip('\r\n ') for fact in f.readlines() if fact != '']
with open(os.path.join(dir_path, "unsafe.txt")) as f:
unsafelist = [fact.rstrip('\r\n ') for fact in f.readlines() if fact != '']
if filter == False:
safelist += unsafelist
return safelist[randint(0, len(safelist) - 1)]
return choice(allFacts)
return choice(safeFacts)

0 comments on commit 1b25543

Please sign in to comment.