{"html_url": "https://github.com/simonw/datasette/issues/203#issuecomment-381763651", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/203", "id": 381763651, "node_id": "MDEyOklzc3VlQ29tbWVudDM4MTc2MzY1MQ==", "user": {"value": 45057, "label": "russss"}, "created_at": "2018-04-16T21:59:17Z", "updated_at": "2018-04-16T21:59:17Z", "author_association": "CONTRIBUTOR", "body": "Ah, I had no idea you could bind python functions into sqlite!\r\n\r\nI think the primary purpose of this issue has been served now - I'm going to close this and create a new issue for the only bit of this that hasn't been touched yet, which is (optionally) exposing units in the JSON API.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 313837303, "label": "Support for units"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/203#issuecomment-381315675", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/203", "id": 381315675, "node_id": "MDEyOklzc3VlQ29tbWVudDM4MTMxNTY3NQ==", "user": {"value": 45057, "label": "russss"}, "created_at": "2018-04-14T09:14:45Z", "updated_at": "2018-04-14T09:27:30Z", "author_association": "CONTRIBUTOR", "body": "> I'd like to figure out a sensible opt-in way to expose this in the JSON output as well. Maybe with a &_units=true parameter?\r\n\r\nFrom a machine-readable perspective I'm not sure why it would be useful to decorate the values with units. Edit: Should have had some coffee first. It's clearly useful for stuff like map rendering!\r\n\r\nI agree that the unit metadata should definitely be exposed in the JSON.\r\n\r\n> In #204 you said \"I'd like to add support for using units when querying but this is PR is pretty usable as-is.\" - I'm fascinated to hear more about how this could work.\r\n\r\nI'm thinking about a couple of approaches here. I think the simplest one is: if the column has a unit attached, optionally accept units in query fields:\r\n\r\n```python\r\ncolumn_units = ureg(\"Hz\") # Create a unit object for the column's unit\r\nquery_variable = ureg(\"4 GHz\") # Supplied query variable\r\n\r\n# Now we can convert the query units into column units before querying\r\nsupplied_value.to(column_units).magnitude\r\n> 4000000000.0\r\n\r\n# If the user doesn't supply units, pint just returns the plain\r\n# number and we can query as usual assuming it's the base unit\r\nquery_variable = ureg(\"50\")\r\nquery_variable\r\n> 50\r\n\r\nisinstance(query_variable, numbers.Number)\r\n> True\r\n```\r\n\r\nThis also lets us do some nice unit conversion on querying:\r\n\r\n```python\r\ncolumn_units = ureg(\"m\")\r\nquery_variable = ureg(\"50 ft\")\r\n\r\nsupplied_value.to(column_units)\r\n> \r\n```\r\n\r\nThe alternative would be to provide a dropdown of units next to the query field (so a \"Hz\" field would give you \"kHz\", \"MHz\", \"GHz\"). Although this would be clearer to the user, it isn't so easy - we'd need to know more about the context of the field to give you sensible SI prefixes (I'm not so interested in nanoHertz, for example).\r\n\r\nYou also lose the bonus of being able to convert - although pint will happily show you all the compatible units, it again suffers from a lack of context:\r\n\r\n```python\r\nureg(\"m\").compatible_units()\r\n> frozenset({,\r\n ,\r\n ,\r\n ,\r\n ,\r\n ,\r\n ,\r\n ,\r\n ,\r\n ,\r\n ,\r\n })\r\n```", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 313837303, "label": "Support for units"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/203#issuecomment-380966565", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/203", "id": 380966565, "node_id": "MDEyOklzc3VlQ29tbWVudDM4MDk2NjU2NQ==", "user": {"value": 45057, "label": "russss"}, "created_at": "2018-04-12T22:43:08Z", "updated_at": "2018-04-12T22:43:08Z", "author_association": "CONTRIBUTOR", "body": "Looks like [pint](https://pint.readthedocs.io/en/latest/tutorial.html) is pretty good at this.\r\n\r\n```python\r\nIn [1]: import pint\r\n\r\nIn [2]: ureg = pint.UnitRegistry()\r\n\r\nIn [3]: q = 3e6 * ureg('Hz')\r\n\r\nIn [4]: '{:~P}'.format(q.to_compact())\r\nOut[4]: '3.0 MHz'\r\n\r\nIn [5]: q = 0.3 * ureg('m')\r\n\r\nIn [5]: '{:~P}'.format(q.to_compact())\r\nOut[5]: '300.0 mm'\r\n\r\nIn [6]: q = 5 * ureg('')\r\n\r\nIn [7]: '{:~P}'.format(q.to_compact())\r\nOut[7]: '5'\r\n```", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 313837303, "label": "Support for units"}, "performed_via_github_app": null}