Skip to content

Commit

Permalink
lint and deprecate camelCase names in favor of snake_case names
Browse files Browse the repository at this point in the history
  • Loading branch information
TabulateJarl8 committed Jul 11, 2021
1 parent 5f4aa51 commit 1bcafca
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 27 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![GitHub issues](https://img.shields.io/github/issues/TabulateJarl8/randfacts.svg)](https://GitHub.com/TabulateJarl8/randfacts/issues/)


Randfacts is a python library that generates random facts. You can use `randfacts.getFact()` to return a random fun fact. Disclaimer: Facts are not guaranteed to be true.
Randfacts is a python library that generates random facts. You can use `randfacts.get_fact()` to return a random fun fact. Disclaimer: Facts are not guaranteed to be true.

# Installation

Expand All @@ -28,25 +28,27 @@ $ makepkg -si

```python
import randfacts
x = randfacts.getFact()
x = randfacts.get_fact()
print(x)
```
will print a random fact like:
`Penguins can't taste sweet or savory flavors, only sour and salty ones`

This package has a filter option to filter out potentially inappropriate facts. The filter is on by default. To disable the filter, you can just set the `filter_enabled` parameter to `False`.
```python
from randfacts import getFact
print(getFact(False))
from randfacts import get_fact
print(get_fact(False))
# or
print(get_fact(filter_enabled=False))
```

`getFact` also has a parameter that will make the function only return unsafe facts. This argument takes precedence over the `filter_enabled` argument. For example:
`get_fact` also has a parameter that will make the function only return unsafe facts. This argument takes precedence over the `filter_enabled` argument. For example:

```py
print(getFact(only_unsafe=True))
print(get_fact(only_unsafe=True))
```

If you want to access the list of facts directly, you can just import the `safeFacts`, `unsafeFacts`, or `allFacts` lists from the randfacts module.
If you want to access the list of facts directly, you can just import the `safe_facts`, `unsafe_facts`, or `all_facts` lists from the randfacts module.


## Command line usage
Expand Down
37 changes: 32 additions & 5 deletions randfacts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
Generate a random safe fact.
>>> randfacts.getFact()
>>> randfacts.get_fact()
Generate a random unsafe fact.
>>> randfacts.getFact(only_unsafe=True)
>>> randfacts.get_fact(only_unsafe=True)
Generate a random mixed fact (possibility of both safe and unsafe facts)
>>> randfacts.getFact(False)
>>> randfacts.get_fact(False)
>>> # or
>>> randfacts.getFact(filter_enabled=False)
>>> randfacts.get_fact(filter_enabled=False)
CLI Examples:
randfacts can be executed via the command line with the following commands:
Expand All @@ -42,5 +42,32 @@
"""

import sys, warnings
from .__version__ import __title__, __description__, __url__, __version__, __author__, __author_email__, __license__, __copyright__
from randfacts.randfacts import getFact, safeFacts, unsafeFacts, allFacts
from randfacts.randfacts import get_fact, safe_facts, unsafe_facts, all_facts

# Deprecated names and methods
getFact = get_fact
safeFacts = safe_facts
unsafeFacts = unsafe_facts
allFacts = all_facts

def WrapMod(mod, deprecated):
"""Return a wrapped object that warns about deprecated accesses"""
deprecated = set(deprecated)
class Wrapper(object):
def __getattr__(self, attr):
name_map = [item for item in deprecated if item[0] == attr]
if name_map:
warnings.warn(f'Property {name_map[0][0]} is deprecated and will be removed in a future version, please use {name_map[0][1]}')

return getattr(mod, attr)

def __setattr__(self, attr, value):
name_map = [item for item in deprecated if item[0] == attr]
if name_map:
warnings.warn(f'Property {name_map[0][0]} is deprecated and will be removed in a future version, please use {name_map[0][1]}')
return setattr(mod, attr, value)
return Wrapper()

sys.modules[__name__] = WrapMod(sys.modules[__name__], deprecated=[('allFacts', 'all_facts'), ('safeFacts', 'safe_facts'), ('unsafeFacts', 'unsafe_facts'), ('getFact', 'get_fact')])
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.7.0"
__version__ = "0.7.1"
__author__ = "Tabulate"
__author_email__ = "[email protected]"
__license__ = "MIT"
Expand Down
53 changes: 40 additions & 13 deletions randfacts/randfacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@
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.rstrip('\r\n ') != '']
safe_facts = [
fact.rstrip('\r\n ')
for fact in f.readlines()
if fact.rstrip('\r\n ') != ''
]

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

all_facts = safe_facts + unsafe_facts

allFacts = safeFacts + unsafeFacts

def getFact(filter_enabled: bool=True, only_unsafe: bool=False) -> str:
def get_fact(filter_enabled: bool = True, only_unsafe: bool = False) -> str:
"""This function returns a random fact.
Parameters
Expand All @@ -32,27 +42,44 @@ def getFact(filter_enabled: bool=True, only_unsafe: bool=False) -> str:
"""

if only_unsafe:
return choice(unsafeFacts)
return choice(unsafe_facts)
if filter_enabled is False:
return choice(allFacts)
return choice(safeFacts)
return choice(all_facts)
return choice(safe_facts)


def _cli_entrypoint():
"""Entrypoint for execution via command-line.
"""

parser = argparse.ArgumentParser(description='Generate random facts from the command-line')
parser = argparse.ArgumentParser(
description='Generate random facts from the command-line'
)

group = parser.add_mutually_exclusive_group()
group.add_argument('-m', '--mixed', action='store_true', help='Include safe and unsafe facts')
group.add_argument('-u', '--unsafe', action='store_true', help='Only include unsafe facts')
group.add_argument(
'-m',
'--mixed',
action='store_true',
help='Include safe and unsafe facts'
)

group.add_argument(
'-u',
'--unsafe',
action='store_true',
help='Only include unsafe facts'
)

args = parser.parse_args()

if args.mixed:
print(getFact(False))
print(get_fact(False))
elif args.unsafe:
print(getFact(only_unsafe=True))
print(get_fact(only_unsafe=True))
else:
print(getFact())
print(get_fact())


if __name__ == '__main__':
_cli_entrypoint()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
"Intended Audience :: Developers"
],
python_requires='>=3.6',
include_package_data=True,
include_package_data=True
)

0 comments on commit 1bcafca

Please sign in to comment.