initial commit

This commit is contained in:
Spy 2022-09-20 10:35:36 +01:00
commit 4a4033c393
12 changed files with 252 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -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

View File

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

7
.idea/discord.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="ASK" />
<option name="description" value="" />
</component>
</project>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectTreeColorHighlighter">
<files />
<colors>
<color id="1" value="#4f060d" name="Color 1" enabled="true" />
<color id="2" value="#44220e" name="Color 2" enabled="true" />
<color id="3" value="#3f371b" name="Color 3" enabled="true" />
<color id="4" value="#162c16" name="Color 4" enabled="true" />
<color id="5" value="#0f2f47" name="Color 5" enabled="true" />
<color id="6" value="#171a34" name="Color 6" enabled="true" />
<color id="7" value="#311333" name="Color 7" enabled="true" />
<color id="8" value="#1e1e1e" name="Color 8" enabled="true" />
</colors>
<option name="marksForCollapsedHighlights" value="Dots" />
</component>
</project>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

4
.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/resistors.iml" filepath="$PROJECT_DIR$/.idea/resistors.iml" />
</modules>
</component>
</project>

8
.idea/resistors.iml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

80
bands.json Normal file
View File

@ -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
}
}

6
colours.py Normal file
View File

@ -0,0 +1,6 @@
red = "\033[91m"
green = "\033[92m"
blue = "\033[94m"
magenta = "\033[35m"
cyan = "\033[96m"
end = "\033[0m"

97
main.py Normal file
View File

@ -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")