Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
61 changes: 61 additions & 0 deletions converters/base_conversion.py
Original file line number Diff line number Diff line change
@@ -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",
},

]
}
35 changes: 31 additions & 4 deletions unit_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,\
Expand Down Expand Up @@ -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
Expand All @@ -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'
Expand All @@ -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,
Expand Down Expand Up @@ -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"')

Expand Down
2 changes: 2 additions & 0 deletions units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -21,6 +22,7 @@ class UnitsManager(object):
pressure,
speed,
temperature,
base_conversion,
]

def __iter__(self):
Expand Down