-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathutil.py
executable file
·59 lines (47 loc) · 1.77 KB
/
util.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
51
52
53
54
55
56
57
58
59
# This file is part of pi-stomp.
#
# pi-stomp is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pi-stomp is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pi-stomp. If not, see <https://www.gnu.org/licenses/>.
def LILV_FOREACH(collection, func):
itr = collection.begin()
while itr:
yield func(collection.get(itr))
itr.next()
if itr.is_end():
break
def DICT_GET(dict, key):
if key in dict:
return dict[key]
else:
return None
def renormalize(n, left_min, left_max, right_min, right_max):
# this remaps a value from original (left) range to new (right) range
# Figure out how 'wide' each range is
delta1 = left_max - left_min
delta2 = right_max - right_min
return round((delta2 * (n - left_min) / delta1) + right_min)
def renormalize_float(value, left_min, left_max, right_min, right_max):
# this remaps a value from original (left) range to new (right) range
# Figure out how 'wide' each range is
left_span = abs(left_max - left_min)
num_divisions = left_span / value
right_span = abs(right_max - right_min)
return round(right_span / num_divisions, 2)
def format_float(value):
if value < 10:
if value < 1:
return "%.2f" % value
else:
return "%.1f" % value
else:
return "%d" % value