annotate misc.py @ 78:47da95b31dcb

Move sifmt function to where it can be reused.
author Daniel O'Connor <doconnor@gsoft.com.au>
date Fri, 27 Sep 2024 16:50:42 +0930
parents
children 4cc3d0706dd1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
78
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
1 #!/usr/bin/env python
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
2
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
3 # Copyright (c) 2024
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
4 # Daniel O'Connor <darius@dons.net.au>. All rights reserved.
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
5 #
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
6 # Redistribution and use in source and binary forms, with or without
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
7 # modification, are permitted provided that the following conditions
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
8 # are met:
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
9 # 1. Redistributions of source code must retain the above copyright
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
10 # notice, this list of conditions and the following disclaimer.
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
11 # 2. Redistributions in binary form must reproduce the above copyright
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
12 # notice, this list of conditions and the following disclaimer in the
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
13 # documentation and/or other materials provided with the distribution.
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
14 #
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
15 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
18 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
25 # SUCH DAMAGE.
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
26 #
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
27
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
28 def sifmt(_v, dp = 3, unit = 'Hz', sp = ' '):
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
29 '''Format a number using SI prefixes'''
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
30 si_prefixes = ('T', 'G', 'M', 'k', '', 'm', 'ยต', 'n', 'p')
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
31 scale = 10 ** 12
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
32 v = abs(_v)
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
33 if v == 0:
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
34 sip = ""
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
35 scale = 0
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
36 for i, sip in enumerate(si_prefixes):
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
37 if v >= scale:
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
38 break
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
39 scale /= 1e3
47da95b31dcb Move sifmt function to where it can be reused.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
40 return ('%.' + str(dp) + 'f%s%s%s') % (_v / scale, sp, si_prefixes[i], unit)