diff --git a/converters/__init__.py b/converters/__init__.py index d8ec7ca..45c4aef 100644 --- a/converters/__init__.py +++ b/converters/__init__.py @@ -55,7 +55,7 @@ def can_convert(from_unit, to_unit): false otherwise """ # There must be an implementation to convert the units - from_unit_category = _find_unit_category(from_unit) + from_unit_category = _find_unit_category(from_unit) to_unit_category = _find_unit_category(to_unit) if from_unit_category is None or to_unit_category is None: diff --git a/converters/base_conversion.py b/converters/base_conversion.py new file mode 100644 index 0000000..25d9cbf --- /dev/null +++ b/converters/base_conversion.py @@ -0,0 +1,61 @@ +base_conversion = { + "name": "base_conversion", + "units": [ + { + "name": "b2", + "_internal_accepted_names": ["b2"], + "si": "b2", + "_internal_conversion": { + "b3": lambda n: n * 10 ** 5, # n to dyn + "b4": lambda n: n * 7.23301, # n to pdl + "b5": lambda n: n * 0.10197, # n to kp + }, + }, + { + "name": "b3", + "_internal_accepted_names": ["b3"], + "si": "b3", + "_internal_conversion": { + "b2": lambda n: n * 10 ** 5, # n to dyn + "b4": lambda n: n * 7.23301, # n to pdl + "b5": lambda n: n * 0.10197, # n to kp + }, + }, + { + "name": "b4", + "_internal_accepted_names": ["b4"], + "si": "b4", + }, + { + "name": "b5", + "_internal_accepted_names": ["b5"], + "si": "b5", + }, + { + "name": "b6", + "_internal_accepted_names": ["b6"], + "si": "b6", + }, + { + "name": "b7", + "_internal_accepted_names": ["b7"], + "si": "b7", + }, + { + "name": "b8", + "_internal_accepted_names": ["b8"], + "si": "b8", + }, + { + "name": "b9", + "_internal_accepted_names": ["b9"], + "si": "b9", + }, + { + "name": "b10", + "_internal_accepted_names": ["b10"], + "si": "b10", + }, + + ] +} \ No newline at end of file diff --git a/unit_converter.py b/unit_converter.py index 96779db..f078d0a 100644 --- a/unit_converter.py +++ b/unit_converter.py @@ -2,7 +2,7 @@ import unit_dictionary from converters import convert, can_convert, get_si, get_suggestions_for_category, get_suggestions_to_given_unit from converters.exceptions import ConversionError, RequireAdditionalParamError - +import re class State(): NO_CATEGORY_ENTERED, CATEGORY_HELP_ENTERED, FIRST_UNIT_ENTERED,\ @@ -118,8 +118,32 @@ def check_metric_imperial(value, units_from, units_to, decimal_places): return False + +def base_conversion(value, from_unit, to_unit): + accepted_entry = ['b2','b3','b4','b5','b6','b7','b8','b9','b10'] + + # connvert input to decimal + if from_unit in accepted_entry and to_unit in accepted_entry: + from_unit = int(from_unit[1]) + to_unit = int(to_unit[1]) + + decimal = 0 + for dig in range(len(value)): + decimal += len(value[dig]) * from_unit ** (len(value) - 1 - dig) * int(value[dig]) + + # convert decimal to target base + output = '' + + while decimal > 0 : + remainder = decimal % to_unit + output += str(remainder) + decimal = decimal // to_unit + + + return int(output[::-1]) def convert_units(value, from_unit, to_unit, **args): + """ Takes the given value, source unit and target unit to convert. :param value: Value to convert @@ -128,7 +152,6 @@ def convert_units(value, from_unit, to_unit, **args): :param args: Additional arguments important for conversion with multiple dependencies :return: """ - # Check if units can be converted if not can_convert(from_unit, to_unit): return '[!] Units cannot be converted\n' @@ -141,8 +164,9 @@ def convert_units(value, from_unit, to_unit, **args): # Return the value if units are the same if from_unit == to_unit: return str(value) + " " + get_si(to_unit) - + responses = [ + base_conversion(value, from_unit, to_unit), check_time(value, from_unit, to_unit, decimal_places), # Time units check_metric_imperial( value, @@ -281,7 +305,10 @@ def input_dialog(): \nUnits supported:\n \ Circle\t\t: radians (rad), degrees (deg) \n \ Temperature\t: Celsius (c), Fahrenheit (f), and Kelvin (k) \n \ - Speed\t\t: Kilometers/hour (kph), miles/hour (mph), knots(kt) .') + Speed\t\t: Kilometers/hour (kph), miles/hour (mph), knots(kt) .\n \ + Base conversion: b2 , b3 , b4 , b5 , b6') + + print('\n\n[-] Example entries: 1.345/rad/degrees, 33/f/c, 2/mph/kph') print('\n\n[-] To close type "exit"') diff --git a/units/__init__.py b/units/__init__.py index 52fd8ae..ff86e0f 100644 --- a/units/__init__.py +++ b/units/__init__.py @@ -6,6 +6,7 @@ from converters.pressure import pressure from converters.speed import speed from converters.temperature import temperature +from converters.base_conversion import base_conversion class UnitsManager(object): @@ -21,6 +22,7 @@ class UnitsManager(object): pressure, speed, temperature, + base_conversion, ] def __iter__(self):