From 4a4033c39323533db7e07c436a6fc8b165e7e300 Mon Sep 17 00:00:00 2001 From: Spy <32415621+SpyHoodle@users.noreply.github.com> Date: Tue, 20 Sep 2022 10:35:36 +0100 Subject: [PATCH] initial commit --- .idea/.gitignore | 8 ++ .idea/codeStyles/codeStyleConfig.xml | 5 + .idea/discord.xml | 7 ++ .idea/highlightedFiles.xml | 17 ++++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/misc.xml | 4 + .idea/modules.xml | 8 ++ .idea/resistors.iml | 8 ++ .idea/vcs.xml | 6 ++ bands.json | 80 +++++++++++++++ colours.py | 6 ++ main.py | 97 +++++++++++++++++++ 12 files changed, 252 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/discord.xml create mode 100644 .idea/highlightedFiles.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/resistors.iml create mode 100644 .idea/vcs.xml create mode 100644 bands.json create mode 100644 colours.py create mode 100644 main.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..30bab2a --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/highlightedFiles.xml b/.idea/highlightedFiles.xml new file mode 100644 index 0000000..f9d3c5e --- /dev/null +++ b/.idea/highlightedFiles.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..dc9ea49 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..da6b32d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/resistors.iml b/.idea/resistors.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/resistors.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/bands.json b/bands.json new file mode 100644 index 0000000..995aa1a --- /dev/null +++ b/bands.json @@ -0,0 +1,80 @@ +{ + "black": + { + "tolerance": 0, + "band": 0, + "multiplier": 1 + }, + "brown": + { + "tolerance": 1, + "band": 1, + "multiplier": 10 + }, + "red": + { + "tolerance": 2, + "band": 2, + "multiplier": 100 + }, + "orange": + { + "tolerance": 3, + "band": 3, + "multiplier": 1000 + }, + "yellow": + { + "tolerance": 4, + "band": 4, + "multiplier": 10000 + }, + "green": + { + "tolerance": 0.5, + "band": 5, + "multiplier": 100000 + }, + "blue": + { + "tolerance": 0.25, + "band": 6, + "multiplier": 1000000 + }, + "violet": + { + "tolerance": 0.1, + "band": 7, + "multiplier": 10000000 + }, + "grey": + { + "tolerance": 0.05, + "band": 8, + "multiplier": 100000000 + }, + "white": + { + "tolerance": 0, + "band": 9, + "multiplier": 1000000000 + }, + "gold": + { + "tolerance": 5, + "band": "", + "multiplier": 0.1 + }, + "silver": + { + "tolerance": 10, + "band": "", + "multiplier": 0.01 + }, + "empty": + { + "tolerance": 20, + "band": "", + "multiplier": 0 + } +} \ No newline at end of file diff --git a/colours.py b/colours.py new file mode 100644 index 0000000..14fa856 --- /dev/null +++ b/colours.py @@ -0,0 +1,6 @@ +red = "\033[91m" +green = "\033[92m" +blue = "\033[94m" +magenta = "\033[35m" +cyan = "\033[96m" +end = "\033[0m" diff --git a/main.py b/main.py new file mode 100644 index 0000000..615d0d5 --- /dev/null +++ b/main.py @@ -0,0 +1,97 @@ +import json +import colours as c + + +def load_json(file_name): + # Open a json file called file_name + with open(file_name) as file: + # Load the json file into a dictionary + return json.load(file) + + +def convert(value): + if 1000 <= value < 1000000: + # Kilo + value = str(value / 1000) + " K" + + elif 1000000 <= value < 1000000000: + # Mega + value = str(value / 1000000) + " M" + + elif value >= 1000000000: + # Giga + value = str(value / 1000000000) + " G" + + return value + + +def input_colours(bands): + colours = [] + while len(colours) < 5: + # Calculate a suffix for the number i.e. 2nd or 3rd + suffixes = ["", "st", "nd", "rd", "th", "th"] + suffix = suffixes[len(colours) + 1] + + # Ask the user to input the colour of the resistor band + colour = input(f"{c.cyan}What is the colour of the " + f"{c.blue}{len(colours) + 1}{suffix}{c.end} " + f"{c.cyan}band? {c.end}").lower() + + if colour in bands: + # Only add the colour if it is valid + colours.append(colour) + + else: + # Otherwise, print an error message + print(f"{c.red}Error: This colour does not exist.{c.end}") + + return colours + + +def calc_resistor(colours, bands): + # Set default values + bands_value = "" + multiplier = 1 + tolerance = 0 + + for i, colour in enumerate(colours): + # Get the band + if i < 3: + bands_value += str(bands[colour]["band"]) + + elif i == 3: + multiplier = bands[colour]["multiplier"] + + elif i == 4: + tolerance = bands[colour]["tolerance"] + + resistor_value = int(bands_value) * multiplier + return resistor_value, tolerance + + +def main(file_name): + # Print the welcome message + print(f"{c.magenta}Resistor Calculator v0.1.0{c.end}") + + # Load the band information from the json file + bands = load_json(file_name) + + # Get the colours of the resistor's bands from the user + colours = input_colours(bands) + + # Calculate the value and tolerance of the resistor + resistor_value, tolerance = calc_resistor(colours, bands) + + # Convert the value to a readable format + converted_value = convert(resistor_value) + + # Collect and create the final value + final_value = f"{converted_value}Ω +/-{tolerance}%" + + # Print the original colours entered and final value + print(f"\n{c.blue}You have entered: {c.cyan}{', '.join(colours)}{c.end}" + f"\n{c.blue}Your resistor is: {c.cyan}{final_value}{c.end}") + + +if __name__ == "__main__": + main("bands.json")