{"id":20548685,"url":"https://github.com/kenkundert/quantiphy_eval","last_synced_at":"2025-07-16T02:48:29.866Z","repository":{"id":57459253,"uuid":"245365422","full_name":"KenKundert/quantiphy_eval","owner":"KenKundert","description":null,"archived":false,"fork":false,"pushed_at":"2023-11-08T20:37:08.000Z","size":90,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-24T14:30:10.595Z","etag":null,"topics":["calculator","quantiphy"],"latest_commit_sha":null,"homepage":"","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/KenKundert.png","metadata":{"files":{"readme":"README.rst","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":"2020-03-06T08:21:15.000Z","updated_at":"2023-06-29T04:11:35.000Z","dependencies_parsed_at":"2024-11-16T02:14:21.711Z","dependency_job_id":"ab8b0852-1ada-46ce-84ba-de6e98d4e6d2","html_url":"https://github.com/KenKundert/quantiphy_eval","commit_stats":{"total_commits":56,"total_committers":1,"mean_commits":56.0,"dds":0.0,"last_synced_commit":"d0b34e712bb420f82bf6a758f1cfa0b23716e67f"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KenKundert%2Fquantiphy_eval","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KenKundert%2Fquantiphy_eval/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KenKundert%2Fquantiphy_eval/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KenKundert%2Fquantiphy_eval/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KenKundert","download_url":"https://codeload.github.com/KenKundert/quantiphy_eval/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242152742,"owners_count":20080345,"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":["calculator","quantiphy"],"created_at":"2024-11-16T02:14:17.590Z","updated_at":"2025-03-06T05:25:44.270Z","avatar_url":"https://github.com/KenKundert.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"QuantiPhy Eval — Computations with Physical Quantities\n======================================================\n\n.. image:: https://pepy.tech/badge/quantiphy_eval/month\n    :target: https://pepy.tech/project/quantiphy_eval\n\n..  image:: https://github.com/KenKundert/quantiphy_eval/actions/workflows/build.yaml/badge.svg\n    :target: https://github.com/KenKundert/quantiphy_eval/actions/workflows/build.yaml\n\n.. image:: https://coveralls.io/repos/github/KenKundert/quantiphy_eval/badge.svg?branch=master\n    :target: https://coveralls.io/github/KenKundert/quantiphy_eval?branch=master\n\n.. image:: https://img.shields.io/pypi/v/quantiphy_eval.svg\n    :target: https://pypi.python.org/pypi/quantiphy_eval\n\n.. image:: https://img.shields.io/pypi/pyversions/quantiphy_eval.svg\n    :target: https://pypi.python.org/pypi/quantiphy_eval/\n\n:Author: Ken Kundert\n:Version: 0.5.0\n:Released: 2022-09-02\n\n\nA companion to `QuantiPhy \u003chttps://quantiphy.readthedocs.io\u003e`_, *quantiphy_eval* \nevaluates strings containing simple algebraic expressions that involve \nquantities. It returns a quantity.  For example::\n\n    \u003e\u003e\u003e from quantiphy_eval import evaluate\n\n    \u003e\u003e\u003e avg_price = evaluate('($1.2M + $1.3M)/2', '$')\n    \u003e\u003e\u003e print(avg_price)\n    $1.25M\n\n    \u003e\u003e\u003e avg_freq = evaluate('(122.317MHz + 129.349MHz)/2', 'Hz')\n    \u003e\u003e\u003e print(avg_freq)\n    125.83 MHz\n\n*QuantiPhy Eval* is used in `networth \u003chttps://github.com/KenKundert/networth\u003e`_ \nto allow you to give your estimated values using expressions that include \nnumbers that have units, SI scale factors, and commas.  That allows you the \nconvenience of copy-and-pasting your numbers from websites without being forced \nto reformat them.\n\nWith *QuantiPhy* the units do not survive operations, so you can specify the \nresolved units using the second argument.  In fact, the second argument is \npassed to *QuantiPhy* as the `model \n\u003chttps://quantiphy.readthedocs.io/en/stable/user.html#the-second-argument-the-model\u003e`_, \nwhich allows you to give the return value a name and description along with \nunits, as demonstrated in the next example.\n\nBy default *QuantiPhy Eval* provides no built-in constants.\nHowever, you can add your own constants::\n\n    \u003e\u003e\u003e from quantiphy import Quantity\n    \u003e\u003e\u003e from quantiphy_eval import evaluate, initialize\n    \u003e\u003e\u003e import math\n\n    \u003e\u003e\u003e my_constants = dict(\n    ...     k = Quantity('k'),\n    ...     q = Quantity('q'),\n    ...     T = Quantity('25°C', scale='K'),\n    ...     π = Quantity(math.pi),\n    ...     τ = Quantity(math.tau),\n    ... )\n    \u003e\u003e\u003e initialize(variables=my_constants)\n\n    \u003e\u003e\u003e Vt = evaluate('k*T/q', 'Vt V thermal voltage')\n    \u003e\u003e\u003e print(Vt.render(show_label='f'))\n    Vt = 25.693 mV — thermal voltage\n\nAlternatively, you can specify the model directly in the text passed to \n*evaluate*. Simply append it in the form of a double-quoted string::\n\n    \u003e\u003e\u003e Vt = evaluate('k*T/q \"Vt V thermal voltage\"')\n    \u003e\u003e\u003e print(Vt.render(show_label='f'))\n    Vt = 25.693 mV — thermal voltage\n\nYou can also use *evaluate* to assign values to names directly, *QuantiPhy Eval* \nremembers these values between calls to *evaluate*::\n\n    \u003e\u003e\u003e f_0 = evaluate('f₀ = 1MHz')\n    \u003e\u003e\u003e omega_0 = evaluate('ω₀ = τ*f₀ \"rads/s\"')\n    \u003e\u003e\u003e print(omega_0.render(show_label=True))\n    ω₀ = 6.2832 Mrads/s\n\nSimilarly, *QuantiPhy Eval* provides no built-in functions by default, but you \ncan add any you need::\n\n    \u003e\u003e\u003e def median(*args):\n    ...    args = sorted(args)\n    ...    l = len(args)\n    ...    m = l//2\n    ...    if l % 2:\n    ...        return args[m]\n    ...    return (args[m] + args[m-1])/2\n\n    \u003e\u003e\u003e initialize(functions = dict(median=median))\n    \u003e\u003e\u003e median_price = evaluate('median($636122, $749151, $706781)', '$')\n    \u003e\u003e\u003e print(median_price.fixed(show_commas=True))\n    $706,781\n\n*initialize* takes three arguments, *variables*, *functions* and *quantity*.  \nBoth *arguments* and *functions* take dictionaries that overwrite any previously \nsaved values. *quantity* takes a *quantiphy* *Quantity* class. The return value \nof *evaluate* will be an object of this class.\n\n*rm_commas* is a function for removing commas from an expression. This is used \nif your number contain commas. Simply stripping the commas it would prevent you \nfrom using multi-argument functions.  However after removing the commas \n*rm_commas* also converts semicolons to commas.  So the previous example could \nbe rewritten as::\n\n    \u003e\u003e\u003e from quantiphy_eval import evaluate, rm_commas\n\n    \u003e\u003e\u003e median_price = evaluate(\n    ...     rm_commas('median($636,122; $749,151; $706,781)'),\n    ...     '$',\n    ... )\n    \u003e\u003e\u003e print(median_price.fixed(show_commas=True))\n    $706,781\n\n*QuantiPhy Eval* supports comments. A ``#`` and anything that follows it to the \nend of the line is ignored::\n\n    \u003e\u003e\u003e average_price = evaluate(\n    ...     rm_commas('''\n    ...         median(\n    ...             $636,122 +   # Zillow\n    ...             $749,151 +   # Redfin\n    ...             $706,781     # Trulia\n    ...         )/3\n    ...     '''),\n    ...     '$'\n    ... )\n    \u003e\u003e\u003e print(average_price.fixed(show_commas=True, prec=2, strip_zeros=False))\n    $697,351.33\n\nFinally, *QuantiPhy Eval* uses `inform.Error \u003chttps://inform.readthedocs.io\u003e`_ \nfor error reporting::\n\n    \u003e\u003e\u003e from inform import Error\n\n    \u003e\u003e\u003e try:\n    ...     Vt = evaluate('kT/q', 'V')\n    ...     print(Vt)\n    ... except Error as e:\n    ...     print(str(e))\n    kT: variable unknown.\n\n\nReleases\n--------\n\n**Latest development release**:\n    | Version: 0.5.0\n    | Released: 2022-09-02\n\n**0.5 (2022-09-02)**:\n    - refactor the project structure\n    - provide *qe* example, a simple calculator\n\n**0.4 (2021-01-27)**:\n    - Add ability to explicitly specify units (or model) in evaluated string.\n\n**0.3 (2020-08-12)**:\n    - complete re-write, parser now implemented with ply rather than pyparsing.\n    - all built-in constants and functions have been removed.\n    - split *evaluate* into two: *evaluate* and *initialize*.\n\n**0.2 (2020-03-06)**:\n    - *rm_commas* now converts semicolons to commas\n    - support comments\n\n**0.1 (2020-03-05)**:\n    - Add support for user-defined constants and functions.\n    - add *rm_commas* function.\n\n**0.0 (2020-02-14)**:\n    Initial version.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkenkundert%2Fquantiphy_eval","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkenkundert%2Fquantiphy_eval","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkenkundert%2Fquantiphy_eval/lists"}