Giter VIP home page Giter VIP logo

unitconverter's Introduction

A unit conversion API module in Python.

Requirements

  • Python2 & above

Supported Units and Categories

Category Units
Angles of Circles radians (rad), degrees (deg)
Temperature Celsius (C), Fahrenheit (F), Kelvins (K)
Distance Metric distances: attometer (am), femtometer (fm), picometer (pm), nanometer (nm), micrometer (μm), milimeter (mm), centimeter (cm), decimeter (dm), meter (m), decameter (dam), hectometer (hm), kilometer (km), megameter (Mm), gigameter (Gm), terameter (Tm), petameter (Pm), exameter (Em) Imperial distances: thou (th), inch (in), link (li), foot (ft), yard (yd), rod (rod), chain (ch), furlong (fur), mile (mi), league (lea)
Volume Metric volumes: attoliter (al) to exaliter (El) Imperial volumes: fluid ounce (floz), gill (gi), pint (pt), quart (qt), gallon (gal)
Mass and Weight Metric mass: attoggram (ag) to Exagram (Eg) Imperial mass: grain (gr), drachm (dr), ounce (oz), pound (lb), stone (st), quarter (qtr or qr), hundredweight (cwt), ton (t)
Time yocotosecond (ys), zeptosecond (zs), attosecond (as), femtosecond (fs), picosecond (ps), nanosecond (ns), microsecond (µs), milisecond (ms), cs, ds, second (s), minute (min), kilosecond (ks), hour (hr), day (day), week (wk), megasecond (Ms), month (mo), year (yr), gigasecond (Gs), terasecond (Ts), petasecond (Ps), exasecond (Es), Zs, Ys
Pressure pascal (pa), torr (torr), pounds per square inch (psi), bar (bar), technical atmosphere (at), atmosphere (atm)
Energy watt (w), joule (j), horsepower (hp), Calorie (cal), British thermal unit (btu)
Speed kilometers per hour (kph), miles per hour (mph), knots (kt)
Force Newton (N), dyne (dyn), Kilogram-Force (kp), Poundal (pdl)
Digital Storage bit (bit), byte (byte), kilobyte (kB), megabyte (MB), gigabyte (GB), terabyte (TB), petabyte (PB), exabyte (EB), zettabyte (ZB), yottabyte (YB)

Examples

UnitConverter $ python unit_converter.py
Python unit converter by mattgd.
     Units supported:
     Circle: radians (rad), degrees (deg)
     Temperature: Celsius (c), Fahrenheit (f), and Kelvin (k)
     Speed: Kilometers/hour (kph), miles/hour (mph), knots(kt) .
Example entries: 1.345/rad/deg, 33/f/c, 2/mph/kph
Enter a unit to convert from: F
Enter a unit to convert to: C
Enter a value to convert from F to C:
37.8 °C
Enter a unit to convert from: kt
Enter a unit to convert to: mph
Enter a value to convert from Kn to mph: 3
3.5 mph
Enter a unit to convert from: W
Enter a unit to convert to: O
Enter a value to convert from W to : 12
Enter an additional unit (choose between ['A', 'V']): V
Enter the value: 12
12 

Contributing to This Repository

Please read CONTRIBUTING.md for information on how to contribute to the UnitConverter repository.

unitconverter's People

Contributors

gdurga avatar ibibgor avatar jerold avatar kalkehcoisa avatar maiquynhtruong avatar mattgd avatar mridulg avatar nhatbui avatar pwnfoo avatar qbunt avatar shashankaryan avatar siqueira-ec avatar suvojit-0x55aa avatar vasudev-bongale avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

unitconverter's Issues

Implement better way to differentiate methods

For example, in conversions like the one below, method names can't really be used as effectively to convert the units.

# Watts to Ohms given Amps
def w_to_o(watts, amps):
    return watts / (amps * amps)

# Watts to Ohms given Volts
def w_to_o2(watts, volts):
    return (volts * volts ) / watts

Right now, most units are converted using the abbreviations from the user input (ex: cm to in) and then selecting the method based on those abbreviations (cm_to_in method), but would like to move to a more widely compatible method for using the API in other projects where user input isn't important.

For reference, this is how the correct method is selected.

conversion_method = units_from + '_to_' + units_to # Name of method to use
thismodule = sys.modules[__name__] # This module as object

try:
   value = getattr(thismodule, conversion_method)(value)
    return str(round(value, decimal_places)) + units_to
except AttributeError:
   return 'Incompatible units.'

Implement number base conversion

Another useful conversion would be number base conversion. A user should be able to enter an number in one base, and provide the base they would like to convert it to and be returned the converted value.

Example input the user would provide:

[*] Enter a unit to convert from: 2 (or B2, meaning Base 2/binary, might be a better option)
[*] Enter a unit to convert to: 8 (or B8, meaning Base 8/octal)
[*] Enter a value to convert from B2 to B8: 1001100

Then the output would be something like: 1001100 in B8: 114

Optimize String handling

Right now, the input is being checked twice for proper syntax and this is redundant. For example, this line is repetitive and can be handled in a better way.

Add conversions for moles

  • Any number can be converted into moles, so just having a check for if the input contains "mol" in it before all of the method checks (before the "Check the rest of the conversion factors" comment in unit_converter.py) is fine.
  • Essentially just a "moles" method that takes conversion factors of two different units.
  • Use 6.022140857e23 for the Avogadro constant.

Implements better way to do function inverses

For example, the following could be turned into one method just by having an "inverse' parameter.

The existing two methods:

# Newtons to Dynes (dyn)
def n_to_dyn(n):
    return n * 10 ^ 5


# Dynes to Newtons (n)
def dyn_to_n(dyn):
    return dyn * 0.00001  # Precision of 5 significant digits

could be turned into one:

# Newtons (N) and Dynes (dyn)
# Decimal value, boolean inverse
def n_dyn(value, inverse):
    if inverse:
        return value / (10 ^ 5)

    return value * 10 ^ 5

This would require a rewrite of the method selection function that would allow it to select the function that has both the units_from and units_to in the name, and then decide if it's units_from_units_to , in which case inverse should be false, or if not then inverse should be set to true. Implementing this will require a bunch of changes to multiple files.

Timezone conversion

Google supports conversion of timezones. I ask what time it is in Tokio when it is 10 am in Berlin.
Maybe this conversion could be implemented.

Create project logo

If anyone has any ideas for a logo, or would like to take a shot at creating one, feel free.

Make code backward compatible with python2

A lot of people have Python2 as their default python interpreter. Currently, the code fails to run on python2. Here is the stacktrace :

$ python unit_converter.py
Python unit converter by mattgd.
     
Units supported:
     Circle     : radians (r), degrees (d) 
     Temperature    : Celsius (c), Fahrenheit (f), and Kelvin (k) 
     Speed      : Kilometers/hour (kph), miles/hour (mph), knots(kt) .


[-] Example entries: 1.345/r/d, 33/f/c, 2/mph/kph

[*] Enter a value and units to convert from and to: 33/f/c
Traceback (most recent call last):
  File "unit_converter.py", line 295, in 
    number = input('\n[*] Enter a value and units to convert from and to: ')
  File "", line 1, in 
NameError: name 'f' is not defined

Implement help to see what units can be converted

Maybe like some kind of menu:
Temperature
Force
Energy
...
And if entering one of the highlighted characters opening a submenu with all of the possible units for that category

And/Or if one unit is already entered show in which unit i can convert the already given unit.
e.g.: Program asks to enter a unit to convert from. Me entering "kph". Program asks me to enter a unit to convert to. Me asking the program to show me all possible units i can convert to. Program answers with "kt" and "mph". -> Then normal program execution.

To solve this one could use the already inflated UNITS array and only show all category names or all units in one category.

Transition to proper API

Right now UnitConverter is more of a command line script, but it may be more useful if it could be used like a proper API.

This shouldn't be too difficult with some refactoring of the convert_units(value, from_unit, to_unit, **args) function, but we should also make use of exceptions where appropriate.

Please feel free to take a stab at this and/or discuss possible designs and their pros/cons below.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.