munits


Namemunits JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttps://github.com/DigitalDetective47/munits
SummaryMultiplication-based units for Python
upload_time2024-05-10 22:11:54
maintainerNone
docs_urlNone
authorDigitalDetective47
requires_python>=3.11
licenseNone
keywords units
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            munits (name not capitalized) is a units library for Python. It allows quantities to be stored with their units attached, and simplifies unit conversion somewhat.
# Initial Setup
To import the munits library, use the import statement.
```python
>>> import munits
```
In the examples presented here, the contents of munits are imported directly into the mainspace.
```python
>>> from munits import *
```
# Defining Base Units
To define a base unit, use the `BaseUnit` class. For this example, we will define pixels as our base unit.
```python
>>> pixel = BaseUnit("px")
```
This creates a new unit object. To check this, we can print the new object. String representation of `BaseUnit`s is slightly altered in this documentation as the memory locations present in the actual representation change each time the module is loaded. They are omitted here for clarity.
```python
>>> pixel
<BaseUnit px>
```
munits also defines many standard units in order to improve inter&hyphen;operability of code. These are found within the `si`, `si.prefix`, `us`, and `data` sub&hyphen;modules.
```python
>>> si.METRE
<BaseUnit m>
```
Note that two `BaseUnit`s having the same symbol does not make them compare equal.
```python
>>> spam = BaseUnit("m")
>>> spam == si.METRE
False
>>> del spam  # cleaning up this demonstration
```
# Using Units
To add units to a quantity, multiply the value with the units to be applied.
```python
>>> 5 * pixel
5.0 * <BaseUnit px>
>>> 7.4 * si.METRE
7.4 * <BaseUnit m>
```
Quantities can take multiple units, as well as units with exponents.
```python
>>> 25 * si.METRE / si.SECOND
25.0 * <BaseUnit m> / <BaseUnit s>
>>> 13 * si.METRE**2
13.0 * <BaseUnit m>**2.0
>>> (13 * si.METRE)**2
169.0 * <BaseUnit m>**2.0
```
# Derived Units
Unlike the base units described above, derived units are defined in terms of other units. They can be defined using the `DerivedUnit` class.
```python
>>> megapixel = DerivedUnit("Mpx", 1e6 * pixel)
```
As described above, munits comes with many standard units, many of which are derived from other included units.
```python
>>> si.prefix.KILOMETER
DerivedUnit("km", 1000.0 * <BaseUnit m>)
>>> si.NEWTON
DerivedUnit("N", 1.0 * <BaseUnit kg> * <BaseUnit m> * <BaseUnit s>**-2.0)
```
It is also possible to create new `DerivedUnit`s using built&hyphen;in `BaseUnit`s.
```python
>>> frame = DerivedUnit("f", si.SECOND / 60)
```
# Other Classes
## `UnitQuantity`
A `UnitQuantity` represents a quantity that has units. This class supports many operations. `UnitQuantity`s can be added and subtracted with other `UnitQuantity`s that have compatible units. The `compatible` method can be used to test if two `UnitQuantity`s have compatible units. The `convert` method is used to convert between compatible units. Equality checks can be performed with any pair of `UnitQuantity`s, and comparisons (`<`, `>`, `<=`, or `>=`) can be performed with `UnitQuantity`s with compatible units. `UnitQuantity`s with units that cancel can be converted to `float`s and `float`s can be converted to `UnitQuantity`s with blank units. Other mathematical operations including multiplication and exponentiation. The `normalize` method converts a `UnitQuantity` into another of the same value with only `BaseUnit`s. The `round` method (different to build&hyphen;in `round`!) can be used to round `UnitQuantity`s, with a customizable rounding interval (for example, round to the nearest 5km) and rounding mode.
## `UnitCombination`
A subclass of `UnitQuantity` with a fixed value of 1. The main purpose is to specify a unit type without specifying a value, such as in `UnitQuantity.convert`. The `units` property of `UnitQuantity` objects also returns a value of this type.
## `Unit`
An abstract class representing a generic unit.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/DigitalDetective47/munits",
    "name": "munits",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "units",
    "author": "DigitalDetective47",
    "author_email": "ninji2701@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/9c/60/2106c4703f443f249e18188b75fc522c1687ecbcdf5f8fb744465e63b2cc/munits-1.0.0.tar.gz",
    "platform": null,
    "description": "munits (name not capitalized) is a units library for Python. It allows quantities to be stored with their units attached, and simplifies unit conversion somewhat.\r\n# Initial Setup\r\nTo import the munits library, use the import statement.\r\n```python\r\n>>> import munits\r\n```\r\nIn the examples presented here, the contents of munits are imported directly into the mainspace.\r\n```python\r\n>>> from munits import *\r\n```\r\n# Defining Base Units\r\nTo define a base unit, use the `BaseUnit` class. For this example, we will define pixels as our base unit.\r\n```python\r\n>>> pixel = BaseUnit(\"px\")\r\n```\r\nThis creates a new unit object. To check this, we can print the new object. String representation of `BaseUnit`s is slightly altered in this documentation as the memory locations present in the actual representation change each time the module is loaded. They are omitted here for clarity.\r\n```python\r\n>>> pixel\r\n<BaseUnit px>\r\n```\r\nmunits also defines many standard units in order to improve inter&hyphen;operability of code. These are found within the `si`, `si.prefix`, `us`, and `data` sub&hyphen;modules.\r\n```python\r\n>>> si.METRE\r\n<BaseUnit m>\r\n```\r\nNote that two `BaseUnit`s having the same symbol does not make them compare equal.\r\n```python\r\n>>> spam = BaseUnit(\"m\")\r\n>>> spam == si.METRE\r\nFalse\r\n>>> del spam  # cleaning up this demonstration\r\n```\r\n# Using Units\r\nTo add units to a quantity, multiply the value with the units to be applied.\r\n```python\r\n>>> 5 * pixel\r\n5.0 * <BaseUnit px>\r\n>>> 7.4 * si.METRE\r\n7.4 * <BaseUnit m>\r\n```\r\nQuantities can take multiple units, as well as units with exponents.\r\n```python\r\n>>> 25 * si.METRE / si.SECOND\r\n25.0 * <BaseUnit m> / <BaseUnit s>\r\n>>> 13 * si.METRE**2\r\n13.0 * <BaseUnit m>**2.0\r\n>>> (13 * si.METRE)**2\r\n169.0 * <BaseUnit m>**2.0\r\n```\r\n# Derived Units\r\nUnlike the base units described above, derived units are defined in terms of other units. They can be defined using the `DerivedUnit` class.\r\n```python\r\n>>> megapixel = DerivedUnit(\"Mpx\", 1e6 * pixel)\r\n```\r\nAs described above, munits comes with many standard units, many of which are derived from other included units.\r\n```python\r\n>>> si.prefix.KILOMETER\r\nDerivedUnit(\"km\", 1000.0 * <BaseUnit m>)\r\n>>> si.NEWTON\r\nDerivedUnit(\"N\", 1.0 * <BaseUnit kg> * <BaseUnit m> * <BaseUnit s>**-2.0)\r\n```\r\nIt is also possible to create new `DerivedUnit`s using built&hyphen;in `BaseUnit`s.\r\n```python\r\n>>> frame = DerivedUnit(\"f\", si.SECOND / 60)\r\n```\r\n# Other Classes\r\n## `UnitQuantity`\r\nA `UnitQuantity` represents a quantity that has units. This class supports many operations. `UnitQuantity`s can be added and subtracted with other `UnitQuantity`s that have compatible units. The `compatible` method can be used to test if two `UnitQuantity`s have compatible units. The `convert` method is used to convert between compatible units. Equality checks can be performed with any pair of `UnitQuantity`s, and comparisons (`<`, `>`, `<=`, or `>=`) can be performed with `UnitQuantity`s with compatible units. `UnitQuantity`s with units that cancel can be converted to `float`s and `float`s can be converted to `UnitQuantity`s with blank units. Other mathematical operations including multiplication and exponentiation. The `normalize` method converts a `UnitQuantity` into another of the same value with only `BaseUnit`s. The `round` method (different to build&hyphen;in `round`!) can be used to round `UnitQuantity`s, with a customizable rounding interval (for example, round to the nearest 5km) and rounding mode.\r\n## `UnitCombination`\r\nA subclass of `UnitQuantity` with a fixed value of 1. The main purpose is to specify a unit type without specifying a value, such as in `UnitQuantity.convert`. The `units` property of `UnitQuantity` objects also returns a value of this type.\r\n## `Unit`\r\nAn abstract class representing a generic unit.\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Multiplication-based units for Python",
    "version": "1.0.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/DigitalDetective47/munits/issues",
        "Homepage": "https://github.com/DigitalDetective47/munits"
    },
    "split_keywords": [
        "units"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5406ce7c3d96f3a7bbb06049ca5cd810f5fdc53bac9fa83cb8639c928c8d301a",
                "md5": "0c710d58526af07925a88a9d0d8bb56d",
                "sha256": "97f30485ffcceb4734291668f57056fbd60bba0be84ef1bfe6e510853daca240"
            },
            "downloads": -1,
            "filename": "munits-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0c710d58526af07925a88a9d0d8bb56d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 16808,
            "upload_time": "2024-05-10T22:11:52",
            "upload_time_iso_8601": "2024-05-10T22:11:52.432801Z",
            "url": "https://files.pythonhosted.org/packages/54/06/ce7c3d96f3a7bbb06049ca5cd810f5fdc53bac9fa83cb8639c928c8d301a/munits-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9c602106c4703f443f249e18188b75fc522c1687ecbcdf5f8fb744465e63b2cc",
                "md5": "9d969fc7313d93fac0d4c7b194c06570",
                "sha256": "5d868c3325bee99bcaf5c972c21d0994f7513201e9f1f49cf989ad5f92a204dc"
            },
            "downloads": -1,
            "filename": "munits-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "9d969fc7313d93fac0d4c7b194c06570",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 18570,
            "upload_time": "2024-05-10T22:11:54",
            "upload_time_iso_8601": "2024-05-10T22:11:54.334393Z",
            "url": "https://files.pythonhosted.org/packages/9c/60/2106c4703f443f249e18188b75fc522c1687ecbcdf5f8fb744465e63b2cc/munits-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-10 22:11:54",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "DigitalDetective47",
    "github_project": "munits",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "munits"
}
        
Elapsed time: 0.22977s