small refactor + readme update
This commit is contained in:
16
README.md
16
README.md
@@ -2,12 +2,15 @@
|
|||||||
|
|
||||||
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
|
## voltage\_divider.py
|
||||||
#### input:
|
#### input:
|
||||||
(will ask the input through CLI if ran directly)
|
+ a list of resistors values
|
||||||
+ a list of resistors values (floats) separated by commas or spaces, accept K and M suffixes for kilo and mega ohms values.
|
|
||||||
*example: 10 50 47 100 5K 1M*
|
*example: 10 50 47 100 5K 1M*
|
||||||
+ a desired ratio \(0 ≤ ratio ≤ 1\) for the voltage divider
|
+ a desired ratio \(0 ≤ ratio ≤ 1\) for the voltage divider
|
||||||
*example: 0.4096*
|
*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).
|
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
|
#!/bin/python
|
||||||
from utils import parse_resistors
|
from utils import parse_values
|
||||||
|
|
||||||
def get_input():
|
def get_input():
|
||||||
raw_input_resistors = input("Enter resistor values separated by spaces or commas: ")
|
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
|
return resistors
|
||||||
|
|
||||||
def calculate_resistance(resistors):
|
def calculate_resistance(resistors):
|
||||||
|
|||||||
19
utils.py
19
utils.py
@@ -1,7 +1,7 @@
|
|||||||
#!/bin/python
|
#!/bin/python
|
||||||
import re
|
import re
|
||||||
|
|
||||||
def parse_resistors(raw_input):
|
def parse_values(raw_input):
|
||||||
values = []
|
values = []
|
||||||
tokens = raw_input.replace(',', ' ').split()
|
tokens = raw_input.replace(',', ' ').split()
|
||||||
|
|
||||||
@@ -15,12 +15,21 @@ def parse_resistors(raw_input):
|
|||||||
raise ValueError(f"Invalid component value '{token}' (must be positive)")
|
raise ValueError(f"Invalid component value '{token}' (must be positive)")
|
||||||
|
|
||||||
suffix = match.group(2)
|
suffix = match.group(2)
|
||||||
if suffix == 'K':
|
match suffix:
|
||||||
|
case "n":
|
||||||
|
multiplier = 1e-9
|
||||||
|
case "u" | "µ":
|
||||||
|
multiplier = 1e-6
|
||||||
|
case "m":
|
||||||
|
multiplier = 1e-3
|
||||||
|
case "k":
|
||||||
multiplier = 1e3
|
multiplier = 1e3
|
||||||
elif suffix == 'M':
|
case "M":
|
||||||
multiplier = 1e6
|
multiplier = 1e6
|
||||||
else:
|
case "G":
|
||||||
multiplier = 1.0
|
multiplier = 1e3
|
||||||
|
case _:
|
||||||
|
1.0
|
||||||
|
|
||||||
values.append(number * multiplier)
|
values.append(number * multiplier)
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#!/bin/python
|
#!/bin/python
|
||||||
from utils import parse_resistors
|
from utils import parse_values
|
||||||
|
|
||||||
def find_bests(desired_ratio, resistors):
|
def find_bests(desired_ratio, resistors):
|
||||||
# only keep the 5 best candidates
|
# only keep the 5 best candidates
|
||||||
@@ -36,7 +36,7 @@ def get_ratio():
|
|||||||
|
|
||||||
def get_inputs():
|
def get_inputs():
|
||||||
raw_input_resistors = input("Enter resistor values separated by spaces or commas: ")
|
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()
|
desired_ratio = get_ratio()
|
||||||
|
|
||||||
return (resistors, desired_ratio)
|
return (resistors, desired_ratio)
|
||||||
|
|||||||
Reference in New Issue
Block a user