Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds unit conversion #20

Closed
wants to merge 51 commits into from
Closed

Commits on Jun 15, 2021

  1. Adds unit conversion

    run the unit_converter.py script to see an example.
    kdschlosser committed Jun 15, 2021
    Configuration menu
    Copy the full SHA
    7ba9cf2 View commit details
    Browse the repository at this point in the history

Commits on Jun 19, 2021

  1. changes floating point computations

    Changed to using `decimal.Decimal` for better floating point accuracy.
    
    Removes having to pass a "unit type" constant
    
    Main entry point now always returns a float
    
    General code cleanup.
    kdschlosser committed Jun 19, 2021
    Configuration menu
    Copy the full SHA
    06a598b View commit details
    Browse the repository at this point in the history

Commits on Oct 31, 2021

  1. Configuration menu
    Copy the full SHA
    33d51d7 View commit details
    Browse the repository at this point in the history

Commits on Nov 1, 2021

  1. Configuration menu
    Copy the full SHA
    5feeea2 View commit details
    Browse the repository at this point in the history
  2. Trimmed some code that was no longer needed.

    General code cleanup and thinning.
    kdschlosser committed Nov 1, 2021
    Configuration menu
    Copy the full SHA
    a1c292f View commit details
    Browse the repository at this point in the history
  3. Trimmed some code that was no longer needed.

    General code cleanup and thinning.
    kdschlosser committed Nov 1, 2021
    Configuration menu
    Copy the full SHA
    dc1dcca View commit details
    Browse the repository at this point in the history
  4. Merge remote-tracking branch 'origin/unit_conversion' into unit_conve…

    …rsion
    
    # Conflicts:
    #	python_utils/unit_converter.py
    kdschlosser committed Nov 1, 2021
    Configuration menu
    Copy the full SHA
    f4dffbe View commit details
    Browse the repository at this point in the history
  5. Trimmed some code that was no longer needed.

    General code cleanup and thinning.
    kdschlosser committed Nov 1, 2021
    Configuration menu
    Copy the full SHA
    da27386 View commit details
    Browse the repository at this point in the history

Commits on Nov 3, 2021

  1. Adds support for complex SI symbol notation

    I scraped most of the original parser for the units. The one I wrote is mostly compliant to the SI standard. super script exponents MUST be used passing `m^2` is not going to work anymore instead `m²` needs to be passed.
    
    There are 2 different measurement scaled for quarts, fluid oz, pint and gallon. There is the US scale and the Imperial scale. To use the imperial scale Imp needs to appended to the symbol without any spaces. None of the symbols as spaces in them because the program converts a space into the symbol for multiplication.
    
    I have added constants for all of the "special" characters that can be used.
    
    SUPER_SCRIPT_0: ⁰
    SUPER_SCRIPT_1: ¹
    SUPER_SCRIPT_2: ²
    SUPER_SCRIPT_3: ³
    SUPER_SCRIPT_4: ⁴
    SUPER_SCRIPT_5: ⁵
    SUPER_SCRIPT_6: ⁶
    SUPER_SCRIPT_7: ⁷
    SUPER_SCRIPT_8: ⁸
    SUPER_SCRIPT_9: ⁹
    SUPER_SCRIPT_DECIMAL: ·  (¹·²)
    SUPER_SCRIPT_MINUS: ⁻  (⁻¹)
    MULTIPLIER: ⋅  (N⋅J)
    
     the special character values I used are what is normally used for mathematical equations. Unfortunately what happens when copying and pasting from say a website is the characters get converted to ascii characters.
    
     As I said above I did stray a small amount from the SI Standard. I have included unit conversions that are not "official" and I also added some abbreviations for common use  things like psi, mph and kph which is ot correct syntax for the symbols. I did remove being able to use cu and sq as abbreviations for cubic and square and superscript MUST be used for these from now on.
    
     I still hav to go over the conversion factors to ensure they are correct.  There is an issue with temperature conversion that I have to figure out. So there is a known glitch there.
    kdschlosser committed Nov 3, 2021
    Configuration menu
    Copy the full SHA
    a4fa888 View commit details
    Browse the repository at this point in the history
  2. Testing code and correcting issues.

    I have corrected a couple of issues and I also added a simple test to help me identify the problems.
    kdschlosser committed Nov 3, 2021
    Configuration menu
    Copy the full SHA
    c8959e2 View commit details
    Browse the repository at this point in the history
  3. Tracebacks fixed and units added

    I added some more units and fixed the tracebacks.
    
    6932 conversions done in 2.106767416000366 seconds
    kdschlosser committed Nov 3, 2021
    Configuration menu
    Copy the full SHA
    8be3f42 View commit details
    Browse the repository at this point in the history
  4. Adds type hinting and moves docstring

    I added Python2/3 type hinting and I mived the docstring from the module level to the `convert` function.
    
    I also added some pycharm inspection ignore statements.
    kdschlosser committed Nov 3, 2021
    Configuration menu
    Copy the full SHA
    3549a28 View commit details
    Browse the repository at this point in the history
  5. Removes test code

    kdschlosser committed Nov 3, 2021
    Configuration menu
    Copy the full SHA
    dae32e1 View commit details
    Browse the repository at this point in the history
  6. Changes how precision of returned value is handled

    I removed the precision parameter from the 'convert' function and I am now using 'decimal.Decimal' to handle setting to float precision. The trailing zeros of a float in `decimal.decimal` are preserved and is what I am using to set the precision. This keeps with the SI standard. If someone passes an integer `decimal.Decimal` instance the returned value would be an integer. if the passed value is an integer the returned value  is an integer as well. If the passed value is a float the return value is float with maximum precision and if passing a float `decimal.Decimal` instance the returned value is a float rounded to the precision gotten from the passed value.
    kdschlosser committed Nov 3, 2021
    Configuration menu
    Copy the full SHA
    ed8a007 View commit details
    Browse the repository at this point in the history

Commits on Nov 7, 2021

  1. Configuration menu
    Copy the full SHA
    8f6527f View commit details
    Browse the repository at this point in the history
  2. Rewrite to add support for more units

    This rewrite adds mechanics that allows the addition of units either by the library maintainer or another person. It also allows the addition of units in user code if needed.
    
    I have begun the bug testing of the code and there are still problems that need to be ironed out. It is working but there is an issue with some of the conversions that I have to sort out;
    kdschlosser committed Nov 7, 2021
    Configuration menu
    Copy the full SHA
    b396a37 View commit details
    Browse the repository at this point in the history

Commits on Nov 8, 2021

  1. I am pretty sure this is what you wanted.

    Adds 2 additional ways to convert units.
    kdschlosser committed Nov 8, 2021
    Configuration menu
    Copy the full SHA
    e0694ec View commit details
    Browse the repository at this point in the history

Commits on Nov 10, 2021

  1. documentation

    kdschlosser committed Nov 10, 2021
    Configuration menu
    Copy the full SHA
    65653fd View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    6253614 View commit details
    Browse the repository at this point in the history

Commits on Nov 21, 2021

  1. Several changes

    Moves temperature conversions into the Unit class. The temperature conversions will not function just like the rest of the conversions.
    
    Added a little more information to the documentation.
    
    Added ft_survey unit.
    
    Increased the precision on several units.
    
    Documentation is going to be a long task that I could use soe help with. I need to separate the units into groups and I also need to provide full names and what exactly they are for. At the same tie if the precision of the unit can be improved then it should be done at that time. It is quite a bit of scowering the internet to find the best precision. At the same time if we happen to come across other units to add we should do so at that time.
    kdschlosser committed Nov 21, 2021
    Configuration menu
    Copy the full SHA
    43c79e7 View commit details
    Browse the repository at this point in the history

Commits on Nov 22, 2021

  1. Configuration menu
    Copy the full SHA
    3d05447 View commit details
    Browse the repository at this point in the history
  2. Splits unit converter into several files

    The code base was getting to long for a single file so for the sake of organization I split it into several files.
    
    The other thing that splitting it into several files allowed me to do was put into place some voodoo code for the unit constants. so now instead of unit_converter.Unit.m it's unit_converter.units.m. Here is the voodoo code bit. To define constants for all of the units is one thing but adding constants for all of the units with all of the prefixes would have been insane. So what the voodoo magic is you can now do the following, unit_converter.units.cm or unit_converter.units.mm and a unit for 'mm' will be created on demand.
    
    I also added support for ^ followed by a numerical value that can be used to signify exponents when passing the unit as a string to `convert` or passing the unit to Unit and not using the exponent keyword.
    kdschlosser committed Nov 22, 2021
    Configuration menu
    Copy the full SHA
    44af537 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    472dbe6 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    b89ca20 View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    effc48c View commit details
    Browse the repository at this point in the history
  6. Fixes RecursionError problem if an unsupported unit is given.

    Adds support for python exponent syntax. all 3 of the following are now supported
    
    unit_converter.convert(71, 'in**3', 'mm**3')
    unit_converter.convert(71, 'in^3', 'mm^3')
    unit_converter.convert(71, 'in³', 'mm³')
    kdschlosser committed Nov 22, 2021
    Configuration menu
    Copy the full SHA
    ca807b5 View commit details
    Browse the repository at this point in the history
  7. Changes returned value type

    `int` return type has been removed. a `float` will always be returned.
    
    if an `int` is passed into the converter it will be treated the same as a `float` being passed in and the returned value will have maximum precision.
    
    I also added passing a `str` or `bytes` and these are treated the same as passing a `decimal.Decimal` instance. trailing zeros  of a fractional `decimal.Decimal`, `str` or `bytes` instance are treated as precision.
    kdschlosser committed Nov 22, 2021
    Configuration menu
    Copy the full SHA
    503c37d View commit details
    Browse the repository at this point in the history
  8. Configuration menu
    Copy the full SHA
    d9359ac View commit details
    Browse the repository at this point in the history
  9. Configuration menu
    Copy the full SHA
    a808de5 View commit details
    Browse the repository at this point in the history
  10. Code organization

    kdschlosser committed Nov 22, 2021
    Configuration menu
    Copy the full SHA
    f81e684 View commit details
    Browse the repository at this point in the history

Commits on Nov 23, 2021

  1. Configuration menu
    Copy the full SHA
    0126bbb View commit details
    Browse the repository at this point in the history

Commits on Nov 24, 2021

  1. Configuration menu
    Copy the full SHA
    ac9e62e View commit details
    Browse the repository at this point in the history
  2. fixed tests

    wolph committed Nov 24, 2021
    Configuration menu
    Copy the full SHA
    e8e9ba8 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    8237b00 View commit details
    Browse the repository at this point in the history

Commits on Nov 25, 2021

  1. Adds quantities and constants

    OK I added the SI constants so the accuracy will be improved. I still have more work to do with it like wrapping all of the numbers in `Decimal`. It works except for floating point math errors which will be taken care of once I wrap the numbers.
    
    This next bit is going to be a tad long because I have to explain it from the bottom up so it will make sense.
    
    All units in the SI system break down into 1 or a combination of 7 core units, This is called the "dimension". What the dimension is used for is to know what units can be converted between each other. I understand the purpose for it but it is really not needed in order to know what units are able to be converted between each other. Let me explain.
    
    There are what is called quantities in the SI system. Now why on earth they called it by that name is beyond me and as much as I search the internet I am not able to find a list of all of the quantities the SI system has. I can find lists for specific fields of study like mechanical and electrical. The issue with trying to put together a list of the quantities in this manner is I do not know all of the fields of study that use the SI system and I cannot find a list of all of the fields of study that use the SI system. Very annoying.
    
    Quantities are simply groups of units that can be converted between each other. Each group has a unit as it's base unit. This is what all units in that group will convert to. This provides the ability to supply aa single factor and be able to convert between any unit in the group. If a unit is a "from" unit it divides the input value by the factor to achieve the group unit and a "to" unit will multiply by the factor to convert from the group unit.
    
    The quantity will typically have it's own symbol just like a unit does, I am not using this symbol at all I just thought it was worth mentioning.
    
    Because the quantity has a specific unit it uses it is not hard to locate either the base SI symbol that represents the unit or it will be a symbol pointing to units outside of the quantity group and from those you can get the SI representations of.
    
    As long as 2 units have the same base SI symbol for the unit that is the "factor" or go between unit for the conversion the units are compatible. That being said you can convert between units that are not in the same quantity group so long as that SI symbol matches.
    
    So I added 169 SI categories along with the SI symbol for the categories unit and I also converted those SI symbols into a base SI symbol and that is when the magic really starts to happen. Once everything is broken down into the base SI symbol units and quantities alike I am now able to connect a unit units that are compatible for conversion. I am also able to link a unit to compatible quantities as well.
    
    Now I am nto finished with the modifications just yet as I still have code cleanup to do (large change) and I still have some ideas rolling around in my head for some other things to add.
    
    I did want to mention that this addition is not going to list units tha are dynamically made or that are user input and there is no pre defined unit for it. an example is miles per hour. While I did hard code in the ability to use "mph" this is an incorrect symbol for miles per hour. The correct symbol is "mi/h" or "mi h^-1".  so if a user selects the unit "mph" and prints out a list of compatible units "mi/h" is ot going to show up in the list. That is because "mi/h" is not a single unit. It is 2 units written as a mathematical equation. If I was to back pedal starting form the 7 core SI units and located every possible combination of units that can mathematically be put together and would be valid for a unit conversion the list would be HUGE and to generate that list would take a large amount of time. That list would also have in it some off the wall combinations of units that really do not relate to each other.
    
    I did also want to mention that I found a couple of bugs during this process and I have fixed them. One of those bugs caused me to write a new unit parser. This new parser is faster and easier to understand what is happening.
    
    Look at the example file at the bottom it shows how to use the new features. I have not yet documented the features  as I am still getting a better understanding of the relationships between quantities and units.
    kdschlosser committed Nov 25, 2021
    Configuration menu
    Copy the full SHA
    f4f6ca3 View commit details
    Browse the repository at this point in the history
  2. Merge remote-tracking branch 'origin/unit_conversion' into unit_conve…

    …rsion
    
    # Conflicts:
    #	python_utils/unit_converter/unit.py
    #	python_utils/unit_converter/unit_builder.py
    kdschlosser committed Nov 25, 2021
    Configuration menu
    Copy the full SHA
    bd230ad View commit details
    Browse the repository at this point in the history

Commits on Nov 27, 2021

  1. Configuration menu
    Copy the full SHA
    0c62cfd View commit details
    Browse the repository at this point in the history

Commits on Nov 28, 2021

  1. Configuration menu
    Copy the full SHA
    4eb03c8 View commit details
    Browse the repository at this point in the history
  2. Working on the quantities

    kdschlosser committed Nov 28, 2021
    Configuration menu
    Copy the full SHA
    6ed2785 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    12963f8 View commit details
    Browse the repository at this point in the history
  4. Fix missing closing quote

    kdschlosser committed Nov 28, 2021
    Configuration menu
    Copy the full SHA
    4b11ee3 View commit details
    Browse the repository at this point in the history

Commits on Nov 29, 2021

  1. New quantity system is done.

    This is another big update to the program.
    
    The quantity system is designed in a manner that allows a user to add additional quantities and that gets done by subclassing the `Quantities` class. There is a metaclass attached to `Quantities` that handles injecting the newly created class. An instance of the class does not need to be made.
    
    It is important to use CaMeL case for the class name. The class name gets parsed and a space is added before every uppercase letter (except  the first) and that gets set to the class attribute `name`. `name` does not have to be set manually unless you want the name to to be different then the class name.
    
    The docstring gets turned into a single line description that is accessible from the description class attribute.
    
    You do not need to set the description attribute unless you want the description to be different then the docstring.
    
    The symbol attribute is optional. It is a nicety that just gives more information is wanted.  At this time it does not effect hot the program runs at all.
    
    The unit attribute is mandatory, this is what the program uses to determine what units are compatible. Each quantity in the SI system typically has a unit attached to it. This unit is what units gets converted to and converted from. It is what all conversion factors for a specific quantity are set for.
    
    How the program check for compatible units is every unit in the SI system breaks down into one or more of 7 base units. by comparing the "SI equivalent" or "SI expression" for a quantity to the same for another quantity if they match then the quantities are compatible and conversions can be done across them.
    
    Here is an example of how to correctly add a quantity to the program
    
    ```python
    
    from python_utils.unit_converter.quantity import Quantities
    
    class Crackle(Quantities):
        """
        Change of jounce per unit time: the fifth time derivative of position
        """
        symbol = 'c'
        unit = units.m / units.s(exponent=5)
    ```
    
    The quantities in the SI system are wild as there are only a small number of them that are in the SI standard. Most of the SI quantities are created based on use of the SI system. Here are some examples.
    
    Physics
    Chemistry
    Mechanical
    Radiometry
    Optics
    Molar
    Electromagnetic
    Photometric
    
    Each of those fields has SI quantities that relate to different unit groupings. I want to note that there are overlaps between the different fields and also with the quantities written into the SI standard. They may share the same name and they may not. If the name is not shared but you would like to have the correct name attached to the quantity then subclass the quantity you want  to create a new name for. You do not have to specify the unit when doing this if the unit is the same. All other attributes function the same way.
    
    Now remember an instance of the new quantity does not need to be created for this to work    you simply hve to define the quantity class and either subclass `Quantities` or another quantity class.
    
    I have done a large amount of trial and error in order to get the best possible speed while keeping the memory footprint as small as possible. In the  example file at the end I have the system enumerate all of the core units by way of iterating over all available quantities and the compatible units for those quantities.
    
    There are 514 core units and 179 core quantities I have added so far. I still have to add the units and quantities related to Optics and any other fields of study that use the SI system. I have not been able to locate a list fo field of study that use the SI system so locating them has been a matter of stumbling across them on accident.
    kdschlosser committed Nov 29, 2021
    Configuration menu
    Copy the full SHA
    7adec5e View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    ee54851 View commit details
    Browse the repository at this point in the history

Commits on Nov 30, 2021

  1. Adds Optics quantities

    kdschlosser committed Nov 30, 2021
    Configuration menu
    Copy the full SHA
    e0ca265 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    21df582 View commit details
    Browse the repository at this point in the history

Commits on Dec 1, 2021

  1. Configuration menu
    Copy the full SHA
    d97dd26 View commit details
    Browse the repository at this point in the history

Commits on Dec 2, 2021

  1. Reworked the operator methods.

    I wrote the operator methods over and expanded them to include true division, floor division, subtraction and addition. This allows for the use of all of the right hand and left hand math operators as well. I still have to add the bit shifting. I do not know if there is a way to dink about with the exponent operator I have never checked. I would assume there is.
    
    I also did some work on the "units" module mechanics. I did not want users altering the actual stored units in any way. The way it is coded now it eliminates IDE errors for the attributes not being found for units with prefixes. The type hinting and autocomplete doesn't work for the units with prefixes and I do not believe there is a way to get them to work either. The new system also creates a new unit instance instead of letting the use alter the ones that are already stored.
    kdschlosser committed Dec 2, 2021
    Configuration menu
    Copy the full SHA
    555a77e View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    87faf92 View commit details
    Browse the repository at this point in the history

Commits on Dec 3, 2021

  1. Adds 2 way conversions

    This is kinda cool.
    
    ```python
    mph = units.mph
    kph = units.kph
    mph /= kph
    
    mph(20)
    print(kph.value)
    
    kph(150)
    print(mph.value)
    ```
    
    so when setting the value of one unit the cnoverted value shows up on the other.  Neat thing about that isit allows 2 remote sections of code in a program to work without actually passing anything between them. An example is say you have one block of code that hands the input and output of data. and that data is in kph, then you have another bunch of code that handles say a GUI interface. when the program initially starts a module level attribute can be created for the conversion using this technique. The data in and out uses the "kph" attribute and the gui uses the "mph" attribute. so the gui would do it's thing   and when updating the information it would  collect the value of "mph" the data end would set the data into "kph" which does the conversion and spits it out on the other end in the "mph" attribute.
    
    It's kinda cool actually.
    
    I can even write it so it supports more then a single conection. so if you wanted to say convert from km/h to mi/h, ft/s and m/s that could be done pretty easily. as it stands right now this can be done.
    
    ```python
    kph = units.kph
    kph.value = 60
    
    kph.mph
    kph.fps
    kph.mps
    ```
    and the returned value would be the converted value.
    kdschlosser committed Dec 3, 2021
    Configuration menu
    Copy the full SHA
    a29a6df View commit details
    Browse the repository at this point in the history

Commits on Dec 6, 2021

  1. Configuration menu
    Copy the full SHA
    8ce1f5d View commit details
    Browse the repository at this point in the history

Commits on May 29, 2022

  1. Merge branch 'develop' into unit_conversion

    # Conflicts:
    #	docs/conf.py
    #	python_utils/time.py
    #	setup.py
    wolph committed May 29, 2022
    Configuration menu
    Copy the full SHA
    7eebf47 View commit details
    Browse the repository at this point in the history