small refactor + readme update
This commit is contained in:
18
README.md
18
README.md
@@ -1,13 +1,16 @@
|
||||
# Electronics scripts
|
||||
|
||||
This repository aim to be a collection of useful scripts when working on small electronic projects.
|
||||
This repository aim to be a collection of useful scripts when working on small electronic projects.
|
||||
|
||||
|
||||
for input values, n, u or µ, m, k, M and G suffixes are accepted, respectively for nano, micro, milli, kilo, mega and giga.
|
||||
The inputs are asked through CLI, when multiple values are possible they can be separated with commas or spaces.
|
||||
|
||||
---
|
||||
|
||||
## voltage\_divider.py
|
||||
#### input:
|
||||
(will ask the input through CLI if ran directly)
|
||||
+ a list of resistors values (floats) separated by commas or spaces, accept K and M suffixes for kilo and mega ohms values.
|
||||
+ a list of resistors values
|
||||
*example: 10 50 47 100 5K 1M*
|
||||
+ a desired ratio \(0 ≤ ratio ≤ 1\) for the voltage divider
|
||||
*example: 0.4096*
|
||||
@@ -15,3 +18,12 @@ This repository aim to be a collection of useful scripts when working on small e
|
||||
The 5 best resistor pairs (closest to the desired ratio).
|
||||
|
||||
---
|
||||
|
||||
## parallel\_resistors.py
|
||||
#### input:
|
||||
+ a list of resistors values
|
||||
#### output
|
||||
+ the total resistance with the resistors connected in parallel
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#!/bin/python
|
||||
from utils import parse_resistors
|
||||
from utils import parse_values
|
||||
|
||||
def get_input():
|
||||
raw_input_resistors = input("Enter resistor values separated by spaces or commas: ")
|
||||
resistors = parse_resistors(raw_input_resistors)
|
||||
resistors = parse_values(raw_input_resistors)
|
||||
return resistors
|
||||
|
||||
def calculate_resistance(resistors):
|
||||
|
||||
23
utils.py
23
utils.py
@@ -1,7 +1,7 @@
|
||||
#!/bin/python
|
||||
import re
|
||||
|
||||
def parse_resistors(raw_input):
|
||||
def parse_values(raw_input):
|
||||
values = []
|
||||
tokens = raw_input.replace(',', ' ').split()
|
||||
|
||||
@@ -15,12 +15,21 @@ def parse_resistors(raw_input):
|
||||
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
|
||||
match suffix:
|
||||
case "n":
|
||||
multiplier = 1e-9
|
||||
case "u" | "µ":
|
||||
multiplier = 1e-6
|
||||
case "m":
|
||||
multiplier = 1e-3
|
||||
case "k":
|
||||
multiplier = 1e3
|
||||
case "M":
|
||||
multiplier = 1e6
|
||||
case "G":
|
||||
multiplier = 1e3
|
||||
case _:
|
||||
1.0
|
||||
|
||||
values.append(number * multiplier)
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#!/bin/python
|
||||
from utils import parse_resistors
|
||||
from utils import parse_values
|
||||
|
||||
def find_bests(desired_ratio, resistors):
|
||||
# only keep the 5 best candidates
|
||||
@@ -36,7 +36,7 @@ def get_ratio():
|
||||
|
||||
def get_inputs():
|
||||
raw_input_resistors = input("Enter resistor values separated by spaces or commas: ")
|
||||
resistors = parse_resistors(raw_input_resistors)
|
||||
resistors = parse_values(raw_input_resistors)
|
||||
desired_ratio = get_ratio()
|
||||
|
||||
return (resistors, desired_ratio)
|
||||
|
||||
Reference in New Issue
Block a user