I love python. Here is a simple script to calculate parallel resistance (or series capacitance):
#!/usr/bin/env python
import sys
divisor = 0
for component in sys.argv[1:]:
divisor += 1 / float(component)
print 1 / divisor
Save this as something like parallel_resistance.py, then make it executable:
$ chmod 755 parallel_resistance.py
You can symlink to multiple easier to type (or easier to remember) program names like:
$ ln -s parallel_resistance.py par-res
$ ln -s parallel_resistance.py ser-cap
Then, run it with any number of like values of parallel resistance or series capacitance. Let’s say you have a 20pf and a couple of 30pf capacitors in series:
$ ./ser-cap 20 30 30
8.57142857143
This little script demonsrtates the ease with which you can step through any number of command line parameters using python’s list processing and ‘for’ statement features. You need to convert the input to type float() or it will be assumed to be a string. Once the input is type float, math will always produce floating point results. If you had an integer you wanted to do floating point math on, you could also force it to float with something like:
divisor += 1.0 / integer_value
The ’1.0′ tells python you want a floating point result, not an integer. Cool.
Obviously if you wanted something more than ad hoc capability, you could check the input, generate error messages, add features with command line switches, etc. But simple list processing of known command line values, and python’s default error handling is often more than adequate for tiny scripts like this… For example:
$ ./ser-cap 10 foo bar
Traceback (most recent call last):
File "./ser-cap", line 8, in
Seems pretty easy to understand what went wrong.
