[Python-ideas] SI scale factors in Python

Chris Angelico rosuav at gmail.com
Thu Aug 25 18:43:21 EDT 2016


On Fri, Aug 26, 2016 at 6:06 AM, Ken Kundert
<python-ideas at shalmirane.com> wrote:
> Here is some simulation code that uses SI scale factors
>
>     for delta in [-500nA, 0, 500nA]:
>         input = 2.75uA + delta
>         wait(1us)
>         expected = 100kOhm*(2.75uA + delta)
>         tolerance = 2.2mV
>         fails = check_output(expected, tolerance)
>         print('%s: I(in)=%rA, measured V(out)=%rV, expected V(out)=%rV, diff=%rV.' % (
>             'FAIL' if fails else 'pass',
>             input, get_output(), expected, get_output() - expected
>         ))
>
> And the same code in Python today ...
>
>     for delta in [-5e-7, 0, 5e-7]:
>         input = 2.75e-6 + delta
>         wait(1e-6)
>         expected = 1e5*(2.75e-6 + delta)
>         tolerance = 2.2e-3
>         fails = check_output(expected, tolerance)
>         print('%s: I(in)=%eA, measured V(out)=%eV, expected V(out)=%eV, diff=%eV.' % (
>             'FAIL' if fails else 'pass',
>             input, get_output(), expected, get_output() - expected
>         ))

Rename a few things:

    for deltaA in [-5e-7, 0, 5e-7]:
        inputA = 2.75e-6 + deltaA
        wait(1e-6)
        expectedA = 1e5*(2.75e-6 + deltaA)
        toleranceV = 2.2e-3
        fails = check_output(expectedA, toleranceV)
        print('%s: I(in)=%eA, measured V(out)=%eV, expected
V(out)=%eV, diff=%eV.' % (
            'FAIL' if fails else 'pass',
            inputA, get_output(), expectedA, get_output() - expectedA
        ))

I may have something wrong here (for instance, I'm not sure if
check_output should be taking an expected amperage and a tolerance in
volts), but you get the idea: it's the variables, not the literals,
that get tagged.

Another way to do this would be to use MyPy and type hinting to create
several subtypes of floating-point number:

class V(float): pass
class A(float): pass

    for delta in [A(-5e-7), A(0), A(5e-7)]:
        input = A(2.75e-6) + delta
        # etc

or in some other way have static analysis tag the variables, based on
their origins.

> There are two things to notice. In the first example the numbers are easier to
> read: for example, 500nA is easier to read the 5e-7.

This is useful, but doesn't depend on the "A" at the end.

> Second, there is
> information in the units that provides provides useful information. One can
> easily see that the input signal is a current and that the output is a voltage.
> Furthermore, anybody can look at this code and do a simple sanity check on the
> expressions even if they don't understand the system being simulated. They can
> check the units on the input and output expressions to assure that they are all
> consistent.

These two are better served by marking the variables rather than the
literals; in fact, if properly done, the tagging can be verified by a
program, not just a human. (That's what tools like MyPy are aiming to
do.)

ChrisA


More information about the Python-ideas mailing list