Can python have a suffix based notation for engineering purposes?

As an electrical engineer, I (we?) Use python to help with calculation / automation, etc.

When working with calculations using some real numbers, VERY often have to think in -nano, -pico, -tera , etc.

For example: I know what a 1pF capacitor is, but a 1e-12 F capacitor is also less friendly. In addition, it is gaining 4 times more ( 1p vs 1e-12 ) and more error prone. Not to mention the fact that the presence of a number suffix is ​​simply simpler when displaying numbers.

So the question is: is it possible to work in python (IPython?):

L = 1n C = 1p f = 1/(2*pi*sqrt(L*C)) print(f) gives: 5.033G (or whatever the accuracy should be) 

It would be incredibly useful as well as a calculator!

Thanks.

UPDATE: What I'm looking for is not processing units, but only processing the suffix of numbers . So whether it doesn’t care about a farad or a kilogram, but DO NOT worry about the suffix (-n, -u, -m, -M, -G ...)

+6
python numbers notation
source share
4 answers

Of course. Just write your own parser and create your own AST using Python language services .

+3
source share

You can create a module with all the necessary units like symbols, for example units.py containing something like

 pico = 1*e-12 nano = 1*e-9 micro = 1*e-6 mili = 1*e-3 Farad = 1 pF = pico*Farad nF = nano*Farad 

then in the 50pF code in Farads

 units 50*units.pF 
+3
source share

It makes no sense to introduce complexity in the language for something that can simply be solved with the correct name and functions:

 L_nano = unit_nano(1) C_pico = unit_pico(1) f = 1/(2*pi*sqrt(L*C)) print(to_Giga(f)) gives: 5.033G 
+1
source share

Examples that come with pyparsing include a simple expression parser called fourFn.py . I adapted it to accept your suffix letters with about 8 lines of additional code, find here . This change supports standard suffixes, with the exception of β€œda” for 1e1, since I limited myself to using single characters, I used β€œD” instead.

If you want to do this in an interactive calculator, you can customize the presentation of Stephen Sack using the same changes that I made for fourFn.py.

+1
source share

All Articles