{"id":15704077,"url":"https://github.com/dylanwal/unitpy","last_synced_at":"2025-05-01T02:34:33.950Z","repository":{"id":62586411,"uuid":"398019386","full_name":"dylanwal/unitpy","owner":"dylanwal","description":"UnitPy is a Python package for defining, converting, and working with units.","archived":false,"fork":false,"pushed_at":"2025-03-02T22:02:24.000Z","size":109,"stargazers_count":8,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T14:17:30.843Z","etag":null,"topics":["python","science","units","units-of-measure"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dylanwal.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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":"2021-08-19T17:12:41.000Z","updated_at":"2025-03-02T21:55:20.000Z","dependencies_parsed_at":"2024-10-24T06:15:59.993Z","dependency_job_id":"a4d5ee73-2da6-4acd-803f-302404e8f9d6","html_url":"https://github.com/dylanwal/unitpy","commit_stats":{"total_commits":48,"total_committers":3,"mean_commits":16.0,"dds":"0.14583333333333337","last_synced_commit":"5100096ea69c5a5085d5bca3966483eb15f81d1b"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylanwal%2Funitpy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylanwal%2Funitpy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylanwal%2Funitpy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylanwal%2Funitpy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dylanwal","download_url":"https://codeload.github.com/dylanwal/unitpy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251812757,"owners_count":21647964,"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":["python","science","units","units-of-measure"],"created_at":"2024-10-03T20:10:15.270Z","updated_at":"2025-05-01T02:34:33.918Z","avatar_url":"https://github.com/dylanwal.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# UnitPy (Unit Python)\n\n---\n---\n\n![Python](https://img.shields.io/pypi/pyversions/unitpy)\n![PyPI](https://img.shields.io/pypi/v/unitpy)\n![downloads](https://static.pepy.tech/badge/unitpy)\n![license](https://img.shields.io/github/license/dylanwal/unitpy)\n\nUnitPy is a Python package for defining, converting, and working with units. \n\nThe goal of the package is to be simple, straightforward, and performant. \n\nThis package directly implements [NIST (National Institute of Standards and Technology)](https://www.nist.gov/pml/special-publication-811/nist-guide-si-chapter-1-introduction) \nofficial unit definitions. \n\n---\n\n## Installation\n\nPip installable package:\n\n`pip install unitpy`\n\n[pypi: unitpy](https://pypi.org/project/unitpy/)\n\n\n---\n\n## Requirements / Dependencies\n\nPython 3.8 and up\n\n---\n\n## Basic Usage\n\n### Defining Unit\n\n```python\nfrom unitpy import U, Q, Unit, Quantity\n# U = Unit\n\nu = Unit(\"kilometer\")\nu = U(\"kilometer\")\nu = U(\"km\")\nu = U.kilometer\nu = U.km\n\nu = U(\"kilometer/hour\")\nu = U(\"km/h\")\nu = U.kilometer / U.hour\nu = U.km / U.h\n\n# properties\nprint(u.dimensionality)  # [length] / [time]\nprint(u.dimensionless)   # False\nprint(u.base_unit)       # meter / second\n```\n\n\n### Defining Quantity\n\n** Quantity = Value + Unit **\n\n```python\nfrom unitpy import U, Q , Unit, Quantity\n# Q = Quantity\n\nq = 1 * U(\"kilometer/hour\") \nq = 1 * (U.kilometer / U.hour)\nq = Quantity(\"1 km/h\")\nq = Q(\"1 kilometer per hour\")\nq = Q(\"1*kilometer/hour\")\n\n\n# properties\nprint(q.unit)            # kilometer / hour\nprint(q.dimensionality)  # [length] / [time]\nprint(q.dimensionless)   # False\nprint(q.base_unit)       # meter / second\n```\n\n\n### Unit Conversion\n\n```python\nfrom unitpy import U, Q , Unit, Quantity\n# Q = Quantity\n\nq = 1 * U(\"km/h\") \nq2 = q.to(\"mile per hour\")\nprint(q2)  # 0.6213711922 mile / hour\n```\n\n\n### Mathematical operation\n\n```python\nfrom unitpy import U, Q \n\nq = 1 * U(\"km/h\") \nq2 = 2.2 * U(\"mile per hour\")\nprint(q2 + q)                 # 2.8213711923 mile / hour\nprint(q2 - q)                 # 1.5786288077 mile / hour\nprint(q2 * q)                 # 2.2 kilometer mile / hour**2\nprint(q2 / q)                 # 2.2 mile / kilometer\nprint((q2 / q).dimensionless) # True\n```\n\nOther supported functions:\n* `sum`\n* `max`\n* `min`\n\n### Temperature\n\n__Abbreviations:__\n\n* fahrenheits: degF\n* celsius: degC\n* kelvin: degK, K\n* rankine: degR\n\n```python\nfrom unitpy import U, Q\n\nq = 300 * U(\"K\")\nq2 = 200 * U(\"K\")\n\nprint(q + q2)        # 500.0 kelvin\nprint(q.to(\"degC\"))  # 26.85 Celsius\nprint(q.to(\"degF\"))  # 830.6344444444 Fahrenheit\nprint(q.to(\"degR\"))  # 166.6666666667 Rankine\n```\n\nTemperature units are non-multiplicative units. They are expressed with respect to a reference point (offset).\n\u003e degC = 5/9 * (degF - 32) \n\nDefault behavior is **absolute units**.\n\nFor **relative units** use dedicated functions `add_rel()` or `sub_rel()`.\n\n```python\nfrom unitpy import U, Q\n\nq = 10 * U(\"degC\")\nq2 = 5 * U(\"degC\")\n\n# absolute\nprint(q.to(\"K\"))         # 283.15 kelvin\nprint(q + q2)            # 288.15 Celsius \nprint((q + q2).to(\"K\"))  # 561.3 kelvin\nprint(q - q2)            # -268.15 Celsius\nprint((q - q2).to(\"K\"))  # 5.0 kelvin\n\n# relative\nprint(q.add_rel(q2))      # 15 Celsius\nprint(q.sub_rel(q2))      # 5 Celsius\nprint(abs(-10 * U.degC))  # 10 Celsius\n```\n\n\n### Time\n\n```python\nfrom datetime import timedelta\nfrom unitpy import U\n\na = 1.234 * U.min\nprint(a)              # 1.234 minute\nb = a.to_timedelta()\nprint(b)              # 0:01:14.040000\nprint(type(b))        # \u003cclass 'datetime.timedelta'\u003e\n```\n\n\n### string formatting\n\n```python\nfrom unitpy import U\n\na = 1.23432453 * U.min\nprint(a)                # 1.23432453 minute\nprint(f\"{a:.2f}\")       # 1.23 minute\nb = 1_000_000 * U.cm\nprint(b)                # 1000000 centimeter\nprint(format(b, \",\"))   # 1,000,000 centimeter\nc = 123 * U.inch\nprint(c)                # 123 inch\nprint(f\"{c:5}\")         #   123 inch  (leading spaces)\nprint(f\"{c:05}\")        # 00123 inch  (leading zeros)\n```\n\n#### configuration with .env\n\nTo configure the string outputs with environment file (.env):\n\n1) install [python-dotenv](https://github.com/theskumar/python-dotenv/tree/main). The code uses this to load \n   variables from the .env file\n```commandline\npip install python-dotenv\n```\n\n2) copy the .env file from this repo into the top level of your repo and edit to your liking\n\nIt should now load the environmental variables when you run the code to format it to your liking without any \nadditional code needed.\n\n\n## Numpy Support\n\n[Numpy](https://github.com/numpy/numpy)\n\n```python\nimport numpy as np\n\nfrom unitpy import Unit, Quantity\n\n# numpy + int\na = np.linspace(0, 4, 5) * Unit.m\nb = 1 * Unit.ft\nc = a+b\nprint(a+b)                           # [0.3048 1.3048 2.3048 3.3048 4.3048] meter\nprint(a-b)                           # [-0.3048  0.6952  1.6952  2.6952  3.6952] meter\na += b\nprint(a)                             # [0.3048 1.3048 2.3048 3.3048 4.3048] meter\n\n# numpy + numpy\na = np.linspace(0, 4, 5) * Unit.m\nb = np.linspace(0, 4, 5) * Unit.ft\nprint(a+b)                           # [0.     1.3048 2.6096 3.9144 5.2192] meter\nprint(a-b)                           # [0.     0.6952 1.3904 2.0856 2.7808] meter\na += b\nprint(a)                             # [0.     1.3048 2.6096 3.9144 5.2192] meter\n\n# numpy * int\na = np.linspace(0, 4, 5) * Unit.m\nb = 1.2 * Unit.s\nprint(a*b)                            # [0.  1.2 2.4 3.6 4.8] meter second\nprint(a/b)                            # [0.  0.83333333 1.66666667 2.5 3.33333333] meter/second\na *= b\nprint(a)                              # [0.  1.2 2.4 3.6 4.8] meter second\n\n# numpy * numpy\na = np.linspace(0, 4, 5) * Unit.m\nb = np.linspace(0, 4, 5) * Unit.s\nprint(a*b)                              # [ 0.  1.  4.  9. 16.] meter second\nprint(a/b)                              # [nan  1.  1.  1.  1.] meter/second\na *= b\nprint(a)                                # [ 0.  1.  4.  9. 16.] meter second\n\n\n# numpy functions\na = np.linspace(-2, 4, 5) * Unit.m\nprint(np.sum(a))                       # 5 meter\nprint(np.max(a))                       # 4 meter\nprint(np.abs(a))                       # [2.  0.5 1.  2.5 4. ] meter\nprint(np.linspace(1*Unit.mm, 2*Unit.ft, 4))  # [  1.         203.86666667 406.73333333 609.6       ] millimeter\n```\n\n---\n\n## Notes\n\n* this package utilizes the American spellings \"meter,\" \"liter,\" and \"ton\"\n* supports pickling \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdylanwal%2Funitpy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdylanwal%2Funitpy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdylanwal%2Funitpy/lists"}