-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
48 lines (40 loc) · 1.52 KB
/
__main__.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
import argparse
import unittest
from .define import defined_systems
from .test import PhysicalQuantitiesTest
def print_units(system):
if system.units:
data = [[data['symbol'], data['expansion']]
for data in system.units.values()]
width = max(len(symbol) for symbol, _ in data)
for item in sorted(data):
print((' {:{width}} {}').format(*item, width=width))
def print_constants(system):
if system.constants:
data = [[data['symbol'], data['definition']]
for data in system.constants.values()]
width = max(len(symbol) for symbol, _ in data)
for item in sorted(data):
print((' {:{width}} {}').format(*item, width=width))
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Perform algebraic operations on physical quantities.')
subparsers = parser.add_subparsers(dest='mode')
list_subparser = subparsers.add_parser('list',
help='list units and constants',
description='List all defined units and constants.')
list_subparser.add_argument('system',
nargs='?', choices=defined_systems, default='si',
help='unit system')
test_subparser = subparsers.add_parser('test',
help='run unit tests',
description='Run unit tests.')
args = parser.parse_args()
if args.mode == 'list':
system = defined_systems[args.system]
print('Available units:')
print_units(system)
print('Available constants:')
print_constants(system)
elif args.mode == 'test':
unittest.TextTestRunner().run(unittest.makeSuite(PhysicalQuantitiesTest))