-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperiodic.py
More file actions
executable file
·109 lines (83 loc) · 2.98 KB
/
periodic.py
File metadata and controls
executable file
·109 lines (83 loc) · 2.98 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
import argparse
from periodic.table import PeriodicTable, PeriodicTableError
from periodic.layouts import layouts
def main():
"""Periodic table word confabulator"""
table_columns = 74
grid_columns = 4
parser = argparse.ArgumentParser(description=main.__doc__)
group = parser.add_mutually_exclusive_group()
parser.add_argument(
"-i", "--info", type=str,
help="show more info about a particular element", metavar=("ELEMENT")
)
parser.add_argument("-w", "--word", type=str, help="a word to render")
group.add_argument(
"-v", "--variations", action="store_true",
help="display all variations, rather than just the best match."
)
group.add_argument("-p", "--phrase", type=str, help="a phrase to render")
parser.add_argument(
"-g", "--grid", action="store_true",
help="show the grid"
)
parser.add_argument(
"-t", "--table", action="store_true",
help="display the entire periodic table"
)
parser.add_argument(
"-l", "--layout", type=str,
help="specify the table layout to render",
choices=[layout for layout in layouts],
default="standard"
)
parser.add_argument(
"-c", "--color", action="store_true",
help="display each element using its color"
)
parser.add_argument(
"--width", type=int, default=80,
help="number of character columns to display on one line"
)
args = parser.parse_args()
if args.grid and args.table and args.width < table_columns + grid_columns:
print(f"Terminal width must be > {table_columns + grid_columns}.")
exit(1)
if args.table and args.width < table_columns:
print(f"Terminal width must be > {table_columns}.")
exit(1)
periodic = PeriodicTable(**vars(args))
if args.info:
symbol = args.info.lower().capitalize()
if symbol.isnumeric():
symbol = periodic.get_symbol_from_atomic_number(symbol)
try:
periodic.render_info(symbol)
except PeriodicTableError as exception:
print(exception)
exit(1)
if args.word:
solutions = periodic.get_solutions(args.word)
if not solutions:
print(f"No solutions found for '{args.word}'.")
exit(1)
if not args.variations:
solutions = solutions[:1]
for solution in solutions:
periodic.render_symbols(solution)
if args.phrase:
symbols = []
for word in args.phrase.split():
solutions = periodic.get_solutions(word)
if not solutions:
print(f"No solution found for '{word}'.")
exit(1)
if len(symbols):
symbols.append(" ")
symbols += solutions[0]
periodic.render_symbols(symbols)
if args.table:
periodic.render_table(show_grid=args.grid, layout=args.layout)
if __name__ == "__main__":
main()