-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorque
executable file
·168 lines (126 loc) · 4.37 KB
/
torque
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
#
# torque: Display torque in various units.
# 2020-04-08: Written by Steven J. DeRose.
#
#pylint: disable=W0703
#
import sys
import argparse
import re
from alogging import ALogger
from pint import UnitRegistry # In PyPI
lg = ALogger()
__metadata__ = {
"title" : "torque",
"description" : "Display torque in various units.",
"rightsHolder" : "Steven J. DeRose",
"creator" : "http://viaf.org/viaf/50334488",
"type" : "http://purl.org/dc/dcmitype/Software",
"language" : "Python 3.7",
"created" : "2020-04-08",
"modified" : "2023-11-23",
"publisher" : "http://github.com/sderose",
"license" : "https://creativecommons.org/licenses/by-sa/3.0/"
}
__version__ = __metadata__['modified']
descr = """
=head1 Description
Given a torque as a number and a unit name, display the equivalent torque
in lots of other units. Give a number, then the units as either length-force or
force-length. For example:
torque 1.2 newton-meter
=head1 Related Commands
*nix ''units''. However, it doesn't seem to handle torque at all.
PyPi 'pint' for basic unit support [https://pypi.org/project/Pint/].
[https://pint.readthedocs.io/en/0.10.1/wrapping.html#checking-dimensionality]
My ''pressure'' does much the same thing for pressure units.
=head1 Known bugs and Limitations
Should probably just learn all the other units; maybe just harvest C<units>'s
F</usr/share/misc/units.lib> list.
=History=
* Written 2020-04-08 by Steven J. DeRose.
=To do=
* Add option to request a particular output unit(s).
* The ''pint'' library supports localized unit names if ''Babel'' is also used,
but it isn't hooked up here.
=Rights=
Copyright 2015, Steven J. DeRose. This work is licensed under a Creative Commons
Attribution-Share Alike 3.0 Unported License. For further information on
this license, see [http://creativecommons.org/licenses/by-sa/3.0/].
For the most recent version, see [http://www.derose.net/steve/utilities] or
[http://github/com/sderose].
=head1 Options
"""
###############################################################################
#
def processOptions():
try:
from BlockFormatter import BlockFormatter
parser = argparse.ArgumentParser(
description=descr, formatter_class=BlockFormatter)
except ImportError:
parser = argparse.ArgumentParser(description=descr)
parser.add_argument(
"--quiet", "-q", action='store_true',
help='Suppress most messages.')
parser.add_argument(
"--verbose", "-v", action='count', default=0,
help='Add more messages (repeatable).')
parser.add_argument(
"--version", action='version', version=__version__,
help='Display version information, then exit.')
parser.add_argument(
'torqueValue', type=float,
help='torque value')
parser.add_argument(
'unit', type=str,
help='Torque unit')
args0 = parser.parse_args()
return(args0)
###############################################################################
# Main
#
args = processOptions()
ureg = UnitRegistry()
print("You have: %16.6f %s" % (args.torqueValue, args.unit))
mat = re.match(r'^(\w+)[-.](\w+)$', args.unit.strip())
if (mat is None):
print("Could not parse unit '%s'." % (args.unit))
sys.exit()
u1 = mat.group(1)
u2 = mat.group(2)
sample = 1.0 * ureg.newton
sample2 = 1.0 * ureg.newton_meter
#print("ureg \n%s" % (lg.formatRec(ureg.__dict__)))
if (not hasattr(ureg, u1)):
print("Could not find unit part 1 '%s'." % (u1))
sys.exit()
if (not hasattr(ureg, u2)):
print("Could not find unit part 2 '%s'." % (u2))
sys.exit()
try:
u1q = ureg.__attr__(u1)
u2q = ureg.__attr__(u2)
if (u1q.check('[length]') and u2q.check('[force]')):
print("length, force")
ar = u1q; fo = u2q
elif (u1q.check('[force]') and u2q.check('[length]')):
print("force, length")
ar = u2q; fo = u1q
else:
print("Units '%s' and '%s' do not seem to be length and force." % (u1, u2))
sys.exit()
except Exception as e:
print("%s" % (e))
#args.p / units[args.unit]
toTry = [
('newton', 'meter'),
('kg', 'meter'),
('foot', 'pound'),
('inch', 'pound'),
# Joule per radian
]
#for u in unitNames:
# print(" = %16.6f %s" % (kPa*units[u], u))
sys.exit()