utils + parallel resistors calculator
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#!/bin/python
|
||||
import re
|
||||
from utils import parse_resistors
|
||||
|
||||
def find_bests(desired_ratio, resistors):
|
||||
# only keep the 5 best candidates
|
||||
@@ -22,31 +22,6 @@ def find_bests(desired_ratio, resistors):
|
||||
for i, (error, R1, R2, ratio) in enumerate(candidates, start=1):
|
||||
print(f"{i}. R1 = {R1} Ω, R2 = {R2} Ω -> ratio = {ratio:.6f}, error = {error:.6e}")
|
||||
|
||||
def parse_resistors(raw_input):
|
||||
values = []
|
||||
tokens = raw_input.replace(',', ' ').split()
|
||||
|
||||
for token in tokens:
|
||||
match = re.fullmatch(r'([0-9]*\.?[0-9]+)\s*([KM]?)', token)
|
||||
if not match:
|
||||
raise ValueError(f"Invalid component value '{token}'")
|
||||
|
||||
number = float(match.group(1))
|
||||
if number <= 0:
|
||||
raise ValueError(f"Invalid component value '{token}' (must be positive)")
|
||||
|
||||
suffix = match.group(2)
|
||||
if suffix == 'K':
|
||||
multiplier = 1e3
|
||||
elif suffix == 'M':
|
||||
multiplier = 1e6
|
||||
else:
|
||||
multiplier = 1.0
|
||||
|
||||
values.append(number * multiplier)
|
||||
|
||||
return values
|
||||
|
||||
def get_ratio():
|
||||
while True:
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user