{"id":20529521,"url":"https://github.com/haimkastner/unitsnet-py","last_synced_at":"2025-04-14T05:18:44.494Z","repository":{"id":149990943,"uuid":"622283434","full_name":"haimkastner/unitsnet-py","owner":"haimkastner","description":"A better way to hold unit variables and easily convert to the destination unit ","archived":false,"fork":false,"pushed_at":"2025-04-02T15:15:29.000Z","size":1814,"stargazers_count":11,"open_issues_count":2,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-14T05:17:38.359Z","etag":null,"topics":["conversion","converter","measure","measurements","measures","quantities","unit","unit-converter","units","units-of-measure"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/unitsnet-py/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/haimkastner.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-01T16:49:18.000Z","updated_at":"2025-04-02T15:15:26.000Z","dependencies_parsed_at":"2023-07-05T04:31:53.092Z","dependency_job_id":"48056b6f-44f2-4e7d-93d5-081df1c491ef","html_url":"https://github.com/haimkastner/unitsnet-py","commit_stats":{"total_commits":132,"total_committers":6,"mean_commits":22.0,"dds":"0.23484848484848486","last_synced_commit":"f2e4b2b78d090ad1ee1922f00f727c3b05883760"},"previous_names":[],"tags_count":128,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haimkastner%2Funitsnet-py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haimkastner%2Funitsnet-py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haimkastner%2Funitsnet-py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haimkastner%2Funitsnet-py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/haimkastner","download_url":"https://codeload.github.com/haimkastner/unitsnet-py/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248824693,"owners_count":21167345,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["conversion","converter","measure","measurements","measures","quantities","unit","unit-converter","units","units-of-measure"],"created_at":"2024-11-15T23:32:35.460Z","updated_at":"2025-04-14T05:18:44.486Z","avatar_url":"https://github.com/haimkastner.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# unitsnet-py\n\n[![unitsnet-py](https://github.com/haimkastner/unitsnet-py/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/haimkastner/unitsnet-py/actions/workflows/build.yml)\n\n [![Latest Release](https://img.shields.io/github/v/release/haimkastner/unitsnet-py)](https://github.com/haimkastner/unitsnet-py/releases) \n[![PyPI version](https://img.shields.io/pypi/v/unitsnet-py)](https://pypi.org/project/unitsnet-py/)\n\n\n[![GitHub stars](https://img.shields.io/github/stars/haimkastner/unitsnet-py.svg?style=social\u0026label=Stars)](https://github.com/haimkastner/unitsnet-py/stargazers) \n[![pypi downloads](https://img.shields.io/pypi/dm/unitsnet-py.svg?style=social)](https://pypi.org/project/unitsnet-py/)\n[![License](https://img.shields.io/github/license/haimkastner/unitsnet-py.svg?style=social)](https://github.com/haimkastner/unitsnet-py/blob/master/LICENSE)\n\nThe unitsnet-py package provides an efficient way to store unit variables and perform easy conversions to different units when it required. \n\nIt offers support for more than 100 unit types across various unit categories, including pretty-printing, comparison, and arithmetic methods. \n\nThe API is designed to be user-friendly and straightforward to use.\n\nThe library is built on top of the [Units.NET](https://github.com/angularsen/UnitsNet) project and leverages their [definitions sources](https://github.com/angularsen/UnitsNet/tree/master/Common/UnitDefinitions) to generate the Python unit classes.\n\n###### The unitsnet-py package does not require any external dependencies or packages to function.\n\n\nPackage is available on PyPI at https://pypi.org/project/unitsnet-py/\n\n[Units.NET on other platforms](#unitsnet-on-other-platforms)\n\n## Install via PyPi\n\n```bash \npip install unitsnet-py\n```\n\n## Example Usage\n\n```python\nfrom unitsnet_py import Angle, AngleUnits, Length, LengthUnits\n\n\nangle = Angle.from_degrees(180)\n# equals to\nangle = Angle(180, AngleUnits.Degree)\n\nprint(angle.radians)  # 3.141592653589793\nprint(angle.microradians)  # 3141592.65358979\nprint(angle.gradians)  # 200\nprint(angle.microdegrees)  # 180000000\n\n\n# As an alternative, a convert style method are also available\nprint(angle.convert(AngleUnits.Radian))  # 3.141592653589793\nprint(angle.convert(AngleUnits.Microradian))  # 3141592.65358979\nprint(angle.convert(AngleUnits.Gradian))  # 200\nprint(angle.convert(AngleUnits.Microdegree))  # 180000000\n\n\n# Print the default unit to_string (The default for angle is degrees)\nprint(angle.to_string())  # 180 °\n\nprint(angle.to_string(AngleUnits.Degree))  # 180 °\nprint(angle.to_string(AngleUnits.Radian))  # 3.141592653589793 rad\n\n# Specify fraction digits max length\nprint(angle.to_string(AngleUnits.Radian, 2))  # 3.14 rad\n```\n\n## Additional methods\n\nCheck, compare, calculate etc. with unitsnet:\n\n```python\nlength1 = Length.from_meters(10)\nlength2 = Length.from_decimeters(100)\nlength3 = Length.from_meters(3)\n\n# 'equals' method\nprint(length1 == length2)  # True\nprint(length1 == length3)  # False\n\n# 'compareTo' method\nprint(length3 \u003e length1)  # False\nprint(length3 \u003c length1)  # True\nprint(length2 \u003e= length1)  # True\n\n# Arithmetics methods\nresults1 = length1 + length3\nresults2 = length1 - length3\nresults3 = length1 * length3\nresults4 = length1 / length3\nresults5 = length1 % length3\nresults6 = length1 ** length3\nprint(results1.to_string(LengthUnits.Meter))  # 13 m\nprint(results2.to_string(LengthUnits.Meter))  # 7 m\nprint(results3.to_string(LengthUnits.Meter))  # 30 m\nprint(results4.to_string(LengthUnits.Meter))  # 3.3333333333333335 m\nprint(results5.to_string(LengthUnits.Meter))  # 1 m\nprint(results6.to_string(LengthUnits.Meter))  # 1000 m\n\n# Complex objects\n\n# Any object supports arithmetic operations can be used as well as unit\n# see numpy array example:\nimport numpy as np\n\nnp_array = np.array([[2, 4, 6], [7, 8, 9]])\n\nnp_array_length = Length.from_kilometers(np_array)\nprint(np_array_length.meters) # [[2000. 4000. 6000.][7000. 8000. 9000.]]\n\nnp_array_double_length = np_array_length + np_array_length\nprint(np_array_double_length.kilometers) # [[ 4.  8. 12.][14. 16. 18.]]\nprint(np_array_double_length.meters) # [[ 4000.  8000. 12000.][14000. 16000. 18000.]]\n\n```\n## DTO - Data Transfer Object\n\nAs UnitsNet provides a convenient way to work within a running service, there are occasions where the data needs to be exposed outside of the service, typically through an API containing the unit value or consumed from an API.\n\nTo support this with a clear API schema and make it easy to convert to and from this schema to the specific format, it's recommended to use DTOs and the UnitsNet flavor converters.\n\n```python\nfrom unitsnet_py import Length, LengthDto, LengthUnits\n\n # Create a Length unit object\nlength = Length.from_meters(100.01)\n# Obtain the DTO object as json, represented by the default unit - meter\nlength_dto_json = length.to_dto_json() # {\"value\":100.01,\"unit\":\"Meter\"}\n# Obtain the same value but represent DTO in KM \nlength_dto_represents_in_km_json = length.to_dto_json(LengthUnits.Kilometer) # {'value': 0.10001, 'unit': 'Kilometer'}\n# Load JSON to DTO, and load\nlength_from_meters_dto = Length.from_dto_json(length_dto_json)\n# The exact same value as\nlength_from_km_dto = Length.from_dto_json(length_dto_represents_in_km_json)\n\n# Additionally, it supports creating and handling as a DTO instance, as well as creating and converting to/from JSON.\n\n# Get a DTO instance from a Length instance\nlength_dto: LengthDto = length.to_dto()\n# Get the json representation of the DTO\nlength_json = length_dto.to_json() # {\"value\":100.01,\"unit\":\"Meter\"}\n# Obtain DTO instance from a json representation\nlength_dto: LengthDto = LengthDto.from_json(length_json)\n# Obtain Length unit from a DTO instance\nlength = Length.from_dto(length_dto)\n```\n\nCheck out the OpenAPI [unitsnet-openapi-spec](https://haimkastner.github.io/unitsnet-openapi-spec-example/) example schema.\n\nAlso, refer to the detailed discussions on GitHub: [haimkastner/unitsnet-js#31](https://github.com/haimkastner/unitsnet-js/issues/31) \u0026 [angularsen/UnitsNet#1378](https://github.com/angularsen/UnitsNet/issues/1378).\n\n## Supported units\n\n[UnitsNet supported Units](https://github.com/haimkastner/unitsnet-py/blob/main/Units.md)\n\n## Units.NET on other platforms\n\nGet the same strongly typed units on other platforms, based on the same [unit definitions](/Common/UnitDefinitions).\n\n| Language                   | Name        | Package                                           \t\t\t\t\t | Repository                                           | Maintainers  |\n|----------------------------|-------------|---------------------------------------------------------------------|------------------------------------------------------|--------------|\n| C#                         | UnitsNet    | [nuget](https://www.nuget.org/packages/UnitsNet/) \t\t\t\t\t | [github](https://github.com/angularsen/UnitsNet)     | @angularsen  |\n| JavaScript /\u003cbr\u003eTypeScript | unitsnet-js | [npm](https://www.npmjs.com/package/unitsnet-js)  \t\t\t\t\t | [github](https://github.com/haimkastner/unitsnet-js) | @haimkastner |\n| Python                     | unitsnet-py | [pypi](https://pypi.org/project/unitsnet-py)      \t\t\t\t\t | [github](https://github.com/haimkastner/unitsnet-py) | @haimkastner |\n| Golang                     | unitsnet-go | [pkg.go.dev](https://pkg.go.dev/github.com/haimkastner/unitsnet-go) | [github](https://github.com/haimkastner/unitsnet-go) | @haimkastner |\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhaimkastner%2Funitsnet-py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhaimkastner%2Funitsnet-py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhaimkastner%2Funitsnet-py/lists"}