-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
179 lines (164 loc) · 3.62 KB
/
server.py
File metadata and controls
179 lines (164 loc) · 3.62 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
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
169
170
171
172
173
174
175
176
177
178
179
from concurrent import futures
import time
import grpc
import unit_pb2
import unit_pb2_grpc
import sys
import optparse
ABBREVIATION_PREFIX = {
"yocto": "y",
"zepto": "z",
"atto": "a",
"femto": "f",
"pico": "p",
"nano": "n",
"micro": "u",
"milli": "m",
"centi": "c",
"deci": "d",
"deca": "da",
"hecto": "h",
"kilo": "k",
"mega": "M",
"giga": "G",
"tera": "T",
"peta": "P",
"exa": "E",
"zetta": "Z",
"yotta": "Y"
}
ABBREVIATION = {
# length
"metre": "m",
"meter": "m",
"inch": "in",
"feet": "ft",
"foot": "ft",
"yard": "yd",
"mile": "mi",
# mass
"gramme": "g",
"gram": "g",
"gm": "g",
"quintal": "q",
"tonne": "t",
"ton": "t",
"carat": "ct",
"ounce": "oz",
"pound": "lb",
# time
"second": "s",
"minute": "min",
"hour": "hr",
"day": "dy",
"week" : "wk",
"month": "mo",
"year" :"yr",
}
LENGTH = {
"fm": 1e-15,
"pm": 1e-12,
"nm": 1e-9,
"um": 1e-6,
"mm": 1e-3,
"cm": 1e-2,
"dm": 1e-1,
"m": 1,
"dam": 1e+1,
"hm": 1e+2,
"km": 1e+3,
"in": 0.0254,
"ft": 0.31622776602,
"yd": 0.9144,
"mi": 1609.344
}
MASS = {
"fg": 1e-15,
"pg": 1e-12,
"ng": 1e-9,
"ug": 1e-6,
"mg": 1e-3,
"g": 1,
"kg": 1e+3,
"q": 1e+5,
"t": 1e+6,
"ct": 0.2,
"oz": 28,
"lb": 453.59237
}
TIME = {
"fs": 1e-15,
"ps": 1e-12,
"ns": 1e-9,
"us": 1e-6,
"ms": 1e-3,
"s": 1,
"min": 60,
"hr": 3.6e+3,
"dy": 86.4e+3,
"wk": 604.8e+3,
"mo": 2.6297e+6,
"yr": 31.556925e+6,
}
def abbreviation_prefix(name):
for full,abbr in ABBREVIATION_PREFIX.items():
i = name.find(full)
i = i if i>=0 else name.lower().find(full)
if i>=0:
return abbr,name[:i]+name[i+len(full):]
return "",name
def abbreviation(name):
prefix,name = abbreviation_prefix(name)
if name in ABBREVIATION:
return prefix+ABBREVIATION[name]
name = name.lower()
if name in ABBREVIATION:
return prefix+ABBREVIATION[name]
name = name[:-2] if name[-2:]=="is" else name
name = name[:-1] if name[-1:]=="s" else name
if name in ABBREVIATION:
return prefix+ABBREVIATION[name]
return prefix+name
def unit_map(name):
if name in LENGTH:
return LENGTH
if name in MASS:
return MASS
if name in TIME:
return TIME
return None
def unit_convert(value, source, target):
source_abbr = abbreviation(source)
target_abbr = abbreviation(target)
source_map = unit_map(source_abbr)
target_map = unit_map(target_abbr)
if source_map is None:
return "Unknown unit "+source+"!",0
if target_map is None:
return "Unknown unit "+target+"!",0
if source_map != target_map:
return source+" and "+target+" are different physical quantities!",0
return "",value*source_map[source_abbr]/source_map[target_abbr]
class Unit(unit_pb2_grpc.UnitServicer):
def Convert(self, request, context):
value,source,target = (request.value,request.source,request.target)
print("\nvalue: "+str(value)+", source: "+source+", target: "+target)
error,value = unit_convert(value, source, target)
print("error: "+error+", value: "+str(value))
return unit_pb2.ConvertReply(error=error, value=value)
parser = optparse.OptionParser()
parser.set_defaults(address="[::]:50051")
parser.add_option("--address", dest="address", help="set server address")
parser.usage = "python server.py [--address <address>]"
(options, args) = parser.parse_args()
address = options.address
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
unit_pb2_grpc.add_UnitServicer_to_server(Unit(), server)
server.add_insecure_port(address)
server.start()
print("Server started on "+address)
try:
while True:
time.sleep(1000)
except KeyboardInterrupt:
server.stop(0)