-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscented-wax-selector.py
executable file
·50 lines (42 loc) · 1.43 KB
/
scented-wax-selector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
import argparse
from csv import DictReader
from random import choice
_SCRIPT_DESCRIPTION = """
Given csv file containing the names of scented waxes in the format of
{name,tags}, script will randomly choose an item from the list. Can pass
specific tags to the script to limit the options to pick from.
"""
def filter_to_requested_tags(data, requested_tags):
def is_tag_requested(data_item):
# set intersection
matches = (requested_tags & set(data_item["tags"]))
return len(matches) > 0
return data if "*" in requested_tags else filter(is_tag_requested, data)
def main():
parser = argparse.ArgumentParser(description=_SCRIPT_DESCRIPTION)
parser.set_defaults(allow_abbrev=False)
# positionals
parser.add_argument(
"tags",
default=["*"],
metavar="TAG",
nargs="*",
help="the tag(s) of items to pick from.")
# flags
parser.add_argument(
"--data",
dest="file",
required=True,
type=argparse.FileType("r"),
help="path to the csv file containing items to pick from.")
args = parser.parse_args()
data = [r for r in DictReader(args.file)]
for row in data:
row["tags"] = row["tags"].split(";")
data = list(filter_to_requested_tags(data, set(args.tags)))
pick = dict(choice(data))
print(f"Name: {pick['name']}")
print(f"Tags: {pick['tags']}")
if __name__ == "__main__":
main()